diff options
Diffstat (limited to 'tests/hawd/datasetdefinition.cpp')
-rw-r--r-- | tests/hawd/datasetdefinition.cpp | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/tests/hawd/datasetdefinition.cpp b/tests/hawd/datasetdefinition.cpp new file mode 100644 index 0000000..550c4fb --- /dev/null +++ b/tests/hawd/datasetdefinition.cpp | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include "datasetdefinition.h" | ||
21 | |||
22 | #include <QDebug> | ||
23 | #include <QFile> | ||
24 | #include <QJsonDocument> | ||
25 | #include <QJsonObject> | ||
26 | |||
27 | #include <iostream> | ||
28 | |||
29 | namespace HAWD | ||
30 | { | ||
31 | |||
32 | static QHash<QString, QMetaType::Type> s_types; | ||
33 | |||
34 | DataDefinition::DataDefinition(const QString &name, QMetaType::Type type, const QString &unit, int min, int max) | ||
35 | : m_name(name), | ||
36 | m_type(type), | ||
37 | m_unit(unit), | ||
38 | m_min(min), | ||
39 | m_max(max) | ||
40 | { | ||
41 | } | ||
42 | |||
43 | DataDefinition::DataDefinition(const QJsonObject &json) | ||
44 | : m_name(json.value("name").toString()), | ||
45 | m_type(QMetaType::Int), | ||
46 | m_unit(json.value("unit").toString()), | ||
47 | m_min(json.value("min").toInt()), | ||
48 | m_max(json.value("max").toInt()) | ||
49 | { | ||
50 | if (s_types.isEmpty()) { | ||
51 | s_types.insert("date", QMetaType::QDate); | ||
52 | s_types.insert("time", QMetaType::QTime); | ||
53 | s_types.insert("int", QMetaType::Int); | ||
54 | s_types.insert("uint", QMetaType::UInt); | ||
55 | s_types.insert("bool", QMetaType::Bool); | ||
56 | s_types.insert("char", QMetaType::QChar); | ||
57 | s_types.insert("string", QMetaType::QString); | ||
58 | s_types.insert("datetime", QMetaType::QDateTime); | ||
59 | } | ||
60 | |||
61 | const QString typeString = json.value("type").toString().toLower(); | ||
62 | if (s_types.contains(typeString)) { | ||
63 | m_type = s_types.value(typeString); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | QString DataDefinition::name() const | ||
68 | { | ||
69 | return m_name; | ||
70 | } | ||
71 | |||
72 | QString DataDefinition::typeString() const | ||
73 | { | ||
74 | return QMetaType::typeName(m_type); | ||
75 | } | ||
76 | |||
77 | QMetaType::Type DataDefinition::type() const | ||
78 | { | ||
79 | return m_type; | ||
80 | } | ||
81 | |||
82 | QString DataDefinition::unit() const | ||
83 | { | ||
84 | return m_unit; | ||
85 | } | ||
86 | |||
87 | int DataDefinition::min() const | ||
88 | { | ||
89 | return m_min; | ||
90 | } | ||
91 | |||
92 | int DataDefinition::max() const | ||
93 | { | ||
94 | return m_max; | ||
95 | } | ||
96 | |||
97 | DatasetRow::DatasetRow(const QHash<QString, DataDefinition> &columns) | ||
98 | : m_columns(columns) | ||
99 | { | ||
100 | QHashIterator<QString, DataDefinition> it(columns); | ||
101 | while (it.hasNext()) { | ||
102 | it.next(); | ||
103 | m_data.insert(it.key(), QVariant()); | ||
104 | } | ||
105 | } | ||
106 | |||
107 | void DatasetRow::setValue(const QString &column, const QVariant &value) | ||
108 | { | ||
109 | if (!m_columns.contains(column) || !value.canConvert(m_columns[column].type())) { | ||
110 | return; | ||
111 | } | ||
112 | |||
113 | m_data[column] = value; | ||
114 | } | ||
115 | |||
116 | void DatasetRow::annotate(const QString ¬e) | ||
117 | { | ||
118 | m_annotation = note; | ||
119 | } | ||
120 | |||
121 | QString DatasetRow::toString() const | ||
122 | { | ||
123 | QString string; | ||
124 | QHashIterator<QString, QVariant> it(m_data); | ||
125 | while (it.hasNext()) { | ||
126 | it.next(); | ||
127 | if (!string.isEmpty()) { | ||
128 | string.append('\t'); | ||
129 | } | ||
130 | |||
131 | string.append(it.value().toString()); | ||
132 | } | ||
133 | |||
134 | if (!m_annotation.isEmpty()) { | ||
135 | string.append('\t').append(m_annotation); | ||
136 | } | ||
137 | |||
138 | return string; | ||
139 | } | ||
140 | |||
141 | DatasetDefinition::DatasetDefinition(const QString &path) | ||
142 | : m_valid(false) | ||
143 | { | ||
144 | QFile file(path); | ||
145 | m_name = file.fileName(); | ||
146 | |||
147 | if (file.open(QIODevice::ReadOnly)) { | ||
148 | QJsonParseError error; | ||
149 | QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &error); | ||
150 | |||
151 | if (jsonDoc.isNull()) { | ||
152 | std::cerr << QObject::tr("Dataset definition file malformed at character %1: %2").arg(error.offset).arg(error.errorString()).toStdString() << std::endl; | ||
153 | } else { | ||
154 | m_valid = true; | ||
155 | QJsonObject json = jsonDoc.object(); | ||
156 | const QString name = json.value("name").toString(); | ||
157 | if (!name.isEmpty()) { | ||
158 | m_name = name; | ||
159 | } | ||
160 | |||
161 | m_description = json.value("description").toString(); | ||
162 | QJsonObject cols = json.value("columns").toObject(); | ||
163 | for (const QString &key: cols.keys()) { | ||
164 | QJsonObject def = cols.value(key).toObject(); | ||
165 | if (!def.isEmpty()) { | ||
166 | m_columns.insert(key, DataDefinition(def)); | ||
167 | } | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | |||
173 | |||
174 | bool DatasetDefinition::isValid() const | ||
175 | { | ||
176 | return m_valid; | ||
177 | } | ||
178 | |||
179 | QString DatasetDefinition::name() const | ||
180 | { | ||
181 | return m_name; | ||
182 | } | ||
183 | |||
184 | QString DatasetDefinition::description() const | ||
185 | { | ||
186 | return m_description; | ||
187 | } | ||
188 | |||
189 | QHash<QString, DataDefinition> DatasetDefinition::columns() const | ||
190 | { | ||
191 | return m_columns; | ||
192 | } | ||
193 | |||
194 | } // namespace HAWD | ||
195 | |||