diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index e11aa5020..7ec36522b 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -131,11 +131,50 @@ VPMainWindow::~VPMainWindow() } //--------------------------------------------------------------------------------------------------------------------- -bool VPMainWindow::LoadFile(QString path) +auto VPMainWindow::CurrentFile() const -> QString { + return curFile; +} + +//--------------------------------------------------------------------------------------------------------------------- +auto VPMainWindow::LoadFile(QString path) -> bool +{ + if (not QFileInfo::exists(path)) + { + qCCritical(pWindow, "%s", qUtf8Printable(tr("File '%1' doesn't exist!").arg(path))); + if (m_cmd->IsTestModeEnabled()) + { + qApp->exit(V_EX_NOINPUT); + } + return false; + } + + // Check if file already opened + QList list = VPApplication::VApp()->MainWindows(); + auto w = std::find_if(list.begin(), list.end(), + [path](VPMainWindow *window) { return window->CurrentFile() == path; }); + if (w != list.end()) + { + (*w)->activateWindow(); + close(); + return false; + } + + VlpCreateLock(lock, path); + + if (not lock->IsLocked()) + { + if (not IgnoreLocking(lock->GetLockError(), path, m_cmd->IsGuiEnabled())) + { + return false; + } + } + try { VLayoutConverter converter(path); + m_curFileFormatVersion = converter.GetCurrentFormatVersion(); + m_curFileFormatVersionStr = converter.GetFormatVersionStr(); path = converter.Convert(); } catch (VException &e) @@ -148,16 +187,32 @@ bool VPMainWindow::LoadFile(QString path) QFile file(path); file.open(QIODevice::ReadOnly); - QScopedPointer fileReader(new VPLayoutFileReader()); + VPLayoutFileReader fileReader; if(m_layout == nullptr) { m_layout = new VPLayout(); } - fileReader->ReadFile(m_layout, &file); + fileReader.ReadFile(m_layout, &file); - // TODO / FIXME : better return value and error handling + if (fileReader.hasError()) + { + qCCritical(pWindow, "%s\n\n%s", qUtf8Printable(tr("File error.")), + qUtf8Printable(tr("Unable to read a layout file"))); + lock.reset(); + + if (m_cmd->IsTestModeEnabled()) + { + qApp->exit(V_EX_NOINPUT); + } + return false; + } + + // updates the properties with the loaded data + SetPropertiesData(); + + // TODO : update the Carrousel and the QGraphicView return true; } @@ -1049,52 +1104,28 @@ void VPMainWindow::on_actionOpen_triggered() qCDebug(pWindow, "Openning puzzle layout file."); const QString filter(tr("Layout files") + QLatin1String(" (*.vlt)")); + //Use standard path to individual measurements + const QString pathTo = VPApplication::VApp()->PuzzleSettings()->GetPathLayouts(); - //Get list last open files - QStringList recentFiles = VPApplication::VApp()->PuzzleSettings()->GetRecentFileList(); - QString dir; - if (recentFiles.isEmpty()) + bool usedNotExistedDir = false; + QDir directory(pathTo); + if (not directory.exists()) { - dir = QDir::homePath(); - } - else - { - //Absolute path to last open file - dir = QFileInfo(recentFiles.first()).absolutePath(); - } - qCDebug(pWindow, "Run QFileDialog::getOpenFileName: dir = %s.", qUtf8Printable(dir)); - const QString filePath = QFileDialog::getOpenFileName(this, tr("Open file"), dir, filter, nullptr); - - if (filePath.isEmpty()) - { - return; + usedNotExistedDir = directory.mkpath(QChar('.')); } + const QString mPath = QFileDialog::getOpenFileName(this, tr("Open file"), pathTo, filter, nullptr, + VAbstractApplication::VApp()->NativeFileDialog()); - // TODO : if m_layout == nullptr, open in current window - // otherwise open in new window - - // TODO : if layout file has a lock, warning message - - - if(!LoadFile(filePath)) + if (not mPath.isEmpty()) { - return; + VPApplication::VApp()->NewMainWindow()->LoadFile(mPath); } - // Updates the list of recent files - recentFiles.removeAll(filePath); - recentFiles.prepend(filePath); - while (recentFiles.size() > MaxRecentFiles) + if (usedNotExistedDir) { - recentFiles.removeLast(); + QDir(pathTo).rmpath(QChar('.')); } - VPApplication::VApp()->PuzzleSettings()->SetRecentFileList(recentFiles); - - // updates the properties with the loaded data - SetPropertiesData(); - - // TODO : update the Carrousel and the QGraphicView } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/vpmainwindow.h b/src/app/puzzle/vpmainwindow.h index 7644939be..b93e8dcfa 100644 --- a/src/app/puzzle/vpmainwindow.h +++ b/src/app/puzzle/vpmainwindow.h @@ -58,6 +58,8 @@ public: explicit VPMainWindow(const VPCommandLinePtr &cmd, QWidget *parent = nullptr); virtual ~VPMainWindow(); + QString CurrentFile() const; + /** * @brief LoadFile Loads the layout file of given path in m_layout. * This function doesn't update the gui. diff --git a/src/app/puzzle/xml/vplayoutfilereader.cpp b/src/app/puzzle/xml/vplayoutfilereader.cpp index ac5cea16d..4936a44ef 100644 --- a/src/app/puzzle/xml/vplayoutfilereader.cpp +++ b/src/app/puzzle/xml/vplayoutfilereader.cpp @@ -33,19 +33,6 @@ #include "../ifc/exception/vexception.h" #include "../ifc/exception/vexceptionconversionerror.h" -//--------------------------------------------------------------------------------------------------------------------- -VPLayoutFileReader::VPLayoutFileReader() -{ - -} - -//--------------------------------------------------------------------------------------------------------------------- -VPLayoutFileReader::~VPLayoutFileReader() -{ - // TODO -} - - //--------------------------------------------------------------------------------------------------------------------- bool VPLayoutFileReader::ReadFile(VPLayout *layout, QFile *file) { @@ -56,7 +43,7 @@ bool VPLayoutFileReader::ReadFile(VPLayout *layout, QFile *file) ReadLayout(layout); } - return !error(); + return hasError(); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/puzzle/xml/vplayoutfilereader.h b/src/app/puzzle/xml/vplayoutfilereader.h index 7ed40f1a8..f7b2b8d59 100644 --- a/src/app/puzzle/xml/vplayoutfilereader.h +++ b/src/app/puzzle/xml/vplayoutfilereader.h @@ -39,8 +39,8 @@ class VPLayoutFileReader : public QXmlStreamReader { Q_DECLARE_TR_FUNCTIONS(VPLayoutFileReader) public: - VPLayoutFileReader(); - ~VPLayoutFileReader(); + VPLayoutFileReader()=default; + ~VPLayoutFileReader()=default; bool ReadFile(VPLayout *layout, QFile *file); @@ -64,7 +64,7 @@ private: static QString ReadAttributeEmptyString(const QXmlStreamAttributes &attribs, const QString &name); static bool ReadAttributeBool(const QXmlStreamAttributes &attribs, const QString &name, const QString &defValue); static qreal ReadAttributeDouble(const QXmlStreamAttributes &attribs, const QString &name, - const QString &defValue); + const QString &defValue); }; #endif // VPLAYOUTFILEREADER_H diff --git a/src/app/tape/mapplication.cpp b/src/app/tape/mapplication.cpp index 592dbd0b8..bf14c3907 100644 --- a/src/app/tape/mapplication.cpp +++ b/src/app/tape/mapplication.cpp @@ -374,7 +374,7 @@ bool MApplication::IsTestMode() const */ bool MApplication::IsAppInGUIMode() const { - return IsTestMode(); + return not IsTestMode(); } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 4670eeeb7..94854b3ff 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -278,21 +278,20 @@ bool TMainWindow::LoadFile(const QString &path) // Check if file already opened const QList list = MApplication::VApp()->MainWindows(); - for (auto w : list) + auto w = std::find_if(list.begin(), list.end(), + [path](TMainWindow *window) { return window->CurrentFile() == path; }); + if (w != list.end()) { - if (w->CurrentFile() == path) - { - w->activateWindow(); - close(); - return false; - } + (*w)->activateWindow(); + close(); + return false; } VlpCreateLock(lock, path); if (not lock->IsLocked()) { - if (not IgnoreLocking(lock->GetLockError(), path)) + if (not IgnoreLocking(lock->GetLockError(), path, MApplication::VApp()->IsAppInGUIMode())) { return false; } @@ -3478,21 +3477,20 @@ bool TMainWindow::LoadFromExistingFile(const QString &path) // Check if file already opened const QList list = MApplication::VApp()->MainWindows(); - for (auto w : list) + auto w = std::find_if(list.begin(), list.end(), + [path](TMainWindow *window) { return window->CurrentFile() == path; }); + if (w != list.end()) { - if (w->CurrentFile() == path) - { - w->activateWindow(); - close(); - return false; - } + (*w)->activateWindow(); + close(); + return false; } VlpCreateLock(lock, path); if (not lock->IsLocked()) { - if (not IgnoreLocking(lock->GetLockError(), path)) + if (not IgnoreLocking(lock->GetLockError(), path, MApplication::VApp()->IsAppInGUIMode())) { return false; } @@ -3617,72 +3615,6 @@ void TMainWindow::CreateWindowMenu(QMenu *menu) } } -//--------------------------------------------------------------------------------------------------------------------- -bool TMainWindow::IgnoreLocking(int error, const QString &path) -{ - QMessageBox::StandardButton answer = QMessageBox::Abort; - if (not MApplication::VApp()->IsTestMode()) - { - switch(error) - { - case QLockFile::LockFailedError: - answer = QMessageBox::warning(this, tr("Locking file"), - tr("This file already opened in another window. Ignore if you want " - "to continue (not recommended, can cause a data corruption)."), - QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); - break; - case QLockFile::PermissionError: - answer = QMessageBox::question(this, tr("Locking file"), - tr("The lock file could not be created, for lack of permissions. " - "Ignore if you want to continue (not recommended, can cause " - "a data corruption)."), - QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); - break; - case QLockFile::UnknownError: - answer = QMessageBox::question(this, tr("Locking file"), - tr("Unknown error happened, for instance a full partition " - "prevented writing out the lock file. Ignore if you want to " - "continue (not recommended, can cause a data corruption)."), - QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); - break; - default: - answer = QMessageBox::Abort; - break; - } - } - - if (answer == QMessageBox::Abort) - { - qCDebug(tMainWindow, "Failed to lock %s", qUtf8Printable(path)); - qCDebug(tMainWindow, "Error type: %d", error); - if (MApplication::VApp()->IsTestMode()) - { - switch(error) - { - case QLockFile::LockFailedError: - qCCritical(tMainWindow, "%s", - qUtf8Printable(tr("This file already opened in another window."))); - break; - case QLockFile::PermissionError: - qCCritical(tMainWindow, "%s", - qUtf8Printable(tr("The lock file could not be created, for lack of permissions."))); - break; - case QLockFile::UnknownError: - qCCritical(tMainWindow, "%s", - qUtf8Printable(tr("Unknown error happened, for instance a full partition " - "prevented writing out the lock file."))); - break; - default: - break; - } - - qApp->exit(V_EX_NOINPUT); - } - return false; - } - return true; -} - //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::HackDimensionBaseValue() { diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h index 6fa275b0d..cd8bdf11b 100644 --- a/src/app/tape/tmainwindow.h +++ b/src/app/tape/tmainwindow.h @@ -228,8 +228,6 @@ private: void CreateWindowMenu(QMenu *menu); - bool IgnoreLocking(int error, const QString &path); - template void HackWidget(T **widget); void HackDimensionBaseValue(); diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index 409f5c84f..ce4f302b7 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -5282,7 +5282,7 @@ bool MainWindow::LoadPattern(QString fileName, const QString& customMeasureFile) } else { - if (not IgnoreLocking(lock->GetLockError(), fileName)) + if (not IgnoreLocking(lock->GetLockError(), fileName, VApplication::IsGUIMode())) { return false; } @@ -6390,73 +6390,6 @@ void MainWindow::UpdateWindowTitle() #endif //defined(Q_OS_MAC) } -//--------------------------------------------------------------------------------------------------------------------- -bool MainWindow::IgnoreLocking(int error, const QString &path) -{ - QMessageBox::StandardButton answer = QMessageBox::Abort; - if (VApplication::IsGUIMode()) - { - switch(error) - { - case QLockFile::LockFailedError: - answer = QMessageBox::warning(this, tr("Locking file"), - tr("This file already opened in another window. Ignore if you want " - "to continue (not recommended, can cause a data corruption)."), - QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); - break; - case QLockFile::PermissionError: - answer = QMessageBox::question(this, tr("Locking file"), - tr("The lock file could not be created, for lack of permissions. " - "Ignore if you want to continue (not recommended, can cause " - "a data corruption)."), - QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); - break; - case QLockFile::UnknownError: - answer = QMessageBox::question(this, tr("Locking file"), - tr("Unknown error happened, for instance a full partition prevented " - "writing out the lock file. Ignore if you want to continue (not " - "recommended, can cause a data corruption)."), - QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); - break; - default: - answer = QMessageBox::Abort; - break; - } - } - - if (answer == QMessageBox::Abort) - { - qCDebug(vMainWindow, "Failed to lock %s", qUtf8Printable(path)); - qCDebug(vMainWindow, "Error type: %d", error); - Clear(); - if (not VApplication::IsGUIMode()) - { - switch(error) - { - case QLockFile::LockFailedError: - qCCritical(vMainWindow, "%s", - qUtf8Printable(tr("This file already opened in another window."))); - break; - case QLockFile::PermissionError: - qCCritical(vMainWindow, "%s", - qUtf8Printable(tr("The lock file could not be created, for lack of permissions."))); - break; - case QLockFile::UnknownError: - qCCritical(vMainWindow, "%s", - qUtf8Printable(tr("Unknown error happened, for instance a full partition prevented " - "writing out the lock file."))); - break; - default: - break; - } - - qApp->exit(V_EX_NOINPUT); - } - return false; - } - return true; -} - //--------------------------------------------------------------------------------------------------------------------- void MainWindow::ToolSelectPoint() { diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 487f564b4..ed8049af7 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -392,8 +392,6 @@ private: void UpdateWindowTitle(); - bool IgnoreLocking(int error, const QString &path); - void ToolSelectPoint(); void ToolSelectPointByPress(); void ToolSelectPointByRelease(); diff --git a/src/libs/vwidgets/vabstractmainwindow.cpp b/src/libs/vwidgets/vabstractmainwindow.cpp index 230528307..f4c2f3de5 100644 --- a/src/libs/vwidgets/vabstractmainwindow.cpp +++ b/src/libs/vwidgets/vabstractmainwindow.cpp @@ -31,18 +31,31 @@ #include "../vmisc/vabstractapplication.h" #include "../vmisc/compatibility.h" #include "../vmisc/def.h" +#include "../vmisc/vsysexits.h" #include "dialogs/dialogexporttocsv.h" +#include "../vwidgets/vmaingraphicsview.h" #include #include #include #include +#include +#include #if defined(Q_OS_MAC) -#include "../vwidgets/vmaingraphicsview.h" #include #endif +#include + +QT_WARNING_PUSH +QT_WARNING_DISABLE_CLANG("-Wmissing-prototypes") +QT_WARNING_DISABLE_INTEL(1418) + +Q_LOGGING_CATEGORY(abstactMainWindow, "abs.MainWindow") + +QT_WARNING_POP + namespace { //--------------------------------------------------------------------------------------------------------------------- @@ -284,6 +297,72 @@ auto VAbstractMainWindow::CheckFilePermissions(const QString &path, QWidget *mes return true; } +//--------------------------------------------------------------------------------------------------------------------- +bool VAbstractMainWindow::IgnoreLocking(int error, const QString &path, bool guiMode) +{ + QMessageBox::StandardButton answer = QMessageBox::Abort; + if (guiMode) + { + switch(error) + { + case QLockFile::LockFailedError: + answer = QMessageBox::warning(this, tr("Locking file"), + tr("This file already opened in another window. Ignore if you want " + "to continue (not recommended, can cause a data corruption)."), + QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); + break; + case QLockFile::PermissionError: + answer = QMessageBox::question(this, tr("Locking file"), + tr("The lock file could not be created, for lack of permissions. " + "Ignore if you want to continue (not recommended, can cause " + "a data corruption)."), + QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); + break; + case QLockFile::UnknownError: + answer = QMessageBox::question(this, tr("Locking file"), + tr("Unknown error happened, for instance a full partition " + "prevented writing out the lock file. Ignore if you want to " + "continue (not recommended, can cause a data corruption)."), + QMessageBox::Abort|QMessageBox::Ignore, QMessageBox::Abort); + break; + default: + answer = QMessageBox::Abort; + break; + } + } + + if (answer == QMessageBox::Abort) + { + qCDebug(abstactMainWindow, "Failed to lock %s", qUtf8Printable(path)); + qCDebug(abstactMainWindow, "Error type: %d", error); + if (not guiMode) + { + switch(error) + { + case QLockFile::LockFailedError: + qCCritical(abstactMainWindow, "%s", + qUtf8Printable(tr("This file already opened in another window."))); + break; + case QLockFile::PermissionError: + qCCritical(abstactMainWindow, "%s", + qUtf8Printable(tr("The lock file could not be created, for lack of permissions."))); + break; + case QLockFile::UnknownError: + qCCritical(abstactMainWindow, "%s", + qUtf8Printable(tr("Unknown error happened, for instance a full partition " + "prevented writing out the lock file."))); + break; + default: + break; + } + + qApp->exit(V_EX_NOINPUT); + } + return false; + } + return true; +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractMainWindow::WindowsLocale() { diff --git a/src/libs/vwidgets/vabstractmainwindow.h b/src/libs/vwidgets/vabstractmainwindow.h index ee803c5a8..424231817 100644 --- a/src/libs/vwidgets/vabstractmainwindow.h +++ b/src/libs/vwidgets/vabstractmainwindow.h @@ -71,7 +71,9 @@ protected: virtual QStringList RecentFileList() const =0; void UpdateRecentFileActions(); - static bool CheckFilePermissions(const QString &path, QWidget *messageBoxParent=nullptr) ; + static bool CheckFilePermissions(const QString &path, QWidget *messageBoxParent=nullptr); + + bool IgnoreLocking(int error, const QString &path, bool guiMode); private: Q_DISABLE_COPY(VAbstractMainWindow)