MNE-CPP 0.1.9
A Framework for Electrophysiology
Loading...
Searching...
No Matches
command.cpp
1
2//=============================================================================================================
3// Includes
4//=============================================================================================================
5
6#include "command.h"
7#include "commandmanager.h"
8
9//=============================================================================================================
10// Qt Includes
11//=============================================================================================================
12
13#include <QVector>
14#include <QString>
15#include <QDebug>
16
17//=============================================================================================================
18// USED NAMESPACES
19//=============================================================================================================
20
21using namespace COMMUNICATIONLIB;
22
23//=============================================================================================================
24// DEFINE MEMBER METHODS
25//=============================================================================================================
26
27Command::Command(bool p_bIsJson, QObject *parent)
28: QObject(parent)
29, m_sCommand("")
30, m_sDescription("")
31, m_bIsJson(p_bIsJson)
32{
33}
34
35//=============================================================================================================
36
37Command::Command(const QString &p_sCommand, const QJsonObject &p_qCommandDescription, bool p_bIsJson, QObject *parent)
38: QObject(parent)
39, m_bIsJson(p_bIsJson)
40{
41 this->m_sCommand = p_sCommand;
42 this->m_sDescription = p_qCommandDescription.value(QString("description")).toString();
43
44 QJsonObject t_jsonObjectParameter = p_qCommandDescription.value(QString("parameters")).toObject();
45
46 QJsonObject::Iterator it;
47
48 for(it = t_jsonObjectParameter.begin(); it != t_jsonObjectParameter.end(); ++it)
49 {
50 QJsonValue t_jsonValueType = it.value().toObject().value(QString("type"));
51 QVariant::Type t_type = QVariant::nameToType(t_jsonValueType.toString().toUtf8().constData());
52
53 this->m_qListParamNames.push_back(it.key());
54 this->m_qListParamValues.push_back(QVariant(t_type));
55 this->m_qListParamDescriptions.push_back(it.value().toObject().value(QString("description")).toString());
56 }
57}
58
59//=============================================================================================================
60
61Command::Command(const QString &p_sCommand, const QString &p_sDescription, bool p_bIsJson, QObject *parent)
62: QObject(parent)
63, m_sCommand(p_sCommand)
64, m_sDescription(p_sDescription)
65, m_bIsJson(p_bIsJson)
66{
67}
68
69//=============================================================================================================
70
71Command::Command( const QString &p_sCommand, const QString &p_sDescription,
72 const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, bool p_bIsJson, QObject *parent)
73: QObject(parent)
74, m_sCommand(p_sCommand)
75, m_sDescription(p_sDescription)
76, m_bIsJson(p_bIsJson)
77{
78 m_qListParamNames = p_qListParamNames;
79 m_qListParamValues = p_qListParamValues;
80
81 for(qint32 i = 0; i < p_qListParamValues.size(); ++i)
82 m_qListParamDescriptions.append("");
83}
84
85//=============================================================================================================
86
87Command::Command( const QString &p_sCommand, const QString &p_sDescription,
88 const QStringList &p_qListParamNames, const QList<QVariant> &p_qListParamValues, const QStringList &p_vecParameterDescriptions, bool p_bIsJson, QObject *parent)
89: QObject(parent)
90, m_sCommand(p_sCommand)
91, m_sDescription(p_sDescription)
92, m_bIsJson(p_bIsJson)
93{
94 if(p_qListParamNames.size() == p_qListParamValues.size())
95 {
96 if(p_qListParamValues.size() == p_vecParameterDescriptions.size())
97 {
98 m_qListParamNames = p_qListParamNames;
99 m_qListParamValues = p_qListParamValues;
100 m_qListParamDescriptions = p_vecParameterDescriptions;
101 }
102 }
103 else
104 {
105 printf("error: description vector hasn't the same size like parameter map.\n");
106 return;
107 }
108}
109
110//=============================================================================================================
111
112Command::Command(const Command &p_Command)
113: QObject(p_Command.parent())
114, m_sCommand(p_Command.m_sCommand)
115, m_sDescription(p_Command.m_sDescription)
116, m_qListParamNames(p_Command.m_qListParamNames)
117, m_qListParamValues(p_Command.m_qListParamValues)
118, m_qListParamDescriptions(p_Command.m_qListParamDescriptions)
119, m_bIsJson(p_Command.m_bIsJson)
120{
121}
122
123//=============================================================================================================
124
128
129//=============================================================================================================
130
132{
133 emit this->executed(*this);
134}
135
136//=============================================================================================================
137
138void Command::reply(const QString &p_sReply)
139{
140 CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
141
142 if(t_commandManager) {
143 emit t_commandManager->response(p_sReply, *this);
144 }
145}
146
147//=============================================================================================================
148
150{
151 CommandManager* t_commandManager = static_cast<CommandManager*> (this->parent());
152
153 if(t_commandManager) {
154 emit t_commandManager->triggered(*this);
155 }
156}
157
158//=============================================================================================================
159
160QJsonObject Command::toJsonObject() const
161{
162 QJsonObject p_jsonCommandObject;
163 p_jsonCommandObject.insert("description", QJsonValue(m_sDescription));
164
165 QJsonObject t_jsonAllParametersObject;
166 for(qint32 i = 0; i < m_qListParamValues.size(); ++i)
167 {
168 QJsonObject t_jsonParameterObject;
169 t_jsonParameterObject.insert("description",QJsonValue(m_qListParamDescriptions[i]));
170 t_jsonParameterObject.insert("type",QString(m_qListParamValues[i].typeName()));
171 t_jsonAllParametersObject.insert(m_qListParamNames[i], QJsonValue(t_jsonParameterObject));
172 }
173 p_jsonCommandObject.insert("parameters", QJsonValue(t_jsonAllParametersObject));
174
175 return p_jsonCommandObject;
176}
177
178//=============================================================================================================
179
180QStringList Command::toStringList() const
181{
182 QStringList p_stringCommandList;
183
184 p_stringCommandList << m_sCommand;
185
186 QString t_sParameters;
187 for(qint32 i = 0; i < m_qListParamDescriptions.size(); ++i)
188 {
189 t_sParameters.append("[");
190 t_sParameters.append(m_qListParamDescriptions[i]);
191 t_sParameters.append("]");
192 }
193 p_stringCommandList << t_sParameters;
194
195 p_stringCommandList << m_sDescription;
196
197 return p_stringCommandList;
198}
199
200//=============================================================================================================
201
203{
204 QString p_stringCommand;
205
206 QString t_sParameters;
207 for(qint32 i = 0; i < m_qListParamNames.size(); ++i)
208 {
209// qDebug() << m_qListParamValues[i];
210 t_sParameters.append(QString("\"%1\":\"%2\"").arg(m_qListParamNames[i]).arg(m_qListParamValues[i].toString()));
211
212 if(i < m_qListParamNames.size()-1)
213 t_sParameters.append(",");
214 }
215
216 p_stringCommand.append(QString("\"%1\":{%2}").arg(m_sCommand).arg(t_sParameters));
217
218 return p_stringCommand;
219}
220
221//=============================================================================================================
222
224{
225 if (this != &rhs) // protect against invalid self-assignment
226 {
227 m_sCommand = rhs.m_sCommand;
228 m_sDescription = rhs.m_sDescription;
229 m_qListParamNames = rhs.m_qListParamNames;
230 m_qListParamValues = rhs.m_qListParamValues;
231 m_qListParamDescriptions = rhs.m_qListParamDescriptions;
232 }
233 // to support chained assignment operators (a=b=c), always return *this
234 return *this;
235}
236
237//=============================================================================================================
238
239QVariant& Command::operator[] (const QString &key)
240{
241 if(m_qListParamNames.contains(key))
242 return m_qListParamValues[m_qListParamNames.indexOf(key)];
243 else
244 return defaultVariant;
245}
246
247//=============================================================================================================
248
249QVariant& Command::operator[] (qint32 idx)
250{
251 if(m_qListParamValues.size() > idx)
252 return m_qListParamValues[idx];
253 else
254 return defaultVariant;
255}
256
257//=============================================================================================================
258
259const QVariant Command::operator[] (const QString &key) const
260{
261 if(m_qListParamNames.contains(key))
262 return m_qListParamValues[m_qListParamNames.indexOf(key)];
263 else
264 return defaultVariant;
265}
Declaration of the Command Class.
QStringList toStringList() const
Definition command.cpp:180
QJsonObject toJsonObject() const
Definition command.cpp:160
void reply(const QString &p_sReply)
Definition command.cpp:138
Command & operator=(const Command &rhs)
Definition command.cpp:223
Command(bool p_bIsJson=true, QObject *parent=0)
Definition command.cpp:27
void executed(Command p_command)
virtual void execute()
Definition command.cpp:131
QString toStringReadySend() const
Definition command.cpp:202
QVariant & operator[](const QString &key)
Definition command.cpp:239
void triggered(Command p_command)
void response(QString p_sReply, Command p_command)