diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-05 15:04:00 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-04-05 15:04:00 +0200 |
commit | 4b1798f0cdf87361869e7cf2b341acacd056c410 (patch) | |
tree | 3ff780641acdcb20b81f9b41533afd50a2525d38 /framework/src/domain/controller.h | |
parent | 71721aa4f3e85bea1a2fe504e86d99f80a3106a9 (diff) | |
download | kube-4b1798f0cdf87361869e7cf2b341acacd056c410.tar.gz kube-4b1798f0cdf87361869e7cf2b341acacd056c410.zip |
Moved cpp code into src directory
Diffstat (limited to 'framework/src/domain/controller.h')
-rw-r--r-- | framework/src/domain/controller.h | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/framework/src/domain/controller.h b/framework/src/domain/controller.h new file mode 100644 index 00000000..bf2a7ace --- /dev/null +++ b/framework/src/domain/controller.h | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include <QObject> | ||
22 | #include <QVariant> | ||
23 | #include <KAsync/Async> | ||
24 | |||
25 | #define KUBE_CONTROLLER_PROPERTY(TYPE, NAME, LOWERCASENAME) \ | ||
26 | public: Q_PROPERTY(TYPE LOWERCASENAME MEMBER m##NAME NOTIFY LOWERCASENAME##Changed) \ | ||
27 | Q_SIGNALS: void LOWERCASENAME##Changed(); \ | ||
28 | private: TYPE m##NAME; \ | ||
29 | public: \ | ||
30 | struct NAME { \ | ||
31 | static constexpr const char *name = #LOWERCASENAME; \ | ||
32 | typedef TYPE Type; \ | ||
33 | }; \ | ||
34 | void set##NAME(const TYPE &value) { setProperty(NAME::name, QVariant::fromValue(value)); } \ | ||
35 | void clear##NAME() { setProperty(NAME::name, QVariant{}); } \ | ||
36 | TYPE get##NAME() const { return m##NAME; } \ | ||
37 | |||
38 | |||
39 | #define KUBE_CONTROLLER_ACTION(NAME) \ | ||
40 | Q_PROPERTY (Kube::ControllerAction* NAME##Action READ NAME##Action CONSTANT) \ | ||
41 | private: QScopedPointer<Kube::ControllerAction> action_##NAME; \ | ||
42 | public: Kube::ControllerAction* NAME##Action() const { Q_ASSERT(action_##NAME); return action_##NAME.data(); } \ | ||
43 | private slots: void NAME(); \ | ||
44 | |||
45 | |||
46 | namespace Kube { | ||
47 | |||
48 | class ControllerAction : public QObject { | ||
49 | Q_OBJECT | ||
50 | Q_PROPERTY(bool enabled MEMBER mEnabled NOTIFY enabledChanged) | ||
51 | public: | ||
52 | ControllerAction(); | ||
53 | template <typename Func> | ||
54 | ControllerAction(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func slot) | ||
55 | : ControllerAction() | ||
56 | { | ||
57 | QObject::connect(this, &ControllerAction::triggered, obj, slot); | ||
58 | } | ||
59 | |||
60 | ~ControllerAction() = default; | ||
61 | |||
62 | Q_INVOKABLE void execute(); | ||
63 | void setEnabled(bool enabled) { setProperty("enabled", enabled); } | ||
64 | |||
65 | signals: | ||
66 | void enabledChanged(); | ||
67 | void triggered(); | ||
68 | |||
69 | private: | ||
70 | bool mEnabled = true; | ||
71 | }; | ||
72 | |||
73 | class Controller : public QObject { | ||
74 | Q_OBJECT | ||
75 | public: | ||
76 | Controller() = default; | ||
77 | virtual ~Controller() = default; | ||
78 | |||
79 | public slots: | ||
80 | virtual void clear(); | ||
81 | |||
82 | signals: | ||
83 | void done(); | ||
84 | void error(); | ||
85 | |||
86 | protected: | ||
87 | void run(const KAsync::Job<void> &job); | ||
88 | }; | ||
89 | |||
90 | } | ||