forked from espakm/qRestAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqRestResult.cpp
153 lines (129 loc) · 4.18 KB
/
qRestResult.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*==============================================================================
Library: qRestAPI
Copyright (c) 2010 Kitware Inc.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
and was partially funded by NIH grant 3P41RR013218-12S1
==============================================================================*/
#include "qRestResult.h"
#include <QDebug>
#include <QEventLoop>
#include <QNetworkReply>
#include <QMetaProperty>
// --------------------------------------------------------------------------
// qRestAPIResult methods
// --------------------------------------------------------------------------
qRestResult::qRestResult(const QUuid& queryId, QObject* parent)
: QObject(parent)
, QueryId(queryId)
, done(false)
{
}
// --------------------------------------------------------------------------
qRestResult::~qRestResult()
{
}
// --------------------------------------------------------------------------
const QUuid& qRestResult::queryId() const
{
return this->QueryId;
}
// --------------------------------------------------------------------------
const QList<QVariantMap>& qRestResult::results() const
{
return this->Result;
}
// --------------------------------------------------------------------------
const QVariantMap qRestResult::result() const
{
return this->Result[0];
}
// --------------------------------------------------------------------------
const QString& qRestResult::error() const
{
return this->Error;
}
// --------------------------------------------------------------------------
void qRestResult::setResult()
{
this->done = true;
emit ready();
}
// --------------------------------------------------------------------------
void qRestResult::setResult(const QList<QVariantMap>& result)
{
this->Result.clear();
for (int i = 0; i < result.size(); ++i)
this->Result.push_back(result[i]);
// this->Result = result;
this->done = true;
emit ready();
}
// --------------------------------------------------------------------------
void qRestResult::setError(const QString& error)
{
this->Error = error;
this->done = true;
emit ready();
}
// --------------------------------------------------------------------------
void qRestResult::waitForDone()
{
if (!done)
{
QEventLoop eventLoop;
// Time out will fire an error which will quit the event loop.
QObject::connect(this, SIGNAL(ready()),
&eventLoop, SLOT(quit()));
eventLoop.exec();
}
}
// --------------------------------------------------------------------------
void qRestResult::downloadReadyRead()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(this->sender());
ioDevice->write(reply->readAll());
}
// --------------------------------------------------------------------------
void qRestResult::downloadFinished()
{
ioDevice->close();
}
// --------------------------------------------------------------------------
void qRestResult::uploadReadyWrite()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(this->sender());
Q_ASSERT(reply);
if (!reply)
{
return;
}
reply->write(ioDevice->readAll());
}
// --------------------------------------------------------------------------
void qRestResult::uploadFinished()
{
ioDevice->close();
}
QVariantMap qRestResult::qObjectToPropertyMap(QObject* object)
{
QVariantMap propertyMap;
const QMetaObject* metaobject = object->metaObject();
int propertyCount = metaobject->propertyCount();
for (int i = 0; i < propertyCount; ++i)
{
QMetaProperty metaProperty = metaobject->property(i);
propertyMap[metaProperty.name()] = metaProperty.read(object);
}
foreach (QByteArray dynamicPropertyName, object->dynamicPropertyNames())
{
propertyMap[dynamicPropertyName] = object->property(dynamicPropertyName);
}
return propertyMap;
}