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) {
m_slots[parent] = slot;
QObject::connect(parent, &QObject::destroyed, [this, slot, parent]() {
QObject::connect(parent, &QObject::destroyed, this, [this, parent]() {
m_slots.remove(parent);
});
}
@ -37,10 +37,10 @@ Action* ConfigOption::addValue(const QString& text, const QVariant& value, Actio
if (actions) {
action = actions->addAction(text, name, function, menu);
} else {
action = new Action(function, name, text);
action = new Action(function, name, text, this);
}
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.append(std::make_pair(action, value));
@ -59,10 +59,10 @@ Action* ConfigOption::addBoolean(const QString& text, ActionMapper* actions, con
if (actions) {
action = actions->addBooleanAction(text, m_name, function, menu);
} 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.append(std::make_pair(action, 1));
@ -103,7 +103,7 @@ ConfigController::ConfigController(QObject* parent)
QString fileName = configDir();
fileName.append(QDir::separator());
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);
@ -150,7 +150,7 @@ ConfigOption* ConfigController::addOption(const char* key) {
}
ConfigOption* newOption = new ConfigOption(optionName, this);
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);
});
return newOption;
@ -292,12 +292,11 @@ void ConfigController::makePortable() {
QString fileName(configDir());
fileName.append(QDir::separator());
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()) {
settings2->setValue(key, m_settings->value(key));
}
delete m_settings;
m_settings = settings2;
m_settings = std::move(settings2);
}
bool ConfigController::isPortable() {

View File

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