📄 q3table.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qglobal.h"#if defined(Q_CC_BOR)// needed for qsort() because of a std namespace problem on Borland#include "qplatformdefs.h"#endif#include "q3table.h"#include <qpainter.h>#include <qlineedit.h>#include <qcursor.h>#include <qapplication.h>#include <qtimer.h>#include <qicon.h>#include <q3combobox.h>#include <qstyleoption.h>#include <qcheckbox.h>#include <q3dragobject.h>#include <qevent.h>#include <q3listbox.h>#include <qstyle.h>#include <q3datatable.h>#include <qvalidator.h>#include <q3button.h>#include <stdlib.h>#include <limits.h>using namespace Qt;class Q3HeaderData;extern bool qt_get_null_label_bit(Q3HeaderData *data, int section);extern void qt_set_null_label_bit(Q3HeaderData *data, int section, bool b);static bool qt_update_cell_widget = true;static bool qt_table_clipper_enabled = true;#ifndef QT_INTERNAL_TABLEQ_COMPAT_EXPORT#endifvoid qt_set_table_clipper_enabled(bool enabled){ qt_table_clipper_enabled = enabled;}class Q_COMPAT_EXPORT Q3TableHeader : public Q3Header{ friend class Q3Table; Q_OBJECTpublic: enum SectionState { Normal, Bold, Selected }; Q3TableHeader(int, Q3Table *t, QWidget* parent=0, const char* name=0); ~Q3TableHeader() {}; void addLabel(const QString &s, int size); void setLabel(int section, const QString & s, int size = -1); void setLabel(int section, const QIconSet & iconset, const QString & s, int size = -1); void setLabels(const QStringList & labels); void removeLabel(int section); void setSectionState(int s, SectionState state); void setSectionStateToAll(SectionState state); SectionState sectionState(int s) const; int sectionSize(int section) const; int sectionPos(int section) const; int sectionAt(int section) const; void setSectionStretchable(int s, bool b); bool isSectionStretchable(int s) const; void updateCache();signals: void sectionSizeChanged(int s);protected: void paintEvent(QPaintEvent *e); void paintSection(QPainter *p, int index, const QRect& fr); void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); void mouseDoubleClickEvent(QMouseEvent *e); void resizeEvent(QResizeEvent *e);private slots: void doAutoScroll(); void sectionWidthChanged(int col, int os, int ns); void indexChanged(int sec, int oldIdx, int newIdx); void updateStretches(); void updateWidgetStretches();private: void updateSelections(); void saveStates(); void setCaching(bool b); void swapSections(int oldIdx, int newIdx, bool swapTable = true); bool doSelection(QMouseEvent *e); void sectionLabelChanged(int section); void resizeArrays(int n);private: Q3MemArray<int> states, oldStates; Q3MemArray<bool> stretchable; Q3MemArray<int> sectionSizes, sectionPoses; bool mousePressed; int pressPos, startPos, endPos; Q3Table *table; QTimer *autoScrollTimer; QWidget *line1, *line2; bool caching; int resizedSection; bool isResizing; int numStretches; QTimer *stretchTimer, *widgetStretchTimer; Q3TableHeaderPrivate *d;};#ifdef _WS_QWS_# define NO_LINE_WIDGET#endifstruct Q3TablePrivate{ Q3TablePrivate() : hasRowSpan(false), hasColSpan(false), inMenuMode(false), redirectMouseEvent(false) { hiddenRows.setAutoDelete(true); hiddenCols.setAutoDelete(true); } uint hasRowSpan : 1; uint hasColSpan : 1; uint inMenuMode : 1; uint redirectMouseEvent : 1; Q3IntDict<int> hiddenRows, hiddenCols; QTimer *geomTimer; int lastVisRow; int lastVisCol;};struct Q3TableHeaderPrivate{#ifdef NO_LINE_WIDGET int oldLinePos;#endif};static bool isRowSelection(Q3Table::SelectionMode selMode){ return selMode == Q3Table::SingleRow || selMode == Q3Table::MultiRow;}/*! \class Q3TableSelection \brief The Q3TableSelection class provides access to a selected area in a Q3Table. \compat The selection is a rectangular set of cells in a Q3Table. One of the rectangle's cells is called the anchor cell; this is the cell that was selected first. The init() function sets the anchor and the selection rectangle to exactly this cell; the expandTo() function expands the selection rectangle to include additional cells. There are various access functions to find out about the area: anchorRow() and anchorCol() return the anchor's position; leftCol(), rightCol(), topRow() and bottomRow() return the rectangle's four edges. All four are part of the selection. A newly created Q3TableSelection is inactive -- isActive() returns false. You must use init() and expandTo() to activate it. \sa Q3Table Q3Table::addSelection() Q3Table::selection() Q3Table::selectCells() Q3Table::selectRow() Q3Table::selectColumn()*//*! Creates an inactive selection. Use init() and expandTo() to activate it.*/Q3TableSelection::Q3TableSelection() : active(false), inited(false), tRow(-1), lCol(-1), bRow(-1), rCol(-1), aRow(-1), aCol(-1){}/*! Creates an active selection, starting at \a start_row and \a start_col, ending at \a end_row and \a end_col.*/Q3TableSelection::Q3TableSelection(int start_row, int start_col, int end_row, int end_col) : active(false), inited(false), tRow(-1), lCol(-1), bRow(-1), rCol(-1), aRow(-1), aCol(-1){ init(start_row, start_col); expandTo(end_row, end_col);}/*! Sets the selection anchor to cell \a row, \a col and the selection to only contain this cell. The selection is not active until expandTo() is called. To extend the selection to include additional cells, call expandTo(). \sa isActive()*/void Q3TableSelection::init(int row, int col){ aCol = lCol = rCol = col; aRow = tRow = bRow = row; active = false; inited = true;}/*! Expands the selection to include cell \a row, \a col. The new selection rectangle is the bounding rectangle of \a row, \a col and the previous selection rectangle. After calling this function the selection is active. If you haven't called init(), this function does nothing. \sa init() isActive()*/void Q3TableSelection::expandTo(int row, int col){ if (!inited) return; active = true; if (row < aRow) { tRow = row; bRow = aRow; } else { tRow = aRow; bRow = row; } if (col < aCol) { lCol = col; rCol = aCol; } else { lCol = aCol; rCol = col; }}/*! Returns true if \a s includes the same cells as the selection; otherwise returns false.*/bool Q3TableSelection::operator==(const Q3TableSelection &s) const{ return (s.active == active && s.tRow == tRow && s.bRow == bRow && s.lCol == lCol && s.rCol == rCol);}/*! \fn bool Q3TableSelection::operator!=(const Q3TableSelection &s) const Returns true if \a s does not include the same cells as the selection; otherwise returns false.*//*! \fn int Q3TableSelection::topRow() const Returns the top row of the selection. \sa bottomRow() leftCol() rightCol()*//*! \fn int Q3TableSelection::bottomRow() const Returns the bottom row of the selection. \sa topRow() leftCol() rightCol()*//*! \fn int Q3TableSelection::leftCol() const Returns the left column of the selection. \sa topRow() bottomRow() rightCol()*//*! \fn int Q3TableSelection::rightCol() const Returns the right column of the selection. \sa topRow() bottomRow() leftCol()*//*! \fn int Q3TableSelection::anchorRow() const Returns the anchor row of the selection. \sa anchorCol() expandTo()*//*! \fn int Q3TableSelection::anchorCol() const Returns the anchor column of the selection. \sa anchorRow() expandTo()*//*! \fn int Q3TableSelection::numRows() const Returns the number of rows in the selection. \sa numCols()*/int Q3TableSelection::numRows() const{ return (tRow < 0) ? 0 : bRow - tRow + 1;}/*! Returns the number of columns in the selection. \sa numRows()*/int Q3TableSelection::numCols() const{ return (lCol < 0) ? 0 : rCol - lCol + 1;}/*! \fn bool Q3TableSelection::isActive() const Returns whether the selection is active or not. A selection is active after init() \e and expandTo() have been called.*//*! \fn bool Q3TableSelection::isEmpty() const Returns whether the selection is empty or not. \sa numRows(), numCols()*//*! \class Q3TableItem \brief The Q3TableItem class provides the cell content for Q3Table cells. \compat For many applications Q3TableItems are ideal for presenting and editing the contents of Q3Table cells. In situations where you need to create very large tables you may prefer an alternative approach to using Q3TableItems: see the notes on large tables. A Q3TableItem contains a cell's data, by default, a string and a pixmap. The table item also holds the cell's display size and how the data should be aligned. The table item specifies the cell's \l EditType and the editor used for in-place editing (by default a QLineEdit). If you want checkboxes use \l{Q3CheckTableItem}, and if you want comboboxes use \l{Q3ComboTableItem}. The \l EditType (set in the constructor) determines whether the cell's contents may be edited. If a pixmap is specified it is displayed to the left of any text. You can change the text or pixmap with setText() and setPixmap() respectively. For text you can use setWordWrap(). When sorting table items the key() function is used; by default this returns the table item's text(). Reimplement key() to customize how your table items will sort.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -