Advanced dialogs (Python)¶

Advanced dialogs (Python)¶
  1previewFile1 = r"d:\\preview1.ipd"
  2previewFile2 = r"d:\\preview2.ipd"
  3
  4pictureFile1 = r"d:\\picture1.jpg"
  5pictureFile2 = r"d:\\picture2.jpg"
  6
  7# Define a function to describe a constant from the ipDialogCallbackReason enumeration
  8def DescribeCallbackReason( reason ):
  9    if reason == ipDialogCallbackReason.dcrFieldSelected:
 10        Str = "Field Selected"
 11    elif reason == ipDialogCallbackReason.dcrFieldCurrent:
 12        Str = "Field Current"
 13    elif reason == ipDialogCallbackReason.dcrFieldNonCurrent:
 14        Str = "Field Non Current"
 15    elif reason == ipDialogCallbackReason.dcrDialogClosing:
 16        Str = "DialogClosing"
 17    DescribeCallbackReason = Str
 18    return locals().get("DescribeCallbackReason")
 19
 20# Define a function to describe a constant from the ipDialogButtonType enumeration
 21def DescribeDialogButtonType( buttonType ):
 22    if buttonType == ipDialogButtonType.dbtUnknown:
 23        Str = "Unknown"
 24    elif buttonType == ipDialogButtonType.dbtOk:
 25        Str = "Ok"
 26    elif buttonType == ipDialogButtonType.dbtCancel:
 27        Str = "Cancel"
 28    elif buttonType == ipDialogButtonType.dbtYes:
 29        Str = "Yes"
 30    elif buttonType == ipDialogButtonType.dbtNo:
 31        Str = "No"
 32    elif buttonType == ipDialogButtonType.dbtNext:
 33        Str = "Next"
 34    elif buttonType == ipDialogButtonType.dbtPrevious:
 35        Str = "Previous"
 36    elif buttonType == ipDialogButtonType.dbtClose:
 37        Str = "Close"
 38    elif buttonType == ipDialogButtonType.dbtPrint:
 39        Str = "Print"
 40    elif buttonType == ipDialogButtonType.dbtCustom:
 41        Str = "Custom"
 42    DescribeDialogButtonType = Str
 43    return locals().get("DescribeDialogButtonType")
 44
 45# Define a function to describe a constant from the ipDialogFieldType enumeration
 46def DescribeDialogFieldType( fieldType ):
 47    if fieldType == ipDialogFieldType.dftUnknown:
 48        Str = "Unknown"
 49    elif fieldType == ipDialogFieldType.dftAngle:
 50        Str = "Angle"
 51    elif fieldType == ipDialogFieldType.dftArea:
 52        Str = "Area"
 53    elif fieldType == ipDialogFieldType.dftCheckBox:
 54        Str = "Check Box"
 55    elif fieldType == ipDialogFieldType.dftComboBox:
 56        Str = "Combo Box"
 57    elif fieldType == ipDialogFieldType.dftDate:
 58        Str = "Date"
 59    elif fieldType == ipDialogFieldType.dftDistance:
 60        Str = "Distance"
 61    elif fieldType == ipDialogFieldType.dftInteger:
 62        Str = "Integer"
 63    elif fieldType == ipDialogFieldType.dftLine:
 64        Str = "Line"
 65    elif fieldType == ipDialogFieldType.dftListBox:
 66        Str = "List Box"
 67    elif fieldType == ipDialogFieldType.dftPreview:
 68        Str = "Preview"
 69    elif fieldType == ipDialogFieldType.dftRadioGroup:
 70        Str = "Radio Group"
 71    elif fieldType == ipDialogFieldType.dftReal:
 72        Str = "Real"
 73    elif fieldType == ipDialogFieldType.dftText:
 74        Str = "Text"
 75    elif fieldType == ipDialogFieldType.dftTextBox:
 76        Str = "Text Box"
 77    elif fieldType == ipDialogFieldType.dftTime:
 78        Str = "Time"
 79    elif fieldType == ipDialogFieldType.dftTwinList:
 80        Str = "Twin List"
 81    elif fieldType == ipDialogFieldType.dftVolume:
 82        Str = "Volume"
 83    elif fieldType == ipDialogFieldType.dftWeight:
 84        Str = "Weight"
 85    elif fieldType == ipDialogFieldType.dftPicture:
 86        Str = "Picture"
 87    elif fieldType == ipDialogFieldType.dftButton:
 88        Str = "Button"
 89    DescribeDialogFieldType = Str
 90    return locals().get("DescribeDialogFieldType")
 91
 92# Define a sub routine to display a list of items in the output toolbox
 93def DisplayListValues(message, list):
 94    ot.add(message)
 95
 96    for item in list:
 97        ot.add(item)
 98
 99# Create a local variable for the impact impact.gui object
