libyui  3.12.1
YSelectionWidget.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YSelectionWidget.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 #ifndef YSelectionWidget_h
26 #define YSelectionWidget_h
27 
28 #include "YWidget.h"
29 #include "YItem.h"
30 #include "ImplPtr.h"
31 
33 
34 /**
35  * Base class for various kinds of multi-value widgets.
36  * - YSelectionBox, YMultiSelectionBox, YComboBox
37  * - YContextMenu, YMenuButton
38  * - YTable
39  * - YTree
40  * - YDumbTab
41  *
42  * See also
43  * https://github.com/libyui/libyui-ncurses/blob/master/doc/nctable-and-nctree.md
44  **/
45 class YSelectionWidget : public YWidget
46 {
47 protected:
48 
49  /**
50  * Constructor.
51  *
52  * 'enforceSingleSelection' indicates if this base class should enforce
53  * single selection when items are added or when items are selected from
54  * the application. Note that single selection can also mean that no item
55  * is selected.
56  **/
58  const std::string & label,
60  bool recursiveSelection = false );
61 
62 public:
63  /**
64  * Destructor.
65  **/
66  virtual ~YSelectionWidget();
67 
68  /**
69  * Returns a descriptive name of this widget class for logging,
70  * debugging etc.
71  **/
72  virtual const char * widgetClass() const { return "YSelectionWidget"; }
73 
74  /**
75  * Return this widget's label (the caption above the item list).
76  **/
77  std::string label() const;
78 
79  /**
80  * Change this widget's label (the caption above the item list).
81  *
82  * Derived classes should overwrite this function, but they should call
83  * this base class function in the new implementation.
84  **/
85  virtual void setLabel( const std::string & newLabel );
86 
87  /**
88  * Add one item. This widget assumes ownership of the item object and will
89  * delete it in its destructor.
90  *
91  * NOTE: For tree items, call this only for the toplevel items; all
92  * non-toplevel items are already owned by their respective parent
93  * items. Adding them to the parent widget will clash with this ownership.
94  *
95  * Derived classes can overwrite this function, but they should call this
96  * base class function in the new implementation.
97  **/
98  virtual void addItem( YItem * item_disown );
99 
100  /**
101  * Overloaded for convenience: Add an item by string.
102  **/
103  void addItem( const std::string & itemLabel, bool selected = false );
104 
105  /**
106  * Overloaded for convenience: Add an item with a text and an icon.
107  * Note that not all UIs can display icons.
108  **/
109  void addItem( const std::string & itemLabel,
110  const std::string & iconName,
111  bool selected = false );
112 
113  /**
114  * Add multiple items. For some UIs, this can be more efficient than
115  * calling addItem() multiple times.
116  **/
117  virtual void addItems( const YItemCollection & itemCollection );
118 
119  /**
120  * Delete all items.
121  *
122  * Derived classes can overwrite this function, but they should call this
123  * base class function in the new implementation.
124  **/
125  virtual void deleteAllItems();
126 
127  /**
128  * Delete all items and add new items.
129  **/
130  void setItems( const YItemCollection & itemCollection )
131  { deleteAllItems(); addItems( itemCollection ); }
132 
133  /**
134  * Return an iterator that points to the first item.
135  *
136  * For YSelectionWidgets that can have tree structures, this iterator will
137  * iterate over the toplevel items.
138  *
139  * Important: Don't use this iterator to iterate over all items and check
140  * their "selected" state; that information might not always be up to
141  * date. Use the dedicated functions for that.
142  **/
145 
146  /**
147  * Return an iterator that points behind the last item.
148  **/
151 
152  /**
153  * Return 'true' if this widget has any items.
154  **/
155  bool hasItems() const;
156 
157  /**
158  * Return the number of items.
159  *
160  * For YSelectionWidgets that can have tree structures, this returns the
161  * number of toplevel items.
162  **/
163  int itemsCount() const;
164 
165  /**
166  * Return the item at index 'index' (from 0)
167  * or 0 if there is no such item.
168  **/
169  YItem * itemAt( int index ) const;
170 
171  /**
172  * Return the first item or 0 if there is none.
173  **/
174  YItem * firstItem() const;
175 
176  /**
177  * Return the (first) selected item or 0 if none is selected.
178  **/
179  virtual YItem * selectedItem();
180 
181  /**
182  * Return all selected items. This is mostly useful for derived classes
183  * that allow selecting multiple items.
184  *
185  * This function does not transfer ownership of those items to the caller,
186  * so don't try to delete them!
187  **/
188  virtual YItemCollection selectedItems();
189 
190  /**
191  * Return 'true' if any item is selected.
192  **/
193  bool hasSelectedItem();
194 
195  /**
196  * Select or deselect an item.
197  *
198  * Notice that this is different from YItem::setSelected() because unlike
199  * the latter function, this function informs the parent widget of the
200  * selection change.
201  *
202  * If only one item can be selected at any time (single selection), the
203  * derived class will make sure to deselect any previous selection, if
204  * applicable.
205  *
206  * Derived classes should overwrite this function, but they should call
207  * this base class function at the new function's start (this will also
208  * check if the item really belongs to this widget and throw an exception
209  * if not).
210  **/
211  virtual void selectItem( YItem * item, bool selected = true );
212 
213  /**
214  * Set the status of an item.
215  *
216  * This is similar to selectItem(), but with numeric values.
217  *
218  * This default implementation just calls selectItem() with 'status'
219  * converted to boolean. Derived classes can choose to make more detailed
220  * use of the numeric value.
221  **/
222  virtual void setItemStatus( YItem * item, int status );
223 
224  /**
225  * Deselect all items.
226  *
227  * Derived classes can overwrite this function, but they should call this
228  * base class function in the new implementation.
229  **/
230  virtual void deselectAllItems();
231 
232  /**
233  * Set this widget's base path where to look up icons.
234  * If this is a relative path, YUI::qApp()->iconBasePath() is prepended.
235  **/
236  void setIconBasePath( const std::string & basePath );
237 
238  /**
239  * Return this widget's base path where to look up icons
240  * as set with setIconBasePath().
241  **/
242  std::string iconBasePath() const;
243 
244  /**
245  * Return the full path + file name for the specified icon name.
246  * If iconBasePath is non-empty, it is prepended to the icon name.
247  * Otherwise, YUI::yApp()->iconLoader() and its icon search paths
248  * is used find the icon in one of them
249  *
250  * If 'iconName' is empty, this will return an empty string.
251  **/
252  std::string iconFullPath( const std::string & iconName ) const;
253 
254  /**
255  * Return the full path + file name for the icon of the specified item.
256  * If iconBasePath is non-empty, it is prepended to the item's iconName.
257  * Otherwise, YUI::yApp()->iconLoader() and its icon search paths
258  * is used find the icon in one of them
259  *
260  * If 'item' does not have an iconName specified, this will return an empty
261  * string.
262  **/
263  std::string iconFullPath( YItem * item ) const;
264 
265  /**
266  * Return 'true' if this widget's items contain the specified item.
267  **/
268  bool itemsContain( YItem * item ) const;
269 
270  /**
271  * Find the (first) item with the specified label.
272  * Return 0 if there is no item with that label.
273  **/
274  YItem * findItem( const std::string & itemLabel ) const;
275 
276  /**
277  * Dump all items and their selection state to the log.
278  **/
279  void dumpItems() const;
280 
281  /**
282  * Return 'true' if this base class should enforce single selection.
283  **/
284  bool enforceSingleSelection() const;
285 
286  /**
287  * Notification that any shortcut of any item was changed by the shortcut
288  * conflict manager YShortcutManager.
289  *
290  * Derived classes should reimplement this.
291  **/
292  virtual void shortcutChanged() {}
293 
294  /**
295  * Get the string of this widget that holds the keyboard shortcut.
296  *
297  * Notice that some sub-classes (e.g., YDumbTab, YItemSelection, YMenuBar)
298  * has one shortcut for each item. This value is not meaningful for such
299  * widget classes.
300  *
301  * Check YItemShortcut in YShortcut.{cc,h} for more details.
302  *
303  * Reimplemented from YWidget.
304  **/
305  virtual std::string shortcutString() const { return label(); }
306 
307  /**
308  * Set the string of this widget that holds the keyboard shortcut.
309  *
310  * Also trigger a shortcutChanged() notification. This is useful for
311  * derived sub-classes to refresh the widget when any shortcut of any item
312  * was changed by the shortcut conflict manager.
313  *
314  * Check YItemShortcut in YShortcut.{cc,h} for more details.
315  *
316  * Reimplemented from YWidget.
317  **/
318  virtual void setShortcutString( const std::string & str );
319 
320 
321 protected:
322 
323  /**
324  * Set single selection mode on or off. In single selection mode, only one
325  * item can be selected at any time.
326  *
327  * If set, this base class enforces this when items are added or when items
328  * are selected from the application. Note that single selection can also
329  * mean that no item is selected.
330  **/
331  void setEnforceSingleSelection( bool on );
332 
333  /**
334  * In single selection mode, enforce selecting an initial item
335  * ('true' by default). This is ignored in multi selection mode.
336  **/
337  void setEnforceInitialSelection( bool on );
338 
339  /**
340  * Return 'true' if this class enforces an initial selection.
341  **/
342  bool enforceInitialSelection() const;
343 
344  /**
345  * Return 'true' if this base class should select children recursively.
346  **/
347  bool recursiveSelection() const;
348 
349  /**
350  * Recursively try to find the first selected item between iterators
351  * 'begin' and 'end'. Return that item or 0 if there is none.
352  **/
355 
356  /**
357  * Recursively find all selected items between iterators 'begin' and 'end'
358  * and add each of them to the 'selectedItems' YItemCollection.
359  **/
363 
364  /**
365  * Recursively deselect all items between iterators 'begin' and 'end'.
366  **/
368  YItemIterator end );
369  /**
370  * Recursively try to find an item with label 'wantedItemLabel' between
371  * iterators 'begin' and 'end'. Return that item or 0 if there is none.
372  **/
373  YItem * findItem ( const std::string & wantedItemLabel,
375  YItemConstIterator end ) const;
376 
377  /**
378  * Recursively check if 'wantedItem' is between iterators 'begin' and
379  * 'end'.
380  **/
381  bool itemsContain ( YItem * wantedItem,
383  YItemConstIterator end ) const;
384 
385 
386 private:
387 
389 };
390 
391 
392 #endif // YSelectionWidget_h
YWidget::parent
YWidget * parent() const
Return this widget's parent or 0 if it doesn't have a parent.
Definition: YWidget.cc:271
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YSelectionWidget
Base class for various kinds of multi-value widgets.
Definition: YSelectionWidget.h:46
YSelectionWidget::selectedItems
virtual YItemCollection selectedItems()
Return all selected items.
Definition: YSelectionWidget.cc:384
YWidget::end
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
YSelectionWidget::setShortcutString
virtual void setShortcutString(const std::string &str)
Set the string of this widget that holds the keyboard shortcut.
Definition: YSelectionWidget.cc:143
YItemIterator
YItemCollection::iterator YItemIterator
Mutable iterator over YItemCollection.
Definition: YItem.h:42
YItemCollection
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:39
YSelectionWidget::setItemStatus
virtual void setItemStatus(YItem *item, int status)
Set the status of an item.
Definition: YSelectionWidget.cc:451
YSelectionWidget::hasSelectedItem
bool hasSelectedItem()
Return 'true' if any item is selected.
Definition: YSelectionWidget.cc:415
YSelectionWidget::~YSelectionWidget
virtual ~YSelectionWidget()
Destructor.
Definition: YSelectionWidget.cc:74
YSelectionWidgetPrivate
Definition: YSelectionWidget.cc:38
YSelectionWidget::itemsEnd
YItemIterator itemsEnd()
Return an iterator that points behind the last item.
Definition: YSelectionWidget.cc:303
YSelectionWidget::selectedItem
virtual YItem * selectedItem()
Return the (first) selected item or 0 if none is selected.
Definition: YSelectionWidget.cc:349
YSelectionWidget::findItem
YItem * findItem(const std::string &itemLabel) const
Find the (first) item with the specified label.
Definition: YSelectionWidget.cc:513
YWidget::begin
YWidgetListIterator begin()
A helper for the range-based "for" loop.
Definition: YWidget.h:238
YSelectionWidget::label
std::string label() const
Return this widget's label (the caption above the item list).
Definition: YSelectionWidget.cc:99
YSelectionWidget::hasItems
bool hasItems() const
Return 'true' if this widget has any items.
Definition: YSelectionWidget.cc:316
YSelectionWidget::deselectAllItems
virtual void deselectAllItems()
Deselect all items.
Definition: YSelectionWidget.cc:491
YSelectionWidget::enforceSingleSelection
bool enforceSingleSelection() const
Return 'true' if this base class should enforce single selection.
Definition: YSelectionWidget.cc:111
YSelectionWidget::iconBasePath
std::string iconBasePath() const
Return this widget's base path where to look up icons as set with setIconBasePath().
Definition: YSelectionWidget.cc:156
YSelectionWidget::enforceInitialSelection
bool enforceInitialSelection() const
Return 'true' if this class enforces an initial selection.
Definition: YSelectionWidget.cc:124
YSelectionWidget::iconFullPath
std::string iconFullPath(const std::string &iconName) const
Return the full path + file name for the specified icon name.
Definition: YSelectionWidget.cc:162
YSelectionWidget::firstItem
YItem * firstItem() const
Return the first item or 0 if there is none.
Definition: YSelectionWidget.cc:329
YSelectionWidget::setEnforceSingleSelection
void setEnforceSingleSelection(bool on)
Set single selection mode on or off.
Definition: YSelectionWidget.cc:137
ImplPtr< YSelectionWidgetPrivate >
YSelectionWidget::recursiveSelection
bool recursiveSelection() const
Return 'true' if this base class should select children recursively.
Definition: YSelectionWidget.cc:130
YSelectionWidget::setLabel
virtual void setLabel(const std::string &newLabel)
Change this widget's label (the caption above the item list).
Definition: YSelectionWidget.cc:105
YSelectionWidget::widgetClass
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YSelectionWidget.h:72
YSelectionWidget::itemAt
YItem * itemAt(int index) const
Return the item at index 'index' (from 0) or 0 if there is no such item.
Definition: YSelectionWidget.cc:339
YSelectionWidget::findSelectedItem
YItem * findSelectedItem(YItemConstIterator begin, YItemConstIterator end)
Recursively try to find the first selected item between iterators 'begin' and 'end'.
Definition: YSelectionWidget.cc:356
YSelectionWidget::itemsBegin
YItemIterator itemsBegin()
Return an iterator that points to the first item.
Definition: YSelectionWidget.cc:290
YSelectionWidget::selectItem
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
Definition: YSelectionWidget.cc:421
YSelectionWidget::setIconBasePath
void setIconBasePath(const std::string &basePath)
Set this widget's base path where to look up icons.
Definition: YSelectionWidget.cc:150
YSelectionWidget::dumpItems
void dumpItems() const
Dump all items and their selection state to the log.
Definition: YSelectionWidget.cc:545
YSelectionWidget::setItems
void setItems(const YItemCollection &itemCollection)
Delete all items and add new items.
Definition: YSelectionWidget.h:130
YSelectionWidget::setEnforceInitialSelection
void setEnforceInitialSelection(bool on)
In single selection mode, enforce selecting an initial item ('true' by default).
Definition: YSelectionWidget.cc:117
YSelectionWidget::YSelectionWidget
YSelectionWidget(YWidget *parent, const std::string &label, bool enforceSingleSelection, bool recursiveSelection=false)
Constructor.
Definition: YSelectionWidget.cc:59
YSelectionWidget::deleteAllItems
virtual void deleteAllItems()
Delete all items.
Definition: YSelectionWidget.cc:80
YSelectionWidget::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YSelectionWidget.cc:271
YSelectionWidget::shortcutChanged
virtual void shortcutChanged()
Notification that any shortcut of any item was changed by the shortcut conflict manager YShortcutMana...
Definition: YSelectionWidget.h:292
YSelectionWidget::shortcutString
virtual std::string shortcutString() const
Get the string of this widget that holds the keyboard shortcut.
Definition: YSelectionWidget.h:305
YSelectionWidget::itemsCount
int itemsCount() const
Return the number of items.
Definition: YSelectionWidget.cc:322
YSelectionWidget::addItem
virtual void addItem(YItem *item_disown)
Add one item.
Definition: YSelectionWidget.cc:193
YItem.h
YSelectionWidget::itemsContain
bool itemsContain(YItem *item) const
Return 'true' if this widget's items contain the specified item.
Definition: YSelectionWidget.cc:457
YSelectionWidget::findSelectedItems
void findSelectedItems(YItemCollection &selectedItems, YItemConstIterator begin, YItemConstIterator end)
Recursively find all selected items between iterators 'begin' and 'end' and add each of them to the '...
Definition: YSelectionWidget.cc:394
YItemConstIterator
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:45
YItem
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:56