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