From b4ea0194f189a80f551ee6f2dbc32e6c3e33daae Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Fri, 17 Feb 2017 12:32:47 +0000 Subject: [PATCH] Support for a startup configuration The --config (-c for short) command line option can be used to select an existing configuration at start up. If the configuration does not exist the last configuration used is selected. git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@7567 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- MultiSettings.cpp | 171 +++++++++++++++++++++++++--------------------- MultiSettings.hpp | 74 ++++++++++---------- main.cpp | 11 ++- 3 files changed, 142 insertions(+), 114 deletions(-) diff --git a/MultiSettings.cpp b/MultiSettings.cpp index 8c00164ea..e06496b23 100644 --- a/MultiSettings.cpp +++ b/MultiSettings.cpp @@ -144,7 +144,7 @@ class MultiSettings::impl final Q_OBJECT public: - explicit impl (MultiSettings const * parent); + explicit impl (MultiSettings const * parent, QString const& config_name); bool reposition (); void create_menu_actions (QMainWindow * main_window, QMenu * menu); bool exit (); @@ -201,8 +201,8 @@ private: #include "MultiSettings.moc" #include "moc_MultiSettings.cpp" -MultiSettings::MultiSettings () - : m_ {this} +MultiSettings::MultiSettings (QString const& config_name) + : m_ {this, config_name} { } @@ -263,7 +263,7 @@ bool MultiSettings::exit () return m_->exit (); } -MultiSettings::impl::impl (MultiSettings const * parent) +MultiSettings::impl::impl (MultiSettings const * parent, QString const& config_name) : settings_ {settings_path (), QSettings::IniFormat} , parent_ {parent} , name_change_emit_pending_ {true} @@ -303,15 +303,33 @@ MultiSettings::impl::impl (MultiSettings const * parent) } // bootstrap + QStringList available_configurations; { SettingsGroup alternatives {&settings_, multi_settings_root_group}; + available_configurations = settings_.childGroups (); + // use last selected configuration current_ = settings_.value (multi_settings_current_name_key).toString (); if (!current_.size ()) { + // no configurations so use default name current_ = tr (default_string); settings_.setValue (multi_settings_current_name_key, current_); } } + + if (config_name.size () && available_configurations.contains (config_name) && config_name != current_) + { + // switch to specified configuration + { + SettingsGroup alternatives {&settings_, multi_settings_root_group}; + // save the target settings + SettingsGroup target_group {&settings_, config_name}; + new_settings_ = get_settings (); + } + current_ = config_name; + reposition_type_ = RepositionType::save_and_replace; + reposition (); + } settings_.sync (); } @@ -394,9 +412,8 @@ void MultiSettings::impl::create_menu_actions (QMainWindow * main_window, QMenu // and set as the current configuration default_menu->menuAction ()->setChecked (true); - QStringList available_configurations; // get the existing alternatives - available_configurations = settings_.childGroups (); + auto const& available_configurations {settings_.childGroups ()}; // add all the other configurations for (auto const& configuration_name: available_configurations) @@ -539,14 +556,14 @@ void MultiSettings::impl::select_configuration (QMainWindow * main_window, QMenu if (target_name != current_) { { - auto const& current_group = settings_.group (); - if (current_group.size ()) settings_.endGroup (); - // position to the alternative settings - SettingsGroup alternatives {&settings_, multi_settings_root_group}; - // save the target settings - SettingsGroup target_group {&settings_, target_name}; - new_settings_ = get_settings (); - if (current_group.size ()) settings_.beginGroup (current_group); + auto const& current_group = settings_.group (); + if (current_group.size ()) settings_.endGroup (); + // position to the alternative settings + SettingsGroup alternatives {&settings_, multi_settings_root_group}; + // save the target settings + SettingsGroup target_group {&settings_, target_name}; + new_settings_ = get_settings (); + if (current_group.size ()) settings_.beginGroup (current_group); } // and set up the restart current_ = target_name; @@ -621,41 +638,41 @@ void MultiSettings::impl::clone_into_configuration (QMainWindow * main_window, Q { QString source_name {1 == sources.size () ? sources.at (0) : dialog.name ()}; if (MessageBox::Yes == MessageBox::query_message (main_window, - tr ("Clone Into Configuration"), - tr ("Confirm overwrite of all values for configuration \"%1\" with values from \"%2\"?") - .arg (target_name) - .arg (source_name))) - { - // grab the data to clone from - if (source_name == current_group_name) - { - // grab the data to clone from the current settings - new_settings_ = get_settings (); - } - else - { - SettingsGroup alternatives {&settings_, multi_settings_root_group}; - SettingsGroup source_group {&settings_, source_name}; - new_settings_ = get_settings (); - } + tr ("Clone Into Configuration"), + tr ("Confirm overwrite of all values for configuration \"%1\" with values from \"%2\"?") + .arg (target_name) + .arg (source_name))) + { + // grab the data to clone from + if (source_name == current_group_name) + { + // grab the data to clone from the current settings + new_settings_ = get_settings (); + } + else + { + SettingsGroup alternatives {&settings_, multi_settings_root_group}; + SettingsGroup source_group {&settings_, source_name}; + new_settings_ = get_settings (); + } - // purge target settings and replace - if (target_name == current_) - { - // restart with new settings - reposition_type_ = RepositionType::replace; - exit_flag_ = false; - main_window->close (); - } - else - { - SettingsGroup alternatives {&settings_, multi_settings_root_group}; - SettingsGroup target_group {&settings_, target_name}; - settings_.remove (QString {}); // purge entire group - load_from (new_settings_); - new_settings_.clear (); - } - } + // purge target settings and replace + if (target_name == current_) + { + // restart with new settings + reposition_type_ = RepositionType::replace; + exit_flag_ = false; + main_window->close (); + } + else + { + SettingsGroup alternatives {&settings_, multi_settings_root_group}; + SettingsGroup target_group {&settings_, target_name}; + settings_.remove (QString {}); // purge entire group + load_from (new_settings_); + new_settings_.clear (); + } + } } if (current_group.size ()) settings_.beginGroup (current_group); } @@ -665,9 +682,9 @@ void MultiSettings::impl::reset_configuration (QMainWindow * main_window, QMenu auto const& target_name = menu->title (); if (MessageBox::Yes != MessageBox::query_message (main_window, - tr ("Reset Configuration"), - tr ("Confirm reset to default values for configuration \"%1\"?") - .arg (target_name))) + tr ("Reset Configuration"), + tr ("Confirm reset to default values for configuration \"%1\"?") + .arg (target_name))) { return; } @@ -711,27 +728,27 @@ void MultiSettings::impl::rename_configuration (QMainWindow * main_window, QMenu if (QDialog::Accepted == dialog.exec ()) { if (target_name == current_) - { - settings_.setValue (multi_settings_current_name_key, dialog.new_name ()); - settings_.sync (); - current_ = dialog.new_name (); - Q_EMIT parent_->configurationNameChanged (current_); - } + { + settings_.setValue (multi_settings_current_name_key, dialog.new_name ()); + settings_.sync (); + current_ = dialog.new_name (); + Q_EMIT parent_->configurationNameChanged (current_); + } else - { - // switch to the target group and fetch the configuration data - Dictionary target_settings; - { - // grab the target configuration settings - SettingsGroup target_group {&settings_, target_name}; - target_settings = get_settings (); - // purge the old configuration data - settings_.remove (QString {}); // purge entire group - } - // load into new configuration group name - SettingsGroup target_group {&settings_, dialog.new_name ()}; - load_from (target_settings); - } + { + // switch to the target group and fetch the configuration data + Dictionary target_settings; + { + // grab the target configuration settings + SettingsGroup target_group {&settings_, target_name}; + target_settings = get_settings (); + // purge the old configuration data + settings_.remove (QString {}); // purge entire group + } + // load into new configuration group name + SettingsGroup target_group {&settings_, dialog.new_name ()}; + load_from (target_settings); + } // change the action text in the menu menu->setTitle (dialog.new_name ()); } @@ -749,12 +766,12 @@ void MultiSettings::impl::delete_configuration (QMainWindow * main_window, QMenu else { if (MessageBox::Yes != MessageBox::query_message (main_window, - tr ("Delete Configuration"), - tr ("Confirm deletion of configuration \"%1\"?") - .arg (target_name))) - { - return; - } + tr ("Delete Configuration"), + tr ("Confirm deletion of configuration \"%1\"?") + .arg (target_name))) + { + return; + } auto const& current_group = settings_.group (); if (current_group.size ()) settings_.endGroup (); SettingsGroup alternatives {&settings_, multi_settings_root_group}; diff --git a/MultiSettings.hpp b/MultiSettings.hpp index 0f2cef2ca..051553b48 100644 --- a/MultiSettings.hpp +++ b/MultiSettings.hpp @@ -3,60 +3,60 @@ #include #include +#include #include "pimpl_h.hpp" class QSettings; class QMainWindow; class QMenu; -class QString; // // MultiSettings - Manage multiple configuration names // // Responsibilities: // -// MultiSettings allows a Qt application to be run with alternative -// settings as stored in a QSettings INI style file. As far as the -// application is concerned it uses the QSettings instance returned -// by the MultiSettings::settings() method as if it were the one and -// only QSettings object. The alternative settings are stored as -// QSettings groups which are children of a root level group called -// MultiSettings. The current settings are themselves stored at the -// root so the QSettings group name MultiSettings is reserved. Also -// at the root level a key called CurrentMultiSettingsConfiguration -// is reserved to store the current configuration name. +// MultiSettings allows a Qt application to be run with alternative +// settings as stored in a QSettings INI style file. As far as the +// application is concerned it uses the QSettings instance returned +// by the MultiSettings::settings() method as if it were the one and +// only QSettings object. The alternative settings are stored as +// QSettings groups which are children of a root level group called +// MultiSettings. The current settings are themselves stored at the +// root so the QSettings group name MultiSettings is reserved. Also +// at the root level a key called CurrentMultiSettingsConfiguration +// is reserved to store the current configuration name. // // // Example Usage: // // #include -// #include "MultiSettings.hpp" -// #include "MyMainWindow.hpp" +// #include "MultiSettings.hpp" +// #include "MyMainWindow.hpp" // -// int main (int argc, char * argv[]) { -// QApplication a {argc, argv}; -// MultiSettings multi_settings; -// int result; -// do { -// MyMainWindow main_window {&multi_settings}; -// main_window.show (); -// result = a.exec (); -// } while (!result && !multi_settings.exit ()); -// return result; -// } +// int main (int argc, char * argv[]) { +// QApplication a {argc, argv}; +// MultiSettings multi_settings; +// int result; +// do { +// MyMainWindow main_window {&multi_settings}; +// main_window.show (); +// result = a.exec (); +// } while (!result && !multi_settings.exit ()); +// return result; +// } // -// In the main window call MultiSettings::create_menu_actions() to -// populate an existing QMenu widget with the configuration switching -// and maintenance actions. This would normally be done in the main -// window class constructor: +// In the main window call MultiSettings::create_menu_actions() to +// populate an existing QMenu widget with the configuration switching +// and maintenance actions. This would normally be done in the main +// window class constructor: // -// MyMainWindow::MyMainWindow (MultiSettings * multi_settings) { -// QSettings * settings {multi_settings->settings ()}; -// // ... -// multi_settings->create_menu_actions (this, ui->configurations_menu); -// // ... -// } +// MyMainWindow::MyMainWindow (MultiSettings * multi_settings) { +// QSettings * settings {multi_settings->settings ()}; +// // ... +// multi_settings->create_menu_actions (this, ui->configurations_menu); +// // ... +// } // class MultiSettings @@ -65,7 +65,11 @@ class MultiSettings Q_OBJECT public: - explicit MultiSettings (); + // config_name will be selected if it is an existing configuration + // name otherwise the last used configuration will be selected or + // the default configuration if none exist + explicit MultiSettings (QString const& config_name = QString {}); + MultiSettings (MultiSettings const&) = delete; MultiSettings& operator = (MultiSettings const&) = delete; ~MultiSettings (); diff --git a/main.cpp b/main.cpp index d5c5ab87b..1f93103e7 100644 --- a/main.cpp +++ b/main.cpp @@ -110,7 +110,7 @@ int main(int argc, char *argv[]) #if QT_VERSION >= 0x050200 QCommandLineParser parser; - parser.setApplicationDescription ("\nJT65A & JT9 Weak Signal Communications Program."); + parser.setApplicationDescription ("\nJT65, JT9, JT4, MSK144, QRA64, ISCAT & WSPR Weak Signal Communications Program."); auto help_option = parser.addHelpOption (); auto version_option = parser.addVersionOption (); @@ -120,6 +120,12 @@ int main(int argc, char *argv[]) , a.translate ("main", "rig-name")); parser.addOption (rig_option); + // support for start up configuration + QCommandLineOption cfg_option (QStringList {} << "c" << "config" + , a.translate ("main", "Where is an existing one.") + , a.translate ("main", "configuration")); + parser.addOption (cfg_option); + QCommandLineOption test_option (QStringList {} << "test-mode" , a.translate ("main", "Writable files in test location. Use with caution, for testing only.")); parser.addOption (test_option); @@ -168,8 +174,9 @@ int main(int argc, char *argv[]) multiple = true; } + // now we have the application name we can open the settings - MultiSettings multi_settings; + MultiSettings multi_settings {parser.value (cfg_option)}; // find the temporary files path QDir temp_dir {QStandardPaths::writableLocation (QStandardPaths::TempLocation)};