Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a unified settings model which will eventually remove the need for QucsSettings #533

Merged
merged 19 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/templates_ngspice/TRAN_FFT_OpAmp.sch
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Qucs Schematic 24.1.0>
<Properties>
<View=-436,-72,1509,1092,0.580396,0,1>
<View=-443,-72,1514,1091,1,0,0>
<Grid=10,10,1>
<DataSet=TRAN_FFT_OpAmp.dat>
<DataDisplay=TRAN_FFT_OpAmp.dpl>
Expand Down Expand Up @@ -52,12 +52,12 @@
<40 180 40 180 "in" 20 150 0 "">
</Wires>
<Diagrams>
<Rect 510 290 541 322 3 #c0c0c0 1 00 1 0 5e-5 0.0004 1 -1.2 0.5 1.2 1 -1.20016 0.5 1.19977 315 0 225 1 0 0 "" "" "">
<"ngspice/tran.v(in)" #0000ff 0 3 0 0 0>
<"ngspice/tran.v(out)" #ff0000 0 3 0 0 1>
<Rect 510 290 541 322 3 #c0c0c0 1 00 1 0 0.0005 0.005 1 -1.2 0.5 1.2 1 -1.19967 0.5 1.20007 315 0 225 1 0 0 "" "" "">
<"ngspice/tran.v(in)" #0000ff 1 3 0 0 0>
<"ngspice/tran.v(out)" #ff0000 1 3 0 0 1>
</Rect>
<Rect 510 680 541 322 3 #c0c0c0 1 00 0 0 1000 10000 1 -333.809 50 25.6053 1 -1 0.5 1 315 0 225 1 0 0 "" "" "">
<"ngspice/ac.s_db" #ff0000 0 3 0 0 0>
<Rect 510 680 541 322 3 #c0c0c0 1 00 0 0 1000 10000 1 -275.258 50 25.0184 1 -1 0.5 1 315 0 225 1 0 0 "" "" "">
<"ngspice/ac.s_db" #ff0000 1 3 0 0 0>
</Rect>
<Rect 510 1050 541 322 3 #c0c0c0 1 00 1 0 2500 20000 1 -0.0606327 0.1 0.66696 1 -1 0.5 1 315 0 225 1 0 0 "" "" "">
<"ngspice/ac.s_db" #0000ff 0 3 0 0 0>
Expand Down
2 changes: 2 additions & 0 deletions qucs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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
)
Expand All @@ -145,6 +146,7 @@ octave_window.h
qucs.h
qucsdoc.h
schematic.h
settings.h
syntax.h
symbolwidget.h
textdoc.h
Expand Down
10 changes: 8 additions & 2 deletions qucs/components/componentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "main.h"
#include "qucs.h"
#include "schematic.h"
#include "settings.h"
#include "misc.h"

#include <cmath>
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions qucs/dialogs/qucssettingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "main.h"
#include "textdoc.h"
#include "schematic.h"
#include "settings.h"

