diff --git a/src/private/multisplitter/Item.cpp b/src/private/multisplitter/Item.cpp index bd86cb87..7f1604f0 100644 --- a/src/private/multisplitter/Item.cpp +++ b/src/private/multisplitter/Item.cpp @@ -215,6 +215,30 @@ void Item::restore(GuestInterface *guest) } } +QVector Item::pathFromRoot() const +{ + // Returns the list of indexes to get to this item, starting from the root container + // Example [0, 1, 3] would mean that the item is the 4th child of the 2nd child of the 1st child of root + // [] would mean 'this' is the root item + // [0] would mean the 1st child of root + + QVector path; + path.reserve(10); // random big number, good to bootstrap it + + const Item *it = this; + while (it) { + if (auto p = it->parentContainer()) { + const int index = p->m_children.indexOf(const_cast(it)); + path.prepend(index); + it = p; + } else { + break; + } + } + + return path; +} + void Item::setHostWidget(QWidget *host) { if (m_hostWidget != host) { @@ -2682,6 +2706,35 @@ QVector ItemContainer::separators() const return m_separators; } +const Item *ItemContainer::itemFromPath(const QVector &path) const +{ + const ItemContainer *container = this; + + for (int i = 0; i < path.size() ; ++i) { + const int index = path[i]; + const bool isLast = i == path.size() - 1; + if (index < 0 || index >= container->m_children.size()) { + // Doesn't happen + root()->dumpLayout(); + qWarning() << Q_FUNC_INFO << "Invalid index" << index + << this << path << isRoot(); + return nullptr; + } + + if (isLast) { + return container->m_children.at(index); + } else { + container = container->m_children.at(index)->asContainer(); + if (!container) { + qWarning() << Q_FUNC_INFO << "Invalid index" << path; + return nullptr; + } + } + } + + return this; +} + Separator *ItemContainer::neighbourSeparator(const Item *item, Side side, Qt::Orientation orientation) const { Item::List children = visibleChildren(); diff --git a/src/private/multisplitter/Item_p.h b/src/private/multisplitter/Item_p.h index 89a582a1..e9e1c380 100644 --- a/src/private/multisplitter/Item_p.h +++ b/src/private/multisplitter/Item_p.h @@ -351,6 +351,8 @@ public: QWidget *hostWidget() const; void restore(GuestInterface *guest); + QVector pathFromRoot() const; + virtual QSize minSize() const; virtual QSize maxSize() const; virtual void setSize_recursive(QSize newSize, ChildrenResizeStrategy strategy = ChildrenResizeStrategy::Percentage); @@ -555,6 +557,7 @@ public: QVector separators() const; Qt::Orientation m_orientation = Qt::Vertical; private: + const Item *itemFromPath(const QVector &path) const; void resizeChildren(QSize oldSize, QSize newSize, SizingInfo::List &sizes, ChildrenResizeStrategy); void scheduleCheckSanity() const; Separator *neighbourSeparator(const Item *item, Side, Qt::Orientation) const;