///////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 2016- Statoil 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 "RicImportSummaryCasesFeature.h" #include "RiaApplication.h" #include "RiaLogging.h" #include "RiaPreferences.h" #include "RicFileHierarchyDialog.h" #include "RifSummaryCaseRestartSelector.h" #include "RimGridSummaryCase.h" #include "RimMainPlotCollection.h" #include "RimOilField.h" #include "RimProject.h" #include "RimSummaryCase.h" #include "RimSummaryCaseCollection.h" #include "RimSummaryCaseMainCollection.h" #include "RimSummaryPlotCollection.h" #include "RiuPlotMainWindow.h" #include "RiuMainWindow.h" #include "SummaryPlotCommands/RicNewSummaryPlotFeature.h" #include "cafProgressInfo.h" #include "cafSelectionManagerTools.h" #include #include #include CAF_CMD_SOURCE_INIT(RicImportSummaryCasesFeature, "RicImportSummaryCasesFeature"); //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QString RicImportSummaryCasesFeature::m_pathFilter = "*"; QString RicImportSummaryCasesFeature::m_fileNameFilter = "*"; //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RicImportSummaryCasesFeature::isCommandEnabled() { return true; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RicImportSummaryCasesFeature::onActionTriggered(bool isChecked) { RiaApplication* app = RiaApplication::instance(); QStringList fileNames = runRecursiveSummaryCaseFileSearchDialog("Import Summary Cases"); std::vector cases; if (!fileNames.isEmpty()) createSummaryCasesFromFiles(fileNames, &cases); addSummaryCases(cases); addCasesToGroupIfRelevant(cases); for (const auto& rimCase : cases) RiaApplication::instance()->addToRecentFiles(rimCase->summaryHeaderFilename()); RiuPlotMainWindow* mainPlotWindow = app->getOrCreateAndShowMainPlotWindow(); if (mainPlotWindow && !cases.empty()) { mainPlotWindow->selectAsCurrentItem(cases.back()); mainPlotWindow->updateSummaryPlotToolBar(); } std::vector allCases; app->project()->allCases(allCases); if (allCases.size() == 0) { RiuMainWindow::instance()->close(); } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RicImportSummaryCasesFeature::setupActionLook(QAction* actionToSetup) { actionToSetup->setIcon(QIcon(":/SummaryCases16x16.png")); actionToSetup->setText("Import Summary Cases Recursively"); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RicImportSummaryCasesFeature::createAndAddSummaryCasesFromFiles(const QStringList& fileNames, std::vector* newCases) { RiaApplication* app = RiaApplication::instance(); std::vector temp; std::vector* cases = newCases ? newCases : &temp; if (createSummaryCasesFromFiles(fileNames, cases)) { addSummaryCases(*cases); RiuPlotMainWindow* mainPlotWindow = app->getOrCreateAndShowMainPlotWindow(); if (mainPlotWindow && !cases->empty()) { mainPlotWindow->selectAsCurrentItem(cases->back()); mainPlotWindow->updateSummaryPlotToolBar(); // Close main window if there are no eclipse cases imported std::vector allCases; app->project()->allCases(allCases); if (allCases.size() == 0) { RiuMainWindow::instance()->close(); } } return true; } return false; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- bool RicImportSummaryCasesFeature::createSummaryCasesFromFiles(const QStringList& fileNames, std::vector* newCases, bool ensembleOrGroup) { RiaApplication* app = RiaApplication::instance(); RimProject* proj = app->project(); RimSummaryCaseMainCollection* sumCaseColl = proj->activeOilField() ? proj->activeOilField()->summaryCaseMainCollection() : nullptr; if (newCases) newCases->clear(); if (!sumCaseColl) return false; RifSummaryCaseRestartSelector fileSelector; fileSelector.setEnsembleOrGroupMode(ensembleOrGroup); fileSelector.determineFilesToImportFromSummaryFiles(fileNames); std::vector importFileInfos = fileSelector.summaryFileInfos(); if (!importFileInfos.empty()) { std::vector sumCases = sumCaseColl->createSummaryCasesFromFileInfos(importFileInfos, true); if (newCases) newCases->insert(newCases->end(), sumCases.begin(), sumCases.end()); } if (fileSelector.foundErrors()) { QString errorMessage = fileSelector.createCombinedErrorMessage(); RiaLogging::error(errorMessage); QMessageBox::warning(nullptr, QString("Problem Importing Summary Case File(s)"), errorMessage); } return true; } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RicImportSummaryCasesFeature::addSummaryCases(const std::vector cases) { RiaApplication* app = RiaApplication::instance(); RimProject* proj = app->project(); RimSummaryCaseMainCollection* sumCaseColl = proj->activeOilField() ? proj->activeOilField()->summaryCaseMainCollection() : nullptr; sumCaseColl->addCases(cases); sumCaseColl->updateAllRequiredEditors(); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void RicImportSummaryCasesFeature::addCasesToGroupIfRelevant(const std::vector cases) { std::vector selectedColl = caf::selectedObjectsByTypeStrict(); if (selectedColl.size() == 1) { RimSummaryCaseCollection* coll = selectedColl.front(); RimSummaryCaseMainCollection* mainColl; coll->firstAncestorOrThisOfType(mainColl); if (mainColl) { for (const auto sumCase : cases) { mainColl->removeCase(sumCase); selectedColl.front()->addCase(sumCase); } mainColl->updateConnectedEditors(); } } } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- QStringList RicImportSummaryCasesFeature::runRecursiveSummaryCaseFileSearchDialog(const QString& dialogTitle) { RiaApplication* app = RiaApplication::instance(); QString defaultDir = app->lastUsedDialogDirectory("INPUT_FILES"); RicFileHierarchyDialogResult result = RicFileHierarchyDialog::runRecursiveSearchDialog(nullptr, dialogTitle, defaultDir, m_pathFilter, m_fileNameFilter, QStringList(".SMSPEC")); // Remember filters m_pathFilter = result.pathFilter; m_fileNameFilter = result.fileNameFilter; if (!result.ok) return QStringList(); // Remember the path to next time app->setLastUsedDialogDirectory("INPUT_FILES", QFileInfo(result.rootDir).absoluteFilePath()); return result.files; }