Support for a startup configuration

The --config <configuration> (-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
This commit is contained in:
Bill Somerville 2017-02-17 12:32:47 +00:00
parent 75d371ebd9
commit b4ea0194f1
3 changed files with 142 additions and 114 deletions

View File

@ -144,7 +144,7 @@ class MultiSettings::impl final
Q_OBJECT Q_OBJECT
public: public:
explicit impl (MultiSettings const * parent); explicit impl (MultiSettings const * parent, QString const& config_name);
bool reposition (); bool reposition ();
void create_menu_actions (QMainWindow * main_window, QMenu * menu); void create_menu_actions (QMainWindow * main_window, QMenu * menu);
bool exit (); bool exit ();
@ -201,8 +201,8 @@ private:
#include "MultiSettings.moc" #include "MultiSettings.moc"
#include "moc_MultiSettings.cpp" #include "moc_MultiSettings.cpp"
MultiSettings::MultiSettings () MultiSettings::MultiSettings (QString const& config_name)
: m_ {this} : m_ {this, config_name}
{ {
} }
@ -263,7 +263,7 @@ bool MultiSettings::exit ()
return m_->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} : settings_ {settings_path (), QSettings::IniFormat}
, parent_ {parent} , parent_ {parent}
, name_change_emit_pending_ {true} , name_change_emit_pending_ {true}
@ -303,15 +303,33 @@ MultiSettings::impl::impl (MultiSettings const * parent)
} }
// bootstrap // bootstrap
QStringList available_configurations;
{ {
SettingsGroup alternatives {&settings_, multi_settings_root_group}; SettingsGroup alternatives {&settings_, multi_settings_root_group};
available_configurations = settings_.childGroups ();
// use last selected configuration
current_ = settings_.value (multi_settings_current_name_key).toString (); current_ = settings_.value (multi_settings_current_name_key).toString ();
if (!current_.size ()) if (!current_.size ())
{ {
// no configurations so use default name
current_ = tr (default_string); current_ = tr (default_string);
settings_.setValue (multi_settings_current_name_key, current_); 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 (); settings_.sync ();
} }
@ -394,9 +412,8 @@ void MultiSettings::impl::create_menu_actions (QMainWindow * main_window, QMenu
// and set as the current configuration // and set as the current configuration
default_menu->menuAction ()->setChecked (true); default_menu->menuAction ()->setChecked (true);
QStringList available_configurations;
// get the existing alternatives // get the existing alternatives
available_configurations = settings_.childGroups (); auto const& available_configurations {settings_.childGroups ()};
// add all the other configurations // add all the other configurations
for (auto const& configuration_name: available_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_) if (target_name != current_)
{ {
{ {
auto const& current_group = settings_.group (); auto const& current_group = settings_.group ();
if (current_group.size ()) settings_.endGroup (); if (current_group.size ()) settings_.endGroup ();
// position to the alternative settings // position to the alternative settings
SettingsGroup alternatives {&settings_, multi_settings_root_group}; SettingsGroup alternatives {&settings_, multi_settings_root_group};
// save the target settings // save the target settings
SettingsGroup target_group {&settings_, target_name}; SettingsGroup target_group {&settings_, target_name};
new_settings_ = get_settings (); new_settings_ = get_settings ();
if (current_group.size ()) settings_.beginGroup (current_group); if (current_group.size ()) settings_.beginGroup (current_group);
} }
// and set up the restart // and set up the restart
current_ = target_name; 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 ()}; QString source_name {1 == sources.size () ? sources.at (0) : dialog.name ()};
if (MessageBox::Yes == MessageBox::query_message (main_window, if (MessageBox::Yes == MessageBox::query_message (main_window,
tr ("Clone Into Configuration"), tr ("Clone Into Configuration"),
tr ("Confirm overwrite of all values for configuration \"%1\" with values from \"%2\"?") tr ("Confirm overwrite of all values for configuration \"%1\" with values from \"%2\"?")
.arg (target_name) .arg (target_name)
.arg (source_name))) .arg (source_name)))
{ {
// grab the data to clone from // grab the data to clone from
if (source_name == current_group_name) if (source_name == current_group_name)
{ {
// grab the data to clone from the current settings // grab the data to clone from the current settings
new_settings_ = get_settings (); new_settings_ = get_settings ();
} }
else else
{ {
SettingsGroup alternatives {&settings_, multi_settings_root_group}; SettingsGroup alternatives {&settings_, multi_settings_root_group};
SettingsGroup source_group {&settings_, source_name}; SettingsGroup source_group {&settings_, source_name};
new_settings_ = get_settings (); new_settings_ = get_settings ();
} }
// purge target settings and replace // purge target settings and replace
if (target_name == current_) if (target_name == current_)
{ {
// restart with new settings // restart with new settings
reposition_type_ = RepositionType::replace; reposition_type_ = RepositionType::replace;
exit_flag_ = false; exit_flag_ = false;
main_window->close (); main_window->close ();
} }
else else
{ {
SettingsGroup alternatives {&settings_, multi_settings_root_group}; SettingsGroup alternatives {&settings_, multi_settings_root_group};
SettingsGroup target_group {&settings_, target_name}; SettingsGroup target_group {&settings_, target_name};
settings_.remove (QString {}); // purge entire group settings_.remove (QString {}); // purge entire group
load_from (new_settings_); load_from (new_settings_);
new_settings_.clear (); new_settings_.clear ();
} }
} }
} }
if (current_group.size ()) settings_.beginGroup (current_group); 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 (); auto const& target_name = menu->title ();
if (MessageBox::Yes != MessageBox::query_message (main_window, if (MessageBox::Yes != MessageBox::query_message (main_window,
tr ("Reset Configuration"), tr ("Reset Configuration"),
tr ("Confirm reset to default values for configuration \"%1\"?") tr ("Confirm reset to default values for configuration \"%1\"?")
.arg (target_name))) .arg (target_name)))
{ {
return; return;
} }
@ -711,27 +728,27 @@ void MultiSettings::impl::rename_configuration (QMainWindow * main_window, QMenu
if (QDialog::Accepted == dialog.exec ()) if (QDialog::Accepted == dialog.exec ())
{ {
if (target_name == current_) if (target_name == current_)
{ {
settings_.setValue (multi_settings_current_name_key, dialog.new_name ()); settings_.setValue (multi_settings_current_name_key, dialog.new_name ());
settings_.sync (); settings_.sync ();
current_ = dialog.new_name (); current_ = dialog.new_name ();
Q_EMIT parent_->configurationNameChanged (current_); Q_EMIT parent_->configurationNameChanged (current_);
} }
else else
{ {
// switch to the target group and fetch the configuration data // switch to the target group and fetch the configuration data
Dictionary target_settings; Dictionary target_settings;
{ {
// grab the target configuration settings // grab the target configuration settings
SettingsGroup target_group {&settings_, target_name}; SettingsGroup target_group {&settings_, target_name};
target_settings = get_settings (); target_settings = get_settings ();
// purge the old configuration data // purge the old configuration data
settings_.remove (QString {}); // purge entire group settings_.remove (QString {}); // purge entire group
} }
// load into new configuration group name // load into new configuration group name
SettingsGroup target_group {&settings_, dialog.new_name ()}; SettingsGroup target_group {&settings_, dialog.new_name ()};
load_from (target_settings); load_from (target_settings);
} }
// change the action text in the menu // change the action text in the menu
menu->setTitle (dialog.new_name ()); menu->setTitle (dialog.new_name ());
} }
@ -749,12 +766,12 @@ void MultiSettings::impl::delete_configuration (QMainWindow * main_window, QMenu
else else
{ {
if (MessageBox::Yes != MessageBox::query_message (main_window, if (MessageBox::Yes != MessageBox::query_message (main_window,
tr ("Delete Configuration"), tr ("Delete Configuration"),
tr ("Confirm deletion of configuration \"%1\"?") tr ("Confirm deletion of configuration \"%1\"?")
.arg (target_name))) .arg (target_name)))
{ {
return; return;
} }
auto const& current_group = settings_.group (); auto const& current_group = settings_.group ();
if (current_group.size ()) settings_.endGroup (); if (current_group.size ()) settings_.endGroup ();
SettingsGroup alternatives {&settings_, multi_settings_root_group}; SettingsGroup alternatives {&settings_, multi_settings_root_group};

View File

@ -3,60 +3,60 @@
#include <QObject> #include <QObject>
#include <QVariant> #include <QVariant>
#include <QString>
#include "pimpl_h.hpp" #include "pimpl_h.hpp"
class QSettings; class QSettings;
class QMainWindow; class QMainWindow;
class QMenu; class QMenu;
class QString;
// //
// MultiSettings - Manage multiple configuration names // MultiSettings - Manage multiple configuration names
// //
// Responsibilities: // Responsibilities:
// //
// MultiSettings allows a Qt application to be run with alternative // MultiSettings allows a Qt application to be run with alternative
// settings as stored in a QSettings INI style file. As far as the // settings as stored in a QSettings INI style file. As far as the
// application is concerned it uses the QSettings instance returned // application is concerned it uses the QSettings instance returned
// by the MultiSettings::settings() method as if it were the one and // by the MultiSettings::settings() method as if it were the one and
// only QSettings object. The alternative settings are stored as // only QSettings object. The alternative settings are stored as
// QSettings groups which are children of a root level group called // QSettings groups which are children of a root level group called
// MultiSettings. The current settings are themselves stored at the // MultiSettings. The current settings are themselves stored at the
// root so the QSettings group name MultiSettings is reserved. Also // root so the QSettings group name MultiSettings is reserved. Also
// at the root level a key called CurrentMultiSettingsConfiguration // at the root level a key called CurrentMultiSettingsConfiguration
// is reserved to store the current configuration name. // is reserved to store the current configuration name.
// //
// //
// Example Usage: // Example Usage:
// //
// #include <QApplication> // #include <QApplication>
// #include "MultiSettings.hpp" // #include "MultiSettings.hpp"
// #include "MyMainWindow.hpp" // #include "MyMainWindow.hpp"
// //
// int main (int argc, char * argv[]) { // int main (int argc, char * argv[]) {
// QApplication a {argc, argv}; // QApplication a {argc, argv};
// MultiSettings multi_settings; // MultiSettings multi_settings;
// int result; // int result;
// do { // do {
// MyMainWindow main_window {&multi_settings}; // MyMainWindow main_window {&multi_settings};
// main_window.show (); // main_window.show ();
// result = a.exec (); // result = a.exec ();
// } while (!result && !multi_settings.exit ()); // } while (!result && !multi_settings.exit ());
// return result; // return result;
// } // }
// //
// In the main window call MultiSettings::create_menu_actions() to // In the main window call MultiSettings::create_menu_actions() to
// populate an existing QMenu widget with the configuration switching // populate an existing QMenu widget with the configuration switching
// and maintenance actions. This would normally be done in the main // and maintenance actions. This would normally be done in the main
// window class constructor: // window class constructor:
// //
// MyMainWindow::MyMainWindow (MultiSettings * multi_settings) { // MyMainWindow::MyMainWindow (MultiSettings * multi_settings) {
// QSettings * settings {multi_settings->settings ()}; // QSettings * settings {multi_settings->settings ()};
// // ... // // ...
// multi_settings->create_menu_actions (this, ui->configurations_menu); // multi_settings->create_menu_actions (this, ui->configurations_menu);
// // ... // // ...
// } // }
// //
class MultiSettings class MultiSettings
@ -65,7 +65,11 @@ class MultiSettings
Q_OBJECT Q_OBJECT
public: 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 (MultiSettings const&) = delete;
MultiSettings& operator = (MultiSettings const&) = delete; MultiSettings& operator = (MultiSettings const&) = delete;
~MultiSettings (); ~MultiSettings ();

View File

@ -110,7 +110,7 @@ int main(int argc, char *argv[])
#if QT_VERSION >= 0x050200 #if QT_VERSION >= 0x050200
QCommandLineParser parser; 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 help_option = parser.addHelpOption ();
auto version_option = parser.addVersionOption (); auto version_option = parser.addVersionOption ();
@ -120,6 +120,12 @@ int main(int argc, char *argv[])
, a.translate ("main", "rig-name")); , a.translate ("main", "rig-name"));
parser.addOption (rig_option); parser.addOption (rig_option);
// support for start up configuration
QCommandLineOption cfg_option (QStringList {} << "c" << "config"
, a.translate ("main", "Where <configuration> is an existing one.")
, a.translate ("main", "configuration"));
parser.addOption (cfg_option);
QCommandLineOption test_option (QStringList {} << "test-mode" QCommandLineOption test_option (QStringList {} << "test-mode"
, a.translate ("main", "Writable files in test location. Use with caution, for testing only.")); , a.translate ("main", "Writable files in test location. Use with caution, for testing only."));
parser.addOption (test_option); parser.addOption (test_option);
@ -168,8 +174,9 @@ int main(int argc, char *argv[])
multiple = true; multiple = true;
} }
// now we have the application name we can open the settings // 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 // find the temporary files path
QDir temp_dir {QStandardPaths::writableLocation (QStandardPaths::TempLocation)}; QDir temp_dir {QStandardPaths::writableLocation (QStandardPaths::TempLocation)};