Init measurements database.

This commit is contained in:
Roman Telezhynskyi 2023-10-28 09:55:21 +03:00
parent b145a25abf
commit 856e472587
9 changed files with 162 additions and 9 deletions

View File

@ -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 <QCommandLineParser>
#include <QDir>
#include <QFileOpenEvent>
#include <QFileSystemWatcher>
#include <QFuture>
#include <QGlobalStatic>
#include <QGridLayout>
#include <QLocalServer>
@ -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<MApplication *>(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<void> 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()
{

View File

@ -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<DialogMDataBase> m_dataBase{};
bool m_testMode{false};
VKnownMeasurementsDatabase *m_knownMeasurementsDatabase{nullptr};
QFileSystemWatcher *m_knownMeasurementsDatabaseWatcher{nullptr};
void Clean();

View File

@ -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 <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileSystemWatcher>
#include <QFuture>
#include <QIcon>
#include <QLoggingCategory>
#include <QMessageBox>
@ -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<void> 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 *
{

View File

@ -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<VLockGuard<QFile>> m_lockLog{};
std::shared_ptr<QTextStream> m_out{nullptr};
VKnownMeasurementsDatabase *m_knownMeasurementsDatabase{nullptr};
QFileSystemWatcher *m_knownMeasurementsDatabaseWatcher{nullptr};
static auto LogDirPath() -> QString;
static auto LogPath() -> QString;
static auto CreateLogDir() -> bool;

View File

@ -28,7 +28,7 @@
#ifndef VKNOWNMEASUREMENTS_H
#define VKNOWNMEASUREMENTS_H
#include "xml/vpatternimage.h"
#include "../ifc/xml/vpatternimage.h"
#include <QSharedDataPointer>
#include <QTypeInfo>

View File

@ -28,13 +28,12 @@
#ifndef VKNOWNMEASUREMENTSDATABASE_H
#define VKNOWNMEASUREMENTSDATABASE_H
#include "vknownmeasurements.h"
#include <QCache>
#include <QHash>
#include <QString>
#include <QUuid>
class VKnownMeasurements;
struct VKnownMeasurementsHeader
{
QUuid uid{};

View File

@ -28,6 +28,9 @@
#include "vabstractapplication.h"
#include "QtConcurrent/qtconcurrentrun.h"
#include "compatibility.h"
#include "svgfont/vsvgfontdatabase.h"
#include <QDir>
#include <QFileSystemWatcher>
#include <QFuture>
@ -40,10 +43,6 @@
#include <QWidget>
#include <QtDebug>
#include "QtConcurrent/qtconcurrentrun.h"
#include "compatibility.h"
#include "svgfont/vsvgfontdatabase.h"
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include "literals.h"
#endif

View File

@ -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);
}
}
//---------------------------------------------------------------------------------------------------------------------

View File

@ -344,6 +344,7 @@ public:
signals:
void SVGFontsPathChanged(const QString &oldPath, const QString &newPath);
void KnownMeasurementsPathChanged(const QString &oldPath, const QString &newPath);
protected:
template <typename T>