]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
- Store persistent user preferences in a .INI file
authorMichal Malý <madcatxster@devoid-pointer.net>
Sat, 7 Mar 2015 15:30:13 +0000 (16:30 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Sat, 7 Mar 2015 15:30:13 +0000 (16:30 +0100)
- Connect sequence and single run loading dialogs to the persistent
storage and remember the last used path

Anyanka.pro
globalinfo.cpp
globalinfo.h
gui/mainwindow.cpp
main.cpp
usersettings.cpp [new file with mode: 0644]
usersettings.h [new file with mode: 0644]

index 23be617ef1403d41a6e06d4e37580d87f4bfca5c..61e0af366bc7c18f985d2886349f56cdafde19a6 100644 (file)
@@ -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 \
index b8023b2ab659c6c1849f8c94368491b094be79e9..0394ee334b0b6bb18fd089567efd43c75ace142a 100644 (file)
@@ -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");
index 2ce4883ecb437f03cd41932c8c909c6a14ef46c0..5f64d16ac1b50b5a8608286292e96e1b2799eadd 100644 (file)
@@ -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
index 6f45266d7bc3dfa1ef37470866cd853cb9d8cf25..66d9d2063ed2a5c878f93effb4b5f1329c3dff3c 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "gui/mainwindow.h"
 #include "ui_mainwindow.h"
+#include "usersettings.h"
 #include <QVBoxLayout>
 
 #include <QDebug>
@@ -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()));
   }
 }
 
index 11df5d4f3254ab61dbdaf753549e8a7dfa69a6a9..f44d9627bf1c8b4116d85dd4307aa1552ccffe7a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -23,6 +23,7 @@
 #include "gui/mainwindow.h"
 #include "datamanager.h"
 #include "logger.h"
+#include "usersettings.h"
 #include <QApplication>
 #include <QtWidgets/QMessageBox>
 
@@ -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<SignalView> 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 (file)
index 0000000..7bc2f8c
--- /dev/null
@@ -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<std::mutex> 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<std::mutex> 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 (file)
index 0000000..8db8fb1
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef USERSETTINGS_H
+#define USERSETTINGS_H
+
+#include <mutex>
+#include <QMap>
+#include <QSettings>
+#include <QVariant>
+
+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<QString, QVariant> m_settings;
+  std::mutex m_lock;
+
+
+  static UserSettings* s_me;
+  static const QStringList KNOWN_KEYS;
+};
+
+#endif // USERSETTINGS_H