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:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user