libyui-qt  2.43.5
 All Classes Functions Variables
QY2ListView.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: QY2ListView.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23  This is a pure Qt widget - it can be used independently of YaST2.
24 
25 /-*/
26 
27 
28 #ifndef QY2ListView_h
29 #define QY2ListView_h
30 
31 #include <QTreeWidget>
32 #include <qtooltip.h>
33 #include <qpoint.h>
34 #include <qcolor.h>
35 #include <vector>
36 
37 #define FIXME_TOOLTIP 0
38 
39 
40 class QY2ListViewItem;
41 class QY2ListViewToolTip;
42 
43 
44 /**
45  * @short Enhanced QTreeWidget
46  **/
47 class QY2ListView : public QTreeWidget
48 {
49  Q_OBJECT
50 
51 public:
52 
53  /**
54  * Constructor
55  **/
56  QY2ListView( QWidget * parent );
57 
58  /**
59  * Destructor
60  **/
61  virtual ~QY2ListView();
62 
63 
64 public slots:
65 
66  /**
67  * Select a list entry (if there is any).
68  * Usually this will be the first list entry, but don't rely on that - this
69  * might change without notice. Emits signal selectionChanged().
70  **/
71  virtual void selectSomething();
72 
73  /**
74  * Reimplemented from Q3ListView:
75  * Adjust header sizes after clearing contents.
76  **/
77  virtual void clear();
78 
79  /**
80  * Update the status display of all list entries:
81  * Call QY2ListViewItem::updateStatus() for each item.
82  * This is an expensive operation.
83  **/
84  void updateItemStates();
85 
86  /**
87  * Update the status display of all list entries:
88  * Call QY2ListViewItem::updateData() for each item.
89  * This is an expensive operation.
90  **/
91  void updateItemData();
92 
93  /**
94  * Save the current column widths.
95  **/
96  void saveColumnWidths();
97 
98  /**
99  * Restore the column widths to what was saved previously with
100  * saveColumnWidths().
101  **/
102  void restoreColumnWidths();
103 
104 
105 signals:
106 
107  /**
108  * Emitted for mouse clicks on an item
109  **/
110  void columnClicked ( int button,
111  QTreeWidgetItem * item,
112  int col,
113  const QPoint & pos );
114 
115  /**
116  * Emitted for mouse double clicks on an item
117  **/
118  void columnDoubleClicked ( int button,
119  QTreeWidgetItem * item,
120  int col,
121  const QPoint & pos );
122 
123 
124 public:
125 
126  /**
127  * Returns a tool tip text for a specific column of a list item.
128  * 'column' is -1 if the mouse pointer is in the tree indentation area.
129  *
130  * This default implementation tries to call
131  * QY2ListViewItem::toolTip( column ) or
132  * QY2CheckListItem::toolTip( column ), respectively
133  * if 'item' is a subclass of either.
134  *
135  * Derived classes may handle this differently.
136  **/
137  virtual QString toolTip( QTreeWidgetItem * item, int column );
138 
139  /**
140  * Returns 'true' if the sort order should always be the item insertion
141  * order, 'false' if the user can change the sort order by clicking on a
142  * column header.
143  **/
144  bool sortByInsertionSequence() const { return _sortByInsertionSequence; }
145 
146  /**
147  * Enforce sorting by item insertion order (true) or let user change
148  * sorting by clicking on a column header (false).
149  **/
151 
152  /**
153  * Returns the next free serial number for items that want to be ordered in
154  * insertion sequence.
155  **/
156  int nextSerial() { return _nextSerial++; }
157 
158  /**
159  * Returns the minimum size required for this widget.
160  * Inherited from QWidget.
161  **/
162  virtual QSize minimumSizeHint() const;
163 
164  /**
165  * Event filter - inherited from QWidget
166  **/
167  virtual bool eventFilter( QObject * obj, QEvent * event );
168 
169 
170 protected slots:
171 
172  /**
173  * Internal: Handle manual column resize.
174  * Save the user's preferred sizes so they don't get overwritten each time
175  * the list is cleared and filled with new contents.
176  **/
177  void columnWidthChanged( int col, int oldSize, int newSize );
178 
179  /**
180  * Internal notification that a tree item has been expanded
181  */
182  void treeExpanded( QTreeWidgetItem * listViewItem );
183 
184  /**
185  * Internal notification that a tree item has been collapsed
186  */
187  void treeCollapsed( QTreeWidgetItem * listViewItem );
188 
189 
190 protected:
191 
192  /**
193  * Handle mouse clicks.
194  * Reimplemented from QScrollView.
195  **/
196  virtual void mousePressEvent( QMouseEvent * e );
197 
198  /**
199  * Handle mouse clicks.
200  * Reimplemented from QScrollView.
201  **/
202  virtual void mouseReleaseEvent( QMouseEvent * );
203 
204  /**
205  * Handle mouse clicks.
206  * Reimplemented from QScrollView.
207  **/
208  virtual void mouseDoubleClickEvent( QMouseEvent * );
209 
210 
211  //
212  // Data members
213  //
214 
215  QTreeWidgetItem * _mousePressedItem;
216  int _mousePressedCol;
217  int _mousePressedButton;
218 
219  std::vector<int> _savedColumnWidth;
220  bool _sortByInsertionSequence;
221  int _nextSerial;
222 
223  QY2ListViewToolTip * _toolTip;
224  bool _mouseButton1PressedInHeader;
225  bool _finalSizeChangeExpected;
226 };
227 
228 
229 
230 /**
231  * Enhanced QTreeWidgetItem
232  **/
233 class QY2ListViewItem: public QTreeWidgetItem
234 {
235 public:
236 
237  /**
238  * Constructor for toplevel items.
239  **/
240  QY2ListViewItem( QY2ListView * parentListView,
241  const QString & text = QString::null );
242 
243 
244  /**
245  * Constructor for deeper level items.
246  **/
247  QY2ListViewItem( QTreeWidgetItem * parentItem,
248  const QString & text = QString::null );
249 
250  /**
251  * Destructor
252  **/
253  virtual ~QY2ListViewItem();
254 
255  /**
256  * Update this item's status.
257  * Triggered by QY2ListView::updateAllItemStates().
258  * Derived classes should overwrite this.
259  * This default implementation does nothing.
260  **/
261  virtual void updateStatus() {}
262 
263  /**
264  * Update this item's data completely.
265  * Triggered by QY2ListView::updateAllItemData().
266  * Derived classes should overwrite this.
267  * This default implementation does nothing.
268  **/
269  virtual void updateData() {}
270 
271  /**
272  * Comparison function used for sorting the list.
273  * Reimplemented from QTreeWidgetItem
274  **/
275  virtual bool operator< ( const QTreeWidgetItem & other ) const;
276 
277  /**
278  * Return this item's serial number.
279  * Useful for comparison functions that order by insertion sequence.
280  **/
281  int serial() const { return _serial; }
282 
283  /**
284  * Returns a tool tip text for a specific column of this item.
285  * 'column' is -1 if the mouse pointer is in the tree indentation area.
286  *
287  * This default implementation does nothing.
288  **/
289  virtual QString toolTip( int column ) { return QString::null; }
290 
291 
292 protected:
293 
294  /**
295  * Paint method. Reimplemented from @ref QTreeWidgetItem so different
296  * colors can be used.
297  *
298  * Reimplemented from QTreeWidgetItem.
299  **/
300  /*
301  virtual void paintCell( QPainter * painter,
302  const QColorGroup & colorGroup,
303  int column,
304  int width,
305  int alignment );
306  */
307  //
308  // Data members
309  //
310 
311  int _serial;
312 
313  QColor _textColor;
314  QColor _backgroundColor;
315 };
316 
317 
318 
319 /**
320  * Enhanced QCheckListItem
321  **/
323 {
324 public:
325 
326  /**
327  * Constructor for toplevel items.
328  **/
329  QY2CheckListItem( QY2ListView * parentListView,
330  const QString & text );
331 
332 
333  /**
334  * Constructor for deeper level items.
335  **/
336  QY2CheckListItem( QTreeWidgetItem * parentItem,
337  const QString & text );
338 
339  /**
340  * Destructor
341  **/
342  virtual ~QY2CheckListItem();
343 
344  /**
345  * Update this item's status.
346  * Triggered by QY2ListView::updateAllItemStates().
347  * Derived classes should overwrite this.
348  * This default implementation does nothing.
349  **/
350  virtual void updateStatus() {}
351 
352  /**
353  * Update this item's data completely.
354  * Triggered by QY2ListView::updateAllItemData().
355  * Derived classes should overwrite this.
356  * This default implementation does nothing.
357  **/
358  virtual void updateData() {}
359 
360  /**
361  * Return this item's serial number.
362  * Useful for comparison functions that order by insertion sequence.
363  **/
364  int serial() const { return _serial; }
365 
366  /**
367  * Set the text foreground color for all columns.
368  * For more specific purposes reimiplement paintCell().
369  **/
370  void setTextColor( const QColor & col )
371  { _textColor = col; }
372 
373  /**
374  * Set the text background color for all columns.
375  * For more specific purposes reimiplement paintCell().
376  **/
377  void setBackgroundColor( const QColor & col )
378  { _backgroundColor = col; }
379 
380  /**
381  * Returns a tool tip text for a specific column of this item.
382  * 'column' is -1 if the mouse pointer is in the tree indentation area.
383  *
384  * This default implementation does nothing.
385  **/
386  virtual QString toolTip( int column ) { return QString(); }
387 
388 
389 protected:
390 
391  //
392  // Data members
393  //
394 
395  int _serial;
396 };
397 
398 
399 #if FIXME_TOOLTIP
400 /**
401  * Tool tip for a QY2ListView widget: Enables individual tool tips specific to
402  * each list item and each column. Overwrite QY2ListViewItem::toolTip() to use
403  * this.
404  **/
405 class QY2ListViewToolTip : public QToolTip
406 {
407 public:
408 
409  /**
410  * Constructor.
411  **/
412  QY2ListViewToolTip( QY2ListView * parent )
413  : QToolTip( parent->viewport() )
414  , _listView( parent ) {}
415 
416  /**
417  * Destructor (to make gcc 4.x happy)
418  **/
419  virtual ~QY2ListViewToolTip() {}
420 
421 
422 protected:
423 
424  /**
425  * Decide if there is a tool tip text at 'p' and display it if there is one.
426  *
427  * Reimplemented from QToolTip.
428  **/
429  virtual void maybeTip( const QPoint & p );
430 
431 
432  //
433  // Data members
434  //
435 
436  QY2ListView * _listView;
437 };
438 #endif
439 
440 #endif // ifndef QY2ListView_h