src/platform/qt/ConfigController.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#pragma once
7
8#include "Override.h"
9
10#include <QMap>
11#include <QObject>
12#include <QSettings>
13#include <QVariant>
14
15#include <functional>
16
17#include <mgba/core/config.h>
18#include <mgba-util/configuration.h>
19#include <mgba/feature/commandline.h>
20
21class QAction;
22class QMenu;
23
24struct mArguments;
25struct GBACartridgeOverride;
26
27namespace QGBA {
28
29class ConfigOption : public QObject {
30Q_OBJECT
31
32public:
33 ConfigOption(QObject* parent = nullptr);
34
35 void connect(std::function<void(const QVariant&)>, QObject* parent = nullptr);
36
37 QAction* addValue(const QString& text, const QVariant& value, QMenu* parent = nullptr);
38 QAction* addValue(const QString& text, const char* value, QMenu* parent = nullptr);
39 QAction* addBoolean(const QString& text, QMenu* parent = nullptr);
40
41public slots:
42 void setValue(bool value);
43 void setValue(int value);
44 void setValue(unsigned value);
45 void setValue(const char* value);
46 void setValue(const QVariant& value);
47
48signals:
49 void valueChanged(const QVariant& value);
50
51private:
52 QMap<QObject*, std::function<void(const QVariant&)>> m_slots;
53 QList<QPair<QAction*, QVariant>> m_actions;
54};
55
56class ConfigController : public QObject {
57Q_OBJECT
58
59public:
60 constexpr static const char* const PORT = "qt";
61 static const int MRU_LIST_SIZE = 10;
62
63 ConfigController(QObject* parent = nullptr);
64 ~ConfigController();
65
66 const mCoreOptions* options() const { return &m_opts; }
67 bool parseArguments(mArguments* args, int argc, char* argv[], mSubParser* subparser = nullptr);
68
69 ConfigOption* addOption(const char* key);
70 void updateOption(const char* key);
71
72 QString getOption(const char* key, const QVariant& defaultVal = QVariant()) const;
73 QString getOption(const QString& key, const QVariant& defaultVal = QVariant()) const;
74
75 QVariant getQtOption(const QString& key, const QString& group = QString()) const;
76
77 QList<QString> getMRU() const;
78 void setMRU(const QList<QString>& mru);
79
80 Configuration* overrides() { return mCoreConfigGetOverrides(&m_config); }
81 void saveOverride(const Override&);
82
83 Configuration* input() { return mCoreConfigGetInput(&m_config); }
84
85 const mCoreConfig* config() { return &m_config; }
86
87 static const QString& configDir();
88
89public slots:
90 void setOption(const char* key, bool value);
91 void setOption(const char* key, int value);
92 void setOption(const char* key, unsigned value);
93 void setOption(const char* key, const char* value);
94 void setOption(const char* key, const QVariant& value);
95 void setQtOption(const QString& key, const QVariant& value, const QString& group = QString());
96
97 void makePortable();
98 void write();
99
100private:
101 Configuration* defaults() { return &m_config.defaultsTable; }
102
103 mCoreConfig m_config;
104 mCoreOptions m_opts{};
105
106 QMap<QString, ConfigOption*> m_optionSet;
107 QSettings* m_settings;
108 static QString s_configDir;
109};
110
111}