This commit is contained in:
Magne Sjaastad
2018-09-26 14:14:38 +02:00
parent 492904d465
commit 088638020b
2 changed files with 42 additions and 33 deletions

View File

@@ -67,23 +67,26 @@ QString RimTools::getCacheRootDirectoryPathFromProject()
/// such that the common start of oldProjectPath and m_gridFileName is removed from m_gridFileName /// such that the common start of oldProjectPath and m_gridFileName is removed from m_gridFileName
/// and replaced with the start of newProjectPath up to where newProjectPath starts to be equal to oldProjectPath /// and replaced with the start of newProjectPath up to where newProjectPath starts to be equal to oldProjectPath
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNewProjectPath, const QString& orgOldProjectPath, QString RimTools::relocateFile(const QString& originalFileName,
bool* foundFile, std::vector<QString>* searchedPaths) const QString& currentProjectPath,
const QString& previousProjectPath,
bool* foundFile,
std::vector<QString>* searchedPaths)
{ {
if (foundFile) *foundFile = true; if (foundFile) *foundFile = true;
// Make sure we have a Qt formatted path ( using "/" not "\") // Make sure we have a Qt formatted path ( using "/" not "\")
QString fileName = QDir::fromNativeSeparators(orgFileName); QString fileName = QDir::fromNativeSeparators(originalFileName);
QString newProjectPath = QDir::fromNativeSeparators(orgNewProjectPath); QString newProjectPath = QDir::fromNativeSeparators(currentProjectPath);
QString oldProjectPath = QDir::fromNativeSeparators(orgOldProjectPath); QString oldProjectPath = QDir::fromNativeSeparators(previousProjectPath);
// If we from a file or whatever gets a real windows path on linux, we need to manually convert it // If we from a file or whatever gets a real windows path on linux, we need to manually convert it
// because Qt will not. QDir::fromNativeSeparators does nothing on linux. // because Qt will not. QDir::fromNativeSeparators does nothing on linux.
bool isWindowsPath = false; bool isWindowsPath = false;
if (orgFileName.count("/")) isWindowsPath = false; // "/" are not allowed in a windows path if (originalFileName.count("/")) isWindowsPath = false; // "/" are not allowed in a windows path
else if (orgFileName.count("\\") else if (originalFileName.count("\\")
&& !caf::Utils::fileExists(orgFileName)) // To make sure we do not convert single linux files containing "\" && !caf::Utils::fileExists(originalFileName)) // To make sure we do not convert single linux files containing "\"
{ {
isWindowsPath = true; isWindowsPath = true;
} }
@@ -114,10 +117,10 @@ QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNew
// Then find the possible move of a directory structure where projects and files referenced are moved in "paralell" // Then find the possible move of a directory structure where projects and files referenced are moved in "paralell"
QFileInfo gridFileInfo(QDir::fromNativeSeparators(fileName)); QFileInfo fileNameFileInfo(QDir::fromNativeSeparators(fileName));
QString gridFilePath = gridFileInfo.path(); QString fileNamePath = fileNameFileInfo.path();
QString gridFileNameWoPath = gridFileInfo.fileName(); QString fileNameWithoutPath = fileNameFileInfo.fileName();
QStringList gridPathElements = gridFilePath.split("/", QString::KeepEmptyParts); QStringList fileNamePathElements = fileNamePath.split("/", QString::KeepEmptyParts);
QString oldProjPath = QDir::fromNativeSeparators(oldProjectPath); QString oldProjPath = QDir::fromNativeSeparators(oldProjectPath);
QStringList oldProjPathElements = oldProjPath.split("/", QString::KeepEmptyParts); QStringList oldProjPathElements = oldProjPath.split("/", QString::KeepEmptyParts);
@@ -130,7 +133,7 @@ QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNew
bool pathStartsAreEqual = false; bool pathStartsAreEqual = false;
bool pathEndsDiffer = false; bool pathEndsDiffer = false;
int firstDiffIdx = 0; int firstDiffIdx = 0;
for (firstDiffIdx = 0; firstDiffIdx < gridPathElements.size() && firstDiffIdx < oldProjPathElements.size(); ++firstDiffIdx) for (firstDiffIdx = 0; firstDiffIdx < fileNamePathElements.size() && firstDiffIdx < oldProjPathElements.size(); ++firstDiffIdx)
{ {
#ifdef WIN32 #ifdef WIN32
// When comparing parts of a file path, the drive letter has been seen to be a mix of // When comparing parts of a file path, the drive letter has been seen to be a mix of
@@ -140,9 +143,9 @@ QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNew
#else #else
Qt::CaseSensitivity cs = Qt::CaseSensitive; Qt::CaseSensitivity cs = Qt::CaseSensitive;
#endif #endif
if (gridPathElements[firstDiffIdx].compare(oldProjPathElements[firstDiffIdx], cs) == 0) if (fileNamePathElements[firstDiffIdx].compare(oldProjPathElements[firstDiffIdx], cs) == 0)
{ {
pathStartsAreEqual = pathStartsAreEqual || !gridPathElements[firstDiffIdx].isEmpty(); pathStartsAreEqual = pathStartsAreEqual || !fileNamePathElements[firstDiffIdx].isEmpty();
} }
else else
{ {
@@ -151,7 +154,7 @@ QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNew
} }
} }
if (!pathEndsDiffer && firstDiffIdx < gridPathElements.size() || firstDiffIdx < oldProjPathElements.size()) if (!pathEndsDiffer && firstDiffIdx < fileNamePathElements.size() || firstDiffIdx < oldProjPathElements.size())
{ {
pathEndsDiffer = true; pathEndsDiffer = true;
} }
@@ -162,11 +165,11 @@ QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNew
{ {
if (pathEndsDiffer) if (pathEndsDiffer)
{ {
QString oldGridFilePathEnd; QString oldFileNamePathEnd;
for (int i = firstDiffIdx; i < gridPathElements.size(); ++i) for (int i = firstDiffIdx; i < fileNamePathElements.size(); ++i)
{ {
oldGridFilePathEnd += gridPathElements[i]; oldFileNamePathEnd += fileNamePathElements[i];
oldGridFilePathEnd += "/"; oldFileNamePathEnd += "/";
} }
// Find the new Project File Start Path // Find the new Project File Start Path
@@ -177,27 +180,29 @@ QString RimTools::relocateFile(const QString& orgFileName, const QString& orgNew
oldProjectFilePathEndElements.push_back(oldProjPathElements[i]); oldProjectFilePathEndElements.push_back(oldProjPathElements[i]);
} }
int ppIdx = oldProjectFilePathEndElements.size() - 1; int lastProjectDiffIdx = newProjPathElements.size() - 1;
int lastDiffIdx = newProjPathElements.size() - 1;
for (; lastDiffIdx >= 0 && ppIdx >= 0; --lastDiffIdx, --ppIdx)
{ {
if (oldProjectFilePathEndElements[ppIdx] != newProjPathElements[lastDiffIdx]) int ppIdx = oldProjectFilePathEndElements.size() - 1;
for (; lastProjectDiffIdx >= 0 && ppIdx >= 0; --lastProjectDiffIdx, --ppIdx)
{
if (oldProjectFilePathEndElements[ppIdx] != newProjPathElements[lastProjectDiffIdx])
{ {
break; break;
} }
} }
QString newProjecetFileStartPath;
for (int i = 0; i <= lastDiffIdx; ++i)
{
newProjecetFileStartPath += newProjPathElements[i];
newProjecetFileStartPath += "/";
} }
QString relocationPath = newProjecetFileStartPath + oldGridFilePathEnd; QString newProjectFileStartPath;
for (int i = 0; i <= lastProjectDiffIdx; ++i)
{
newProjectFileStartPath += newProjPathElements[i];
newProjectFileStartPath += "/";
}
QString relocatedFileName = relocationPath + gridFileNameWoPath; QString relocationPath = newProjectFileStartPath + oldFileNamePathEnd;
QString relocatedFileName = relocationPath + fileNameWithoutPath;
if (searchedPaths) searchedPaths->push_back(relocatedFileName); if (searchedPaths) searchedPaths->push_back(relocatedFileName);

View File

@@ -43,7 +43,11 @@ class RimTools
public: public:
static QString getCacheRootDirectoryPathFromProject(); static QString getCacheRootDirectoryPathFromProject();
static QString relocateFile(const QString& fileName, const QString& newProjectPath, const QString& oldProjectPath, bool* foundFile, std::vector<QString>* searchedPaths); static QString relocateFile(const QString& originalFileName,
const QString& currentProjectPath,
const QString& previousProjectPath,
bool* foundFile,
std::vector<QString>* searchedPaths);
static void wellPathOptionItems(QList<caf::PdmOptionItemInfo>* options); static void wellPathOptionItems(QList<caf::PdmOptionItemInfo>* options);
static void wellPathWithFormationsOptionItems(QList<caf::PdmOptionItemInfo>* options); static void wellPathWithFormationsOptionItems(QList<caf::PdmOptionItemInfo>* options);