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_H |
41 | #define QTABLEWIDGET_H |
42 | |
43 | #include <QtWidgets/qtwidgetsglobal.h> |
44 | #include <QtWidgets/qtableview.h> |
45 | #include <QtCore/qvariant.h> |
46 | #include <QtCore/qvector.h> |
47 | |
48 | QT_REQUIRE_CONFIG(tablewidget); |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | class Q_WIDGETS_EXPORT QTableWidgetSelectionRange |
53 | { |
54 | public: |
55 | QTableWidgetSelectionRange(); |
56 | QTableWidgetSelectionRange(int top, int left, int bottom, int right); |
57 | QTableWidgetSelectionRange(const QTableWidgetSelectionRange &other); |
58 | ~QTableWidgetSelectionRange(); |
59 | |
60 | inline int topRow() const { return top; } |
61 | inline int bottomRow() const { return bottom; } |
62 | inline int leftColumn() const { return left; } |
63 | inline int rightColumn() const { return right; } |
64 | inline int rowCount() const { return bottom - top + 1; } |
65 | inline int columnCount() const { return right - left + 1; } |
66 | |
67 | private: |
68 | int top, left, bottom, right; |
69 | }; |
70 | |
71 | class QTableWidget; |
72 | class QTableModel; |
73 | class QWidgetItemData; |
74 | class QTableWidgetItemPrivate; |
75 | |
76 | class Q_WIDGETS_EXPORT QTableWidgetItem |
77 | { |
78 | friend class QTableWidget; |
79 | friend class QTableModel; |
80 | public: |
81 | enum ItemType { Type = 0, UserType = 1000 }; |
82 | explicit QTableWidgetItem(int type = Type); |
83 | explicit QTableWidgetItem(const QString &text, int type = Type); |
84 | explicit QTableWidgetItem(const QIcon &icon, const QString &text, int type = Type); |
85 | QTableWidgetItem(const QTableWidgetItem &other); |
86 | virtual ~QTableWidgetItem(); |
87 | |
88 | virtual QTableWidgetItem *clone() const; |
89 | |
90 | inline QTableWidget *tableWidget() const { return view; } |
91 | |
92 | inline int row() const; |
93 | inline int column() const; |
94 | |
95 | void setSelected(bool select); |
96 | bool isSelected() const; |
97 | |
98 | inline Qt::ItemFlags flags() const { return itemFlags; } |
99 | void setFlags(Qt::ItemFlags flags); |
100 | |
101 | inline QString text() const |
102 | { return data(Qt::DisplayRole).toString(); } |
103 | inline void setText(const QString &text); |
104 | |
105 | inline QIcon icon() const |
106 | { return qvariant_cast<QIcon>(data(Qt::DecorationRole)); } |
107 | inline void setIcon(const QIcon &icon); |
108 | |
109 | inline QString statusTip() const |
110 | { return data(Qt::StatusTipRole).toString(); } |
111 | inline void setStatusTip(const QString &statusTip); |
112 | |
113 | #ifndef QT_NO_TOOLTIP |
114 | inline QString toolTip() const |
115 | { return data(Qt::ToolTipRole).toString(); } |
116 | inline void setToolTip(const QString &toolTip); |
117 | #endif |
118 | |
119 | #if QT_CONFIG(whatsthis) |
120 | inline QString whatsThis() const |
121 | { return data(Qt::WhatsThisRole).toString(); } |
122 | inline void setWhatsThis(const QString &whatsThis); |
123 | #endif |
124 | |
125 | inline QFont font() const |
126 | { return qvariant_cast<QFont>(data(Qt::FontRole)); } |
127 | inline void setFont(const QFont &font); |
128 | |
129 | inline int textAlignment() const |
130 | { return data(Qt::TextAlignmentRole).toInt(); } |
131 | inline void setTextAlignment(int alignment) |
132 | { setData(Qt::TextAlignmentRole, alignment); } |
133 | |
134 | #if QT_DEPRECATED_SINCE(5, 13) |
135 | QT_DEPRECATED_X ("Use QTableWidgetItem::background() instead" ) |
136 | inline QColor backgroundColor() const |
137 | { return qvariant_cast<QColor>(data(Qt::BackgroundRole)); } |
138 | QT_DEPRECATED_X ("Use QTableWidgetItem::setBackground() instead" ) |
139 | inline void setBackgroundColor(const QColor &color) |
140 | { setData(Qt::BackgroundRole, color); } |
141 | #endif |
142 | |
143 | inline QBrush background() const |
144 | { return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); } |
145 | inline void setBackground(const QBrush &brush) |
146 | { setData(Qt::BackgroundRole, brush); } |
147 | |
148 | #if QT_DEPRECATED_SINCE(5, 13) |
149 | QT_DEPRECATED_X ("Use QTableWidgetItem::foreground() instead" ) |
150 | inline QColor textColor() const |
151 | { return qvariant_cast<QColor>(data(Qt::ForegroundRole)); } |
152 | QT_DEPRECATED_X ("Use QTableWidgetItem::setForeground() instead" ) |
153 | inline void setTextColor(const QColor &color) |
154 | { setData(Qt::ForegroundRole, color); } |
155 | #endif |
156 | |
157 | inline QBrush foreground() const |
158 | { return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); } |
159 | inline void setForeground(const QBrush &brush) |
160 | { setData(Qt::ForegroundRole, brush); } |
161 | |
162 | inline Qt::CheckState checkState() const |
163 | { return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); } |
164 | inline void setCheckState(Qt::CheckState state) |
165 | { setData(Qt::CheckStateRole, state); } |
166 | |
167 | inline QSize sizeHint() const |
168 | { return qvariant_cast<QSize>(data(Qt::SizeHintRole)); } |
169 | inline void setSizeHint(const QSize &size) |
170 | { setData(Qt::SizeHintRole, size); } |
171 | |
172 | virtual QVariant data(int role) const; |
173 | virtual void setData(int role, const QVariant &value); |
174 | |
175 | virtual bool operator<(const QTableWidgetItem &other) const; |
176 | |
177 | #ifndef QT_NO_DATASTREAM |
178 | virtual void read(QDataStream &in); |
179 | virtual void write(QDataStream &out) const; |
180 | #endif |
181 | QTableWidgetItem &operator=(const QTableWidgetItem &other); |
182 | |
183 | inline int type() const { return rtti; } |
184 | |
185 | private: |
186 | QTableModel *tableModel() const; |
187 | |
188 | private: |
189 | int rtti; |
190 | QVector<QWidgetItemData> values; |
191 | QTableWidget *view; |
192 | QTableWidgetItemPrivate *d; |
193 | Qt::ItemFlags itemFlags; |
194 | }; |
195 | |
196 | inline void QTableWidgetItem::setText(const QString &atext) |
197 | { setData(Qt::DisplayRole, atext); } |
198 | |
199 | inline void QTableWidgetItem::setIcon(const QIcon &aicon) |
200 | { setData(Qt::DecorationRole, aicon); } |
201 | |
202 | inline void QTableWidgetItem::setStatusTip(const QString &astatusTip) |
203 | { setData(Qt::StatusTipRole, astatusTip); } |
204 | |
205 | #ifndef QT_NO_TOOLTIP |
206 | inline void QTableWidgetItem::setToolTip(const QString &atoolTip) |
207 | { setData(Qt::ToolTipRole, atoolTip); } |
208 | #endif |
209 | |
210 | #if QT_CONFIG(whatsthis) |
211 | inline void QTableWidgetItem::setWhatsThis(const QString &awhatsThis) |
212 | { setData(Qt::WhatsThisRole, awhatsThis); } |
213 | #endif |
214 | |
215 | inline void QTableWidgetItem::setFont(const QFont &afont) |
216 | { setData(Qt::FontRole, afont); } |
217 | |
218 | #ifndef QT_NO_DATASTREAM |
219 | Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item); |
220 | Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item); |
221 | #endif |
222 | |
223 | class QTableWidgetPrivate; |
224 | |
225 | class Q_WIDGETS_EXPORT QTableWidget : public QTableView |
226 | { |
227 | Q_OBJECT |
228 | Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount) |
229 | Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount) |
230 | |
231 | friend class QTableModel; |
232 | public: |
233 | explicit QTableWidget(QWidget *parent = nullptr); |
234 | QTableWidget(int rows, int columns, QWidget *parent = nullptr); |
235 | ~QTableWidget(); |
236 | |
237 | void setRowCount(int rows); |
238 | int rowCount() const; |
239 | |
240 | void setColumnCount(int columns); |
241 | int columnCount() const; |
242 | |
243 | int row(const QTableWidgetItem *item) const; |
244 | int column(const QTableWidgetItem *item) const; |
245 | |
246 | QTableWidgetItem *item(int row, int column) const; |
247 | void setItem(int row, int column, QTableWidgetItem *item); |
248 | QTableWidgetItem *takeItem(int row, int column); |
249 | |
250 | QTableWidgetItem *(int row) const; |
251 | void (int row, QTableWidgetItem *item); |
252 | QTableWidgetItem *(int row); |
253 | |
254 | QTableWidgetItem *(int column) const; |
255 | void (int column, QTableWidgetItem *item); |
256 | QTableWidgetItem *(int column); |
257 | void (const QStringList &labels); |
258 | void (const QStringList &labels); |
259 | |
260 | int currentRow() const; |
261 | int currentColumn() const; |
262 | QTableWidgetItem *currentItem() const; |
263 | void setCurrentItem(QTableWidgetItem *item); |
264 | void setCurrentItem(QTableWidgetItem *item, QItemSelectionModel::SelectionFlags command); |
265 | void setCurrentCell(int row, int column); |
266 | void setCurrentCell(int row, int column, QItemSelectionModel::SelectionFlags command); |
267 | |
268 | void sortItems(int column, Qt::SortOrder order = Qt::AscendingOrder); |
269 | void setSortingEnabled(bool enable); |
270 | bool isSortingEnabled() const; |
271 | |
272 | void editItem(QTableWidgetItem *item); |
273 | void openPersistentEditor(QTableWidgetItem *item); |
274 | void closePersistentEditor(QTableWidgetItem *item); |
275 | using QAbstractItemView::isPersistentEditorOpen; |
276 | bool isPersistentEditorOpen(QTableWidgetItem *item) const; |
277 | |
278 | QWidget *cellWidget(int row, int column) const; |
279 | void setCellWidget(int row, int column, QWidget *widget); |
280 | inline void removeCellWidget(int row, int column); |
281 | |
282 | #if QT_DEPRECATED_SINCE(5, 13) |
283 | QT_DEPRECATED_X ("Use QTableWidgetItem::isSelected() instead" ) |
284 | bool isItemSelected(const QTableWidgetItem *item) const; |
285 | QT_DEPRECATED_X ("Use QTableWidgetItem::setSelected() instead" ) |
286 | void setItemSelected(const QTableWidgetItem *item, bool select); |
287 | #endif |
288 | void setRangeSelected(const QTableWidgetSelectionRange &range, bool select); |
289 | |
290 | QList<QTableWidgetSelectionRange> selectedRanges() const; |
291 | QList<QTableWidgetItem*> selectedItems() const; |
292 | QList<QTableWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags) const; |
293 | |
294 | int visualRow(int logicalRow) const; |
295 | int visualColumn(int logicalColumn) const; |
296 | |
297 | QTableWidgetItem *itemAt(const QPoint &p) const; |
298 | inline QTableWidgetItem *itemAt(int x, int y) const; |
299 | QRect visualItemRect(const QTableWidgetItem *item) const; |
300 | |
301 | const QTableWidgetItem *itemPrototype() const; |
302 | void setItemPrototype(const QTableWidgetItem *item); |
303 | |
304 | public Q_SLOTS: |
305 | void scrollToItem(const QTableWidgetItem *item, QAbstractItemView::ScrollHint hint = EnsureVisible); |
306 | void insertRow(int row); |
307 | void insertColumn(int column); |
308 | void removeRow(int row); |
309 | void removeColumn(int column); |
310 | void clear(); |
311 | void clearContents(); |
312 | |
313 | Q_SIGNALS: |
314 | void itemPressed(QTableWidgetItem *item); |
315 | void itemClicked(QTableWidgetItem *item); |
316 | void itemDoubleClicked(QTableWidgetItem *item); |
317 | |
318 | void itemActivated(QTableWidgetItem *item); |
319 | void itemEntered(QTableWidgetItem *item); |
320 | // ### Qt 6: add changed roles |
321 | void itemChanged(QTableWidgetItem *item); |
322 | |
323 | void currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous); |
324 | void itemSelectionChanged(); |
325 | |
326 | void cellPressed(int row, int column); |
327 | void cellClicked(int row, int column); |
328 | void cellDoubleClicked(int row, int column); |
329 | |
330 | void cellActivated(int row, int column); |
331 | void cellEntered(int row, int column); |
332 | void cellChanged(int row, int column); |
333 | |
334 | void currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn); |
335 | |
336 | protected: |
337 | bool event(QEvent *e) override; |
338 | virtual QStringList mimeTypes() const; |
339 | #if QT_VERSION >= QT_VERSION_CHECK(6,0,0) |
340 | virtual QMimeData *mimeData(const QList<QTableWidgetItem *> &items) const; |
341 | #else |
342 | virtual QMimeData *mimeData(const QList<QTableWidgetItem*> items) const; |
343 | #endif |
344 | virtual bool dropMimeData(int row, int column, const QMimeData *data, Qt::DropAction action); |
345 | virtual Qt::DropActions supportedDropActions() const; |
346 | |
347 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) |
348 | public: |
349 | #else |
350 | protected: |
351 | #endif |
352 | QList<QTableWidgetItem*> items(const QMimeData *data) const; |
353 | |
354 | QModelIndex indexFromItem(const QTableWidgetItem *item) const; |
355 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
356 | QModelIndex indexFromItem(QTableWidgetItem *item) const; // ### Qt 6: remove |
357 | #endif |
358 | QTableWidgetItem *itemFromIndex(const QModelIndex &index) const; |
359 | |
360 | protected: |
361 | #if QT_CONFIG(draganddrop) |
362 | void dropEvent(QDropEvent *event) override; |
363 | #endif |
364 | private: |
365 | void setModel(QAbstractItemModel *model) override; |
366 | |
367 | Q_DECLARE_PRIVATE(QTableWidget) |
368 | Q_DISABLE_COPY(QTableWidget) |
369 | |
370 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed(const QModelIndex &index)) |
371 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked(const QModelIndex &index)) |
372 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked(const QModelIndex &index)) |
373 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated(const QModelIndex &index)) |
374 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered(const QModelIndex &index)) |
375 | Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged(const QModelIndex &index)) |
376 | Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex ¤t)) |
377 | Q_PRIVATE_SLOT(d_func(), void _q_sort()) |
378 | Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)) |
379 | }; |
380 | |
381 | inline void QTableWidget::removeCellWidget(int arow, int acolumn) |
382 | { setCellWidget(arow, acolumn, nullptr); } |
383 | |
384 | inline QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const |
385 | { return itemAt(QPoint(ax, ay)); } |
386 | |
387 | inline int QTableWidgetItem::row() const |
388 | { return (view ? view->row(this) : -1); } |
389 | |
390 | inline int QTableWidgetItem::column() const |
391 | { return (view ? view->column(this) : -1); } |
392 | |
393 | QT_END_NAMESPACE |
394 | |
395 | #endif // QTABLEWIDGET_H |
396 | |