libyui-qt  2.52.4
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() );
242 
243 
244  /**
245  * Constructor for deeper level items.
246  **/
247  QY2ListViewItem( QTreeWidgetItem * parentItem,
248  const QString & text = QString() );
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  * Returns 'true' if the sort order should always be the item insertion
279  * order, 'false' if the user can change the sort order by clicking on a
280  * column header.
281  **/
282  bool sortByInsertionSequence() const;
283 
284  /**
285  * Return this item's serial number.
286  * Useful for comparison functions that order by insertion sequence.
287  **/
288  int serial() const { return _serial; }
289 
290  /**
291  * Compare two string locate-aware. Strings representing integers
292  * have special handling.
293  **/
294  bool compare(const QString& text1, const QString& text2) const;
295 
296  /**
297  * The text of the table cell or the sort-key if available.
298  **/
299  virtual QString smartSortKey(int column) const;
300 
301  /**
302  * Returns a tool tip text for a specific column of this item.
303  * 'column' is -1 if the mouse pointer is in the tree indentation area.
304  *
305  * This default implementation does nothing.
306  **/
307  virtual QString toolTip( int column ) { return QString(); }
308 
309 
310 protected:
311 
312  //
313  // Data members
314  //
315 
316  int _serial;
317 
318  QColor _textColor;
319  QColor _backgroundColor;
320 };
321 
322 
323 
324 /**
325  * Enhanced QCheckListItem
326  **/
328 {
329 public:
330 
331  /**
332  * Constructor for toplevel items.
333  **/
334  QY2CheckListItem( QY2ListView * parentListView,
335  const QString & text );
336 
337 
338  /**
339  * Constructor for deeper level items.
340  **/
341  QY2CheckListItem( QTreeWidgetItem * parentItem,
342  const QString & text );
343 
344  /**
345  * Destructor
346  **/
347  virtual ~QY2CheckListItem();
348 
349  /**
350  * Update this item's status.
351  * Triggered by QY2ListView::updateAllItemStates().
352  * Derived classes should overwrite this.
353  * This default implementation does nothing.
354  **/
355  virtual void updateStatus() {}
356 
357  /**
358  * Update this item's data completely.
359  * Triggered by QY2ListView::updateAllItemData().
360  * Derived classes should overwrite this.
361  * This default implementation does nothing.
362  **/
363  virtual void updateData() {}
364 
365  /**
366  * Return this item's serial number.
367  * Useful for comparison functions that order by insertion sequence.
368  **/
369  int serial() const { return _serial; }
370 
371  /**
372  * Set the text foreground color for all columns.
373  * For more specific purposes reimiplement paintCell().
374  **/
375  void setTextColor( const QColor & col )
376  { _textColor = col; }
377 
378  /**
379  * Set the text background color for all columns.
380  * For more specific purposes reimiplement paintCell().
381  **/
382  void setBackgroundColor( const QColor & col )
383  { _backgroundColor = col; }
384 
385  /**
386  * Returns a tool tip text for a specific column of this item.
387  * 'column' is -1 if the mouse pointer is in the tree indentation area.
388  *
389  * This default implementation does nothing.
390  **/
391  virtual QString toolTip( int column ) { return QString(); }
392 
393 
394 protected:
395 
396  //
397  // Data members
398  //
399 
400  int _serial;
401 };
402 
403 
404 #if FIXME_TOOLTIP
405 /**
406  * Tool tip for a QY2ListView widget: Enables individual tool tips specific to
407  * each list item and each column. Overwrite QY2ListViewItem::toolTip() to use
408  * this.
409  **/
410 class QY2ListViewToolTip : public QToolTip
411 {
412 public:
413 
414  /**
415  * Constructor.
416  **/
417  QY2ListViewToolTip( QY2ListView * parent )
418  : QToolTip( parent->viewport() )
419  , _listView( parent ) {}
420 
421  /**
422  * Destructor (to make gcc 4.x happy)
423  **/
424  virtual ~QY2ListViewToolTip() {}
425 
426 
427 protected:
428 
429  /**
430  * Decide if there is a tool tip text at 'p' and display it if there is one.
431  *
432  * Reimplemented from QToolTip.
433  **/
434  virtual void maybeTip( const QPoint & p );
435 
436 
437  //
438  // Data members
439  //
440 
441  QY2ListView * _listView;
442 };
443 #endif
444 
445 #endif // ifndef QY2ListView_h
int serial() const
Return this item&#39;s serial number.
Definition: QY2ListView.h:288
QY2ListViewItem(QY2ListView *parentListView, const QString &text=QString())
Constructor for toplevel items.
Definition: QY2ListView.cc:368
bool sortByInsertionSequence() const
Returns &#39;true&#39; if the sort order should always be the item insertion order, &#39;false&#39; 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
virtual QSize minimumSizeHint() const
Returns the minimum size required for this widget.
Definition: QY2ListView.cc:348
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:375
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:382
virtual QString smartSortKey(int column) const
The text of the table cell or the sort-key if available.
Definition: QY2ListView.cc:461
QY2ListView(QWidget *parent)
Constructor.
Definition: QY2ListView.cc:37
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.
bool compare(const QString &text1, const QString &text2) const
Compare two string locate-aware.
Definition: QY2ListView.cc:441
virtual void updateData()
Update this item&#39;s data completely.
Definition: QY2ListView.h:363
virtual void mouseDoubleClickEvent(QMouseEvent *)
Handle mouse clicks.
Definition: QY2ListView.cc:267
virtual void updateStatus()
Update this item&#39;s status.
Definition: QY2ListView.h:355
virtual void updateData()
Update this item&#39;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:491
virtual QString toolTip(int column)
Returns a tool tip text for a specific column of this item.
Definition: QY2ListView.h:391
bool sortByInsertionSequence() const
Returns &#39;true&#39; if the sort order should always be the item insertion order, &#39;false&#39; if the user can c...
Definition: QY2ListView.cc:429
void treeCollapsed(QTreeWidgetItem *listViewItem)
Internal notification that a tree item has been collapsed.
Definition: QY2ListView.cc:554
void columnDoubleClicked(int button, QTreeWidgetItem *item, int col, const QPoint &pos)
Emitted for mouse double clicks on an item.
Enhanced QCheckListItem.
Definition: QY2ListView.h:327
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:307
QY2CheckListItem(QY2ListView *parentListView, const QString &text)
Constructor for toplevel items.
Definition: QY2ListView.cc:467
void treeExpanded(QTreeWidgetItem *listViewItem)
Internal notification that a tree item has been expanded.
Definition: QY2ListView.cc:547
int serial() const
Return this item&#39;s serial number.
Definition: QY2ListView.h:369
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
void restoreColumnWidths()
Restore the column widths to what was saved previously with saveColumnWidths().
Definition: QY2ListView.cc:185
virtual void updateStatus()
Update this item&#39;s status.
Definition: QY2ListView.h:261