4.33. Client code execution

All the Python code you write inside the application XML file or dialogs XML files is executed at the client side. The communications with the server are achieved via the XML-RPC protocol through the Rpc handler.

There are two namespaces, the first is global and available from the application code and the dialogs code. The second namespace is available only from dialogs code. Each dialog has his own namespace.

The following functions are available everywhere :

AddMenu(name, label=None, icon=None, parent=None, qmenu=None, before=None)
Add a new menu named name labeled label with an icon specified in icon and with the given parent. You can pass a QMenu object in qmenu if you have an already created menu. To insert the menu before another menu, specify its name in before.
ShowMenu(menu)
Show menu.
HideMenu(menu)
Hide menu.
EnableMenu(menu)
Enable menu.
DisableMenu(menu)
Disable menu.
DelMenu(menu)
Delete menu.
AddMenuItem(menu, label=None, icon=None, shortcut=None, group=None, callback=None, name=None, before=None)
Add an item to menu with a label, an icon and a shortcut. The item can be member of group and named name. When the item is clicked, callback function is called. To insert the item before another item, specify its name in before.
ShowMenuItem(item)
Show item.
HideMenuItem(item)
Hide item.
EnableMenuItem(item)
Enable item.
DisableMenuItem(item)
Disable item.
DelMenuItem(item)
Delete item.
AddToolBar(name, label=None, area='top', allowed_areas='all', before=None)
Add a toolbar named name labeled label. area defines the area on which the toolbar is created, possible values are : left, right, top and bottom. allowed_areas defines areas on which the toolbar can be moved, possible values are : all, left, right, top and bottom. To insert the toolbar before another toolbar, specify its name in before.
ShowToolBar(toolbar)
Show toolbar.
HideToolBar(toolbar)
Hide toolbar.
EnableToolBar(toolbar)
Enable toolbar.
DisableToolBar(toolbar)
Disable toolbar.
DelToolBar(toolbar)
Delete toolbar.
AddToolBarItem(toolbar, label=None, icon=None, shortcut=None, group=None, callback=None, name=None, before=None)
Add an item to toolbar with a label, an icon and a shortcut. The item can be member of group and named name. When the item is clicked, callback function is called. To insert the item before another item, specify its name in before.
ShowToolBarItem(item)
Show item.
HideToolBarItem(item)
Hide item.
EnableToolBarItem(item)
Enable item.
DisableToolBarItem(item)
Disable item.
DelToolBarItem(item)
Delete item.
AddDock(dialog_name, dialog_data=None, label=None, area='left', allowed_areas='all', features='all')
Add a dock labeled label and put the dialog named dialog_name inside. dialog_data is the data attached to the dialog. area defines the area on which the dock is created, possible values are : left, right, top and bottom. allowed_areas defines areas on which the dock can be moved, possible values are : all, left, right, top and bottom. features defines the features of the dock, possible values are : all, close, move, float.
ShowDock(dialog_name)
Show the dock containing the dialog named dialog_name.
HideDock(dialog_name)
Hide the dock containing the dialog named dialog_name.
EnableDock(dialog_name)
Enable the dock containing the dialog named dialog_name.
DisableDock(dialog_name)
Disable the dock containing the dialog named dialog_name.
DelDock(dialog_name)
Delete the dock containing the dialog named dialog_name.
BuildDialog(dialog_name, dialog_data=None, duplicate=False)
Build and return the dialog named dialog_name and attach dialog_data to it. The attached data is available via the global variable DialogData defined in the dialog's namespace. If duplicate is set to True, you can create more than one instance of the same dialog.
ShowDialog(dialog_name, dialog_data=None, duplicate=False)
Build and show the dialog named dialog_name and attach dialog_data to it. The attached data is available via the global variable DialogData defined in the dialog's namespace. If duplicate is set to True, you can show more than one instance of the same dialog.
Connect()
Connect is just a shortcut to the connect Qt function available for convenience.
XmlEscape(string)
This function returns a suitable version of string that can be used inside XML data.
Qstr(string)
This function returns a QString object initialized with string.
Obj2Str(object)
This function returns an ascii representation of object that can be used where binary data must be avoided.
Str2Obj(string)
Converts string back to the original object. This function is used in conjunction with Obj2Str.
MsgBox(data)
This function is used to display different types of message boxes. data can be a simple text that will be displayed or it can be in the form : "#msg= #title= #icon=question|info|warn|error] #buttons=Bt1, Bt2, ...". This function returns the clicked button index.
Tr(string)
Translate the string using the internal translator. You can use Tr when initiating Dialog object.
Xpm(item)
Returns a QPixmap initialized from item. item is interpreted like a Pixmap input value.
SBText(text, show_spinning=None)
Set the status bar's text to text. if show_spinning is True a spinning animation appears, otherwise it is hidden. If show_spinning is None (which is the default) the spinning animation is untouched.
SBProgress(value, total=None)
Set the status bar's progress bar value to value. total can be an integer indicating the maximum value of the progress bar, it can also be a list of two integers indicating the minimum and maximum values of the progress bar.
SBClear
This function clears status bar's text and resets progress bar values.
Event(function, *args, **kwargs)
This function creates and adds a QEvent to an event queue and returns immediately. The event will execute function with optional *args and/or **kwargs inside the Qt's main event loop. It makes sense to use this function only from other threads.
Thread(function, *args, **kwargs)
Thread is a class representing a worker thread that executes in parallel with the main thread (which is the Qt's main thread) the function with optional *args and/or **kwargs, the thread starts upon creation. The Thread class inherits QThread Qt class. When the thread is finished, the return value of the execution is available via the ret attribute, you can use the function isFinished() to query the thread's activity.
ProcessEvents(exclude_user_input=True, sleep_time=0.01)
This function processes all pending events for the calling thread until there are no more events to process, and sleeps for sleep_time seconds. if exclude_user_input is set to False, mouse and keyboard events are also processed. You can call this function occasionally when your program is busy performing a long operation, to prevent screen freeze for example.
WaitCursor(show_wait_cursor)
This function displays a wait cursor if show_wait_cursor is True, otherwise it restores the normal cursor.
HandleException(caller=None, show_tb=True, show_code=True, show_break=True, foreign_tb=None)
This function can be executed from an except block to display an error message box. If show_tb is set to False, the execution traceback is not available. If show_code is set to False, the code line that triggers the error is not displayed. If show_break is set to False, the Break button will not be available. Others arguments are internal. The common usage is to call this function with no arguments.
Error(error_msg='')
This function can be called anywhere in you Python code to stops the execution and displays error_msg in a message box.
Exit()
This function stops silently the code execution.
Quit()
Quit PyDADL application.

The following attributes are available everywhere :

ApplicationId
The internal PyDADL application's ID.
ApplicationName
The PyDADL application's name.
ServerIP
The PyDADL server's IP address.
ServerPort
The PyDADL server's TCP port.
Lang
The language selected by the user.
User
The username connected.
UserFlags
The user's flags (permissions and attributes).
AppValues
The return value from the XML-RPC function defined in values_call attribute in the application XML file.
Dialogs
Dialogs is a Dict that holds the displayed dialogs, the dict keys corresponds to the dialog's names.
Debug
A boolbean value that indicates if the debug mode is active.
Rpc
Rpc is the XML-RPC client handler that is used to execute remote XML-RPC calls. The syntax is Rpc.function_name(arg1, arg2, ..., argN), The return value is the result of the remote call.
MdiArea
MdiArea is the QMdiArea instance which is the central widget of the PyDADL client. MdiArea is made public so you can show your own widgets within the MDI area via the addSubWindow function. MdiArea may not be defined if you create the client's main window with Qt Designer without an MDI area.
MainWindow
MainWindow is the main client's widget. MainWindow is normally an instance of QMainWindow unless you choose to use a Qt Designer .ui file which is not a main window.

The following function is available only in the dialog code :

Close(r=0)
This function closes the dialog and sets the return value to r.

The following attributes are available only in the dialog code :

d
d is the current Dialog object. Widget's objects are available through d, ex: d.line_widget_1.
DialogName
The dialog's name.
DialogValues
The return value from the XML-RPC function defined in values_call.
DialogData
The data attached to the dialog. Data is attached to a dialog upon creation via the function showDialog.
[Note]Note
Instead of writing Python code inside a tag, you can include an external file using the syntax include:<filename>. It can be usefull if you have to write a long code with many character prohibited in XML tag values. When you use the include statement, you cannot write additional Python code in the tag.