libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/QY2ListView.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       QY2ListView.h
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023   This is a pure Qt widget - it can be used independently of YaST2.
00024 
00025 /-*/
00026 
00027 
00028 #ifndef QY2ListView_h
00029 #define QY2ListView_h
00030 
00031 #include <QTreeWidget>
00032 #include <qtooltip.h>
00033 #include <qpoint.h>
00034 #include <qcolor.h>
00035 #include <vector>
00036 
00037 #define FIXME_TOOLTIP 0
00038 
00039 
00040 class QY2ListViewItem;
00041 class QY2ListViewToolTip;
00042 
00043 
00044 /**
00045  * @short Enhanced QTreeWidget
00046  **/
00047 class QY2ListView : public QTreeWidget
00048 {
00049     Q_OBJECT
00050 
00051 public:
00052 
00053     /**
00054      * Constructor
00055      **/
00056     QY2ListView( QWidget * parent );
00057 
00058     /**
00059      * Destructor
00060      **/
00061     virtual ~QY2ListView();
00062 
00063 
00064 public slots:
00065 
00066     /**
00067      * Select a list entry (if there is any).
00068      * Usually this will be the first list entry, but don't rely on that - this
00069      * might change without notice. Emits signal selectionChanged().
00070      **/
00071     virtual void selectSomething();
00072 
00073     /**
00074      * Reimplemented from Q3ListView:
00075      * Adjust header sizes after clearing contents.
00076      **/
00077     virtual void clear();
00078 
00079     /**
00080      * Update the status display of all list entries:
00081      * Call QY2ListViewItem::updateStatus() for each item.
00082      * This is an expensive operation.
00083      **/
00084     void updateItemStates();
00085 
00086     /**
00087      * Update the status display of all list entries:
00088      * Call QY2ListViewItem::updateData() for each item.
00089      * This is an expensive operation.
00090      **/
00091     void updateItemData();
00092 
00093     /**
00094      * Save the current column widths.
00095      **/
00096     void saveColumnWidths();
00097 
00098     /**
00099      * Restore the column widths to what was saved previously with
00100      * saveColumnWidths().
00101      **/
00102     void restoreColumnWidths();
00103 
00104 
00105 signals:
00106 
00107     /**
00108      * Emitted for mouse clicks on an item
00109      **/
00110     void columnClicked          ( int                   button,
00111                                   QTreeWidgetItem *     item,
00112                                   int                   col,
00113                                   const QPoint &        pos );
00114 
00115     /**
00116      * Emitted for mouse double clicks on an item
00117      **/
00118     void columnDoubleClicked    ( int                   button,
00119                                   QTreeWidgetItem *     item,
00120                                   int                   col,
00121                                   const QPoint &        pos );
00122 
00123 
00124 public:
00125 
00126     /**
00127      * Returns a tool tip text for a specific column of a list item.
00128      * 'column' is -1 if the mouse pointer is in the tree indentation area.
00129      *
00130      * This default implementation tries to call
00131      * QY2ListViewItem::toolTip( column ) or
00132      * QY2CheckListItem::toolTip( column ), respectively
00133      * if 'item' is a subclass of either.
00134      *
00135      * Derived classes may handle this differently.
00136      **/
00137     virtual QString toolTip( QTreeWidgetItem * item, int column );
00138 
00139     /**
00140      * Returns 'true' if the sort order should always be the item insertion
00141      * order, 'false' if the user can change the sort order by clicking on a
00142      * column header.
00143      **/
00144     bool sortByInsertionSequence() const { return _sortByInsertionSequence; }
00145 
00146     /**
00147      * Enforce sorting by item insertion order (true) or let user change
00148      * sorting by clicking on a column header (false).
00149      **/
00150     virtual void setSortByInsertionSequence( bool sortByInsertionSequence );
00151 
00152     /**
00153      * Returns the next free serial number for items that want to be ordered in
00154      * insertion sequence.
00155      **/
00156     int nextSerial() { return _nextSerial++; }
00157 
00158     /**
00159      * Returns the minimum size required for this widget.
00160      * Inherited from QWidget.
00161      **/
00162     virtual QSize minimumSizeHint() const;
00163 
00164     /**
00165      * Event filter - inherited from QWidget
00166      **/
00167     virtual bool eventFilter( QObject * obj, QEvent * event );
00168 
00169 
00170 protected slots:
00171 
00172     /**
00173      * Internal: Handle manual column resize.
00174      * Save the user's preferred sizes so they don't get overwritten each time
00175      * the list is cleared and filled with new contents.
00176      **/
00177     void columnWidthChanged( int col, int oldSize, int newSize );
00178 
00179     /**
00180      * Internal notification that a tree item has been expanded
00181      */
00182     void treeExpanded( QTreeWidgetItem * listViewItem );
00183 
00184     /**
00185      * Internal notification that a tree item has been collapsed
00186      */
00187     void treeCollapsed( QTreeWidgetItem * listViewItem );
00188 
00189 
00190 protected:
00191 
00192     /**
00193      * Handle mouse clicks.
00194      * Reimplemented from QScrollView.
00195      **/
00196     virtual void mousePressEvent( QMouseEvent * e );
00197 
00198     /**
00199      * Handle mouse clicks.
00200      * Reimplemented from QScrollView.
00201      **/
00202     virtual void mouseReleaseEvent( QMouseEvent * );
00203 
00204     /**
00205      * Handle mouse clicks.
00206      * Reimplemented from QScrollView.
00207      **/
00208     virtual void mouseDoubleClickEvent( QMouseEvent * );
00209 
00210 
00211     //
00212     // Data members
00213     //
00214 
00215     QTreeWidgetItem *           _mousePressedItem;
00216     int                         _mousePressedCol;
00217     int                         _mousePressedButton;
00218 
00219     std::vector<int>            _savedColumnWidth;
00220     bool                        _sortByInsertionSequence;
00221     int                         _nextSerial;
00222 
00223     QY2ListViewToolTip *        _toolTip;
00224     bool                        _mouseButton1PressedInHeader;
00225     bool                        _finalSizeChangeExpected;
00226 };
00227 
00228 
00229 
00230 /**
00231  * Enhanced QTreeWidgetItem
00232  **/
00233 class QY2ListViewItem: public QTreeWidgetItem
00234 {
00235 public:
00236 
00237     /**
00238      * Constructor for toplevel items.
00239      **/
00240     QY2ListViewItem( QY2ListView *      parentListView,
00241                      const QString &    text = QString::null );
00242 
00243 
00244     /**
00245      * Constructor for deeper level items.
00246      **/
00247     QY2ListViewItem( QTreeWidgetItem *  parentItem,
00248                      const QString &    text = QString::null );
00249 
00250     /**
00251      * Destructor
00252      **/
00253     virtual ~QY2ListViewItem();
00254 
00255     /**
00256      * Update this item's status.
00257      * Triggered by QY2ListView::updateAllItemStates().
00258      * Derived classes should overwrite this.
00259      * This default implementation does nothing.
00260      **/
00261     virtual void updateStatus() {}
00262 
00263     /**
00264      * Update this item's data completely.
00265      * Triggered by QY2ListView::updateAllItemData().
00266      * Derived classes should overwrite this.
00267      * This default implementation does nothing.
00268      **/
00269     virtual void updateData() {}
00270 
00271     /**
00272      * Comparison function used for sorting the list.
00273      * Reimplemented from QTreeWidgetItem
00274      **/
00275     virtual bool operator< ( const QTreeWidgetItem & other ) const;
00276 
00277     /**
00278      * Return this item's serial number.
00279      * Useful for comparison functions that order by insertion sequence.
00280      **/
00281     int serial() const { return _serial; }
00282 
00283     /**
00284      * Returns a tool tip text for a specific column of this item.
00285      * 'column' is -1 if the mouse pointer is in the tree indentation area.
00286      *
00287      * This default implementation does nothing.
00288      **/
00289     virtual QString toolTip( int column ) { return QString::null; }
00290 
00291 
00292 protected:
00293 
00294     /**
00295      * Paint method. Reimplemented from @ref QTreeWidgetItem so different
00296      * colors can be used.
00297      *
00298      * Reimplemented from QTreeWidgetItem.
00299      **/
00300     /*
00301     virtual void paintCell( QPainter *          painter,
00302                             const QColorGroup & colorGroup,
00303                             int                 column,
00304                             int                 width,
00305                             int                 alignment );
00306     */
00307     //
00308     // Data members
00309     //
00310 
00311     int         _serial;
00312 
00313     QColor      _textColor;
00314     QColor      _backgroundColor;
00315 };
00316 
00317 
00318 
00319 /**
00320  * Enhanced QCheckListItem
00321  **/
00322 class QY2CheckListItem: public QY2ListViewItem
00323 {
00324 public:
00325 
00326     /**
00327      * Constructor for toplevel items.
00328      **/
00329     QY2CheckListItem( QY2ListView *             parentListView,
00330                       const QString &           text );
00331 
00332 
00333     /**
00334      * Constructor for deeper level items.
00335      **/
00336     QY2CheckListItem( QTreeWidgetItem *                 parentItem,
00337                       const QString &           text );
00338 
00339     /**
00340      * Destructor
00341      **/
00342     virtual ~QY2CheckListItem();
00343 
00344     /**
00345      * Update this item's status.
00346      * Triggered by QY2ListView::updateAllItemStates().
00347      * Derived classes should overwrite this.
00348      * This default implementation does nothing.
00349      **/
00350     virtual void updateStatus() {}
00351 
00352     /**
00353      * Update this item's data completely.
00354      * Triggered by QY2ListView::updateAllItemData().
00355      * Derived classes should overwrite this.
00356      * This default implementation does nothing.
00357      **/
00358     virtual void updateData() {}
00359 
00360     /**
00361      * Return this item's serial number.
00362      * Useful for comparison functions that order by insertion sequence.
00363      **/
00364     int serial() const { return _serial; }
00365 
00366     /**
00367      * Set the text foreground color for all columns.
00368      * For more specific purposes reimiplement paintCell().
00369      **/
00370     void setTextColor( const QColor & col )
00371         { _textColor = col; }
00372 
00373     /**
00374      * Set the text background color for all columns.
00375      * For more specific purposes reimiplement paintCell().
00376      **/
00377     void setBackgroundColor( const QColor & col )
00378         { _backgroundColor = col; }
00379 
00380     /**
00381      * Returns a tool tip text for a specific column of this item.
00382      * 'column' is -1 if the mouse pointer is in the tree indentation area.
00383      *
00384      * This default implementation does nothing.
00385      **/
00386     virtual QString toolTip( int column ) { return QString(); }
00387 
00388 
00389 protected:
00390 
00391     //
00392     // Data members
00393     //
00394 
00395     int         _serial;
00396 };
00397 
00398 
00399 #if FIXME_TOOLTIP
00400 /**
00401  * Tool tip for a QY2ListView widget: Enables individual tool tips specific to
00402  * each list item and each column. Overwrite QY2ListViewItem::toolTip() to use
00403  * this.
00404  **/
00405 class QY2ListViewToolTip : public QToolTip
00406 {
00407 public:
00408 
00409     /**
00410      * Constructor.
00411      **/
00412     QY2ListViewToolTip( QY2ListView * parent )
00413         : QToolTip( parent->viewport() )
00414         , _listView( parent )  {}
00415 
00416     /**
00417      * Destructor (to make gcc 4.x happy)
00418      **/
00419     virtual ~QY2ListViewToolTip() {}
00420 
00421 
00422 protected:
00423 
00424     /**
00425      * Decide if there is a tool tip text at 'p' and display it if there is one.
00426      *
00427      * Reimplemented from QToolTip.
00428      **/
00429     virtual void maybeTip( const QPoint & p );
00430 
00431 
00432     //
00433     // Data members
00434     //
00435 
00436     QY2ListView * _listView;
00437 };
00438 #endif
00439 
00440 #endif // ifndef QY2ListView_h
 All Classes Functions Variables