Debian Codegraph @

src/Gui/ParamHandler.h

raw
1 /****************************************************************************
2 * Copyright (c) 2023 Zheng Lei (realthunder) <[email protected]> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * FreeCAD is free software: you can redistribute it and/or modify it *
7 * under the terms of the GNU Lesser General Public License as *
8 * published by the Free Software Foundation, either version 2.1 of the *
9 * License, or (at your option) any later version. *
10 * *
11 * FreeCAD is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with FreeCAD. If not, see *
18 * <https://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21
22 #ifndef GUI_PARAM_HANDLER_H
23 #define GUI_PARAM_HANDLER_H
24
25 #include <map>
26 #include <memory>
27 #include <boost/signals2.hpp>
28 #include <QTimer>
29
30 #include <FCGlobal.h>
31 #include <App/Application.h>
32 #include <Base/Parameter.h>
33
34 namespace Gui
35 {
36
37 /// Structure for storing a parameter key and its path to be used in std::map
38 struct GuiExport ParamKey
39 {
40 ParameterGrp::handle hGrp;
41 const char* key;
42
43 ParamKey(const char* path, const char* key)
44 : hGrp(App::GetApplication().GetUserParameter().GetGroup(path))
45 , key(key)
46 {}
47
48 ParamKey(ParameterGrp* h, const char* key)
49 : hGrp(h)
50 , key(key)
51 {}
52
53 bool operator<(const ParamKey& other) const
54 {
55 if (hGrp < other.hGrp) {
56 return true;
57 }
58 if (hGrp > other.hGrp) {
59 return false;
60 }
61 return strcmp(key, other.key) < 0;
62 }
63 };
64
65 /// Helper class to handle parameter change
66 class GuiExport ParamHandler
67 {
68 public:
69 virtual ~ParamHandler()
70 {}
71
72 /** Called when the corresponding parameter key changes
73 * @param key: the parameter key
74 * @return Returns true if the handler needs to be delay triggered by a timer
75 */
76 virtual bool onChange(const ParamKey* key) = 0;
77 /// Called in delay triggered
78 virtual void onTimer()
79 {}
80 };
81
82 /// Template class for a non-delayed parameter handler
83 template<class Func>
84 class ParamHandlerT: public ParamHandler
85 {
86 public:
87 ParamHandlerT(Func f)
88 : func(f)
89 {}
90
91 bool onChange(const ParamKey* key) override
92 {
93 func(key);
94 return false;
95 }
96
97 private:
98 Func func;
99 };
100
101 /// Template class for a delayed parameter handler
102 template<class Func>
103 class ParamDelayedHandlerT: public ParamHandler
104 {
105 public:
106 ParamDelayedHandlerT(Func f)
107 : func(f)
108 {}
109
110 bool onChange(const ParamKey*) override
111 {
112 return true;
113 }
114
115 void onTimer() override
116 {
117 func();
118 }
119
120 private:
121 Func func;
122 };
123
124 // Helper class to manage handlers of a list of parameters.
125 //
126 // The handlers are stored in a map from ParamKey to shared pointer to a
127 // ParamHandler. The same handler can be registered with multiple keys. When
128 // the registered parameter key is changed, the manager will call the
129 // registered handler function ParamHandler::onChange(). If it returns True,
130 // then the handler will be appended to a queue to be invoked later by a timer
131 // to avoid repetitive processing on change of multiple keys.
132 //
133 // The handler manager is meant to be initiated by some static function, e.g.
134 // DlgSettingsGeneral::attachObserver(). It is intended to be one and only
135 // place of handling changes of the given set of parameters, regardless of
136 // whether the changes are coming from direct editing through parameter editor,
137 // user code changing of parameters, changing preference dialog, or loading a
138 // preference pack.
139 //
140 class GuiExport ParamHandlers
141 {
142 public:
143 ParamHandlers();
144 virtual ~ParamHandlers();
145
146 void addHandler(const ParamKey& key, const std::shared_ptr<ParamHandler>& handler);
147
148 void addHandler(const char* path, const char* key, const std::shared_ptr<ParamHandler>& handler)
149 {
150 addHandler(ParamKey(path, key), handler);
151 }
152
153 void addHandler(ParameterGrp* hGrp, const char* key, const std::shared_ptr<ParamHandler>& handler)
154 {
155 addHandler(ParamKey(hGrp, key), handler);
156 }
157
158 void addHandler(const std::vector<ParamKey>& keys, const std::shared_ptr<ParamHandler>& handler)
159 {
160 for (const auto& key : keys) {
161 addHandler(key, handler);
162 }
163 }
164
165 void addHandler(
166 const char* path,
167 const std::vector<const char*>& keys,
168 const std::shared_ptr<ParamHandler>& handler
169 )
170 {
171 for (const auto& key : keys) {
172 addHandler(path, key, handler);
173 }
174 }
175
176 void addHandler(
177 ParameterGrp* hGrp,
178 const std::vector<const char*>& keys,
179 const std::shared_ptr<ParamHandler>& handler
180 )
181 {
182 for (const auto& key : keys) {
183 addHandler(hGrp, key, handler);
184 }
185 }
186
187 template<class Func>
188 std::shared_ptr<ParamHandler> addHandler(const char* path, const char* key, Func func)
189 {
190 std::shared_ptr<ParamHandler> handler(new ParamHandlerT<Func>(func));
191 addHandler(path, key, handler);
192 return handler;
193 }
194
195 template<class Func>
196 std::shared_ptr<ParamHandler> addHandler(ParameterGrp* hGrp, const char* key, Func func)
197 {
198 std::shared_ptr<ParamHandler> handler(new ParamHandlerT<Func>(func));
199 addHandler(hGrp, key, handler);
200 return handler;
201 }
202
203 template<class Func>
204 std::shared_ptr<ParamHandler> addDelayedHandler(const char* path, const char* key, Func func)
205 {
206 auto hGrp = App::GetApplication().GetUserParameter().GetGroup(path);
207 auto wrap = [hGrp, func]() {
208 func(hGrp);
209 };
210 std::shared_ptr<ParamHandler> handler(new ParamDelayedHandlerT<decltype(wrap)>(wrap));
211 addHandler(hGrp, key, handler);
212 return handler;
213 }
214
215 template<class Func>
216 std::shared_ptr<ParamHandler> addDelayedHandler(ParameterGrp* hGrp, const char* key, Func func)
217 {
218 auto wrap = [hGrp, func]() {
219 func(hGrp);
220 };
221 std::shared_ptr<ParamHandler> handler(new ParamDelayedHandlerT<decltype(wrap)>(wrap));
222 addHandler(hGrp, key, handler);
223 return handler;
224 }
225
226 template<class Func>
227 std::shared_ptr<ParamHandler> addDelayedHandler(
228 const char* path,
229 const std::vector<const char*>& keys,
230 Func func
231 )
232 {
233 auto hGrp = App::GetApplication().GetUserParameter().GetGroup(path);
234 auto wrap = [hGrp, func]() {
235 func(hGrp);
236 };
237 std::shared_ptr<ParamHandler> handler(new ParamDelayedHandlerT<decltype(wrap)>(wrap));
238 for (const auto& key : keys) {
239 addHandler(ParamKey(hGrp, key), handler);
240 }
241 return handler;
242 }
243
244 template<class Func>
245 std::shared_ptr<ParamHandler> addDelayedHandler(
246 ParameterGrp::handle hGrp,
247 const std::vector<const char*>& keys,
248 Func func
249 )
250 {
251 auto wrap = [hGrp, func]() {
252 func(hGrp);
253 };
254 std::shared_ptr<ParamHandler> handler(new ParamDelayedHandlerT<decltype(wrap)>(wrap));
255 for (const auto& key : keys) {
256 addHandler(ParamKey(hGrp, key), handler);
257 }
258 return handler;
259 }
260
261 protected:
262 std::map<ParamKey, std::shared_ptr<ParamHandler>> handlers;
263 std::set<std::shared_ptr<ParamHandler>> pendings;
264 boost::signals2::scoped_connection conn;
265 QTimer timer;
266 };
267
268 } // namespace Gui
269
270 #endif // GUI_PARAM_HANDLER_H