From fbfba745c38e695d60e3d7a71ee29e83b2112652 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Sun, 29 Sep 2019 12:31:21 +0200 Subject: [PATCH] #4796 Summary Plot Template : Add context menu to selected summary cases --- .../CMakeLists_files.cmake | 2 + .../RicCreatePlotFromTemplateFeature.cpp | 160 ++++++++++++++++++ .../RicCreatePlotFromTemplateFeature.h | 45 +++++ .../RimContextCommandBuilder.cpp | 37 ++++ .../RimContextCommandBuilder.h | 5 + 5 files changed, 249 insertions(+) create mode 100644 ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.cpp create mode 100644 ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.h diff --git a/ApplicationCode/Commands/PlotTemplateCommands/CMakeLists_files.cmake b/ApplicationCode/Commands/PlotTemplateCommands/CMakeLists_files.cmake index 9bb7dc6b79..97c843653e 100644 --- a/ApplicationCode/Commands/PlotTemplateCommands/CMakeLists_files.cmake +++ b/ApplicationCode/Commands/PlotTemplateCommands/CMakeLists_files.cmake @@ -5,6 +5,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicCreatePlotFromSelectionFeature.h ${CMAKE_CURRENT_LIST_DIR}/RicSelectPlotTemplateUi.h ${CMAKE_CURRENT_LIST_DIR}/RicSummaryPlotTemplateTools.h ${CMAKE_CURRENT_LIST_DIR}/RicReloadPlotTemplatesFeature.h +${CMAKE_CURRENT_LIST_DIR}/RicCreatePlotFromTemplateFeature.h ) set (SOURCE_GROUP_SOURCE_FILES @@ -13,6 +14,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RicCreatePlotFromSelectionFeature.cpp ${CMAKE_CURRENT_LIST_DIR}/RicSelectPlotTemplateUi.cpp ${CMAKE_CURRENT_LIST_DIR}/RicSummaryPlotTemplateTools.cpp ${CMAKE_CURRENT_LIST_DIR}/RicReloadPlotTemplatesFeature.cpp +${CMAKE_CURRENT_LIST_DIR}/RicCreatePlotFromTemplateFeature.cpp ) list(APPEND CODE_HEADER_FILES diff --git a/ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.cpp b/ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.cpp new file mode 100644 index 0000000000..91882c1fe7 --- /dev/null +++ b/ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.cpp @@ -0,0 +1,160 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2019- Equinor ASA +// +// 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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#include "RicCreatePlotFromTemplateFeature.h" + +#include "RicSelectPlotTemplateUi.h" +#include "RicSummaryPlotTemplateTools.h" + +#include "RiaGuiApplication.h" +#include "RiaLogging.h" +#include "RiaSummaryTools.h" + +#include "RifSummaryReaderInterface.h" + +#include "PlotTemplates/RimPlotTemplateFileItem.h" +#include "RimDialogData.h" +#include "RimMainPlotCollection.h" +#include "RimProject.h" +#include "RimSummaryCase.h" +#include "RimSummaryCurve.h" +#include "RimSummaryPlot.h" +#include "RimSummaryPlotCollection.h" +#include "RimWellPath.h" + +#include "RiuPlotMainWindow.h" + +#include "cafPdmObject.h" +#include "cafPdmUiPropertyViewDialog.h" +#include "cafSelectionManager.h" + +#include +#include + +CAF_CMD_SOURCE_INIT( RicCreatePlotFromTemplateFeature, "RicCreatePlotFromTemplateFeature" ); + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool RicCreatePlotFromTemplateFeature::isCommandEnabled() +{ + return !selectedSummaryCases().empty(); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RicCreatePlotFromTemplateFeature::onActionTriggered( bool isChecked ) +{ + QString fileName = userData().toString(); + std::vector sumCases = selectedSummaryCases(); + + RimSummaryPlot* newSummaryPlot = RicSummaryPlotTemplateTools::createPlotFromTemplateFile( fileName ); + if ( newSummaryPlot ) + { + RimSummaryPlotCollection* plotColl = + RiaApplication::instance()->project()->mainPlotCollection()->summaryPlotCollection(); + + plotColl->summaryPlots.push_back( newSummaryPlot ); + newSummaryPlot->resolveReferencesRecursively(); + newSummaryPlot->initAfterReadRecursively(); + + auto summaryCurves = newSummaryPlot->summaryCurves(); + + for ( const auto& curve : summaryCurves ) + { + auto fieldHandle = curve->findField( "SummaryCase" ); + if ( fieldHandle ) + { + auto referenceString = fieldHandle->xmlCapability()->referenceString(); + auto stringList = referenceString.split( " " ); + if ( stringList.size() == 2 ) + { + QString indexAsString = stringList[1]; + + bool conversionOk = false; + auto index = indexAsString.toUInt( &conversionOk ); + + if ( conversionOk && index < sumCases.size() ) + { + auto summaryCaseY = sumCases[index]; + curve->setSummaryCaseY( summaryCaseY ); + + auto currentAddressY = curve->summaryAddressY(); + if ( summaryCaseY->summaryReader() && + !summaryCaseY->summaryReader()->hasAddress( currentAddressY ) ) + { + auto allAddresses = summaryCaseY->summaryReader()->allResultAddresses(); + + auto candidate = RicCreatePlotFromTemplateFeature::firstAddressByQuantity( currentAddressY, + allAddresses ); + if ( candidate.category() != RifEclipseSummaryAddress::SUMMARY_INVALID ) + { + curve->setSummaryAddressY( candidate ); + } + } + } + } + } + } + + // TODO: Create additional curves in selected case count is larger than template count + + plotColl->updateConnectedEditors(); + + newSummaryPlot->loadDataAndUpdate(); + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RicCreatePlotFromTemplateFeature::setupActionLook( QAction* actionToSetup ) +{ + actionToSetup->setText( "Create Plot from Template" ); + actionToSetup->setIcon( QIcon( ":/SummaryTemplate16x16.png" ) ); +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +std::vector RicCreatePlotFromTemplateFeature::selectedSummaryCases() const +{ + std::vector objects; + caf::SelectionManager::instance()->objectsByType( &objects ); + + return objects; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +RifEclipseSummaryAddress + RicCreatePlotFromTemplateFeature::firstAddressByQuantity( const RifEclipseSummaryAddress& sourceAddress, + const std::set& allAddresses ) +{ + for ( const auto& a : allAddresses ) + { + if ( sourceAddress.quantityName() == a.quantityName() ) + { + return a; + } + } + + return RifEclipseSummaryAddress(); +} diff --git a/ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.h b/ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.h new file mode 100644 index 0000000000..6a21ee19c0 --- /dev/null +++ b/ApplicationCode/Commands/PlotTemplateCommands/RicCreatePlotFromTemplateFeature.h @@ -0,0 +1,45 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Copyright (C) 2019- Equinor ASA +// +// 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 +// for more details. +// +///////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include "RifEclipseSummaryAddress.h" + +#include "cafCmdFeature.h" + +#include + +class RimSummaryCase; + +//================================================================================================== +/// +//================================================================================================== +class RicCreatePlotFromTemplateFeature : public caf::CmdFeature +{ + CAF_CMD_HEADER_INIT; + +protected: + bool isCommandEnabled() override; + void onActionTriggered( bool isChecked ) override; + void setupActionLook( QAction* actionToSetup ) override; + +private: + std::vector selectedSummaryCases() const; + static RifEclipseSummaryAddress firstAddressByQuantity( const RifEclipseSummaryAddress& sourceAddress, + const std::set& allAddresses ); +}; diff --git a/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.cpp b/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.cpp index 2b65d88cd3..6aee7d7d0a 100644 --- a/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.cpp +++ b/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.cpp @@ -882,6 +882,10 @@ caf::CmdFeatureMenuBuilder RimContextCommandBuilder::commandsFromSelection() menuBuilder << "RicAsciiExportSummaryPlotFeature"; } } + else if ( dynamic_cast( uiItem ) ) + { + appendPlotTemplateMenus( menuBuilder ); + } else if ( dynamic_cast( uiItem ) ) { menuBuilder << "RicAsciiExportWellLogPlotFeature"; @@ -1031,6 +1035,19 @@ void RimContextCommandBuilder::createExecuteScriptForCasesFeatureMenu( caf::CmdF } } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimContextCommandBuilder::appendPlotTemplateMenus( caf::CmdFeatureMenuBuilder& menuBuilder ) +{ + RiaApplication* app = RiaApplication::instance(); + RimProject* proj = app->project(); + if ( proj && proj->rootPlotTemlateItem() ) + { + appendPlotTemplateItems( menuBuilder, proj->rootPlotTemlateItem() ); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- @@ -1068,6 +1085,26 @@ void RimContextCommandBuilder::appendScriptItems( caf::CmdFeatureMenuBuilder& me menuBuilder.subMenuEnd(); } +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void RimContextCommandBuilder::appendPlotTemplateItems( caf::CmdFeatureMenuBuilder& menuBuilder, + RimPlotTemplateFolderItem* plotTemplateRoot ) +{ + for ( const auto& fileItem : plotTemplateRoot->fileNames() ) + { + QString menuText = fileItem->uiName(); + menuBuilder.addCmdFeatureWithUserData( "RicCreatePlotFromTemplateFeature", + menuText, + QVariant( fileItem->absoluteFilePath() ) ); + } + + for ( const auto& folder : plotTemplateRoot->subFolders() ) + { + appendPlotTemplateItems( menuBuilder, folder ); + } +} + //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.h b/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.h index d331310d90..fe2f1d436c 100644 --- a/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.h +++ b/ApplicationCode/ProjectDataModel/RimContextCommandBuilder.h @@ -32,6 +32,7 @@ class QStringList; class QMenu; class RimWellPath; class RimScriptCollection; +class RimPlotTemplateFolderItem; //================================================================================================== /// @@ -45,7 +46,11 @@ public: private: static std::vector allWellPaths(); static void createExecuteScriptForCasesFeatureMenu( caf::CmdFeatureMenuBuilder& menuBuilder ); + static void appendPlotTemplateMenus( caf::CmdFeatureMenuBuilder& menuBuilder ); + static void appendScriptItems( caf::CmdFeatureMenuBuilder& menuBuilder, RimScriptCollection* scriptCollection ); + static void appendPlotTemplateItems( caf::CmdFeatureMenuBuilder& menuBuilder, + RimPlotTemplateFolderItem* plotTemplateRoot ); static int appendImportMenu( caf::CmdFeatureMenuBuilder& menuBuilder, bool addSeparatorBeforeMenu = false ); static int appendCreateCompletions( caf::CmdFeatureMenuBuilder& menuBuilder, bool addSeparatorBeforeMenu = false );