1 /**************************************************************************** 2 ** 3 ** Definition of QTable widget class 4 ** 5 ** Created : 000607 6 ** 7 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved. 8 ** 9 ** This file is part of the table module of the Qt GUI Toolkit. 10 ** 11 ** This file may be distributed under the terms of the Q Public License 12 ** as defined by Trolltech ASA of Norway and appearing in the file 13 ** LICENSE.QPL included in the packaging of this file. 14 ** 15 ** This file may be distributed and/or modified under the terms of the 16 ** GNU General Public License version 2 as published by the Free Software 17 ** Foundation and appearing in the file LICENSE.GPL included in the 18 ** packaging of this file. 19 ** 20 ** Licensees holding valid Qt Enterprise Edition licenses may use this 21 ** file in accordance with the Qt Commercial License Agreement provided 22 ** with the Software. 23 ** 24 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 25 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 26 ** 27 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for 28 ** information about Qt Commercial License Agreements. 29 ** See http://www.trolltech.com/qpl/ for QPL licensing information. 30 ** See http://www.trolltech.com/gpl/ for GPL licensing information. 31 ** 32 ** Contact info@trolltech.com if any conditions of this licensing are 33 ** not clear to you. 34 ** 35 **********************************************************************/ 36 37 #ifndef QTABLE_H 38 #define QTABLE_H 39 40 #ifndef QT_H 41 #include "qscrollview.h" 42 #include "qpixmap.h" 43 #include "qptrvector.h" 44 #include "qheader.h" 45 #include "qmemarray.h" 46 #include "qptrlist.h" 47 #include "qguardedptr.h" 48 #include "qshared.h" 49 #include "qintdict.h" 50 #include "qstringlist.h" 51 #endif // QT_H 52 53 54 #ifndef QT_NO_TABLE 55 56 #if !defined( QT_MODULE_TABLE ) || defined( QT_LICENSE_PROFESSIONAL ) || defined( QT_INTERNAL_TABLE ) 57 #define QM_EXPORT_TABLE 58 #ifndef QM_TEMPLATE_EXTERN_TABLE 59 # define QM_TEMPLATE_EXTERN_TABLE 60 #endif 61 #else 62 #define QM_EXPORT_TABLE Q_EXPORT 63 #define QM_TEMPLATE_EXTERN_TABLE Q_TEMPLATE_EXTERN 64 #endif 65 66 class QTableHeader; 67 class QValidator; 68 class QTable; 69 class QPaintEvent; 70 class QTimer; 71 class QResizeEvent; 72 class QComboBox; 73 class QCheckBox; 74 class QDragObject; 75 76 struct QTablePrivate; 77 struct QTableHeaderPrivate; 78 79 80 class QM_EXPORT_TABLE QTableSelection 81 { 82 public: 83 QTableSelection(); 84 QTableSelection( int start_row, int start_col, int end_row, int end_col ); 85 void init( int row, int col ); 86 void expandTo( int row, int col ); 87 bool operator==( const QTableSelection &s ) const; 88 bool operator!=( const QTableSelection &s ) const { return !(operator==(s)); } 89 topRow()90 int topRow() const { return tRow; } bottomRow()91 int bottomRow() const { return bRow; } leftCol()92 int leftCol() const { return lCol; } rightCol()93 int rightCol() const { return rCol; } anchorRow()94 int anchorRow() const { return aRow; } anchorCol()95 int anchorCol() const { return aCol; } 96 int numRows() const; 97 int numCols() const; 98 isActive()99 bool isActive() const { return active; } isEmpty()100 bool isEmpty() const { return numRows() == 0; } 101 102 private: 103 uint active : 1; 104 uint inited : 1; 105 int tRow, lCol, bRow, rCol; 106 int aRow, aCol; 107 }; 108 109 #define Q_DEFINED_QTABLE_SELECTION 110 #include "qwinexport.h" 111 112 class QM_EXPORT_TABLE QTableItem : public Qt 113 { 114 friend class QTable; 115 116 public: 117 enum EditType { Never, OnTyping, WhenCurrent, Always }; 118 119 QTableItem( QTable *table, EditType et ); 120 QTableItem( QTable *table, EditType et, const QString &text ); 121 QTableItem( QTable *table, EditType et, const QString &text, 122 const QPixmap &p ); 123 virtual ~QTableItem(); 124 125 virtual QPixmap pixmap() const; 126 virtual QString text() const; 127 virtual void setPixmap( const QPixmap &p ); 128 virtual void setText( const QString &t ); table()129 QTable *table() const { return t; } 130 131 virtual int alignment() const; 132 virtual void setWordWrap( bool b ); 133 bool wordWrap() const; 134 135 EditType editType() const; 136 virtual QWidget *createEditor() const; 137 virtual void setContentFromEditor( QWidget *w ); 138 virtual void setReplaceable( bool ); 139 bool isReplaceable() const; 140 141 virtual QString key() const; 142 virtual QSize sizeHint() const; 143 144 virtual void setSpan( int rs, int cs ); 145 int rowSpan() const; 146 int colSpan() const; 147 148 virtual void setRow( int r ); 149 virtual void setCol( int c ); 150 int row() const; 151 int col() const; 152 153 virtual void paint( QPainter *p, const QColorGroup &cg, 154 const QRect &cr, bool selected ); 155 156 void updateEditor( int oldRow, int oldCol ); 157 158 virtual void setEnabled( bool b ); 159 bool isEnabled() const; 160 161 virtual int rtti() const; 162 static int RTTI; 163 164 private: 165 QString txt; 166 QPixmap pix; 167 QTable *t; 168 EditType edType; 169 uint wordwrap : 1; 170 uint tcha : 1; 171 uint enabled : 1; 172 int rw, cl; 173 int rowspan, colspan; 174 #if (QT_VERSION >= 0x040000) 175 #error "Add a setAlignment() function in 4.0 (but no d pointer)" 176 #endif 177 }; 178 179 #define Q_DEFINED_QTABLE_ITEM 180 #include "qwinexport.h" 181 182 class QM_EXPORT_TABLE QComboTableItem : public QTableItem 183 { 184 public: 185 QComboTableItem( QTable *table, const QStringList &list, bool editable = FALSE ); 186 ~QComboTableItem(); 187 virtual QWidget *createEditor() const; 188 virtual void setContentFromEditor( QWidget *w ); 189 virtual void paint( QPainter *p, const QColorGroup &cg, 190 const QRect &cr, bool selected ); 191 virtual void setCurrentItem( int i ); 192 virtual void setCurrentItem( const QString &i ); 193 int currentItem() const; 194 QString currentText() const; 195 int count() const; 196 #if !defined(Q_NO_USING_KEYWORD) 197 using QTableItem::text; 198 #endif 199 QString text( int i ) const; 200 virtual void setEditable( bool b ); 201 bool isEditable() const; 202 virtual void setStringList( const QStringList &l ); 203 204 int rtti() const; 205 static int RTTI; 206 207 QSize sizeHint() const; 208 209 private: 210 QComboBox *cb; 211 QStringList entries; 212 int current; 213 bool edit; 214 static QComboBox *fakeCombo; 215 static QWidget *fakeComboWidget; 216 static int fakeRef; 217 218 }; 219 220 class QM_EXPORT_TABLE QCheckTableItem : public QTableItem 221 { 222 public: 223 QCheckTableItem( QTable *table, const QString &txt ); 224 void setText( const QString &t ); 225 virtual QWidget *createEditor() const; 226 virtual void setContentFromEditor( QWidget *w ); 227 virtual void paint( QPainter *p, const QColorGroup &cg, 228 const QRect &cr, bool selected ); 229 virtual void setChecked( bool b ); 230 bool isChecked() const; 231 232 int rtti() const; 233 static int RTTI; 234 235 QSize sizeHint() const; 236 237 private: 238 QCheckBox *cb; 239 bool checked; 240 241 }; 242 243 class QM_EXPORT_TABLE QTable : public QScrollView 244 { 245 Q_OBJECT 246 Q_ENUMS( SelectionMode FocusStyle ) 247 Q_PROPERTY( int numRows READ numRows WRITE setNumRows ) 248 Q_PROPERTY( int numCols READ numCols WRITE setNumCols ) 249 Q_PROPERTY( bool showGrid READ showGrid WRITE setShowGrid ) 250 Q_PROPERTY( bool rowMovingEnabled READ rowMovingEnabled WRITE setRowMovingEnabled ) 251 Q_PROPERTY( bool columnMovingEnabled READ columnMovingEnabled WRITE setColumnMovingEnabled ) 252 Q_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly ) 253 Q_PROPERTY( bool sorting READ sorting WRITE setSorting ) 254 Q_PROPERTY( SelectionMode selectionMode READ selectionMode WRITE setSelectionMode ) 255 Q_PROPERTY( FocusStyle focusStyle READ focusStyle WRITE setFocusStyle ) 256 Q_PROPERTY( int numSelections READ numSelections ) 257 258 friend class QTableHeader; 259 friend class QComboTableItem; 260 friend class QCheckTableItem; 261 friend class QTableItem; 262 263 public: 264 QTable( QWidget* parent=0, const char* name=0 ); 265 QTable( int numRows, int numCols, 266 QWidget* parent=0, const char* name=0 ); 267 ~QTable(); 268 269 QHeader *horizontalHeader() const; 270 QHeader *verticalHeader() const; 271 272 enum SelectionMode { Single, Multi, SingleRow, MultiRow, NoSelection }; 273 virtual void setSelectionMode( SelectionMode mode ); 274 SelectionMode selectionMode() const; 275 276 virtual void setItem( int row, int col, QTableItem *item ); 277 virtual void setText( int row, int col, const QString &text ); 278 virtual void setPixmap( int row, int col, const QPixmap &pix ); 279 virtual QTableItem *item( int row, int col ) const; 280 virtual QString text( int row, int col ) const; 281 virtual QPixmap pixmap( int row, int col ) const; 282 virtual void clearCell( int row, int col ); 283 284 virtual QRect cellGeometry( int row, int col ) const; 285 virtual int columnWidth( int col ) const; 286 virtual int rowHeight( int row ) const; 287 virtual int columnPos( int col ) const; 288 virtual int rowPos( int row ) const; 289 virtual int columnAt( int x ) const; 290 virtual int rowAt( int y ) const; 291 292 virtual int numRows() const; 293 virtual int numCols() const; 294 295 void updateCell( int row, int col ); 296 297 bool eventFilter( QObject * o, QEvent * ); 298 currentRow()299 int currentRow() const { return curRow; } currentColumn()300 int currentColumn() const { return curCol; } 301 void ensureCellVisible( int row, int col ); 302 303 bool isSelected( int row, int col ) const; 304 bool isRowSelected( int row, bool full = FALSE ) const; 305 bool isColumnSelected( int col, bool full = FALSE ) const; 306 int numSelections() const; 307 QTableSelection selection( int num ) const; 308 virtual int addSelection( const QTableSelection &s ); 309 virtual void removeSelection( const QTableSelection &s ); 310 virtual void removeSelection( int num ); 311 virtual int currentSelection() const; 312 313 void selectCells( int start_row, int start_col, int end_row, int end_col ); 314 void selectRow( int row ); 315 void selectColumn( int col ); 316 317 bool showGrid() const; 318 319 bool columnMovingEnabled() const; 320 bool rowMovingEnabled() const; 321 322 virtual void sortColumn( int col, bool ascending = TRUE, 323 bool wholeRows = FALSE ); 324 bool sorting() const; 325 326 virtual void takeItem( QTableItem *i ); 327 328 virtual void setCellWidget( int row, int col, QWidget *e ); 329 virtual QWidget *cellWidget( int row, int col ) const; 330 virtual void clearCellWidget( int row, int col ); 331 332 virtual QRect cellRect( int row, int col ) const; 333 334 virtual void paintCell( QPainter *p, int row, int col, 335 const QRect &cr, bool selected ); 336 virtual void paintCell( QPainter *p, int row, int col, 337 const QRect &cr, bool selected, const QColorGroup &cg ); 338 virtual void paintFocus( QPainter *p, const QRect &r ); 339 QSize sizeHint() const; 340 341 bool isReadOnly() const; 342 bool isRowReadOnly( int row ) const; 343 bool isColumnReadOnly( int col ) const; 344 345 void setEnabled( bool b ); 346 347 void repaintSelections(); 348 349 enum FocusStyle { FollowStyle, SpreadSheet }; 350 virtual void setFocusStyle( FocusStyle fs ); 351 FocusStyle focusStyle() const; 352 353 void updateHeaderStates(); 354 355 public slots: 356 virtual void setNumRows( int r ); 357 virtual void setNumCols( int r ); 358 virtual void setShowGrid( bool b ); 359 virtual void hideRow( int row ); 360 virtual void hideColumn( int col ); 361 virtual void showRow( int row ); 362 virtual void showColumn( int col ); 363 bool isRowHidden( int row ) const; 364 bool isColumnHidden( int col ) const; 365 366 virtual void setColumnWidth( int col, int w ); 367 virtual void setRowHeight( int row, int h ); 368 369 virtual void adjustColumn( int col ); 370 virtual void adjustRow( int row ); 371 372 virtual void setColumnStretchable( int col, bool stretch ); 373 virtual void setRowStretchable( int row, bool stretch ); 374 bool isColumnStretchable( int col ) const; 375 bool isRowStretchable( int row ) const; 376 virtual void setSorting( bool b ); 377 virtual void swapRows( int row1, int row2, bool swapHeader = FALSE ); 378 virtual void swapColumns( int col1, int col2, bool swapHeader = FALSE ); 379 virtual void swapCells( int row1, int col1, int row2, int col2 ); 380 381 virtual void setLeftMargin( int m ); 382 virtual void setTopMargin( int m ); 383 virtual void setCurrentCell( int row, int col ); 384 void clearSelection( bool repaint = TRUE ); 385 virtual void setColumnMovingEnabled( bool b ); 386 virtual void setRowMovingEnabled( bool b ); 387 388 virtual void setReadOnly( bool b ); 389 virtual void setRowReadOnly( int row, bool ro ); 390 virtual void setColumnReadOnly( int col, bool ro ); 391 392 virtual void setDragEnabled( bool b ); 393 bool dragEnabled() const; 394 395 virtual void insertRows( int row, int count = 1 ); 396 virtual void insertColumns( int col, int count = 1 ); 397 virtual void removeRow( int row ); 398 virtual void removeRows( const QMemArray<int> &rows ); 399 virtual void removeColumn( int col ); 400 virtual void removeColumns( const QMemArray<int> &cols ); 401 402 virtual void editCell( int row, int col, bool replace = FALSE ); 403 404 void setRowLabels( const QStringList &labels ); 405 void setColumnLabels( const QStringList &labels ); 406 407 protected: 408 enum EditMode { NotEditing, Editing, Replacing }; 409 void drawContents( QPainter *p, int cx, int cy, int cw, int ch ); 410 void contentsMousePressEvent( QMouseEvent* ); 411 void contentsMouseMoveEvent( QMouseEvent* ); 412 void contentsMouseDoubleClickEvent( QMouseEvent* ); 413 void contentsMouseReleaseEvent( QMouseEvent* ); 414 void contentsContextMenuEvent( QContextMenuEvent * e ); 415 void keyPressEvent( QKeyEvent* ); 416 void focusInEvent( QFocusEvent* ); 417 void focusOutEvent( QFocusEvent* ); 418 void viewportResizeEvent( QResizeEvent * ); 419 void showEvent( QShowEvent *e ); 420 void paintEvent( QPaintEvent *e ); 421 void setEditMode( EditMode mode, int row, int col ); 422 #ifndef QT_NO_DRAGANDDROP 423 virtual void contentsDragEnterEvent( QDragEnterEvent *e ); 424 virtual void contentsDragMoveEvent( QDragMoveEvent *e ); 425 virtual void contentsDragLeaveEvent( QDragLeaveEvent *e ); 426 virtual void contentsDropEvent( QDropEvent *e ); 427 virtual QDragObject *dragObject(); 428 virtual void startDrag(); 429 #endif 430 431 virtual void paintEmptyArea( QPainter *p, int cx, int cy, int cw, int ch ); 432 virtual void activateNextCell(); 433 virtual QWidget *createEditor( int row, int col, bool initFromCell ) const; 434 virtual void setCellContentFromEditor( int row, int col ); 435 virtual QWidget *beginEdit( int row, int col, bool replace ); 436 virtual void endEdit( int row, int col, bool accept, bool replace ); 437 438 virtual void resizeData( int len ); 439 virtual void insertWidget( int row, int col, QWidget *w ); 440 int indexOf( int row, int col ) const; 441 442 void windowActivationChange( bool ); 443 bool isEditing() const; 444 EditMode editMode() const; 445 int currEditRow() const; 446 int currEditCol() const; 447 448 protected slots: 449 virtual void columnWidthChanged( int col ); 450 virtual void rowHeightChanged( int row ); 451 virtual void columnIndexChanged( int section, int fromIndex, int toIndex ); 452 virtual void rowIndexChanged( int section, int fromIndex, int toIndex ); 453 virtual void columnClicked( int col ); 454 455 signals: 456 void currentChanged( int row, int col ); 457 void clicked( int row, int col, int button, const QPoint &mousePos ); 458 void doubleClicked( int row, int col, int button, const QPoint &mousePos ); 459 void pressed( int row, int col, int button, const QPoint &mousePos ); 460 void selectionChanged(); 461 void valueChanged( int row, int col ); 462 void contextMenuRequested( int row, int col, const QPoint &pos ); 463 #ifndef QT_NO_DRAGANDDROP 464 void dropped( QDropEvent *e ); 465 #endif 466 467 private slots: 468 void doAutoScroll(); 469 void doValueChanged(); 470 void updateGeometriesSlot(); 471 472 private: 473 void contentsMousePressEventEx( QMouseEvent* ); 474 void drawContents( QPainter* ); 475 void updateGeometries(); 476 void repaintSelections( QTableSelection *oldSelection, 477 QTableSelection *newSelection, 478 bool updateVertical = TRUE, 479 bool updateHorizontal = TRUE ); 480 QRect rangeGeometry( int topRow, int leftCol, 481 int bottomRow, int rightCol, bool &optimize ); 482 void fixRow( int &row, int y ); 483 void fixCol( int &col, int x ); 484 485 void init( int numRows, int numCols ); 486 QSize tableSize() const; 487 void repaintCell( int row, int col ); 488 void contentsToViewport2( int x, int y, int& vx, int& vy ); 489 QPoint contentsToViewport2( const QPoint &p ); 490 void viewportToContents2( int vx, int vy, int& x, int& y ); 491 QPoint viewportToContents2( const QPoint &p ); 492 493 void updateRowWidgets( int row ); 494 void updateColWidgets( int col ); 495 bool isSelected( int row, int col, bool includeCurrent ) const; 496 void setCurrentCell( int row, int col, bool updateSelections, bool ensureVisible = FALSE ); 497 void fixCell( int &row, int &col, int key ); 498 void delayedUpdateGeometries(); 499 struct TableWidget 500 { TableWidgetTableWidget501 TableWidget( QWidget *w, int r, int c ) : wid( w ), row( r ), col ( c ) {} 502 QWidget *wid; 503 int row, col; 504 }; 505 void saveContents( QPtrVector<QTableItem> &tmp, 506 QPtrVector<TableWidget> &tmp2 ); 507 void updateHeaderAndResizeContents( QTableHeader *header, 508 int num, int colRow, 509 int width, bool &updateBefore ); 510 void restoreContents( QPtrVector<QTableItem> &tmp, 511 QPtrVector<TableWidget> &tmp2 ); 512 void finishContentsResze( bool updateBefore ); 513 514 private: 515 QPtrVector<QTableItem> contents; 516 QPtrVector<QWidget> widgets; 517 int curRow; 518 int curCol; 519 QTableHeader *leftHeader, *topHeader; 520 EditMode edMode; 521 int editCol, editRow; 522 QPtrList<QTableSelection> selections; 523 QTableSelection *currentSel; 524 QTimer *autoScrollTimer; 525 int lastSortCol; 526 bool sGrid : 1; 527 bool mRows : 1; 528 bool mCols : 1; 529 bool asc : 1; 530 bool doSort : 1; 531 bool unused : 1; 532 bool readOnly : 1; 533 bool shouldClearSelection : 1; 534 bool dEnabled : 1; 535 bool context_menu : 1; 536 bool drawActiveSelection : 1; 537 bool was_visible : 1; 538 SelectionMode selMode; 539 int pressedRow, pressedCol; 540 QTablePrivate *d; 541 QIntDict<int> roRows; 542 QIntDict<int> roCols; 543 int startDragRow; 544 int startDragCol; 545 QPoint dragStartPos; 546 int oldCurrentRow, oldCurrentCol; 547 QWidget *unused_topLeftCorner; //### remove in 4.0 548 FocusStyle focusStl; 549 QSize unused_cachedSizeHint; // ### remove in 4.0 550 551 #if defined(Q_DISABLE_COPY) 552 QTable( const QTable & ); 553 QTable &operator=( const QTable & ); 554 #endif 555 }; 556 557 #define Q_DEFINED_QTABLE 558 #include "qwinexport.h" 559 #endif // QT_NO_TABLE 560 #endif // TABLE_H 561