mirror of
https://github.com/ra3xdh/qucs_s
synced 2025-03-28 21:13:26 +00:00
Implement a singleton / static settingsManager
This change implements a QucsSingleton class that can be used to make any class singleton. It also provides a new settingsManager class which uses QucsSingleton to provide a single, static access point to the standard QSettings functionality. A shortcut is provided and a new method for providing a default value if the requested key is not set.
This commit is contained in:
parent
2e54b7c964
commit
c35fb1f489
@ -122,6 +122,7 @@ SET(QUCS_SRCS
|
||||
mouseactions.cpp qucs_actions.cpp schematic_file.cpp
|
||||
wirelabel.cpp node.cpp qucs_init.cpp
|
||||
syntax.cpp misc.cpp messagedock.cpp
|
||||
settings.cpp
|
||||
imagewriter.cpp printerwriter.cpp projectView.cpp
|
||||
symbolwidget.cpp
|
||||
)
|
||||
@ -139,6 +140,7 @@ octave_window.h
|
||||
qucs.h
|
||||
qucsdoc.h
|
||||
schematic.h
|
||||
settings.h
|
||||
syntax.h
|
||||
symbolwidget.h
|
||||
textdoc.h
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include "main.h"
|
||||
#include "qucs.h"
|
||||
#include "schematic.h"
|
||||
#include "settings.h"
|
||||
#include "misc.h"
|
||||
|
||||
#include <cmath>
|
||||
@ -41,8 +42,13 @@
|
||||
ComponentDialog::ComponentDialog(Component *c, Schematic *d)
|
||||
: QDialog(d)
|
||||
{
|
||||
QSettings settings("qucs","qucs_s");
|
||||
restoreGeometry(settings.value("ComponentDialog/geometry").toByteArray());
|
||||
// qDebug() << "Restore: " << _settings::Get().value("ComponentDialog/geometry").toByteArray();
|
||||
// qDebug() << "Settings: " << _settings::Get().organizationName() << " " << _settings::Get().applicationName();
|
||||
// qDebug() << "Default test (found)" << _settings::Get().item<int>("Foo");
|
||||
// qDebug() << "Default test (not found int type)" << _settings::Get().item<int>("Bar");
|
||||
// qDebug() << "Default test (found wrong type)" << _settings::Get().item<QString>("Foo");
|
||||
// qDebug() << "Default test (not found string type)" << _settings::Get().item<QString>("Bar");
|
||||
restoreGeometry(_settings::Get().item<QByteArray>("ComponentDialog/geometry"));
|
||||
|
||||
setWindowTitle(tr("Edit Component Properties"));
|
||||
Comp = c;
|
||||
|
@ -53,6 +53,7 @@
|
||||
#include "schematic.h"
|
||||
#include "mouseactions.h"
|
||||
#include "messagedock.h"
|
||||
#include "settings.h"
|
||||
#include "wire.h"
|
||||
#include "module.h"
|
||||
#include "projectView.h"
|
||||
@ -101,6 +102,10 @@ QucsApp::QucsApp()
|
||||
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
|
||||
#endif
|
||||
|
||||
// Instantiate settings. It isn't necessary to do it at this point,
|
||||
// but it is useful for testing.
|
||||
_settings::Get();
|
||||
|
||||
windowTitle = misc::getWindowTitle();
|
||||
setWindowTitle(windowTitle);
|
||||
|
||||
|
29
qucs/qucs.h
29
qucs/qucs.h
@ -441,4 +441,33 @@ private:
|
||||
QString lastExportFilename;
|
||||
};
|
||||
|
||||
/** \brief Provide a template to declare singleton classes.
|
||||
*
|
||||
* Classes implemented using this template will be singletons (i.e., only
|
||||
* one instance of the class will exist per invokation). Primarily this
|
||||
* is used to support static / access to an application-wide function, e.g.
|
||||
* settings.
|
||||
*
|
||||
*/
|
||||
template < typename T >
|
||||
class QucsSingleton final
|
||||
{
|
||||
public:
|
||||
static T& Get()
|
||||
{
|
||||
static T instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Prevent overriding default ctor, dtor, copying, or multiple instances.
|
||||
private:
|
||||
QucsSingleton() = default;
|
||||
~QucsSingleton() = default;
|
||||
|
||||
QucsSingleton(const QucsSingleton&) = delete;
|
||||
QucsSingleton& operator=(const QucsSingleton&) = delete;
|
||||
QucsSingleton(QucsSingleton&&) = delete;
|
||||
QucsSingleton& operator=(QucsSingleton&&) = delete;
|
||||
};
|
||||
|
||||
#endif /* QUCS_H */
|
||||
|
15
qucs/settings.cpp
Normal file
15
qucs/settings.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include <settings.h>
|
||||
|
||||
settingsManager::settingsManager()
|
||||
:QSettings("qucs", "qucs_s")
|
||||
{
|
||||
// qDebug() << this << " created " << organizationName() << " " << applicationName();
|
||||
|
||||
m_Defaults["Foo"] = 1234;
|
||||
|
||||
}
|
||||
|
||||
settingsManager::~settingsManager()
|
||||
{
|
||||
// qDebug() << this << " destroyed";
|
||||
}
|
32
qucs/settings.h
Normal file
32
qucs/settings.h
Normal file
@ -0,0 +1,32 @@
|
||||
#include <map>
|
||||
#include <QDebug>
|
||||
#include <QSettings>
|
||||
#include <qucs.h>
|
||||
|
||||
class settingsManager : public QSettings
|
||||
{
|
||||
// Q_OBJECT
|
||||
|
||||
// TODO: Make constructors private / protected so this class can only be
|
||||
// instantiated by QucsSingleton.
|
||||
public:
|
||||
explicit settingsManager();
|
||||
~settingsManager();
|
||||
|
||||
template<class T>
|
||||
T item(const QString& key)
|
||||
{
|
||||
if (contains(key)) {
|
||||
return value(key).value<T>();
|
||||
}
|
||||
|
||||
else {
|
||||
return m_Defaults[key].value<T>();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<QString, QVariant> m_Defaults;
|
||||
};
|
||||
|
||||
typedef QucsSingleton<settingsManager> _settings;
|
Loading…
x
Reference in New Issue
Block a user