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 bool clearItemData(const QModelIndex &index) override;
133
134 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
135
136 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
137 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) override;
138
139 Qt::ItemFlags flags(const QModelIndex &index) const override;
140
141 void sort(int column, Qt::SortOrder order) override;
142 static bool itemLessThan(const QPair<QTableWidgetItem*,int> &left,
143 const QPair<QTableWidgetItem*,int> &right);
144 static bool itemGreaterThan(const QPair<QTableWidgetItem*,int> &left,
145 const QPair<QTableWidgetItem*,int> &right);
146
147 void ensureSorted(int column, Qt::SortOrder order, int start, int end);
148 QList<QTableWidgetItem *> columnItems(int column) const;
149 void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow);
150 static QList<QTableWidgetItem *>::iterator
151 sortedInsertionIterator(const QList<QTableWidgetItem *>::iterator &begin,
152 const QList<QTableWidgetItem *>::iterator &end, Qt::SortOrder order,
153 QTableWidgetItem *item);
154
155 bool isValid(const QModelIndex &index) const;
156 inline long tableIndex(int row, int column) const
157 { return (row * horizontalHeaderItems.count()) + column; }
158
159 void clear();
160 void clearContents();
161 void itemChanged(QTableWidgetItem *item, const QList<int> &roles = QList<int>());
162
163 QTableWidgetItem *createItem() const;
164 const QTableWidgetItem *itemPrototype() const;
165 void setItemPrototype(const QTableWidgetItem *item);
166
167 // dnd
168 QStringList mimeTypes() const override;
169 QMimeData *mimeData(const QModelIndexList &indexes) const override;
170 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
171 int row, int column, const QModelIndex &parent) override;
172 Qt::DropActions supportedDropActions() const override;
173
174 QMimeData *internalMimeData() const;
175
176private:
177 const QTableWidgetItem *prototype;
178 QList<QTableWidgetItem *> tableItems;
179 QList<QTableWidgetItem *> verticalHeaderItems;
180 QList<QTableWidgetItem *> horizontalHeaderItems;
181
182 // A cache must be mutable if get-functions should have const modifiers
183 mutable QModelIndexList cachedIndexes;
184};
185
186class QTableWidgetPrivate : public QTableViewPrivate
187{
188 Q_DECLARE_PUBLIC(QTableWidget)
189public:
190 QTableWidgetPrivate() : QTableViewPrivate() {}
191 inline QTableModel *tableModel() const { return qobject_cast<QTableModel*>(model); }
192 void setup();
193
194 // view signals
195 void _q_emitItemPressed(const QModelIndex &index);
196 void _q_emitItemClicked(const QModelIndex &index);
197 void _q_emitItemDoubleClicked(const QModelIndex &index);
198 void _q_emitItemActivated(const QModelIndex &index);
199 void _q_emitItemEntered(const QModelIndex &index);
200 // model signals
201 void _q_emitItemChanged(const QModelIndex &index);
202 // selection signals
203 void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current);
204 // sorting
205 void _q_sort();
206 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
207};
208
209class QTableWidgetItemPrivate
210{
211public:
212 QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1) {}
213 QTableWidgetItem *q;
214 int id;
215};
216
217QT_END_NAMESPACE
218
219#endif // QTABLEWIDGET_P_H
220