diff options
Diffstat (limited to 'src/widgets/itemviews/qtreewidget.cpp')
-rw-r--r-- | src/widgets/itemviews/qtreewidget.cpp | 243 |
1 files changed, 146 insertions, 97 deletions
diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp index 4768869843..29cc199526 100644 --- a/src/widgets/itemviews/qtreewidget.cpp +++ b/src/widgets/itemviews/qtreewidget.cpp @@ -1009,8 +1009,18 @@ void QTreeModel::timerEvent(QTimerEvent *ev) Sets the selected state of the item to \a select. \sa isSelected() - */ +void QTreeWidgetItem::setSelected(bool select) +{ + const QTreeModel *model = treeModel(); + if (!model || !view->selectionModel()) + return; + const QModelIndex index = model->index(this, 0); + view->selectionModel()->select(index, (select ? QItemSelectionModel::Select + : QItemSelectionModel::Deselect) + | QItemSelectionModel::Rows); + d->selected = select; +} /*! \fn bool QTreeWidgetItem::isSelected() const @@ -1020,6 +1030,10 @@ void QTreeModel::timerEvent(QTimerEvent *ev) \sa setSelected() */ +bool QTreeWidgetItem::isSelected() const +{ + return d->selected; +} /*! \fn void QTreeWidgetItem::setHidden(bool hide) @@ -1033,12 +1047,18 @@ void QTreeModel::timerEvent(QTimerEvent *ev) \sa isHidden() */ -void QTreeWidgetItem::setHidden(bool ahide) +void QTreeWidgetItem::setHidden(bool hide) { - if (view) { - view->setItemHidden(this, ahide); - d->hidden = ahide; + const QTreeModel *model = treeModel(); + if (!model) + return; + if (this == model->headerItem) { + view->header()->setHidden(hide); + } else { + const QModelIndex index = view->d_func()->index(this); + view->setRowHidden(index.row(), index.parent(), hide); } + d->hidden = hide; } /*! @@ -1052,7 +1072,15 @@ void QTreeWidgetItem::setHidden(bool ahide) bool QTreeWidgetItem::isHidden() const { - return (view ? d->hidden : false); + const QTreeModel *model = treeModel(); + if (!model) + return false; + if (this == model->headerItem) + return view->header()->isHidden(); + if (view->d_func()->hiddenIndexes.isEmpty()) + return false; + QTreeModel::SkipSorting skipSorting(model); + return view->d_func()->isRowHidden(view->d_func()->index(this)); } /*! @@ -1064,6 +1092,14 @@ bool QTreeWidgetItem::isHidden() const \sa isExpanded() */ +void QTreeWidgetItem::setExpanded(bool expand) +{ + const QTreeModel *model = treeModel(); + if (!model) + return; + QTreeModel::SkipSorting skipSorting(model); + view->setExpanded(view->d_func()->index(this), expand); +} /*! \fn bool QTreeWidgetItem::isExpanded() const @@ -1073,6 +1109,14 @@ bool QTreeWidgetItem::isHidden() const \sa setExpanded() */ +bool QTreeWidgetItem::isExpanded() const +{ + const QTreeModel *model = treeModel(); + if (!model) + return false; + QTreeModel::SkipSorting skipSorting(model); + return view->isExpanded(view->d_func()->index(this)); +} /*! \fn void QTreeWidgetItem::setFirstColumnSpanned(bool span) @@ -1083,6 +1127,14 @@ bool QTreeWidgetItem::isHidden() const \sa isFirstColumnSpanned() */ +void QTreeWidgetItem::setFirstColumnSpanned(bool span) +{ + const QTreeModel *model = treeModel(); + if (!model || this == model->headerItem) + return; // We can't set the header items to spanning + const QModelIndex index = model->index(this, 0); + view->setFirstColumnSpanned(index.row(), index.parent(), span); +} /*! \fn bool QTreeWidgetItem::isFirstColumnSpanned() const @@ -1092,6 +1144,14 @@ bool QTreeWidgetItem::isHidden() const \sa setFirstColumnSpanned() */ +bool QTreeWidgetItem::isFirstColumnSpanned() const +{ + const QTreeModel *model = treeModel(); + if (!model || this == model->headerItem) + return false; + const QModelIndex index = model->index(this, 0); + return view->isFirstColumnSpanned(index.row(), index.parent()); +} /*! \fn QString QTreeWidgetItem::text(int column) const @@ -1346,7 +1406,7 @@ bool QTreeWidgetItem::isHidden() const \sa type() */ QTreeWidgetItem::QTreeWidgetItem(int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled @@ -1365,7 +1425,7 @@ QTreeWidgetItem::QTreeWidgetItem(int type) \sa type() */ QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled @@ -1385,16 +1445,16 @@ QTreeWidgetItem::QTreeWidgetItem(const QStringList &strings, int type) \sa type() */ -QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), +QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, int type) + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled) { - if (view && view->model()) { - QTreeModel *model = qobject_cast<QTreeModel*>(view->model()); + // do not set this->view here otherwise insertChild() will fail + if (QTreeModel *model = treeModel(treeview)) { model->rootItem->addChild(this); values.reserve(model->headerItem->columnCount()); } @@ -1410,8 +1470,8 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, int type) \sa type() */ -QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), +QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, const QStringList &strings, int type) + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled @@ -1420,8 +1480,8 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, { for (int i = 0; i < strings.count(); ++i) setText(i, strings.at(i)); - if (view && view->model()) { - QTreeModel *model = qobject_cast<QTreeModel*>(view->model()); + // do not set this->view here otherwise insertChild() will fail + if (QTreeModel *model = treeModel(treeview)) { model->rootItem->addChild(this); values.reserve(model->headerItem->columnCount()); } @@ -1435,21 +1495,19 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, const QStringList &strings, \sa type() */ -QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), +QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *treeview, QTreeWidgetItem *after, int type) + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled |Qt::ItemIsDropEnabled) { - if (view) { - QTreeModel *model = qobject_cast<QTreeModel*>(view->model()); - if (model) { - int i = model->rootItem->children.indexOf(after) + 1; - model->rootItem->insertChild(i, this); - values.reserve(model->headerItem->columnCount()); - } + // do not set this->view here otherwise insertChild() will fail + if (QTreeModel *model = treeModel(treeview)) { + int i = model->rootItem->children.indexOf(after) + 1; + model->rootItem->insertChild(i, this); + values.reserve(model->headerItem->columnCount()); } } @@ -1459,7 +1517,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *view, QTreeWidgetItem *after, int \sa type() */ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled @@ -1477,7 +1535,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, int type) \sa type() */ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &strings, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled @@ -1499,7 +1557,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, const QStringList &str \sa type() */ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after, int type) - : rtti(type), view(0), d(new QTreeWidgetItemPrivate(this)), par(0), + : rtti(type), view(nullptr), d(new QTreeWidgetItemPrivate(this)), par(nullptr), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled @@ -1522,7 +1580,7 @@ QTreeWidgetItem::QTreeWidgetItem(QTreeWidgetItem *parent, QTreeWidgetItem *after QTreeWidgetItem::~QTreeWidgetItem() { - QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0); + QTreeModel *model = treeModel(); bool wasSkipSort = false; if (model) { wasSkipSort = model->skipPendingSort; @@ -1684,16 +1742,17 @@ void QTreeWidgetItem::setFlags(Qt::ItemFlags flags) void QTreeWidgetItemPrivate::updateHiddenStatus(QTreeWidgetItem *item, bool inserting) { - QTreeModel *model = (item->view ? qobject_cast<QTreeModel*>(item->view->model()) : 0); + QTreeModel *model = item->treeModel(); if (!model) return; QStack<QTreeWidgetItem *> parents; parents.push(item); while (!parents.isEmpty()) { QTreeWidgetItem *parent = parents.pop(); - QModelIndex index = model->index(parent, 0); - if (parent->d->hidden) + if (parent->d->hidden) { + const QModelIndex index = model->index(parent, 0); item->view->setRowHidden(index.row(), index.parent(), inserting); + } for (int i = 0; i < parent->children.count(); ++i) { QTreeWidgetItem *child = parent->children.at(i); parents.push(child); @@ -1758,7 +1817,7 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value) if (column < 0) return; - QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0); + QTreeModel *model = treeModel(); switch (role) { case Qt::EditRole: case Qt::DisplayRole: { @@ -1966,7 +2025,7 @@ void QTreeWidgetItem::insertChild(int index, QTreeWidgetItem *child) if (index < 0 || index > children.count() || child == 0 || child->view != 0 || child->par != 0) return; - if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0)) { + if (QTreeModel *model = treeModel()) { const bool wasSkipSort = model->skipPendingSort; model->skipPendingSort = true; if (model->rootItem == this) @@ -2017,7 +2076,7 @@ QTreeWidgetItem *QTreeWidgetItem::takeChild(int index) { // we move this outside the check of the index to allow executing // pending sorts from inline functions, using this function (hack) - QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0); + QTreeModel *model = treeModel(); if (model) { // This will trigger a layoutChanged signal, thus we might want to optimize // this function by not emitting the rowsRemoved signal etc to the view. @@ -2075,7 +2134,7 @@ void QTreeWidgetItem::insertChildren(int index, const QList<QTreeWidgetItem*> &c insertChild(index, children.at(n)); return; } - QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0); + QTreeModel *model = treeModel(); QStack<QTreeWidgetItem*> stack; QList<QTreeWidgetItem*> itemsToInsert; for (int n = 0; n < children.count(); ++n) { @@ -2122,7 +2181,7 @@ QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren() { QList<QTreeWidgetItem*> removed; if (children.count() > 0) { - QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0); + QTreeModel *model = treeModel(); if (model) { // This will trigger a layoutChanged signal, thus we might want to optimize // this function by not emitting the rowsRemoved signal etc to the view. @@ -2154,7 +2213,7 @@ QList<QTreeWidgetItem*> QTreeWidgetItem::takeChildren() void QTreeWidgetItemPrivate::sortChildren(int column, Qt::SortOrder order, bool climb) { - QTreeModel *model = (q->view ? qobject_cast<QTreeModel*>(q->view->model()) : 0); + QTreeModel *model = q->treeModel(); if (!model) return; model->sortItems(&q->children, column, order); @@ -2177,7 +2236,7 @@ void QTreeWidgetItemPrivate::sortChildren(int column, Qt::SortOrder order, bool */ void QTreeWidgetItem::sortChildren(int column, Qt::SortOrder order, bool climb) { - QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0); + QTreeModel *model = treeModel(); if (!model) return; if (model->isChanging()) @@ -2257,7 +2316,7 @@ void QTreeWidgetItem::emitDataChanged() */ void QTreeWidgetItem::itemChanged() { - if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0)) + if (QTreeModel *model = treeModel()) model->itemChanged(this); } @@ -2266,10 +2325,21 @@ void QTreeWidgetItem::itemChanged() */ void QTreeWidgetItem::executePendingSort() const { - if (QTreeModel *model = (view ? qobject_cast<QTreeModel*>(view->model()) : 0)) + if (QTreeModel *model = treeModel()) model->executePendingSort(); } +/*! + \internal + returns the QTreeModel if a view is set +*/ +QTreeModel *QTreeWidgetItem::treeModel(QTreeWidget *v) const +{ + if (!v) + v = view; + return (v ? qobject_cast<QTreeModel*>(v->model()) : nullptr); +} + #ifndef QT_NO_DATASTREAM /*! @@ -2469,8 +2539,8 @@ void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, QStyle::SH_ItemView_ActivateItemOnSingleClick style hint) or pressing a special key (e.g., \uicontrol Enter). - The specified \a item is the item that was clicked, or 0 if no - item was clicked. The \a column is the item's column that was + The specified \a item is the item that was clicked, or \nullptr if + no item was clicked. The \a column is the item's column that was clicked, or -1 if no item was clicked. */ @@ -2480,8 +2550,8 @@ void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, This signal is emitted when the user presses a mouse button inside the widget. - The specified \a item is the item that was clicked, or 0 if no - item was clicked. The \a column is the item's column that was + The specified \a item is the item that was clicked, or \nullptr if + no item was clicked. The \a column is the item's column that was clicked, or -1 if no item was clicked. */ @@ -2501,8 +2571,8 @@ void QTreeWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, This signal is emitted when the user double clicks inside the widget. - The specified \a item is the item that was clicked, or 0 if no - item was clicked. The \a column is the item's column that was + The specified \a item is the item that was clicked, or \nullptr if + no item was clicked. The \a column is the item's column that was clicked. If no item was double clicked, no signal will be emitted. */ @@ -2654,8 +2724,8 @@ QTreeWidgetItem *QTreeWidget::invisibleRootItem() const } /*! - Returns the top level item at the given \a index, or 0 if the item does - not exist. + Returns the top level item at the given \a index, or \nullptr if the + item does not exist. \sa topLevelItemCount(), insertTopLevelItem() */ @@ -2709,7 +2779,7 @@ void QTreeWidget::addTopLevelItem(QTreeWidgetItem *item) /*! Removes the top-level item at the given \a index in the tree and - returns it, otherwise returns 0; + returns it, otherwise returns \nullptr; \sa insertTopLevelItem(), topLevelItem(), topLevelItemCount() */ @@ -3031,7 +3101,7 @@ QWidget *QTreeWidget::itemWidget(QTreeWidgetItem *item, int column) const This function should only be used to display static content in the place of a tree widget item. If you want to display custom dynamic content or - implement a custom editor widget, use QTreeView and subclass QItemDelegate + implement a custom editor widget, use QTreeView and subclass QStyledItemDelegate instead. This function cannot be called before the item hierarchy has been set up, @@ -3048,6 +3118,7 @@ void QTreeWidget::setItemWidget(QTreeWidgetItem *item, int column, QWidget *widg QAbstractItemView::setIndexWidget(d->index(item, column), widget); } +#if QT_DEPRECATED_SINCE(5, 13) /*! Returns \c true if the \a item is selected; otherwise returns \c false. @@ -3059,9 +3130,7 @@ void QTreeWidget::setItemWidget(QTreeWidgetItem *item, int column, QWidget *widg */ bool QTreeWidget::isItemSelected(const QTreeWidgetItem *item) const { - if (!item) - return false; - return item->d->selected; + return ((item && item->treeWidget() == this) ? item->isSelected() : false); } /*! @@ -3076,16 +3145,10 @@ bool QTreeWidget::isItemSelected(const QTreeWidgetItem *item) const */ void QTreeWidget::setItemSelected(const QTreeWidgetItem *item, bool select) { - Q_D(QTreeWidget); - - if (!item) - return; - - selectionModel()->select(d->index(item), (select ? QItemSelectionModel::Select - : QItemSelectionModel::Deselect) - |QItemSelectionModel::Rows); - item->d->selected = select; + if (item && item->treeWidget() == this) + const_cast<QTreeWidgetItem*>(item)->setSelected(select); } +#endif /*! Returns a list of all selected non-hidden items. @@ -3102,7 +3165,7 @@ QList<QTreeWidgetItem*> QTreeWidget::selectedItems() const seen.reserve(indexes.count()); for (const auto &index : indexes) { QTreeWidgetItem *item = d->item(index); - if (isItemHidden(item) || seen.contains(item)) + if (item->isHidden() || seen.contains(item)) continue; seen.insert(item); items.append(item); @@ -3126,6 +3189,7 @@ QList<QTreeWidgetItem*> QTreeWidget::findItems(const QString &text, Qt::MatchFla return items; } +#if QT_DEPRECATED_SINCE(5, 13) /*! Returns \c true if the \a item is explicitly hidden, otherwise returns \c false. @@ -3135,13 +3199,7 @@ QList<QTreeWidgetItem*> QTreeWidget::findItems(const QString &text, Qt::MatchFla */ bool QTreeWidget::isItemHidden(const QTreeWidgetItem *item) const { - Q_D(const QTreeWidget); - if (item == d->treeModel()->headerItem) - return header()->isHidden(); - if (d->hiddenIndexes.isEmpty()) - return false; - QTreeModel::SkipSorting skipSorting(d->treeModel()); - return d->isRowHidden(d->index(item)); + return ((item && item->treeWidget() == this) ? item->isHidden() : false); } /*! @@ -3155,16 +3213,8 @@ bool QTreeWidget::isItemHidden(const QTreeWidgetItem *item) const */ void QTreeWidget::setItemHidden(const QTreeWidgetItem *item, bool hide) { - if (!item) - return; - Q_D(QTreeWidget); - if (item == d->treeModel()->headerItem) { - header()->setHidden(hide); - } else { - const QModelIndex index = d->index(item); - setRowHidden(index.row(), index.parent(), hide); - } - item->d->hidden = hide; + if (item && item->treeWidget() == this) + const_cast<QTreeWidgetItem*>(item)->setHidden(hide); } /*! @@ -3178,9 +3228,7 @@ void QTreeWidget::setItemHidden(const QTreeWidgetItem *item, bool hide) */ bool QTreeWidget::isItemExpanded(const QTreeWidgetItem *item) const { - Q_D(const QTreeWidget); - QTreeModel::SkipSorting skipSorting(d->treeModel()); - return isExpanded(d->index(item)); + return ((item && item->treeWidget() == this) ? item->isExpanded() : false); } /*! @@ -3195,9 +3243,8 @@ bool QTreeWidget::isItemExpanded(const QTreeWidgetItem *item) const */ void QTreeWidget::setItemExpanded(const QTreeWidgetItem *item, bool expand) { - Q_D(QTreeWidget); - QTreeModel::SkipSorting skipSorting(d->treeModel()); - setExpanded(d->index(item), expand); + if (item && item->treeWidget() == this) + const_cast<QTreeWidgetItem*>(item)->setExpanded(expand); } /*! @@ -3207,14 +3254,14 @@ void QTreeWidget::setItemExpanded(const QTreeWidgetItem *item, bool expand) otherwise returns \c false. \sa setFirstItemColumnSpanned() + + \obsolete + + This function is deprecated. Use \l{QTreeWidgetItem::isFirstColumnSpanned()} instead. */ bool QTreeWidget::isFirstItemColumnSpanned(const QTreeWidgetItem *item) const { - Q_D(const QTreeWidget); - if (item == d->treeModel()->headerItem) - return false; // We can't set the header items to spanning - const QModelIndex index = d->index(item); - return isFirstColumnSpanned(index.row(), index.parent()); + return ((item && item->treeWidget() == this) ? item->isFirstColumnSpanned() : false); } /*! @@ -3224,15 +3271,17 @@ bool QTreeWidget::isFirstItemColumnSpanned(const QTreeWidgetItem *item) const otherwise the item will show one section per column. \sa isFirstItemColumnSpanned() + + \obsolete + + This function is deprecated. Use \l{QTreeWidgetItem::setFirstColumnSpanned()} instead. */ void QTreeWidget::setFirstItemColumnSpanned(const QTreeWidgetItem *item, bool span) { - Q_D(QTreeWidget); - if (item == d->treeModel()->headerItem) - return; // We can't set header items to spanning - const QModelIndex index = d->index(item); - setFirstColumnSpanned(index.row(), index.parent(), span); + if (item && item->treeWidget() == this) + const_cast<QTreeWidgetItem*>(item)->setFirstColumnSpanned(span); } +#endif /*! \since 4.3 |