The widgets in this module were primarily created in function of the pyFormex GUI. The user can apply them to change the GUI or to add interactive widgets to his scripts. Of course he can also use all the Qt widgets directly.
Classes defined in module widgets
A single input item.
This is the base class for widgets holding a single input item. A single input item is any item that is treated as a unit and refered to by a single name.
This base class is rarely used directly. Most of the components of an InputDialog are subclasses of hereof, each specialized in some form of input data or representation. There is e.g. an InputInteger class to input an integer number and an InputString for the input of a string. The base class groups the functionality that is common to the different input widgets.
The InputItem widget holds a horizontal layout box (QHBoxLayout) to group its its components. In most cases there are just two components: a label with the name of the field, and the actual input field. Other components, such as buttons or sliders, may be added. This is often done in subclasses.
The constructor has one required argument: name. Other (optional) positional parameters are passed to the QtGui.QWidget constructor. The remaining keyword parameters are options that somehow change the default behavior of the InputItem class.
Parameters:
Subclasses should have an __init__() method which first constructs a proper widget for the input field, and stores it in the attribute self.input. Then the baseclass should be properly initialized, passing any optional parameters:
self.input = SomeInputWidget()
InputItem.__init__(self,name,*args,**kargs)
Subclasses should also override the following default methods of the InputItem base class:
Subclasses are allowed to NOT have a self.input attribute, IFF they redefine both the value() and the setValue() methods.
Subclasses can set validators on the input, like:
self.input.setValidator(QtGui.QIntValidator(self.input))
Subclasses can define a show() method e.g. to select the data in the input field on display of the dialog.
Return the name of the InputItem.
Return the displayed text of the InputItem.
Return the widget’s value.
Change the widget’s value.
An unchangeable input field with a label in front.
It is just like an InputString, but the text can not be edited. The value should be a simple string without newlines.
There are no specific options.
Return the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
Change the widget’s value.
An unchangeable information field.
The value is displayed as a string, but may contain more complex texts.
By default, the text format will be guessed to be either plain text, ReStructuredText ot html. Specify plain=True to display in plain text.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
Return the widget’s value.
A string input field with a label in front.
If the type of value is not a string, the input string will be eval’ed before returning.
Options:
Select all text on first display.
Return the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
Change the widget’s value.
A scrollable text input field with a label in front.
By default, the text format will be guessed to be either plain text, ReStructuredText ot html.
Specify plain=True to display in plain text.
If the type of value is not a string, the input text will be eval’ed before returning.
Select all text on first display.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A boolean input item.
Creates a new checkbox for the input of a boolean value.
Displays the name next to a checkbox, which will initially be set if value evaluates to True. (Does not use the label) The value is either True or False,depending on the setting of the checkbox.
Options:
Return the displayed text.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
A list selection InputItem.
A list selection is a widget allowing the selection of zero, one or more items from a list.
choices is a list/tuple of possible values. default is the initial/default list of selected items. Values in default that are not in the choices list, are ignored. If default is None or an empty list, nothing is selected initially.
By default, the user can select multiple items and the return value is a list of all currently selected items. If single is True, only a single item can be selected.
If maxh==-1, the widget gets a fixed height to precisely take the number of items in the list. If maxh>=0, the widget will get scrollbars when the height is not sufficient to show all items. With maxh>0, the item will get the specified height (in pixels), while maxh==0 will try to give the widget the required height to show all items
If check is True, all items have a checkbox and only the checked items are returned. This option sets single==False.
Mark the specified items as selected or not.
Mark the specified items as checked or not.
Return the widget’s value.
Change the widget’s value.
Mark all items as selected/checked.
Mark all items as not selected/checked.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A combobox InputItem.
A combobox is a widget allowing the selection of an item from a drop down list.
choices is a list/tuple of possible values. value is the initial/default choice. If value is not in the choices list, it is prepended.
The choices are presented to the user as a combobox, which will initially be set to the default value.
An optional onselect function may be specified, which will be called whenever the current selection changes. The function is passed the selected option string
Return the widget’s value.
Change the widget’s current value.
Change the widget’s choices.
This also sets the current value to the first in the list.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A radiobuttons InputItem.
Radio buttons are a set of buttons used to select a value from a list.
choices is a list/tuple of possible values. value is the initial/default choice. If value is not in the choices list, it is prepended. If value is None, the first item of choices is taken as the default.
The choices are presented to the user as a hbox with radio buttons, of which the default will initially be pressed. If direction == ‘v’, the options are in a vbox.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A pushbuttons InputItem.
Creates pushbuttons for the selection of a value from a list.
choices is a list/tuple of possible values. value is the initial/default choice. If value is not in the choices list, it is prepended. If value is None, the first item of choices is taken as the default.
The choices are presented to the user as a hbox with radio buttons, of which the default will initially be selected. If direction == ‘v’, the options are in a vbox.
Change the text on button index.
Change the icon on button index.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
An integer input item.
Options:
Select all text on first display.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A float input item.
Select all text on first display.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
An input item for tabular data.
value: a 2-D array of items, with nrow rows and ncol columns.
If value is an numpy array, the Table will use the ArrayModel: editing the data will directly change the input data array; all items are of the same type; the size of the table can not be changed.
Else a TableModel is used. Rows and columns can be added to or removed from the table. Item type can be set per row or per column or for the whole table.
autowidth:
additionally, all keyword parameters of the TableModel or ArrayModel may be passed
Return the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
Change the widget’s value.
An integer input item using a slider.
Options:
Return the name of the InputItem.
Return the displayed text of the InputItem.
Select all text on first display.
Return the widget’s value.
Change the widget’s value.
A float input item using a slider.
Options:
Return the name of the InputItem.
Return the displayed text of the InputItem.
Select all text on first display.
Return the widget’s value.
Change the widget’s value.
A 3D point/vector input item.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A vector of int values.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
A button input item.
The button input field is a button displaying the current value. Clicking on the button executes a function responsible for changing the value.
Extra parameters:
Return the widget’s value.
Set the value by calling the button’s func
Return the name of the InputItem.
Return the displayed text of the InputItem.
Change the widget’s value.
A color input item. Creates a new color input field with a label in front.
The color input field is a button displaying the current color. Clicking on the button opens a color dialog, and the returned value is set in the button.
Options:
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
An input item to select a font.
Return the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
Change the widget’s value.
An input item to select a file.
The following arguments are passed to the FileSelection widget: path,pattern,exist,multi,dir.
Return the widget’s value.
Return the name of the InputItem.
Return the displayed text of the InputItem.
An input item containing any other widget.
The widget should have:
The return value of this item is an ODict.
Return the displayed text.
Return the widget’s value.
Change the widget’s value.
Return the name of the InputItem.
An input form.
The input form is a layout box in which the items are layed out vertically. The layout can also contain any number of tab widgets in which items can be layed out using tab pages.
An scrolling input form.
The input form is a layout box in which the items are layed out vertically. The layout can also contain any number of tab widgets in which items can be layed out using tab pages.
A boxed group of InputItems.
Return the widget’s value.
Change the widget’s value.
A tab page in an input form.
A dialog widget to interactively set the value of one or more items.
Overview
The pyFormex user has full access to the Qt4 framework on which the GUI was built. Therefore he can built input dialogs as complex and powerful as he can imagine. However, directly dealing with the Qt4 libraries requires some skills and, for simple input widgets, more effort than needed.
The InputDialog class presents a unified system for quick and easy creation of common dialog types. The provided dialog can become quite sophisticated with tabbed pages, groupboxes and custom widgets. Both modal and modeless (non-modal) dialogs can be created.
Items
Each basic input item is a dictionary, where the fields have the following meaning:
For convenience, simple items can also be specified as a tuple. A tuple (key,value) will be transformed to a dict {‘key’:key, ‘value’:value}.
Other arguments
Add input items to form.
items is a list of input item data layout is the widget layout where the input widgets will be added
Add a Tab page of input items.
Add a group of input items.
Add a single input item to the form.
Hide the dialog and set the result code to TIMEOUT
Returns True if the result code was set to TIMEOUT
Show the dialog.
For a non-modal dialog, the user has to call this function to display the dialog. For a modal dialog, this is implicitely executed by getResult().
If a timeout is given, start the timeout timer.
Update the dialog’s return value from the field values.
This function is connected to the ‘accepted()’ signal. Modal dialogs should normally not need to call it. In non-modal dialogs however, you can call it to update the results without having to raise the accepted() signal (which would close the dialog).
Update a dialog from the data in given dictionary.
d is a dictionary where the keys are field names in the dialog. The values will be set in the corresponding input items.
Get the results from the input dialog.
This fuction is used to present a modal dialog to the user (i.e. a dialog that must be ended before the user can continue with the program. The dialog is shown and user interaction is processed. The user ends the interaction either by accepting the data (e.g. by pressing the OK button or the ENTER key) or by rejecting them (CANCEL button or ESC key). On accept, a dictionary with all the fields and their values is returned. On reject, an empty dictionary is returned.
If a timeout (in seconds) is given, a timer will be started and if no user input is detected during this period, the input dialog returns with the default values set. A value 0 will timeout immediately, a negative value will never timeout. The default is to use the global variable input_timeout.
The result() method can be used to find out how the dialog was ended. Its value will be one of ACCEPTED, REJECTED ot TIMEOUT.
A customized QListWidget with ability to compute its required size.
A model representing a two-dimensional array of items.
Make the table editable or not.
Return number of rows in the table
Return number of columns in the table
Return the data at the specified index
Return the type of the item at the specified position
Set the value of an individual table element.
This changes the stored data, not the interface.
Set the value of an individual table element.
Return the header data for the sepcified row or column
Insert row(s) in table
Remove row(s) from table
Return the TableModel flags.
A model representing a two-dimensional numpy array.
Make the table editable or not.
Return number of rows in the table
Return number of columns in the table
Return the type of the item at the specified position
Set the value of an individual table element.
Return the header data for the sepcified row or column
Return the TableModel flags.
A widget to show/edit a two-dimensional array of items.
data: a 2-D array of items, with nrow rows and ncol columns.
If data is an numpy array, the Table will use the ArrayModel: editing the data will directly change the input data array; all items are of the same type; the size of the table can not be changed.
Else a TableModel is used. Rows and columns can be added to or removed from the table. Item type can be set per row or per column or for the whole table.
label: currently unused (intended to display an optional label in the upper left corner if both chead and rhead are specified.
parent:
autowidth:
additionally, all other parameters for the initialization of the TableModel or ArrayModel may be passed
Return the width of the columns in the table
Return the height of the rows in the table
Update the table.
This method should be called to update the widget when the data of the table have changed. If autowidth is True, this will also adjust the column widths.
Return the Table’s value.
A file selection dialog.
The FileSelection dialog is a special purpose complex dialog widget that allows to interactively select a file or directory from the file system, possibly even multiple files, create new files or directories.
Parameters:
path: the path shown on initial display of the dialog. It should be an existing path in the file system. The default is ‘.’ for the current directory.
pattern: a string or a list of strings: specifies one or more UNIX glob patterns, used to limit the set of displayed filenames to those matching the glob. Each string can contain multiple globs, and an explanation string can be place in front:
'Image files (*.png *.jpg)'
The function utils.fileDescription() can be used to create some strings for common classes of files.
As a convenience, if a string starts with a ‘.’, the remainder of the string will be used as a lookup key in utils.fileDescription to get the actual string to be used. Thus, pattern=’.png’ will filter all ‘.png’ files, and pattern=’.img’ will filter all image files in any of the supported formats.
If a list of multiple strings is given, a combo box will allow the user to select between one of them.
exist: bool: if True, the filename must exist. The default will allow any new filename to be created.
multi: bool: if True, multiple files can be selected. The default is to allow only a single file.
dir: bool: if True, only directories can be selected. If dir evaluates to True, but is not the value True, either a directory or a filename can be selected.
button: string: the label to be displayed on the accept button. The default is set to ‘Save’ if new files are allowed or ‘Open’ if only existing files can be selected.
Return the selected value
Ask for a filename by user interaction.
Return the filename selected by the user. If the user hits CANCEL or ESC, None is returned.
A file selection dialog specialized for opening projects.
Return the selected value
Ask for a filename by user interaction.
Return the filename selected by the user. If the user hits CANCEL or ESC, None is returned.
A dialog for saving to an image file.
The dialog contains the normal file selection widget plus some extra fields to set the Save Image parameters:
Return the selected value
Ask for a filename by user interaction.
Return the filename selected by the user. If the user hits CANCEL or ESC, None is returned.
A dialog for selecting one or more items from a list.
This is a convenient class which constructs an input dialog with a single input item: an InputList. It allows the user to select one or more items from a list. The constructor supports all arguments of the InputDialog and the InputList classes. The return value is the value of the InputList, not the result of the InputDialog.
Mark the specified items as selected.
Return the selected items.
Show the modal dialog and return the list of selected values.
If the user cancels the selection operation, the return value is None. Else, the result is always a list, possibly empty or with a single value.
Add input items to form.
items is a list of input item data layout is the widget layout where the input widgets will be added
Add a Tab page of input items.
Add a group of input items.
Add a single input item to the form.
Hide the dialog and set the result code to TIMEOUT
Returns True if the result code was set to TIMEOUT
Show the dialog.
For a non-modal dialog, the user has to call this function to display the dialog. For a modal dialog, this is implicitely executed by getResult().
If a timeout is given, start the timeout timer.
Update the dialog’s return value from the field values.
This function is connected to the ‘accepted()’ signal. Modal dialogs should normally not need to call it. In non-modal dialogs however, you can call it to update the results without having to raise the accepted() signal (which would close the dialog).
Update a dialog from the data in given dictionary.
d is a dictionary where the keys are field names in the dialog. The values will be set in the corresponding input items.
Get the results from the input dialog.
This fuction is used to present a modal dialog to the user (i.e. a dialog that must be ended before the user can continue with the program. The dialog is shown and user interaction is processed. The user ends the interaction either by accepting the data (e.g. by pressing the OK button or the ENTER key) or by rejecting them (CANCEL button or ESC key). On accept, a dictionary with all the fields and their values is returned. On reject, an empty dictionary is returned.
If a timeout (in seconds) is given, a timer will be started and if no user input is detected during this period, the input dialog returns with the default values set. A value 0 will timeout immediately, a negative value will never timeout. The default is to use the global variable input_timeout.
The result() method can be used to find out how the dialog was ended. Its value will be one of ACCEPTED, REJECTED ot TIMEOUT.
A generic dialog widget.
The dialog is formed by a number of widgets stacked in a vertical box layout. At the bottom is a horizontal button box with possible actions.
A message box is a widget displaying a short text for the user.
The message box displays a text, an optional icon depending on the level and a number of push buttons.
When the MessageBox is displayed with the getResult() method, a modal dialog is created, i.e. the user will have to click a button or hit the ESC key before he can continue.
If you want a modeless dialog, allowing the user to continue while the message stays open, use the show() mehod to display it.
Add a check field at the bottom of the layout.
Display the message box and wait for user to click a button.
This will show the message box as a modal dialog, so that the user has to click a button (or hit the ESC key) before he can continue. Returns the text of the button that was clicked or an empty string if ESC was hit.
A message box is a widget displaying a short text for the user.
The message box displays a text, an optional icon depending on the level and a number of push buttons.
When the MessageBox is displayed with the getResult() method, a modal dialog is created, i.e. the user will have to click a button or hit the ESC key before he can continue.
If you want a modeless dialog, allowing the user to continue while the message stays open, use the show() mehod to display it.
Display a text and wait for user response.
Possible choices are ‘OK’ and ‘CANCEL’. The function returns True if the OK button was clicked or ‘ENTER’ was pressed, False if the ‘CANCEL’ button was pressed or ESC was pressed.
A box with action buttons.
Change the text on button index.
Change the icon on button index.
A widget displaying the coordinates of a point.
Return the current x,y,z values as a list of floats.
Set the three values of the widget.
A widget displaying an image.
Show an image in the viewer.
image: either a filename or an existing QImage instance. If a filename, it should be an image file that can be read by the QImage constructor. Most image formats are understood by QImage. The variable gui.image.image_formats_qtr provides a list.
Functions defined in module widgets
Create a pyFormex icon.
Returns a QIcon with an image taken from the pyFormex icons directory. icon is the basename of the image file (.xpm or .png).
Return the width and height of an object.
Returns a tuple w,h for any object that has width and height methods.
Return the maximum widget size.
The maximum widget size is the maximum size for a window on the screen. The available size may be smaller than the physical screen size (e.g. it may exclude the space for docking panels).
Add a timeout to a widget.
If timeout is positive, a timer will be installed into the widget which will call the timeoutfunc after timeout seconds have elapsed. The timeoutfunc can be any callable, but usually will emit a signal to make the widget accept or reject the input. The timeoutfunc will not be called is if the widget is destructed before the timer has finished.
Guess the InputItem type from the value
A convenience function to create an InputItem dictionary
A convenience function to create an InputItem dictionary
A convenience function to create an InputItem dictionary
Convert InputItem item to a dict or a widget.
This function tries to convert some old style or sloppy InputItem item to a proper InputItem item dict.
The conversion does the following:
Create an InputItem of any type, depending on the arguments.
Arguments: only name, value and itemtype are required
Update the input data fields with new data values
The data items with a name occurring as a key in newdata will have their value replaced with the corresponding value in newdata, unless this value is None.
The user should make sure to set only values of the proper type!
Ask the user to select a font.
A font selection dialog widget is displayed and the user is requested to select a font. Returns a font if the user exited the dialog with the OK button. Returns None if the user clicked CANCEL.
Create a color selection dialog and return the selected color.
col is the initial selection. If a valid color is selected, its string name is returned, usually as a hex #RRGGBB string. If the dialog is canceled, None is returned.
Update the text of a text display widget.
This function allows to display other text formats besides the plain text and html supported by the widget. Any format other than plain or html will be converted to one of these before sending it to the widget. Currently, we convert the following formats:
Add a set of action buttons to a layout
layout is a QLayout
actions is a list of tuples (name,) or (name,function). If a function is specified, it will be executed on pressing the button. If no function is specified, and name is one of ‘ok’ or ‘cancel’ (case is ignored), the button will be bound to the dialog’s ‘accept’ or ‘reject’ slot. If actions==None (default), it will be set to the default [('Cancel',),('OK',)].
Specify actions=[] if you want an empty dialogDuttons. default is the name of the action to set as the default. If no default is given, it is set to the LAST button.
Returns a horizontal box layout with the buttons.