From 76b5f460f9a5052571d730918b2ee778753f4c59 Mon Sep 17 00:00:00 2001 From: David Llewellyn-Jones Date: Sun, 22 Jul 2018 00:26:29 +0100 Subject: [PATCH] Support multiple graphs --- harbour-pedalo.pro | 8 +++++-- qml/pages/Stats.qml | 54 ++++++++++++++++++++++++++++++++++++++++++ src/harbour-pedalo.cpp | 17 +++++++++++++ src/stats.cpp | 23 ++++++++++++++++++ src/stats.h | 22 +++++++++++++++++ src/statsmodel.cpp | 36 ++++++++++++++++++++++++++++ src/statsmodel.h | 35 +++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 src/stats.cpp create mode 100644 src/stats.h create mode 100644 src/statsmodel.cpp create mode 100644 src/statsmodel.h diff --git a/harbour-pedalo.pro b/harbour-pedalo.pro index a6e2abe..7c12a39 100644 --- a/harbour-pedalo.pro +++ b/harbour-pedalo.pro @@ -33,7 +33,9 @@ SOURCES += src/harbour-pedalo.cpp \ src/status.cpp \ src/settings.cpp \ src/imageprovider.cpp \ - src/graph.cpp + src/graph.cpp \ + src/statsmodel.cpp \ + src/stats.cpp DISTFILES += qml/harbour-pedalo.qml \ qml/cover/CoverPage.qml \ @@ -75,4 +77,6 @@ HEADERS += \ src/settings.h \ src/harbour-pedalo.h \ src/imageprovider.h \ - src/graph.h + src/graph.h \ + src/statsmodel.h \ + src/stats.h diff --git a/qml/pages/Stats.qml b/qml/pages/Stats.qml index ad2dcae..e082b0f 100644 --- a/qml/pages/Stats.qml +++ b/qml/pages/Stats.qml @@ -72,6 +72,59 @@ Page { } } + SlideshowView { + id: graphsView + width: isPortrait ? parent.width : parent.width * 0.5 + height: (isPortrait ? statsPage.height / 2.0 : statsPage.height) - Theme.paddingLarge + itemWidth: width + clip: true + + y: (isPortrait ? (statsPage.height / 2.0) : statsColumn.y) + + model: statsmodel + delegate: Rectangle { + width: graphsView.itemWidth + height: graphsView.height + color: "transparent" + + SectionHeader { + id: sectionHeaderItem + text: "item " + index + } + + Graph { + id: graph + width: parent.width - 2 * Theme.horizontalPageMargin + anchors.top: sectionHeaderItem.bottom + height: (isPortrait ? (statsPage.height / 2.0) - Theme.paddingLarge : statsPage.height - Theme.paddingLarge - headerItem.height) - sectionHeaderItem.height + anchors.horizontalCenter: parent.horizontalCenter + model: values + labelsx: labels + //labelsy: ["0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"] + unitsy: "%" + primary: Theme.primaryColor + secondary: Theme.highlightColor + highlight: Theme.highlightColor + miny: 0.0 + maxy: 1.0 + stepy: 0.1 + gap: 0.1 + fontsize: Theme.fontSizeExtraSmall + /* + PropertyAnimation on animate { + id: animx + duration: 2000 + easing.type: Easing.InOutExpo + from: 0.0 + to: 1.0 + } + */ + } + + } + } + + /* Column { id: graphsColumn spacing: Theme.paddingLarge @@ -105,5 +158,6 @@ Page { } } } + */ } } diff --git a/src/harbour-pedalo.cpp b/src/harbour-pedalo.cpp index 729edae..abbe2be 100644 --- a/src/harbour-pedalo.cpp +++ b/src/harbour-pedalo.cpp @@ -7,6 +7,7 @@ #include "journey.h" #include "journeymodel.h" +#include "statsmodel.h" #include "status.h" #include "settings.h" #include "imageprovider.h" @@ -43,6 +44,21 @@ int main(int argc, char *argv[]) Settings::getInstance().setMainStatus(currentStatus); Settings::getInstance().loadSettings(); + StatsModel statsmodel; + Stats stats; + QList data{0.1, 0.1, 0.2}; + QStringList labels{"A", "B", "C"}; + stats.setValues(data); + stats.setLabels(labels); + statsmodel.addStats(stats); + + data = QList{0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.5}; + labels = QStringList{"M", "T", "W", "Th", "F", "S", "Su"}; + stats.setValues(data); + stats.setLabels(labels); + statsmodel.addStats(stats); + + QFile file; file.setFileName(Settings::getConfigDir() + "/journeys.csv"); journeys.importFromFile(file); @@ -61,6 +77,7 @@ int main(int argc, char *argv[]) ctxt->setContextProperty("journeymodel", &journeys); ctxt->setContextProperty("currentStatus", ¤tStatus); + ctxt->setContextProperty("statsmodel", &statsmodel); view->show(); int result = app->exec(); diff --git a/src/stats.cpp b/src/stats.cpp new file mode 100644 index 0000000..c858ccb --- /dev/null +++ b/src/stats.cpp @@ -0,0 +1,23 @@ +#include "stats.h" + +Stats::Stats() +{ + +} + +QStringList Stats::getLabels() const { + return labels; +} + +QList Stats::getValues() const { + return values; +} + +void Stats::setLabels(QStringList &value) { + labels = value; +} + +void Stats::setValues(QList &value) { + values = value; +} + diff --git a/src/stats.h b/src/stats.h new file mode 100644 index 0000000..12fb41e --- /dev/null +++ b/src/stats.h @@ -0,0 +1,22 @@ +#ifndef STATS_H +#define STATS_H + +#include + +class Stats +{ +public: + Stats(); + + QStringList getLabels() const; + QList getValues() const; + + void setLabels(QStringList &value); + void setValues(QList &value); + +private: + QStringList labels; + QList values; +}; + +#endif // STATS_H diff --git a/src/statsmodel.cpp b/src/statsmodel.cpp new file mode 100644 index 0000000..52090c4 --- /dev/null +++ b/src/statsmodel.cpp @@ -0,0 +1,36 @@ +#include "statsmodel.h" + +StatsModel::StatsModel(QObject *parent) : QAbstractListModel(parent) { + roles[ValuesRole] = "values"; + roles[LabelsRole] = "labels"; +} + +void StatsModel::addStats(const Stats &stats) { + this->stats.append(stats); +} + +QHash StatsModel::roleNames() const { + return roles; +} + +int StatsModel::rowCount(const QModelIndex & parent) const { + Q_UNUSED(parent) + return stats.count(); +} + +QVariant StatsModel::data(const QModelIndex & index, int role) const { + if (index.row() < 0 || index.row() > stats.count()) + return QVariant(); + + const Stats &stat = stats[index.row()]; + if (role == ValuesRole) + return QVariant::fromValue>(stat.getValues()); + else if (role == LabelsRole) + return stat.getLabels(); + return QVariant(); +} + +void StatsModel::clear() { + stats.clear(); +} + diff --git a/src/statsmodel.h b/src/statsmodel.h new file mode 100644 index 0000000..2c9991a --- /dev/null +++ b/src/statsmodel.h @@ -0,0 +1,35 @@ +#ifndef STATSMODEL_H +#define STATSMODEL_H + +#include +#include + +#include "stats.h" + +class StatsModel : public QAbstractListModel +{ + Q_OBJECT +public: + enum StatsRoles { + ValuesRole = Qt::UserRole + 1, + LabelsRole + }; + + QHash roleNames() const; + + StatsModel(QObject *parent = 0); + + void addStats(const Stats &stats); + + int rowCount(const QModelIndex & parent = QModelIndex()) const; + + QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; + + void clear(); + +private: + QHash roles; + QList stats; +}; + +#endif // STATSMODEL_H -- 2.25.1