2016-09-10 21:09:28 -04:00
|
|
|
#ifndef MULTISETTINGS_HPP__
|
|
|
|
#define MULTISETTINGS_HPP__
|
|
|
|
|
2016-10-28 13:15:04 -04:00
|
|
|
#include <QObject>
|
2016-09-10 21:09:28 -04:00
|
|
|
#include <QVariant>
|
2017-02-17 07:32:47 -05:00
|
|
|
#include <QString>
|
2016-04-17 19:39:12 -04:00
|
|
|
|
|
|
|
#include "pimpl_h.hpp"
|
|
|
|
|
|
|
|
class QSettings;
|
|
|
|
class QMainWindow;
|
|
|
|
class QMenu;
|
|
|
|
|
|
|
|
//
|
|
|
|
// MultiSettings - Manage multiple configuration names
|
|
|
|
//
|
|
|
|
// Responsibilities:
|
|
|
|
//
|
2017-02-17 07:32:47 -05:00
|
|
|
// 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.
|
2016-04-17 19:39:12 -04:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Example Usage:
|
|
|
|
//
|
|
|
|
// #include <QApplication>
|
2017-02-17 07:32:47 -05:00
|
|
|
// #include "MultiSettings.hpp"
|
|
|
|
// #include "MyMainWindow.hpp"
|
2016-04-17 19:39:12 -04:00
|
|
|
//
|
2017-02-17 07:32:47 -05:00
|
|
|
// 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;
|
|
|
|
// }
|
2016-04-17 19:39:12 -04:00
|
|
|
//
|
2017-02-17 07:32:47 -05:00
|
|
|
// 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:
|
2016-04-17 19:39:12 -04:00
|
|
|
//
|
2017-02-17 07:32:47 -05:00
|
|
|
// MyMainWindow::MyMainWindow (MultiSettings * multi_settings) {
|
|
|
|
// QSettings * settings {multi_settings->settings ()};
|
|
|
|
// // ...
|
|
|
|
// multi_settings->create_menu_actions (this, ui->configurations_menu);
|
|
|
|
// // ...
|
|
|
|
// }
|
2016-04-17 19:39:12 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
class MultiSettings
|
2016-10-28 13:15:04 -04:00
|
|
|
: public QObject
|
2016-04-17 19:39:12 -04:00
|
|
|
{
|
2016-10-28 13:15:04 -04:00
|
|
|
Q_OBJECT
|
|
|
|
|
2016-04-17 19:39:12 -04:00
|
|
|
public:
|
2017-02-17 07:32:47 -05:00
|
|
|
// 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 {});
|
|
|
|
|
2016-04-17 19:39:12 -04:00
|
|
|
MultiSettings (MultiSettings const&) = delete;
|
|
|
|
MultiSettings& operator = (MultiSettings const&) = delete;
|
|
|
|
~MultiSettings ();
|
|
|
|
|
|
|
|
// Add multiple configurations navigation and maintenance actions to
|
|
|
|
// a provided menu. The provided main window object instance will
|
|
|
|
// have its close() function called when a "Switch To" configuration
|
|
|
|
// action is triggered.
|
|
|
|
void create_menu_actions (QMainWindow *, QMenu *);
|
|
|
|
|
2019-06-12 20:44:28 -04:00
|
|
|
// switch to this configuration if it exists
|
|
|
|
Q_SLOT void select_configuration (QString const& name);
|
|
|
|
QString configuration_name () const;
|
|
|
|
|
2016-04-17 19:39:12 -04:00
|
|
|
// Access to the QSettings object instance.
|
|
|
|
QSettings * settings ();
|
|
|
|
|
2016-09-10 21:09:28 -04:00
|
|
|
// Access to values in a common section
|
|
|
|
QVariant common_value (QString const& key, QVariant const& default_value = QVariant {}) const;
|
|
|
|
void set_common_value (QString const& key, QVariant const& value);
|
|
|
|
void remove_common_value (QString const& key);
|
|
|
|
|
2016-04-17 19:39:12 -04:00
|
|
|
// Call this to determine if the application is terminating, if it
|
|
|
|
// returns false then the application main window should be
|
|
|
|
// recreated, shown and the application exec() function called
|
|
|
|
// again.
|
|
|
|
bool exit ();
|
|
|
|
|
2016-10-28 13:15:04 -04:00
|
|
|
// emitted when the name of the current configuration changes
|
2016-11-26 20:34:35 -05:00
|
|
|
Q_SIGNAL void configurationNameChanged (QString name) const;
|
2016-10-28 13:15:04 -04:00
|
|
|
|
2016-04-17 19:39:12 -04:00
|
|
|
private:
|
|
|
|
class impl;
|
|
|
|
pimpl<impl> m_;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|