100g = impact.gui
101
102# Create a local variable for the output toolbox
103ot = g.output_tool_box
104
105# Create a dialog
106d = g.make_dialog( "Options" )
107
108# Check the dialog was created
109if not d.isNone():
110
111    # Create a local variable for the fields of the dialog
112    fields = d.fields
113
114    # Build up the controls to display in the dialog
115    d.add_button(ipDialogButtonType.dbtOk)
116
117    typesTab = d.add_tab( "Types" )
118
119    fields.add_line( "Simple Types" )
120    checkBoxField = fields.add_check_box( False, "Boolean" )
121    checkBoxField.value = True
122    checkBoxField.use_callback = True
123    integerField = fields.add_integer( 5, "Integer" )
124    integerField.value = 2
125    integerField.use_callback = True
126    realField = fields.add_real( 91.2, "Real" )
127    realField.value = 6.23
128    realField.use_callback = True
129    textField = fields.add_text( "Simple text", "Text" )
130    textField.value = "Modified simple text"
131    textField.case = ipDialogStringCase.dscAll
132    textField.chars = ipDialogStringChars.dshAll
133    shortTextField = fields.add_text( "AA123", "Short text" )
134    shortTextField.max_length = 5
135    shortTextField.case = ipDialogStringCase.dscUpper
136    shortTextField.chars = ipDialogStringChars.dshAlphaNumeric
137    shortTextField.use_callback = True
138    textBoxField = fields.add_text_box( "Advanced text" + "\r\n" + "New Line", "Text Box" )
139    textBoxField.value = "Modified advanced text" + "\r\n" + "New Line"
140    textBoxField.use_callback = True
141    dateField = fields.add_date( Date, "Date" )
142    dateField.value = Date
143    dateField.use_callback = True
144    timeField = fields.add_time( Time, "Time" )
145    timeField.value = Time
146    timeField.use_callback = True
147
148    fields.add_line( "Advanced Types" )
149    distanceField = fields.add_distance( -200.0, "Distance", True )
150    distanceField.value = 100.0
151    distanceField.hint = "Enter a distance"
152    distanceField.use_callback = True
153    angleField = fields.add_angle( 45.0, "Angle" + "\r\n" + "(use 90 if possible)" )
154    angleField.value = 180.0
155    angleField.use_callback = True
156    weightField = fields.add_weight( 9.2, "Weight" )
157    weightField.value = 64.5
158    weightField.use_callback = True
159    areaField = fields.add_area( 8.6, "Area" )
160    areaField.value = 23.9
161    areaField.use_callback = True
162    volumeField = fields.add_volume( 2.4, "Volume" )
163    volumeField.value = 15.2
164    volumeField.use_callback = True
165    buttonField = fields.add_button( "Click Me!" )
166
167    buttonField.hint = "Click this button to show how a callback is triggered"
168
169    # Adds a checkbox field to control the closing of the dialog
170    allowCloseField = fields.add_check_box( False, "Allow close on OK" )
171    allowCloseField.value = True
172    allowCloseField.use_callback = True
173
174    listsTab = d.add_tab( "Lists" )
175
176    fruits = [None] * 8
177    fruits[0] = "Orange"
178    fruits[1] = "Apple"
179    fruits[2] = "Banana"
180    fruits[3] = "Lemon"
181    fruits[4] = "Pineapple"
182    fruits[5] = "Kiwi"
183    fruits[6] = "Strawberry"
184    fruits[7] = "Lime"
185
186    fields.add_line( "Select a single fruit from this list" )
187    comboBoxField = fields.add_combo_box( "Lemon", "Fruit Combo", fruits )
188    # Set comboBoxField = fields.add_combo_box( 3, "Fruit Combo", fruits, False )
189    # comboBoxField.value = 4
190    comboBoxField.value = "Kiwi"
191    comboBoxField.use_callback = True
192
193    selection = [None] * 8
194    selection[0] = False
195    selection[1] = True
196    selection[2] = False
197    selection[3] = True
198    selection[4] = False
199    selection[5] = True
200    selection[6] = False
201    selection[7] = True
202
203    fields.add_line( "Select none or more fruits from this list box" )
204    listBoxField = fields.add_list_box( fruits, selection, True, 5 )
205    listBoxField.height = 5
206
207    fields.add_line( "Select a single fruit from this group" )
208    radioGroupField = fields.add_radio_group( "Apple", "Fruit Group", fruits )
209    # Set radioGroupField = fields.add_radio_group( 1, "Fruit Group", fruits )
210    # radioGroupField.value = 2
211    radioGroupField.value = "Banana"
212    radioGroupField.use_callback = True
213
214    fields.add_line( "Select none or more fruits from this twin list" )
215    twinListField = fields.add_twin_list( "Left", "Right", fruits, selection, 6 )
216    twinListField.height = 6
217
218    previewTab = d.add_tab( "Preview" )
219
220    previews = [None] * 2
221    previews[0] = "Preview 1"
222    previews[1] = "Preview 2"
223    previewGroupField = fields.add_radio_group( "Preview 1", "Preview Group", previews )
224    previewGroupField.use_callback = True
225    previewField = fields.add_preview( previewFile1, "One up", True, ipDialogPreviewOrientation.dpoVertical )
226
227    pictureTab = d.add_tab( "Picture" )
228    pic1Opts = impact.gui.create_dialog_picture_options()
229
230    pic1Opts.width = 320
231    pic1Opts.height = 240
232
233    pic1Opts.command_panel_position = ipDialogPictureCommandsPosition.dpcpRightTop
234    pic1Opts.command_panel_always_visible = True
235    pic1Field = fields.add_picture(pictureFile1, "Picture1", pic1Opts)
236
237    pic1Field.use_callback = True
238    loadPic1Button = fields.add_button("load Picture 1")
239    loadPic2Button = fields.add_button("load Picture 2")
240
241    # Display the type of each field in the output toolbox
242    for field in fields:
243
244        ot.add(DescribeDialogFieldType( field.field_type ))
245    # Show the dialog with a callback handler
246    buttonPressed = d.show_modal( GetRef("Callback") )
247
248    if buttonPressed == ipDialogButtonType.dbtOk:
249
250        # Display the values from the dialog
251        ot.add(checkBoxField.label + ": " + checkBoxField.value)
252        ot.add(integerField.label + ": " + integerField.value)
253        ot.add(realField.label + ": " + realField.value)
254        ot.add(textField.label + ": " + textField.value)
255        ot.add(textBoxField.label + ": " + textBoxField.value)
256        ot.add(dateField.label + ": " + dateField.value)
257        ot.add(timeField.label + ": " + timeField.value)
258
259        ot.add(distanceField.label + ": " + distanceField.value)
260        ot.add(angleField.label + ": " + angleField.value)
261        ot.add(weightField.label + ": " + weightField.value)
262        ot.add(areaField.label + ": " + areaField.value)
263        ot.add(volumeField.label + ": " + volumeField.value)
264
265        ot.add(radioGroupField.label + ": " + radioGroupField.value)
266        ot.add(radioGroupField.label + ": " + radioGroupField.selected_item)
267
268        ot.add(comboBoxField.label + ": " + comboBoxField.value)
269        ot.add(comboBoxField.label + ": " + comboBoxField.selected_item)
270
271        DisplayListValues("Listbox selected values:", listBoxField.selected_items)
272        DisplayListValues("Listbox selected indicies:", listBoxField.selected_indices)
273        DisplayListValues("Twinlist selected values:", twinListField.selected_items)
274        DisplayListValues("Twinlist selected indicies:", twinListField.selected_indices)
275
276        ot.add(pic1Field.label + ": " + pic1Field.picture_file_name)
277
278# Define a callback, passed into the ShowModal method of IDialog
279def Callback(callbackParams):
280    field = callbackParams.field
281
282    if not field.isNone():
283
284        ot.add(field.label + ": " + DescribeCallbackReason( callbackParams.reason ))
285
286        if field.field_type == ipDialogFieldType.dftPicture:
287            ot.add("HasImage: " + str(field.picture_has_image))
288        else:
289            ot.add("Value: " + field.value)
290
291        if field.label == "Click Me!":
292
293            impact.gui.show_message(ipShowMessageType.smtInfo, "You clicked me!", None)
294
295        elif field.label == "Preview Group":
296
297            if field.value == 0:
298                previewField.show_preview(previewFile1, "One up", True)
299            else:
300                previewField.show_preview(previewFile2, "One up", True)
301
302        elif field.label == "load Picture 1":
303
304            pic1Field.show_picture(pictureFile1)
305
306        elif field.label == "load Picture 2":
307
308            pic1Field.show_picture(pictureFile2)
309
310        elif field.field_type == ipDialogFieldType.dftPicture:
311
312            if pic1Field.picture_has_image:
313
314                if pic1Field.picture_file_name == "":
315
316                    impact.gui.show_message(ipShowMessageType.smtInfo, "The image was changed from the clipboard", None)
317
318                else:
319
320                    impact.gui.show_message(ipShowMessageType.smtInfo, "The image was changed and loaded from '" + pic1Field.picture_file_name + "'", None)
321
322            else:
323
324                impact.gui.show_message(ipShowMessageType.smtInfo, "The image was cleared", None)
325
326    if callbackParams.reason == ipDialogCallbackReason.dcrDialogClosing:
327
328        ot.add(DescribeDialogButtonType( callbackParams.button_type ))
329
330        if callbackParams.button_type == ipDialogButtonType.dbtOk:
331
332            callbackParams.can_close = allowCloseField.value