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