From bb2eda3e739e4a1e954459ea9fd119f7a8c0d6d4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jacob=20St=C3=B8ren?= <jacob.storen@ceetronSolutions.com>
Date: Mon, 17 Aug 2015 09:38:27 +0200
Subject: [PATCH] (#379)(#377) Moved store/restore of tree view state to new
 tree Also refactored slotCreateCommandObject

---
 .../ProjectDataModel/CMakeLists_files.cmake   |   3 +
 .../RimTreeViewStateSerializer.cpp            | 129 ++++++++++++++++++
 .../RimTreeViewStateSerializer.h              |  18 +++
 .../ProjectDataModel/RimUiTreeView.cpp        | 129 +-----------------
 .../ProjectDataModel/RimUiTreeView.h          |   6 -
 .../UserInterface/RiuMainWindow.cpp           |  43 +++---
 6 files changed, 179 insertions(+), 149 deletions(-)
 create mode 100644 ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.cpp
 create mode 100644 ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.h

diff --git a/ApplicationCode/ProjectDataModel/CMakeLists_files.cmake b/ApplicationCode/ProjectDataModel/CMakeLists_files.cmake
index 8c5f2ef7c5..5d5a1383ec 100644
--- a/ApplicationCode/ProjectDataModel/CMakeLists_files.cmake
+++ b/ApplicationCode/ProjectDataModel/CMakeLists_files.cmake
@@ -60,6 +60,7 @@ ${CEE_CURRENT_LIST_DIR}RimGeoMechResultDefinition.h
 ${CEE_CURRENT_LIST_DIR}RimGeoMechCellColors.h
 ${CEE_CURRENT_LIST_DIR}RimView.h
 ${CEE_CURRENT_LIST_DIR}RimCase.h
+${CEE_CURRENT_LIST_DIR}RimTreeViewStateSerializer.h
 )
 
 set (SOURCE_GROUP_SOURCE_FILES
@@ -118,6 +119,8 @@ ${CEE_CURRENT_LIST_DIR}RimGeoMechResultDefinition.cpp
 ${CEE_CURRENT_LIST_DIR}RimGeoMechCellColors.cpp
 ${CEE_CURRENT_LIST_DIR}RimView.cpp
 ${CEE_CURRENT_LIST_DIR}RimCase.cpp
+${CEE_CURRENT_LIST_DIR}RimTreeViewStateSerializer.cpp
+
 )
 
 list(APPEND CODE_HEADER_FILES
diff --git a/ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.cpp b/ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.cpp
new file mode 100644
index 0000000000..04ac193bed
--- /dev/null
+++ b/ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.cpp
@@ -0,0 +1,129 @@
+#include "RimTreeViewStateSerializer.h"
+#include <QTreeView>
+
+//--------------------------------------------------------------------------------------------------
+/// 
+//--------------------------------------------------------------------------------------------------
+void setExpandedState(QStringList& nodes, QTreeView* view, QAbstractItemModel* model,
+                      const QModelIndex startIndex, QString path)
+{
+    path += QString::number(startIndex.row()) + QString::number(startIndex.column());
+    for (int i = 0; i < model->rowCount(startIndex); ++i)
+    {
+        QModelIndex nextIndex = model->index(i, 0, startIndex);
+        QString nextPath = path + QString::number(nextIndex.row()) + QString::number(nextIndex.column());
+        if(!nodes.contains(nextPath))
+            continue;
+        
+        setExpandedState(nodes, view, model, model->index(i, 0, startIndex), path);
+    }
+    
+    if (nodes.contains(path))
+    {
+        view->setExpanded( startIndex.sibling(startIndex.row(), 0), true );
+    }
+}
+
+//--------------------------------------------------------------------------------------------------
+/// 
+//--------------------------------------------------------------------------------------------------
+void storeExpandedState(QStringList & nodes, const QTreeView * view, QAbstractItemModel * model,
+                        const QModelIndex startIndex, QString path)
+{
+    path += QString::number(startIndex.row()) + QString::number(startIndex.column());
+    for (int i = 0; i < model->rowCount(startIndex); ++i)
+    {
+        if(!view->isExpanded(model->index(i, 0, startIndex)))
+            continue;
+
+        storeExpandedState(nodes, view, model, model->index(i, 0, startIndex), path);
+    }
+
+    if (view->isExpanded(startIndex))
+    {
+        nodes << path;
+    }
+}
+
+//--------------------------------------------------------------------------------------------------
+/// 
+//--------------------------------------------------------------------------------------------------
+void RimTreeViewStateSerializer::applyTreeViewStateFromString( QTreeView* treeView, const QString& treeViewState)
+{
+    if (treeView->model())
+    {
+        treeView->collapseAll();
+
+        QStringList nodes = treeViewState.split(";");
+
+        QString path;
+        setExpandedState(nodes, treeView, treeView->model(), QModelIndex(), path);
+    }
+}
+
+//--------------------------------------------------------------------------------------------------
+/// 
+//--------------------------------------------------------------------------------------------------
+void RimTreeViewStateSerializer::storeTreeViewStateToString(const QTreeView* treeView, QString& treeViewState)
+{
+    if (treeView->model())
+    {
+        QStringList nodes;
+        QString path;
+
+        storeExpandedState(nodes, treeView, treeView->model(), QModelIndex(), path);
+
+        treeViewState = nodes.join(";");
+    }
+}
+
+//--------------------------------------------------------------------------------------------------
+/// Find index based of an encode QString <row> <column>;<row> <column>;...;<row> <column>
+/// Set the decoded index as current index in the QAbstractItemView
+//--------------------------------------------------------------------------------------------------
+QModelIndex RimTreeViewStateSerializer::getModelIndexFromString(QAbstractItemModel* model, 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 = model->index(row, col, mi);
+    }
+
+    return mi;
+}
+
+//--------------------------------------------------------------------------------------------------
+/// Store path to model index in item view using follwoing encoding into a QString <row> <column>;<row> <column>;...;<row> <column>
+//--------------------------------------------------------------------------------------------------
+void RimTreeViewStateSerializer::encodeStringFromModelIndex(const QModelIndex mi, QString& encodedModelIndex)
+{
+    if (!mi.isValid()) return;
+
+    QModelIndex localModelIdx = mi;
+
+    while (localModelIdx.isValid())
+    {
+        if (encodedModelIndex.isEmpty())
+        {
+            encodedModelIndex = QString("%1 %2").arg(localModelIdx.row()).arg(localModelIdx.column()) + encodedModelIndex;
+        }
+        else
+        {
+            encodedModelIndex = QString("%1 %2;").arg(localModelIdx.row()).arg(localModelIdx.column()) + encodedModelIndex;
+        }
+        localModelIdx = localModelIdx.parent();
+    }
+}
+
+
+
diff --git a/ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.h b/ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.h
new file mode 100644
index 0000000000..7fe24ef03f
--- /dev/null
+++ b/ApplicationCode/ProjectDataModel/RimTreeViewStateSerializer.h
@@ -0,0 +1,18 @@
+#pragma once
+ 
+#include <QModelIndex>
+
+class QTreeView;
+class QString;
+class QAbstractItemModel;
+
+class RimTreeViewStateSerializer
+{
+public:
+    static void applyTreeViewStateFromString(QTreeView* treeView, const QString& treeViewState);
+    static void storeTreeViewStateToString  (const QTreeView* treeView, QString& treeViewState);
+
+    static QModelIndex getModelIndexFromString(QAbstractItemModel* model, const QString& currentIndexString);
+    static void encodeStringFromModelIndex(const QModelIndex mi, QString& currentIndexString);
+};
+
diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp b/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp
index 4e4f837609..4c7f00b2cf 100644
--- a/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp
+++ b/ApplicationCode/ProjectDataModel/RimUiTreeView.cpp
@@ -74,6 +74,7 @@
 #include <QFileInfo>
 #include <QMenu>
 #include <QMessageBox>
