libyui  3.12.1
YTree.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: YTree.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 #ifndef YTree_h
26 #define YTree_h
27 
28 #include "YSelectionWidget.h"
29 
30 class YTreeItem;
31 class YTreePrivate;
32 
33 
34 /**
35  * Tree: List box that displays a (scrollable) list of hierarchical items from
36  * which the user can select exactly one. Each item has a label text and an
37  * optional icon (*).
38  *
39  * This is very similar to SelectionBox, but each item can have subitems that
40  * can be open (expanded) or closed (collapsed).
41  *
42  * The tree widget also has a caption label that is displayed above the
43  * tree. The hotkey displayed in that caption label will move the keyboard
44  * focus into the tree item list.
45  *
46  *
47  * (*) Not all UIs (in particular not text-based UIs) support displaying icons,
48  * so an icon should never be an exclusive means to display any kind of
49  * information.
50 
51  * 'multiSelection' indicates whether or not the user can select multiple
52  * items at the same time. This can only be set in the constructor.
53  *
54  * See also
55  * https://github.com/libyui/libyui-ncurses/blob/master/doc/nctable-and-nctree.md
56  **/
57 class YTree : public YSelectionWidget
58 {
59 protected:
60 
61  /**
62  * Constructor.
63  **/
64  YTree( YWidget * parent,
65  const std::string & label,
66  bool multiSelection,
67  bool recursiveSelection);
68 
69 public:
70 
71  /**
72  * Destructor.
73  **/
74  virtual ~YTree();
75 
76  /**
77  * Returns a descriptive name of this widget class for logging,
78  * debugging etc.
79  **/
80  virtual const char * widgetClass() const { return "YTree"; }
81 
82  /**
83  * Rebuild the displayed tree from the internally stored YTreeItems.
84  *
85  * The application should call this (once) after all items have been added
86  * with addItem(). YTree::addItems() calls this automatically.
87  *
88  * Derived classes are required to implement this.
89  **/
90  virtual void rebuildTree() = 0;
91 
92  /**
93  * Add multiple items. For some UIs, this can be more efficient than
94  * calling addItem() multiple times. This function also automatically calls
95  * rebuildTree() at the end.
96  *
97  * Derived classes can overwrite this function, but they should call this
98  * base class function at the end of the new implementation.
99  *
100  * Reimplemented from YSelectionWidget.
101  **/
102  virtual void addItems( const YItemCollection & itemCollection );
103 
104  /**
105  * Deliver even more events than with notify() set.
106  *
107  * For YTree, this is relevant mostly for the NCurses UI:
108  *
109  * In graphical UIs like the Qt UI, the user can use the mouse to select an
110  * item in a tree. With notify() set, this will send an event right away
111  * (i.e., it will make UserInput and related return, while normally it
112  * would only return when the user clicks a PushButton).
113  *
114  * In the NCurses UI, there is no mouse, so the user has to use the cursor
115  * keys to move to the item he wants to select. In immediateMode(), every
116  * cursor key press will make the tree send an event. Without
117  * immediateMode(), the NCTree will wait until the user hits the [Return]
118  * key until an event is sent. Depending on what the application does upon
119  * each selection box event, immediateMode() might make the application
120  * less responsive.
121  **/
122  bool immediateMode() const;
123 
124  /**
125  * Set immediateMode() on or off.
126  **/
127  void setImmediateMode( bool on = true );
128 
129  /**
130  * Set a property.
131  * Reimplemented from YWidget.
132  *
133  * This function may throw YUIPropertyExceptions.
134  *
135  * This function returns 'true' if the value was successfully set and
136  * 'false' if that value requires special handling (not in error cases:
137  * those are covered by exceptions).
138  **/
139  virtual bool setProperty( const std::string & propertyName,
140  const YPropertyValue & val );
141 
142  /**
143  * Get a property.
144  * Reimplemented from YWidget.
145  *
146  * This method may throw YUIPropertyExceptions.
147  **/
148  virtual YPropertyValue getProperty( const std::string & propertyName );
149 
150  /**
151  * Return this class's property set.
152  * This also initializes the property upon the first call.
153  *
154  * Reimplemented from YWidget.
155  **/
156  virtual const YPropertySet & propertySet();
157 
158  /**
159  * The name of the widget property that will return user input.
160  * Inherited from YWidget.
161  **/
162  const char * userInputProperty() { return YUIProperty_CurrentItem; }
163 
164 
165  /**
166  * Return 'true' if the user can select multiple items at the same time
167  **/
168  bool hasMultiSelection() const;
169 
170  /**
171  * Return the the item that currently has the keyboard focus
172  * or 0 if no item currently has the keyboard focus.
173  *
174  * Notice that for a MultiSelectionBox the current item is not necessarily
175  * selected, i.e., its check box may or may not be checked.
176  *
177  * Derived classes are required to implement this function.
178  **/
179  virtual YTreeItem * currentItem() = 0;
180 
181  /**
182  * Activate the item selected in the tree. This can be used in automated
183  * tests to simulate user input.
184  *
185  * Derived classes are required to implement this.
186  **/
187  virtual void activate() = 0;
188 
189  /**
190  * Return the item in the tree that matches path of labels or 0 if not found.
191  *
192  * 'path' is a vector of strings with the path components, e.g.
193  * ["usr", "share", "doc", "packages"].
194  **/
195  YTreeItem * findItem( const std::vector<std::string> & path ) const;
196 
197 
198 protected:
199 
200  /**
201  * Recursively search the items between item iterators 'begin' and 'end'
202  * for a path specified in a string vector between 'path_begin' and
203  * 'path_end'. Return that item or 0 if not found.
204  *
205  * This is a helper function for findItem( std::vector<std::string> & ).
206  */
207  YTreeItem * findItem( std::vector<std::string>::const_iterator path_begin,
208  std::vector<std::string>::const_iterator path_end,
210  YItemConstIterator end ) const;
211 
212 private:
213 
215 };
216 
217 
218 #endif // YTree_h
YWidget::parent
YWidget * parent() const
Return this widget's parent or 0 if it doesn't have a parent.
Definition: YWidget.cc:271
YTree::userInputProperty
const char * userInputProperty()
The name of the widget property that will return user input.
Definition: YTree.h:162
YTreeItem
Item class for tree items.
Definition: YTreeItem.h:36
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
YTree::setImmediateMode
void setImmediateMode(bool on=true)
Set immediateMode() on or off.
Definition: YTree.cc:78
YWidget::end
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
YItemCollection
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:39
YTree::immediateMode
bool immediateMode() const
Deliver even more events than with notify() set.
Definition: YTree.cc:71
YTree
Tree: List box that displays a (scrollable) list of hierarchical items from which the user can select...
Definition: YTree.h:58
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:198
YTree::YTree
YTree(YWidget *parent, const std::string &label, bool multiSelection, bool recursiveSelection)
Constructor.
Definition: YTree.cc:48
YTree::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YTree.cc:155
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
YTree::currentItem
virtual YTreeItem * currentItem()=0
Return the the item that currently has the keyboard focus or 0 if no item currently has the keyboard ...
YTree::findItem
YTreeItem * findItem(const std::vector< std::string > &path) const
Return the item in the tree that matches path of labels or 0 if not found.
Definition: YTree.cc:182
ImplPtr< YTreePrivate >
YSelectionWidget::recursiveSelection
bool recursiveSelection() const
Return 'true' if this base class should select children recursively.
Definition: YSelectionWidget.cc:130
YTreePrivate
Definition: YTree.cc:39
YTree::activate
virtual void activate()=0
Activate the item selected in the tree.
YTree::widgetClass
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YTree.h:80
YTree::~YTree
virtual ~YTree()
Destructor.
Definition: YTree.cc:64
YTree::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YTree.cc:88
YTree::hasMultiSelection
bool hasMultiSelection() const
Return 'true' if the user can select multiple items at the same time.
Definition: YTree.cc:175
YTree::rebuildTree
virtual void rebuildTree()=0
Rebuild the displayed tree from the internally stored YTreeItems.
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:105
YTree::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YTree.cc:133
YTree::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YTree.cc:96
YItemConstIterator
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:45