NXWidgets: clistdata.cxx Source File

NXWidgets 1.19

clistdata.cxx
Go to the documentation of this file.
1 /****************************************************************************
2 * NxWidgets/libnxwidgets/src/clistdata.cxx
3 *
4 * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5 * Author: Gregory Nutt <gnutt@nuttx.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18 * me be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *
34 ****************************************************************************
35 *
36 * Portions of this package derive from Woopsi (http://woopsi.org/) and
37 * portions are original efforts. It is difficult to determine at this
38 * point what parts are original efforts and which parts derive from Woopsi.
39 * However, in any event, the work of Antony Dzeryn will be acknowledged
40 * in most NxWidget files. Thanks Antony!
41 *
42 * Copyright (c) 2007-2011, Antony Dzeryn
43 * All rights reserved.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions are met:
47 *
48 * * Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * * Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * * Neither the names "Woopsi", "Simian Zombie" nor the
54 * names of its contributors may be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60 * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 *
68 ****************************************************************************/
69
70 /****************************************************************************
71 * Included Files
72 ****************************************************************************/
73
74 #include <nuttx/config.h>
75
76 #include <cstdint>
77 #include <cstdbool>
78 #include <cstring>
79
80 #include "clistdata.hxx"
81 #include "ilistdataeventhandler.hxx"
82
83 /****************************************************************************
84 * Pre-Processor Definitions
85 ****************************************************************************/
86
87 /****************************************************************************
88 * Method Implementations
89 ****************************************************************************/
90
91 using namespace NXWidgets;
92
93 /**
94 * Constructor.
95 */
96
97 CListData::CListData(void)
98 {
99 m_allowMultipleSelections = true;
100 m_sortInsertedItems = false;
101 }
102
103 /**
104 * Destructor.
105 */
106
107 CListData::~CListData(void)
108 {
109 removeAllItems();
110 }
111
112 /**
113 * Add a new item.
114 *
115 * @param text Text to show in the option.
116 * @param value The value of the option.
117 */
118
119 void CListData::addItem(const CNxString &text, const uint32_t value)
120 {
121 // Create new option
122
123 addItem(new CListDataItem(text, value));
124 }
125
126 /**
127 * Add an existing item. CListData becomes the owner of the option and will delete it
128 * when the list is deleted.
129 *
130 * @param item The item to add.
131 */
132
133 void CListData::addItem(CListDataItem *item)
134 {
135 // Determine insert type
136
137 if (m_sortInsertedItems)
138 {
139 // Sorted insert
140
141 m_items.insert(getSortedInsertionIndex(item), item);
142 }
143 else
144 {
145 // Append
146
147 m_items.push_back(item);
148 }
149
150 raiseDataChangedEvent();
151 }
152
153 /**
154 * Remove an item by its index.
155 *
156 * @param index The index of the option to remove.
157 */
158
159 void CListData::removeItem(const int index)
160 {
161 // Bounds check
162
163 if (index < m_items.size())
164 {
165 // Delete the option
166
167 delete m_items[index];
168
169 // Erase the option from the list
170
171 m_items.erase(index);
172 raiseDataChangedEvent();
173 }
174 }
175
176 /**
177 * Select an item by its index.
178 *
179 * @param index The index of the item to select.
180 */
181
182 void CListData::selectItem(const int index)
183 {
184 setItemSelected(index, true);
185 }
186
187 /**
188 * Deselect an item by its index.
189 *
190 * @param index The index of the item to select.
191 */
192
193 void CListData::deselectItem(const int index)
194 {
195 setItemSelected(index, false);
196 }
197
198 /**
199 * Remove all items.
200 */
201
202 void CListData::removeAllItems(void)
203 {
204 // Delete all option data
205
206 for (int i = 0; i < m_items.size(); i++)
207 {
208 delete m_items[i];
209 }
210
211 m_items.clear();
212 raiseDataChangedEvent();
213 }
214
215 /**
216 * Get the selected index. Returns -1 if nothing is selected. If more than one
217 * item is selected, the index of the first selected item is returned.
218 *
219 * @return The selected index.
220 */
221
222 const int CListData::getSelectedIndex(void) const
223 {
224 // Get the first selected index
225
226 for (int i = 0; i < m_items.size(); i++)
227 {
228 if (m_items[i]->isSelected())
229 {
230 return i;
231 }
232 }
233
234 return -1;
235 }
236
237 /**
238 * Sets the selected index. Specify -1 to select nothing. Resets any
239 * other selected items to deselected.
240 *
241 * @param index The selected index.
242 */
243
244 void CListData::setSelectedIndex(const int index)
245 {
246 setItemSelected(index, true);
247 }
248
249 /**
250 * Get the selected item. Returns NULL if nothing is selected.
251 *
252 * @return The selected option.
253 */
254
255 const CListDataItem *CListData::getSelectedItem(void) const
256 {
257 // Get the first selected option
258
259 int index = getSelectedIndex();
260 if (index > -1)
261 {
262 return m_items[index];
263 }
264 return (CListDataItem *)NULL;
265 }
266
267 /**
268 * Sort the items using their compareTo() methods.
269 */
270
271 void CListData::sort(void)
272 {
273 quickSort(0, m_items.size() - 1);
274 raiseDataChangedEvent();
275 }
276
277 /**
278 * Select all items. Does nothing if the list does not allow
279 * multiple selections.
280 */
281
282 void CListData::selectAllItems(void)
283 {
284 if (m_allowMultipleSelections)
285 {
286 for (int i = 0; i < m_items.size(); i++)
287 {
288 m_items[i]->setSelected(true);
289 }
290
291 raiseSelectionChangedEvent();
292 }
293 }
294
295 /**
296 * Deselect all items.
297 */
298
299 void CListData::deselectAllItems(void)
300 {
301 for (int i = 0; i < m_items.size(); i++)
302 {
303 m_items[i]->setSelected(false);
304 }
305
306 raiseSelectionChangedEvent();
307 }
308
309 /**
310 * Select or deselect an item by its index. Does not deselect any
311 * other selected items. Set index to -1 to select nothing.
312 *
313 * @param index The index of the item to select.
314 * @param selected True to select the item, false to deselect it.
315 */
316
317 void CListData::setItemSelected(const int index, bool selected)
318 {
319 // Deselect old options if we're making an option selected and
320 // we're not a multiple list
321
322 if (((!m_allowMultipleSelections) || (index == -1)) && (selected))
323 {
324 for (int i = 0; i < m_items.size(); i++)
325 {
326 m_items[i]->setSelected(false);
327 }
328 }
329
330 // Select or deselect the new option
331
332 if ((index > -1) && (index < m_items.size()))
333 {
334 m_items[index]->setSelected(selected);
335 }
336
337 raiseSelectionChangedEvent();
338 }
339
340 /**
341 * Remove an event handler.
342 *
343 * @param eventHandler The event handler to remove.
344 */
345
346 void CListData::removeListDataEventHandler(IListDataEventHandler *eventHandler)
347 {
348 for (int i = 0; i < m_listDataEventhandlers.size(); ++i)
349 {
350 if (m_listDataEventhandlers.at(i) == eventHandler)
351 {
352 m_listDataEventhandlers.erase(i);
353 return;
354 }
355 }
356 }
357
358 /**
359 * Quick sort the items using their compareTo() methods.
360 *
361 * @param start The index to start sorting at.
362 * @param end The index to stop sorting at.
363 */
364
365 void CListData::quickSort(const int start, const int end)
366 {
367 if (end > start)
368 {
369 int left = start;
370 int right = end;
371
372 CListDataItem *pivot = m_items[(start + end) >> 1];
373
374 do
375 {
376 while ((pivot->compareTo(m_items[left]) > 0) && (left < end))
377 {
378 left++;
379 }
380
381 while ((pivot->compareTo(m_items[right]) < 0) && (right > start))
382 {
383 right--;
384 }
385
386 if (left > right)
387 {
388 break;
389 }
390
391 swapItems(left, right);
392 left++;
393 right--;
394 }
395 while (left <= right);
396
397 quickSort(start, right);
398 quickSort(left, end);
399 }
400 }
401
402 /**
403 * Swap the locations of two items in the array.
404 *
405 * @param index1 The index of the first item to swap.
406 * @param index2 The index of the second item to swap.
407 */
408
409 void CListData::swapItems(const int index1, const int index2)
410 {
411 CListDataItem *tmp = m_items[index1];
412 m_items[index1] = m_items[index2];
413 m_items[index2] = tmp;
414 }
415
416 /**
417 * Return the index that an item should be inserted at to maintain a sorted list of data.
418 *
419 * @param item The item to insert.
420 * @return The index that the item should be imserted into at.
421 */
422
423 const int CListData::getSortedInsertionIndex(const CListDataItem *item) const
424 {
425 int i = 0;
426
427 // Locate slot where new option should go
428
429 while ((i < m_items.size()) && (item->compareTo(m_items[i]) > 0))
430 {
431 i++;
432 }
433 return i;
434 }
435
436 /**
437 * Raise a data changed event.
438 */
439
440 void CListData::raiseDataChangedEvent(void)
441 {
442 CListDataEventArgs eventArgs(this);
443
444 for (int i = 0; i < m_listDataEventhandlers.size(); ++i)
445 {
446 m_listDataEventhandlers.at(i)->handleListDataChangedEvent(eventArgs);
447 }
448 }
449
450 /**
451 * Raise a selection changed event.
452 */
453
454 void CListData::raiseSelectionChangedEvent(void)
455 {
456 CListDataEventArgs eventArgs(this);
457
458 for (int i = 0; i < m_listDataEventhandlers.size(); ++i)
459 {
460 m_listDataEventhandlers.at(i)->handleListDataSelectionChangedEvent(eventArgs);
461 }
462 }
NXWidgets::CListData::CListData
CListData(void)
Definition: clistdata.cxx:97
NXWidgets::CListData::raiseSelectionChangedEvent
void raiseSelectionChangedEvent(void)
Definition: clistdata.cxx:454
NXWidgets::CListData::quickSort
virtual void quickSort(const int start, const int end)
Definition: clistdata.cxx:365
clistdata.hxx
NXWidgets::IListDataEventHandler
Definition: ilistdataeventhandler.hxx:103
NXWidgets::CListDataItem
Definition: clistdataitem.hxx:103
NXWidgets::CListData::removeItem
virtual void removeItem(const int index)
Definition: clistdata.cxx:159
NXWidgets::CListData::swapItems
virtual void swapItems(const int index1, const int index2)
Definition: clistdata.cxx:409
NXWidgets::CListData::raiseDataChangedEvent
void raiseDataChangedEvent(void)
Definition: clistdata.cxx:440
NXWidgets::CListData::removeListDataEventHandler
void removeListDataEventHandler(IListDataEventHandler *eventHandler)
Definition: clistdata.cxx:346
NXWidgets::CListData::getSelectedItem
virtual const CListDataItem * getSelectedItem(void) const
Definition: clistdata.cxx:255
NXWidgets::CListDataEventArgs
Definition: clistdataeventargs.hxx:103
NXWidgets::CListData::sort
virtual void sort(void)
Definition: clistdata.cxx:271
NXWidgets
Definition: cbgwindow.hxx:65
NXWidgets::CListData::setSelectedIndex
virtual void setSelectedIndex(const int index)
Definition: clistdata.cxx:244
NXWidgets::CListData::m_items
TNxArray< CListDataItem * > m_items
Definition: clistdata.hxx:110
NXWidgets::CListData::m_allowMultipleSelections
bool m_allowMultipleSelections
Definition: clistdata.hxx:112
NXWidgets::CListDataItem::compareTo
virtual int compareTo(const CListDataItem *item) const
Definition: clistdataitem.cxx:125
NXWidgets::CListData::getSelectedIndex
virtual const int getSelectedIndex(void) const
Definition: clistdata.cxx:222
NXWidgets::CListData::removeAllItems
virtual void removeAllItems(void)
Definition: clistdata.cxx:202
NXWidgets::CListData::deselectItem
virtual void deselectItem(const int index)
Definition: clistdata.cxx:193
NXWidgets::CListData::~CListData
virtual ~CListData(void)
Definition: clistdata.cxx:107
NXWidgets::CListData::m_sortInsertedItems
bool m_sortInsertedItems
Definition: clistdata.hxx:114
NXWidgets::CListData::m_listDataEventhandlers
TNxArray< IListDataEventHandler * > m_listDataEventhandlers
Definition: clistdata.hxx:111
NXWidgets::CListData::selectItem
virtual void selectItem(const int index)
Definition: clistdata.cxx:182
NXWidgets::CListData::deselectAllItems
virtual void deselectAllItems(void)
Definition: clistdata.cxx:299
NXWidgets::CNxString
Definition: cnxstring.hxx:121
NXWidgets::CListData::selectAllItems
virtual void selectAllItems(void)
Definition: clistdata.cxx:282
NXWidgets::CListData::setItemSelected
virtual void setItemSelected(const int index, const bool selected)
Definition: clistdata.cxx:317
NXWidgets::CListData::getSortedInsertionIndex
const int getSortedInsertionIndex(const CListDataItem *item) const
Definition: clistdata.cxx:423
NXWidgets::CListData::addItem
virtual void addItem(const CNxString &text, const uint32_t value)
Definition: clistdata.cxx:119
ilistdataeventhandler.hxx