libyui  3.12.1
YTableItem.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: YTableItem.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 #ifndef YTableItem_h
26 #define YTableItem_h
27 
28 #include "YTreeItem.h"
29 
30 
31 class YTableCell;
32 
33 // without "documenting" the file, typedefs will be dropped
34 //! @file
35 
36 //! Collection of pointers to YTableCell
37 typedef std::vector<YTableCell *> YTableCellCollection;
38 
39 //! Mutable iterator over @ref YTableCellCollection
40 typedef YTableCellCollection::iterator YTableCellIterator;
41 
42 //! Const iterator over @ref YTableCellCollection
43 typedef YTableCellCollection::const_iterator YTableCellConstIterator;
44 
45 
46 
47 /**
48  * Item class for YTable items. Each YTableItem corresponds to one row in a
49  * YTable.
50  *
51  * A YTableItem might have any number of cells (columns within this row),
52  * including none. The YTable widget is free to ignore any excess cells if
53  * there are more than the YTable widget has columns. The YTable widget is to
54  * treat nonexistent cells like empty ones.
55  *
56  * Note that while YTable items and their cells can be manipulated through
57  * pointers, their visual representation on screen might be updated only upon
58  * calling certain methods of the YTable widget. See the YTable reference for
59  * details.
60  **/
61 class YTableItem: public YTreeItem
62 {
63 public:
64 
65  /**
66  * Default constructor. Use addCell() to give it any content.
67  **/
68  YTableItem();
69 
70  /**
71  * Constructor for a nested table item, i.e. one with a parent item.
72  **/
74  bool isOpen = false );
75 
76  /**
77  * Convenience constructor for a (toplevel) table item without any icons.
78  *
79  * This will create up to 10 (0..9) cells. Empty cells for empty labels at
80  * the end of the labels are not created, but empty cells in between are.
81  *
82  * new YTableItem( "one", "two", "", "", "five" );
83  *
84  * will create an item with 5 cells:
85  *
86  * cell[0] ==> "one"
87  * cell[1] ==> "two"
88  * cell[2] ==> ""
89  * cell[3] ==> ""
90  * cell[4] ==> "five"
91  **/
92  YTableItem( const std::string & label_0,
93  const std::string & label_1 = std::string(),
94  const std::string & label_2 = std::string(),
95  const std::string & label_3 = std::string(),
96  const std::string & label_4 = std::string(),
97  const std::string & label_5 = std::string(),
98  const std::string & label_6 = std::string(),
99  const std::string & label_7 = std::string(),
100  const std::string & label_8 = std::string(),
101  const std::string & label_9 = std::string() );
102 
103  /**
104  * Convenience constructor for a nested table item without any icons.
105  **/
107  const std::string & label_0,
108  const std::string & label_1 = std::string(),
109  const std::string & label_2 = std::string(),
110  const std::string & label_3 = std::string(),
111  const std::string & label_4 = std::string(),
112  const std::string & label_5 = std::string(),
113  const std::string & label_6 = std::string(),
114  const std::string & label_7 = std::string(),
115  const std::string & label_8 = std::string(),
116  const std::string & label_9 = std::string() );
117 
118  /**
119  * Destructor.
120  *
121  * This will delete all cells.
122  **/
123  virtual ~YTableItem();
124 
125  /**
126  * Returns a descriptive name of this widget class for logging,
127  * debugging etc.
128  **/
129  virtual const char * itemClass() const { return "YTableItem"; }
130 
131  /**
132  * Add a cell. This item will assume ownership over the cell and delete it
133  * when appropriate (when the table is destroyed or when table items are
134  * replaced), at which time the pointer will become invalid.
135  *
136  * Cells can still be changed after they (and the item they belong to) are
137  * added, but in that case, YTable::cellChanged() needs to be called to
138  * update the table display accordingly.
139  **/
140  void addCell( YTableCell * cell_disown );
141 
142  /**
143  * Create a new cell and add it (even if all 'label', 'iconName' and
144  * 'sortKey' are empty).
145  **/
146  void addCell( const std::string & label,
147  const std::string & iconName = std::string(),
148  const std::string & sortKey = std::string() );
149 
150  /**
151  * Add up to 10 cells without any icons.
152  **/
153  void addCells( const std::string & label_0,
154  const std::string & label_1,
155  const std::string & label_2 = std::string(),
156  const std::string & label_3 = std::string(),
157  const std::string & label_4 = std::string(),
158  const std::string & label_5 = std::string(),
159  const std::string & label_6 = std::string(),
160  const std::string & label_7 = std::string(),
161  const std::string & label_8 = std::string(),
162  const std::string & label_9 = std::string() );
163 
164  /**
165  * Delete all cells.
166  **/
167  void deleteCells();
168 
169  /**
170  * Return an iterator that points to the first cell of this item.
171  **/
172  YTableCellIterator cellsBegin() { return _cells.begin(); }
173  YTableCellConstIterator cellsBegin() const { return _cells.begin(); }
174 
175  /**
176  * Return an iterator that points after the last cell of this item.
177  **/
178  YTableCellIterator cellsEnd() { return _cells.end(); }
179  YTableCellConstIterator cellsEnd() const { return _cells.end(); }
180 
181  /**
182  * Return the cell at the specified index (counting from 0 on)
183  * or 0 if there is none.
184  **/
185  const YTableCell * cell( int index ) const;
186  YTableCell * cell( int index );
187 
188  /**
189  * Return the number of cells this item has.
190  **/
191  int cellCount() const { return _cells.size(); }
192 
193  /**
194  * Return 'true' if this item has a cell with the specified index
195  * (counting from 0 on), 'false' otherwise.
196  **/
197  bool hasCell( int index ) const;
198 
199  /**
200  * Return the label of cell no. 'index' (counting from 0 on) or an empty
201  * string if there is no cell with that index.
202  **/
203  std::string label( int index ) const;
204 
205  /**
206  * Return the icon name of cell no. 'index' (counting from 0 on) or an empty
207  * string if there is no cell with that index.
208  **/
209  std::string iconName( int index ) const;
210 
211  /**
212  * Return 'true' if there is a cell with the specified index that has an
213  * icon name.
214  **/
215  bool hasIconName( int index ) const;
216 
217  /**
218  * Just for debugging.
219  **/
220  std::string label() const { return label(0); }
221 
222  /**
223  * Return a descriptive label of this item instance for debugging.
224  * This might be truncated if the original label is too long.
225  **/
226  virtual std::string debugLabel() const;
227 
228 
229 private:
230 
231  // Disable unwanted base class methods. They don't make sense in this
232  // context since there is not just one single label or icon name, but one
233  // for each cell.
234 
235  std::string iconName() const { return ""; }
236  bool hasIconName() const { return false; }
237  void setLabel ( const std::string & ) {}
238  void setIconName ( const std::string & ) {}
239 
240 
241  //
242  // Data members
243  //
244 
245  YTableCellCollection _cells;
246 };
247 
248 
249 
250 /**
251  * One cell (one column in one row) of a YTableItem. Each cell has a label (a
252  * user visible text), optionally an icon (*) and also optionally a sort-key.
253  *
254  * Note that cells don't have individual IDs; they have just an index.
255  * The first cell in an item is cell(0). In an ideal world, each YTableItem
256  * would have exactly as many cells as there are columns in the YTable, but
257  * these classes make no such assumptions. A YTableItem might have any number
258  * of cells, including none.
259  *
260  * The YTable widget is free to ignore any excess cells if there are more than
261  * the YTable widget has columns. If there are less cells than the table has
262  * columns, the nonexistent cells will be treated as empty.
263  *
264  *
265  * (*) Not all UIs can handle icons. UIs that can't handle them will simply
266  * ignore any icons specified for YTableCells. Thus, applications should either
267  * check the UI capabilities if it can handle icons or use icons only as an
268  * additional visual cue that still has a text counterpart (so the user can
269  * still make sense of the table content when no icons are visible).
270  **/
272 {
273 public:
274  /**
275  * Constructor with label and optional icon name and optional sort
276  * key for cells that don't have a parent item yet (that will be
277  * added to a parent later with setParent()).
278  **/
279  YTableCell( const std::string & label, const std::string & iconName = "",
280  const std::string & sortKey = "" )
281  : _label( label )
282  , _iconName( iconName )
283  , _sortKey( sortKey )
284  , _parent( 0 )
285  , _column ( -1 )
286  {}
287 
288  /**
289  * Constructor with parent, column no., label and optional icon name for
290  * cells that are created with a parent.
291  **/
293  int column,
294  const std::string & label,
295  const std::string & iconName = "",
296  const std::string & sortKey = "" )
297  : _label( label )
298  , _iconName( iconName )
299  , _sortKey( sortKey )
300  , _parent( parent )
301  , _column ( column )
302  {}
303 
304  /**
305  * Destructor. Not strictly needed inside this class, but useful for
306  * derived classes. Since this is the only virtual method of this class,
307  * the cost of this is a vtable for this class and a pointer to the vtable
308  * in each instance.
309  **/
310  virtual ~YTableCell() {}
311 
312  /**
313  * Return this cells's label. This is what the user sees in a dialog, so
314  * this will usually be a translated text.
315  **/
316  std::string label() const { return _label; }
317 
318  /**
319  * Set this cell's label.
320  *
321  * If this is called after the corresponding table item (table row) is
322  * added to the table widget, call YTable::cellChanged() to notify the
323  * table widget about the fact. Only then will the display be updated.
324  **/
325  void setLabel( const std::string & newLabel ) { _label = newLabel; }
326 
327  /**
328  * Return this cell's icon name.
329  **/
330  std::string iconName() const { return _iconName; }
331 
332  /**
333  * Return 'true' if this cell has an icon name.
334  **/
335  bool hasIconName() const { return ! _iconName.empty(); }
336 
337  /**
338  * Set this cell's icon name.
339  *
340  * If this is called after the corresponding table item (table row) is
341  * added to the table widget, call YTable::cellChanged() to notify the
342  * table widget about the fact. Only then will the display be updated.
343  **/
344  void setIconName( const std::string & newIconName ) { _iconName = newIconName; }
345 
346  /**
347  * Return this cell's sort key.
348  **/
349  std::string sortKey() const { return _sortKey; }
350 
351  /**
352  * Return 'true' if this cell has a sort key.
353  **/
354  bool hasSortKey() const { return ! _sortKey.empty(); }
355 
356  /**
357  * Set this cell's sort key.
358  *
359  * If this is called after the corresponding table item (table row) is
360  * added to the table widget, call YTable::cellChanged() to notify the
361  * table widget about the fact. Only then will the display be updated.
362  **/
363  void setSortKey( const std::string & newSortKey ) { _sortKey = newSortKey; }
364 
365  /**
366  * Return this cell's parent item or 0 if it doesn't have one yet.
367  **/
368  YTableItem * parent() const { return _parent; }
369 
370  /**
371  * Return this cell's column no. (counting from 0on) or -1 if it doesn't
372  * have a parent yet.
373  **/
374  int column() const { return _column; }
375 
376  /**
377  * Convenience function: Return this cell's parent item's index within its
378  * table widget or -1 if there is no parent item or no parent table.
379  **/
380  int itemIndex() const { return _parent ? _parent->index() : -1; }
381 
382  /**
383  * Set this cell's parent item and column no. if it doesn't have a parent
384  * yet.
385  *
386  * This method will throw an exception if the cell already has a parent.
387  **/
388  void reparent( YTableItem * parent, int column );
389 
390 
391 private:
392 
393  std::string _label;
394  std::string _iconName;
395  std::string _sortKey;
396  YTableItem * _parent;
397  int _column;
398 };
399 
400 
401 #endif // YTableItem_h
YTreeItem
Item class for tree items.
Definition: YTreeItem.h:36
YTableCell::label
std::string label() const
Return this cells's label.
Definition: YTableItem.h:316
YTableItem::cellsBegin
YTableCellIterator cellsBegin()
Return an iterator that points to the first cell of this item.
Definition: YTableItem.h:172
YTableItem::cellCount
int cellCount() const
Return the number of cells this item has.
Definition: YTableItem.h:191
YItem::index
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:153
YTableCell::parent
YTableItem * parent() const
Return this cell's parent item or 0 if it doesn't have one yet.
Definition: YTableItem.h:368
YTableItem::debugLabel
virtual std::string debugLabel() const
Return a descriptive label of this item instance for debugging.
Definition: YTableItem.cc:238
YTableItem::label
std::string label() const
Just for debugging.
Definition: YTableItem.h:220
YTableCell::iconName
std::string iconName() const
Return this cell's icon name.
Definition: YTableItem.h:330
YTableCell::column
int column() const
Return this cell's column no.
Definition: YTableItem.h:374
YTableCell::YTableCell
YTableCell(YTableItem *parent, int column, const std::string &label, const std::string &iconName="", const std::string &sortKey="")
Constructor with parent, column no., label and optional icon name for cells that are created with a p...
Definition: YTableItem.h:292
YTableCell::setIconName
void setIconName(const std::string &newIconName)
Set this cell's icon name.
Definition: YTableItem.h:344
YTableCell::reparent
void reparent(YTableItem *parent, int column)
Set this cell's parent item and column no.
Definition: YTableItem.cc:263
YTableCell::itemIndex
int itemIndex() const
Convenience function: Return this cell's parent item's index within its table widget or -1 if there i...
Definition: YTableItem.h:380
YTableItem::itemClass
virtual const char * itemClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YTableItem.h:129
YTreeItem::isOpen
bool isOpen() const
Return 'true' if this tree item should be displayed open (with its children visible) by default.
Definition: YTreeItem.cc:101
YTableItem
Item class for YTable items.
Definition: YTableItem.h:62
YTableItem::deleteCells
void deleteCells()
Delete all cells.
Definition: YTableItem.cc:111
YTableCellCollection
std::vector< YTableCell * > YTableCellCollection
Collection of pointers to YTableCell.
Definition: YTableItem.h:37
YTreeItem::parent
virtual YTreeItem * parent() const
Returns this item's parent item or 0 if it is a toplevel item.
Definition: YTreeItem.h:134
YTableItem::cellsEnd
YTableCellIterator cellsEnd()
Return an iterator that points after the last cell of this item.
Definition: YTableItem.h:178
YTableCell::hasIconName
bool hasIconName() const
Return 'true' if this cell has an icon name.
Definition: YTableItem.h:335
YTableCell::setLabel
void setLabel(const std::string &newLabel)
Set this cell's label.
Definition: YTableItem.h:325
YTableItem::YTableItem
YTableItem()
Default constructor.
Definition: YTableItem.cc:38
YTableCellIterator
YTableCellCollection::iterator YTableCellIterator
Mutable iterator over YTableCellCollection.
Definition: YTableItem.h:40
YTableItem::addCells
void addCells(const std::string &label_0, const std::string &label_1, const std::string &label_2=std::string(), const std::string &label_3=std::string(), const std::string &label_4=std::string(), const std::string &label_5=std::string(), const std::string &label_6=std::string(), const std::string &label_7=std::string(), const std::string &label_8=std::string(), const std::string &label_9=std::string())
Add up to 10 cells without any icons.
Definition: YTableItem.cc:149
YTableCell::hasSortKey
bool hasSortKey() const
Return 'true' if this cell has a sort key.
Definition: YTableItem.h:354
YTableCell::YTableCell
YTableCell(const std::string &label, const std::string &iconName="", const std::string &sortKey="")
Constructor with label and optional icon name and optional sort key for cells that don't have a paren...
Definition: YTableItem.h:279
YTableItem::cell
const YTableCell * cell(int index) const
Return the cell at the specified index (counting from 0 on) or 0 if there is none.
Definition: YTableItem.cc:201
YTableCellConstIterator
YTableCellCollection::const_iterator YTableCellConstIterator
Const iterator over YTableCellCollection.
Definition: YTableItem.h:43
YTableCell::~YTableCell
virtual ~YTableCell()
Destructor.
Definition: YTableItem.h:310
YTableItem::~YTableItem
virtual ~YTableItem()
Destructor.
Definition: YTableItem.cc:104
YTableItem::hasIconName
bool hasIconName(int index) const
Return 'true' if there is a cell with the specified index that has an icon name.
Definition: YTableItem.cc:231
YTableCell::setSortKey
void setSortKey(const std::string &newSortKey)
Set this cell's sort key.
Definition: YTableItem.h:363
YTableItem::addCell
void addCell(YTableCell *cell_disown)
Add a cell.
Definition: YTableItem.cc:127
YTableCell
One cell (one column in one row) of a YTableItem.
Definition: YTableItem.h:272
YTableItem::hasCell
bool hasCell(int index) const
Return 'true' if this item has a cell with the specified index (counting from 0 on),...
Definition: YTableItem.cc:194
YTableCell::sortKey
std::string sortKey() const
Return this cell's sort key.
Definition: YTableItem.h:349