1 /**************************************************************************** 2 ** $Id: qt/qlistview.h 3.3.8 edited Jan 11 14:38 $ 3 ** 4 ** Definition of QListView widget class 5 ** 6 ** Created : 970809 7 ** 8 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved. 9 ** 10 ** This file is part of the widgets module of the Qt GUI Toolkit. 11 ** 12 ** This file may be distributed under the terms of the Q Public License 13 ** as defined by Trolltech ASA of Norway and appearing in the file 14 ** LICENSE.QPL included in the packaging of this file. 15 ** 16 ** This file may be distributed and/or modified under the terms of the 17 ** GNU General Public License version 2 as published by the Free Software 18 ** Foundation and appearing in the file LICENSE.GPL included in the 19 ** packaging of this file. 20 ** 21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition 22 ** licenses may use this file in accordance with the Qt Commercial License 23 ** Agreement provided with the Software. 24 ** 25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 27 ** 28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 29 ** information about Qt Commercial License Agreements. 30 ** See http://www.trolltech.com/qpl/ for QPL licensing information. 31 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 32 ** 33 ** Contact info@trolltech.com if any conditions of this licensing are 34 ** not clear to you. 35 ** 36 **********************************************************************/ 37 38 #ifndef QLISTVIEW_H 39 #define QLISTVIEW_H 40 41 #ifndef QT_H 42 #include "qscrollview.h" 43 #endif // QT_H 44 45 #ifndef QT_NO_LISTVIEW 46 47 48 class QPixmap; 49 class QFont; 50 class QHeader; 51 class QIconSet; 52 53 class QListView; 54 struct QListViewPrivate; 55 struct QCheckListItemPrivate; 56 class QListViewItemIterator; 57 struct QListViewItemIteratorPrivate; 58 class QDragObject; 59 class QMimeSource; 60 class QLineEdit; 61 class QListViewToolTip; 62 63 class Q_EXPORT QListViewItem : public Qt 64 { 65 friend class QListViewItemIterator; 66 friend class QListViewToolTip; 67 68 public: 69 QListViewItem( QListView * parent ); 70 QListViewItem( QListViewItem * parent ); 71 QListViewItem( QListView * parent, QListViewItem * after ); 72 QListViewItem( QListViewItem * parent, QListViewItem * after ); 73 74 QListViewItem( QListView * parent, 75 QString, QString = QString::null, 76 QString = QString::null, QString = QString::null, 77 QString = QString::null, QString = QString::null, 78 QString = QString::null, QString = QString::null ); 79 QListViewItem( QListViewItem * parent, 80 QString, QString = QString::null, 81 QString = QString::null, QString = QString::null, 82 QString = QString::null, QString = QString::null, 83 QString = QString::null, QString = QString::null ); 84 85 QListViewItem( QListView * parent, QListViewItem * after, 86 QString, QString = QString::null, 87 QString = QString::null, QString = QString::null, 88 QString = QString::null, QString = QString::null, 89 QString = QString::null, QString = QString::null ); 90 QListViewItem( QListViewItem * parent, QListViewItem * after, 91 QString, QString = QString::null, 92 QString = QString::null, QString = QString::null, 93 QString = QString::null, QString = QString::null, 94 QString = QString::null, QString = QString::null ); 95 virtual ~QListViewItem(); 96 97 virtual void insertItem( QListViewItem * ); 98 virtual void takeItem( QListViewItem * ); removeItem(QListViewItem * item)99 virtual void removeItem( QListViewItem *item ) { takeItem( item ); } //obsolete, use takeItem instead 100 101 int height() const; 102 virtual void invalidateHeight(); 103 int totalHeight() const; 104 virtual int width( const QFontMetrics&, 105 const QListView*, int column) const; 106 void widthChanged(int column=-1) const; 107 int depth() const; 108 109 virtual void setText( int, const QString &); 110 virtual QString text( int ) const; 111 112 virtual void setPixmap( int, const QPixmap & ); 113 virtual const QPixmap * pixmap( int ) const; 114 115 virtual QString key( int, bool ) const; 116 virtual int compare( QListViewItem *i, int col, bool ) const; 117 virtual void sortChildItems( int, bool ); 118 childCount()119 int childCount() const { return nChildren; } 120 isOpen()121 bool isOpen() const { return open; } 122 virtual void setOpen( bool ); 123 virtual void setup(); 124 125 virtual void setSelected( bool ); isSelected()126 bool isSelected() const { return selected; } 127 128 virtual void paintCell( QPainter *, const QColorGroup & cg, 129 int column, int width, int alignment ); 130 virtual void paintBranches( QPainter * p, const QColorGroup & cg, 131 int w, int y, int h ); 132 virtual void paintFocus( QPainter *, const QColorGroup & cg, 133 const QRect & r ); 134 135 QListViewItem * firstChild() const; nextSibling()136 QListViewItem * nextSibling() const { return siblingItem; } 137 QListViewItem * parent() const; 138 139 QListViewItem * itemAbove(); 140 QListViewItem * itemBelow(); 141 142 int itemPos() const; 143 144 QListView *listView() const; 145 146 virtual void setSelectable( bool enable ); isSelectable()147 bool isSelectable() const { return selectable && enabled; } 148 149 virtual void setExpandable( bool ); isExpandable()150 bool isExpandable() const { return expandable; } 151 152 void repaint() const; 153 154 virtual void sort(); 155 void moveItem( QListViewItem *after ); 156 157 virtual void setDragEnabled( bool allow ); 158 virtual void setDropEnabled( bool allow ); 159 bool dragEnabled() const; 160 bool dropEnabled() const; 161 virtual bool acceptDrop( const QMimeSource *mime ) const; 162 163 void setVisible( bool b ); 164 bool isVisible() const; 165 166 virtual void setRenameEnabled( int col, bool b ); 167 bool renameEnabled( int col ) const; 168 virtual void startRename( int col ); 169 170 virtual void setEnabled( bool b ); 171 bool isEnabled() const; 172 173 virtual int rtti() const; 174 // ### Qt 4: make const or better use an enum 175 static int RTTI; 176 177 virtual void setMultiLinesEnabled( bool b ); 178 bool multiLinesEnabled() const; 179 180 protected: 181 virtual void enforceSortOrder() const; 182 virtual void setHeight( int ); 183 virtual void activate(); 184 185 bool activatedPos( QPoint & ); 186 #ifndef QT_NO_DRAGANDDROP 187 virtual void dropped( QDropEvent *e ); 188 #endif 189 virtual void dragEntered(); 190 virtual void dragLeft(); 191 virtual void okRename( int col ); 192 virtual void cancelRename( int col ); 193 194 void ignoreDoubleClick(); 195 196 private: 197 void init(); 198 void moveToJustAfter( QListViewItem * ); 199 void enforceSortOrderBackToRoot(); 200 void removeRenameBox(); 201 202 int ownHeight; 203 int maybeTotalHeight; 204 int nChildren; 205 206 uint lsc: 14; 207 uint lso: 1; 208 uint open : 1; 209 uint selected : 1; 210 uint selectable: 1; 211 uint configured: 1; 212 uint expandable: 1; 213 uint is_root: 1; 214 uint allow_drag : 1; 215 uint allow_drop : 1; 216 uint visible : 1; 217 uint enabled : 1; 218 uint mlenabled : 1; 219 220 QListViewItem * parentItem; 221 QListViewItem * siblingItem; 222 QListViewItem * childItem; 223 QLineEdit *renameBox; 224 int renameCol; 225 226 void * columns; 227 228 friend class QListView; 229 }; 230 231 class QCheckListItem; 232 233 class Q_EXPORT QListView: public QScrollView 234 { 235 friend class QListViewItemIterator; 236 friend class QListViewItem; 237 friend class QCheckListItem; 238 friend class QListViewToolTip; 239 240 Q_OBJECT 241 Q_ENUMS( SelectionMode ResizeMode RenameAction ) 242 Q_PROPERTY( int columns READ columns ) 243 Q_PROPERTY( bool multiSelection READ isMultiSelection WRITE setMultiSelection DESIGNABLE false ) 244 Q_PROPERTY( SelectionMode selectionMode READ selectionMode WRITE setSelectionMode ) 245 Q_PROPERTY( int childCount READ childCount ) 246 Q_PROPERTY( bool allColumnsShowFocus READ allColumnsShowFocus WRITE setAllColumnsShowFocus ) 247 Q_PROPERTY( bool showSortIndicator READ showSortIndicator WRITE setShowSortIndicator ) 248 Q_PROPERTY( int itemMargin READ itemMargin WRITE setItemMargin ) 249 Q_PROPERTY( bool rootIsDecorated READ rootIsDecorated WRITE setRootIsDecorated ) 250 Q_PROPERTY( bool showToolTips READ showToolTips WRITE setShowToolTips ) 251 Q_PROPERTY( ResizeMode resizeMode READ resizeMode WRITE setResizeMode ) 252 Q_PROPERTY( int treeStepSize READ treeStepSize WRITE setTreeStepSize ) 253 Q_PROPERTY( RenameAction defaultRenameAction READ defaultRenameAction WRITE setDefaultRenameAction ) 254 255 public: 256 QListView( QWidget* parent=0, const char* name=0, WFlags f = 0 ); 257 ~QListView(); 258 259 int treeStepSize() const; 260 virtual void setTreeStepSize( int ); 261 262 virtual void insertItem( QListViewItem * ); 263 virtual void takeItem( QListViewItem * ); removeItem(QListViewItem * item)264 virtual void removeItem( QListViewItem *item ) { takeItem( item ); } // obsolete, use takeItem instead 265 266 QHeader * header() const; 267 268 virtual int addColumn( const QString &label, int size = -1); 269 virtual int addColumn( const QIconSet& iconset, const QString &label, int size = -1); 270 virtual void removeColumn( int index ); 271 virtual void setColumnText( int column, const QString &label ); 272 virtual void setColumnText( int column, const QIconSet& iconset, const QString &label ); 273 QString columnText( int column ) const; 274 virtual void setColumnWidth( int column, int width ); 275 int columnWidth( int column ) const; 276 enum WidthMode { Manual, Maximum }; 277 virtual void setColumnWidthMode( int column, WidthMode ); 278 WidthMode columnWidthMode( int column ) const; 279 int columns() const; 280 281 virtual void setColumnAlignment( int, int ); 282 int columnAlignment( int ) const; 283 284 void show(); 285 286 QListViewItem * itemAt( const QPoint & screenPos ) const; 287 QRect itemRect( const QListViewItem * ) const; 288 int itemPos( const QListViewItem * ); 289 290 void ensureItemVisible( const QListViewItem * ); 291 292 void repaintItem( const QListViewItem * ) const; 293 294 virtual void setMultiSelection( bool enable ); 295 bool isMultiSelection() const; 296 297 enum SelectionMode { Single, Multi, Extended, NoSelection }; 298 void setSelectionMode( SelectionMode mode ); 299 SelectionMode selectionMode() const; 300 301 virtual void clearSelection(); 302 virtual void setSelected( QListViewItem *, bool ); 303 void setSelectionAnchor( QListViewItem * ); 304 bool isSelected( const QListViewItem * ) const; 305 QListViewItem * selectedItem() const; 306 virtual void setOpen( QListViewItem *, bool ); 307 bool isOpen( const QListViewItem * ) const; 308 309 virtual void setCurrentItem( QListViewItem * ); 310 QListViewItem * currentItem() const; 311 312 QListViewItem * firstChild() const; 313 QListViewItem * lastItem() const; 314 315 int childCount() const; 316 317 virtual void setAllColumnsShowFocus( bool ); 318 bool allColumnsShowFocus() const; 319 320 virtual void setItemMargin( int ); 321 int itemMargin() const; 322 323 virtual void setRootIsDecorated( bool ); 324 bool rootIsDecorated() const; 325 326 virtual void setSorting( int column, bool ascending = TRUE ); 327 int sortColumn() const; 328 void setSortColumn( int column ); 329 SortOrder sortOrder() const; 330 void setSortOrder( SortOrder order ); 331 virtual void sort(); 332 333 virtual void setFont( const QFont & ); 334 virtual void setPalette( const QPalette & ); 335 336 bool eventFilter( QObject * o, QEvent * ); 337 338 QSize sizeHint() const; 339 QSize minimumSizeHint() const; 340 341 virtual void setShowSortIndicator( bool show ); 342 bool showSortIndicator() const; 343 virtual void setShowToolTips( bool b ); 344 bool showToolTips() const; 345 346 enum ResizeMode { NoColumn, AllColumns, LastColumn }; 347 virtual void setResizeMode( ResizeMode m ); 348 ResizeMode resizeMode() const; 349 350 QListViewItem * findItem( const QString& text, int column, ComparisonFlags compare = ExactMatch | CaseSensitive ) const; 351 352 enum RenameAction { Accept, Reject }; 353 virtual void setDefaultRenameAction( RenameAction a ); 354 RenameAction defaultRenameAction() const; 355 bool isRenaming() const; 356 357 void hideColumn( int column ); 358 359 public slots: 360 virtual void clear(); 361 virtual void invertSelection(); 362 virtual void selectAll( bool select ); 363 void triggerUpdate(); 364 void setContentsPos( int x, int y ); 365 void adjustColumn( int col ); 366 367 signals: 368 void selectionChanged(); 369 void selectionChanged( QListViewItem * ); 370 void currentChanged( QListViewItem * ); 371 void clicked( QListViewItem * ); 372 void clicked( QListViewItem *, const QPoint &, int ); 373 void pressed( QListViewItem * ); 374 void pressed( QListViewItem *, const QPoint &, int ); 375 376 void doubleClicked( QListViewItem * ); 377 void doubleClicked( QListViewItem *, const QPoint&, int ); 378 void returnPressed( QListViewItem * ); 379 void spacePressed( QListViewItem * ); 380 void rightButtonClicked( QListViewItem *, const QPoint&, int ); 381 void rightButtonPressed( QListViewItem *, const QPoint&, int ); 382 void mouseButtonPressed( int, QListViewItem *, const QPoint& , int ); 383 void mouseButtonClicked( int, QListViewItem *, const QPoint&, int ); 384 385 void contextMenuRequested( QListViewItem *, const QPoint &, int ); 386 387 void onItem( QListViewItem *item ); 388 void onViewport(); 389 390 void expanded( QListViewItem *item ); 391 void collapsed( QListViewItem *item ); 392 #ifndef QT_NO_DRAGANDDROP 393 void dropped( QDropEvent *e ); 394 #endif 395 void itemRenamed( QListViewItem *item, int col, const QString & ); 396 void itemRenamed( QListViewItem *item, int col ); 397 398 protected: 399 void contentsMousePressEvent( QMouseEvent * e ); 400 void contentsMouseReleaseEvent( QMouseEvent * e ); 401 void contentsMouseMoveEvent( QMouseEvent * e ); 402 void contentsMouseDoubleClickEvent( QMouseEvent * e ); 403 void contentsContextMenuEvent( QContextMenuEvent * e ); 404 #ifndef QT_NO_DRAGANDDROP 405 void contentsDragEnterEvent( QDragEnterEvent *e ); 406 void contentsDragMoveEvent( QDragMoveEvent *e ); 407 void contentsDragLeaveEvent( QDragLeaveEvent *e ); 408 void contentsDropEvent( QDropEvent *e ); 409 virtual QDragObject *dragObject(); 410 virtual void startDrag(); 411 #endif 412 413 void focusInEvent( QFocusEvent * e ); 414 void focusOutEvent( QFocusEvent * e ); 415 416 void keyPressEvent( QKeyEvent *e ); 417 418 void resizeEvent( QResizeEvent *e ); 419 void viewportResizeEvent( QResizeEvent *e ); 420 421 void showEvent( QShowEvent * ); 422 423 void drawContentsOffset( QPainter *, int ox, int oy, 424 int cx, int cy, int cw, int ch ); 425 426 virtual void paintEmptyArea( QPainter *, const QRect & ); 427 void styleChange( QStyle& ); 428 void windowActivationChange( bool ); 429 430 protected slots: 431 void updateContents(); 432 void doAutoScroll(); 433 434 private slots: 435 void changeSortColumn( int ); 436 void handleIndexChange(); 437 void updateDirtyItems(); 438 void makeVisible(); 439 void handleSizeChange( int, int, int ); 440 void startRename(); 441 void openFocusItem(); 442 443 private: 444 void contentsMousePressEventEx( QMouseEvent * e ); 445 void contentsMouseReleaseEventEx( QMouseEvent * e ); 446 void init(); 447 void updateGeometries(); 448 void buildDrawableList() const; 449 void reconfigureItems(); 450 void widthChanged(const QListViewItem*, int c); 451 void handleItemChange( QListViewItem *old, bool shift, bool control ); 452 void selectRange( QListViewItem *from, QListViewItem *to, bool invert, bool includeFirst, bool clearSel = FALSE ); 453 bool selectRange( QListViewItem *newItem, QListViewItem *oldItem, QListViewItem *anchorItem ); 454 bool clearRange( QListViewItem *from, QListViewItem *to, bool includeFirst = TRUE ); 455 void doAutoScroll( const QPoint &cursorPos ); 456 457 QListViewPrivate * d; 458 459 private: // Disabled copy constructor and operator= 460 #if defined(Q_DISABLE_COPY) 461 QListView( const QListView & ); 462 QListView &operator=( const QListView & ); 463 #endif 464 }; 465 466 467 class Q_EXPORT QCheckListItem : public QListViewItem 468 { 469 public: 470 enum Type { RadioButton, 471 CheckBox, 472 Controller, 473 RadioButtonController=Controller, 474 CheckBoxController }; 475 // ### should be integrated with qbutton in ver4 perhaps 476 enum ToggleState { Off, NoChange, On }; 477 478 QCheckListItem( QCheckListItem *parent, const QString &text, 479 Type = RadioButtonController ); 480 QCheckListItem( QCheckListItem *parent, QListViewItem *after, 481 const QString &text, Type = RadioButtonController ); 482 QCheckListItem( QListViewItem *parent, const QString &text, 483 Type = RadioButtonController ); 484 QCheckListItem( QListViewItem *parent, QListViewItem *after, 485 const QString &text, Type = RadioButtonController ); 486 QCheckListItem( QListView *parent, const QString &text, 487 Type = RadioButtonController ); 488 QCheckListItem( QListView *parent, QListViewItem *after, 489 const QString &text, Type = RadioButtonController ); 490 QCheckListItem( QListViewItem *parent, const QString &text, 491 const QPixmap & ); 492 QCheckListItem( QListView *parent, const QString &text, 493 const QPixmap & ); 494 ~QCheckListItem(); 495 496 void paintCell( QPainter *, const QColorGroup & cg, 497 int column, int width, int alignment ); 498 virtual void paintFocus( QPainter *, const QColorGroup & cg, 499 const QRect & r ); 500 int width( const QFontMetrics&, const QListView*, int column) const; 501 void setup(); 502 503 virtual void setOn( bool ); // ### should be replaced by setChecked in ver4 isOn()504 bool isOn() const { return on; } type()505 Type type() const { return myType; } text()506 QString text() const { return QListViewItem::text( 0 ); } text(int n)507 QString text( int n ) const { return QListViewItem::text( n ); } 508 509 void setTristate( bool ); 510 bool isTristate() const; 511 ToggleState state() const; 512 void setState( ToggleState s); 513 514 int rtti() const; 515 static int RTTI; 516 517 protected: 518 void activate(); 519 void turnOffChild(); 520 virtual void stateChange( bool ); 521 522 private: 523 void init(); 524 ToggleState internalState() const; 525 void setStoredState( ToggleState newState, void *key ); 526 ToggleState storedState( void *key ) const; 527 void stateChange( ToggleState s ); 528 void restoreState( void *key, int depth = 0 ); 529 void updateController( bool update = TRUE , bool store = FALSE ); 530 void updateStoredState( void *key ); 531 void setState( ToggleState s, bool update, bool store ); 532 void setCurrentState( ToggleState s ); 533 534 Type myType; 535 bool on; // ### remove in ver4 536 QCheckListItemPrivate *d; 537 }; 538 539 class Q_EXPORT QListViewItemIterator 540 { 541 friend struct QListViewPrivate; 542 friend class QListView; 543 friend class QListViewItem; 544 545 public: 546 enum IteratorFlag { 547 Visible = 0x00000001, 548 Invisible = 0x00000002, 549 Selected = 0x00000004, 550 Unselected = 0x00000008, 551 Selectable = 0x00000010, 552 NotSelectable = 0x00000020, 553 DragEnabled = 0x00000040, 554 DragDisabled = 0x00000080, 555 DropEnabled = 0x00000100, 556 DropDisabled = 0x00000200, 557 Expandable = 0x00000400, 558 NotExpandable = 0x00000800, 559 Checked = 0x00001000, 560 NotChecked = 0x00002000 561 }; 562 563 QListViewItemIterator(); 564 QListViewItemIterator( QListViewItem *item ); 565 QListViewItemIterator( QListViewItem *item, int iteratorFlags ); 566 567 QListViewItemIterator( const QListViewItemIterator &it ); 568 QListViewItemIterator( QListView *lv ); 569 QListViewItemIterator( QListView *lv, int iteratorFlags ); 570 571 QListViewItemIterator &operator=( const QListViewItemIterator &it ); 572 573 ~QListViewItemIterator(); 574 575 QListViewItemIterator &operator++(); 576 const QListViewItemIterator operator++( int ); 577 QListViewItemIterator &operator+=( int j ); 578 579 QListViewItemIterator &operator--(); 580 const QListViewItemIterator operator--( int ); 581 QListViewItemIterator &operator-=( int j ); 582 583 QListViewItem* operator*(); 584 QListViewItem *current() const; 585 586 protected: 587 QListViewItem *curr; 588 QListView *listView; 589 590 private: 591 QListViewItemIteratorPrivate* d() const; 592 void init( int flags ); 593 void addToListView(); 594 void currentRemoved(); 595 bool matchesFlags( const QListViewItem* ) const; 596 bool testPair( QListViewItemIterator::IteratorFlag, QListViewItemIterator::IteratorFlag, bool ) const; 597 bool isChecked( const QListViewItem* ) const; 598 }; 599 600 #endif // QT_NO_LISTVIEW 601 602 #endif // QLISTVIEW_H 603