+#include "RimTreeViewStateSerializer.h"
 
 
 
@@ -627,7 +628,7 @@ void RimUiTreeView::slotExecuteScriptForSelectedCases()
     if (!action) return;
 
     QString encodedModelIndex = action->data().toString();
-    QModelIndex mi = RimUiTreeView::getModelIndexFromString(model(), encodedModelIndex);
+    QModelIndex mi = RimTreeViewStateSerializer::getModelIndexFromString(model(), encodedModelIndex);
 
     RimUiTreeModelPdm* myModel = dynamic_cast<RimUiTreeModelPdm*>(model());
     if (!myModel) return;
@@ -1306,130 +1307,6 @@ void RimUiTreeView::mousePressEvent(QMouseEvent* mouseEvent)
     QTreeView::mousePressEvent(mouseEvent);
 }
 
-//--------------------------------------------------------------------------------------------------
-/// 
-//--------------------------------------------------------------------------------------------------
-void setExpandedState(QStringList& nodes, QTreeView* view, QAbstractItemModel* model,
-                      const QModelIndex startIndex, QString path)
-{
-    path += QString::number(startIndex.row()) + QString::number(startIndex.column());
-    for (int i = 0; i < model->rowCount(startIndex); ++i)
-    {
-        QModelIndex nextIndex = model->index(i, 0, startIndex);
-        QString nextPath = path + QString::number(nextIndex.row()) + QString::number(nextIndex.column());
-        if(!nodes.contains(nextPath))
-            continue;
-        
-        setExpandedState(nodes, view, model, model->index(i, 0, startIndex), path);
-    }
-    
-    if (nodes.contains(path))
-    {
-        view->setExpanded( startIndex.sibling(startIndex.row(), 0), true );
-    }
-}
-
-//--------------------------------------------------------------------------------------------------
-/// 
-//--------------------------------------------------------------------------------------------------
-void storeExpandedState(QStringList & nodes, QTreeView * view, QAbstractItemModel * model,
-                        const QModelIndex startIndex, QString path)
-{
-    path += QString::number(startIndex.row()) + QString::number(startIndex.column());
-    for (int i = 0; i < model->rowCount(startIndex); ++i)
-    {
-        if(!view->isExpanded(model->index(i, 0, startIndex)))
-            continue;
-
-        storeExpandedState(nodes, view, model, model->index(i, 0, startIndex), path);
-    }
-
-    if (view->isExpanded(startIndex))
-    {
-        nodes << path;
-    }
-}
-
-//--------------------------------------------------------------------------------------------------
-/// 
-//--------------------------------------------------------------------------------------------------
-void RimUiTreeView::applyTreeViewStateFromString(const QString& treeViewState)
-{
-    if (this->model())
-    {
-        this->collapseAll();
-
-        QStringList nodes = treeViewState.split(";");
-
-        QString path;
-        setExpandedState(nodes, this, this->model(), QModelIndex(), path);
-    }
-}
-
-//--------------------------------------------------------------------------------------------------
-/// 
-//--------------------------------------------------------------------------------------------------
-void RimUiTreeView::storeTreeViewStateToString(QString& treeViewState)
-{
-    if (this->model())
-    {
-        QStringList nodes;
-        QString path;
-
-        storeExpandedState(nodes, this, this->model(), QModelIndex(), path);
-
-        treeViewState = nodes.join(";");
-    }
-}
-
-//--------------------------------------------------------------------------------------------------
-/// Find index based of an encode QString <row> <column>;<row> <column>;...;<row> <column>
-/// Set the decoded index as current index in the QAbstractItemView
-//--------------------------------------------------------------------------------------------------
-QModelIndex RimUiTreeView::getModelIndexFromString(QAbstractItemModel* model, 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 = model->index(row, col, mi);
-    }
-
-    return mi;
-}
-
-//--------------------------------------------------------------------------------------------------
-/// Store path to model index in item view using follwoing encoding into a QString <row> <column>;<row> <column>;...;<row> <column>
-//--------------------------------------------------------------------------------------------------
-void RimUiTreeView::encodeStringFromModelIndex(const QModelIndex mi, QString& encodedModelIndex)
-{
-    if (!mi.isValid()) return;
-
-    QModelIndex localModelIdx = mi;
-
-    while (localModelIdx.isValid())
-    {
-        if (encodedModelIndex.isEmpty())
-        {
-            encodedModelIndex = QString("%1 %2").arg(localModelIdx.row()).arg(localModelIdx.column()) + encodedModelIndex;
-        }
-        else
-        {
-            encodedModelIndex = QString("%1 %2;").arg(localModelIdx.row()).arg(localModelIdx.column()) + encodedModelIndex;
-        }
-        localModelIdx = localModelIdx.parent();
-    }
-}
-
 
 //--------------------------------------------------------------------------------------------------
 /// 
