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/actions/action.cpp | |
parent | 71721aa4f3e85bea1a2fe504e86d99f80a3106a9 (diff) | |
download | kube-4b1798f0cdf87361869e7cf2b341acacd056c410.tar.gz kube-4b1798f0cdf87361869e7cf2b341acacd056c410.zip |
Moved cpp code into src directory
Diffstat (limited to 'framework/src/actions/action.cpp')
-rw-r--r-- | framework/src/actions/action.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/framework/src/actions/action.cpp b/framework/src/actions/action.cpp new file mode 100644 index 00000000..1344d112 --- /dev/null +++ b/framework/src/actions/action.cpp | |||
@@ -0,0 +1,127 @@ | |||
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 | #include "action.h" | ||
20 | |||
21 | #include <QDebug> | ||
22 | #include <QEvent> | ||
23 | #include <QPointer> | ||
24 | #include <QDynamicPropertyChangeEvent> | ||
25 | #include <QMetaObject> | ||
26 | #include <QMetaProperty> | ||
27 | |||
28 | #include "actionbroker.h" | ||
29 | #include "actionhandler.h" | ||
30 | #include "context.h" | ||
31 | |||
32 | using namespace Kube; | ||
33 | |||
34 | Action::Action(QObject *parent) | ||
35 | : QObject(parent), | ||
36 | mContext(nullptr) | ||
37 | { | ||
38 | } | ||
39 | |||
40 | Action::Action(const QByteArray &actionId, Context &context, QObject *parent) | ||
41 | : QObject(parent), | ||
42 | mContext(&context), | ||
43 | mActionId(actionId) | ||
44 | { | ||
45 | setContext(&context); | ||
46 | } | ||
47 | |||
48 | Action::~Action() | ||
49 | { | ||
50 | } | ||
51 | |||
52 | void Action::setContext(Context *context) | ||
53 | { | ||
54 | //Get notified when any property changes | ||
55 | for (int i = context->metaObject()->propertyOffset(); i < context->metaObject()->propertyCount(); i++) { | ||
56 | auto property = context->metaObject()->property(i) ; | ||
57 | // qWarning() << "Property " << property.name() << property.hasNotifySignal() << property.notifySignal().name(); | ||
58 | if (QString(property.name()) != "objectName") { | ||
59 | //We do what SIGNAL does to connect to the changed signal automatically | ||
60 | QObject::connect(context, "2"+property.notifySignal().name()+"()", this, SLOT(contextChanged())); | ||
61 | } | ||
62 | } | ||
63 | mContext = context; | ||
64 | mContext->installEventFilter(this); | ||
65 | emit readyChanged(); | ||
66 | } | ||
67 | |||
68 | bool Action::eventFilter(QObject *obj, QEvent *e) | ||
69 | { | ||
70 | if (obj == mContext) { | ||
71 | if (e->type() == QEvent::DynamicPropertyChange) { | ||
72 | contextChanged(); | ||
73 | } | ||
74 | } | ||
75 | return QObject::eventFilter(obj, e); | ||
76 | } | ||
77 | |||
78 | void Action::contextChanged() | ||
79 | { | ||
80 | emit readyChanged(); | ||
81 | } | ||
82 | |||
83 | Context *Action::context() const | ||
84 | { | ||
85 | return mContext; | ||
86 | } | ||
87 | |||
88 | void Action::setActionId(const QByteArray &actionId) | ||
89 | { | ||
90 | mActionId = actionId; | ||
91 | emit readyChanged(); | ||
92 | } | ||
93 | |||
94 | QByteArray Action::actionId() const | ||
95 | { | ||
96 | return mActionId; | ||
97 | } | ||
98 | |||
99 | bool Action::ready() const | ||
100 | { | ||
101 | return ActionBroker::instance().isActionReady(mActionId, mContext, mPreHandler); | ||
102 | } | ||
103 | |||
104 | void Action::execute() | ||
105 | { | ||
106 | ActionBroker::instance().executeAction(mActionId, mContext, mPreHandler, mPostHandler); | ||
107 | } | ||
108 | |||
109 | ActionResult Action::executeWithResult() | ||
110 | { | ||
111 | return ActionBroker::instance().executeAction(mActionId, mContext, mPreHandler, mPostHandler); | ||
112 | } | ||
113 | |||
114 | void Action::addPreHandler(ActionHandler *handler) | ||
115 | { | ||
116 | //For cleanup | ||
117 | handler->setParent(this); | ||
118 | mPreHandler << handler; | ||
119 | } | ||
120 | |||
121 | void Action::addPostHandler(ActionHandler *handler) | ||
122 | { | ||
123 | //For cleanup | ||
124 | handler->setParent(this); | ||
125 | mPostHandler << handler; | ||
126 | } | ||
127 | |||