libyui  3.12.1
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 <shundhammer@suse.de>
22 
23 /-*/
24 
25 #define MIN_DEBUG_LABEL_LEN 20
26 #define MAX_DEBUG_LABEL_LEN 40
27 
28 
29 #define YUILogComponent "ui"
30 #include "YUILog.h"
31 
32 #include "YTableItem.h"
33 #include "YUIException.h"
34 
35 using std::string;
36 
37 
39  : YTreeItem( "" )
40 {
41  // NOP
42 }
43 
44 
46  bool isOpen )
47  : YTreeItem( parent, "", isOpen )
48 {
49  // NOP
50 }
51 
52 
53 YTableItem::YTableItem( const string & label_0,
54  const string & label_1,
55  const string & label_2,
56  const string & label_3,
57  const string & label_4,
58  const string & label_5,
59  const string & label_6,
60  const string & label_7,
61  const string & label_8,
62  const string & label_9 )
63  : YTreeItem( "" )
64 {
65  addCells( label_0,
66  label_1,
67  label_2,
68  label_3,
69  label_4,
70  label_5,
71  label_6,
72  label_7,
73  label_8,
74  label_9 );
75 }
76 
77 
79  const string & label_0,
80  const string & label_1,
81  const string & label_2,
82  const string & label_3,
83  const string & label_4,
84  const string & label_5,
85  const string & label_6,
86  const string & label_7,
87  const string & label_8,
88  const string & label_9 )
89  : YTreeItem( parent, "" )
90 {
91  addCells( label_0,
92  label_1,
93  label_2,
94  label_3,
95  label_4,
96  label_5,
97  label_6,
98  label_7,
99  label_8,
100  label_9 );
101 }
102 
103 
105 {
106  deleteCells();
107 }
108 
109 
110 void
112 {
114 
115  while ( it != cellsEnd() )
116  {
117  YTableCell * cell = *it;
118  ++it;
119  delete cell;
120  }
121 
122  _cells.clear();
123 }
124 
125 
126 void
128 {
129  YUI_CHECK_PTR( cell );
130  _cells.push_back( cell );
131 
132  cell->reparent( this, _cells.size() - 1 );
133 }
134 
135 
136 void
137 YTableItem::addCell( const string & label,
138  const string & iconName,
139  const string & sortKey )
140 {
141  YTableCell * cell = new YTableCell( label, iconName, sortKey );
142  YUI_CHECK_NEW( cell );
143 
144  addCell( cell );
145 }
146 
147 
148 void
149 YTableItem::addCells( const std::string & label_0,
150  const std::string & label_1,
151  const std::string & label_2,
152  const std::string & label_3,
153  const std::string & label_4,
154  const std::string & label_5,
155  const std::string & label_6,
156  const std::string & label_7,
157  const std::string & label_8,
158  const std::string & label_9 )
159 {
160  std::vector<string> labels;
161  labels.reserve(10); // slight optimization
162  labels.push_back( label_0 );
163  labels.push_back( label_1 );
164  labels.push_back( label_2 );
165  labels.push_back( label_3 );
166  labels.push_back( label_4 );
167  labels.push_back( label_5 );
168  labels.push_back( label_6 );
169  labels.push_back( label_7 );
170  labels.push_back( label_8 );
171  labels.push_back( label_9 );
172 
173  //
174  // Find the last non-empty label
175  //
176 
177  unsigned lastLabel = labels.size() - 1;
178 
179  while ( labels[ lastLabel ].empty() && --lastLabel > 0 )
180  {}
181 
182  //
183  // Create cells
184  //
185 
186  for ( unsigned i = 0; i <= lastLabel; ++i )
187  {
188  addCell( labels[i] );
189  }
190 }
191 
192 
193 bool
194 YTableItem::hasCell( int index ) const
195 {
196  return index >= 0 && (unsigned) index < _cells.size();
197 }
198 
199 
200 const YTableCell *
201 YTableItem::cell( int index ) const
202 {
203  return hasCell( index ) ?
204  _cells[ index ] : 0;
205 }
206 
207 
208 YTableCell *
209 YTableItem::cell( int index )
210 {
211  return hasCell( index ) ?
212  _cells[ index ] : 0;
213 }
214 
215 
216 string
217 YTableItem::label( int index ) const
218 {
219  return hasCell( index ) ? _cells[ index ]->label() : "";
220 }
221 
222 
223 string
224 YTableItem::iconName( int index ) const
225 {
226  return hasCell( index ) ? _cells[ index ]->iconName() : "";
227 }
228 
229 
230 bool
231 YTableItem::hasIconName( int index ) const
232 {
233  return hasCell( index ) ? _cells[ index ]->hasIconName() : false;
234 }
235 
236 
237 string
239 {
240  if ( _cells.empty() )
241  return "[empty]";
242 
243  string txt;
244 
245  for ( unsigned i=0; i < _cells.size(); ++i )
246  {
247  if ( ! txt.empty() )
248  txt += " | ";
249 
250  txt += label( i );
251 
252  if ( txt.size() > MIN_DEBUG_LABEL_LEN )
253  break;
254  }
255 
256  return limitLength( txt, MAX_DEBUG_LABEL_LEN );
257 }
258 
259 
260 //----------------------------------------------------------------------
261 
262 
263 void YTableCell::reparent( YTableItem * parent, int column )
264 {
265  YUI_CHECK_PTR( parent );
266 
267  if ( _parent && _parent != parent && _column != column )
268  YUI_THROW( YUIException( string( "Cannot reparent YTableCell \"" )
269  + _label
270  + "to different parent." ) );
271  _parent = parent;
272  _column = column;
273 }
YTreeItem
Item class for tree items.
Definition: YTreeItem.h:36
YTableItem::cellsBegin
YTableCellIterator cellsBegin()
Return an iterator that points to the first cell of this item.
Definition: YTableItem.h:172
YItem::index
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:153
YTableItem::iconName
std::string iconName(int index) const
Return the icon name of cell no.
Definition: YTableItem.cc:224
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::column
int column() const
Return this cell's column no.
Definition: YTableItem.h:374
YTableCell::reparent
void reparent(YTableItem *parent, int column)
Set this cell's parent item and column no.
Definition: YTableItem.cc:263
YTableItem
Item class for YTable items.
Definition: YTableItem.h:62
YTableItem::deleteCells
void deleteCells()
Delete all cells.
Definition: YTableItem.cc:111
YTableItem::cellsEnd
YTableCellIterator cellsEnd()
Return an iterator that points after the last cell of this item.
Definition: YTableItem.h:178
YTableItem::YTableItem
YTableItem()
Default constructor.
Definition: YTableItem.cc:38
YTableItem.h
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
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
YItem::limitLength
std::string limitLength(const std::string &text, int limit) const
Return a string of maximum 'limit' characters.
Definition: YItem.cc:51
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
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
YUIException
Base class for UI Exceptions.
Definition: YUIException.h:298