diff --git a/src/frontends/qt/TocWidget.cpp b/src/frontends/qt/TocWidget.cpp index 4cbcf96196..0770cf3ab9 100644 --- a/src/frontends/qt/TocWidget.cpp +++ b/src/frontends/qt/TocWidget.cpp @@ -611,6 +611,19 @@ QModelIndex TocWidget::getAncestor(QModelIndex const & descendant) const } +int TocWidget::getItemDepth(QModelIndex const & index_in) +{ + QModelIndex index = index_in; + int depth = 1; + while (index.parent().isValid()) + { + index = index.parent(); + ++depth; + } + return depth; +} + + void TocWidget::collapseAllOthers(int const depth) { if (!tocTV->model()) @@ -620,11 +633,13 @@ void TocWidget::collapseAllOthers(int const depth) int size = indices.size(); // collapse parents which are not in our ancestry line + // and which exceed the requested depth for (int i = size - 1; i >= 0; i--) { QModelIndex index = indices[i]; if (tocTV->isExpanded(index) && !isAncestor(index, tocTV->currentIndex())) { - tocTV->collapse(index); + if (depth < getItemDepth(index)) + tocTV->collapse(index); if (depth > 0 && index.parent() == QModelIndex()) tocTV->expandRecursively(index, depth - 1); } diff --git a/src/frontends/qt/TocWidget.h b/src/frontends/qt/TocWidget.h index c56d0ae051..72c6089e06 100644 --- a/src/frontends/qt/TocWidget.h +++ b/src/frontends/qt/TocWidget.h @@ -108,6 +108,8 @@ private: QModelIndex getAncestor(QModelIndex const & descendant) const; /// \returns \c true if \p ancestor is an ancestor (parent, grandparent, etc.) of \p descendant bool isAncestor(QModelIndex const & ancestor, QModelIndex const & descendant) const; + /// \returns depth of the current item + int getItemDepth(QModelIndex const & index); /// collapse all nodes to \c depth except for the branch of the currently active item void collapseAllOthers(int const depth); ///