libyui  3.0.13
 All Classes Functions Variables Enumerations Friends
YItem.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: YItem.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YItem_h
26 #define YItem_h
27 
28 #include <string>
29 #include <vector>
30 
31 
32 class YItem;
33 
34 typedef std::vector<YItem *> YItemCollection;
35 typedef YItemCollection::iterator YItemIterator;
36 typedef YItemCollection::const_iterator YItemConstIterator;
37 
38 
39 /**
40  * Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc. items.
41  * This class provides stubs for children management.
42  **/
43 class YItem
44 {
45 public:
46  /**
47  * Constructor with just the label and optionally the selected state.
48  **/
49  YItem( const std::string & label, bool selected = false )
50  : _label( label )
51  , _selected( selected )
52  , _index( -1 )
53  , _data( 0 )
54  {}
55 
56  /**
57  * Constructor with label and icon name and optionally the selected state.
58  **/
59  YItem( const std::string & label, const std::string & iconName, bool selected = false )
60  : _label( label )
61  , _iconName( iconName )
62  , _selected( selected )
63  , _index( -1 )
64  , _data( 0 )
65  {}
66 
67  /**
68  * Destructor.
69  **/
70  virtual ~YItem() {}
71 
72  /**
73  * Return this item's label. This is what the user sees in a dialog, so
74  * this will usually be a translated text.
75  **/
76  std::string label() const { return _label; }
77 
78  /**
79  * Set this item's label.
80  **/
81  void setLabel( const std::string & newLabel ) { _label = newLabel; }
82 
83  /**
84  * Return this item's icon name.
85  **/
86  std::string iconName() const { return _iconName; }
87 
88  /**
89  * Return 'true' if this item has an icon name.
90  **/
91  bool hasIconName() const { return ! _iconName.empty(); }
92 
93  /**
94  * Set this item's icon name.
95  **/
96  void setIconName( const std::string & newIconName ) { _iconName = newIconName; }
97 
98  /**
99  * Return 'true' if this item is currently selected.
100  **/
101  bool selected() const { return _selected; }
102 
103  /**
104  * Select or unselect this item. This does not have any effect on any other
105  * item; if it is desired that only one item is selected at any time, the
106  * caller has to take care of that.
107  **/
108  void setSelected( bool sel = true ) { _selected = sel; }
109 
110  /**
111  * Set this item's index.
112  **/
113  void setIndex( int index ) { _index = index; }
114 
115  /**
116  * Return the index of this item (as set with setIndex() ).
117  **/
118  int index() const { return _index; }
119 
120  /**
121  * Set the opaque data pointer for application use.
122  *
123  * Applications can use this to store the pointer to a counterpart of this
124  * tree item. It is the application's responsibility to watch for dangling
125  * pointers and possibliy deleting the data. All this class ever does with
126  * this pointer is to store it.
127  **/
128  void setData( void * newData ) { _data = newData; }
129 
130  /**
131  * Return the opaque data pointer.
132  **/
133  void * data() const { return _data; }
134 
135  //
136  // Children management stubs.
137  //
138  // Derived classes that can handle child items should reimplement those
139  // functions.
140  // The following default implementations don't do anything with children;
141  // they act as if this item didn't have any children.
142  //
143 
144  /**
145  * Return 'true' if this item has any child items.
146  **/
147  virtual bool hasChildren() const { return false; }
148 
149  /**
150  * Return an iterator that points to the first child item of this item.
151  *
152  * This default implementation returns the 'end' iterator of the
153  * class-static always empty _noChildren YItemCollection.
154  * It is safe to use this iterator in classic iterator loops:
155  *
156  * for ( YItemIterator it = myItem->childrenBegin();
157  * it != myItem->childrenEnd();
158  * ++it )
159  * {
160  * ...
161  * }
162  *
163  * The loop body will only ever be executed if this item is a derived class
164  * that actually manages child items.
165  **/
166  virtual YItemIterator childrenBegin() { return _noChildren.end(); }
167  virtual YItemConstIterator childrenBegin() const { return _noChildren.end(); }
168 
169  /**
170  * Return an iterator that points after the last child item of this item.
171  *
172  * This default implementation returns the 'end' iterator of the
173  * class-static always empty _noChildren YItemCollection.
174  **/
175  virtual YItemIterator childrenEnd() { return _noChildren.end(); }
176  virtual YItemConstIterator childrenEnd() const { return _noChildren.end(); }
177 
178  /**
179  * Returns this item's parent item or 0 if it is a toplevel item.
180  * This default implementation always returns 0.
181  * Derived classes that handle children should reimplement this.
182  **/
183  virtual YItem * parent() const { return 0; }
184 
185 
186 private:
187 
188  std::string _label;
189  std::string _iconName;
190  bool _selected;
191  int _index;
192  void * _data;
193 
194  /**
195  * Static children collection that is always empty so the children
196  * iterators of this base class have something valid to return.
197  **/
198  static YItemCollection _noChildren;
199 };
200 
201 
202 
203 #endif // YItem_h
virtual YItem * parent() const
Definition: YItem.h:183
void setIndex(int index)
Definition: YItem.h:113
virtual YItemIterator childrenBegin()
Definition: YItem.h:166
YItem(const std::string &label, bool selected=false)
Definition: YItem.h:49
void setSelected(bool sel=true)
Definition: YItem.h:108
void setIconName(const std::string &newIconName)
Definition: YItem.h:96
void * data() const
Definition: YItem.h:133
int index() const
Definition: YItem.h:118
Definition: YItem.h:43
void setLabel(const std::string &newLabel)
Definition: YItem.h:81
virtual YItemIterator childrenEnd()
Definition: YItem.h:175
virtual bool hasChildren() const
Definition: YItem.h:147
bool selected() const
Definition: YItem.h:101
virtual ~YItem()
Definition: YItem.h:70
std::string iconName() const
Definition: YItem.h:86
void setData(void *newData)
Definition: YItem.h:128
YItem(const std::string &label, const std::string &iconName, bool selected=false)
Definition: YItem.h:59
std::string label() const
Definition: YItem.h:76
bool hasIconName() const
Definition: YItem.h:91