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