libyui  3.10.0
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 <sh@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 
36 
38 {
39  YTreePrivate()
40  : immediateMode( false )
41  {}
42 
43  bool immediateMode;
44 };
45 
46 
47 YTree::YTree( YWidget * parent, const string & label, bool multiSelection, bool recursiveSelection )
48  : YSelectionWidget( parent, label,
49  ! multiSelection,
50  recursiveSelection )
51  , priv( new YTreePrivate() )
52 {
53  YUI_CHECK_NEW( priv );
54 
55  setDefaultStretchable( YD_HORIZ, true );
56  setDefaultStretchable( YD_VERT, true );
57 }
58 
59 
61 {
62  // NOP
63 }
64 
65 
66 bool
68 {
69  return priv->immediateMode;
70 }
71 
72 
73 void
74 YTree::setImmediateMode( bool immediateMode )
75 {
76  priv->immediateMode = immediateMode;
77 
78  if ( immediateMode )
79  setNotify( true );
80 }
81 
82 
83 void
84 YTree::addItems( const YItemCollection & itemCollection )
85 {
86  YSelectionWidget::addItems( itemCollection );
87  rebuildTree();
88 }
89 
90 
91 const YPropertySet &
93 {
94  static YPropertySet propSet;
95 
96  if ( propSet.isEmpty() )
97  {
98  /*
99  * @property itemID Value The currently selected item
100  * @property itemID CurrentItem The currently selected item
101  * @property list<itemID> CurrentBranch List of IDs of current branch from root to current item
102  * @property itemList Items All items
103  * @property map<ItemID> OpenItems Map of IDs of all open items - can only be queried, not set
104  * @property string Label Caption above the tree
105  * @property string IconPath Base path for icons
106  * @property bool MultiSelection Flag: User can select multiple items (read-only)
107  */
108  propSet.add( YProperty( YUIProperty_Value, YOtherProperty ) );
109  propSet.add( YProperty( YUIProperty_CurrentItem, YOtherProperty ) );
110  propSet.add( YProperty( YUIProperty_CurrentBranch, YOtherProperty ) );
111  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
112  propSet.add( YProperty( YUIProperty_OpenItems, YOtherProperty ) );
113  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
114  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
115  propSet.add( YProperty( YUIProperty_SelectedItems, YOtherProperty ) );
116  propSet.add( YProperty( YUIProperty_MultiSelection, YBoolProperty, true ) ); // read-only
117  propSet.add( YWidget::propertySet() );
118 
119  }
120 
121  return propSet;
122 }
123 
124 
125 bool
126 YTree::setProperty( const string & propertyName, const YPropertyValue & val )
127 {
128  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
129 
130  if ( propertyName == YUIProperty_Value ) return false; // Needs special handling
131  else if ( propertyName == YUIProperty_CurrentItem ) return false; // Needs special handling
132  else if ( propertyName == YUIProperty_CurrentBranch ) return false; // Needs special handling
133  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
134  else if ( propertyName == YUIProperty_OpenItems ) return false; // Needs special handling
135  else if ( propertyName == YUIProperty_SelectedItems ) return false; // Needs special handling
136  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
137  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
138 
139  else
140  {
141  return YWidget::setProperty( propertyName, val );
142  }
143 
144  return true; // success -- no special processing necessary
145 }
146 
147 
149 YTree::getProperty( const string & propertyName )
150 {
151  propertySet().check( propertyName ); // throws exceptions if not found
152 
153  if ( propertyName == YUIProperty_Value ) return YPropertyValue( YOtherProperty );
154  else if ( propertyName == YUIProperty_CurrentItem ) return YPropertyValue( YOtherProperty );
155  else if ( propertyName == YUIProperty_CurrentBranch ) return YPropertyValue( YOtherProperty );
156  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
157  else if ( propertyName == YUIProperty_OpenItems ) return YPropertyValue( YOtherProperty );
158  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
159  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
160  else if ( propertyName == YUIProperty_SelectedItems ) return YPropertyValue( YOtherProperty );
161  else
162  {
163  return YWidget::getProperty( propertyName );
164  }
165 }
166 
167 bool
169 {
171 }
172 
173 
174 YTreeItem *
175 YTree::findItem( std::vector<std::string> & path ) const
176 {
177  return findItem( path.begin(), path.end(), itemsBegin(), itemsEnd());
178 }
179 
180 
181 YTreeItem *
182 YTree::findItem( std::vector<std::string>::iterator path_begin,
183  std::vector<std::string>::iterator path_end,
184  YItemConstIterator begin,
185  YItemConstIterator end ) const
186 {
187  for ( YItemConstIterator it = begin; it != end; ++it )
188  {
189  YTreeItem * item = dynamic_cast<YTreeItem *>(*it);
190  // Test that dynamic_cast didn't fail
191  if (!item)
192  return nullptr;
193 
194  if( item->label() == *path_begin )
195  {
196  if ( std::next(path_begin) == path_end )
197  {
198  return item;
199  }
200  // Look in child nodes and return if found one
201  YTreeItem * result = findItem( ++path_begin, path_end, item->childrenBegin(), item->childrenEnd() );
202  if ( result )
203  return result;
204  }
205  }
206 
207  return nullptr;
208 }
YTreeItem
Item class for tree items.
Definition: YTreeItem.h:36
YItem::label
std::string label() const
Return this item's label.
Definition: YItem.h:82
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:43
YTree::setImmediateMode
void setImmediateMode(bool on=true)
Set immediateMode() on or off.
Definition: YTree.cc:74
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:38
YTree::immediateMode
bool immediateMode() const
Deliver even more events than with notify() set.
Definition: YTree.cc:67
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:47
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:149
YSelectionWidget::itemsEnd
YItemIterator itemsEnd()
Return an iterator that points behind the last item.
Definition: YSelectionWidget.cc:296
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:83
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:149
YTreeItem::childrenEnd
virtual YItemIterator childrenEnd()
Return an iterator that points after the last child item of this item.
Definition: YTreeItem.h:91
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YTree::findItem
YTreeItem * findItem(std::vector< std::string >::iterator path_begin, std::vector< std::string >::iterator path_end, YItemConstIterator begin, YItemConstIterator end) const
Recursively looks for the first item in the tree of the menu items using depth first search.
Definition: YTree.cc:182
YWidget::setNotify
void setNotify(bool notify=true)
Sets the Notify property.
Definition: YWidget.cc:522
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:38
YSelectionWidget::itemsBegin
YItemIterator itemsBegin()
Return an iterator that points to the first item.
Definition: YSelectionWidget.cc:283
YSelectionWidget::setIconBasePath
void setIconBasePath(const std::string &basePath)
Set this widget's base path where to look up icons.
Definition: YSelectionWidget.cc:143
YTree::~YTree
virtual ~YTree()
Destructor.
Definition: YTree.cc:60
YTree::addItems
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YTree.cc:84
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:168
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:264
YTree::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YTree.cc:126
YTree::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YTree.cc:92
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:42
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395