home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / include / qtable.h < prev    next >
C/C++ Source or Header  |  2001-10-01  |  16KB  |  498 lines

  1. /****************************************************************************
  2. **
  3. ** Definition of QTable widget class
  4. **
  5. ** Created : 000607
  6. **
  7. ** Copyright (C) 1992-2000 Trolltech AS.  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 AS 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. class QTableHeader;
  57. class QValidator;
  58. class QTable;
  59. class QPaintEvent;
  60. class QTimer;
  61. class QResizeEvent;
  62. class QComboBox;
  63. class QCheckBox;
  64. class QDragObject;
  65.  
  66. struct QTablePrivate;
  67. struct QTableHeaderPrivate;
  68.  
  69.  
  70. class Q_EXPORT QTableSelection
  71. {
  72. public:
  73.     QTableSelection();
  74.     void init( int row, int col );
  75.     void expandTo( int row, int col );
  76.     bool operator==( const QTableSelection &s ) const;
  77.  
  78.     int topRow() const { return tRow; }
  79.     int bottomRow() const { return bRow; }
  80.     int leftCol() const { return lCol; }
  81.     int rightCol() const { return rCol; }
  82.     int anchorRow() const { return aRow; }
  83.     int anchorCol() const { return aCol; }
  84.  
  85.     bool isActive() const { return active; }
  86.  
  87. private:
  88.     uint active : 1;
  89.     uint inited : 1;
  90.     int tRow, lCol, bRow, rCol;
  91.     int aRow, aCol;
  92. };
  93.  
  94.  
  95. class Q_EXPORT QTableItem : public Qt
  96. {
  97. public:
  98.     enum EditType { Never, OnTyping, WhenCurrent, Always };
  99.  
  100.     QTableItem( QTable *table, EditType et, const QString &text );
  101.     QTableItem( QTable *table, EditType et, const QString &text,
  102.                 const QPixmap &p );
  103.     virtual ~QTableItem();
  104.  
  105.     virtual QPixmap pixmap() const;
  106.     virtual QString text() const;
  107.     virtual void setPixmap( const QPixmap &p );
  108.     virtual void setText( const QString &t );
  109.     QTable *table() const { return t; }
  110.  
  111.     virtual int alignment() const;
  112.     virtual void setWordWrap( bool b );
  113.     bool wordWrap() const;
  114.  
  115.     EditType editType() const;
  116.     virtual QWidget *createEditor() const;
  117.     virtual void setContentFromEditor( QWidget *w );
  118.     virtual void setReplaceable( bool );
  119.     bool isReplaceable() const;
  120.  
  121.     virtual QString key() const;
  122.     virtual QSize sizeHint() const;
  123.  
  124.     virtual void setSpan( int rs, int cs );
  125.     int rowSpan() const;
  126.     int colSpan() const;
  127.  
  128.     virtual void setRow( int r );
  129.     virtual void setCol( int c );
  130.     int row() const;
  131.     int col() const;
  132.  
  133.     virtual void paint( QPainter *p, const QColorGroup &cg,
  134.                         const QRect &cr, bool selected );
  135.  
  136.     void updateEditor( int oldRow, int oldCol );
  137.  
  138.     virtual void setEnabled( bool b );
  139.     bool isEnabled() const;
  140.  
  141.     virtual int rtti() const;
  142.     static int RTTI;
  143.  
  144. private:
  145.     QString txt;
  146.     QPixmap pix;
  147.     QTable *t;
  148.     EditType edType;
  149.     uint wordwrap : 1;
  150.     uint tcha : 1;
  151.     uint enabled : 1;
  152.     int rw, cl;
  153.     int rowspan, colspan;
  154.  
  155. };
  156.  
  157. class Q_EXPORT QComboTableItem : public QTableItem
  158. {
  159. public:
  160.     QComboTableItem( QTable *table, const QStringList &list, bool editable = FALSE );
  161.     virtual QWidget *createEditor() const;
  162.     virtual void setContentFromEditor( QWidget *w );
  163.     virtual void paint( QPainter *p, const QColorGroup &cg,
  164.                         const QRect &cr, bool selected );
  165.     virtual void setCurrentItem( int i );
  166.     virtual void setCurrentItem( const QString &i );
  167.     int currentItem() const;
  168.     QString currentText() const;
  169.     int count() const;
  170.     QString text( int i ) const;
  171.     virtual void setEditable( bool b );
  172.     bool isEditable() const;
  173.     virtual void setStringList( const QStringList &l );
  174.  
  175.     int rtti() const;
  176.     static int RTTI;
  177.  
  178. private:
  179.     QComboBox *cb;
  180.     QStringList entries;
  181.     int current;
  182.     bool edit;
  183.     static QComboBox *fakeCombo;
  184.  
  185. };
  186.  
  187. class Q_EXPORT QCheckTableItem : public QTableItem
  188. {
  189. public:
  190.     QCheckTableItem( QTable *table, const QString &txt );
  191.     virtual QWidget *createEditor() const;
  192.     virtual void setContentFromEditor( QWidget *w );
  193.     virtual void paint( QPainter *p, const QColorGroup &cg,
  194.                         const QRect &cr, bool selected );
  195.     virtual void setChecked( bool b );
  196.     bool isChecked() const;
  197.  
  198.     int rtti() const;
  199.     static int RTTI;
  200.  
  201. private:
  202.     QCheckBox *cb;
  203.     bool checked;
  204.  
  205. };
  206.  
  207. #if defined(Q_TEMPLATEDLL)
  208. // MOC_SKIP_BEGIN
  209. template class Q_EXPORT QPtrVector<QTableItem>;
  210. template class Q_EXPORT QPtrVector<QWidget>;
  211. template class Q_EXPORT QPtrList<QTableSelection>;
  212. template class Q_EXPORT QIntDict<int>;
  213. // MOC_SKIP_END
  214. #endif
  215.  
  216.  
  217. class Q_EXPORT QTable : public QScrollView
  218. {
  219.     Q_OBJECT
  220.     Q_ENUMS( SelectionMode FocusStyle )
  221.     Q_PROPERTY( int numRows READ numRows WRITE setNumRows )
  222.     Q_PROPERTY( int numCols READ numCols WRITE setNumCols )
  223.     Q_PROPERTY( bool showGrid READ showGrid WRITE setShowGrid )
  224.     Q_PROPERTY( bool rowMovingEnabled READ rowMovingEnabled WRITE setRowMovingEnabled )
  225.     Q_PROPERTY( bool columnMovingEnabled READ columnMovingEnabled WRITE setColumnMovingEnabled )
  226.     Q_PROPERTY( bool readOnly READ isReadOnly WRITE setReadOnly )
  227.     Q_PROPERTY( bool sorting READ sorting WRITE setSorting )
  228.     Q_PROPERTY( SelectionMode selectionMode READ selectionMode WRITE setSelectionMode )
  229.     Q_PROPERTY( FocusStyle focusStyle READ focusStyle WRITE setFocusStyle )
  230.  
  231.     friend class QTableHeader;
  232.     friend class QComboTableItem;
  233.     friend class QCheckTableItem;
  234.  
  235. public:
  236.     QTable( QWidget* parent=0, const char* name=0 );
  237.     QTable( int numRows, int numCols,
  238.             QWidget* parent=0, const char* name=0 );
  239.     ~QTable();
  240.  
  241.     QHeader *horizontalHeader() const;
  242.     QHeader *verticalHeader() const;
  243.  
  244.     enum SelectionMode { Single, Multi, SingleRow, MultiRow, NoSelection };
  245.     virtual void setSelectionMode( SelectionMode mode );
  246.     SelectionMode selectionMode() const;
  247.  
  248.     virtual void setItem( int row, int col, QTableItem *item );
  249.     virtual void setText( int row, int col, const QString &text );
  250.     virtual void setPixmap( int row, int col, const QPixmap &pix );
  251.     virtual QTableItem *item( int row, int col ) const;
  252.     virtual QString text( int row, int col ) const;
  253.     virtual QPixmap pixmap( int row, int col ) const;
  254.     virtual void clearCell( int row, int col );
  255.  
  256.     virtual QRect cellGeometry( int row, int col ) const;
  257.     virtual int columnWidth( int col ) const;
  258.     virtual int rowHeight( int row ) const;
  259.     virtual int columnPos( int col ) const;
  260.     virtual int rowPos( int row ) const;
  261.     virtual int columnAt( int x ) const;
  262.     virtual int rowAt( int y ) const;
  263.  
  264.     virtual int numRows() const;
  265.     virtual int numCols() const;
  266.  
  267.     void updateCell( int row, int col );
  268.  
  269.     bool eventFilter( QObject * o, QEvent * );
  270.  
  271.     int currentRow() const { return curRow; }
  272.     int currentColumn() const { return curCol; }
  273.     void ensureCellVisible( int row, int col );
  274.  
  275.     bool isSelected( int row, int col ) const;
  276.     bool isRowSelected( int row, bool full = FALSE ) const;
  277.     bool isColumnSelected( int col, bool full = FALSE ) const;
  278.     int numSelections() const;
  279.     QTableSelection selection( int num ) const;
  280.     virtual int addSelection( const QTableSelection &s );
  281.     virtual void removeSelection( const QTableSelection &s );
  282.     virtual void removeSelection( int num );
  283.     virtual int currentSelection() const;
  284.  
  285.     bool showGrid() const;
  286.  
  287.     bool columnMovingEnabled() const;
  288.     bool rowMovingEnabled() const;
  289.  
  290.     virtual void sortColumn( int col, bool ascending = TRUE,
  291.                              bool wholeRows = FALSE );
  292.     bool sorting() const;
  293.  
  294.     virtual void takeItem( QTableItem *i );
  295.  
  296.     virtual void setCellWidget( int row, int col, QWidget *e );
  297.     virtual QWidget *cellWidget( int row, int col ) const;
  298.     virtual void clearCellWidget( int row, int col );
  299.  
  300.     virtual QRect cellRect( int row, int col ) const;
  301.  
  302.     virtual void paintCell( QPainter *p, int row, int col,
  303.                             const QRect &cr, bool selected );
  304.     virtual void paintCell( QPainter *p, int row, int col,
  305.                             const QRect &cr, bool selected, const QColorGroup &cg );
  306.     virtual void paintFocus( QPainter *p, const QRect &r );
  307.     QSize sizeHint() const;
  308.  
  309.     bool isReadOnly() const;
  310.     bool isRowReadOnly( int row ) const;
  311.     bool isColumnReadOnly( int col ) const;
  312.  
  313.     void setEnabled( bool b );
  314.  
  315.     void repaintSelections();
  316.  
  317.     enum FocusStyle { FollowStyle, SpreadSheet };
  318.     virtual void setFocusStyle( FocusStyle fs );
  319.     FocusStyle focusStyle() const;
  320.  
  321. public slots:
  322.     virtual void setNumRows( int r );
  323.     virtual void setNumCols( int r );
  324.     virtual void setShowGrid( bool b );
  325.     virtual void hideRow( int row );
  326.     virtual void hideColumn( int col );
  327.     virtual void showRow( int row );
  328.     virtual void showColumn( int col );
  329.  
  330.     virtual void setColumnWidth( int col, int w );
  331.     virtual void setRowHeight( int row, int h );
  332.  
  333.     virtual void adjustColumn( int col );
  334.     virtual void adjustRow( int row );
  335.  
  336.     virtual void setColumnStretchable( int col, bool stretch );
  337.     virtual void setRowStretchable( int row, bool stretch );
  338.     bool isColumnStretchable( int col ) const;
  339.     bool isRowStretchable( int row ) const;
  340.     virtual void setSorting( bool b );
  341.     virtual void swapRows( int row1, int row2, bool swapHeader = FALSE );
  342.     virtual void swapColumns( int col1, int col2, bool swapHeader = FALSE );
  343.     virtual void swapCells( int row1, int col1, int row2, int col2 );
  344.  
  345.     virtual void setLeftMargin( int m );
  346.     virtual void setTopMargin( int m );
  347.     virtual void setCurrentCell( int row, int col );
  348.     void clearSelection( bool repaint = TRUE );
  349.     virtual void setColumnMovingEnabled( bool b );
  350.     virtual void setRowMovingEnabled( bool b );
  351.  
  352.     virtual void setReadOnly( bool b );
  353.     virtual void setRowReadOnly( int row, bool ro );
  354.     virtual void setColumnReadOnly( int col, bool ro );
  355.  
  356.     virtual void setDragEnabled( bool b );
  357.     bool dragEnabled() const;
  358.  
  359.     virtual void insertRows( int row, int count = 1 );
  360.     virtual void insertColumns( int col, int count = 1 );
  361.     virtual void removeRow( int row );
  362.     virtual void removeRows( const QMemArray<int> &rows );
  363.     virtual void removeColumn( int col );
  364.     virtual void removeColumns( const QMemArray<int> &cols );
  365.  
  366.     virtual void editCell( int row, int col, bool replace = FALSE );
  367.  
  368. protected:
  369.     enum EditMode { NotEditing, Editing, Replacing };
  370.     void drawContents( QPainter *p, int cx, int cy, int cw, int ch );
  371.     void contentsMousePressEvent( QMouseEvent* );
  372.     void contentsMouseMoveEvent( QMouseEvent* );
  373.     void contentsMouseDoubleClickEvent( QMouseEvent* );
  374.     void contentsMouseReleaseEvent( QMouseEvent* );
  375.     void contentsContextMenuEvent( QContextMenuEvent * e );
  376.     void keyPressEvent( QKeyEvent* );
  377.     void focusInEvent( QFocusEvent* );
  378.     void focusOutEvent( QFocusEvent* );
  379.     void viewportResizeEvent( QResizeEvent * );
  380.     void showEvent( QShowEvent *e );
  381.     void setEditMode( EditMode mode, int row, int col );
  382. #ifndef QT_NO_DRAGANDDROP
  383.     virtual void contentsDragEnterEvent( QDragEnterEvent *e );
  384.     virtual void contentsDragMoveEvent( QDragMoveEvent *e );
  385.     virtual void contentsDragLeaveEvent( QDragLeaveEvent *e );
  386.     virtual void contentsDropEvent( QDropEvent *e );
  387.     virtual QDragObject *dragObject();
  388.     virtual void startDrag();
  389. #endif
  390.  
  391.     virtual void paintEmptyArea( QPainter *p, int cx, int cy, int cw, int ch );
  392.     virtual void activateNextCell();
  393.     virtual QWidget *createEditor( int row, int col, bool initFromCell ) const;
  394.     virtual void setCellContentFromEditor( int row, int col );
  395.     virtual QWidget *beginEdit( int row, int col, bool replace );
  396.     virtual void endEdit( int row, int col, bool accept, bool replace );
  397.  
  398.     virtual void resizeData( int len );
  399.     virtual void insertWidget( int row, int col, QWidget *w );
  400.     int indexOf( int row, int col ) const;
  401.  
  402.     void windowActivationChange( bool );
  403.     bool isEditing() const;
  404.     EditMode editMode() const;
  405.     int currEditRow() const;
  406.     int currEditCol() const;
  407.  
  408. protected slots:
  409.     virtual void columnWidthChanged( int col );
  410.     virtual void rowHeightChanged( int row );
  411.     virtual void columnIndexChanged( int section, int fromIndex, int toIndex );
  412.     virtual void rowIndexChanged( int section, int fromIndex, int toIndex );
  413.     virtual void columnClicked( int col );
  414.  
  415. signals:
  416.     void currentChanged( int row, int col );
  417.     void clicked( int row, int col, int button, const QPoint &mousePos );
  418.     void doubleClicked( int row, int col, int button, const QPoint &mousePos );
  419.     void pressed( int row, int col, int button, const QPoint &mousePos );
  420.     void selectionChanged();
  421.     void valueChanged( int row, int col );
  422.     void contextMenuRequested( int row, int col, const QPoint &pos );
  423. #ifndef QT_NO_DRAGANDDROP
  424.     void dropped( QDropEvent *e );
  425. #endif
  426.  
  427. private slots:
  428.     void doAutoScroll();
  429.     void doValueChanged();
  430.  
  431. private:
  432.     void contentsMousePressEventEx( QMouseEvent* );
  433.     void drawContents( QPainter* );
  434.     void updateGeometries();
  435.     void repaintSelections( QTableSelection *oldSelection,
  436.                             QTableSelection *newSelection,
  437.                             bool updateVertical = TRUE,
  438.                             bool updateHorizontal = TRUE );
  439.     QRect rangeGeometry( int topRow, int leftCol,
  440.                          int bottomRow, int rightCol, bool &optimize );
  441.     void fixRow( int &row, int y );
  442.     void fixCol( int &col, int x );
  443.  
  444.     void init( int numRows, int numCols );
  445.     QSize tableSize() const;
  446.     void repaintCell( int row, int col );
  447.     void contentsToViewport2( int x, int y, int& vx, int& vy );
  448.     QPoint contentsToViewport2( const QPoint &p );
  449.     void viewportToContents2( int vx, int vy, int& x, int& y );
  450.     QPoint viewportToContents2( const QPoint &p );
  451.  
  452.     void updateRowWidgets( int row );
  453.     void updateColWidgets( int col );
  454.     bool isSelected( int row, int col, bool includeCurrent ) const;
  455.     void setCurrentCell( int row, int col, bool updateSelections );
  456.  
  457. private:
  458.     QPtrVector<QTableItem> contents;
  459.     QPtrVector<QWidget> widgets;
  460.     int curRow;
  461.     int curCol;
  462.     QTableHeader *leftHeader, *topHeader;
  463.     EditMode edMode;
  464.     int editCol, editRow;
  465.     QPtrList<QTableSelection> selections;
  466.     QTableSelection *currentSel;
  467.     QTimer *autoScrollTimer;
  468.     int lastSortCol;
  469.     bool sGrid : 1;
  470.     bool mRows : 1;
  471.     bool mCols : 1;
  472.     bool asc : 1;
  473.     bool doSort : 1;
  474.     bool mousePressed : 1;
  475.     bool readOnly : 1;
  476.     bool shouldClearSelection : 1;
  477.     bool dEnabled : 1;
  478.     bool context_menu : 1;
  479.     bool drawActiveSelection : 1;
  480.     bool was_visible : 1;
  481.     SelectionMode selMode;
  482.     int pressedRow, pressedCol;
  483.     QTablePrivate *d;
  484.     QIntDict<int> roRows;
  485.     QIntDict<int> roCols;
  486.     int startDragRow;
  487.     int startDragCol;
  488.     QPoint dragStartPos;
  489.     int oldCurrentRow, oldCurrentCol;
  490.     QWidget *topLeftCorner;
  491.     FocusStyle focusStl;
  492.     QSize cachedSizeHint;
  493.  
  494. };
  495.  
  496. #endif // QT_NO_TABLE
  497. #endif // TABLE_H
  498.