(#379)(#377) Moved store/restore of tree view state to new tree

Also refactored slotCreateCommandObject
This commit is contained in:
Jacob Støren 2015-08-17 09:38:27 +02:00
parent 2f49415d3f
commit bb2eda3e73
6 changed files with 179 additions and 149 deletions

View File

@ -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

View File

@ -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();
}
}

View File

@ -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);
};

View File

@ -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));
}

View File

@ -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);

View File

@ -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);