diff --git a/ApplicationCode/ProjectDataModel/RimProject.cpp b/ApplicationCode/ProjectDataModel/RimProject.cpp index 77f6d0c9d0..b367e20759 100644 --- a/ApplicationCode/ProjectDataModel/RimProject.cpp +++ b/ApplicationCode/ProjectDataModel/RimProject.cpp @@ -44,6 +44,9 @@ RimProject::RimProject(void) CAF_PDM_InitFieldNoDefault(&treeViewState, "TreeViewState", "", "", "", ""); treeViewState.setUiHidden(true); + CAF_PDM_InitFieldNoDefault(¤tModelIndexPath, "TreeViewCurrentModelIndexPath", "", "", "", ""); + currentModelIndexPath.setUiHidden(true); + scriptCollection = new RimScriptCollection(); scriptCollection->directory.setUiHidden(true); diff --git a/ApplicationCode/ProjectDataModel/RimProject.h b/ApplicationCode/ProjectDataModel/RimProject.h index af18f82f11..d3ab262d60 100644 --- a/ApplicationCode/ProjectDataModel/RimProject.h +++ b/ApplicationCode/ProjectDataModel/RimProject.h @@ -38,6 +38,7 @@ public: caf::PdmPointersField caseGroups; caf::PdmField scriptCollection; caf::PdmField treeViewState; + caf::PdmField currentModelIndexPath; void setScriptDirectories(const QString& scriptDirectories); diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp b/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp index b0834e2b89..e88b83ce1a 100644 --- a/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp +++ b/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp @@ -1297,6 +1297,55 @@ void RimUiTreeView::storeTreeViewStateToString(QString& treeViewState) } } +//-------------------------------------------------------------------------------------------------- +/// Find index based of an encode QString ; ;...; +/// Set the decoded index as current index in the QAbstractItemView +//-------------------------------------------------------------------------------------------------- +void RimUiTreeView::applyCurrentIndexFromString(QAbstractItemView& itemView, const QString& currentIndexString) +{ + QStringList modelIndexStringList = currentIndexString.split(";"); + + QModelIndex mi; + + foreach (QString modelIndexString, modelIndexStringList) + { + QStringList items = modelIndexString.split(" "); + + if (items.size() != 2) continue; + + int row = items[0].toInt(); + int col = items[1].toInt(); + + mi = itemView.model()->index(row, col, mi); + } + + if (mi.isValid()) + { + itemView.setCurrentIndex(mi); + } +} + +//-------------------------------------------------------------------------------------------------- +/// Store path to current index in item view using follwoing encoding into a QString ; ;...; +//-------------------------------------------------------------------------------------------------- +void RimUiTreeView::storeCurrentIndexToString(const QAbstractItemView& itemView, QString& currentIndexString) +{ + QModelIndex mi = itemView.currentIndex(); + if (!mi.isValid()) return; + + QString path = QString("%1 %2").arg(mi.row()).arg(mi.column()); + mi = mi.parent(); + + while (mi.isValid()) + { + path = QString("%1 %2;").arg(mi.row()).arg(mi.column()) + path; + mi = mi.parent(); + } + + currentIndexString = path; +} + + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeView.h b/ApplicationCode/ProjectDataModel/RimUiTreeView.h index 191fa948b6..d5f456485e 100644 --- a/ApplicationCode/ProjectDataModel/RimUiTreeView.h +++ b/ApplicationCode/ProjectDataModel/RimUiTreeView.h @@ -46,6 +46,9 @@ public: void applyTreeViewStateFromString(const QString& treeViewState); void storeTreeViewStateToString(QString& treeViewState); + static void applyCurrentIndexFromString(QAbstractItemView& itemView, const QString& currentIndexString); + static void storeCurrentIndexToString(const QAbstractItemView& itemView, QString& currentIndexString); + protected: void contextMenuEvent(QContextMenuEvent* event); diff --git a/ApplicationCode/UserInterface/RiuMainWindow.cpp b/ApplicationCode/UserInterface/RiuMainWindow.cpp index 2b1128799b..b4e61a8ac1 100644 --- a/ApplicationCode/UserInterface/RiuMainWindow.cpp +++ b/ApplicationCode/UserInterface/RiuMainWindow.cpp @@ -1428,10 +1428,13 @@ void RiuMainWindow::storeTreeViewState() if (m_treeView) { QString treeViewState; - m_treeView->storeTreeViewStateToString(treeViewState); + + QString currentIndexString; + RimUiTreeView::storeCurrentIndexToString(*m_treeView, currentIndexString); RiaApplication::instance()->project()->treeViewState = treeViewState; + RiaApplication::instance()->project()->currentModelIndexPath = currentIndexString; } } @@ -1448,6 +1451,12 @@ void RiuMainWindow::restoreTreeViewState() m_treeView->collapseAll(); m_treeView->applyTreeViewStateFromString(stateString); } + + QString currentIndexString = RiaApplication::instance()->project()->currentModelIndexPath; + if (!currentIndexString.isEmpty()) + { + RimUiTreeView::applyCurrentIndexFromString(*m_treeView, currentIndexString); + } } }