File indexing completed on 2024-09-15 04:47:08

0001 /*
0002  * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #include "datasetdefinition.h"
0022 
0023 #include <QDateTime>
0024 #include <QDebug>
0025 #include <QFile>
0026 #include <QJsonDocument>
0027 #include <QJsonObject>
0028 #include <QJsonArray>
0029 
0030 #include <iostream>
0031 
0032 namespace HAWD
0033 {
0034 
0035 static QHash<QString, QMetaType::Type> s_types;
0036 
0037 DataDefinition::DataDefinition(const QString &name, QMetaType::Type type, const QString &unit, int min, int max)
0038     : m_name(name),
0039       m_type(type),
0040       m_unit(unit),
0041       m_min(min),
0042       m_max(max)
0043 {
0044 }
0045 
0046 DataDefinition::DataDefinition(const QJsonObject &json)
0047     : m_name(json.value("name").toString()),
0048       m_type(QMetaType::Int),
0049       m_unit(json.value("unit").toString()),
0050       m_min(json.value("min").toInt()),
0051       m_max(json.value("max").toInt())
0052 {
0053     if (s_types.isEmpty()) {
0054         s_types.insert("date", QMetaType::QDate);
0055         s_types.insert("time", QMetaType::QTime);
0056         s_types.insert("int", QMetaType::Int);
0057         s_types.insert("uint", QMetaType::UInt);
0058         s_types.insert("bool", QMetaType::Bool);
0059         s_types.insert("float", QMetaType::Float);
0060         s_types.insert("double", QMetaType::Double);
0061         s_types.insert("char", QMetaType::QChar);
0062         s_types.insert("string", QMetaType::QString);
0063         s_types.insert("datetime", QMetaType::QDateTime);
0064     }
0065 
0066     const QString typeString = json.value("type").toString().toLower();
0067     if (s_types.contains(typeString)) {
0068         m_type = s_types.value(typeString);
0069     }
0070 }
0071 
0072 QString DataDefinition::name() const
0073 {
0074     return m_name;
0075 }
0076 
0077 QString DataDefinition::typeString() const
0078 {
0079     return QMetaType::typeName(m_type);
0080 }
0081 
0082 QMetaType::Type DataDefinition::type() const
0083 {
0084     return m_type;
0085 }
0086 
0087 QString DataDefinition::unit() const
0088 {
0089     return m_unit;
0090 }
0091 
0092 int DataDefinition::min() const
0093 {
0094     return m_min;
0095 }
0096 
0097 int DataDefinition::max() const
0098 {
0099     return m_max;
0100 }
0101 
0102 DatasetDefinition::DatasetDefinition(const QString &path)
0103     : m_valid(false)
0104 {
0105     QFile file(path);
0106     m_name = file.fileName();
0107 
0108     if (file.open(QIODevice::ReadOnly)) {
0109         QJsonParseError error;
0110         QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &error);
0111 
0112         if (jsonDoc.isNull()) {
0113             m_lastError = QObject::tr("Dataset definition file malformed at character %1: %2").arg(error.offset).arg(error.errorString());
0114         } else {
0115             m_valid = true;
0116             QJsonObject json = jsonDoc.object();
0117             const QString name = json.value("name").toString();
0118             if (!name.isEmpty()) {
0119                 m_name = name;
0120             }
0121 
0122             m_description = json.value("description").toString();
0123             auto cols = json.value("columns").toArray();
0124             for (QJsonValueRef entry: cols) {
0125                 QJsonObject def = entry.toObject();
0126                 if (!def.isEmpty()) {
0127                     m_columns << qMakePair(def.value("name").toString(), DataDefinition(def));
0128                 }
0129             }
0130         }
0131     } else {
0132         m_lastError = QObject::tr("Could not open file for parsing: ").arg(path);
0133     }
0134 }
0135 
0136 bool DatasetDefinition::isValid() const
0137 {
0138     return m_valid;
0139 }
0140 
0141 QString DatasetDefinition::lastError() const
0142 {
0143     return m_lastError;
0144 }
0145 
0146 QString DatasetDefinition::name() const
0147 {
0148     return m_name;
0149 }
0150 
0151 QString DatasetDefinition::description() const
0152 {
0153     return m_description;
0154 }
0155 
0156 QList<QPair<QString, DataDefinition> > DatasetDefinition::columns() const
0157 {
0158     return m_columns;
0159 }
0160 
0161 } // namespace HAWD
0162