libyui-qt  2.52.4
YQTree.h
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: YQTree.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YQTree_h
26 #define YQTree_h
27 
28 #include <QFrame>
29 #include <QTreeWidget>
30 
31 #include <yui/YTree.h>
32 
33 
34 class YQWidgetCaption;
35 class YQTreeItem;
36 
37 
38 class YQTree : public QFrame, public YTree
39 {
40  Q_OBJECT
41 
42 public:
43 
44  /**
45  * Constructor.
46  **/
47  YQTree( YWidget * parent,
48  const std::string & label,
49  bool multiSelection,
50  bool recursiveSelection );
51 
52  /**
53  * Destructor.
54  **/
55  virtual ~YQTree();
56 
57  /**
58  * Change the label text.
59  *
60  * Reimplemented from YSelectionWidget.
61  **/
62  virtual void setLabel( const std::string & label );
63 
64  /**
65  * Rebuild the displayed tree from the internally stored YTreeItems.
66  *
67  * The application should call this (once) after all items have been added
68  * with addItem(). YTree::addItems() calls this automatically.
69  *
70  * Implemented from YTree.
71  **/
72  virtual void rebuildTree();
73 
74  /**
75  * Select or deselect an item.
76  *
77  * Reimplemented from YSelectionWidget.
78  **/
79  virtual void selectItem( YItem * item, bool selected = true );
80 
81  /**
82  * Deselect all items.
83  *
84  * Reimplemented from YSelectionWidget.
85  **/
86  virtual void deselectAllItems();
87 
88  /**
89  * Delete all items.
90  *
91  * Reimplemented from YSelectionWidget.
92  **/
93  virtual void deleteAllItems();
94 
95 
96  /**
97  * Set enabled/disbled state.
98  *
99  * Reimplemented from YWidget.
100  **/
101  virtual void setEnabled( bool enabled );
102 
103  /**
104  * Preferred width of the widget.
105  *
106  * Reimplemented from YWidget.
107  **/
108  virtual int preferredWidth();
109 
110  /**
111  * Preferred height of the widget.
112  *
113  * Reimplemented from YWidget.
114  **/
115  virtual int preferredHeight();
116 
117  /**
118  * Set the new size of the widget.
119  *
120  * Reimplemented from YWidget.
121  **/
122  virtual void setSize( int newWidth, int newHeight );
123 
124  /**
125  * Accept the keyboard focus.
126  **/
127  virtual bool setKeyboardFocus();
128 
129 
130  /**
131  * Return the the item that currently has the keyboard focus
132  * or 0 if no item currently has the keyboard focus.
133  *
134  * Reimplemented from YMultiSelectionBox.
135  **/
136  virtual YTreeItem * currentItem();
137 
138  /**
139  * Activate the item selected in the tree. Can be used in tests to simulate user input.
140  *
141  * Derived classes are required to implement this.
142  **/
143  virtual void activate();
144 
145 
146 protected slots:
147 
148 
149 
150  /**
151  * Propagate a tree item selection.
152  *
153  * This will trigger a 'SelectionChanged' event if 'notify' is set.
154  **/
155  void slotSelectionChanged();
156 
157  /**
158  * Propagate a tree item change.
159  *
160  * This will trigger a 'ValueChanged' event if 'notify' is set.
161  **/
162  void slotItemChanged( QTreeWidgetItem * item);
163  void slotItemClicked( QTreeWidgetItem * item, int column );
164 
165  /**
166  * Propagate a double click or pressing the space key on a tree item.
167  *
168  * This will trigger an 'Activated' event if 'notify' is set.
169  **/
170  void slotActivated( QTreeWidgetItem * item);
171 
172  /**
173  * Propagate an "item expanded" event to the underlying YTreeItem.
174  **/
175  void slotItemExpanded( QTreeWidgetItem * item );
176 
177  /**
178  * Propagate an "item collapsed" event to the underlying YTreeItem.
179  **/
180  void slotItemCollapsed( QTreeWidgetItem * item );
181 
182  /**
183  * Propagate a context menu selection
184  *
185  * This will trigger an 'ContextMenuActivated' event if 'notifyContextMenu' is set.
186  **/
187  void slotContextMenu ( const QPoint & pos );
188 
189 
190 protected:
191 
192  /**
193  * Select an item via the corresponding YQTreItem.
194  **/
195  void selectItem( YQTreeItem * item );
196 
197  /**
198  * Select or deselect an item, recursively
199  **/
200  void selectItem(QTreeWidgetItem * item, bool selected, bool recursive);
201 
202  /**
203  * Open the branch of 'item' recursively to its toplevel item.
204  **/
205  void openBranch( YQTreeItem * item );
206 
207  /**
208  * Build a tree of items that will be displayed (YQTreeItems) from the
209  * original items between iterators 'begin' and 'end' as child items of
210  * 'parentItem' (or as toplevel items if 'parentItem' is 0).
211  **/
212  void buildDisplayTree( YQTreeItem * parentItem,
213  YItemIterator begin,
214  YItemIterator end );
215 
216  //
217  // Data members
218  //
219 
220  YQWidgetCaption * _caption;
221  QTreeWidget * _qt_treeWidget;
222  int _nextSerialNo;
223 };
224 
225 
226 class YQTreeItem: public QTreeWidgetItem
227 {
228 public:
229 
230  /**
231  * Constructor for a top level item.
232  **/
233  YQTreeItem( YQTree * tree,
234  QTreeWidget * parent,
235  YTreeItem * origItem,
236  int serial );
237 
238  /**
239  * Constructor for a non-top level item.
240  **/
241  YQTreeItem( YQTree * tree,
242  YQTreeItem * parent,
243  YTreeItem * origItem,
244  int serial );
245 
246  /**
247  * Returns the original YTreeItem of which this item is a clone.
248  **/
249  YTreeItem * origItem() { return _origItem; }
250 
251  /**
252  * Open this item.
253  *
254  * Reimplemented from QTreeWidgetItem.
255  **/
256  virtual void setOpen( bool open );
257 
258 
259 private:
260 
261  /**
262  * Init function. All constructors end up here.
263  **/
264  void init( YQTree * tree,
265  YTreeItem * yTreeItem,
266  int serial );
267 
268 
269 protected:
270 
271  /**
272  * Sort key of this item.
273  *
274  * Reimplemented from QTreeWidgetItem.
275  **/
276  QString key( int column, bool ascending ) const;
277 
278 
279  //
280  // Data members
281  //
282 
283  YQTree * _tree;
284  YTreeItem * _origItem;
285  int _serialNo;
286 
287 };
288 
289 
290 #endif // ifndef YQTree_h
virtual void activate()
Activate the item selected in the tree.
Definition: YQTree.cc:438
void slotItemChanged(QTreeWidgetItem *item)
Propagate a tree item change.
Definition: YQTree.cc:298
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQTree.cc:369
virtual void setOpen(bool open)
Open this item.
Definition: YQTree.cc:513
virtual void rebuildTree()
Rebuild the displayed tree from the internally stored YTreeItems.
Definition: YQTree.cc:125
virtual YTreeItem * currentItem()
Return the the item that currently has the keyboard focus or 0 if no item currently has the keyboard ...
Definition: YQTree.cc:421
YQTree(YWidget *parent, const std::string &label, bool multiSelection, bool recursiveSelection)
Constructor.
Definition: YQTree.cc:50
Definition: YQTree.h:38
void openBranch(YQTreeItem *item)
Open the branch of &#39;item&#39; recursively to its toplevel item.
Definition: YQTree.cc:208
void slotItemExpanded(QTreeWidgetItem *item)
Propagate an "item expanded" event to the underlying YTreeItem.
Definition: YQTree.cc:218
virtual void setLabel(const std::string &label)
Change the label text.
Definition: YQTree.cc:118
void slotItemCollapsed(QTreeWidgetItem *item)
Propagate an "item collapsed" event to the underlying YTreeItem.
Definition: YQTree.cc:229
virtual void deselectAllItems()
Deselect all items.
Definition: YQTree.cc:240
virtual ~YQTree()
Destructor.
Definition: YQTree.cc:112
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQTree.cc:387
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
Definition: YQTree.cc:162
YQTreeItem(YQTree *tree, QTreeWidget *parent, YTreeItem *origItem, int serial)
Constructor for a top level item.
Definition: YQTree.cc:449
YTreeItem * origItem()
Returns the original YTreeItem of which this item is a clone.
Definition: YQTree.h:249
QString key(int column, bool ascending) const
Sort key of this item.
Definition: YQTree.cc:521
void slotSelectionChanged()
Propagate a tree item selection.
Definition: YQTree.cc:344
virtual void deleteAllItems()
Delete all items.
Definition: YQTree.cc:266
virtual void setEnabled(bool enabled)
Set enabled/disbled state.
Definition: YQTree.cc:393
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
void slotActivated(QTreeWidgetItem *item)
Propagate a double click or pressing the space key on a tree item.
Definition: YQTree.cc:360
void buildDisplayTree(YQTreeItem *parentItem, YItemIterator begin, YItemIterator end)
Build a tree of items that will be displayed (YQTreeItems) from the original items between iterators ...
Definition: YQTree.cc:135
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQTree.cc:376
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQTree.cc:401
void slotContextMenu(const QPoint &pos)
Propagate a context menu selection.
Definition: YQTree.cc:409