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
25QT_REQUIRE_CONFIG(tablewidget);
26
27QT_BEGIN_NAMESPACE
28
29class QTableWidgetMimeData : public QMimeData
30{
31 Q_OBJECT
32public:
33 QList<QTableWidgetItem*> items;
34};
35
36class QTableModelLessThan
37{
38public:
39 inline bool operator()(QTableWidgetItem *i1, QTableWidgetItem *i2) const
40 { return (*i1 < *i2); }
41};
42
43class QTableModelGreaterThan
44{
45public:
46 inline bool operator()(QTableWidgetItem *i1, QTableWidgetItem *i2) const
47 { return (*i2 < *i1); }
48};
49
50class QTableModel : public QAbstractTableModel
51{
52 Q_OBJECT
53 friend class QTableWidget;
54
55public:
56 QTableModel(int rows, int columns, QTableWidget *parent);
57 ~QTableModel();
58
59 bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
60 bool insertColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex()) override;
61
62 bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()) override;
63 bool removeColumns(int column, int count = 1, const QModelIndex &parent = QModelIndex()) override;
64
65 void setItem(int row, int column, QTableWidgetItem *item);
66 QTableWidgetItem *takeItem(int row, int column);
67 QTableWidgetItem *item(int row, int column) const;
68 QTableWidgetItem *item(const QModelIndex &index) const;
69 void removeItem(QTableWidgetItem *item);
70
71 void setHorizontalHeaderItem(int section, QTableWidgetItem *item);
72 void setVerticalHeaderItem(int section, QTableWidgetItem *item);
73 QTableWidgetItem *takeHorizontalHeaderItem(int section);
74 QTableWidgetItem *takeVerticalHeaderItem(int section);
75 QTableWidgetItem *horizontalHeaderItem(int section);
76 QTableWidgetItem *verticalHeaderItem(int section);
77
78 QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
79 { return QAbstractTableModel::index(row, column, parent); }
80
81 QModelIndex index(const QTableWidgetItem *item) const;
82
83 void setRowCount(int rows);
84 void setColumnCount(int columns);
85
86 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
87 int columnCount(const QModelIndex &parent = QModelIndex()) const override;
88
89 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
90 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
91 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles) override;
92 bool clearItemData(const QModelIndex &index) override;
93
94 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
95
96 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
97 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role) override;
98
99 Qt::ItemFlags flags(const QModelIndex &index) const override;
100
101 void sort(int column, Qt::SortOrder order) override;
102 static bool itemLessThan(const QPair<QTableWidgetItem*,int> &left,
103 const QPair<QTableWidgetItem*,int> &right);
104 static bool itemGreaterThan(const QPair<QTableWidgetItem*,int> &left,
105 const QPair<QTableWidgetItem*,int> &right);
106
107 void ensureSorted(int column, Qt::SortOrder order, int start, int end);
108 QList<QTableWidgetItem *> columnItems(int column) const;
109 void updateRowIndexes(QModelIndexList &indexes, int movedFromRow, int movedToRow);
110 static QList<QTableWidgetItem *>::iterator
111 sortedInsertionIterator(const QList<QTableWidgetItem *>::iterator &begin,
112 const QList<QTableWidgetItem *>::iterator &end, Qt::SortOrder order,
113 QTableWidgetItem *item);
114
115 bool isValid(const QModelIndex &index) const;
116 inline long tableIndex(int row, int column) const
117 { return (row * horizontalHeaderItems.size()) + column; }
118
119 void clear();
120 void clearContents();
121 void itemChanged(QTableWidgetItem *item, const QList<int> &roles = QList<int>());
122
123 QTableWidgetItem *createItem() const;
124 const QTableWidgetItem *itemPrototype() const;
125 void setItemPrototype(const QTableWidgetItem *item);
126
127 // dnd
128 QStringList mimeTypes() const override;
129 QMimeData *mimeData(const QModelIndexList &indexes) const override;
130 bool dropMimeData(const QMimeData *data, Qt::DropAction action,
131 int row, int column, const QModelIndex &parent) override;
132 Qt::DropActions supportedDropActions() const override;
133
134 QMimeData *internalMimeData() const;
135
136private:
137 const QTableWidgetItem *prototype;
138 QList<QTableWidgetItem *> tableItems;
139 QList<QTableWidgetItem *> verticalHeaderItems;
140 QList<QTableWidgetItem *> horizontalHeaderItems;
141
142 // A cache must be mutable if get-functions should have const modifiers
143 mutable QModelIndexList cachedIndexes;
144};
145
146class QTableWidgetPrivate : public QTableViewPrivate
147{
148 Q_DECLARE_PUBLIC(QTableWidget)
149public:
150 QTableWidgetPrivate() : QTableViewPrivate() {}
151 inline QTableModel *tableModel() const { return qobject_cast<QTableModel*>(object: model); }
152 void setup();
153
154 // view signals
155 void _q_emitItemPressed(const QModelIndex &index);
156 void _q_emitItemClicked(const QModelIndex &index);
157 void _q_emitItemDoubleClicked(const QModelIndex &index);
158 void _q_emitItemActivated(const QModelIndex &index);
159 void _q_emitItemEntered(const QModelIndex &index);
160 // model signals
161 void _q_emitItemChanged(const QModelIndex &index);
162 // selection signals
163 void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex &current);
164 // sorting
165 void _q_sort();
166 void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
167};
168
169class QTableWidgetItemPrivate
170{
171public:
172 QTableWidgetItemPrivate(QTableWidgetItem *item) : q(item), id(-1), headerItem(false) {}
173 QTableWidgetItem *q;
174 int id;
175 bool headerItem; // Qt 7 TODO: inline this stuff in the public class.
176};
177
178QT_END_NAMESPACE
179
180#endif // QTABLEWIDGET_P_H
181

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