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