Simple dialog showing pulldown list

Simple dialog showing pulldown list
 1# simple function to allow the user to choose a palette (uses an array)
 2def palette_chooser1():
 3    the_dialog = impact.gui.make_dialog("Choose a palette")
 4
 5    # we must make an array of the palette names
 6    pal_name_array = []
 7    pal_count = 0
 8    count = 0
 9
10    # in this example we will only show the Cut palettes
11    for pal in active_drawing.palettes:
12        if pal.palette_type == ipPaletteType.ptCut:
13
14            # we need to resize the array each time we add an item
15            pal_name_array = (list(pal_name_array or []) + [None] * (count + 1))[:count + 1]
16            pal_name_array[count] = pal.full_name
17            count = count +1
18
19    # now add this field to the dialog
20    field_palette_list = the_dialog.fields.add_combo_box(0, "Palettes", pal_name_array)
21
22    # only one button
23    the_dialog.add_button(ipDialogButtonType.dbtOk)
24
25    # display the dialog
26    the_button = the_dialog.show_modal(None)
27
28    # now find with item the user has chosen (for example the third item), and
29    # find which palette name this corresponds to
30    chosen_pal_item = field_palette_list.value
31    chosen_pal = pal_name_array[chosen_pal_item]
32
33    # finally we return this palette name
34    _result = chosen_pal
35    return _result
36
37
38# simple function to allow the user to choose a palette (uses an arraylist, for easy sorting)
39def palette_chooser2():
40    the_dialog = impact.gui.make_dialog("Choose a palette")
41
42    # we must make an array of the palette names, but we will do this with an arraylist object
43    pal_name_array_list = []
44
45    # in this example we will only show the Cut palettes
46    for pal in active_drawing.palettes:
47        if pal.palette_type == ipPaletteType.ptCut:
48
49            # add the palette name to the arraylist
50            pal_name_array_list.append(pal.full_name)
51
52    # now sort the names alphabetically
53    pal_name_array_list.sort()
54
55    # convert the arraylist to an array
56    pal_name_array = list(pal_name_array_list)
57
58    # now add this field to the dialog
59    field_palette_list = the_dialog.fields.add_combo_box(0, "Palettes", pal_name_array)
60
61    # only one button
62    the_dialog.add_button(ipDialogButtonType.dbtOk)
63
64    # display the dialog
65    the_button = the_dialog.show_modal(None)
66
67    # now find with item the user has chosen (for example the third item), and
68    # find which palette name this corresponds to
69    chosen_pal_item = field_palette_list.value
70    chosen_pal = pal_name_array[chosen_pal_item]
71
72    # finally we return this palette name
73    _result = chosen_pal
74    return _result
75
76if not active_drawing.isNone():
77    pal_name = palette_chooser2()
78
79    impact.gui.output_toolbox.add("The chosen palette was '" + str(pal_name) + "'")