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.cpp | |
parent | 8455e38d9269cfeeb33cbf123688e19186c8be4c (diff) | |
download | kube-243f18fed2ee9b8f2778e83ee4fbe2b3275b0370.tar.gz kube-243f18fed2ee9b8f2778e83ee4fbe2b3275b0370.zip |
Subcontrollers for list properties
Diffstat (limited to 'framework/src/domain/controller.cpp')
-rw-r--r-- | framework/src/domain/controller.cpp | 108 |
1 files changed, 107 insertions, 1 deletions
diff --git a/framework/src/domain/controller.cpp b/framework/src/domain/controller.cpp index 52f4cd1f..82a761f6 100644 --- a/framework/src/domain/controller.cpp +++ b/framework/src/domain/controller.cpp | |||
@@ -20,16 +20,24 @@ | |||
20 | 20 | ||
21 | #include <QQmlEngine> | 21 | #include <QQmlEngine> |
22 | #include <QMetaProperty> | 22 | #include <QMetaProperty> |
23 | #include <QStandardItemModel> | ||
24 | #include <QStandardItem> | ||
25 | #include <QUuid> | ||
23 | #include <sink/log.h> | 26 | #include <sink/log.h> |
24 | 27 | ||
25 | using namespace Kube; | 28 | using namespace Kube; |
26 | 29 | ||
27 | ControllerAction::ControllerAction() | 30 | ControllerState::ControllerState() |
28 | : QObject() | 31 | : QObject() |
29 | { | 32 | { |
30 | QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); | 33 | QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership); |
31 | } | 34 | } |
32 | 35 | ||
36 | ControllerAction::ControllerAction() | ||
37 | : ControllerState() | ||
38 | { | ||
39 | } | ||
40 | |||
33 | void ControllerAction::execute() | 41 | void ControllerAction::execute() |
34 | { | 42 | { |
35 | emit triggered(); | 43 | emit triggered(); |
@@ -57,3 +65,101 @@ void Controller::run(const KAsync::Job<void> &job) | |||
57 | //TODO attach a log context to the execution that we can gather from the job? | 65 | //TODO attach a log context to the execution that we can gather from the job? |
58 | jobToExec.exec(); | 66 | jobToExec.exec(); |
59 | } | 67 | } |
68 | |||
69 | |||
70 | static void traverse(const QStandardItemModel *model, const std::function<bool(QStandardItem *item)> &f) | ||
71 | { | ||
72 | auto root = model->invisibleRootItem(); | ||
73 | for (int row = 0; row < root->rowCount(); row++) { | ||
74 | if (!f(root->child(row, 0))) { | ||
75 | return; | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | |||
80 | ListPropertyController::ListPropertyController(const QStringList &roles) | ||
81 | : QObject(), | ||
82 | mModel(new QStandardItemModel) | ||
83 | { | ||
84 | //Generate a set of roles for the names. We're not using any enum, so the actual role value doesn't matter. | ||
85 | int role = Qt::UserRole + 1; | ||
86 | mRoles.insert("id", role); | ||
87 | role++; | ||
88 | for (const auto &r : roles) { | ||
89 | mRoles.insert(r, role); | ||
90 | role++; | ||
91 | } | ||
92 | |||
93 | QHash<int, QByteArray> roleNames; | ||
94 | for (const auto r : mRoles.keys()) { | ||
95 | roleNames.insert(mRoles[r], r.toLatin1()); | ||
96 | } | ||
97 | mModel->setItemRoleNames(roleNames); | ||
98 | } | ||
99 | |||
100 | void ListPropertyController::add(const QVariantMap &value) | ||
101 | { | ||
102 | auto item = new QStandardItem; | ||
103 | auto id = QUuid::createUuid().toByteArray(); | ||
104 | item->setData(id, mRoles["id"]); | ||
105 | for (const auto &k : value.keys()) { | ||
106 | item->setData(value.value(k), mRoles[k]); | ||
107 | } | ||
108 | mModel->appendRow(QList<QStandardItem*>() << item); | ||
109 | emit added(id, value); | ||
110 | } | ||
111 | |||
112 | void ListPropertyController::remove(const QByteArray &id) | ||
113 | { | ||
114 | auto root = mModel->invisibleRootItem(); | ||
115 | const auto idRole = mRoles["id"]; | ||
116 | for (int row = 0; row < root->rowCount(); row++) { | ||
117 | if (root->child(row, 0)->data(idRole).toByteArray() == id) { | ||
118 | root->removeRow(row); | ||
119 | return; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||
124 | void ListPropertyController::clear() | ||
125 | { | ||
126 | mModel->clear(); | ||
127 | } | ||
128 | |||
129 | QAbstractItemModel *ListPropertyController::model() | ||
130 | { | ||
131 | QQmlEngine::setObjectOwnership(mModel.data(), QQmlEngine::CppOwnership); | ||
132 | return mModel.data(); | ||
133 | } | ||
134 | |||
135 | void ListPropertyController::setValue(const QByteArray &id, const QString &key, const QVariant &value) | ||
136 | { | ||
137 | setValues(id, {{key, value}}); | ||
138 | } | ||
139 | |||
140 | void ListPropertyController::setValues(const QByteArray &id, const QVariantMap &values) | ||
141 | { | ||
142 | const auto idRole = mRoles["id"]; | ||
143 | ::traverse(mModel.data(), [&] (QStandardItem *item) { | ||
144 | if (item->data(idRole).toByteArray() == id) { | ||
145 | for (const auto &key : values.keys()) { | ||
146 | item->setData(values.value(key), mRoles[key]); | ||
147 | } | ||
148 | return false; | ||
149 | } | ||
150 | return true; | ||
151 | }); | ||
152 | } | ||
153 | |||
154 | void ListPropertyController::traverse(const std::function<void(const QVariantMap &)> &f) | ||
155 | { | ||
156 | ::traverse(mModel.data(), [&] (QStandardItem *item) { | ||
157 | QVariantMap map; | ||
158 | for (const auto &key : mRoles.keys()) { | ||
159 | map.insert(key, item->data(mRoles[key])); | ||
160 | } | ||
161 | f(map); | ||
162 | return true; | ||
163 | }); | ||
164 | } | ||
165 | |||