1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTABLEWIDGET_P_H
41#define QTABLEWIDGET_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. This header file may change
48// from version to version without notice, or even be removed.
49//
50// We mean it.
51//
52
53#include <QtWidgets/private/qtwidgetsglobal_p.h>
54#include <qheaderview.h>
55#include <qtablewidget.h>
56#include <qabstractitemmodel.h>
57#include <private/qabstractitemmodel_p.h>
58#include <private/qtableview_p.h>
59#include <private/qwidgetitemdata_p.h>
60
61QT_REQUIRE_CONFIG(tablewidget);
62
63QT_BEGIN_NAMESPACE
64
65class QTableWidgetMimeData : public QMimeData
66{
67 Q_OBJECT
68public:
69 QList<QTableWidgetItem*> items;
70};
71
72class QTableModelLessThan
73{
74public:
75 inline bool operator()(QTableWidgetItem *i1, QTableWidgetItem *i2) const
76 { return (*i1 < *i2); }
77};
78
79class QTableModelGreaterThan
80{
81public:
82 inline bool operator()(QTableWidgetItem *i1, QTableWidgetItem *i2) const
83 { return (*i2 < *i1); }
84};
85
86class QTableModel : public QAbstractTableModel
87{
88 Q_OBJECT
89 friend class QTableWidget;
90
91public:
92 enum ItemFlagsExtension {
93 ItemIsHeaderItem = 128
94 }; // we need this to separate header items from other items
95
96 QTableModel(int rows, int columns, QTableWidget *parent);
97 ~QTableModel();
98
99 bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
100 bool insertColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex()) override;
101
102 bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
103 bool removeColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex()) override;
104
105 void setItem(int row, int column, QTableWidgetItem *item);
106 QTableWidgetItem *takeItem(int row, int column);
107 QTableWidgetItem *item(int row, int column) const;
108 QTableWidgetItem *item(const QModelIndex &index) const;
109 void removeItem(QTableWidgetItem *item);
110
111 void setHorizontalHeaderItem(int section, QTableWidgetItem *item);
112 void setVerticalHeaderItem(int section, QTableWidgetItem *item);
113 QTableWidgetItem *takeHorizontalHeaderItem(int section);
114 QTableWidgetItem *takeVerticalHeaderItem(int section);
115 QTableWidgetItem *horizontalHeaderItem(int section);
116 QTableWidgetItem *verticalHeaderItem(int section);
117
118 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
119 { return QAbstractTableModel::index(row, column, parent); }
120
121 QModelIndex index(const QTableWidgetItem *item) const;
122
123 void setRowCount(int rows);
124 void setColumnCount(int columns);
125
126 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
127 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
128
129 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
130 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
131 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
132#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
133 bool clearItemData(const QModelIndex &index) override;
134#endif
135
136 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
137
138 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
139 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) override;
140
141 Qt::ItemFlags flags(const QModelIndex &index) const override;
142
143 void sort(int column, Qt::SortOrder order) override;
144 static bool itemLessThan(const QPair<QTableWidgetItem*,int> &left,
145 const QPair<QTableWidgetItem*,int> &right);
146 static bool itemGreaterThan(const QPair<QTableWidgetItem*,int> &left,
147 const QPair<QTableWidgetItem*,int> &right);
148
149 void ensureSorted(int column, Qt::SortOrder order, int start, int end);
150 QVector<QTableWidgetItem*> columnItems(int column) const;
151 void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow);
152 static QVector<QTableWidgetItem*>::iterator sortedInsertionIterator(
153 const QVector<QTableWidgetItem*>::iterator &begin,
154 const QVector<QTableWidgetItem*>::iterator &end,
155 Qt::SortOrder order, QTableWidgetItem *item);
156
157 bool isValid(const QModelIndex &index) const;
158 inline long tableIndex(int row, int column) const
159 { return (row * horizontalHeaderItems.count()) + column; }
160
161 void clear();
162 void clearContents();
163 void itemChanged(QTableWidgetItem *item, const QVector<int> &roles = QVector<int>());
164
165 QTableWidgetItem *createItem() const;
166 const QTableWidgetItem *itemPrototype() const;
167 void setItemPrototype(const QTableWidgetItem *item);
168
169 // dnd
170 QStringList mimeTypes() const override;
171 QMimeData *mimeData(const QModelIndexList &indexes) const override;
172 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
173 int row, int column, const QModelIndex &parent) override;
174 Qt::DropActions supportedDropActions() const override;
175
176 QMimeData *internalMimeData() const;
177
178private:
179 const QTableWidgetItem *prototype;
180 QVector<QTableWidgetItem*> tableItems;
181 QVector<QTableWidgetItem*> verticalHeaderItems;
182 QVector<QTableWidgetItem*> horizontalHeaderItems;
183
184 // A cache must be mutable if get-functions should have const modifiers
185 mutable QModelIndexList cachedIndexes;
186};
187
188class QTableWidgetPrivate : public QTableViewPrivate
189{
190 Q_DECLARE_PUBLIC(QTableWidget)
191public:
192 QTableWidgetPrivate() : QTableViewPrivate() {}
193 inline QTableModel *tableModel() const { return qobject_cast<QTableModel*>(object: model); }
194 void setup();
195
196 // view signals
197 void _q_emitItemPressed(const QModelIndex &index);
198 void _q_emitItemClicked(const QModelIndex &index);
199 void _q_emitItemDoubleClicked(const QModelIndex &index);
200 void _q_emitItemActivated(const QModelIndex &index);
201 void _q_emitItemEntered(const QModelIndex &index);
202 // model signals
203 void _q_emitItemChanged(const QModelIndex &index);
204 // selection signals
205 void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current);
206 // sorting
207 void _q_sort();
208 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
209};
210
211class QTableWidgetItemPrivate
212{
213public:
214 QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1) {}
215 QTableWidgetItem *q;
216 int id;
217};
218
219QT_END_NAMESPACE
220
221#endif // QTABLEWIDGET_P_H
222

source code of qtbase/src/widgets/itemviews/qtablewidget_p.h