From daf4132dec7f59bf0f5357aaeae34fbc02c35d67 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Fri, 20 Sep 2019 14:18:52 +0200 Subject: [PATCH] #4744 AppFwk : Auto scroll to first checked item --- .../cafPdmUiTreeSelectionEditor.cpp | 48 +++++++++++++++++++ .../cafPdmUiTreeSelectionEditor.h | 2 + 2 files changed, 50 insertions(+) diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp index df3cdcba00..24959a67d2 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp @@ -52,6 +52,7 @@ #include #include #include +#include #include #include @@ -374,6 +375,10 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName) } } } + + // It is required to use a timer here, as the layout of the widgets are handled by events + // Calling scrollTo() here has no effect, or scrolls to wrong location + QTimer::singleShot(150, this, SLOT(slotScrollToFirstCheckedItem())); } //-------------------------------------------------------------------------------------------------- @@ -647,6 +652,49 @@ void PdmUiTreeSelectionEditor::slotClicked(const QModelIndex& index) currentChanged(index); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void PdmUiTreeSelectionEditor::slotScrollToFirstCheckedItem() +{ + auto firstVisibleIndex = m_treeView->indexAt(m_treeView->viewport()->rect().topLeft()); + auto lastVisibleIndex = m_treeView->indexAt(m_treeView->viewport()->rect().bottomRight()); + + if (!firstVisibleIndex.isValid()) + { + return; + } + + if (!lastVisibleIndex.isValid()) + { + return; + } + + for (int i = firstVisibleIndex.row(); i < lastVisibleIndex.row(); i++) + { + auto treeViewIndex = m_proxyModel->index(i, 0); + + if (m_proxyModel->data(treeViewIndex, Qt::CheckStateRole).toBool()) + { + // Do nothing if there is a checked and visible item in the view + return; + } + } + + for (int i = 0; i < m_proxyModel->rowCount(); i++) + { + auto treeViewIndex = m_proxyModel->index(i, 0); + + if (m_proxyModel->data(treeViewIndex, Qt::CheckStateRole).toBool()) + { + // Scroll to the first checked item if no checked items are visible + m_treeView->scrollTo(treeViewIndex); + + return; + } + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.h b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.h index 542f5d4418..9de488f9de 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.h +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.h @@ -115,6 +115,8 @@ private slots: void slotClicked(const QModelIndex& index); + void slotScrollToFirstCheckedItem(); + private: void currentChanged(const QModelIndex& current);