From b9ed45af2c23c28a945a0bef5a80f3198fbd1176 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Mon, 8 Apr 2013 08:36:34 +0200 Subject: [PATCH] If project is moved on disk, execute initAfterRead() to propagate project file name changes to underlying objects p4#: 21159 --- .../Application/RiaApplication.cpp | 8 +- .../ProjectDataModel/RimResultCase.cpp | 99 +++++++++++-------- .../ProjectDataModel/RimResultCase.h | 11 +-- 3 files changed, 68 insertions(+), 50 deletions(-) diff --git a/ApplicationCode/Application/RiaApplication.cpp b/ApplicationCode/Application/RiaApplication.cpp index 503d3e2d9d..ce024743c7 100644 --- a/ApplicationCode/Application/RiaApplication.cpp +++ b/ApplicationCode/Application/RiaApplication.cpp @@ -235,7 +235,13 @@ bool RiaApplication::loadProject(const QString& projectFileName) m_project->fileName = projectFileName; m_project->readFile(); - m_project->fileName = projectFileName; // Make sure we overwrite the old filename read from the project file + + // If the project filename has changed, call initAfterRead once more to propagate new location of project + if (m_project->fileName() != projectFileName) + { + m_project->fileName = projectFileName; + caf::PdmDocument::initAfterReadTraversal(m_project); + } // On error, delete everything, and bail out. diff --git a/ApplicationCode/ProjectDataModel/RimResultCase.cpp b/ApplicationCode/ProjectDataModel/RimResultCase.cpp index 35df9639a0..41aa4bedd7 100644 --- a/ApplicationCode/ProjectDataModel/RimResultCase.cpp +++ b/ApplicationCode/ProjectDataModel/RimResultCase.cpp @@ -27,6 +27,7 @@ #include "cafProgressInfo.h" #include "RimProject.h" #include "RifEclipseOutputFileTools.h" +#include "RiaApplication.h" CAF_PDM_SOURCE_INIT(RimResultCase, "EclipseCase"); @@ -76,15 +77,14 @@ bool RimResultCase::openEclipseGridFile() } else { - QString fname = createAbsoluteFilenameFromCase(caseName); - if (fname.isEmpty()) + if (!QFile::exists(caseFileName())) { return false; } cvf::ref eclipseCase = new RigCaseData; readerInterface = new RifReaderEclipseOutput; - if (!readerInterface->open(fname, eclipseCase.p())) + if (!readerInterface->open(caseFileName(), eclipseCase.p())) { return false; } @@ -119,8 +119,7 @@ bool RimResultCase::openAndReadActiveCellData(RigCaseData* mainEclipseCase) } else { - QString fname = createAbsoluteFilenameFromCase(caseName); - if (fname.isEmpty()) + if (!QFile::exists(caseFileName())) { return false; } @@ -140,7 +139,7 @@ bool RimResultCase::openAndReadActiveCellData(RigCaseData* mainEclipseCase) std::vector timeStepDates = mainEclipseCase->results(RifReaderInterface::MATRIX_RESULTS)->timeStepDates(scalarIndexWithMaxTimeStepCount); cvf::ref readerEclipseOutput = new RifReaderEclipseOutput; - if (!readerEclipseOutput->openAndReadActiveCellData(fname, timeStepDates, eclipseCase.p())) + if (!readerEclipseOutput->openAndReadActiveCellData(caseFileName(), timeStepDates, eclipseCase.p())) { return false; } @@ -262,50 +261,64 @@ QString RimResultCase::locationOnDisc() const //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -QString RimResultCase::createAbsoluteFilenameFromCase(const QString& caseName) +void RimResultCase::readGridDimensions(std::vector< std::vector >& gridDimensions) { - QString candidate; - - candidate = QDir::fromNativeSeparators(caseDirectory.v() + QDir::separator() + caseName + ".EGRID"); - if (QFile::exists(candidate)) return candidate; - - candidate = QDir::fromNativeSeparators(caseDirectory.v() + QDir::separator() + caseName + ".GRID"); - if (QFile::exists(candidate)) return candidate; - - std::vector parentObjects; - this->parentObjects(parentObjects); - - QString projectPath; - for (size_t i = 0; i < parentObjects.size(); i++) - { - caf::PdmObject* obj = parentObjects[i]; - RimProject* proj = dynamic_cast(obj); - if (proj) - { - QFileInfo fi(proj->fileName); - projectPath = fi.path(); - } - } - - if (!projectPath.isEmpty()) - { - candidate = QDir::fromNativeSeparators(projectPath + QDir::separator() + caseName + ".EGRID"); - if (QFile::exists(candidate)) return candidate; - - candidate = QDir::fromNativeSeparators(projectPath + QDir::separator() + caseName + ".GRID"); - if (QFile::exists(candidate)) return candidate; - } - - return QString(); + RifEclipseOutputFileTools::readGridDimensions(caseFileName, gridDimensions); } //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- -void RimResultCase::readGridDimensions(std::vector< std::vector >& gridDimensions) +void RimResultCase::initAfterRead() { - QString fname = createAbsoluteFilenameFromCase(caseName); + RimCase::initAfterRead(); - RifEclipseOutputFileTools::readGridDimensions(fname, gridDimensions); + // Update filename and folder paths when opening project from a different file location + if (!QFile::exists(caseFileName)) + { + QString candidate; + + candidate = QDir::fromNativeSeparators(caseDirectory.v() + QDir::separator() + caseName + ".EGRID"); + if (QFile::exists(candidate)) + { + caseFileName = candidate; + return; + } + + candidate = QDir::fromNativeSeparators(caseDirectory.v() + QDir::separator() + caseName + ".GRID"); + if (QFile::exists(candidate)) + { + caseFileName = candidate; + return; + } + + QString projPath = projectPath(); + + candidate = QDir::fromNativeSeparators(projPath + QDir::separator() + caseName + ".EGRID"); + if (QFile::exists(candidate)) + { + caseFileName = candidate; + caseDirectory = projectPath; + return; + } + + candidate = QDir::fromNativeSeparators(projPath + QDir::separator() + caseName + ".GRID"); + if (QFile::exists(candidate)) + { + caseFileName = candidate; + caseDirectory = projPath; + return; + } + } +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +QString RimResultCase::projectPath() const +{ + QString projectFileName = RiaApplication::instance()->project()->fileName(); + QFileInfo fileInfo(projectFileName); + return fileInfo.path(); } diff --git a/ApplicationCode/ProjectDataModel/RimResultCase.h b/ApplicationCode/ProjectDataModel/RimResultCase.h index 3a2e5042bb..1037d230fd 100644 --- a/ApplicationCode/ProjectDataModel/RimResultCase.h +++ b/ApplicationCode/ProjectDataModel/RimResultCase.h @@ -41,7 +41,6 @@ public: RimResultCase(const QString& caseName, const QString& caseFileName, const QString& caseDirectory); virtual ~RimResultCase(); - // Fields: caf::PdmField caseFileName; caf::PdmField caseDirectory; @@ -50,13 +49,13 @@ public: bool openAndReadActiveCellData(RigCaseData* mainEclipseCase); void readGridDimensions(std::vector< std::vector >& gridDimensions); - //virtual caf::PdmFieldHandle* userDescriptionField() { return &caseName;} + virtual QString locationOnDisc() const; - virtual QString locationOnDisc() const; +protected: + virtual void initAfterRead(); private: + QString projectPath() const; + cvf::ref createMockModel(QString modelName); - - QString createAbsoluteFilenameFromCase(const QString& caseName); - };