libyui  3.3.2
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 
35 {
36  YTreePrivate()
37  : immediateMode( false )
38  {}
39 
40  bool immediateMode;
41 };
42 
43 
44 YTree::YTree( YWidget * parent, const std::string & label, bool multiSelection, bool recursiveSelection )
45  : YSelectionWidget( parent, label,
46  ! multiSelection,
47  recursiveSelection )
48  , priv( new YTreePrivate() )
49 {
50  YUI_CHECK_NEW( priv );
51 
52  setDefaultStretchable( YD_HORIZ, true );
53  setDefaultStretchable( YD_VERT, true );
54 }
55 
56 
58 {
59  // NOP
60 }
61 
62 
63 bool
65 {
66  return priv->immediateMode;
67 }
68 
69 
70 void
72 {
73  priv->immediateMode = immediateMode;
74 
75  if ( immediateMode )
76  setNotify( true );
77 }
78 
79 
80 void
81 YTree::addItems( const YItemCollection & itemCollection )
82 {
83  YSelectionWidget::addItems( itemCollection );
84  rebuildTree();
85 }
86 
87 
88 const YPropertySet &
90 {
91  static YPropertySet propSet;
92 
93  if ( propSet.isEmpty() )
94  {
95  /*
96  * @property itemID Value The currently selected item
97  * @property itemID CurrentItem The currently selected item
98  * @property list<itemID> CurrentBranch List of IDs of current branch from root to current item
99  * @property itemList Items All items
100  * @property map<ItemID> OpenItems Map of IDs of all open items - can only be queried, not set
101  * @property std::string Label Caption above the tree
102  * @property std::string IconPath Base path for icons
103  * @property bool MultiSelection Flag: User can select multiple items (read-only)
104  */
105  propSet.add( YProperty( YUIProperty_Value, YOtherProperty ) );
106  propSet.add( YProperty( YUIProperty_CurrentItem, YOtherProperty ) );
107  propSet.add( YProperty( YUIProperty_CurrentBranch, YOtherProperty ) );
108  propSet.add( YProperty( YUIProperty_Items, YOtherProperty ) );
109  propSet.add( YProperty( YUIProperty_OpenItems, YOtherProperty ) );
110  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
111  propSet.add( YProperty( YUIProperty_IconPath, YStringProperty ) );
112  propSet.add( YProperty( YUIProperty_SelectedItems, YOtherProperty ) );
113  propSet.add( YProperty( YUIProperty_MultiSelection, YBoolProperty, true ) ); // read-only
114  propSet.add( YWidget::propertySet() );
115 
116  }
117 
118  return propSet;
119 }
120 
121 
122 bool
123 YTree::setProperty( const std::string & propertyName, const YPropertyValue & val )
124 {
125  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
126 
127  if ( propertyName == YUIProperty_Value ) return false; // Needs special handling
128  else if ( propertyName == YUIProperty_CurrentItem ) return false; // Needs special handling
129  else if ( propertyName == YUIProperty_CurrentBranch ) return false; // Needs special handling
130  else if ( propertyName == YUIProperty_Items ) return false; // Needs special handling
131  else if ( propertyName == YUIProperty_OpenItems ) return false; // Needs special handling
132  else if ( propertyName == YUIProperty_SelectedItems ) return false; // Needs special handling
133  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
134  else if ( propertyName == YUIProperty_IconPath ) setIconBasePath( val.stringVal() );
135 
136  else
137  {
138  return YWidget::setProperty( propertyName, val );
139  }
140 
141  return true; // success -- no special processing necessary
142 }
143 
144 
146 YTree::getProperty( const std::string & propertyName )
147 {
148  propertySet().check( propertyName ); // throws exceptions if not found
149 
150  if ( propertyName == YUIProperty_Value ) return YPropertyValue( YOtherProperty );
151  else if ( propertyName == YUIProperty_CurrentItem ) return YPropertyValue( YOtherProperty );
152  else if ( propertyName == YUIProperty_CurrentBranch ) return YPropertyValue( YOtherProperty );
153  else if ( propertyName == YUIProperty_Items ) return YPropertyValue( YOtherProperty );
154  else if ( propertyName == YUIProperty_OpenItems ) return YPropertyValue( YOtherProperty );
155  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
156  else if ( propertyName == YUIProperty_IconPath ) return YPropertyValue( iconBasePath() );
157  else if ( propertyName == YUIProperty_SelectedItems ) return YPropertyValue( YOtherProperty );
158  else
159  {
160  return YWidget::getProperty( propertyName );
161  }
162 }
163 
164 bool
166 {
168 }
std::string label() const
Return this widget&#39;s label (the caption above the item list).
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
bool immediateMode() const
Deliver even more events than with notify() set.
Definition: YTree.cc:64
virtual void setLabel(const std::string &newLabel)
Change this widget&#39;s label (the caption above the item list).
Transport class for the value of simple properties.
Definition: YProperty.h:104
std::vector< YItem * > YItemCollection
Collection of pointers to YItem.
Definition: YItem.h:38
void setImmediateMode(bool on=true)
Set immediateMode() on or off.
Definition: YTree.cc:71
Base class for various kinds of multi-value widgets.
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:145
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:428
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YTree.cc:146
A set of properties to check names and types against.
Definition: YProperty.h:197
YTree(YWidget *parent, const std::string &label, bool multiSelection, bool recursiveSelection)
Constructor.
Definition: YTree.cc:44
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:453
virtual void addItems(const YItemCollection &itemCollection)
Add multiple items.
Definition: YTree.cc:81
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:561
Class for widget properties.
Definition: YProperty.h:51
virtual ~YTree()
Destructor.
Definition: YTree.cc:57
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YTree.cc:123
virtual void rebuildTree()=0
Rebuild the displayed tree from the internally stored YTreeItems.
void setNotify(bool notify=true)
Sets the Notify property.
Definition: YWidget.cc:517
void setIconBasePath(const std::string &basePath)
Set this widget&#39;s base path where to look up icons.
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YTree.cc:89
bool hasMultiSelection() const
Return &#39;true&#39; if the user can select multiple items at the same time.
Definition: YTree.cc:165
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
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
std::string iconBasePath() const
Return this widget&#39;s base path where to look up icons as set with setIconBasePath().