From 9195a0531f0388575dfbe16df7f3563a988e7253 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Sat, 7 Mar 2015 16:30:13 +0100 Subject: [PATCH] - Store persistent user preferences in a .INI file - Connect sequence and single run loading dialogs to the persistent storage and remember the last used path --- Anyanka.pro | 6 ++-- globalinfo.cpp | 2 ++ globalinfo.h | 2 ++ gui/mainwindow.cpp | 14 ++++++-- main.cpp | 5 +++ usersettings.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++ usersettings.h | 30 +++++++++++++++++ 7 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 usersettings.cpp create mode 100644 usersettings.h diff --git a/Anyanka.pro b/Anyanka.pro index 23be617..61e0af3 100644 --- a/Anyanka.pro +++ b/Anyanka.pro @@ -59,7 +59,8 @@ SOURCES += main.cpp\ jsonserializable.cpp \ libHPCS/libhpcs.c \ gui/failedfilesdialog.cpp \ - helpers.cpp + helpers.cpp \ + usersettings.cpp HEADERS += \ datafilesloader.h \ @@ -94,7 +95,8 @@ HEADERS += \ jsonserializable.h \ helpers.h \ libHPCS/libhpcs.h \ - gui/failedfilesdialog.h + gui/failedfilesdialog.h \ + usersettings.h FORMS += \ gui/mainwindow.ui \ diff --git a/globalinfo.cpp b/globalinfo.cpp index b8023b2..0394ee3 100644 --- a/globalinfo.cpp +++ b/globalinfo.cpp @@ -26,3 +26,5 @@ const int GlobalInfo::APP_VERSION_MAJ(0); const int GlobalInfo::APP_VERSION_MIN(3); const QString GlobalInfo::APP_VERSION_REV("e"); +const QString GlobalInfo::APP_NAME("Anyanka"); +const QString GlobalInfo::ORG_NAME("devoid-pointer.net"); diff --git a/globalinfo.h b/globalinfo.h index 2ce4883..5f64d16 100644 --- a/globalinfo.h +++ b/globalinfo.h @@ -37,6 +37,8 @@ public: static const int APP_VERSION_MAJ; static const int APP_VERSION_MIN; static const QString APP_VERSION_REV; + static const QString APP_NAME; + static const QString ORG_NAME; }; #endif // GLOBALINFO_H diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 6f45266..66d9d20 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -23,6 +23,7 @@ #include "gui/mainwindow.h" #include "ui_mainwindow.h" +#include "usersettings.h" #include #include @@ -40,7 +41,7 @@ MainWindow::MainWindow(QWidget *parent) : m_loadDataFileDialog = new QFileDialog(this); m_loadDataFileDialog->setFileMode(QFileDialog::Directory); - m_loadDataFileDialog->setOptions(QFileDialog::ReadOnly); + m_loadDataFileDialog->setOptions(QFileDialog::ReadOnly | QFileDialog::DontUseNativeDialog); } /* Public methods */ @@ -161,17 +162,26 @@ void MainWindow::onIntegrateIntersectionSelected() void MainWindow::onLoadSequence() { + QVariant path = UserSettings::get("last_sequence_directory"); + if (path.isValid()) + m_loadDataFileDialog->setDirectory(path.toString()); m_loadDataFileDialog->setWindowTitle("Load sequence"); - if (m_loadDataFileDialog->exec() == QDialog::Accepted) + if (m_loadDataFileDialog->exec() == QDialog::Accepted) { emit loadSequence(m_loadDataFileDialog->selectedFiles()[0]); + UserSettings::set("last_sequence_directory", QVariant(m_loadDataFileDialog->directory().absolutePath())); + } } void MainWindow::onLoadSingleRun() { + QVariant path = UserSettings::get("last_singlerun_directory"); + if (path.isValid()) + m_loadDataFileDialog->setDirectory(path.toString()); m_loadDataFileDialog->setWindowTitle("Load single run"); if (m_loadDataFileDialog->exec() == QDialog::Accepted) { qDebug() << "Chosen file" << m_loadDataFileDialog->selectedFiles(); emit loadSingleRun(m_loadDataFileDialog->selectedFiles()[0]); + UserSettings::set("last_singlerun_directory", QVariant(m_loadDataFileDialog->directory().absolutePath())); } } diff --git a/main.cpp b/main.cpp index 11df5d4..f44d962 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ #include "gui/mainwindow.h" #include "datamanager.h" #include "logger.h" +#include "usersettings.h" #include #include @@ -30,6 +31,9 @@ int main(int argc, char *argv[]) { int ret; QApplication a(argc, argv); + a.setApplicationName(GlobalInfo::APP_NAME); + a.setOrganizationName(GlobalInfo::ORG_NAME); + UserSettings::initialize(); #ifndef _MSC_VER #warning Revisit the metatype shared_ptr registration @@ -68,6 +72,7 @@ int main(int argc, char *argv[]) ret = a.exec(); dMgr->saveUserDataOnExit(); + UserSettings::dispose(); return ret; } diff --git a/usersettings.cpp b/usersettings.cpp new file mode 100644 index 0000000..7bc2f8c --- /dev/null +++ b/usersettings.cpp @@ -0,0 +1,84 @@ +#include "globalinfo.h" +#include "logger.h" +#include "usersettings.h" + +UserSettings* UserSettings::s_me = nullptr; +const QStringList UserSettings::KNOWN_KEYS = QStringList() << "last_sequence_directory" /* Directory from which the last sequence was loaded */ + << "last_singlerun_directory"; /* Directory from which the last single run was loaded */ +void UserSettings::dispose() +{ + delete s_me; + s_me = nullptr; +} + +QVariant UserSettings::get(const QString key) +{ + if (s_me == nullptr) { + Logger::log(Logger::Level::ERROR, "UserSettings", __QFUNC__ + "UserSettings not initialized"); + return QVariant(); + } + + std::lock_guard lk(s_me->m_lock); + QVariant v = s_me->m_settings[key.toLower()]; + Logger::log(Logger::Level::DEBUG, "UserSettings ", __QFUNC__ + QString("Key: %1, Value: %2").arg(key).arg(v.toString())); + return v; +} + +void UserSettings::initialize() +{ + if (s_me == nullptr) + s_me = new UserSettings; +} + +void UserSettings::set(const QString key, const QVariant value) +{ + if (s_me == nullptr) { + Logger::log(Logger::Level::ERROR, "UserSettings", __QFUNC__ + "UserSettings not initialized"); + return; + } + + QString lkey = key.toLower(); + std::lock_guard lk(s_me->m_lock); + if (!KNOWN_KEYS.contains(lkey)) + return; + + QVariant svalue; + if (value.type() == QVariant::String) { + QString s = value.toString(); + s.replace("\\", "\\\\"); /* Propery escape backslashes in Windows paths */ + svalue = QVariant(s); + } else + svalue = value; + + s_me->m_settings[lkey] = svalue; +} + +UserSettings::UserSettings() +{ + QSettings* settings = openSettingsFile(); + + for (const QString& k : KNOWN_KEYS) { + QVariant v = settings->value(k); + m_settings.insert(k, v); + } + + delete settings; +} + +QSettings* UserSettings::openSettingsFile() +{ + return new QSettings(QSettings::IniFormat, QSettings::UserScope, GlobalInfo::ORG_NAME, GlobalInfo::APP_NAME); +} + +UserSettings::~UserSettings() +{ + QSettings* settings = openSettingsFile(); + + for (const QString& k : KNOWN_KEYS) { + QVariant v = m_settings[k]; + settings->setValue(k, v); + } + settings->sync(); + + delete settings; +} diff --git a/usersettings.h b/usersettings.h new file mode 100644 index 0000000..8db8fb1 --- /dev/null +++ b/usersettings.h @@ -0,0 +1,30 @@ +#ifndef USERSETTINGS_H +#define USERSETTINGS_H + +#include +#include +#include +#include + +class UserSettings +{ +public: + static void dispose(); + static QVariant get(const QString key); + static void initialize(); + static void set(const QString key, const QVariant value); + ~UserSettings(); + +private: + UserSettings(); + QSettings* openSettingsFile(); + + QMap m_settings; + std::mutex m_lock; + + + static UserSettings* s_me; + static const QStringList KNOWN_KEYS; +}; + +#endif // USERSETTINGS_H -- 2.43.5