libyui  3.12.1
YItem.h
Go to the documentation of this file.
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: YItem.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 #ifndef YItem_h
26 #define YItem_h
27 
28 #include <string>
29 #include <vector>
30 #include <iosfwd>
31 
32 
33 class YItem;
34 
35 // without "documenting" the file, typedefs will be dropped
36 //! @file
37 
38 //! Collection of pointers to YItem.
39 typedef std::vector<YItem *> YItemCollection;
40 
41 //! Mutable iterator over @ref YItemCollection.
42 typedef YItemCollection::iterator YItemIterator;
43 
44 //! Const iterator over @ref YItemCollection.
45 typedef YItemCollection::const_iterator YItemConstIterator;
46 
47 
48 /**
49  * Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc. items.
50  * This class provides stubs for children management.
51  *
52  * See also
53  * https://github.com/libyui/libyui-ncurses/blob/master/doc/nctable-and-nctree.md
54  **/
55 class YItem
56 {
57 public:
58  /**
59  * Constructor with just the label and optionally the selected state.
60  **/
61  YItem( const std::string & label,
62  bool selected = false )
63  : _label( label )
64  , _status( selected ? 1 : 0 )
65  , _index( -1 )
66  , _data( 0 )
67  {}
68 
69  /**
70  * Constructor with label and icon name and optionally the selected state.
71  **/
72  YItem( const std::string & label,
73  const std::string & iconName,
74  bool selected = false )
75  : _label( label )
76  , _iconName( iconName )
77  , _status( selected ? 1 : 0 )
78  , _index( -1 )
79  , _data( 0 )
80  {}
81 
82  /**
83  * Destructor.
84  **/
85  virtual ~YItem() {}
86 
87  /**
88  * Returns a descriptive name of this widget class for logging,
89  * debugging etc.
90  **/
91  virtual const char * itemClass() const { return "YItem"; }
92 
93  /**
94  * Return this item's label. This is what the user sees in a dialog, so
95  * this will usually be a translated text.
96  **/
97  std::string label() const { return _label; }
98 
99  /**
100  * Set this item's label.
101  **/
102  void setLabel( const std::string & newLabel ) { _label = newLabel; }
103 
104  /**
105  * Return this item's icon name.
106  **/
107  std::string iconName() const { return _iconName; }
108 
109  /**
110  * Return 'true' if this item has an icon name.
111  **/
112  bool hasIconName() const { return ! _iconName.empty(); }
113 
114  /**
115  * Set this item's icon name.
116  **/
117  void setIconName( const std::string & newIconName ) { _iconName = newIconName; }
118 
119  /**
120  * Return 'true' if this item is currently selected.
121  **/
122  bool selected() const { return _status != 0; }
123 
124  /**
125  * Select or unselect this item. This does not have any effect on any other
126  * item; if it is desired that only one item is selected at any time, the
127  * caller has to take care of that.
128  **/
129  void setSelected( bool sel = true ) { _status = sel ? 1 : 0; }
130 
131  /**
132  * Return the status of this item. This is a bit more generalized than
133  * 'selected'. Values other than 0 or 1 can mean different things to the
134  * application or to the specific widget.
135  **/
136  int status() const { return _status; }
137 
138  /**
139  * Set the status of this item. Most widgets only use 0 for "not selected"
140  * or nonzero for "selected". Some widgets may make use of other values as
141  * well.
142  **/
143  void setStatus( int newStatus ) { _status = newStatus; }
144 
145  /**
146  * Set this item's index.
147  **/
148  void setIndex( int index ) { _index = index; }
149 
150  /**
151  * Return the index of this item (as set with setIndex() ).
152  **/
153  int index() const { return _index; }
154 
155  /**
156  * Set the opaque data pointer for application use.
157  *
158  * Applications can use this to store the pointer to a counterpart of this
159  * tree item. It is the application's responsibility to watch for dangling
160  * pointers and possibliy deleting the data. All this class ever does with
161  * this pointer is to store it.
162  **/
163  void setData( void * newData ) { _data = newData; }
164 
165  /**
166  * Return the opaque data pointer.
167  **/
168  void * data() const { return _data; }
169 
170  //
171  // Children management stubs.
172  //
173  // Derived classes that can handle child items should reimplement those
174  // functions.
175  // The following default implementations don't do anything with children;
176  // they act as if this item didn't have any children.
177  //
178 
179  /**
180  * Return 'true' if this item has any child items.
181  **/
182  virtual bool hasChildren() const { return false; }
183 
184  /**
185  * Return an iterator that points to the first child item of this item.
186  *
187  * This default implementation returns the 'end' iterator of the
188  * class-static always empty _noChildren YItemCollection.
189  * It is safe to use this iterator in classic iterator loops:
190  *
191  * for ( YItemIterator it = myItem->childrenBegin();
192  * it != myItem->childrenEnd();
193  * ++it )
194  * {
195  * ...
196  * }
197  *
198  * The loop body will only ever be executed if this item is a derived class
199  * that actually manages child items.
200  **/
201  virtual YItemIterator childrenBegin() { return _noChildren.end(); }
202  virtual YItemConstIterator childrenBegin() const { return _noChildren.end(); }
203 
204  /**
205  * Return an iterator that points after the last child item of this item.
206  *
207  * This default implementation returns the 'end' iterator of the
208  * class-static always empty _noChildren YItemCollection.
209  **/
210  virtual YItemIterator childrenEnd() { return _noChildren.end(); }
211  virtual YItemConstIterator childrenEnd() const { return _noChildren.end(); }
212 
213  /**
214  * Return this item's parent item or 0 if it is a toplevel item.
215  * This default implementation always returns 0.
216  * Derived classes that handle children should reimplement this.
217  **/
218  virtual YItem * parent() const { return 0; }
219 
220  /**
221  * Return a descriptive label of this item instance for debugging.
222  * This might be truncated if the original label is too long.
223  **/
224  virtual std::string debugLabel() const;
225 
226  /**
227  * Return a string of maximum 'limit' characters. Add an ellipsis ("...")
228  * if it was truncated.
229  **/
230  std::string limitLength( const std::string & text, int limit ) const;
231 
232 
233 private:
234 
235  std::string _label;
236  std::string _iconName;
237  int _status;
238  int _index;
239  void * _data;
240 
241  /**
242  * Static children collection that is always empty so the children
243  * iterators of this base class have something valid to return.
244  **/
245  static YItemCollection _noChildren;
246 };
247 
248 
249 std::ostream & operator<<( std::ostream & stream, const YItem * item );
250 
251 
252 
253 #endif // YItem_h
YItem::label
std::string label() const
Return this item's label.
Definition: YItem.h:97
YItem::setData
void setData(void *newData)
Set the opaque data pointer for application use.
Definition: YItem.h:163
YItem::setIndex
void setIndex(int index)
Set this item's index.
Definition: YItem.h:148
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
YItem::index
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:153
YItem::childrenBegin
virtual YItemIterator childrenBegin()
Return an iterator that points to the first child item of this item.
Definition: YItem.h:201
YItem::selected
bool selected() const
Return 'true' if this item is currently selected.
Definition: YItem.h:122
YItem::iconName
std::string iconName() const
Return this item's icon name.
Definition: YItem.h:107
YItem::hasIconName
bool hasIconName() const
Return 'true' if this item has an icon name.
Definition: YItem.h:112
YItem::setIconName
void setIconName(const std::string &newIconName)
Set this item's icon name.
Definition: YItem.h:117
YItem::parent
virtual YItem * parent() const
Return this item's parent item or 0 if it is a toplevel item.
Definition: YItem.h:218
YItem::data
void * data() const
Return the opaque data pointer.
Definition: YItem.h:168
YItem::hasChildren
virtual bool hasChildren() const
Return 'true' if this item has any child items.
Definition: YItem.h:182
YItem::YItem
YItem(const std::string &label, bool selected=false)
Constructor with just the label and optionally the selected state.
Definition: YItem.h:61
YItem::debugLabel
virtual std::string debugLabel() const
Return a descriptive label of this item instance for debugging.
Definition: YItem.cc:44
YItem::status
int status() const
Return the status of this item.
Definition: YItem.h:136
YItem::setSelected
void setSelected(bool sel=true)
Select or unselect this item.
Definition: YItem.h:129
YItem::setLabel
void setLabel(const std::string &newLabel)
Set this item's label.
Definition: YItem.h:102
YItem::childrenEnd
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YItem.h:210
YItem::limitLength
std::string limitLength(const std::string &text, int limit) const
Return a string of maximum 'limit' characters.
Definition: YItem.cc:51
YItem::YItem
YItem(const std::string &label, const std::string &iconName, bool selected=false)
Constructor with label and icon name and optionally the selected state.
Definition: YItem.h:72
YItem::setStatus
void setStatus(int newStatus)
Set the status of this item.
Definition: YItem.h:143
YItem::itemClass
virtual const char * itemClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YItem.h:91
YItem::~YItem
virtual ~YItem()
Destructor.
Definition: YItem.h:85
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