#include <QWidget>
#include <QLabel>
Expand Down
303 changes: 116 additions & 187 deletions qucs/main.cpp

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion qucs/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct tQucsSettings {
int panelIconsTheme;
int compIconsTheme;

int x, y, dx, dy; // position and size of main window
QFont font;
QFont appFont;
QFont textFont;
Expand Down
25 changes: 16 additions & 9 deletions qucs/qucs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "schematic.h"
#include "mouseactions.h"
#include "messagedock.h"
#include "settings.h"
#include "wire.h"
#include "module.h"
#include "projectView.h"
Expand Down Expand Up @@ -102,6 +103,21 @@ QucsApp::QucsApp()

QucsSettings.hasDarkTheme = misc::isDarkTheme();

// Instantiate settings singleton and restore window geometry.
const auto geometry = _settings::Get().item<QByteArray>("MainWindowGeometry");

if (!geometry.isEmpty()) {
qDebug() << "Saved geometry is: " << geometry;
restoreGeometry(geometry);
}
// Set a default size and position if geometry does not yet exist.
else {
QSize size = QGuiApplication::primaryScreen()->size();
int w = size.width();
int h = size.height();
setGeometry (w * 0.25, h * 0.25, w * 0.5, h * 0.5);
}

QucsFileFilter =
tr("Schematic") + " (*.sch);;" +
tr("Data Display") + " (*.dpl);;" +
Expand All @@ -116,9 +132,6 @@ QucsApp::QucsApp()
//updateSchNameHash();
//updateSpiceNameHash();

move (QucsSettings.x, QucsSettings.y);
resize(QucsSettings.dx, QucsSettings.dy);

MouseMoveAction = 0;
MousePressAction = 0;
MouseReleaseAction = 0;
Expand Down Expand Up @@ -2178,12 +2191,6 @@ void QucsApp::closeEvent(QCloseEvent* Event)
// Saves settings
void QucsApp::saveSettings()
{
qDebug()<<"x"<<pos().x()<<" ,y"<<pos().y();
qDebug()<<"dx"<<size().width()<<" ,dy"<<size().height();
QucsSettings.x=pos().x();
QucsSettings.y=pos().y();
QucsSettings.dx=size().width();
QucsSettings.dy=size().height();
QucsSettings.FileToolbar = fileToolbar->isVisible();
QucsSettings.EditToolbar = editToolbar->isVisible();
QucsSettings.ViewToolbar = viewToolbar->isVisible();
Expand Down
29 changes: 29 additions & 0 deletions qucs/qucs.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,33 @@ private slots:
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 */
78 changes: 78 additions & 0 deletions qucs/settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <QApplication>

#include "main.h"
#include "settings.h"
#include "extsimkernels/spicecompat.h"

settingsManager::settingsManager()
:QSettings("qucs", "qucs_s")
{
// qDebug() << this << " created " << organizationName() << " " << applicationName();

initAliases();
initDefaults();
}

settingsManager::~settingsManager()
{
// qDebug() << this << " destroyed";
}

void settingsManager::resetDefaults(const QString &group)
{
qDebug() << "Reset settings group " << group;

if (group == "All") {
// Remove all settings (including those for which there is no default).
// clear();

// Repopulate with known defaults.
for (auto const& item : m_Defaults) {
qDebug() << "Resetting item " << item.first << " " << item.second;
setValue(item.first, item.second);
}
}

else {
beginGroup(group);
for (const QString& key : allKeys()) {
setValue(key, m_Defaults[key]);
}
}
}

void settingsManager::initDefaults()
{
m_Defaults["DefaultSimulator"] = spicecompat::simNotSpecified;
m_Defaults["FirstRun"] = true;
m_Defaults["font"] = QApplication::font();
m_Defaults["appFont"] = QApplication::font();
m_Defaults["LargeFontSize"] = static_cast<double>(16.0);
m_Defaults["maxUndo"] = 20;
m_Defaults["panelIconsTheme"] = qucs::autoIcons;
m_Defaults["compIconsTheme"] = qucs::autoIcons;

#ifdef Q_OS_WIN
m_Defaults["NgspiceExecutable"] = "ngspice_con.exe";
m_Defaults["XyceExecutable"] = "Xyce.exe";
m_Defaults["RFLayoutExecutable"] = "qucsrflayout.exe";
#else
m_Defaults["NgspiceExecutable"] = "ngspice";
m_Defaults["XyceExecutable"] = "/usr/local/Xyce-Release-6.8.0-OPENSOURCE/bin/Xyce";
m_Defaults["RFLayoutExecutable"] = "qucsrflayout";
#endif

m_Defaults["XyceParExecutable"] = "mpirun -np %p /usr/local/Xyce-Release-6.8.0-OPENMPI-OPENSOURCE/bin/Xyce";
m_Defaults["S4Q_workdir"] = "";
m_Defaults["Nprocs"] = 4;
m_Defaults["SpiceOpusExecutable"] = "spiceopus";
m_Defaults["SimParameters"] = "";
m_Defaults["GraphAntiAliasing"] = false;
m_Defaults["TextAntiAliasing"] = false;
m_Defaults["fullTraceName"] = false;
}

void settingsManager::initAliases()
{
m_Aliases["IgnoreVersion"] = QStringList({"IngnoreVersion"});
}
68 changes: 68 additions & 0 deletions qucs/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#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();

/** \brief Set all settings in the given group to their default value.
*/
void resetDefaults(const QString& group = "All");

/** \brief Get the default value for a given setting.
*/
template<class T>
T itemDefault(const QString& key)
{
return value(key).value<T>();
}

/** \brief Store a setting.
*/
template<class T>
void setItem(const QString& key, const T& value)
{
setValue(key, value);
}

/** \brief Retrieve a setting value by its key.
*/
template<class T>
T item(const QString& key)
{
if (contains(key)) {
return value(key).value<T>();
}

for (auto alias : m_Aliases[key]) {
if (contains(alias)) {
// qDebug() << "Found alias: " << alias;
return value(alias).value<T>();
}
}
return m_Defaults[key].value<T>();
}

private:
std::map<QString, QVariant> m_Defaults;
std::map<QString, QStringList> m_Aliases;

/** \brief Populate the list of items that have a (hard coded) default value.
*/
void initDefaults();

/** \brief Populate the list of aliases.
* (legacy names for settings that have been used in the past).
*/
void initAliases();
};

typedef QucsSingleton<settingsManager> _settings;
Loading