Commit 1f1f1644 authored by Volker Krause's avatar Volker Krause
Browse files

Read and display ratio set data in the analyzer.

parent f7242b47
......@@ -9,6 +9,7 @@ set(analyzer_srcs
product.cpp
productmodel.cpp
productschemaentry.cpp
ratioset.cpp
restclient.cpp
sample.cpp
schemamodel.cpp
......
......@@ -17,6 +17,7 @@
#include "datamodel.h"
#include "ratioset.h"
#include "restclient.h"
#include "sample.h"
......@@ -93,6 +94,8 @@ QVariant DataModel::data(const QModelIndex &index, int role) const
const auto v = sample.value(m_product.schema().at(index.column() - 1).name());
if (v.type() == QVariant::StringList)
return v.toStringList().join(QStringLiteral(", "));
if (v.userType() == qMetaTypeId<RatioSet>())
return v.value<RatioSet>().displayString();
return v;
} else if (role == SampleRole) {
return QVariant::fromValue(m_data.at(index.row()));
......
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ratioset.h"
#include <QDebug>
#include <QHash>
#include <QJsonObject>
#include <QSharedData>
#include <QString>
#include <QVector>
#include <algorithm>
using namespace UserFeedback::Analyzer;
namespace UserFeedback {
namespace Analyzer {
class RatioSetData : public QSharedData
{
public:
QHash<QString, double> ratioSet;
};
}
}
RatioSet::RatioSet() : d(new RatioSetData)
{
}
RatioSet::RatioSet(const Analyzer::RatioSet&) = default;
RatioSet::~RatioSet() = default;
RatioSet& RatioSet::operator=(const RatioSet&) = default;
QString RatioSet::displayString() const
{
QVector<QPair<QString, double>> data;
data.reserve(d->ratioSet.size());
for (auto it = d->ratioSet.constBegin(); it != d->ratioSet.constEnd(); ++it)
data.push_back(qMakePair(it.key(), it.value()));
std::sort(data.begin(), data.end(), [](const QPair<QString, double> &lhs, const QPair<QString, double> &rhs) {
return rhs.second < lhs.second;
});
QStringList l;
l.reserve(data.size());
for (auto it = data.constBegin(); it != data.constEnd(); ++it)
l.push_back((*it).first + QStringLiteral(": ") + QString::number((*it).second * 100.0, 'f', 1) + QLatin1Char('%'));
return l.join(QStringLiteral(", "));
}
RatioSet RatioSet::fromJson(const QJsonObject& obj)
{
RatioSet set;
for (auto it = obj.begin(); it != obj.end(); ++it) {
set.d->ratioSet.insert(it.key(), it.value().toDouble());
}
return set;
}
/*
Copyright (C) 2016 Volker Krause <vkrause@kde.org>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef USERFEEDBACK_ANALYZER_RATIOSET_H
#define USERFEEDBACK_ANALYZER_RATIOSET_H
#include <QSharedDataPointer>
#include <QMetaType>
class QJsonObject;
namespace UserFeedback {
namespace Analyzer {
class RatioSetData;
class RatioSet
{
public:
RatioSet();
RatioSet(const RatioSet&);
~RatioSet();
RatioSet& operator=(const RatioSet&);
QString displayString() const;
static RatioSet fromJson(const QJsonObject &obj);
private:
QSharedDataPointer<RatioSetData> d;
};
}
}
Q_DECLARE_METATYPE(UserFeedback::Analyzer::RatioSet)
Q_DECLARE_TYPEINFO(UserFeedback::Analyzer::RatioSet, Q_MOVABLE_TYPE);
#endif // USERFEEDBACK_ANALYZER_RATIOSET_H
......@@ -18,6 +18,7 @@
#include "sample.h"
#include "product.h"
#include "productschemaentry.h"
#include "ratioset.h"
#include <QDateTime>
#include <QDebug>
......@@ -83,6 +84,12 @@ QVector<Sample> Sample::fromJson(const QByteArray &json, const Product &product)
s.d->data.insert(entry.name(), l);
break;
}
case ProductSchemaEntry::RatioSetType:
{
const auto set = RatioSet::fromJson(obj.value(entry.name()).toObject());
s.d->data.insert(entry.name(), QVariant::fromValue(set));
break;
}
}
}
samples.push_back(s);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment