mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Added context menu used to execute a script for multiple cases
For all selected cases, set current case and launch an external Octave process using this current case p4#: 21742
This commit is contained in:
@@ -1038,9 +1038,16 @@ void RiaApplication::slotWorkerProcessFinished(int exitCode, QProcess::ExitStatu
|
||||
return;
|
||||
}
|
||||
|
||||
//MFLog::info("Simulation completed successfully.");
|
||||
|
||||
//MFMainWindow::instance()->slotLoadResultsFromSimulationFolder();
|
||||
// If multiple cases are present, invoke launchProcess() which will set next current case, and run script on this case
|
||||
if (m_currentCaseIds.size() > 0)
|
||||
{
|
||||
launchProcess(m_currentProgram, m_currentArguments);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Disable concept of current case
|
||||
m_socketServer->setCurrentCaseId(-1);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -1050,6 +1057,20 @@ bool RiaApplication::launchProcess(const QString& program, const QStringList& ar
|
||||
{
|
||||
if (m_workerProcess == NULL)
|
||||
{
|
||||
// If multiple cases are present, pop the first case ID from the list and set as current case
|
||||
if (m_currentCaseIds.size() > 0)
|
||||
{
|
||||
int nextCaseId = m_currentCaseIds.front();
|
||||
m_currentCaseIds.pop_front();
|
||||
|
||||
m_socketServer->setCurrentCaseId(nextCaseId);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Disable current case concept
|
||||
m_socketServer->setCurrentCaseId(-1);
|
||||
}
|
||||
|
||||
m_workerProcess = new caf::UiProcess(this);
|
||||
connect(m_workerProcess, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(slotWorkerProcessFinished(int, QProcess::ExitStatus)));
|
||||
|
||||
@@ -1077,6 +1098,20 @@ bool RiaApplication::launchProcess(const QString& program, const QStringList& ar
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RiaApplication::launchProcessForMultipleCases(const QString& program, const QStringList& arguments, const std::vector<int>& caseIds)
|
||||
{
|
||||
m_currentCaseIds.clear();
|
||||
std::copy( caseIds.begin(), caseIds.end(), std::back_inserter( m_currentCaseIds ) );
|
||||
|
||||
m_currentProgram = program;
|
||||
m_currentArguments = arguments;
|
||||
|
||||
return launchProcess(m_currentProgram, m_currentArguments);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Read fields of a Pdm object using QSettings
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -1580,3 +1615,4 @@ RimProject* RiaApplication::project()
|
||||
{
|
||||
return m_project;
|
||||
}
|
||||
|
||||
|
||||
@@ -115,6 +115,7 @@ public:
|
||||
QString octavePath() const;
|
||||
|
||||
bool launchProcess(const QString& program, const QStringList& arguments);
|
||||
bool launchProcessForMultipleCases(const QString& program, const QStringList& arguments, const std::vector<int>& caseIds);
|
||||
void terminateProcess();
|
||||
|
||||
RiaPreferences* preferences();
|
||||
@@ -142,6 +143,12 @@ private:
|
||||
|
||||
caf::UiProcess* m_workerProcess;
|
||||
|
||||
// Execute for all settings
|
||||
std::list<int> m_currentCaseIds;
|
||||
QString m_currentProgram;
|
||||
QStringList m_currentArguments;
|
||||
|
||||
|
||||
RiaPreferences* m_preferences;
|
||||
|
||||
std::map<QString, QString> m_fileDialogDefaultDirectories;
|
||||
|
||||
@@ -56,11 +56,13 @@
|
||||
#include "RimWell.h"
|
||||
#include "RimCellEdgeResultSlot.h"
|
||||
#include "RimWellCollection.h"
|
||||
#include "RimWellPathCollection.h"
|
||||
#include "RimReservoirCellResultsCacher.h"
|
||||
#include "Rim3dOverlayInfoConfig.h"
|
||||
|
||||
#include "cafPdmFieldCvfColor.h"
|
||||
#include "cafPdmFieldCvfMat4d.h"
|
||||
#include "RimProject.h"
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
@@ -177,7 +179,7 @@ void RimUiTreeView::contextMenuEvent(QContextMenuEvent* event)
|
||||
}
|
||||
else if (dynamic_cast<RimStatisticsCaseCollection*>(uiItem->dataObject().p()))
|
||||
{
|
||||
menu.addAction(QString("New Statistcs Case"), this, SLOT(slotNewStatisticsCase()));
|
||||
menu.addAction(QString("New Statistics Case"), this, SLOT(slotNewStatisticsCase()));
|
||||
}
|
||||
else if (dynamic_cast<RimStatisticsCase*>(uiItem->dataObject().p()))
|
||||
{
|
||||
@@ -216,6 +218,40 @@ void RimUiTreeView::contextMenuEvent(QContextMenuEvent* event)
|
||||
menu.addAction(QString("Delete Script Path"), this, SLOT(slotDeleteScriptPath()));
|
||||
}
|
||||
|
||||
// Execute script on selection of cases
|
||||
RiuMainWindow* ruiMainWindow = RiuMainWindow::instance();
|
||||
if (ruiMainWindow)
|
||||
{
|
||||
std::vector<RimCase*> cases;
|
||||
ruiMainWindow->selectedCases(cases);
|
||||
|
||||
if (cases.size() > 0)
|
||||
{
|
||||
QMenu* executeMenu = menu.addMenu("Execute script");
|
||||
|
||||
RiaApplication* app = RiaApplication::instance();
|
||||
RimProject* proj = app->project();
|
||||
if (proj && proj->scriptCollection())
|
||||
{
|
||||
RimScriptCollection* rootScriptCollection = proj->scriptCollection();
|
||||
|
||||
// Root script collection holds a list of subdirectories of user defined script folders
|
||||
for (size_t i = 0; i < rootScriptCollection->subDirectories.size(); i++)
|
||||
{
|
||||
RimScriptCollection* subDir = rootScriptCollection->subDirectories[i];
|
||||
|
||||
if (subDir)
|
||||
{
|
||||
appendScriptItems(executeMenu, subDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
menu.addSeparator();
|
||||
menu.addMenu(executeMenu);
|
||||
}
|
||||
}
|
||||
|
||||
appendToggleItemActions(menu);
|
||||
menu.exec(event->globalPos());
|
||||
}
|
||||
@@ -515,6 +551,83 @@ void RimUiTreeView::slotExecuteScript()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimUiTreeView::slotExecuteScriptForSelectedCases()
|
||||
{
|
||||
QAction* action = qobject_cast<QAction*>(sender());
|
||||
|
||||
if (!action) return;
|
||||
|
||||
QString encodedModelIndex = action->data().toString();
|
||||
QModelIndex mi = RimUiTreeView::getModelIndexFromString(model(), encodedModelIndex);
|
||||
|
||||
RimUiTreeModelPdm* myModel = dynamic_cast<RimUiTreeModelPdm*>(model());
|
||||
caf::PdmUiTreeItem* uiItem = myModel->getTreeItemFromIndex(mi);
|
||||
if (uiItem)
|
||||
{
|
||||
RimCalcScript* calcScript = dynamic_cast<RimCalcScript*>(uiItem->dataObject().p());
|
||||
if (!calcScript) return;
|
||||
|
||||
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
|
||||
|
||||
// -p path
|
||||
// Add path to the head of the search path for function files. The value of path specified on the command line
|
||||
// will override any value of OCTAVE_PATH found in the environment, but not any commands in the system or
|
||||
// user startup files that set the internal load path through one of the path functions.
|
||||
|
||||
// -q
|
||||
// Don't print the usual greeting and version message at startup.
|
||||
|
||||
|
||||
// TODO: Must rename RimCalcScript::absolutePath to absoluteFileName, as the code below is confusing
|
||||
// absolutePath() is a function in QFileInfo
|
||||
QFileInfo fi(calcScript->absolutePath());
|
||||
QString octaveFunctionSearchPath = fi.absolutePath();
|
||||
|
||||
QStringList arguments;
|
||||
arguments.append("--path");
|
||||
arguments << octaveFunctionSearchPath;
|
||||
|
||||
arguments.append("-q");
|
||||
arguments << calcScript->absolutePath();
|
||||
|
||||
// Get case ID from selected cases in selection model
|
||||
std::vector<int> caseIdsInSelection;
|
||||
{
|
||||
QItemSelectionModel* m = selectionModel();
|
||||
CVF_ASSERT(m);
|
||||
|
||||
caf::PdmObjectGroup group;
|
||||
|
||||
QModelIndexList mil = m->selectedRows();
|
||||
populateObjectGroupFromModelIndexList(mil, &group);
|
||||
|
||||
std::vector<caf::PdmPointer<RimCase> > typedObjects;
|
||||
group.objectsByType(&typedObjects);
|
||||
|
||||
for (size_t i = 0; i < typedObjects.size(); i++)
|
||||
{
|
||||
RimCase* rimReservoir = typedObjects[i];
|
||||
caseIdsInSelection.push_back(rimReservoir->caseId);
|
||||
}
|
||||
}
|
||||
|
||||
if (caseIdsInSelection.size() > 0)
|
||||
{
|
||||
RiaApplication::instance()->launchProcessForMultipleCases(octavePath, arguments, caseIdsInSelection);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
@@ -1221,7 +1334,7 @@ void RimUiTreeView::storeTreeViewStateToString(QString& treeViewState)
|
||||
/// Find index based of an encode QString <row> <column>;<row> <column>;...;<row> <column>
|
||||
/// Set the decoded index as current index in the QAbstractItemView
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimUiTreeView::applyCurrentIndexFromString(QAbstractItemView& itemView, const QString& currentIndexString)
|
||||
QModelIndex RimUiTreeView::getModelIndexFromString(QAbstractItemModel* model, const QString& currentIndexString)
|
||||
{
|
||||
QStringList modelIndexStringList = currentIndexString.split(";");
|
||||
|
||||
@@ -1236,33 +1349,33 @@ void RimUiTreeView::applyCurrentIndexFromString(QAbstractItemView& itemView, con
|
||||
int row = items[0].toInt();
|
||||
int col = items[1].toInt();
|
||||
|
||||
mi = itemView.model()->index(row, col, mi);
|
||||
mi = model->index(row, col, mi);
|
||||
}
|
||||
|
||||
if (mi.isValid())
|
||||
{
|
||||
itemView.setCurrentIndex(mi);
|
||||
}
|
||||
return mi;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// Store path to current index in item view using follwoing encoding into a QString <row> <column>;<row> <column>;...;<row> <column>
|
||||
/// Store path to model index in item view using follwoing encoding into a QString <row> <column>;<row> <column>;...;<row> <column>
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimUiTreeView::storeCurrentIndexToString(const QAbstractItemView& itemView, QString& currentIndexString)
|
||||
void RimUiTreeView::encodeStringFromModelIndex(const QModelIndex mi, QString& encodedModelIndex)
|
||||
{
|
||||
QModelIndex mi = itemView.currentIndex();
|
||||
if (!mi.isValid()) return;
|
||||
|
||||
QString path = QString("%1 %2").arg(mi.row()).arg(mi.column());
|
||||
mi = mi.parent();
|
||||
QModelIndex localModelIdx = mi;
|
||||
|
||||
while (mi.isValid())
|
||||
while (localModelIdx.isValid())
|
||||
{
|
||||
path = QString("%1 %2;").arg(mi.row()).arg(mi.column()) + path;
|
||||
mi = mi.parent();
|
||||
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();
|
||||
}
|
||||
|
||||
currentIndexString = path;
|
||||
}
|
||||
|
||||
|
||||
@@ -1478,3 +1591,42 @@ void RimUiTreeView::populateObjectGroupFromModelIndexList(const QModelIndexList&
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimUiTreeView::appendScriptItems(QMenu* menu, RimScriptCollection* scriptCollection)
|
||||
{
|
||||
CVF_ASSERT(menu);
|
||||
|
||||
QDir dir(scriptCollection->directory);
|
||||
QMenu* subMenu = menu->addMenu(dir.dirName());
|
||||
|
||||
for (size_t i = 0; i < scriptCollection->calcScripts.size(); i++)
|
||||
{
|
||||
RimCalcScript* calcScript = scriptCollection->calcScripts[i];
|
||||
QFileInfo fi(calcScript->absolutePath());
|
||||
|
||||
QString menuText = fi.baseName();
|
||||
QAction* scriptAction = subMenu->addAction(menuText, this, SLOT(slotExecuteScriptForSelectedCases()));
|
||||
|
||||
QModelIndex mi;
|
||||
RimUiTreeModelPdm* myModel = dynamic_cast<RimUiTreeModelPdm*>(model());
|
||||
if (myModel)
|
||||
{
|
||||
mi = myModel->getModelIndexFromPdmObject(calcScript);
|
||||
}
|
||||
|
||||
QString encodedModelIndex;
|
||||
RimUiTreeView::encodeStringFromModelIndex(mi, encodedModelIndex);
|
||||
|
||||
scriptAction->setData(QVariant(encodedModelIndex));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < scriptCollection->subDirectories.size(); i++)
|
||||
{
|
||||
RimScriptCollection* subDir = scriptCollection->subDirectories[i];
|
||||
|
||||
appendScriptItems(subMenu, subDir);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
class QItemSelection;
|
||||
class RimIdenticalGridCaseGroup;
|
||||
class RimScriptCollection;
|
||||
|
||||
namespace caf {
|
||||
class PdmObjectGroup;
|
||||
@@ -48,8 +49,8 @@ public:
|
||||
|
||||
void populateObjectGroupFromModelIndexList(const QModelIndexList& modelIndexList, caf::PdmObjectGroup* objectGroup);
|
||||
|
||||
static void applyCurrentIndexFromString(QAbstractItemView& itemView, const QString& currentIndexString);
|
||||
static void storeCurrentIndexToString(const QAbstractItemView& itemView, QString& currentIndexString);
|
||||
static QModelIndex getModelIndexFromString(QAbstractItemModel* model, const QString& currentIndexString);
|
||||
static void encodeStringFromModelIndex(const QModelIndex mi, QString& currentIndexString);
|
||||
|
||||
protected:
|
||||
void contextMenuEvent(QContextMenuEvent* event);
|
||||
@@ -70,6 +71,7 @@ private slots:
|
||||
void slotEditScript();
|
||||
void slotNewScript();
|
||||
void slotExecuteScript();
|
||||
void slotExecuteScriptForSelectedCases();
|
||||
|
||||
void slotAddView();
|
||||
void slotDeleteView();
|
||||
@@ -128,6 +130,8 @@ private:
|
||||
void appendToggleItemActions(QMenu& contextMenu);
|
||||
|
||||
void setExpandedUpToRoot(const QModelIndex& itemIndex);
|
||||
|
||||
void appendScriptItems(QMenu* menu, RimScriptCollection* scriptCollection);
|
||||
private:
|
||||
QAction* m_pasteAction;
|
||||
};
|
||||
|
||||
@@ -74,7 +74,7 @@ public:
|
||||
static QString commandName () { return QString("GetCurrentCase"); }
|
||||
virtual bool interpretCommand(RiaSocketServer* server, const QList<QByteArray>& args, QDataStream& socketStream)
|
||||
{
|
||||
qint64 caseId = -1;
|
||||
qint64 caseId = server->currentCaseId();
|
||||
QString caseName;
|
||||
QString caseType;
|
||||
qint64 caseGroupId = -1;
|
||||
|
||||
@@ -1467,11 +1467,13 @@ void RiuMainWindow::storeTreeViewState()
|
||||
QString treeViewState;
|
||||
m_treeView->storeTreeViewStateToString(treeViewState);
|
||||
|
||||
QString currentIndexString;
|
||||
RimUiTreeView::storeCurrentIndexToString(*m_treeView, currentIndexString);
|
||||
QModelIndex mi = m_treeView->currentIndex();
|
||||
|
||||
QString encodedModelIndexString;
|
||||
RimUiTreeView::encodeStringFromModelIndex(mi, encodedModelIndexString);
|
||||
|
||||
RiaApplication::instance()->project()->treeViewState = treeViewState;
|
||||
RiaApplication::instance()->project()->currentModelIndexPath = currentIndexString;
|
||||
RiaApplication::instance()->project()->currentModelIndexPath = encodedModelIndexString;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1492,7 +1494,8 @@ void RiuMainWindow::restoreTreeViewState()
|
||||
QString currentIndexString = RiaApplication::instance()->project()->currentModelIndexPath;
|
||||
if (!currentIndexString.isEmpty())
|
||||
{
|
||||
RimUiTreeView::applyCurrentIndexFromString(*m_treeView, currentIndexString);
|
||||
QModelIndex mi = RimUiTreeView::getModelIndexFromString(m_treeView->model(), currentIndexString);
|
||||
m_treeView->setCurrentIndex(mi);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1545,7 +1548,7 @@ void RiuMainWindow::updateScaleValue()
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
/// TODO: This function will be moved to a class responsible for handling the application selection concept
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RiuMainWindow::selectedCases(std::vector<RimCase*>& cases)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user