@@ -1603,7 +1480,7 @@ void RimUiTreeView::appendScriptItems(QMenu* menu, RimScriptCollection* scriptCo
         }
 
         QString encodedModelIndex;
-        RimUiTreeView::encodeStringFromModelIndex(mi, encodedModelIndex);
+        RimTreeViewStateSerializer::encodeStringFromModelIndex(mi, encodedModelIndex);
 
         scriptAction->setData(QVariant(encodedModelIndex));
     }
diff --git a/ApplicationCode/ProjectDataModel/RimUiTreeView.h b/ApplicationCode/ProjectDataModel/RimUiTreeView.h
index 04fd062cf5..ce21d3e9aa 100644
--- a/ApplicationCode/ProjectDataModel/RimUiTreeView.h
+++ b/ApplicationCode/ProjectDataModel/RimUiTreeView.h
@@ -47,12 +47,6 @@ public:
     void selectedUiItems(std::vector<caf::PdmUiItem*>& objects);
 
 
-    void applyTreeViewStateFromString(const QString& treeViewState);
-    void storeTreeViewStateToString(QString& treeViewState);
-
-    static QModelIndex getModelIndexFromString(QAbstractItemModel* model, const QString& currentIndexString);
-    static void encodeStringFromModelIndex(const QModelIndex mi, QString& currentIndexString);
-
 protected:
     void contextMenuEvent(QContextMenuEvent* event);
 
