Prototyped command object infrastructure to be used in regression tests of Octave scripts

p4#: 22387
This commit is contained in:
Magne Sjaastad 2013-09-12 08:11:56 +02:00
parent f928ef87e8
commit c3adfb7cb0
14 changed files with 312 additions and 30 deletions

View File

@ -350,6 +350,14 @@ bool RiaApplication::loadProject(const QString& projectFileName)
caseProgress.incrementProgress();
}
// Loop over command objects and execute them
for (size_t i = 0; i < m_project->commandObjects.size(); i++)
{
m_commandQueue.push_back(m_project->commandObjects[i]);
}
executeCommandObjects();
onProjectOpenedOrClosed();
return true;
@ -495,6 +503,8 @@ bool RiaApplication::closeProject(bool askToSaveIfDirty)
caf::EffectGenerator::clearEffectCache();
m_project->close();
m_commandQueue.clear();
onProjectOpenedOrClosed();
return true;
@ -959,6 +969,10 @@ void RiaApplication::slotWorkerProcessFinished(int exitCode, QProcess::ExitStatu
return;
}
executeCommandObjects();
// Exit code != 0 means we have an error
if (exitCode != 0)
{
@ -1678,3 +1692,27 @@ QVariant RiaApplication::cacheDataObject(const QString& key) const
return QVariant();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaApplication::addCommandObject(RimCommandObject* commandObject)
{
m_commandQueue.push_back(commandObject);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaApplication::executeCommandObjects()
{
if (m_commandQueue.size() > 0)
{
std::list< RimCommandObject* >::iterator it = m_commandQueue.begin();
RimCommandObject* first = *it;
first->redo();
m_commandQueue.pop_front();
}
}

View File

@ -35,6 +35,7 @@ class RiaSocketServer;
class RiaPreferences;
class RimReservoirView;
class RimProject;
class RimCommandObject;
namespace caf
{
@ -133,6 +134,9 @@ public:
void setCacheDataObject(const QString& key, const QVariant& dataObject);
QVariant cacheDataObject(const QString& key) const;
void addCommandObject(RimCommandObject* commandObject);
void executeCommandObjects();
private:
void onProjectOpenedOrClosed();
void setWindowCaptionFromAppState();
@ -168,4 +172,6 @@ private:
cvf::ref<cvf::Font> m_standardFont;
QMap<QString, QVariant> m_sessionCache; // Session cache used to store username/passwords per session
std::list<RimCommandObject*> m_commandQueue;
};

View File

@ -42,6 +42,7 @@ ${CEE_CURRENT_LIST_DIR}RimUiTreeView.h
${CEE_CURRENT_LIST_DIR}RimReservoirCellResultsCacher.h
${CEE_CURRENT_LIST_DIR}RimStatisticsCaseEvaluator.h
${CEE_CURRENT_LIST_DIR}RimMimeData.h
${CEE_CURRENT_LIST_DIR}RimCommandObject.h
)
set (SOURCE_GROUP_SOURCE_FILES
@ -82,6 +83,7 @@ ${CEE_CURRENT_LIST_DIR}RimUiTreeView.cpp
${CEE_CURRENT_LIST_DIR}RimReservoirCellResultsCacher.cpp
${CEE_CURRENT_LIST_DIR}RimStatisticsCaseEvaluator.cpp
${CEE_CURRENT_LIST_DIR}RimMimeData.cpp
${CEE_CURRENT_LIST_DIR}RimCommandObject.cpp
)
list(APPEND CODE_HEADER_FILES

View File

@ -0,0 +1,126 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2012 Statoil ASA, Ceetron AS
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimCommandObject.h"
#include "RiaApplication.h"
#include "RimCalcScript.h"
#include "cafPdmUiTextEditor.h"
#include "cafPdmDocument.h"
#include <QFile>
CAF_PDM_SOURCE_INIT(RimCommandObject, "RimCommandObject");
CAF_PDM_SOURCE_INIT(RimCommandExecuteScript, "RimCommandExecuteScript");
//------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCommandObject::RimCommandObject()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCommandObject::~RimCommandObject()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCommandExecuteScript::RimCommandExecuteScript()
{
CAF_PDM_InitField(&scriptText, "ScriptText", QString(), "ScriptText", "", "" ,"");
scriptText.setUiEditorTypeName(caf::PdmUiTextEditor::uiEditorTypeName());
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimCommandExecuteScript::~RimCommandExecuteScript()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCommandExecuteScript::redo()
{
RiaApplication* app = RiaApplication::instance();
QString octavePath = app->octavePath();
if (!octavePath.isEmpty())
{
// http://www.gnu.org/software/octave/doc/interpreter/Command-Line-Options.html#Command-Line-Options
QStringList arguments;
arguments.append("--path");
arguments << QApplication::applicationDirPath();
arguments.append("-q");
arguments.append("--eval");
arguments << this->scriptText();
RiaApplication::instance()->launchProcess(octavePath, arguments);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCommandExecuteScript::undo()
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimCommandFactory::createCommandObjects(const caf::PdmObjectGroup& selectedObjects, std::vector<RimCommandObject*>& commandObjects)
{
for (size_t i = 0; i < selectedObjects.objects.size(); i++)
{
caf::PdmObject* pdmObject = selectedObjects.objects[i];
if (dynamic_cast<RimCalcScript*>(pdmObject))
{
RimCalcScript* calcScript = dynamic_cast<RimCalcScript*>(pdmObject);
QFile file(calcScript->absolutePath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
QByteArray byteArray = file.readAll();
QString scriptText(byteArray);
RimCommandExecuteScript* command = new RimCommandExecuteScript;
command->scriptText = scriptText;
commandObjects.push_back(command);
}
}
}
}

View File

@ -0,0 +1,62 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2011-2012 Statoil ASA, Ceetron AS
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "cvfBase.h"
#include "cvfObject.h"
#include "cafPdmField.h"
#include "cafPdmObject.h"
#include <QString>
//==================================================================================================
//
//
//==================================================================================================
class RimCommandObject : public caf::PdmObject
{
CAF_PDM_HEADER_INIT;
public:
RimCommandObject();
virtual ~RimCommandObject();
virtual void redo() {};
virtual void undo() {};
};
class RimCommandExecuteScript : public RimCommandObject
{
CAF_PDM_HEADER_INIT;
public:
RimCommandExecuteScript();
virtual ~RimCommandExecuteScript();
caf::PdmField<QString> scriptText;
virtual void redo();
virtual void undo();
};
class RimCommandFactory
{
public:
static void createCommandObjects(const caf::PdmObjectGroup& selectedObjects, std::vector<RimCommandObject*>& commandObjects);
};

View File

@ -75,6 +75,9 @@ RimProject::RimProject(void)
wellPathImport = new RimWellPathImport();
wellPathImport.setUiHidden(true);
CAF_PDM_InitFieldNoDefault(&commandObjects, "CommandObjects", "CommandObjects", "", "", "");
//wellPathImport.setUiHidden(true);
CAF_PDM_InitFieldNoDefault(&currentModelIndexPath, "TreeViewCurrentModelIndexPath", "", "", "", "");
currentModelIndexPath.setUiHidden(true);

View File

@ -20,6 +20,7 @@
#include "cafPdmDocument.h"
#include "RimWellPathImport.h"
#include "RimCommandObject.h"
class RimOilField;
class RimCase;
@ -28,7 +29,6 @@ class RimScriptCollection;
class RimIdenticalGridCaseGroup;
class RigMainGrid;
class RigCaseData;
class RimWellPathCollection;
//==================================================================================================
///
@ -45,6 +45,7 @@ public:
caf::PdmPointersField<RimOilField*> oilFields;
caf::PdmField<RimScriptCollection*> scriptCollection;
caf::PdmField<RimWellPathImport*> wellPathImport;
caf::PdmPointersField<RimCommandObject*> commandObjects;
caf::PdmField<QString> treeViewState;
caf::PdmField<QString> currentModelIndexPath;
caf::PdmField<int> nextValidCaseId; // Unique case ID within a project, used to identify a case from Octave scripts

View File

@ -1084,3 +1084,21 @@ void RimUiTreeModelPdm::deleteAllWellPaths(const QModelIndex& itemIndex)
clearClipboard();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimUiTreeModelPdm::populateObjectGroupFromModelIndexList(const QModelIndexList& modelIndexList, caf::PdmObjectGroup* objectGroup)
{
CVF_ASSERT(objectGroup);
for (int i = 0; i < modelIndexList.size(); i++)
{
caf::PdmUiTreeItem* uiItem = UiTreeModelPdm::getTreeItemFromIndex(modelIndexList.at(i));
if (uiItem && uiItem->dataObject() && uiItem->dataObject().p())
{
objectGroup->addObject(uiItem->dataObject().p());
}
}
}

View File

@ -64,6 +64,7 @@ public:
void addToParentAndBuildUiItems(caf::PdmUiTreeItem* parentTreeItem, int position, caf::PdmObject* pdmObject);
void populateObjectGroupFromModelIndexList(const QModelIndexList& modelIndexList, caf::PdmObjectGroup* objectGroup);
void addObjects(const QModelIndex& itemIndex, caf::PdmObjectGroup& pdmObjects);
void moveObjects(const QModelIndex& itemIndex, caf::PdmObjectGroup& pdmObjects);

View File

@ -560,7 +560,6 @@ void RimUiTreeView::slotExecuteScript()
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@ -574,6 +573,8 @@ void RimUiTreeView::slotExecuteScriptForSelectedCases()
QModelIndex mi = RimUiTreeView::getModelIndexFromString(model(), encodedModelIndex);
RimUiTreeModelPdm* myModel = dynamic_cast<RimUiTreeModelPdm*>(model());
if (!myModel) return;
caf::PdmUiTreeItem* uiItem = myModel->getTreeItemFromIndex(mi);
if (uiItem)
{
@ -618,7 +619,8 @@ void RimUiTreeView::slotExecuteScriptForSelectedCases()
caf::PdmObjectGroup group;
QModelIndexList mil = m->selectedRows();
populateObjectGroupFromModelIndexList(mil, &group);
myModel->populateObjectGroupFromModelIndexList(mil, &group);
std::vector<caf::PdmPointer<RimCase> > typedObjects;
group.objectsByType(&typedObjects);
@ -909,7 +911,7 @@ void RimUiTreeView::slotCloseCase()
caf::PdmObjectGroup group;
QModelIndexList mil = m->selectedRows();
populateObjectGroupFromModelIndexList(mil, &group);
myModel->populateObjectGroupFromModelIndexList(mil, &group);
std::vector<caf::PdmPointer<RimCase> > typedObjects;
group.objectsByType(&typedObjects);
@ -1048,7 +1050,7 @@ void RimUiTreeView::createPdmObjectsFromClipboard(caf::PdmObjectGroup* objectGro
if (!mdWithIndexes) return;
QModelIndexList indexList = mdWithIndexes->indexes();
populateObjectGroupFromModelIndexList(indexList, objectGroup);
myModel->populateObjectGroupFromModelIndexList(indexList, objectGroup);
}
//--------------------------------------------------------------------------------------------------
@ -1536,27 +1538,6 @@ void RimUiTreeView::slotToggleItemsOff()
executeSelectionToggleOperation(TOGGLE_OFF);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimUiTreeView::populateObjectGroupFromModelIndexList(const QModelIndexList& modelIndexList, caf::PdmObjectGroup* objectGroup)
{
CVF_ASSERT(objectGroup);
RimUiTreeModelPdm* myModel = dynamic_cast<RimUiTreeModelPdm*>(model());
if (!myModel) return;
for (int i = 0; i < modelIndexList.size(); i++)
{
caf::PdmUiTreeItem* uiItem = myModel->getTreeItemFromIndex(modelIndexList.at(i));
if (uiItem && uiItem->dataObject() && uiItem->dataObject().p())
{
objectGroup->addObject(uiItem->dataObject().p());
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -47,8 +47,6 @@ public:
void applyTreeViewStateFromString(const QString& treeViewState);
void storeTreeViewStateToString(QString& treeViewState);
void populateObjectGroupFromModelIndexList(const QModelIndexList& modelIndexList, caf::PdmObjectGroup* objectGroup);
static QModelIndex getModelIndexFromString(QAbstractItemModel* model, const QString& currentIndexString);
static void encodeStringFromModelIndex(const QModelIndex mi, QString& currentIndexString);

View File

@ -952,7 +952,12 @@ public:
{
if (m_currentReservoir->reservoirViews[i])
{
m_currentReservoir->reservoirViews[i]->updateCurrentTimeStepAndRedraw();
// As new result might have been introduced, update all editors connected
m_currentReservoir->reservoirViews[i]->cellResult->updateConnectedEditors();
// It is usually not needed to create new display model, but if any derived geometry based on generated data (from Octave)
// a full display model rebuild is required
m_currentReservoir->reservoirViews[i]->scheduleCreateDisplayModelAndRedraw();
}
}
}

View File

@ -59,6 +59,7 @@
#include "RimCellRangeFilterCollection.h"
#include "Rim3dOverlayInfoConfig.h"
#include "RiuWellImportWizard.h"
#include "RimCalcScript.h"
@ -198,6 +199,8 @@ void RiuMainWindow::createActions()
m_snapshotToClipboard = new QAction(QIcon(":/SnapShot.png"), "Copy Snapshot To Clipboard", this);
m_snapshotAllViewsToFile = new QAction(QIcon(":/SnapShotSaveViews.png"), "Snapshot All Views To File", this);
m_createCommandObject = new QAction("Create Command Object", this);
m_saveProjectAction = new QAction(QIcon(":/Save.png"), "&Save Project", this);
m_saveProjectAsAction = new QAction(QIcon(":/Save.png"), "Save Project &As", this);
@ -221,6 +224,8 @@ void RiuMainWindow::createActions()
connect(m_snapshotToClipboard, SIGNAL(triggered()), SLOT(slotSnapshotToClipboard()));
connect(m_snapshotAllViewsToFile, SIGNAL(triggered()), SLOT(slotSnapshotAllViewsToFile()));
connect(m_createCommandObject, SIGNAL(triggered()), SLOT(slotCreateCommandObject()));
connect(m_saveProjectAction, SIGNAL(triggered()), SLOT(slotSaveProject()));
connect(m_saveProjectAsAction, SIGNAL(triggered()), SLOT(slotSaveProjectAs()));
@ -355,9 +360,13 @@ void RiuMainWindow::createMenus()
debugMenu->addAction(m_mockResultsModelAction);
debugMenu->addAction(m_mockLargeResultsModelAction);
debugMenu->addAction(m_mockInputModelAction);
debugMenu->addSeparator();
debugMenu->addAction(m_createCommandObject);
connect(debugMenu, SIGNAL(aboutToShow()), SLOT(slotRefreshDebugActions()));
// Windows menu
m_windowMenu = menuBar()->addMenu("&Windows");
connect(m_windowMenu, SIGNAL(aboutToShow()), SLOT(slotBuildWindowActions()));
@ -1581,7 +1590,7 @@ void RiuMainWindow::selectedCases(std::vector<RimCase*>& cases)
QModelIndexList selectedModelIndexes = m_treeView->selectionModel()->selectedIndexes();
caf::PdmObjectGroup group;
m_treeView->populateObjectGroupFromModelIndexList(selectedModelIndexes, &group);
m_treeModelPdm->populateObjectGroupFromModelIndexList(selectedModelIndexes, &group);
std::vector<caf::PdmPointer<RimCase> > typedObjects;
group.objectsByType(&typedObjects);
@ -1661,3 +1670,31 @@ void RiuMainWindow::slotShowCommandLineHelp()
QString text = app->commandLineParameterHelp();
app->showFormattedTextInMessageBox(text);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiuMainWindow::slotCreateCommandObject()
{
RiaApplication* app = RiaApplication::instance();
if (!app->project()) return;
QItemSelectionModel* selectionModel = m_treeView->selectionModel();
if (selectionModel)
{
QModelIndexList selectedModelIndices = selectionModel->selectedIndexes();
caf::PdmObjectGroup selectedObjects;
m_treeModelPdm->populateObjectGroupFromModelIndexList(selectedModelIndices, &selectedObjects);
std::vector<RimCommandObject*> commandObjects;
RimCommandFactory::createCommandObjects(selectedObjects, commandObjects);
for (size_t i = 0; i < commandObjects.size(); i++)
{
app->project()->commandObjects.push_back(commandObjects[i]);
}
app->project()->updateConnectedEditors();
}
}

View File

@ -154,6 +154,8 @@ private:
QAction* m_snapshotToClipboard;
QAction* m_snapshotAllViewsToFile;
QAction* m_createCommandObject;
// Help actions
QAction* m_aboutAction;
QAction* m_commandLineHelpAction;
@ -221,6 +223,8 @@ private slots:
void slotSnapshotToClipboard();
void slotSnapshotAllViewsToFile();
void slotCreateCommandObject();
// Mock models
void slotMockModel();
void slotMockResultsModel();