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