diff --git a/ApplicationCode/UserInterface/RiuMainWindow.cpp b/ApplicationCode/UserInterface/RiuMainWindow.cpp
index 4f17df2891..07947172f5 100644
--- a/ApplicationCode/UserInterface/RiuMainWindow.cpp
+++ b/ApplicationCode/UserInterface/RiuMainWindow.cpp
@@ -74,6 +74,7 @@
 #include "cafSelectionManager.h"
 
 #include "cvfTimer.h"
+#include "RimTreeViewStateSerializer.h"
 
 
 //==================================================================================================
@@ -1917,15 +1918,15 @@ void RiuMainWindow::slotDisableLightingAction(bool disable)
 //--------------------------------------------------------------------------------------------------
 void RiuMainWindow::storeTreeViewState()
 {
-    if (m_OBSOLETE_treeView)
+    if (m_projectTreeView)
     {
         QString treeViewState;
-        m_OBSOLETE_treeView->storeTreeViewStateToString(treeViewState);
+        RimTreeViewStateSerializer::storeTreeViewStateToString(m_projectTreeView->treeView(), treeViewState);
 
-        QModelIndex mi = m_OBSOLETE_treeView->currentIndex();
+        QModelIndex mi = m_projectTreeView->treeView()->currentIndex();
 
         QString encodedModelIndexString;
-        RimUiTreeView::encodeStringFromModelIndex(mi, encodedModelIndexString);
+        RimTreeViewStateSerializer::encodeStringFromModelIndex(mi, encodedModelIndexString);
         
         RiaApplication::instance()->project()->treeViewState = treeViewState;
         RiaApplication::instance()->project()->currentModelIndexPath = encodedModelIndexString;
@@ -1937,20 +1938,20 @@ void RiuMainWindow::storeTreeViewState()
 //--------------------------------------------------------------------------------------------------
 void RiuMainWindow::restoreTreeViewState()
 {
-    if (m_OBSOLETE_treeView)
+    if (m_projectTreeView)
     {
         QString stateString = RiaApplication::instance()->project()->treeViewState;
         if (!stateString.isEmpty())
         {
-            m_OBSOLETE_treeView->collapseAll();
-            m_OBSOLETE_treeView->applyTreeViewStateFromString(stateString);
+            m_projectTreeView->treeView()->collapseAll();
+            RimTreeViewStateSerializer::applyTreeViewStateFromString(m_projectTreeView->treeView(), stateString);
         }
 
         QString currentIndexString = RiaApplication::instance()->project()->currentModelIndexPath;
         if (!currentIndexString.isEmpty())
         {
-            QModelIndex mi = RimUiTreeView::getModelIndexFromString(m_OBSOLETE_treeView->model(), currentIndexString);
-            m_OBSOLETE_treeView->setCurrentIndex(mi);
+            QModelIndex mi = RimTreeViewStateSerializer::getModelIndexFromString(m_projectTreeView->treeView()->model(), currentIndexString);
+            m_projectTreeView->treeView()->setCurrentIndex(mi);
         }
     }
 }
@@ -1960,6 +1961,7 @@ void RiuMainWindow::restoreTreeViewState()
 //--------------------------------------------------------------------------------------------------
 void RiuMainWindow::setCurrentObjectInTreeView(caf::PdmObject* object)
 {
+#if 1 // OBSOLETE
     if (m_OBSOLETE_treeView && m_OBSOLETE_treeModelPdm)
     {
         QModelIndex mi = m_OBSOLETE_treeModelPdm->getModelIndexFromPdmObject(object);
@@ -1969,7 +1971,7 @@ void RiuMainWindow::setCurrentObjectInTreeView(caf::PdmObject* object)
             m_OBSOLETE_treeView->setCurrentIndex(mi);
         }
     }
-
+#endif
     m_projectTreeView->selectAsCurrentItem(object);
 }
 
@@ -2058,14 +2060,21 @@ void RiuMainWindow::slotCreateCommandObject()
     RiaApplication* app = RiaApplication::instance();
     if (!app->project()) return;
 
-    QItemSelectionModel* selectionModel = m_OBSOLETE_treeView->selectionModel();
-    if (selectionModel)
-    {
-        QModelIndexList selectedModelIndices = selectionModel->selectedIndexes();
-        
-        caf::PdmObjectGroup selectedObjects;
-        m_OBSOLETE_treeModelPdm->populateObjectGroupFromModelIndexList(selectedModelIndices, &selectedObjects);
+    std::vector<caf::PdmUiItem*> selectedUiItems;
+    m_projectTreeView->selectedObjects(selectedUiItems);
 
+    caf::PdmObjectGroup selectedObjects;
+    for (size_t i = 0; i < selectedUiItems.size(); ++i)
+    {
+        caf::PdmUiObjectHandle* uiObj = dynamic_cast<caf::PdmUiObjectHandle*>(selectedUiItems[i]);
+        if (uiObj) 
+        {
+            selectedObjects.addObject(uiObj->objectHandle());
+        }
+    }
+
+    if (selectedObjects.objects.size())
+    { 
         std::vector<RimCommandObject*> commandObjects;
         RimCommandFactory::createCommandObjects(selectedObjects, &commandObjects);