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