source: orange/docs/extend-widgets/rst/owgui.rst @ 11049:f4dd8dbc57bb

Revision 11049:f4dd8dbc57bb, 15.6 KB checked in by Miha Stajdohar <miha.stajdohar@…>, 16 months ago (diff)

From HTML to Sphinx.

RevLine 
[11049]1#####################################
2OWGUI: Library of Common GUI Controls
3#####################################
4
5Orange Widgets wrap Orange's classes in an easy to use interactive graphical
6interface. As such, much of their code is about the interface, event control
7and maintaining the state of the GUI controls.
8
9In the spirit of constructive laziness, we wrote a library using which a single
10line of code can construct a check box, line edit or a combo, make it being
11synchronized with a Python object's attribute (which, by the way, gets
12automatically saved and retrieved when the widgets is closed and reopened),
13attaches a callback function to the control, make it disable or enable other controls...
14
15*****************
16Common Attributes
17*****************
18
19Many functions that construct controls share one or more common arguments,
20usually in the same order. These are described here. Descriptions of individual
21controls will only list their specific arguments, while the arguments which are
22presented here will only be described in cases where they have a different meaning.
23
24widget (required)
25Widget on which control will be drawn - can be widget's :obj:`controlArea` or another box.
26
27master (required)
28Object which includes an attribute that are used to store control's
29state, most often the object which called the function that
30initialized the control.
31
32value (required)
33String with the name of the master's attribute that synchronizes with the
34state of the control (and vice-versa - when this attribute is changed, the control changes as well). This attribute should usually be also included the master's :obj:`settingsList`, so that it is automatically saved and retrieved.
35
36box (default: None)
37Indicates if there should be a box that is drawn around the control. If :obj:`box` is :obj:`None`, no box is drawn; if it is a string, it is also used as box's name. If :obj:`box` is any other true value (such as :obj:`True` :), an unlabeled box is drawn.
38
39callback (default: None)
40A function to be called when the state of the control is changed. Can include a single function, or a list of functions that will be called in the order provided. If callback function changes the value of the controlled attribute (the one given as the :obj:`value` argument described above) it may trigger a cycle; a simple trick to avoid this is shown in the description of <a href="#listBox">listBox function</a>.
41
42tooltip (default: None)
43A string that is displayed in a tooltip that appears when mouse is over the control.
44
45label (default: None)
46A string that is displayed as control's label.
47
48labelWidth (default: None)
49Sets the label's width. This is useful for aligning the controls.
50
51orientation (default: "vertical")
52When label is used, determines the relative placement of the label and the control. Label can be above the control, "vertical", or in the same line with control, "horizontal". Instead of "vertical" and "horizontal" you can also use :obj:`True` and :obj:`False` or 1 and 0, respectively. (Remember this as "vertical" being the usual order of controls in the widgets, so vertical is "true".)
53
54disabled (default: False)
55Tells whether the control be disabled upon the initialization.
56
57addSpace (default: False)
58If true, a space of 8 pixels is added after the widget by calling :obj:`OWGUI.separator`. :obj:`addSpace` can also be an integer specifying the height of the added space.
59
60
61********
62Controls
63********
64
65This section describes the OWGUI wrappers for controls like check boxes, buttons
66and similar. All the important Qt's controls can be constructed through this
67functions. You should always use them instead of calling Qt directly, not only
68because they are convenient, but also because they set up a lot of things that happen in behind.
69
70
71Check Box
72*********
73
74Check box, a wrapper around QCheckBox, adding a label, box, tooltip, callback
75and synchronization with the designated widget's attribute.
76
77checkBox(widget, master, value, label[, box, tooltip, callback, disabled, labelWidth, disables])
78
79disables (default: [])
80If the check box needs to disable some other controls they can be given in list  :obj:`disables`, e.g. :obj:`disables=[someOtherCheckBox, someLineEdit]`. If the other control should be disabled when the checkbox is checked, do it like this: :obj:`disables=[someOtherCheckBox, (-1, someLineEdit)]` - now :obj:`someOtherCheckBox` will be enabled when this check box is checked, while :obj:`someLineEdit` will be enabled when the check box is unchecked.
81
82labelWidth (default: None)
83:obj:`labelWidth` can be used to align this widget with others.
84
85
86Line Edit
87*********
88
89Edit box, a wrapper around QLineEdit.
90
91lineEdit(widget, master, value[, label, labelWidth, orientation, box, tooltip, callback, valueType, validator, controlWidth])
92
93
94valueType (default: str)
95A type into which the value is cast.
96
97validator (default: None)
98A standard Qt validator that can be associated with the control.
99
100
101Button
102******
103
104A wrapper around QPushButton, just to be able to define a button
105and its callback in a single line.
106
107button(widget, master, label[, callback, disabled, tooltip])
108
109
110Radio Buttons
111*************
112
113OWGUI can create an individual radio button or a box of radio buttons or an individual radio button.
114
115An individual radio button is created by :obj:`radioButton`.
116
117radioButton(widget, master, value, label[, box, tooltip, callback, addSpace])
118
119The function provides the usual capabilities of OWGUI controls. It is though
120your responsibility to put it in something like a :obj:`QVButtonGroup`.
121
122A box of radio buttons is created by function :obj:`radioButtonsInBox`.
123
124
125radioButtonsInBox(widget, master, value, btnLabels[, box, tooltips, callback)
126
127value (required)
128Synchronized with the index of the selected radio button.
129
130btnLabels (required)
131A list with labels for radio buttons. Labels can be strings or pixmaps.
132
133tooltips (default: None)
134A list of tooltips, one for each button.
135
136
137Combo Box
138*********
139
140A wrapper around QComboBox.
141
142comboBox(widget, master, value[, box, label, labelWidth, orientation, items, tooltip, callback, sendSelectedValue, valueType, control2attributeDict, emptyString])
143
144<dl class="attributes">
145items (default: [])
146A list of combo box's items. Unlike most OWGUI, :obj:`items` have one Orange-specific quirk: its element can be either a string, in which case it is used as a label, or a tuple, where the first element is a label name and the last is the attribute type which is used to create an icon. Most attribute lists in Orange Widgets are constructed this way.
147
148sendSelectedValue (default: 0)
149If false, attribute :obj:`value` will be assigned the index of the selected item. Otherwise, it is assigned the currently selected item's label.
150
151control2attributeDict (default: {})
152A dictionary for translating the item's label into :obj:`value`. It is used only is :obj:`sendSelectedValue` is true, and even then a label is translated only if an item with such a key is found in the dictionary; otherwise, label is written to :obj:`value` as it is.
153
154emptyString (default: "")
155Tells which combo box's item corresponds to an empty :obj:`value`. This is typically used when combo box's labels are attribute names and an item "(none)", which allows user to select no attribute. If we give :obj:`emptyString="(none)"`, :obj:`value` will be an empty string when the user selects "(none)". This is equivalent to specifying :obj:`control2attributeDict = {"(none)": ""}` (and is actually implemented like that), but far more convenient.
156
157valueType (default: str or unicode)
158A function through which the currently selected item's label is converted prior to looking into :obj:`control2attributeDict`. Needed to convert Qt's QString.
159
160
161List Box
162********
163
164This control, which might be the most complicated control in OWGUI, is a
165sophisticated wrapper around QListBox. It's complexity arises from synchronization.
166
167
168listBox(widget, master, value, labels[, box, tooltip, callback, selectionMode])
169
170<dl class="attributes">
171value (required)
172The name of master's attribute containing indices of all selected values.
173
174labels (required)
175The name of master's attribute containing the list box's labels. Similar to :obj:`items` in combo box, list :obj:`labels` have one Orange-specific quirk: its element can be either a string, in which case it is used as a label, or a tuple, where the first element is a label name and the second can be either an icon on an integer, representing the attribute type which is used to create an icon. Most attribute lists in Orange Widgets are constructed this way.
176
177selectionMode (default: QListWidget.SingleSelection)
178Tells whether the user can select a single item (:obj:`QListWidget.SingleSelection`), multiple items (:obj:`QListWidget.MultiSelection`, :obj:`QListWidget.ExtendedSelection`) or nothing (:obj:`QListWidget.NoSelection`).
179
180
181:obj:`value` is automatically cast to :obj:`OWGUI.ControlledList` (this is needed because the list should report any changes to the control, the list box; :obj:`OWGUI.ControlledList` is like an ordinary Python :obj:`list` except that it triggers synchronization with the list box at every change).
182
183:obj:`labels` is only partially synchronized with the list box: if a new list is assigning to :obj:`labels` attribute, the list will change. If elements of the existing list are changed or added, the list box won't budge. You should never change the list, but always assign a new list (or reassign the same after it's changed). If the labels are stored in :obj:`self.listLabels` and you write :obj:`self.listLabels[1]="a new label"`, the list box won't change. To trigger the synchronization, you should continue by :obj:`self.listLabels = self.listLabels`. This may seem awkward, but by our experience a list of selected items is seldom changed changed "per-item", so we were too lazy to write the annoyingly complex backward callbacks.
184
185<span>
186<span onclick="toggleVisibility(this);" class="hideshow">Show Example</span>
187<span class="hideshow"><a href="gui_listbox.py">Download example (gui_listbox.py)</a></span>
188<span class="hideshow"><a href="gui_listbox_attr.py">Download example (gui_listbox_attr.py)</a></span>
189
190
191Spin
192****
193
194Spin control, a wrapper around QSpinBox.
195
196spin(widget, master, value, min, max[, step, box, label, labelWidth, orientation, tooltip, callback, controlWidth])
197
198
199min, max, step=1
200Minimal and maximal value, and step.
201
202
203Slider
204******
205
206A wrapper around QSlider that allows user setting a numerical value between the given bounds.
207
208hSlider(widget, master, value[, box, minValue, maxValue, step, callback, labelFormat, ticks, divideFactor])
209
210
211minValue (default: 0), maxValue (default: 10), step (default: 1)
212Minimal and maximal value for the spin control, and its step.
213
214ticks (default: 0)
215If non-zero, it gives the interval between two ticks. The ticks will appear below the groove.
216
217labelFormat (default: " %d")
218Defines the look of the label on the righthand side of the slider. It has to contain one format character (like %d in the default), but can contain other text as well.
219
220divideFactor (default: 1.0)
221The value printed in the label is divided by :obj:`divideFactor`.
222
223
224For an example of usage, see the second example in the description of <a href="#labels-example">labels</a>.
225
226
227Check Box with Spin
228*******************
229
230Check box with spin, or, essentially, a wrapper around
231OWGUI.checkBox and OWGUI.spin.
232
233checkWithSpin(widget, master, label, min, max, checked, value[, posttext, step, tooltip, checkCallback, spinCallback, labelWidth])
234
235min, max, step (required)
236Minimal and maximal value for the spin control, and its step.
237
238checked (required)
239Master's attribute that is synchronized with the state of the check box.
240
241value (required)
242The attribute that is synchronized with the spin.
243
244posttext (default: None)
245Text which appears on the right-hand side of the control.
246
247checkCallback (default: None), spinCallback (default: None)
248Function that are called when the state of the check box or spin changes.
249
250
251Labels
252******
253
254There are two functions for constructing labels. The first is a simple wrapper around QLabel which differs only in allowing to specify a fixed width without needing an extra line. Note that unlike most other OWGUI widgets, this one does not have the argument :obj:`master`.
255
256widgetLabel(widget, label[, labelWidth])
257
258The second is a label which can synchronize with values of master widget's attributes.
259
260label(widget, master, label[, labelWidth])
261
262label
263:obj:`label` is a format string following Python's syntax (see the corresponding Python documentation): the label's content is rendered as :obj:`label % master.__dict__`.
264
265
266*********
267Utilities
268*********
269
270Widget box
271**********
272
273
274widgetBox(widget, box=None, orientation='vertical', addSpace=False)
275Creates a box in which other widgets can be put. If :obj:`box` is given and not false, the box will be framed. If :obj:`box` is a string, it will be used for the box name (don't capitalize each word; spaces in front or after the string will be trimmed and replaced with a single space). Argument :obj:`orientation` can be :obj:`"vertical"` or :obj:`"horizontal"` (or :obj:`True` and :obj:`False`, or :obj:`1` and :obj:`0`, respectively).
276
277
278Idented box
279***********
280
281
282indentedBox(widget, sep=20)
283Creates an indented box. Widgets which are subsequently put into that box will be arranged vertically and aligned with an indentation of :obj:`sep`.
284
285
286Inserting Space between Widgets
287*******************************
288
289Most widgets look better if we insert some vertical space between the controls
290or groups of controls. A few functions have an optional argument :obj:`addSpace`
291by which we can request such space to be added. For other occasions, we can use
292the following two functions.
293
294separator(widget, width=0, height=8)
295
296Function :obj:`separator` inserts a fixed amount of space into :obj:`widget`.
297Although the caller can specify the amount, leaving the default will help the
298widgets having uniform look.
299
300rubber(widget[, orientation="vertical"])
301
302Similar to separator, except that the size is (1, 1) and that it expands in the
303specified direction if the widget is expanded. Most widgets should have rubber
304somewhere in their :obj:`controlArea`.
305
306Attribute Icons
307***************
308
309getAttributeIcons()
310
311Returns a dictionary with attribute types (:obj:`orange.VarTypes.Discrete`,
312:obj:`orange.VarTypes.Continuous`, :obj:`orange.VarTypes.String`, -1) as keys
313and colored pixmaps as values. The dictionary can be used in list and combo
314boxes showing attributes for easier distinguishing between attributes of different types.
315
316Send automatically / Send
317*************************
318
319Many widgets have a "Send" button (perhaps named "Apply", "Commit"...) accompanied with a check box "Send automatically", having the same effect as if the user pressed the button after each change. A well behaved widget cares to:
320
321* disable the button, when the check box is checked;
322* when the user checks the check box, the data needs to be send (or the changes applied), but only if there is any pending change which has not been (manually) sent yet.
323
324Programming this into every widget is annoying and error-prone; at the time when the function described here was written, not many widgets actually did this properly.
325
326setStopper(master, sendButton, stopCheckbox, changedFlag, callback)
327
328sendButton
329The button that will be disabled when the check box is checked.
330
331stopCheckbox
332Check box that decides whether the changes are sent/commited/applied automatically.
333
334changedFlag
335The name of the :obj:`master`'s attribute which tells whether there is a change which has not been sent/applied yet.
336
337callback
338The function that sends the data or applies the changes. This is typically the function which is also used as the :obj:`sendButton`'s callback.
339
340
341:obj:`setStopper` is a trivial three lines long function which connects a few signals. Its true importance is in enforcing the correct procedure for implementing such button-check box combinations. Make sure to carefully observe and follow the example provided below.
342
Note: See TracBrowser for help on using the repository browser.