| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QTABLEWIDGET_P_H |
| 5 | #define QTABLEWIDGET_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. This header file may change |
| 12 | // from version to version without notice, or even be removed. |
| 13 | // |
| 14 | // We mean it. |
| 15 | // |
| 16 | |
| 17 | #include <QtWidgets/private/qtwidgetsglobal_p.h> |
| 18 | #include <qheaderview.h> |
| 19 | #include <qtablewidget.h> |
| 20 | #include <qabstractitemmodel.h> |
| 21 | #include <private/qabstractitemmodel_p.h> |
| 22 | #include <private/qtableview_p.h> |
| 23 | #include <private/qwidgetitemdata_p.h> |
| 24 | |
| 25 | #include <array> |
| 26 | |
| 27 | QT_REQUIRE_CONFIG(tablewidget); |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | class QTableWidgetMimeData : public QMimeData |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | public: |
| 35 | QList<QTableWidgetItem*> items; |
| 36 | }; |
| 37 | |
| 38 | class QTableModelLessThan |
| 39 | { |
| 40 | public: |
| 41 | inline bool operator()(QTableWidgetItem *i1, QTableWidgetItem *i2) const |
| 42 | { return (*i1 < *i2); } |
| 43 | }; |
| 44 | |
| 45 | class QTableModelGreaterThan |
| 46 | { |
| 47 | public: |
| 48 | inline bool operator()(QTableWidgetItem *i1, QTableWidgetItem *i2) const |
| 49 | { return (*i2 < *i1); } |
| 50 | }; |
| 51 | |
| 52 | class QTableModel : public QAbstractTableModel |
| 53 | { |
| 54 | Q_OBJECT |
| 55 | friend class QTableWidget; |
| 56 | |
| 57 | public: |
| 58 | QTableModel(int rows, int columns, QTableWidget *parent); |
| 59 | ~QTableModel(); |
| 60 | |
| 61 | bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override; |
| 62 | bool insertColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex()) override; |
| 63 | |
| 64 | bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override; |
| 65 | bool removeColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex()) override; |
| 66 | |
| 67 | bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override; |
| 68 | |
| 69 | void setItem(int row, int column, QTableWidgetItem *item); |
| 70 | QTableWidgetItem *takeItem(int row, int column); |
| 71 | QTableWidgetItem *item(int row, int column) const; |
| 72 | QTableWidgetItem *item(const QModelIndex &index) const; |
| 73 | void removeItem(QTableWidgetItem *item); |
| 74 | |
| 75 | void (int section, QTableWidgetItem *item); |
| 76 | void (int section, QTableWidgetItem *item); |
| 77 | QTableWidgetItem *(int section); |
| 78 | QTableWidgetItem *(int section); |
| 79 | QTableWidgetItem *(int section); |
| 80 | QTableWidgetItem *(int section); |
| 81 | |
| 82 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override |
| 83 | { return QAbstractTableModel::index(row, column, parent); } |
| 84 | |
| 85 | QModelIndex index(const QTableWidgetItem *item) const; |
| 86 | |
| 87 | void setRowCount(int rows); |
| 88 | void setColumnCount(int columns); |
| 89 | |
| 90 | int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
| 91 | int columnCount(const QModelIndex &parent = QModelIndex()) const override; |
| 92 | |
| 93 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 94 | bool setData(const QModelIndex &index, const QVariant &value, int role) override; |
| 95 | bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override; |
| 96 | bool clearItemData(const QModelIndex &index) override; |
| 97 | |
| 98 | QMap<int, QVariant> itemData(const QModelIndex &index) const override; |
| 99 | |
| 100 | QVariant (int section, Qt::Orientation orientation, int role) const override; |
| 101 | bool (int section, Qt::Orientation orientation, const QVariant &value, int role) override; |
| 102 | |
| 103 | Qt::ItemFlags flags(const QModelIndex &index) const override; |
| 104 | |
| 105 | void sort(int column, Qt::SortOrder order) override; |
| 106 | static bool itemLessThan(const std::pair<QTableWidgetItem*,int> &left, |
| 107 | const std::pair<QTableWidgetItem*,int> &right); |
| 108 | static bool itemGreaterThan(const std::pair<QTableWidgetItem*,int> &left, |
| 109 | const std::pair<QTableWidgetItem*,int> &right); |
| 110 | |
| 111 | void ensureSorted(int column, Qt::SortOrder order, int start, int end); |
| 112 | QList<QTableWidgetItem *> columnItems(int column) const; |
| 113 | void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow); |
| 114 | static QList<QTableWidgetItem *>::iterator |
| 115 | sortedInsertionIterator(const QList<QTableWidgetItem *>::iterator &begin, |
| 116 | const QList<QTableWidgetItem *>::iterator &end, Qt::SortOrder order, |
| 117 | QTableWidgetItem *item); |
| 118 | |
| 119 | bool isValid(const QModelIndex &index) const; |
| 120 | inline long tableIndex(int row, int column) const |
| 121 | { return (row * horizontalHeaderItems.size()) + column; } |
| 122 | |
| 123 | void clear(); |
| 124 | void clearContents(); |
| 125 | void itemChanged(QTableWidgetItem *item, const QList<int> &roles = QList<int>()); |
| 126 | |
| 127 | QTableWidgetItem *createItem() const; |
| 128 | const QTableWidgetItem *itemPrototype() const; |
| 129 | void setItemPrototype(const QTableWidgetItem *item); |
| 130 | |
| 131 | // dnd |
| 132 | QStringList mimeTypes() const override; |
| 133 | QMimeData *mimeData(const QModelIndexList &indexes) const override; |
| 134 | bool dropMimeData(const QMimeData *data, Qt::DropAction action, |
| 135 | int row, int column, const QModelIndex &parent) override; |
| 136 | Qt::DropActions supportedDropActions() const override; |
| 137 | |
| 138 | QMimeData *internalMimeData() const; |
| 139 | |
| 140 | private: |
| 141 | const QTableWidgetItem *prototype; |
| 142 | QList<QTableWidgetItem *> tableItems; |
| 143 | QList<QTableWidgetItem *> ; |
| 144 | QList<QTableWidgetItem *> ; |
| 145 | |
| 146 | // A cache must be mutable if get-functions should have const modifiers |
| 147 | mutable QModelIndexList cachedIndexes; |
| 148 | }; |
| 149 | |
| 150 | class QTableWidgetPrivate : public QTableViewPrivate |
| 151 | { |
| 152 | Q_DECLARE_PUBLIC(QTableWidget) |
| 153 | public: |
| 154 | QTableWidgetPrivate() : QTableViewPrivate() {} |
| 155 | inline QTableModel *tableModel() const { return qobject_cast<QTableModel*>(object: model); } |
| 156 | void setup(); |
| 157 | void clearConnections(); |
| 158 | |
| 159 | // view signals |
| 160 | void emitItemPressed(const QModelIndex &index); |
| 161 | void emitItemClicked(const QModelIndex &index); |
| 162 | void emitItemDoubleClicked(const QModelIndex &index); |
| 163 | void emitItemActivated(const QModelIndex &index); |
| 164 | void emitItemEntered(const QModelIndex &index); |
| 165 | // model signals |
| 166 | void emitItemChanged(const QModelIndex &index); |
| 167 | // selection signals |
| 168 | void emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex ¤t); |
| 169 | // sorting |
| 170 | void sort(); |
| 171 | void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); |
| 172 | |
| 173 | std::array<QMetaObject::Connection, 10> connections; |
| 174 | }; |
| 175 | |
| 176 | class QTableWidgetItemPrivate |
| 177 | { |
| 178 | public: |
| 179 | QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1), headerItem(false) {} |
| 180 | QTableWidgetItem *q; |
| 181 | int id; |
| 182 | bool ; // Qt 7 TODO: inline this stuff in the public class. |
| 183 | }; |
| 184 | |
| 185 | QT_END_NAMESPACE |
| 186 | |
| 187 | #endif // QTABLEWIDGET_P_H |
| 188 | |