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