Set depth correctly in the active branch
This commit is contained in:
Juergen Spitzmueller 2023-09-18 09:27:38 +02:00
parent 63b99beedd
commit d3102380bc
2 changed files with 18 additions and 1 deletions

View File

@ -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) void TocWidget::collapseAllOthers(int const depth)
{ {
if (!tocTV->model()) if (!tocTV->model())
@ -620,11 +633,13 @@ void TocWidget::collapseAllOthers(int const depth)
int size = indices.size(); int size = indices.size();
// collapse parents which are not in our ancestry line // collapse parents which are not in our ancestry line
// and which exceed the requested depth
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
QModelIndex index = indices[i]; QModelIndex index = indices[i];
if (tocTV->isExpanded(index) if (tocTV->isExpanded(index)
&& !isAncestor(index, tocTV->currentIndex())) { && !isAncestor(index, tocTV->currentIndex())) {
tocTV->collapse(index); if (depth < getItemDepth(index))
tocTV->collapse(index);
if (depth > 0 && index.parent() == QModelIndex()) if (depth > 0 && index.parent() == QModelIndex())
tocTV->expandRecursively(index, depth - 1); tocTV->expandRecursively(index, depth - 1);
} }

View File

@ -108,6 +108,8 @@ private:
QModelIndex getAncestor(QModelIndex const & descendant) const; QModelIndex getAncestor(QModelIndex const & descendant) const;
/// \returns \c true if \p ancestor is an ancestor (parent, grandparent, etc.) of \p descendant /// \returns \c true if \p ancestor is an ancestor (parent, grandparent, etc.) of \p descendant
bool isAncestor(QModelIndex const & ancestor, QModelIndex const & descendant) const; 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 /// collapse all nodes to \c depth except for the branch of the currently active item
void collapseAllOthers(int const depth); void collapseAllOthers(int const depth);
/// ///