From 4ab33fdfbb23fdfed5327d402989fd53d79f5380 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 22 Aug 2015 19:02:25 +0200 Subject: main, ui: store config files in a predefined directory Settings files are now stored in Documents/opentrack-version. The transition path is to copy all .ini files into that directory, accessible through the "Profile" menu dropdown. Issue: #179 --- facetracknoir/main.ui | 1900 ++++++++++---------- facetracknoir/new_config.ui | 45 + facetracknoir/new_file_dialog.h | 48 + facetracknoir/ui.cpp | 172 +- facetracknoir/ui.h | 15 +- .../ftnoir_tracker_hat_settings.cpp | 21 +- opentrack/options.hpp | 53 +- 7 files changed, 1207 insertions(+), 1047 deletions(-) create mode 100644 facetracknoir/new_config.ui create mode 100644 facetracknoir/new_file_dialog.h mode change 100755 => 100644 facetracknoir/ui.cpp diff --git a/facetracknoir/main.ui b/facetracknoir/main.ui index 2328be40..f240a510 100644 --- a/facetracknoir/main.ui +++ b/facetracknoir/main.ui @@ -7,8 +7,8 @@ 0 0 - 708 - 504 + 707 + 492 @@ -53,13 +53,7 @@ 0 - - - 0 - - - 0 - + 0 @@ -72,728 +66,755 @@ 0 + + 0 + - - - - 0 - 0 - - - - - 480 - 360 - - - - - 480 - 360 - - - - - - 0 - 0 - 480 - 360 - - - - - 0 - 0 - - - - - - 0 - 0 - 480 - 360 - - - - - 0 - 0 - - - - - 480 - 360 - - - - - 480 - 360 - - - - - - - - - - - + QFrame::NoFrame - - 0 - - + - 12 + 0 - 6 + 0 - 12 + 0 - 8 + 0 - 4 + 0 - + - + 0 0 - + - 80 - 90 + 480 + 360 - - - - - - - 0 - 0 - - - - Raw tracker data + + + 480 + 360 + - - - 0 - - - 0 + + + + 0 + 0 + 480 + 360 + - - 0 - - - 0 + + + 0 + 0 + - - 3 - - - 2 - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Plain - - - 1 - - - true - - - 4 - - - QLCDNumber::Outline - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - yaw - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - roll - - - - - - - true - - - - 0 - 0 - - - - false - - - QFrame::Raised - - - TZ - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - pitch - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Plain - - - 1 - - - true - - - 4 - - - QLCDNumber::Outline - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Plain - - - 1 - - - true - - - 4 - - - QLCDNumber::Outline - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Plain - - - 1 - - - true - - - 4 - - - QLCDNumber::Outline - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Plain - - - 1 - - - true - - - 4 - - - QLCDNumber::Outline - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - TX - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - QFrame::Plain - - - 1 - - - true - - - 4 - - - QLCDNumber::Outline - - - - - - - true - - - - 0 - 0 - - - - false - - - QFrame::Raised - - - TY - - - - + + + + 0 + 0 + 480 + 360 + + + + + 0 + 0 + + + + + 480 + 360 + + + + + 480 + 360 + + + + + + + - - - - 0 - 0 - + + + QFrame::NoFrame - - Game data + + 0 - + + + 12 + - 0 + 6 - 0 - - - 0 - - - 0 - - - 3 - - - 2 - - - - - true - - - - 0 - 0 - - - - false - - - QFrame::Raised - - - TY - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - pitch - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - true - - - 4 - - - QLCDNumber::Flat - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - yaw - - - - - - - - 0 - 0 - - - - QFrame::Raised - - - roll - - - - - - - true - - - - 0 - 0 - - - - false - - - QFrame::Raised - - - TZ - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - true - - - 4 - - - QLCDNumber::Flat - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - true - - - 4 - - - QLCDNumber::Flat - - - - - - - true - - - - 0 - 0 - - - - QFrame::NoFrame - - - true - - - 4 - - - QLCDNumber::Flat - - - - - - - true - + 12 + + + 8 + + + 4 + + + - + 0 0 - - QFrame::NoFrame - - - true - - - 4 - - - QLCDNumber::Flat + + + 80 + 90 + - - + + - + 0 0 - - QFrame::Raised - - - TX - + + Raw tracker data + + + + 0 + + + 0 + + + 0 + + + 0 + + + 3 + + + 2 + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 1 + + + true + + + 4 + + + QLCDNumber::Outline + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + yaw + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + roll + + + + + + + true + + + + 0 + 0 + + + + false + + + QFrame::Raised + + + TZ + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + pitch + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 1 + + + true + + + 4 + + + QLCDNumber::Outline + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 1 + + + true + + + 4 + + + QLCDNumber::Outline + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 1 + + + true + + + 4 + + + QLCDNumber::Outline + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 1 + + + true + + + 4 + + + QLCDNumber::Outline + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + TX + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + QFrame::Plain + + + 1 + + + true + + + 4 + + + QLCDNumber::Outline + + + + + + + true + + + + 0 + 0 + + + + false + + + QFrame::Raised + + + TY + + + + - - - - true - + + - + 0 0 - - QFrame::NoFrame - - - true - - - 4 - - - QLCDNumber::Flat - + + Game data + + + + 0 + + + 0 + + + 0 + + + 0 + + + 3 + + + 2 + + + + + true + + + + 0 + 0 + + + + false + + + QFrame::Raised + + + TY + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + pitch + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + true + + + 4 + + + QLCDNumber::Flat + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + yaw + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + roll + + + + + + + true + + + + 0 + 0 + + + + false + + + QFrame::Raised + + + TZ + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + true + + + 4 + + + QLCDNumber::Flat + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + true + + + 4 + + + QLCDNumber::Flat + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + true + + + 4 + + + QLCDNumber::Flat + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + true + + + 4 + + + QLCDNumber::Flat + + + + + + + + 0 + 0 + + + + QFrame::Raised + + + TX + + + + + + + true + + + + 0 + 0 + + + + QFrame::NoFrame + + + true + + + 4 + + + QLCDNumber::Flat + + + + @@ -802,112 +823,142 @@ - + + + + 0 + 0 + + + + 4 + - 0 + 8 + + + 10 0 - 6 + 4 0 - - + + + + + 4 + 0 + + - Controls + Settings true - + + + 3 + 4 - 12 + 2 - 4 + 0 - 12 + 6 - - - - false - - - - 0 - 0 - - - - Stop - - - - - + + - + 0 - 0 + 2 - - Start - + + + 0 + + + 0 + + + 2 + + + 2 + + + + + true + + + + 0 + 0 + + + + Profile + + + QToolButton::InstantPopup + + + Qt::ToolButtonTextBesideIcon + + + true + + + Qt::DownArrow + + + + + + + + 0 + 0 + + + + 10 + + + + - - - - - - - Settings - - - true - - - - 4 - - - 0 - - - 4 - - - 4 - - - 2 - - - + + - + 0 - 0 + 3 - Options + Mapping - :/images/tools.png:/images/tools.png + :/images/curves.png:/images/curves.png @@ -917,20 +968,20 @@ - - + + - + 0 - 0 + 3 - Mapping + Options - :/images/curves.png:/images/curves.png + :/images/tools.png:/images/tools.png @@ -943,230 +994,225 @@ - - + + + + + 3 + 0 + + - Filter + Controls true - + + + 8 + - 4 + 0 - 0 + 8 - 4 + 0 - 4 - - - 3 - - - 2 + 9 - - - - - - - true + + + + + 0 + 0 + - ... - - - false + Start - - - - - - - Tracker - - - true - - - - 4 - - - 0 - - - 4 - - - 4 - - - 3 - - - 2 - - - - - - + + - true - - - ... - - false - - - - - - - - - Protocol - - - true - - - - 4 - - - 0 - - - 4 - - - 4 - - - 3 - - - 2 - - - - - true + + + 0 + 0 + - ... - - - false + Stop - - - - + - - 0 + + 4 0 - - Profile + + QFrame::NoFrame - - true + + QFrame::Raised - + + + 2 + - 4 + 3 - 0 + 2 - 4 + 3 - 4 + 8 - - 2 - - - - - - 0 - 0 - - - - 10 - - - - - - - - 0 - 0 - - - - Load - + + + + Tracker + + + + 4 + + + 0 + + + 4 + + + 0 + + + 3 + + + 0 + + + + + + + + true + + + ... + + + false + + + + - - - - true - - - - 0 - 0 - - - - Save - + + + + Protocol + + + + 4 + + + 0 + + + 4 + + + 0 + + + 3 + + + 0 + + + + + + + + true + + + ... + + + false + + + + - - - - true - - - - 0 - 0 - - - - Save... - + + + + Filter + + + + 4 + + + 0 + + + 4 + + + 0 + + + 3 + + + 0 + + + + + + + + true + + + ... + + + false + + + + @@ -1197,10 +1243,6 @@ btnShowServerControls iconcomboFilter btnShowFilterControls - iconcomboProfile - btnLoad - btnSave - btnSaveAs btnEditCurves btnShortcuts diff --git a/facetracknoir/new_config.ui b/facetracknoir/new_config.ui new file mode 100644 index 00000000..27dce0f8 --- /dev/null +++ b/facetracknoir/new_config.ui @@ -0,0 +1,45 @@ + + + UI_new_config + + + Qt::ApplicationModal + + + + 0 + 0 + 269 + 67 + + + + Config filename + + + + images/facetracknoir.pngimages/facetracknoir.png + + + + + + New file name: + + + + + + + + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + diff --git a/facetracknoir/new_file_dialog.h b/facetracknoir/new_file_dialog.h new file mode 100644 index 00000000..dc821592 --- /dev/null +++ b/facetracknoir/new_file_dialog.h @@ -0,0 +1,48 @@ +#pragma once + +#include "ui_new_config.h" +#include "opentrack/options.hpp" +#include +#include +#include +#include + +class new_file_dialog : public QDialog +{ + Q_OBJECT +public: + new_file_dialog(QWidget* parent = 0) : QDialog(parent), ok(false) + { + ui.setupUi(this); + connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(ok_clicked())); + connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(cancel_clicked())); + setFixedSize(size()); + } + bool is_ok(QString& name_) + { + name_ = name; + return ok; + } +private: + Ui::UI_new_config ui; + bool ok; + QString name; +private slots: + void cancel_clicked() { close(); } + void ok_clicked() + { + QString text = ui.lineEdit->text(); + text = text.replace('/', ""); + text = text.replace('\\', ""); + if (text == "" || QFile(options::group::ini_directory() + "/" + text).exists()) + { + QMessageBox::warning(this, + "File exists", "This file already exists. Pick another name.", + QMessageBox::Ok, QMessageBox::NoButton); + return; + } + ok = true; + close(); + name = text; + } +}; diff --git a/facetracknoir/ui.cpp b/facetracknoir/ui.cpp old mode 100755 new mode 100644 index 3f87ca2f..958a2439 --- a/facetracknoir/ui.cpp +++ b/facetracknoir/ui.cpp @@ -33,7 +33,10 @@ #include "ui.h" #include "opentrack/tracker.h" #include "opentrack/options.hpp" +#include "facetracknoir/new_file_dialog.h" #include +#include +#include #ifndef _WIN32 # include @@ -53,10 +56,6 @@ MainWindow::MainWindow() : updateButtonState(false, false); ui.video_frame_label->setPixmap(no_feed_pixmap); - connect(ui.btnLoad, SIGNAL(clicked()), this, SLOT(open())); - connect(ui.btnSave, SIGNAL(clicked()), this, SLOT(save())); - connect(ui.btnSaveAs, SIGNAL(clicked()), this, SLOT(saveAs())); - connect(ui.btnEditCurves, SIGNAL(clicked()), this, SLOT(showCurveConfiguration())); connect(ui.btnShortcuts, SIGNAL(clicked()), this, SLOT(showKeyboardShortcuts())); connect(ui.btnShowEngineControls, SIGNAL(clicked()), this, SLOT(showTrackerSettings())); @@ -74,7 +73,7 @@ MainWindow::MainWindow() : for (auto x : modules.filters()) ui.iconcomboFilter->addItem(x->icon, x->name); - fill_profile_combobox(); + refresh_config_list(); tie_setting(s.tracker_dll, ui.iconcomboTrackerSource); tie_setting(s.protocol_dll, ui.iconcomboProtocol); @@ -82,15 +81,15 @@ MainWindow::MainWindow() : connect(ui.iconcomboTrackerSource, static_cast(&QComboBox::currentIndexChanged), - [&](int) -> void { if (pTrackerDialog) pTrackerDialog = nullptr; }); + [&](int) -> void { if (pTrackerDialog) pTrackerDialog = nullptr; save(); }); connect(ui.iconcomboProtocol, static_cast(&QComboBox::currentIndexChanged), - [&](int) -> void { if (pProtocolDialog) pProtocolDialog = nullptr; }); + [&](int) -> void { if (pProtocolDialog) pProtocolDialog = nullptr; save(); }); connect(ui.iconcomboFilter, static_cast(&QComboBox::currentIndexChanged), - [&](int) -> void { if (pFilterDialog) pFilterDialog = nullptr; }); + [&](int) -> void { if (pFilterDialog) pFilterDialog = nullptr; save(); }); connect(ui.btnStartTracker, SIGNAL(clicked()), this, SLOT(startTracker())); connect(ui.btnStopTracker, SIGNAL(clicked()), this, SLOT(stopTracker())); @@ -98,14 +97,46 @@ MainWindow::MainWindow() : connect(&pose_update_timer, SIGNAL(timeout()), this, SLOT(showHeadPose())); connect(&kbd_quit, SIGNAL(activated()), this, SLOT(exit())); + + auto menu = new QMenu; + menu->addAction("Create new empty config", this, SLOT(make_empty_config())); + menu->addAction("Create new copied config", this, SLOT(make_copied_config())); + menu->addAction("Open configuration directory", this, SLOT(open_config_directory())); + menu->addAction("Refresh configuration list", this, SLOT(refresh_config_list())); + ui.profile_button->setMenu(menu); + kbd_quit.setEnabled(true); connect(&det_timer, SIGNAL(timeout()), this, SLOT(maybe_start_profile_from_executable())); det_timer.start(1000); ensure_tray(); - set_working_directory(); + + if (!QFile(group::ini_pathname()).exists()) + { + set_profile(OPENTRACK_DEFAULT_CONFIG); + if (!QFile(group::ini_pathname()).exists()) + { + const auto pathname = group::ini_pathname(); + QFile file(pathname); + if (file.open(QFile::ReadWrite)) + { + QTextStream stream(&file); + stream << "\n"; + } + } + } +} + +bool MainWindow::get_new_config_name_from_dialog(QString& ret) +{ + new_file_dialog dlg; + dlg.exec(); + bool b = dlg.is_ok(ret); + if (b && !ret.endsWith(".ini")) + ret += ".ini"; + return b; } MainWindow::~MainWindow() @@ -121,27 +152,6 @@ void MainWindow::set_working_directory() QDir::setCurrent(QCoreApplication::applicationDirPath()); } -void MainWindow::open() { - QFileDialog dialog(this); - dialog.setFileMode(QFileDialog::ExistingFile); - QString dir_path = QFileInfo(group::ini_pathname()).absolutePath(); - QString fileName = dialog.getOpenFileName( - this, - tr("Open the settings file"), - dir_path, - tr("Settings file (*.ini);;All Files (*)")); - set_working_directory(); - - if (!fileName.isEmpty()) { - { - QSettings settings(OPENTRACK_ORG); - settings.setValue(OPENTRACK_CONFIG_FILENAME_KEY, remove_app_path(fileName)); - } - fill_profile_combobox(); - load_settings(); - } -} - void MainWindow::save_mappings() { pose.save_mappings(); } @@ -164,29 +174,6 @@ void MainWindow::save() { #endif } -void MainWindow::saveAs() -{ - QString oldFile = group::ini_pathname(); - QString fileName = QFileDialog::getSaveFileName(this, tr("Save file"), - oldFile, - tr("Settings file (*.ini);;All Files (*)")); - set_working_directory(); - - if (fileName.isEmpty()) - return; - - (void) QFile::remove(fileName); - - { - (void) QFile::copy(oldFile, fileName); - QSettings settings(OPENTRACK_ORG); - settings.setValue (OPENTRACK_CONFIG_FILENAME_KEY, remove_app_path(fileName)); - } - - save(); - fill_profile_combobox(); -} - void MainWindow::load_mappings() { pose.load_mappings(); } @@ -196,10 +183,59 @@ void MainWindow::load_settings() { load_mappings(); } +void MainWindow::make_empty_config() +{ + QString name; + const QString dir = group::ini_directory(); + if (dir != "" && get_new_config_name_from_dialog(name)) + { + QFile filename(dir + "/" + name); + if (filename.open(QFile::ReadWrite)) + { + QTextStream stream(&filename); + stream << "\n"; + refresh_config_list(); + } + } +} + +void MainWindow::make_copied_config() +{ + const QString dir = group::ini_directory(); + const QString cur = group::ini_pathname(); + QString name; + if (cur != "" && dir != "" && get_new_config_name_from_dialog(name)) + { + const QString new_name = dir + "/" + name; + (void) QFile::remove(new_name); + (void) QFile::copy(cur, new_name); + refresh_config_list(); + } +} + +void MainWindow::open_config_directory() +{ + const QString path = group::ini_directory(); + if (path != "") + { + QDesktopServices::openUrl("file:///" + QDir::toNativeSeparators(path)); + } +} + extern "C" volatile const char* opentrack_version; -void MainWindow::fill_profile_combobox() +void MainWindow::refresh_config_list() { + if (group::ini_list().size() == 0) + { + QFile filename(group::ini_directory() + "/" OPENTRACK_DEFAULT_CONFIG); + if (filename.open(QFile::ReadWrite)) + { + QTextStream stream(&filename); + stream << "\n"; + } + } + QStringList ini_list = group::ini_list(); set_title(); QString current = QFileInfo(group::ini_pathname()).fileName(); @@ -219,8 +255,7 @@ void MainWindow::updateButtonState(bool running, bool inertialp) ui.iconcomboFilter->setEnabled ( not_running ); ui.iconcomboTrackerSource->setEnabled(not_running); ui.video_frame_label->setVisible(not_running || inertialp); - ui.btnSaveAs->setEnabled(not_running); - ui.btnLoad->setEnabled(not_running); + ui.profile_button->setEnabled(not_running); } void MainWindow::bindKeyboardShortcuts() @@ -440,26 +475,6 @@ void MainWindow::exit() { QCoreApplication::exit(0); } -QString MainWindow::remove_app_path(const QString full_path) -{ - QFileInfo path_info(full_path); - QString path = path_info.absolutePath(); - - QFileInfo app_path(QCoreApplication::applicationDirPath()); - QString app_prefix(app_path.absoluteFilePath()); - - if (path == app_prefix) - { - path = "."; - } - else if (path.startsWith(app_prefix + "/")) - { - path = "./" + path.mid(app_prefix.size() + 1); - } - - return path + "/" + path_info.fileName(); -} - void MainWindow::profileSelected(int index) { if (index == -1) @@ -467,8 +482,7 @@ void MainWindow::profileSelected(int index) { QSettings settings(OPENTRACK_ORG); - settings.setValue (OPENTRACK_CONFIG_FILENAME_KEY, remove_app_path(QFileInfo(group::ini_pathname()).absolutePath() + "/" + - ui.iconcomboProfile->itemText(index))); + settings.setValue (OPENTRACK_CONFIG_FILENAME_KEY, ui.iconcomboProfile->itemText(index)); } set_title(); @@ -550,5 +564,5 @@ void MainWindow::maybe_start_profile_from_executable() void MainWindow::set_profile(const QString &profile) { QSettings settings(OPENTRACK_ORG); - settings.setValue(OPENTRACK_CONFIG_FILENAME_KEY, MainWindow::remove_app_path(profile)); + settings.setValue(OPENTRACK_CONFIG_FILENAME_KEY, profile); } diff --git a/facetracknoir/ui.h b/facetracknoir/ui.h index 097b2439..0c3063d4 100644 --- a/facetracknoir/ui.h +++ b/facetracknoir/ui.h @@ -93,22 +93,20 @@ class MainWindow : public QMainWindow, private State void changeEvent(QEvent* e) override; - void createIconGroupBox(); void load_settings(); void updateButtonState(bool running, bool inertialp); - void fill_profile_combobox(); void display_pose(const double* mapped, const double* raw); void ensure_tray(); void set_title(const QString& game_title = QStringLiteral("")); + void save(); + static bool get_new_config_name_from_dialog(QString &ret); + static void set_profile(const QString& profile); public slots: void shortcutRecentered(); void shortcutToggled(); void shortcutZeroed(); void bindKeyboardShortcuts(); private slots: - void open(); - void save(); - void saveAs(); void exit(); void profileSelected(int index); @@ -121,6 +119,11 @@ private slots: void restore_from_tray(QSystemTrayIcon::ActivationReason); void maybe_start_profile_from_executable(); + + void make_empty_config(); + void make_copied_config(); + void open_config_directory(); + void refresh_config_list(); public slots: void startTracker(); void stopTracker(); @@ -129,7 +132,5 @@ public: ~MainWindow(); void save_mappings(); void load_mappings(); - static QString remove_app_path(const QString full_path); static void set_working_directory(); - static void set_profile(const QString& profile); }; diff --git a/ftnoir_tracker_hatire/ftnoir_tracker_hat_settings.cpp b/ftnoir_tracker_hatire/ftnoir_tracker_hat_settings.cpp index 80543e7a..0a4454e4 100644 --- a/ftnoir_tracker_hatire/ftnoir_tracker_hat_settings.cpp +++ b/ftnoir_tracker_hatire/ftnoir_tracker_hat_settings.cpp @@ -35,15 +35,11 @@ void TrackerSettings::load_ini() { -#ifdef OPENTRACK_API +#ifndef OPENTRACK_API QSettings settings(OPENTRACK_ORG); // Registry settings (in HK_USER) + QString currentFile = settings.value( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString(); #else - QSettings settings("opentrack"); // Registry settings (in HK_USER) -#endif -#ifdef OPENTRACK_API - QString currentFile = settings.value( OPENTRACK_CONFIG_FILENAME_KEY, QCoreApplication::applicationDirPath() + OPENTRACK_DEFAULT_CONFIG_PATH ).toString(); -#else - QString currentFile = settings.value( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString(); + QString currentFile = options::group::ini_pathname(); #endif QSettings iniFile( currentFile, QSettings::IniFormat ); // Application settings (in INI-file) @@ -101,19 +97,14 @@ void TrackerSettings::load_ini() iniFile.endGroup(); } - void TrackerSettings::save_ini() const { -#ifdef OPENTRACK_API +#ifndef OPENTRACK_API QSettings settings(OPENTRACK_ORG); // Registry settings (in HK_USER) + QString currentFile = settings.value( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString(); #else - QSettings settings("opentrack"); // Registry settings (in HK_USER) -#endif -#ifdef OPENTRACK_API - QString currentFile = settings.value( OPENTRACK_CONFIG_FILENAME_KEY, QCoreApplication::applicationDirPath() + OPENTRACK_DEFAULT_CONFIG_PATH ).toString(); -#else - QString currentFile = settings.value( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString(); + QString currentFile = options::group::ini_pathname(); #endif QSettings iniFile( currentFile, QSettings::IniFormat ); // Application settings (in INI-file) diff --git a/opentrack/options.hpp b/opentrack/options.hpp index f4deb8a1..8e623092 100644 --- a/opentrack/options.hpp +++ b/opentrack/options.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include @@ -38,8 +39,8 @@ #include template using mem = std::shared_ptr; -#define OPENTRACK_CONFIG_FILENAME_KEY "settings-file" -#define OPENTRACK_DEFAULT_CONFIG_PATH "/settings/default.ini" +#define OPENTRACK_CONFIG_FILENAME_KEY "settings-filename" +#define OPENTRACK_DEFAULT_CONFIG "default.ini" #define OPENTRACK_ORG "opentrack-2.3" namespace options { @@ -90,30 +91,30 @@ namespace options { public: group(const string& name) : name(name) { - QSettings conf(ini_pathname(), QSettings::IniFormat); + auto conf = ini_file(); auto q_name = QString::fromStdString(name); - conf.beginGroup(q_name); - for (auto& k_ : conf.childKeys()) + conf->beginGroup(q_name); + for (auto& k_ : conf->childKeys()) { auto tmp = k_.toUtf8(); string k(tmp); - kvs[k] = conf.value(k_); + kvs[k] = conf->value(k_); } - conf.endGroup(); + conf->endGroup(); } void save() { - QSettings s(ini_pathname(), QSettings::IniFormat); + auto s = ini_file(); auto q_name = QString::fromStdString(name); - s.beginGroup(q_name); + s->beginGroup(q_name); for (auto& i : kvs) { auto k = QString::fromStdString(i.first); - s.setValue(k, i.second); + s->setValue(k, i.second); } - s.endGroup(); - s.sync(); + s->endGroup(); + s->sync(); } template @@ -132,22 +133,40 @@ namespace options { return kvs.count(s) != 0; } - static const QString ini_pathname() + static QString ini_directory() { + const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + if (dirs.size() == 0) + return ""; + if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) + return dirs[0] + "/" OPENTRACK_ORG; + return ""; + } + + static QString ini_pathname() + { + const auto dir = ini_directory(); + if (dir == "") + return ""; QSettings settings(OPENTRACK_ORG); - return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, QCoreApplication::applicationDirPath() + OPENTRACK_DEFAULT_CONFIG_PATH).toString(); + return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); } static const QStringList ini_list() { - QFileInfo info(group::ini_pathname()); - QDir settings_dir(info.dir()); + const auto dirname = ini_directory(); + if (dirname == "") + return QStringList(); + QDir settings_dir(dirname); return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); } static const mem ini_file() { - return std::make_shared(ini_pathname(), QSettings::IniFormat); + const auto pathname = ini_pathname(); + if (pathname != "") + return std::make_shared(ini_pathname(), QSettings::IniFormat); + return std::make_shared(); } }; -- cgit v1.2.3