JS8Call-Improved master
Loading...
Searching...
No Matches
MultiSettings.h
1#ifndef MULTISETTINGS_HPP__
2#define MULTISETTINGS_HPP__
3
4#include "JS8_Include/pimpl_h.h"
5
6#include <QObject>
7#include <QString>
8#include <QVariant>
9
10class QSettings;
11class QMainWindow;
12class QMenu;
13
14//
15// MultiSettings - Manage multiple configuration names
16//
17// Responsibilities:
18//
19// MultiSettings allows a Qt application to be run with alternative
20// settings as stored in a QSettings INI style file. As far as the
21// application is concerned it uses the QSettings instance returned
22// by the MultiSettings::settings() method as if it were the one and
23// only QSettings object. The alternative settings are stored as
24// QSettings groups which are children of a root level group called
25// MultiSettings. The current settings are themselves stored at the
26// root so the QSettings group name MultiSettings is reserved. Also
27// at the root level a key called CurrentMultiSettingsConfiguration
28// is reserved to store the current configuration name.
29//
30//
31// Example Usage:
32//
33// #include <QApplication>
34// #include "MultiSettings.h"
35// #include "MyMainWindow.h"
36//
37// int main (int argc, char * argv[]) {
38// QApplication a {argc, argv};
39// MultiSettings multi_settings;
40// int result;
41// do {
42// MyMainWindow main_window {&multi_settings};
43// main_window.show ();
44// result = a.exec ();
45// } while (!result && !multi_settings.exit ());
46// return result;
47// }
48//
49// In the main window call MultiSettings::create_menu_actions() to
50// populate an existing QMenu widget with the configuration switching
51// and maintenance actions. This would normally be done in the main
52// window class constructor:
53//
54// MyMainWindow::MyMainWindow (MultiSettings * multi_settings) {
55// QSettings * settings {multi_settings->settings ()};
56// // ...
57// multi_settings->create_menu_actions (this, ui->configurations_menu);
58// // ...
59// }
60//
61
62class MultiSettings : public QObject {
63 Q_OBJECT
64
65 public:
66 // config_name will be selected if it is an existing configuration
67 // name otherwise the last used configuration will be selected or
68 // the default configuration if none exist
69 explicit MultiSettings(QString const &config_name = QString{});
70
71 MultiSettings(MultiSettings const &) = delete;
72 MultiSettings &operator=(MultiSettings const &) = delete;
73 ~MultiSettings();
74
75 // Add multiple configurations navigation and maintenance actions to
76 // a provided menu. The provided main window object instance will
77 // have its close() function called when a "Switch To" configuration
78 // action is triggered.
79 void create_menu_actions(QMainWindow *, QMenu *);
80
81 // switch to this configuration if it exists
82 Q_SLOT void select_configuration(QString const &name);
83 QString configuration_name() const;
84
85 // Access to the QSettings object instance.
86 QSettings *settings();
87
88 // Access to values in a common section
89 QVariant common_value(QString const &key,
90 QVariant const &default_value = QVariant{}) const;
91 void set_common_value(QString const &key, QVariant const &value);
92 void remove_common_value(QString const &key);
93
94 // Call this to determine if the application is terminating, if it
95 // returns false then the application main window should be
96 // recreated, shown and the application exec() function called
97 // again.
98 bool exit();
99
100 // emitted when the name of the current configuration changes
101 Q_SIGNAL void configurationNameChanged(QString name) const;
102
103 private:
104 class impl;
105 pimpl<impl> m_;
106};
107
108#endif
Definition MultiSettings.cpp:149
Definition pimpl_h.h:16