Debian Codegraph @

Nasal/canvas/gui/widgets/List.nas

raw
1 # List.nas - a list widget similar to Qt's QListWidget
2
3 # SPDX-FileCopyrightText: (C) 2022 Frederic Croix <[email protected]>
4 # SPDX-License-Identifier: GPL-2.0-or-later
5
6 gui.widgets.ListItem = {
7 new: func(parent, style = nil, cfg = nil) {
8 style = style or canvas.style;
9 cfg = Config.new(cfg);
10 var m = gui.Widget.new(gui.widgets.ListItem, cfg);
11 m._focus_policy = m.NoFocus;
12
13 m._data = cfg.get("data", {});
14 m._text = cfg.get("text", "");
15 m._data["text"] = m._text;
16 m._selected = cfg.get("selected", 0);
17 m._list = nil;
18
19 m._setView(style.createWidget(parent, "list-item", cfg));
20 m._view._updateLayoutSizes(m);
21
22 m.setText(m._text);
23 m.setSelected(m._selected);
24 m.setLayoutMaximumSize([m._MAX_SIZE, 24]);
25
26 return m;
27 },
28
29 # @description Set the data for this item.
30 # @param key Union[scalar, hash] required If @param key is a hash, this item's data is replaced with that hash.
31 # If @param key is a scalar, the data field with that name will be set to value.
32 # If @param key is anything else, an error will be raised.
33 # @param value Any optional The value to set the data field with key @param key to, if @param key is a scalar;
34 # otherwise, this argument is ignored.
35 # @return canvas.gui.widgets.ListItem This list item to support method chaining.
36 setData: func(key, value = nil) {
37 if (isscalar(key)) {
38 me._data[key] = value;
39 } elsif (ishash(key)) {
40 me._data = key;
41 } else {
42 die("cannot set data field with non-scalar key !");
43 }
44 return me;
45 },
46
47 # @description Get the data of this item.
48 # @param key Union[scalar, nil] The scalar key of the data field to return the value of, or nil to return the whole data.
49 # @return Any If @param key is a scalar, the value of the field with key @param key, else the whole data as a hash.
50 getData: func(key = nil) {
51 if (key != nil) {
52 if (!isscalar(key)) {
53 die("cannot get data field with non-scalar key !")
54 }
55 return me._data[key];
56 } else {
57 return me._data;
58 }
59 },
60
61 # @description Clear data
62 # @return canvas.gui.widgets.ListItem This list item to support method chaining.
63 clearData: func {
64 me._data = {};
65 return me;
66 },
67
68 setText: func(text) {
69 me._text = text;
70 me._view.setText(me, text);
71 return me;
72 },
73
74 getText: func {
75 return me._text;
76 },
77
78 setSelected: func(selected = 1) {
79 if (me.getParent() != nil) {
80 me.getParent().setItemSelection(me, selected);
81 }
82 return me;
83 },
84
85 getSelected: func {
86 return me._selected;
87 },
88
89 _setSelected: func(selected) {
90 if (selected != me._selected) {
91 me._selected = selected;
92 me.update();
93 me._trigger("selection-state-changed", {"selected": selected});
94 if (me._selected) {
95 me._trigger("selected");
96 } else {
97 me._trigger("unselected");
98 }
99 }
100 return me;
101 },
102
103 _setView: func(view) {
104 call(gui.Widget._setView, [view], me);
105
106 var el = view._root;
107 el.addEventListener("click", func me.setSelected());
108 },
109
110 update: func {
111 me._view.update(me);
112 return me;
113 },
114 };
115
116 gui.widgets.List = {
117 new: func(parent, style = nil, cfg = nil) {
118 cfg = Config.new(cfg);
119 var m = gui.Widget.new(gui.widgets.List, cfg);
120 m._style = style or canvas.style;
121 m._focus_policy = m.NoFocus;
122
123 m._setView(m._style.createWidget(parent, "list", m._cfg));
124
125 m._scroll = gui.widgets.ScrollArea.new(m._view._root, style, {});
126 m._scrollLayout = VBoxLayout.new();
127 m._scroll.setLayout(m._scrollLayout);
128 m._scrollLayout.setSpacing(0);
129
130 m.setLayoutMinimumSize([48, 24]);
131
132 if (contains(m._cfg, "items")) {
133 foreach (var item; m._cfg["items"]) {
134 if (isa(item, gui.widgets.ListItem)) {
135 m.addItem(item);
136 } elsif (ishash(item)) {
137 m.createItem(cfg: item);
138 } else {
139 m.createItem(text: item);
140 }
141 }
142 }
143
144 return m;
145 },
146
147 setSize: func {
148 if (size(arg) == 1) {
149 var arg = arg[0];
150 }
151 var (x, y) = arg;
152 me._size = [x, y];
153 me._scroll.setSize(x, y);
154 me._view.setSize(me, x, y);
155 return me.update();
156 },
157
158 _onItemSelectionStateChanged: func {
159 var items = me.getSelectedItems();
160 me._trigger("selection-changed");
161 },
162
163 filter: func(filter = nil, dataItem = "text") {
164 foreach (var item; me.items()) {
165 if (filter == nil or find(filter, item.getData(dataItem)) > -1) {
166 item.setVisible(1);
167 } else {
168 item.setVisible(0);
169 }
170 }
171 },
172
173 # @description Add the given item to this list.
174 # @param item canvas.gui.widgets.ListItem required The item to be added to this list.
175 # @return canvas.gui.widgets.List This list to support method chaining.
176 addItem: func(item) {
177 item.listen("selection-state-changed", func me._onItemSelectionStateChanged());
178 item.setAlignment(canvas.AlignTop);
179 me._scrollLayout.addItem(item);
180 item.setParent(me);
181 return me;
182 },
183
184 # @description Create an item with the given text and optionally the given config.
185 # @param text str required Text of the new item.
186 # @param cfg hash optional Additional configuration of the item.
187 # @return canvas.gui.widgets.ListItem The created list item.
188 createItem: func(text = nil, cfg = nil) {
189 cfg = cfg or {};
190 if (text) {
191 cfg["text"] = text;
192 }
193 var item = gui.widgets.ListItem.new(me._scroll.getContent(), me._style, cfg);
194 me.addItem(item);
195 return item;
196 },
197
198 # @description Count the items of this list.
199 # @return int Number of items in this list.
200 count: func {
201 return me._scrollLayout.count();
202 },
203
204 # @description Remove all items from this list.
205 # @return canvas.gui.widgets.List This list to support method chaining.
206 clear: func {
207 me._scrollLayout.clear();
208 return me;
209 },
210
211 # @description Get the item with the given index or text
212 # @param text_or_index Union[int, str] required If text_or_index is an integer, the item at index text_or_index is returned.
213 # If text_or_index is a string, the item with text == text_or_index is returned.
214 # @return canvas.gui.widgets.ListItem The item with the given text or index, or nil if no such item is found.
215 getItem: func(text_or_index) {
216 if (isint(text_or_index)) {
217 return me._scrollLayout.itemAt(text_or_index);
218 } else {
219 for (var i = 0; i < me.count(); i += 1) {
220 if (me._scrollLayout.itemAt(i)._text == text_or_index) {
221 return me.getItem(i);
222 }
223 }
224 }
225 },
226
227 # @description Return a vector containing all items of this list
228 # @return vector[canvas.gui.widgets.ListItem] Vector containing all items of this list
229 getItems: func() {
230 return me._scrollLayout.items();
231 },
232
233 # @description Find the index of the given item or item with the given text.
234 # @param text_or_item Union[str, canvas.gui.widgets.ListItem] required The item or text of the item to return the index of.
235 # @return int The index of the given item or item with the given text, or -1 if the given item or no item with the given text is found.
236 findItem: func(text_or_item) {
237 for (var i = 0; i < me.count(); i += 1) {
238 if (
239 (isstr(text_or_item) and me._scrollLayout.itemAt(i)._text == text_or_index) or
240 text_or_item == me.getItem(i)
241 ) {
242 return i;
243 }
244 }
245 return -1;
246 },
247
248 # @description Find the index of the given item or item with the given data value at the given data key.
249 # @param key str required The data key to search for
250 # @param .value Any required The data value at the given data key to search for.
251 # @return int The index of the item with the given data value at the given data key,
252 # or -1 if no item with the given data value is found.
253 findItemByData: func(key, value) {
254 for (var i = 0; i < me.count(); i += 1) {
255 if (me.getItem(i).getData(key) == value) {
256 return i;
257 }
258 }
259 return -1;
260 },
261
262 # @description Remove and return the item with the given text or index.
263 # @param text_or_item Union[str, int] required The text or index of the item to remove and return
264 # @return int The item with the given text or index, or nil if no item with the given text or index is found.
265 takeItem: func(text_or_index) {
266 var index = text_or_index;
267 if (!isint(index)) {
268 index = me.findItem(index);
269 if (index < 0) {
270 return nil;
271 }
272 } elsif (index < 0) {
273 return nil;
274 }
275 return me._scrollLayout.takeAt(index);
276 },
277
278 # @description Remove the item with the given text or index or given item.
279 # @param text_or_index_or_item Union[str, int, canvas.gui.widgets.ListItem] required The text or index of the item, or the item, to remove.
280 # @return canvas.gui.widgets.List This list to support method chaining.
281 removeItem: func(text_or_index_or_item) {
282 if (isscalar(text_or_index_or_item)) {
283 me.takeItem(text_or_index_or_item);
284 } else {
285 me._scrollLayout.removeItem(item);
286 }
287 return me;
288 },
289
290 # @description Unselect all items of this list.
291 # @return canvas.gui.widgets.List This list to support method chaining.
292 clearSelection: func {
293 for (var i = 0; i < me.count(); i += 1) {
294 me.getItem(i)._selected = 0;
295 me.getItem(i).update();
296 }
297 return me;
298 },
299
300 # @description Set the selected state of the item item with the given index or text.
301 # @param text_or_index Union[int, str] required If text_or_index is an integer, the item at index text_or_index is returned.
302 # If text_or_index is a string, the item with text == text_or_index is returned.
303 # @param selected bool optional The selection state of the item, defaults to selected if not given.
304 # @return canvas.gui.widgets.List This list to support method chaining.
305 setItemSelection: func(text_or_index_or_item, selected = 1) {
306 var item = text_or_index_or_item;
307 if (typeof(item) == "scalar") {
308 item = me.getItem(text_or_index_or_item);
309 if (!item) {
310 die("no item found with given text or index '" ~ text_or_index_or_item ~ "' found !");
311 }
312 }
313 me.clearSelection();
314 item._setSelected(selected);
315 return me;
316 },
317
318 # @description Get a vector containing all selected items of this list
319 # @return vector[canvas.gui.widgets.ListItem] Vector containing the selected items
320 getSelectedItems: func {
321 var items = [];
322 for (var i = 0; i < me.count(); i += 1) {
323 var item = me.getItem(i);
324 if (item._selected) {
325 append(items, item);
326 }
327 }
328 return items;
329 },
330
331 update: func {
332 me._scroll.update();
333 me._view.update(me);
334 return me;
335 }
336 }
337