Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:09:40

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QTREEWIDGET_H
0005 #define QTREEWIDGET_H
0006 
0007 #include <QtWidgets/qtreeview.h>
0008 #include <QtWidgets/qtreewidgetitemiterator.h>
0009 #include <QtWidgets/qtwidgetsglobal.h>
0010 #include <QtCore/qlist.h>
0011 #include <QtCore/qvariant.h>
0012 
0013 QT_REQUIRE_CONFIG(treewidget);
0014 
0015 QT_BEGIN_NAMESPACE
0016 
0017 class QTreeWidget;
0018 class QTreeModel;
0019 class QWidgetItemData;
0020 class QTreeWidgetItemPrivate;
0021 
0022 class Q_WIDGETS_EXPORT QTreeWidgetItem
0023 {
0024     friend class QTreeModel;
0025     friend class QTreeWidget;
0026     friend class QTreeWidgetPrivate;
0027     friend class QTreeWidgetItemIterator;
0028     friend class QTreeWidgetItemPrivate;
0029 public:
0030     enum ItemType { Type = 0, UserType = 1000 };
0031     explicit QTreeWidgetItem(int type = Type);
0032     explicit QTreeWidgetItem(const QStringList &strings, int type = Type);
0033     explicit QTreeWidgetItem(QTreeWidget *treeview, int type = Type);
0034     QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type = Type);
0035     QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after, int type = Type);
0036     explicit QTreeWidgetItem(QTreeWidgetItem *parent, int type = Type);
0037     QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type = Type);
0038     QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type = Type);
0039     QTreeWidgetItem(const QTreeWidgetItem &other);
0040     virtual ~QTreeWidgetItem();
0041 
0042     virtual QTreeWidgetItem *clone() const;
0043 
0044     inline QTreeWidget *treeWidget() const { return view; }
0045 
0046     void setSelected(bool select);
0047     bool isSelected() const;
0048 
0049     void setHidden(bool hide);
0050     bool isHidden() const;
0051 
0052     void setExpanded(bool expand);
0053     bool isExpanded() const;
0054 
0055     void setFirstColumnSpanned(bool span);
0056     bool isFirstColumnSpanned() const;
0057 
0058     inline void setDisabled(bool disabled);
0059     inline bool isDisabled() const;
0060 
0061     enum ChildIndicatorPolicy { ShowIndicator, DontShowIndicator, DontShowIndicatorWhenChildless };
0062     void setChildIndicatorPolicy(QTreeWidgetItem::ChildIndicatorPolicy policy);
0063     QTreeWidgetItem::ChildIndicatorPolicy childIndicatorPolicy() const;
0064 
0065     Qt::ItemFlags flags() const;
0066     void setFlags(Qt::ItemFlags flags);
0067 
0068     inline QString text(int column) const
0069         { return data(column, Qt::DisplayRole).toString(); }
0070     inline void setText(int column, const QString &text);
0071 
0072     inline QIcon icon(int column) const
0073         { return qvariant_cast<QIcon>(data(column, Qt::DecorationRole)); }
0074     inline void setIcon(int column, const QIcon &icon);
0075 
0076     inline QString statusTip(int column) const
0077         { return data(column, Qt::StatusTipRole).toString(); }
0078     inline void setStatusTip(int column, const QString &statusTip);
0079 
0080 #if QT_CONFIG(tooltip)
0081     inline QString toolTip(int column) const
0082         { return data(column, Qt::ToolTipRole).toString(); }
0083     inline void setToolTip(int column, const QString &toolTip);
0084 #endif
0085 
0086 #if QT_CONFIG(whatsthis)
0087     inline QString whatsThis(int column) const
0088         { return data(column, Qt::WhatsThisRole).toString(); }
0089     inline void setWhatsThis(int column, const QString &whatsThis);
0090 #endif
0091 
0092     inline QFont font(int column) const
0093         { return qvariant_cast<QFont>(data(column, Qt::FontRole)); }
0094     inline void setFont(int column, const QFont &font);
0095 
0096 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
0097     inline int textAlignment(int column) const
0098         { return data(column, Qt::TextAlignmentRole).toInt(); }
0099 #else
0100     inline Qt::Alignment textAlignment(int column) const
0101     { return qvariant_cast<Qt::Alignment>(data(column, Qt::TextAlignmentRole)); }
0102 #endif
0103 #if QT_DEPRECATED_SINCE(6, 4)
0104     QT_DEPRECATED_VERSION_X_6_4("Use the overload taking Qt::Alignment")
0105     inline void setTextAlignment(int column, int alignment)
0106         { setData(column, Qt::TextAlignmentRole, alignment); }
0107     inline void setTextAlignment(int column, Qt::AlignmentFlag alignment)
0108         { setData(column, Qt::TextAlignmentRole, QVariant::fromValue(Qt::Alignment(alignment))); }
0109 #endif
0110     inline void setTextAlignment(int column, Qt::Alignment alignment)
0111         { setData(column, Qt::TextAlignmentRole, QVariant::fromValue(alignment)); }
0112 
0113     inline QBrush background(int column) const
0114         { return qvariant_cast<QBrush>(data(column, Qt::BackgroundRole)); }
0115     inline void setBackground(int column, const QBrush &brush)
0116         { setData(column, Qt::BackgroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
0117 
0118     inline QBrush foreground(int column) const
0119         { return qvariant_cast<QBrush>(data(column, Qt::ForegroundRole)); }
0120     inline void setForeground(int column, const QBrush &brush)
0121         { setData(column, Qt::ForegroundRole, brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }
0122 
0123     inline Qt::CheckState checkState(int column) const
0124         { return qvariant_cast<Qt::CheckState>(data(column, Qt::CheckStateRole)); }
0125     inline void setCheckState(int column, Qt::CheckState state)
0126         { setData(column, Qt::CheckStateRole, state); }
0127 
0128     inline QSize sizeHint(int column) const
0129         { return qvariant_cast<QSize>(data(column, Qt::SizeHintRole)); }
0130     inline void setSizeHint(int column, const QSize &size)
0131         { setData(column, Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); }
0132 
0133     virtual QVariant data(int column, int role) const;
0134     virtual void setData(int column, int role, const QVariant &value);
0135 
0136     virtual bool operator<(const QTreeWidgetItem &other) const;
0137 
0138 #ifndef QT_NO_DATASTREAM
0139     virtual void read(QDataStream &in);
0140     virtual void write(QDataStream &out) const;
0141 #endif
0142     QTreeWidgetItem &operator=(const QTreeWidgetItem &other);
0143 
0144     inline QTreeWidgetItem *parent() const { return par; }
0145     inline QTreeWidgetItem *child(int index) const {
0146         if (index < 0 || index >= children.size())
0147             return nullptr;
0148         executePendingSort();
0149         return children.at(index);
0150     }
0151     inline int childCount() const { return int(children.size()); }
0152     inline int columnCount() const { return int(values.size()); }
0153     inline int indexOfChild(QTreeWidgetItem *child) const;
0154 
0155     void addChild(QTreeWidgetItem *child);
0156     void insertChild(int index, QTreeWidgetItem *child);
0157     void removeChild(QTreeWidgetItem *child);
0158     QTreeWidgetItem *takeChild(int index);
0159 
0160     void addChildren(const QList<QTreeWidgetItem*> &children);
0161     void insertChildren(int index, const QList<QTreeWidgetItem*> &children);
0162     QList<QTreeWidgetItem*> takeChildren();
0163 
0164     inline int type() const { return rtti; }
0165     inline void sortChildren(int column, Qt::SortOrder order)
0166         { sortChildren(column, order, false); }
0167 
0168 protected:
0169     void emitDataChanged();
0170 
0171 private:
0172     void sortChildren(int column, Qt::SortOrder order, bool climb);
0173     QVariant childrenCheckState(int column) const;
0174     void itemChanged();
0175     void executePendingSort() const;
0176     QTreeModel *treeModel(QTreeWidget *v = nullptr) const;
0177 
0178     int rtti;
0179     // One item has a vector of column entries. Each column has a vector of (role, value) pairs.
0180     QList<QList<QWidgetItemData>> values;
0181     QTreeWidget *view = nullptr;
0182     QTreeWidgetItemPrivate *d;
0183     QTreeWidgetItem *par = nullptr;
0184     // ### Qt7: Move children to d-pointer and replace QList by a suitable data structure.
0185     //          to fix QTBUG-94546
0186     QList<QTreeWidgetItem*> children;
0187     Qt::ItemFlags itemFlags = Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled
0188             | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
0189 };
0190 
0191 inline void QTreeWidgetItem::setText(int column, const QString &atext)
0192 { setData(column, Qt::DisplayRole, atext); }
0193 
0194 inline void QTreeWidgetItem::setIcon(int column, const QIcon &aicon)
0195 { setData(column, Qt::DecorationRole, aicon); }
0196 
0197 #if QT_CONFIG(statustip)
0198 inline void QTreeWidgetItem::setStatusTip(int column, const QString &astatusTip)
0199 { setData(column, Qt::StatusTipRole, astatusTip); }
0200 #endif
0201 
0202 #if QT_CONFIG(tooltip)
0203 inline void QTreeWidgetItem::setToolTip(int column, const QString &atoolTip)
0204 { setData(column, Qt::ToolTipRole, atoolTip); }
0205 #endif
0206 
0207 #if QT_CONFIG(whatsthis)
0208 inline void QTreeWidgetItem::setWhatsThis(int column, const QString &awhatsThis)
0209 { setData(column, Qt::WhatsThisRole, awhatsThis); }
0210 #endif
0211 
0212 inline void QTreeWidgetItem::setFont(int column, const QFont &afont)
0213 { setData(column, Qt::FontRole, afont); }
0214 
0215 inline int QTreeWidgetItem::indexOfChild(QTreeWidgetItem *achild) const
0216 { executePendingSort(); return int(children.indexOf(achild)); }
0217 
0218 #ifndef QT_NO_DATASTREAM
0219 Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &out, const QTreeWidgetItem &item);
0220 Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &in, QTreeWidgetItem &item);
0221 #endif
0222 
0223 class QTreeWidgetPrivate;
0224 
0225 class Q_WIDGETS_EXPORT QTreeWidget : public QTreeView
0226 {
0227     Q_OBJECT
0228     Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)
0229     Q_PROPERTY(int topLevelItemCount READ topLevelItemCount)
0230 
0231     friend class QTreeModel;
0232     friend class QTreeWidgetItem;
0233 public:
0234     explicit QTreeWidget(QWidget *parent = nullptr);
0235     ~QTreeWidget();
0236 
0237     int columnCount() const;
0238     void setColumnCount(int columns);
0239 
0240     QTreeWidgetItem *invisibleRootItem() const;
0241     QTreeWidgetItem *topLevelItem(int index) const;
0242     int topLevelItemCount() const;
0243     void insertTopLevelItem(int index, QTreeWidgetItem *item);
0244     void addTopLevelItem(QTreeWidgetItem *item);
0245     QTreeWidgetItem *takeTopLevelItem(int index);
0246     int indexOfTopLevelItem(QTreeWidgetItem *item) const;
0247 
0248     void insertTopLevelItems(int index, const QList<QTreeWidgetItem*> &items);
0249     void addTopLevelItems(const QList<QTreeWidgetItem*> &items);
0250 
0251     QTreeWidgetItem *headerItem() const;
0252     void setHeaderItem(QTreeWidgetItem *item);
0253     void setHeaderLabels(const QStringList &labels);
0254     inline void setHeaderLabel(const QString &label);
0255 
0256     QTreeWidgetItem *currentItem() const;
0257     int currentColumn() const;
0258     void setCurrentItem(QTreeWidgetItem *item);
0259     void setCurrentItem(QTreeWidgetItem *item, int column);
0260     void setCurrentItem(QTreeWidgetItem *item, int column, QItemSelectionModel::SelectionFlags command);
0261 
0262     QTreeWidgetItem *itemAt(const QPoint &p) const;
0263     inline QTreeWidgetItem *itemAt(int x, int y) const;
0264     QRect visualItemRect(const QTreeWidgetItem *item) const;
0265 
0266     int sortColumn() const;
0267     void sortItems(int column, Qt::SortOrder order);
0268 
0269     void editItem(QTreeWidgetItem *item, int column = 0);
0270     void openPersistentEditor(QTreeWidgetItem *item, int column = 0);
0271     void closePersistentEditor(QTreeWidgetItem *item, int column = 0);
0272     using QAbstractItemView::isPersistentEditorOpen;
0273     bool isPersistentEditorOpen(QTreeWidgetItem *item, int column = 0) const;
0274 
0275     QWidget *itemWidget(QTreeWidgetItem *item, int column) const;
0276     void setItemWidget(QTreeWidgetItem *item, int column, QWidget *widget);
0277     inline void removeItemWidget(QTreeWidgetItem *item, int column);
0278 
0279     QList<QTreeWidgetItem*> selectedItems() const;
0280     QList<QTreeWidgetItem*> findItems(const QString &text, Qt::MatchFlags flags,
0281                                       int column = 0) const;
0282 
0283     QTreeWidgetItem *itemAbove(const QTreeWidgetItem *item) const;
0284     QTreeWidgetItem *itemBelow(const QTreeWidgetItem *item) const;
0285 
0286     QModelIndex indexFromItem(const QTreeWidgetItem *item, int column = 0) const;
0287     QTreeWidgetItem *itemFromIndex(const QModelIndex &index) const;
0288 
0289     void setSelectionModel(QItemSelectionModel *selectionModel) override;
0290 
0291 public Q_SLOTS:
0292     void scrollToItem(const QTreeWidgetItem *item,
0293                       QAbstractItemView::ScrollHint hint = EnsureVisible);
0294     void expandItem(const QTreeWidgetItem *item);
0295     void collapseItem(const QTreeWidgetItem *item);
0296     void clear();
0297 
0298 Q_SIGNALS:
0299     void itemPressed(QTreeWidgetItem *item, int column);
0300     void itemClicked(QTreeWidgetItem *item, int column);
0301     void itemDoubleClicked(QTreeWidgetItem *item, int column);
0302     void itemActivated(QTreeWidgetItem *item, int column);
0303     void itemEntered(QTreeWidgetItem *item, int column);
0304     void itemChanged(QTreeWidgetItem *item, int column);
0305     void itemExpanded(QTreeWidgetItem *item);
0306     void itemCollapsed(QTreeWidgetItem *item);
0307     void currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
0308     void itemSelectionChanged();
0309 
0310 protected:
0311     bool event(QEvent *e) override;
0312     virtual QStringList mimeTypes() const;
0313     virtual QMimeData *mimeData(const QList<QTreeWidgetItem *> &items) const;
0314     virtual bool dropMimeData(QTreeWidgetItem *parent, int index,
0315                               const QMimeData *data, Qt::DropAction action);
0316     virtual Qt::DropActions supportedDropActions() const;
0317 
0318 #if QT_CONFIG(draganddrop)
0319     void dropEvent(QDropEvent *event) override;
0320 #endif
0321 private:
0322     void setModel(QAbstractItemModel *model) override;
0323 
0324     Q_DECLARE_PRIVATE(QTreeWidget)
0325     Q_DISABLE_COPY(QTreeWidget)
0326 };
0327 
0328 inline void QTreeWidget::removeItemWidget(QTreeWidgetItem *item, int column)
0329 { setItemWidget(item, column, nullptr); }
0330 
0331 inline QTreeWidgetItem *QTreeWidget::itemAt(int ax, int ay) const
0332 { return itemAt(QPoint(ax, ay)); }
0333 
0334 inline void QTreeWidget::setHeaderLabel(const QString &alabel)
0335 { setHeaderLabels(QStringList(alabel)); }
0336 
0337 inline void QTreeWidgetItem::setDisabled(bool disabled)
0338 { setFlags(disabled ? (flags() & ~Qt::ItemIsEnabled) : flags() | Qt::ItemIsEnabled); }
0339 
0340 inline bool QTreeWidgetItem::isDisabled() const
0341 { return !(flags() & Qt::ItemIsEnabled); }
0342 
0343 QT_END_NAMESPACE
0344 
0345 #endif // QTREEWIDGET_H