libyui-qt  2.46.13
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  Qt::MouseButton _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  // Data members
296  //
297 
298  int _serial;
299 
300  QColor _textColor;
301  QColor _backgroundColor;
302 };
303 
304 
305 
306 /**
307  * Enhanced QCheckListItem
308  **/
310 {
311 public:
312 
313  /**
314  * Constructor for toplevel items.
315  **/
316  QY2CheckListItem( QY2ListView * parentListView,
317  const QString & text );
318 
319 
320  /**
321  * Constructor for deeper level items.
322  **/
323  QY2CheckListItem( QTreeWidgetItem * parentItem,
324  const QString & text );
325 
326  /**
327  * Destructor
328  **/
329  virtual ~QY2CheckListItem();
330 
331  /**
332  * Update this item's status.
333  * Triggered by QY2ListView::updateAllItemStates().
334  * Derived classes should overwrite this.
335  * This default implementation does nothing.
336  **/
337  virtual void updateStatus() {}
338 
339  /**
340  * Update this item's data completely.
341  * Triggered by QY2ListView::updateAllItemData().
342  * Derived classes should overwrite this.
343  * This default implementation does nothing.
344  **/
345  virtual void updateData() {}
346 
347  /**
348  * Return this item's serial number.
349  * Useful for comparison functions that order by insertion sequence.
350  **/
351  int serial() const { return _serial; }
352 
353  /**
354  * Set the text foreground color for all columns.
355  * For more specific purposes reimiplement paintCell().
356  **/
357  void setTextColor( const QColor & col )
358  { _textColor = col; }
359 
360  /**
361  * Set the text background color for all columns.
362  * For more specific purposes reimiplement paintCell().
363  **/
364  void setBackgroundColor( const QColor & col )
365  { _backgroundColor = col; }
366 
367  /**
368  * Returns a tool tip text for a specific column of this item.
369  * 'column' is -1 if the mouse pointer is in the tree indentation area.
370  *
371  * This default implementation does nothing.
372  **/
373  virtual QString toolTip( int column ) { return QString(); }
374 
375 
376 protected:
377 
378  //
379  // Data members
380  //
381 
382  int _serial;
383 };
384 
385 
386 #if FIXME_TOOLTIP
387 /**
388  * Tool tip for a QY2ListView widget: Enables individual tool tips specific to
389  * each list item and each column. Overwrite QY2ListViewItem::toolTip() to use
390  * this.
391  **/
392 class QY2ListViewToolTip : public QToolTip
393 {
394 public:
395 
396  /**
397  * Constructor.
398  **/
399  QY2ListViewToolTip( QY2ListView * parent )
400  : QToolTip( parent->viewport() )
401  , _listView( parent ) {}
402 
403  /**
404  * Destructor (to make gcc 4.x happy)
405  **/
406  virtual ~QY2ListViewToolTip() {}
407 
408 
409 protected:
410 
411  /**
412  * Decide if there is a tool tip text at 'p' and display it if there is one.
413  *
414  * Reimplemented from QToolTip.
415  **/
416  virtual void maybeTip( const QPoint & p );
417 
418 
419  //
420  // Data members
421  //
422 
423  QY2ListView * _listView;
424 };
425 #endif
426 
427 #endif // ifndef QY2ListView_h
bool sortByInsertionSequence() const
Returns 'true' if the sort order should always be the item insertion order, 'false' if the user can c...
Definition: QY2ListView.h:144
virtual void setSortByInsertionSequence(bool sortByInsertionSequence)
Enforce sorting by item insertion order (true) or let user change sorting by clicking on a column hea...
Definition: QY2ListView.cc:355
virtual bool operator<(const QTreeWidgetItem &other) const
Comparison function used for sorting the list.
Definition: QY2ListView.cc:396
QY2ListViewItem(QY2ListView *parentListView, const QString &text=QString::null)
Constructor for toplevel items.
Definition: QY2ListView.cc:368
void saveColumnWidths()
Save the current column widths.
Definition: QY2ListView.cc:170
void setTextColor(const QColor &col)
Set the text foreground color for all columns.
Definition: QY2ListView.h:357
void columnWidthChanged(int col, int oldSize, int newSize)
Internal: Handle manual column resize.
Definition: QY2ListView.cc:289
void setBackgroundColor(const QColor &col)
Set the text background color for all columns.
Definition: QY2ListView.h:364
QY2ListView(QWidget *parent)
Constructor.
Definition: QY2ListView.cc:37
virtual QSize minimumSizeHint() const
Returns the minimum size required for this widget.
Definition: QY2ListView.cc:348
virtual void clear()
Reimplemented from Q3ListView: Adjust header sizes after clearing contents.
Definition: QY2ListView.cc:102
void columnClicked(int button, QTreeWidgetItem *item, int col, const QPoint &pos)
Emitted for mouse clicks on an item.
virtual void updateData()
Update this item's data completely.
Definition: QY2ListView.h:345
virtual void mouseDoubleClickEvent(QMouseEvent *)
Handle mouse clicks.
Definition: QY2ListView.cc:267
virtual void updateStatus()
Update this item's status.
Definition: QY2ListView.h:337
virtual void updateData()
Update this item's data completely.
Definition: QY2ListView.h:269
virtual QString toolTip(QTreeWidgetItem *item, int column)
Returns a tool tip text for a specific column of a list item.
Definition: QY2ListView.cc:144
virtual bool eventFilter(QObject *obj, QEvent *event)
Event filter - inherited from QWidget.
Definition: QY2ListView.cc:317
void updateItemStates()
Update the status display of all list entries: Call QY2ListViewItem::updateStatus() for each item...
Definition: QY2ListView.cc:110
virtual ~QY2CheckListItem()
Destructor.
Definition: QY2ListView.cc:471
virtual QString toolTip(int column)
Returns a tool tip text for a specific column of this item.
Definition: QY2ListView.h:373
int serial() const
Return this item's serial number.
Definition: QY2ListView.h:281
void treeCollapsed(QTreeWidgetItem *listViewItem)
Internal notification that a tree item has been collapsed.
Definition: QY2ListView.cc:534
void columnDoubleClicked(int button, QTreeWidgetItem *item, int col, const QPoint &pos)
Emitted for mouse double clicks on an item.
Enhanced QCheckListItem.
Definition: QY2ListView.h:309
Enhanced QTreeWidget.
Definition: QY2ListView.h:47
virtual QString toolTip(int column)
Returns a tool tip text for a specific column of this item.
Definition: QY2ListView.h:289
QY2CheckListItem(QY2ListView *parentListView, const QString &text)
Constructor for toplevel items.
Definition: QY2ListView.cc:447
void treeExpanded(QTreeWidgetItem *listViewItem)
Internal notification that a tree item has been expanded.
Definition: QY2ListView.cc:527
Enhanced QTreeWidgetItem.
Definition: QY2ListView.h:233
virtual ~QY2ListViewItem()
Destructor.
Definition: QY2ListView.cc:389
virtual ~QY2ListView()
Destructor.
Definition: QY2ListView.cc:72
void updateItemData()
Update the status display of all list entries: Call QY2ListViewItem::updateData() for each item...
Definition: QY2ListView.cc:127
virtual void mouseReleaseEvent(QMouseEvent *)
Handle mouse clicks.
Definition: QY2ListView.cc:237
virtual void selectSomething()
Select a list entry (if there is any).
Definition: QY2ListView.cc:82
int nextSerial()
Returns the next free serial number for items that want to be ordered in insertion sequence...
Definition: QY2ListView.h:156
virtual void mousePressEvent(QMouseEvent *e)
Handle mouse clicks.
Definition: QY2ListView.cc:212
int serial() const
Return this item's serial number.
Definition: QY2ListView.h:351
void restoreColumnWidths()
Restore the column widths to what was saved previously with saveColumnWidths().
Definition: QY2ListView.cc:185
virtual void updateStatus()
Update this item's status.
Definition: QY2ListView.h:261