Qt: Clean up memory model in ConfigController

This commit is contained in:
Vicki Pfau 2021-01-23 20:25:19 -08:00
parent 3b1b890f3e
commit 561c253ff8
2 changed files with 14 additions and 14 deletions

View File

@ -23,7 +23,7 @@ ConfigOption::ConfigOption(const QString& name, QObject* parent)
void ConfigOption::connect(std::function<void(const QVariant&)> slot, QObject* parent) { void ConfigOption::connect(std::function<void(const QVariant&)> slot, QObject* parent) {
m_slots[parent] = slot; m_slots[parent] = slot;
QObject::connect(parent, &QObject::destroyed, [this, slot, parent]() { QObject::connect(parent, &QObject::destroyed, this, [this, parent]() {
m_slots.remove(parent); m_slots.remove(parent);
}); });
} }
@ -37,10 +37,10 @@ Action* ConfigOption::addValue(const QString& text, const QVariant& value, Actio
if (actions) { if (actions) {
action = actions->addAction(text, name, function, menu); action = actions->addAction(text, name, function, menu);
} else { } else {
action = new Action(function, name, text); action = new Action(function, name, text, this);
} }
action->setExclusive(); action->setExclusive();
QObject::connect(action, &QObject::destroyed, [this, action, value]() { QObject::connect(action, &QObject::destroyed, this, [this, action, value]() {
m_actions.removeAll(std::make_pair(action, value)); m_actions.removeAll(std::make_pair(action, value));
}); });
m_actions.append(std::make_pair(action, value)); m_actions.append(std::make_pair(action, value));
@ -59,10 +59,10 @@ Action* ConfigOption::addBoolean(const QString& text, ActionMapper* actions, con
if (actions) { if (actions) {
action = actions->addBooleanAction(text, m_name, function, menu); action = actions->addBooleanAction(text, m_name, function, menu);
} else { } else {
action = new Action(function, m_name, text); action = new Action(function, m_name, text, this);
} }
QObject::connect(action, &QObject::destroyed, [this, action]() { QObject::connect(action, &QObject::destroyed, this, [this, action]() {
m_actions.removeAll(std::make_pair(action, 1)); m_actions.removeAll(std::make_pair(action, 1));
}); });
m_actions.append(std::make_pair(action, 1)); m_actions.append(std::make_pair(action, 1));
@ -103,7 +103,7 @@ ConfigController::ConfigController(QObject* parent)
QString fileName = configDir(); QString fileName = configDir();
fileName.append(QDir::separator()); fileName.append(QDir::separator());
fileName.append("qt.ini"); fileName.append("qt.ini");
m_settings = new QSettings(fileName, QSettings::IniFormat, this); m_settings = std::make_unique<QSettings>(fileName, QSettings::IniFormat);
mCoreConfigInit(&m_config, PORT); mCoreConfigInit(&m_config, PORT);
@ -150,7 +150,7 @@ ConfigOption* ConfigController::addOption(const char* key) {
} }
ConfigOption* newOption = new ConfigOption(optionName, this); ConfigOption* newOption = new ConfigOption(optionName, this);
m_optionSet[optionName] = newOption; m_optionSet[optionName] = newOption;
connect(newOption, &ConfigOption::valueChanged, [this, key](const QVariant& value) { connect(newOption, &ConfigOption::valueChanged, this, [this, key](const QVariant& value) {
setOption(key, value); setOption(key, value);
}); });
return newOption; return newOption;
@ -292,12 +292,11 @@ void ConfigController::makePortable() {
QString fileName(configDir()); QString fileName(configDir());
fileName.append(QDir::separator()); fileName.append(QDir::separator());
fileName.append("qt.ini"); fileName.append("qt.ini");
QSettings* settings2 = new QSettings(fileName, QSettings::IniFormat, this); auto settings2 = std::make_unique<QSettings>(fileName, QSettings::IniFormat);
for (const auto& key : m_settings->allKeys()) { for (const auto& key : m_settings->allKeys()) {
settings2->setValue(key, m_settings->value(key)); settings2->setValue(key, m_settings->value(key));
} }
delete m_settings; m_settings = std::move(settings2);
m_settings = settings2;
} }
bool ConfigController::isPortable() { bool ConfigController::isPortable() {

View File

@ -7,12 +7,13 @@
#include "Override.h" #include "Override.h"
#include <QMap> #include <QHash>
#include <QObject> #include <QObject>
#include <QSettings> #include <QSettings>
#include <QVariant> #include <QVariant>
#include <functional> #include <functional>
#include <memory>
#include <mgba/core/config.h> #include <mgba/core/config.h>
#include <mgba-util/configuration.h> #include <mgba-util/configuration.h>
@ -53,7 +54,7 @@ signals:
void valueChanged(const QVariant& value); void valueChanged(const QVariant& value);
private: private:
QMap<QObject*, std::function<void(const QVariant&)>> m_slots; QHash<QObject*, std::function<void(const QVariant&)>> m_slots;
QList<std::pair<Action*, QVariant>> m_actions; QList<std::pair<Action*, QVariant>> m_actions;
QString m_name; QString m_name;
}; };
@ -110,8 +111,8 @@ private:
mCoreConfig m_config; mCoreConfig m_config;
mCoreOptions m_opts{}; mCoreOptions m_opts{};
QMap<QString, ConfigOption*> m_optionSet; QHash<QString, ConfigOption*> m_optionSet;
QSettings* m_settings; std::unique_ptr<QSettings> m_settings;
static QString s_configDir; static QString s_configDir;
}; };