]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/QY2ListView.h
unhide some virtual overloads - fixing Huha's only complaint :)
[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 <q3listview.h>
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 Q3ListView
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                                   Q3ListViewItem *      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                                   Q3ListViewItem *      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( Q3ListViewItem * 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     Q3ListViewItem *            _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 Q3ListViewItem
214  **/
215 class QY2ListViewItem: public Q3ListViewItem
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( Q3ListViewItem *   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 Q3ListViewItem
261      **/
262     virtual int compare( Q3ListViewItem *       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 Q3ListViewItem so different
299      * colors can be used.
300      *
301      * Reimplemented from Q3ListViewItem.
302      **/
303     virtual void paintCell( QPainter *          painter,
304                             const QColorGroup & colorGroup,
305                             int                 column,
306                             int                 width,
307                             int                 alignment );
308
309     //
310     // Data members
311     //
312
313     int         _serial;
314
315     QColor      _textColor;
316     QColor      _backgroundColor;
317 };
318
319
320
321 /**
322  * Enhanced QCheckListItem
323  **/
324 class QY2CheckListItem: public Q3CheckListItem
325 {
326 public:
327
328     /**
329      * Constructor for toplevel items.
330      **/
331     QY2CheckListItem( QY2ListView *             parentListView,
332                       const QString &           text,
333                       Q3CheckListItem::Type     type );
334
335
336     /**
337      * Constructor for deeper level items.
338      **/
339     QY2CheckListItem( Q3ListViewItem *          parentItem,
340                       const QString &           text,
341                       Q3CheckListItem::Type     type );
342
343
344     /**
345      * Constructor for deeper level items for QCheckListItem parents.
346      **/
347     QY2CheckListItem( Q3CheckListItem *                 parentItem,
348                       const QString &           text,
349                       Q3CheckListItem::Type     type );
350
351     /**
352      * Destructor
353      **/
354     virtual ~QY2CheckListItem();
355
356     /**
357      * Update this item's status.
358      * Triggered by QY2ListView::updateAllItemStates().
359      * Derived classes should overwrite this.
360      * This default implementation does nothing.
361      **/
362     virtual void updateStatus() {}
363
364     /**
365      * Update this item's data completely.
366      * Triggered by QY2ListView::updateAllItemData().
367      * Derived classes should overwrite this.
368      * This default implementation does nothing.
369      **/
370     virtual void updateData() {}
371
372     /**
373      * Comparison function used for sorting the list.
374      * Returns:
375      * -1 if this <  other
376      *  0 if this == other
377      * +1 if this >  other
378      *
379      * Reimplemented from Q3ListViewItem
380      **/
381     virtual int compare( Q3ListViewItem *       other,
382                          int                    col,
383                          bool                   ascending ) const;
384
385     /**
386      * Return this item's serial number.
387      * Useful for comparison functions that order by insertion sequence.
388      **/
389     int serial() const { return _serial; }
390
391     /**
392      * Set the text foreground color for all columns.
393      * For more specific purposes reimiplement paintCell().
394      **/
395     void setTextColor( const QColor & col )
396         { _textColor = col; }
397
398     /**
399      * Set the text background color for all columns.
400      * For more specific purposes reimiplement paintCell().
401      **/
402     void setBackgroundColor( const QColor & col )
403         { _backgroundColor = col; }
404
405     /**
406      * Returns a tool tip text for a specific column of this item.
407      * 'column' is -1 if the mouse pointer is in the tree indentation area.
408      *
409      * This default implementation does nothing.
410      **/
411     virtual QString toolTip( int column ) { return QString(); }
412
413
414 protected:
415
416     /**
417      * Paint method. Reimplemented from @ref Q3ListViewItem so different
418      * colors can be used.
419      *
420      * Reimplemented from Q3ListViewItem.
421      **/
422     virtual void paintCell( QPainter *          painter,
423                             const QColorGroup & colorGroup,
424                             int                 column,
425                             int                 width,
426                             int                 alignment );
427
428     //
429     // Data members
430     //
431
432     int         _serial;
433
434     QColor      _textColor;
435     QColor      _backgroundColor;
436 };
437
438
439 #if FIXME_TOOLTIP
440 /**
441  * Tool tip for a QY2ListView widget: Enables individual tool tips specific to
442  * each list item and each column. Overwrite QY2ListViewItem::toolTip() to use
443  * this.
444  **/
445 class QY2ListViewToolTip : public QToolTip
446 {
447 public:
448
449     /**
450      * Constructor.
451      **/
452     QY2ListViewToolTip( QY2ListView * parent )
453         : QToolTip( parent->viewport() )
454         , _listView( parent )  {}
455
456     /**
457      * Destructor (to make gcc 4.x happy)
458      **/
459     virtual ~QY2ListViewToolTip() {}
460
461
462 protected:
463
464     /**
465      * Decide if there is a tool tip text at 'p' and display it if there is one.
466      *
467      * Reimplemented from QToolTip.
468      **/
469     virtual void maybeTip( const QPoint & p );
470
471
472     //
473     // Data members
474     //
475
476     QY2ListView * _listView;
477 };
478 #endif
479
480 #endif // ifndef QY2ListView_h