diff options
Diffstat (limited to 'src/widgets/itemviews/qtreeview.cpp')
-rw-r--r-- | src/widgets/itemviews/qtreeview.cpp | 99 |
1 files changed, 61 insertions, 38 deletions
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp index f3647f656a..e7196c9d5d 100644 --- a/src/widgets/itemviews/qtreeview.cpp +++ b/src/widgets/itemviews/qtreeview.cpp @@ -1089,15 +1089,8 @@ void QTreeView::keyboardSearch(const QString &search) if (start.column() > 0) index = index.sibling(index.row(), start.column()); - if (index.isValid()) { - QItemSelectionModel::SelectionFlags flags = (d->selectionMode == SingleSelection - ? QItemSelectionModel::SelectionFlags( - QItemSelectionModel::ClearAndSelect - |d->selectionBehaviorFlags()) - : QItemSelectionModel::SelectionFlags( - QItemSelectionModel::NoUpdate)); - selectionModel()->setCurrentIndex(index, flags); - } + if (index.isValid()) + setCurrentIndex(index); } /*! @@ -1992,21 +1985,7 @@ void QTreeView::keyPressEvent(QKeyEvent *event) if (d->isIndexValid(current) && d->model && d->itemsExpandable) { switch (event->key()) { case Qt::Key_Asterisk: { - // do layouting only once after expanding is done - d->doDelayedItemsLayout(); - QStack<QModelIndex> parents; - parents.push(current); - while (!parents.isEmpty()) { - QModelIndex parent = parents.pop(); - for (int row = 0; row < d->model->rowCount(parent); ++row) { - QModelIndex child = d->model->index(row, 0, parent); - if (!d->isIndexValid(child)) - break; - parents.push(child); - expand(child); - } - } - expand(current); + expandRecursively(current); break; } case Qt::Key_Plus: expand(current); @@ -2160,12 +2139,10 @@ int QTreeView::verticalOffset() const // ### find a faster way to do this d->executePostedLayout(); int offset = 0; - for (int i = 0; i < d->viewItems.count(); ++i) { - if (i == verticalScrollBar()->value()) - return offset; + const int cnt = std::min(d->viewItems.count(), verticalScrollBar()->value()); + for (int i = 0; i < cnt; ++i) offset += d->itemHeight(i); - } - return 0; + return offset; } // scroll per pixel return verticalScrollBar()->value(); @@ -2621,10 +2598,13 @@ void QTreeView::resizeColumnToContents(int column) d->header->resizeSection(column, qMax(contents, header)); } +#if QT_DEPRECATED_SINCE(5, 13) /*! \obsolete \overload + This function is deprecated. Use + sortByColumn(int column, Qt::SortOrder order) instead. Sorts the model by the values in the given \a column. */ void QTreeView::sortByColumn(int column) @@ -2632,6 +2612,7 @@ void QTreeView::sortByColumn(int column) Q_D(QTreeView); sortByColumn(column, d->header->sortIndicatorOrder()); } +#endif /*! \since 4.2 @@ -2647,10 +2628,12 @@ void QTreeView::sortByColumn(int column) void QTreeView::sortByColumn(int column, Qt::SortOrder order) { Q_D(QTreeView); - - //If sorting is enabled will emit a signal connected to _q_sortIndicatorChanged, which then actually sorts + if (column < 0) + return; + // If sorting is enabled it will emit a signal connected to + // _q_sortIndicatorChanged, which then actually sorts d->header->setSortIndicator(column, order); - //If sorting is not enabled, force to sort now. + // If sorting is not enabled, force to sort now if (!d->sortingEnabled) d->model->sort(column, order); } @@ -2694,11 +2677,7 @@ QSize QTreeView::viewportSizeHint() const QSize result = QSize(d->header->length(), deepestRect.bottom() + 1); // add size for header - result += QSize(0, d->header->isVisible() ? d->header->height() : 0); - - // add size for scrollbars - result += QSize(verticalScrollBar()->isVisible() ? verticalScrollBar()->width() : 0, - horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0); + result += QSize(0, d->header->isHidden() ? 0 : d->header->height()); return result; } @@ -2707,7 +2686,7 @@ QSize QTreeView::viewportSizeHint() const \since 4.2 Expands all expandable items. - Warning: if the model contains a large number of items, + \warning: if the model contains a large number of items, this function will take some time to execute. \sa collapseAll(), expand(), collapse(), setExpanded() @@ -2723,6 +2702,50 @@ void QTreeView::expandAll() } /*! + \since 5.13 + Expands the item at the given \a index and all its children to the + given \a depth. The \a depth is relative to the given \a index. + A \a depth of -1 will expand all children, a \a depth of 0 will + only expand the given \a index. + + \warning: if the model contains a large number of items, + this function will take some time to execute. + + \sa expandAll() +*/ +void QTreeView::expandRecursively(const QModelIndex &index, int depth) +{ + Q_D(QTreeView); + + if (depth < -1) + return; + // do layouting only once after expanding is done + d->doDelayedItemsLayout(); + expand(index); + if (depth == 0) + return; + QStack<QPair<QModelIndex, int>> parents; + parents.push({index, 0}); + while (!parents.isEmpty()) { + const QPair<QModelIndex, int> elem = parents.pop(); + const QModelIndex &parent = elem.first; + const int curDepth = elem.second; + const int rowCount = d->model->rowCount(parent); + for (int row = 0; row < rowCount; ++row) { + const QModelIndex child = d->model->index(row, 0, parent); + if (!d->isIndexValid(child)) + break; + if (depth == -1 || curDepth + 1 < depth) + parents.push({child, curDepth + 1}); + if (d->isIndexExpanded(child)) + continue; + if (d->storeExpanded(child)) + emit expanded(child); + } + } +} + +/*! \since 4.2 Collapses all expanded items. |