diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-12-04 00:02:58 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2017-12-04 10:19:23 +0100 |
commit | 243f18fed2ee9b8f2778e83ee4fbe2b3275b0370 (patch) | |
tree | ac60f61ad7aaf9c997410ae2f7dcab1a128e157a /framework/src/domain/controller.h | |
parent | 8455e38d9269cfeeb33cbf123688e19186c8be4c (diff) | |
download | kube-243f18fed2ee9b8f2778e83ee4fbe2b3275b0370.tar.gz kube-243f18fed2ee9b8f2778e83ee4fbe2b3275b0370.zip |
Subcontrollers for list properties
Diffstat (limited to 'framework/src/domain/controller.h')
-rw-r--r-- | framework/src/domain/controller.h | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/framework/src/domain/controller.h b/framework/src/domain/controller.h index bf2a7ace..ba7ac8fe 100644 --- a/framework/src/domain/controller.h +++ b/framework/src/domain/controller.h | |||
@@ -42,13 +42,36 @@ | |||
42 | public: Kube::ControllerAction* NAME##Action() const { Q_ASSERT(action_##NAME); return action_##NAME.data(); } \ | 42 | public: Kube::ControllerAction* NAME##Action() const { Q_ASSERT(action_##NAME); return action_##NAME.data(); } \ |
43 | private slots: void NAME(); \ | 43 | private slots: void NAME(); \ |
44 | 44 | ||
45 | #define KUBE_CONTROLLER_LISTCONTROLLER(NAME) \ | ||
46 | Q_PROPERTY (Kube::ListPropertyController* NAME READ NAME##Controller CONSTANT) \ | ||
47 | private: QScopedPointer<Kube::ListPropertyController> controller_##NAME; \ | ||
48 | public: Kube::ListPropertyController* NAME##Controller() const { Q_ASSERT(controller_##NAME); return controller_##NAME.data(); } \ | ||
49 | |||
50 | |||
51 | class QAbstractItemModel; | ||
52 | class QStandardItemModel; | ||
45 | 53 | ||
46 | namespace Kube { | 54 | namespace Kube { |
47 | 55 | ||
48 | class ControllerAction : public QObject { | 56 | class ControllerState : public QObject { |
49 | Q_OBJECT | 57 | Q_OBJECT |
50 | Q_PROPERTY(bool enabled MEMBER mEnabled NOTIFY enabledChanged) | 58 | Q_PROPERTY(bool enabled MEMBER mEnabled NOTIFY enabledChanged) |
51 | public: | 59 | public: |
60 | ControllerState(); | ||
61 | ~ControllerState() = default; | ||
62 | |||
63 | void setEnabled(bool enabled) { setProperty("enabled", enabled); } | ||
64 | |||
65 | signals: | ||
66 | void enabledChanged(); | ||
67 | |||
68 | private: | ||
69 | bool mEnabled = false; | ||
70 | }; | ||
71 | |||
72 | class ControllerAction : public ControllerState { | ||
73 | Q_OBJECT | ||
74 | public: | ||
52 | ControllerAction(); | 75 | ControllerAction(); |
53 | template <typename Func> | 76 | template <typename Func> |
54 | ControllerAction(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func slot) | 77 | ControllerAction(const typename QtPrivate::FunctionPointer<Func>::Object *obj, Func slot) |
@@ -60,14 +83,9 @@ public: | |||
60 | ~ControllerAction() = default; | 83 | ~ControllerAction() = default; |
61 | 84 | ||
62 | Q_INVOKABLE void execute(); | 85 | Q_INVOKABLE void execute(); |
63 | void setEnabled(bool enabled) { setProperty("enabled", enabled); } | ||
64 | 86 | ||
65 | signals: | 87 | signals: |
66 | void enabledChanged(); | ||
67 | void triggered(); | 88 | void triggered(); |
68 | |||
69 | private: | ||
70 | bool mEnabled = true; | ||
71 | }; | 89 | }; |
72 | 90 | ||
73 | class Controller : public QObject { | 91 | class Controller : public QObject { |
@@ -87,4 +105,42 @@ protected: | |||
87 | void run(const KAsync::Job<void> &job); | 105 | void run(const KAsync::Job<void> &job); |
88 | }; | 106 | }; |
89 | 107 | ||
108 | class ListPropertyController : public QObject | ||
109 | { | ||
110 | Q_OBJECT | ||
111 | Q_PROPERTY(QAbstractItemModel* model READ model CONSTANT) | ||
112 | |||
113 | public: | ||
114 | ListPropertyController(const QStringList &roles); | ||
115 | Q_INVOKABLE virtual void add(const QVariantMap &value); | ||
116 | Q_INVOKABLE virtual void remove(const QByteArray &id); | ||
117 | Q_INVOKABLE void clear(); | ||
118 | |||
119 | QAbstractItemModel *model(); | ||
120 | |||
121 | void setValue(const QByteArray &id, const QString &key, const QVariant &); | ||
122 | void setValues(const QByteArray &id, const QVariantMap &values); | ||
123 | void traverse(const std::function<void(const QVariantMap &)> &f); | ||
124 | |||
125 | template<typename T> | ||
126 | QList<T> getList(const QString &property) | ||
127 | { | ||
128 | QList<T> list; | ||
129 | traverse([&] (const QVariantMap &map) { | ||
130 | list << map[property].value<T>(); | ||
131 | }); | ||
132 | return list; | ||
133 | } | ||
134 | |||
135 | Q_SIGNALS: | ||
136 | void added(QByteArray, QVariantMap); | ||
137 | |||
138 | protected: | ||
139 | QScopedPointer<QStandardItemModel> mModel; | ||
140 | |||
141 | private: | ||
142 | QHash<QString, int> mRoles; | ||
143 | }; | ||
144 | |||
145 | |||
90 | } | 146 | } |