Debian Codegraph @

Source/Core/DolphinQt/Config/ConfigControls/ConfigControl.h

raw symbols SCIP
1 // Copyright 2024 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <QFont>
7 #include <QMouseEvent>
8
9 #include "Common/Config/Enums.h"
10 #include "Common/Config/Layer.h"
11 #include "DolphinQt/Settings.h"
12
13 namespace Config
14 {
15 template <typename T>
16 class Info;
17 struct Location;
18 } // namespace Config
19
20 template <class Derived>
21 class ConfigControl : public Derived
22 {
23 public:
24 ConfigControl(const Config::Location& location, Config::Layer* layer)
25 : m_location(location), m_layer(layer)
26 {
27 ConnectConfig();
28 }
29 ConfigControl(const QString& label, const Config::Location& location, Config::Layer* layer)
30 : Derived(label), m_location(location), m_layer(layer)
31 {
32 ConnectConfig();
33 }
34 ConfigControl(const Qt::Orientation& orient, const Config::Location& location,
35 Config::Layer* layer)
36 : Derived(orient), m_location(location), m_layer(layer)
37 {
38 ConnectConfig();
39 }
40
41 const Config::Location GetLocation() const { return m_location; }
42
43 protected:
44 void ConnectConfig()
45 {
46 Derived::connect(&Settings::Instance(), &Settings::ConfigChanged, this, [this] {
47 QFont bf = Derived::font();
48 bf.setBold(IsConfigLocal());
49 Derived::setFont(bf);
50
51 // This isn't signal blocked because the UI may need to be updated.
52 m_updating = true;
53 OnConfigChanged();
54 m_updating = false;
55 });
56 }
57
58 template <typename T>
59 void SaveValue(const Config::Info<T>& setting, const T& value)
60 {
61 // Avoid OnConfigChanged -> option changed to current config's value -> unnecessary save ->
62 // ConfigChanged.
63 if (m_updating)
64 return;
65
66 if (m_layer != nullptr)
67 {
68 m_layer->Set(m_location, value);
69 Config::OnConfigChanged();
70 return;
71 }
72
73 Config::SetBaseOrCurrent(setting, value);
74 }
75
76 template <typename T>
77 const T ReadValue(const Config::Info<T>& setting) const
78 {
79 // For loading game specific settings. If the game setting doesn't exist, load the current
80 // global setting. There's no way to know what game is being edited, so GlobalGame settings
81 // can't be shown, but otherwise would be good to include.
82 if (m_layer != nullptr)
83 {
84 if (m_layer->Exists(m_location))
85 return m_layer->Get(setting);
86 else
87 return Config::GetBase(setting);
88 }
89
90 return Config::Get(setting);
91 }
92
93 virtual void OnConfigChanged() {}
94
95 private:
96 bool IsConfigLocal() const
97 {
98 if (m_layer != nullptr)
99 return m_layer->Exists(m_location);
100 else
101 return Config::GetActiveLayerForConfig(m_location) != Config::LayerType::Base;
102 }
103
104 void mousePressEvent(QMouseEvent* event) override
105 {
106 if (m_layer != nullptr && event->button() == Qt::RightButton)
107 {
108 m_layer->DeleteKey(m_location);
109 Config::OnConfigChanged();
110 }
111 else
112 {
113 Derived::mousePressEvent(event);
114 }
115 }
116
117 bool m_updating = false;
118 const Config::Location m_location;
119 Config::Layer* m_layer;
120 };