From 856e472587f37f0d24311aefb0a07e7d8cddb1eb Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 28 Oct 2023 09:55:21 +0300 Subject: [PATCH] Init measurements database. --- src/app/tape/mapplication.cpp | 60 +++++++++++++++++ src/app/tape/mapplication.h | 8 +++ src/app/valentina/core/vapplication.cpp | 66 +++++++++++++++++++ src/app/valentina/core/vapplication.h | 11 ++++ .../knownmeasurements/vknownmeasurements.h | 2 +- .../vknownmeasurementsdatabase.h | 3 +- src/libs/vmisc/vabstractapplication.cpp | 7 +- src/libs/vmisc/vcommonsettings.cpp | 13 +++- src/libs/vmisc/vcommonsettings.h | 1 + 9 files changed, 162 insertions(+), 9 deletions(-) diff --git a/src/app/tape/mapplication.cpp b/src/app/tape/mapplication.cpp index ea2bd0cd0..5513376c6 100644 --- a/src/app/tape/mapplication.cpp +++ b/src/app/tape/mapplication.cpp @@ -34,19 +34,24 @@ #include "../ifc/exception/vexceptionobjecterror.h" #include "../ifc/exception/vexceptionwrongid.h" #include "../qmuparser/qmuparsererror.h" +#include "../vformat/knownmeasurements/vknownmeasurementsdatabase.h" #include "../vganalytics/def.h" #include "../vganalytics/vganalytics.h" #include "../vmisc/projectversion.h" #include "../vmisc/qt_dispatch/qt_dispatch.h" #include "../vmisc/theme/vtheme.h" #include "../vmisc/vsysexits.h" +#include "qtpreprocessorsupport.h" #include "tmainwindow.h" #include "version.h" #include "vtapeshortcutmanager.h" +#include "QtConcurrent/qtconcurrentrun.h" #include #include #include +#include +#include #include #include #include @@ -578,6 +583,7 @@ void MApplication::OpenSettings() settings = new VTapeSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName(), this); connect(settings, &VTapeSettings::SVGFontsPathChanged, this, &MApplication::SVGFontsPathChanged); + connect(settings, &VTapeSettings::KnownMeasurementsPathChanged, this, &MApplication::KnownMeasurementsPathChanged); } //--------------------------------------------------------------------------------------------------------------------- @@ -725,6 +731,36 @@ auto MApplication::VApp() -> MApplication * return qobject_cast(QCoreApplication::instance()); } +//--------------------------------------------------------------------------------------------------------------------- +auto MApplication::KnownMeasurementsDatabase() -> VKnownMeasurementsDatabase * +{ + if (m_knownMeasurementsDatabase == nullptr) + { + m_knownMeasurementsDatabase = new VKnownMeasurementsDatabase(); + + RestartKnownMeasurementsDatabaseWatcher(); + } + + if (!m_knownMeasurementsDatabase->IsPopulated()) + { + m_knownMeasurementsDatabase->PopulateMeasurementsDatabase(); + } + + return m_knownMeasurementsDatabase; +} + +//--------------------------------------------------------------------------------------------------------------------- +void MApplication::RestartKnownMeasurementsDatabaseWatcher() +{ + if (m_knownMeasurementsDatabase != nullptr) + { + delete m_knownMeasurementsDatabaseWatcher; + m_knownMeasurementsDatabaseWatcher = new QFileSystemWatcher({settings->GetPathKnownMeasurements()}, this); + connect(m_knownMeasurementsDatabaseWatcher, &QFileSystemWatcher::directoryChanged, this, + &MApplication::RepopulateMeasurementsDatabase); + } +} + //--------------------------------------------------------------------------------------------------------------------- auto MApplication::NewMainWindow() -> TMainWindow * { @@ -773,6 +809,30 @@ void MApplication::NewLocalSocketConnection() MainWindow()->activateWindow(); } +//--------------------------------------------------------------------------------------------------------------------- +void MApplication::RepopulateMeasurementsDatabase(const QString &path) +{ + Q_UNUSED(path) + if (m_knownMeasurementsDatabase != nullptr) + { + QFuture future = + QtConcurrent::run([this]() { m_knownMeasurementsDatabase->PopulateMeasurementsDatabase(); }); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void MApplication::KnownMeasurementsPathChanged(const QString &oldPath, const QString &newPath) +{ + if (oldPath != newPath) + { + if (m_knownMeasurementsDatabase != nullptr) + { + RestartKnownMeasurementsDatabaseWatcher(); + RepopulateMeasurementsDatabase(newPath); + } + } +} + //--------------------------------------------------------------------------------------------------------------------- void MApplication::Clean() { diff --git a/src/app/tape/mapplication.h b/src/app/tape/mapplication.h index c2b848c8f..a82dbd460 100644 --- a/src/app/tape/mapplication.h +++ b/src/app/tape/mapplication.h @@ -37,6 +37,7 @@ class TMainWindow; class QLocalServer; class QCommandLineParser; +class VKnownMeasurementsDatabase; enum class SocketConnection : bool { @@ -77,6 +78,9 @@ public: static auto VApp() -> MApplication *; + auto KnownMeasurementsDatabase() -> VKnownMeasurementsDatabase *; + void RestartKnownMeasurementsDatabaseWatcher(); + public slots: void ProcessCMD(); @@ -89,6 +93,8 @@ protected slots: private slots: void NewLocalSocketConnection(); + void RepopulateMeasurementsDatabase(const QString &path); + void KnownMeasurementsPathChanged(const QString &oldPath, const QString &newPath); private: // cppcheck-suppress unknownMacro @@ -98,6 +104,8 @@ private: VTranslateVars *m_trVars{nullptr}; QPointer m_dataBase{}; bool m_testMode{false}; + VKnownMeasurementsDatabase *m_knownMeasurementsDatabase{nullptr}; + QFileSystemWatcher *m_knownMeasurementsDatabaseWatcher{nullptr}; void Clean(); diff --git a/src/app/valentina/core/vapplication.cpp b/src/app/valentina/core/vapplication.cpp index ad7bc8574..fe47363f8 100644 --- a/src/app/valentina/core/vapplication.cpp +++ b/src/app/valentina/core/vapplication.cpp @@ -36,6 +36,7 @@ #include "../mainwindow.h" #include "../qmuparser/qmuparsererror.h" #include "../version.h" +#include "../vformat/knownmeasurements/vknownmeasurementsdatabase.h" #include "../vganalytics/def.h" #include "../vganalytics/vganalytics.h" #include "../vmisc/qt_dispatch/qt_dispatch.h" @@ -48,9 +49,12 @@ #include "../vmisc/backport/text.h" #endif +#include "QtConcurrent/qtconcurrentrun.h" #include #include #include +#include +#include #include #include #include @@ -784,6 +788,36 @@ auto VApplication::LogFile() -> QTextStream * return m_out.get(); } +//--------------------------------------------------------------------------------------------------------------------- +auto VApplication::KnownMeasurementsDatabase() -> VKnownMeasurementsDatabase * +{ + if (m_knownMeasurementsDatabase == nullptr) + { + m_knownMeasurementsDatabase = new VKnownMeasurementsDatabase(); + + RestartKnownMeasurementsDatabaseWatcher(); + } + + if (!m_knownMeasurementsDatabase->IsPopulated()) + { + m_knownMeasurementsDatabase->PopulateMeasurementsDatabase(); + } + + return m_knownMeasurementsDatabase; +} + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::RestartKnownMeasurementsDatabaseWatcher() +{ + if (m_knownMeasurementsDatabase != nullptr) + { + delete m_knownMeasurementsDatabaseWatcher; + m_knownMeasurementsDatabaseWatcher = new QFileSystemWatcher({settings->GetPathKnownMeasurements()}, this); + connect(m_knownMeasurementsDatabaseWatcher, &QFileSystemWatcher::directoryChanged, this, + &VApplication::RepopulateMeasurementsDatabase); + } +} + //--------------------------------------------------------------------------------------------------------------------- auto VApplication::TrVars() -> const VTranslateVars * { @@ -850,6 +884,30 @@ void VApplication::AboutToQuit() Settings()->sync(); } +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::RepopulateMeasurementsDatabase(const QString &path) +{ + Q_UNUSED(path) + if (m_knownMeasurementsDatabase != nullptr) + { + QFuture future = + QtConcurrent::run([this]() { m_knownMeasurementsDatabase->PopulateMeasurementsDatabase(); }); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::KnownMeasurementsPathChanged(const QString &oldPath, const QString &newPath) +{ + if (oldPath != newPath) + { + if (m_knownMeasurementsDatabase != nullptr) + { + RestartKnownMeasurementsDatabaseWatcher(); + RepopulateMeasurementsDatabase(newPath); + } + } +} + //--------------------------------------------------------------------------------------------------------------------- auto VApplication::IsGUIMode() -> bool { @@ -871,6 +929,14 @@ auto VApplication::IsPedantic() const -> bool return (VCommandLine::instance != nullptr) && VCommandLine::instance->IsPedantic(); } +//--------------------------------------------------------------------------------------------------------------------- +void VApplication::OpenSettings() +{ + VAbstractValApplication::OpenSettings(); + connect(settings, &VValentinaSettings::KnownMeasurementsPathChanged, this, + &VApplication::KnownMeasurementsPathChanged); +} + //--------------------------------------------------------------------------------------------------------------------- auto VApplication::VApp() -> VApplication * { diff --git a/src/app/valentina/core/vapplication.h b/src/app/valentina/core/vapplication.h index b1ae7d81f..937a2d9f7 100644 --- a/src/app/valentina/core/vapplication.h +++ b/src/app/valentina/core/vapplication.h @@ -35,6 +35,7 @@ #include "vcmdexport.h" class VApplication; // use in define +class VKnownMeasurementsDatabase; /** * @brief The VApplication class reimplamentation QApplication class. @@ -65,12 +66,17 @@ public: void StartLogging(); auto LogFile() -> QTextStream *; + auto KnownMeasurementsDatabase() -> VKnownMeasurementsDatabase *; + void RestartKnownMeasurementsDatabaseWatcher(); + auto TrVars() -> const VTranslateVars * override; auto static IsGUIMode() -> bool; auto IsAppInGUIMode() const -> bool override; auto IsPedantic() const -> bool override; + void OpenSettings() override; + static auto VApp() -> VApplication *; static auto CommandLine() -> VCommandLinePtr; @@ -80,6 +86,8 @@ protected: protected slots: void AboutToQuit() override; + void RepopulateMeasurementsDatabase(const QString &path); + void KnownMeasurementsPathChanged(const QString &oldPath, const QString &newPath); private: // cppcheck-suppress unknownMacro @@ -90,6 +98,9 @@ private: QSharedPointer> m_lockLog{}; std::shared_ptr m_out{nullptr}; + VKnownMeasurementsDatabase *m_knownMeasurementsDatabase{nullptr}; + QFileSystemWatcher *m_knownMeasurementsDatabaseWatcher{nullptr}; + static auto LogDirPath() -> QString; static auto LogPath() -> QString; static auto CreateLogDir() -> bool; diff --git a/src/libs/vformat/knownmeasurements/vknownmeasurements.h b/src/libs/vformat/knownmeasurements/vknownmeasurements.h index c94e5b9e3..ffc548389 100644 --- a/src/libs/vformat/knownmeasurements/vknownmeasurements.h +++ b/src/libs/vformat/knownmeasurements/vknownmeasurements.h @@ -28,7 +28,7 @@ #ifndef VKNOWNMEASUREMENTS_H #define VKNOWNMEASUREMENTS_H -#include "xml/vpatternimage.h" +#include "../ifc/xml/vpatternimage.h" #include #include diff --git a/src/libs/vformat/knownmeasurements/vknownmeasurementsdatabase.h b/src/libs/vformat/knownmeasurements/vknownmeasurementsdatabase.h index b226ec89d..1c65de267 100644 --- a/src/libs/vformat/knownmeasurements/vknownmeasurementsdatabase.h +++ b/src/libs/vformat/knownmeasurements/vknownmeasurementsdatabase.h @@ -28,13 +28,12 @@ #ifndef VKNOWNMEASUREMENTSDATABASE_H #define VKNOWNMEASUREMENTSDATABASE_H +#include "vknownmeasurements.h" #include #include #include #include -class VKnownMeasurements; - struct VKnownMeasurementsHeader { QUuid uid{}; diff --git a/src/libs/vmisc/vabstractapplication.cpp b/src/libs/vmisc/vabstractapplication.cpp index de5d8c65e..4b9165f06 100644 --- a/src/libs/vmisc/vabstractapplication.cpp +++ b/src/libs/vmisc/vabstractapplication.cpp @@ -28,6 +28,9 @@ #include "vabstractapplication.h" +#include "QtConcurrent/qtconcurrentrun.h" +#include "compatibility.h" +#include "svgfont/vsvgfontdatabase.h" #include #include #include @@ -40,10 +43,6 @@ #include #include -#include "QtConcurrent/qtconcurrentrun.h" -#include "compatibility.h" -#include "svgfont/vsvgfontdatabase.h" - #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #include "literals.h" #endif diff --git a/src/libs/vmisc/vcommonsettings.cpp b/src/libs/vmisc/vcommonsettings.cpp index d568d44f9..69b5ac3e7 100644 --- a/src/libs/vmisc/vcommonsettings.cpp +++ b/src/libs/vmisc/vcommonsettings.cpp @@ -350,9 +350,11 @@ auto VCommonSettings::GetPathSVGFonts() const -> QString //--------------------------------------------------------------------------------------------------------------------- void VCommonSettings::SetPathSVGFonts(const QString &value) { - const QString oldPath = GetDefPathSVGFonts(); + const QString oldPath = GetPathSVGFonts(); - setValue(*settingPathsSVGFonts, value); + QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename); + settings.setValue(*settingPathsSVGFonts, value); + settings.sync(); if (oldPath != value) { @@ -397,9 +399,16 @@ auto VCommonSettings::GetPathKnownMeasurements() const -> QString //--------------------------------------------------------------------------------------------------------------------- void VCommonSettings::SetPathKnownMeasurements(const QString &value) { + const QString oldPath = GetPathKnownMeasurements(); + QSettings settings(this->format(), this->scope(), this->organizationName(), *commonIniFilename); settings.setValue(*settingPathsKnownMeasurements, value); settings.sync(); + + if (oldPath != value) + { + emit KnownMeasurementsPathChanged(oldPath, value); + } } //--------------------------------------------------------------------------------------------------------------------- diff --git a/src/libs/vmisc/vcommonsettings.h b/src/libs/vmisc/vcommonsettings.h index da0e3c7b7..b49724dcc 100644 --- a/src/libs/vmisc/vcommonsettings.h +++ b/src/libs/vmisc/vcommonsettings.h @@ -344,6 +344,7 @@ public: signals: void SVGFontsPathChanged(const QString &oldPath, const QString &newPath); + void KnownMeasurementsPathChanged(const QString &oldPath, const QString &newPath); protected: template