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:
Iwbnwif Yiw 2024-02-05 20:43:11 +00:00
parent 2e54b7c964
commit c35fb1f489
6 changed files with 91 additions and 2 deletions

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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
View 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
View 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;