libyui  3.4.2
YTableItem.cc
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.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include "YTableItem.h"
26 #include "YUIException.h"
27 
28 
30  : YItem( "" )
31 {
32  // NOP
33 }
34 
35 
36 YTableItem::YTableItem( const std::string & label_0,
37  const std::string & label_1,
38  const std::string & label_2,
39  const std::string & label_3,
40  const std::string & label_4,
41  const std::string & label_5,
42  const std::string & label_6,
43  const std::string & label_7,
44  const std::string & label_8,
45  const std::string & label_9 )
46  : YItem( "" )
47 {
48  std::vector<std::string> labels;
49  labels.reserve(10); // slight optimization
50  labels.push_back( label_0 );
51  labels.push_back( label_1 );
52  labels.push_back( label_2 );
53  labels.push_back( label_3 );
54  labels.push_back( label_4 );
55  labels.push_back( label_5 );
56  labels.push_back( label_6 );
57  labels.push_back( label_7 );
58  labels.push_back( label_8 );
59  labels.push_back( label_9 );
60 
61  //
62  // Find the last non-empty label
63  //
64 
65  unsigned lastLabel = labels.size() - 1;
66 
67  while ( labels[ lastLabel ].empty() && --lastLabel > 0 )
68  {}
69 
70  //
71  // Create cells
72  //
73 
74  for ( unsigned i = 0; i <= lastLabel; ++i )
75  {
76  addCell( labels[i] );
77  }
78 }
79 
80 
81 
83 {
84  deleteCells();
85 }
86 
87 
88 void
90 {
92 
93  while ( it != cellsEnd() )
94  {
95  YTableCell * cell = *it;
96  ++it;
97  delete cell;
98  }
99 
100  _cells.clear();
101 }
102 
103 
104 void
106 {
107  YUI_CHECK_PTR( cell );
108  _cells.push_back( cell );
109 
110  cell->reparent( this, _cells.size() - 1 );
111 }
112 
113 
114 void
115 YTableItem::addCell( const std::string & label, const std::string & iconName )
116 {
117  YTableCell * cell = new YTableCell( label, iconName );
118  YUI_CHECK_NEW( cell );
119 
120  addCell( cell );
121 }
122 
123 
124 bool
126 {
127  return index >= 0 && (unsigned) index < _cells.size();
128 }
129 
130 
131 const YTableCell *
133 {
134  return hasCell( index ) ?
135  _cells[ index ] : 0;
136 }
137 
138 
139 YTableCell *
140 YTableItem::cell( int index )
141 {
142  return hasCell( index ) ?
143  _cells[ index ] : 0;
144 }
145 
146 
147 std::string
149 {
150  return hasCell( index ) ? _cells[ index ]->label() : "";
151 }
152 
153 
154 std::string
156 {
157  return hasCell( index ) ? _cells[ index ]->iconName() : "";
158 }
159 
160 
161 bool
163 {
164  return hasCell( index ) ? _cells[ index ]->hasIconName() : false;
165 }
166 
167 
168 
169 
170 
172 {
173  YUI_CHECK_PTR( parent );
174 
175  if ( _parent && _parent != parent && _column != column )
176  YUI_THROW( YUIException( std::string( "Cannot reparent YTableCell \"" )
177  + _label
178  + "to different parent." ) );
179  _parent = parent;
180  _column = column;
181 }
std::string label() const
Just for debugging.
Definition: YTableItem.h:174
void deleteCells()
Delete all cells.
Definition: YTableItem.cc:89
std::string iconName(int index) const
Return the icon name of cell no.
Definition: YTableItem.cc:155
void addCell(YTableCell *cell_disown)
Add a cell.
Definition: YTableItem.cc:105
bool hasCell(int index) const
Return &#39;true&#39; if this item has a cell with the specified index (counting from 0 on), &#39;false&#39; otherwise.
Definition: YTableItem.cc:125
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:132
virtual ~YTableItem()
Destructor.
Definition: YTableItem.cc:82
One cell (one column in one row) of a YTableItem.
Definition: YTableItem.h:218
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:124
YTableCellCollection::iterator YTableCellIterator
Mutable iterator over YTableCellCollection.
Definition: YTableItem.h:39
virtual YItem * parent() const
Returns this item&#39;s parent item or 0 if it is a toplevel item.
Definition: YItem.h:189
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:49
bool hasIconName(int index) const
Return &#39;true&#39; if there is a cell with the specified index that has an icon name.
Definition: YTableItem.cc:162
YTableCellIterator cellsEnd()
Return an iterator that points after the last cell of this item.
Definition: YTableItem.h:132
void reparent(YTableItem *parent, int column)
Set this cell&#39;s parent item and column no.
Definition: YTableItem.cc:171
Item class for YTable items.
Definition: YTableItem.h:58
YTableItem()
Default constructor.
Definition: YTableItem.cc:29
Base class for UI Exceptions.
Definition: YUIException.h:297
YTableCellIterator cellsBegin()
Return an iterator that points to the first cell of this item.
Definition: YTableItem.h:126