Debian Codegraph @

doc/sphinx/techref/askmulti.rst

raw
1 The `askMulti` Dialog
2 =====================
3
4 When called in a UI-enabled environment the Python
5 :py:func:`fontforge.askMulti()` method raises a modal dialog that prompts the
6 user to answer one or more questions, potentially organized into "categories"
7 displayed in separate tabs. It takes two parameters, ``title`` and ``specification``.
8
9 The ``title`` parameter should be a string that will be displayed in the dialog
10 window title bar.
11
12 The ``specification`` paramter is either a Python list or dictionary object
13 specifying the questions. This is explained below.
14
15 Questions
16 ---------
17
18 There are four question types, corresponding to the single-question
19 :py:func:`fontforge.askString()`, :py:func:`fontforge.askChoices()`,
20 :py:func:`fontforge.openFilename()`, and :py:func:`fontforge.saveFilename()`
21 dialog methods. Each individual question is specified as a dictionary.
22
23 All question types use the keys ``type``, ``question````, ``tag`` and ``align``. The
24 ``type`` value specifies the type of question as outlined below.
25
26 The value corresponding to ``question`` is the label string, which is displayed
27 before the answer field. This key must be present but the value can be ``None``
28 if no label is needed.
29
30 The ``tag`` is optional but either it or ``question`` must have a value. When
31 present its value will be used as the key corresponding to this question in the
32 result dictionary returned by the method. Otherwise the ``question`` value
33 will be used. The ``tag`` value can be any "hashable" type—that is, any type
34 that can serve as a key in a Python dictionary.
35
36 The ``align`` value must be boolean-evaluable (ideally ``True`` or ``False``).
37 It indicates whether, when the dialog is being laid out, the label for this
38 question should be aligned with the other labels of the same category. When
39 absent the default is ``True``. (This has no effect when the ``question`` value
40 is ``None``, so if you want an aligned, empty label use a space as the
41 ``question`` value.)
42
43 String Questions
44 ^^^^^^^^^^^^^^^^
45
46 A string question displays the label followed by a text entry field. Its
47 ``type`` value must be "string".
48
49 The only additional key for a string question is ``default``, which is optional.
50 When the key present its value, which must be a string, will be the initial
51 value of the answer entry field. An example dictionary is::
52
53 { 'type': 'string', 'question': 'Number of contours:',
54 'tag': 3, 'default': '4' }
55
56 Choice Questions
57 ^^^^^^^^^^^^^^^^
58
59 Choice questions ask the user to pick a subset of given answers. Its ``type``
60 value must be "choice". This question type has three additional dictionary
61 keys: ``answers``, ``multiple``, and ``checks``.
62
63 The ``answers`` value must be a Python list of potential answers, each of which is
64 its own small dictionary. The keys to an answer dictionary are ``name``, ``tag``,
65 and ``default``. The value of ``name`` must be the string the user will choose among.
66 The value of ``tag``, when present, will be used to report the answer in the
67 result dictionary—when not present the ``name`` string will be used. Unlike the
68 question ``tag`` an answer ``tag`` value can be of any type whatsoever. The ``default``
69 key is an optional key that must have a boolean-evaluable value. When ``True``
70 the answer is selected when the dialog is presented.
71
72 The ``multiple`` value must be boolean-evaluable; when the key is absent the
73 default is ``False``. It indicates whether the user must choose exactly one
74 answer or can choose multiple or, potentially, no answers. When ``multiple`` is
75 false there must be at most one answer for which ``default`` is ``True``.
76
77 The ``checks`` value must also be boolean-evaluable and its default is also
78 ``False``. By default a choice question is presented as in the
79 :py:func:`fontforge.askChoices()` dialog, with a (potentially) scrolling
80 vertical list similar to pop-up menu. When ``checks`` is ``True`` and ``multiple``
81 is ``False`` the answers are presented as radio buttons. When ``checks`` is ``True``
82 and ``multiple`` is ``True`` the answers are presented as checkboxes. Radio
83 buttons and checkboxes allow better keyboard-based selection (see the
84 mnemonics section) and may have other advantages.
85
86 An example choice question dictionary is: ::
87
88 { 'type': 'choice', 'question': 'What direction:',
89 'tag': 'direction', 'checks': True,
90 'answers': [ { 'name': 'Clockwise', 'tag': 'cw', 'default': True },
91 { 'name': 'Counter-Clockwise', 'tag': 'ccw' } ]
92 }
93
94 Pathname Questions
95 ^^^^^^^^^^^^^^^^^^
96
97 The open pathname and save pathname questions have the respective type values
98 "openpath" and "savepath". These differ from the
99 :py:func:`fontforge.openFilename()` and :py:func:`fontforge.saveFilename()`
100 methods in that they do not raise a file browser directly. Instead they display
101 an entry field, similar to a string question, followed by a button to raise a
102 browser, similar to the path-specifying entries in :doc:``the preferences dialog
103 </ui/dialogs/prefs>``. Each also has an optional ``default`` key for an initial
104 pathname value, and an optional ``filter`` key to specify a file filter. Both
105 must be strings. An example dictionary is: ::
106
107 { 'type': 'openpath', 'question': 'Glif file:', 'tag': 'file',
108 'default': 'input.glif', 'filter': '*.glif' }
109
110 Categories
111 ----------
112
113 Questions are organized into categories. A category is a dictionary with
114 two keys: ``category`` and ``questions``. The value of ``category`` should be a
115 either string to display to the user or ``None`` if no label is needed.
116 The value of ``questions`` must be a list of question dictionaries, as
117 described above. A category containing the dictionaries above looks like:
118
119 .. figure:: /images/multi_example.png
120 :alt: Example ``askMulti`` dialog
121
122 The Specification
123 -----------------
124
125 The specification passed as the ``spec`` parameter can be any of:
126
127 * A question dictionary
128 * A list of question dictionaries
129 * A category dictionary
130 * A list of category dictionaries
131
132 In the last case each category will be given its own tab, with the
133 category names selectable on the left side of the dialog. (Whenever
134 there is only one category there is no list, even when the category
135 has a name.)
136
137 The Return Value
138 ----------------
139
140 When the user cancels the dialog, either with the "Cancel" button or
141 by closing the window, the ``askMulti`` method returns None. Otherwise
142 it returns a Python dictionary of results, one for each question. The
143 key will be the ``tag`` value or, if none was present, the ``question``
144 string. For all question types other than "choice" the value is a
145 string. For a choice question where ``multiple`` is ``False`` the value
146 is the ``tag`` or ``name`` of the answer. For a ``multiple`` choice question
147 the value is a tuple of ``tag`` or ``name`` values. The tuple may be
148 empty.
149
150 A result dictionary for the collection above could be: ::
151
152 {3: '20', 'direction': 'ccw', 'file': '/tmp/in.glif'}
153
154 When the specification is invalid the method will throw a ``TypeError``
155 or ``ValueError`` exception with information about the problem.