libyui  3.9.3
YTable.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: YTable.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include "YUISymbols.h"
30 #include "YTable.h"
31 
32 using std::string;
33 
34 
36 {
37  YTablePrivate( YTableHeader * header )
38  : header( header )
39  , keepSorting( false )
40  , immediateMode( false )
41  {
42  }
43 
44  YTableHeader * header;
45  bool keepSorting;
46  bool immediateMode;
47 };
48 
49 
50 
51 
52 YTable::YTable( YWidget * parent, YTableHeader * header, bool multiSelection )
53  : YSelectionWidget( parent,
54  "", // label
55  ! multiSelection ) // enforceSingleSelection
56  , priv( new YTablePrivate( header ) )
57 {
58  YUI_CHECK_PTR( header );
59  YUI_CHECK_NEW( priv );
60 
61  setDefaultStretchable( YD_HORIZ, true );
62  setDefaultStretchable( YD_VERT, true );
63 }
64 
65 
67 {
68  if ( priv->header )
69  delete priv->header;
70 }
71 
72 
73 void
75 {
76  YUI_CHECK_PTR( newHeader );
77 
78  if ( priv->header->columns() != newHeader->columns() )
80 
81  delete priv->header;
82  priv->header = newHeader;
83 }
84 
85 
86 int
88 {
89  return priv->header->columns();
90 }
91 
92 
93 bool
94 YTable::hasColumn( int column ) const
95 {
96  return priv->header->hasColumn( column );
97 }
98 
99 
100 string
101 YTable::header( int column ) const
102 {
103  return priv->header->header( column );
104 }
105 
106 
107 YAlignmentType
108 YTable::alignment( int column ) const
109 {
110  return priv->header->alignment( column );
111 }
112 
113 
114 bool
116 {
117  return priv->immediateMode;
118 }
119 
120 
121 void
122 YTable::setImmediateMode( bool immediateMode )
123 {
124  priv->immediateMode = immediateMode;
125 
126  if ( immediateMode )
127  setNotify( true );
128 }
129 
130 
131 bool
133 {
134  return priv->keepSorting;
135 }
136 
137 
138 void
139 YTable::setKeepSorting( bool keepSorting )
140 {
141  priv->keepSorting = keepSorting;
142 }
143 
144 
145 bool
147 {
149 }
150 
151 YItem *
152 YTable::findItem( const string & wantedItemLabel, int column ) const
153 {
154  return YTable::findItem( wantedItemLabel, column, itemsBegin(), itemsEnd() );
155 }
156 
157 
158 YItem *
159 YTable::findItem( const string & wantedItemLabel,
160  int column,
161  YItemConstIterator begin,
162  YItemConstIterator end ) const
163 {
164  if ( ! hasColumn( column ) )
165  return nullptr;
166 
167  for ( YItemConstIterator it = begin; it != end; ++it )
168  {
169  auto * item = dynamic_cast<YTableItem *>(*it);
170 
171  if ( item && item->label( column ) == wantedItemLabel )
172  return item;
173  }
174 
175  return nullptr;
176 }
177 
178 const YPropertySet &
180 {
181  static YPropertySet propSet;
182 
183  if ( propSet.isEmpty() )
184  {
185  /*
186  * @property itemID Value The currently selected item
187  * @property itemID CurrentItem The currently selected item
188  * @property itemList Items All items
189  * @property itemList SelectedItems All currently selected items
190  * @property string Cell One cell (one column of one item)
191  * @property integer Cell (ChangeWidget only) One cell as integer
192  * @property `icon(...) Cell Icon for one one cell
193  * @property string Item Alias for Cell
194  * @property string Item QueryWidget only: Return one complete item
195  * @property string IconPath Base path for icons
196  * @property bool MultiSelection Flag: User can select multiple items (read-only)
197  */
198  propSet.add( YProperty( YUIProperty_Value, YOtherProperty ) );
199  propSet.add( YProperty( YUIProperty_CurrentItem, YOtherProperty ) );
200  propSet.add( YProperty( YUIProperty_SelectedItems, YOtherProperty ) );
201  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
202  propSet.add( YProperty( YUIProperty_Cell, YOtherProperty ) );
203  propSet.add( YProperty( YUIProperty_Item, YOtherProperty ) );
204  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
205  propSet.add( YProperty( YUIProperty_MultiSelection, YBoolProperty, true ) ); // read-only
206  propSet.add( YWidget::propertySet() );
207  }
208 
209  return propSet;
210 }
211 
212 
213 bool
214 YTable::setProperty( const string & propertyName, const YPropertyValue & val )
215 {
216  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
217 
218  if ( propertyName == YUIProperty_Value ) return false; // Needs special handling
219  else if ( propertyName == YUIProperty_CurrentItem ) return false; // Needs special handling
220  else if ( propertyName == YUIProperty_SelectedItems ) return false; // Needs special handling
221  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
222  else if ( propertyName == YUIProperty_Cell ) return false; // Needs special handling
223  else if ( propertyName == YUIProperty_Item ) return false; // Needs special handling
224  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
225  else
226  {
227  return YWidget::setProperty( propertyName, val );
228  }
229 
230  return true; // success -- no special processing necessary
231 }
232 
233 
235 YTable::getProperty( const string & propertyName )
236 {
237  propertySet().check( propertyName ); // throws exceptions if not found
238 
239  if ( propertyName == YUIProperty_Value ) return YPropertyValue( YOtherProperty );
240  else if ( propertyName == YUIProperty_CurrentItem ) return YPropertyValue( YOtherProperty );
241  else if ( propertyName == YUIProperty_SelectedItems ) return YPropertyValue( YOtherProperty );
242  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
243  else if ( propertyName == YUIProperty_Cell ) return YPropertyValue( YOtherProperty );
244  else if ( propertyName == YUIProperty_Item ) return YPropertyValue( YOtherProperty );
245  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
246  else
247  {
248  return YWidget::getProperty( propertyName );
249  }
250 }
int columns() const
Return the number of columns of this table.
Definition: YTable.cc:87
YAlignmentType alignment(int column) const
Return the alignment for the specified column.
Definition: YTable.cc:108
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
YTable(YWidget *parent, YTableHeader *header, bool multiSelection)
Constructor.
Definition: YTable.cc:52
YItemIterator itemsEnd()
Return an iterator that points behind the last item.
Transport class for the value of simple properties.
Definition: YProperty.h:104
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
bool hasMultiSelection() const
Return &#39;true&#39; if the user can select multiple items at the same time (e.g., with shift-click or ctrl-...
Definition: YTable.cc:146
Base class for various kinds of multi-value widgets.
Helper class for YTable for table column properties:
Definition: YTableHeader.h:43
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
A set of properties to check names and types against.
Definition: YProperty.h:197
void setImmediateMode(bool immediateMode=true)
Set immediateMode() on or off.
Definition: YTable.cc:122
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YTable.cc:235
bool hasColumn(int column) const
Return &#39;true&#39; if this table header has a column no.
Definition: YTableHeader.cc:79
int columns() const
Return the number of columns.
Definition: YTableHeader.cc:72
YItemIterator itemsBegin()
Return an iterator that points to the first item.
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YTable.cc:179
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:395
virtual ~YTable()
Destructor.
Definition: YTable.cc:66
std::string header(int column) const
Return the header text for the specified column.
Definition: YTable.cc:101
virtual void deleteAllItems()
Delete all items.
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YTable.cc:214
YItem * findItem(const std::string &wantedItemLabel, int column) const
Try to find an item with label &#39;wantedItemLabel&#39; in column &#39;column&#39; between iterators &#39;begin&#39; and &#39;en...
Definition: YTable.cc:152
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
bool immediateMode() const
Deliver even more events than with notify() set.
Definition: YTable.cc:115
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:566
Class for widget properties.
Definition: YProperty.h:51
std::string header(int column) const
Return the header text for the specified column.
Definition: YTableHeader.cc:86
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:49
std::string iconBasePath() const
Return this widget&#39;s base path where to look up icons as set with setIconBasePath().
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:42
void setTableHeader(YTableHeader *newHeader)
Exchange the previous table header with a new one.
Definition: YTable.cc:74
void setNotify(bool notify=true)
Sets the Notify property.
Definition: YWidget.cc:522
void setIconBasePath(const std::string &basePath)
Set this widget&#39;s base path where to look up icons.
YWidgetListIterator begin()
A helper for the range-based "for" loop.
Definition: YWidget.h:238
virtual void setKeepSorting(bool keepSorting)
Switch between sorting by item insertion order (keepSorting: true) or allowing the user to sort by an...
Definition: YTable.cc:139
YAlignmentType alignment(int column) const
Return the alignment for the specified column.
Definition: YTableHeader.cc:96
bool keepSorting() const
Return &#39;true&#39; if the sort order is to be kept in item insertion order, i.e.
Definition: YTable.cc:132
bool hasColumn(int column) const
Return &#39;true&#39; if this table has a column no.
Definition: YTable.cc:94
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:88
Item class for YTable items.
Definition: YTableItem.h:58
Abstract base class of all UI widgets.
Definition: YWidget.h:54
bool enforceSingleSelection() const
Return &#39;true&#39; if this base class should enforce single selection.
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169