libyui  3.12.1
YTree.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: YTree.cc
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include "YUISymbols.h"
30 #include "YSelectionWidget.h"
31 #include "YTree.h"
32 #include "YTreeItem.h"
33 
34 using std::string;
35 using std::vector;
36 
37 
39 {
40  YTreePrivate()
41  : immediateMode( false )
42  {}
43 
44  bool immediateMode;
45 };
46 
47 
49  const string & label,
50  bool multiSelection,
51  bool recursiveSelection )
52  : YSelectionWidget( parent, label,
53  ! multiSelection,
54  recursiveSelection )
55  , priv( new YTreePrivate() )
56 {
57  YUI_CHECK_NEW( priv );
58 
59  setDefaultStretchable( YD_HORIZ, true );
60  setDefaultStretchable( YD_VERT, true );
61 }
62 
63 
65 {
66  // NOP
67 }
68 
69 
70 bool
72 {
73  return priv->immediateMode;
74 }
75 
76 
77 void
78 YTree::setImmediateMode( bool immediateMode )
79 {
80  priv->immediateMode = immediateMode;
81 
82  if ( immediateMode )
83  setNotify( true );
84 }
85 
86 
87 void
88 YTree::addItems( const YItemCollection & itemCollection )
89 {
90  YSelectionWidget::addItems( itemCollection );
91  rebuildTree();
92 }
93 
94 
95 const YPropertySet &
97 {
98  static YPropertySet propSet;
99 
100  if ( propSet.isEmpty() )
101  {
102  /*
103  * @property itemID Value The currently selected item
104  * @property itemID CurrentItem The currently selected item
105  * @property list<itemID> CurrentBranch List of IDs of current branch from root to current item
106  * @property itemList Items All items
107  * @property string Label Caption above the tree
108  * @property string IconPath Base path for icons
109  * @property bool MultiSelection Flag: User can select multiple items (read-only)
110  *
111  * @property map<ItemID, string> OpenItems Map of IDs of all open items (read-only)
112  * to either "ID" or "Text":
113  * { "myFirstItemID": "ID",
114  * "myThirdItem": "Text" }
115  */
116  propSet.add( YProperty( YUIProperty_Value, YOtherProperty ) );
117  propSet.add( YProperty( YUIProperty_CurrentItem, YOtherProperty ) );
118  propSet.add( YProperty( YUIProperty_CurrentBranch, YOtherProperty ) );
119  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
120  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
121  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
122  propSet.add( YProperty( YUIProperty_SelectedItems, YOtherProperty ) );
123  propSet.add( YProperty( YUIProperty_MultiSelection, YBoolProperty, true ) ); // read-only
124  propSet.add( YProperty( YUIProperty_OpenItems, YOtherProperty, true ) ); // read-only
125  propSet.add( YWidget::propertySet() );
126  }
127 
128  return propSet;
129 }
130 
131 
132 bool
133 YTree::setProperty( const string & propertyName, const YPropertyValue & val )
134 {
135  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
136 
137  if ( propertyName == YUIProperty_Value ) return false; // Needs special handling
138  else if ( propertyName == YUIProperty_CurrentItem ) return false; // Needs special handling
139  else if ( propertyName == YUIProperty_CurrentBranch ) return false; // Needs special handling
140  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
141  else if ( propertyName == YUIProperty_SelectedItems ) return false; // Needs special handling
142  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
143  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
144 
145  else
146  {
147  return YWidget::setProperty( propertyName, val );
148  }
149 
150  return true; // success -- no special processing necessary
151 }
152 
153 
155 YTree::getProperty( const string & propertyName )
156 {
157  propertySet().check( propertyName ); // throws exceptions if not found
158 
159  if ( propertyName == YUIProperty_Value ) return YPropertyValue( YOtherProperty );
160  else if ( propertyName == YUIProperty_CurrentItem ) return YPropertyValue( YOtherProperty );
161  else if ( propertyName == YUIProperty_CurrentBranch ) return YPropertyValue( YOtherProperty );
162  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
163  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
164  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
165  else if ( propertyName == YUIProperty_SelectedItems ) return YPropertyValue( YOtherProperty );
166  else if ( propertyName == YUIProperty_MultiSelection ) return YPropertyValue( hasMultiSelection() );
167  else if ( propertyName == YUIProperty_OpenItems ) return YPropertyValue( YOtherProperty );
168  else
169  {
170  return YWidget::getProperty( propertyName );
171  }
172 }
173 
174 bool
176 {
178 }
179 
180 
181 YTreeItem *
182 YTree::findItem( const vector<string> & path ) const
183 {
184  return findItem( path.begin(), path.end(),
185  itemsBegin(), itemsEnd() );
186 }
187 
188 
189 YTreeItem *
190 YTree::findItem( vector<string>::const_iterator path_begin,
191  vector<string>::const_iterator path_end,
192  YItemConstIterator begin,
193  YItemConstIterator end ) const
194 {
195  for ( YItemConstIterator it = begin; it != end; ++it )
196  {
197  YTreeItem * item = dynamic_cast<YTreeItem *>( *it );
198 
199  if ( ! item )
200  return 0;
201 
202  if ( item->label() == *path_begin )
203  {
204  if ( std::next( path_begin ) == path_end )
205  {
206  return item;
207  }
208 
209  // Recursively search child items
210  YTreeItem * result = findItem( ++path_begin, path_end,
211  item->childrenBegin(), item->childrenEnd() );
212 
213  if ( result )
214  return result;
215  }
216  }
217 
218  return 0;
219 }
YTreeItem
Item class for tree items.
Definition: YTreeItem.h:36
YItem::label
std::string label() const
Return this item's label.
Definition: YItem.h:97
YPropertySet::add
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YSelectionWidget
Base class for various kinds of multi-value widgets.
Definition: YSelectionWidget.h:46
YTree::setImmediateMode
void setImmediateMode(bool on=true)
Set immediateMode() on or off.
Definition: YTree.cc:78
YWidget::end
YWidgetListIterator end()
A helper for the range-based "for" loop.
Definition: YWidget.h:245
YItemCollection
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:39
YTree::immediateMode
bool immediateMode() const
Deliver even more events than with notify() set.
Definition: YTree.cc:71
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:198
YTree::YTree
YTree(YWidget *parent, const std::string &label, bool multiSelection, bool recursiveSelection)
Constructor.
Definition: YTree.cc:48
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YTree::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YTree.cc:155
YSelectionWidget::itemsEnd
YItemIterator itemsEnd()
Return an iterator that points behind the last item.
Definition: YSelectionWidget.cc:303
YWidget::begin
YWidgetListIterator begin()
A helper for the range-based "for" loop.
Definition: YWidget.h:238
YPropertyValue::stringVal
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
YSelectionWidget::label
std::string label() const
Return this widget's label (the caption above the item list).
Definition: YSelectionWidget.cc:99
YTreeItem::childrenBegin
virtual YItemIterator childrenBegin()
Return an iterator that points to the first child item of this item.
Definition: YTreeItem.h:89
YSelectionWidget::enforceSingleSelection
bool enforceSingleSelection() const
Return 'true' if this base class should enforce single selection.
Definition: YSelectionWidget.cc:111
YPropertyValue::type
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YSelectionWidget::iconBasePath
std::string iconBasePath() const
Return this widget's base path where to look up icons as set with setIconBasePath().
Definition: YSelectionWidget.cc:156
YTreeItem::childrenEnd
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YTreeItem.h:97
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YWidget::setNotify
void setNotify(bool notify=true)
Sets the Notify property.
Definition: YWidget.cc:522
YTree::findItem
YTreeItem * findItem(const std::vector< std::string > &path) const
Return the item in the tree that matches path of labels or 0 if not found.
Definition: YTree.cc:182
YProperty
Class for widget properties.
Definition: YProperty.h:52
YSelectionWidget::setLabel
virtual void setLabel(const std::string &newLabel)
Change this widget's label (the caption above the item list).
Definition: YSelectionWidget.cc:105
YTreePrivate
Definition: YTree.cc:39
YSelectionWidget::itemsBegin
YItemIterator itemsBegin()
Return an iterator that points to the first item.
Definition: YSelectionWidget.cc:290
YSelectionWidget::setIconBasePath
void setIconBasePath(const std::string &basePath)
Set this widget's base path where to look up icons.
Definition: YSelectionWidget.cc:150
YTree::~YTree
virtual ~YTree()
Destructor.
Definition: YTree.cc:64
YTree::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YTree.cc:88
YPropertySet::check
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
YTree::hasMultiSelection
bool hasMultiSelection() const
Return 'true' if the user can select multiple items at the same time.
Definition: YTree.cc:175
YTree::rebuildTree
virtual void rebuildTree()=0
Rebuild the displayed tree from the internally stored YTreeItems.
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:105
YWidget::setDefaultStretchable
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:566
YSelectionWidget::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YSelectionWidget.cc:271
YTree::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YTree.cc:133
YTree::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YTree.cc:96
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YItemConstIterator
YItemCollection::const_iterator YItemConstIterator
Const iterator over YItemCollection.
Definition: YItem.h:45
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395