Support multiple graphs
authorDavid Llewellyn-Jones <david@flypig.co.uk>
Sat, 21 Jul 2018 23:26:29 +0000 (00:26 +0100)
committerDavid Llewellyn-Jones <david@flypig.co.uk>
Sat, 21 Jul 2018 23:26:29 +0000 (00:26 +0100)
harbour-pedalo.pro
qml/pages/Stats.qml
src/harbour-pedalo.cpp
src/stats.cpp [new file with mode: 0644]
src/stats.h [new file with mode: 0644]
src/statsmodel.cpp [new file with mode: 0644]
src/statsmodel.h [new file with mode: 0644]

index a6e2abe..7c12a39 100644 (file)
@@ -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
index ad2dcae..e082b0f 100644 (file)
@@ -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 {
                 }
             }
         }
+        */
     }
 }
index 729edae..abbe2be 100644 (file)
@@ -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<float> data{0.1, 0.1, 0.2};
+    QStringList labels{"A", "B", "C"};
+    stats.setValues(data);
+    stats.setLabels(labels);
+    statsmodel.addStats(stats);
+
+    data = QList<float>{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", &currentStatus);
+    ctxt->setContextProperty("statsmodel", &statsmodel);
 
     view->show();
     int result = app->exec();
diff --git a/src/stats.cpp b/src/stats.cpp
new file mode 100644 (file)
index 0000000..c858ccb
--- /dev/null
@@ -0,0 +1,23 @@
+#include "stats.h"
+
+Stats::Stats()
+{
+
+}
+
+QStringList Stats::getLabels() const {
+    return labels;
+}
+
+QList<float> Stats::getValues() const {
+    return values;
+}
+
+void Stats::setLabels(QStringList &value) {
+    labels = value;
+}
+
+void Stats::setValues(QList<float> &value) {
+    values = value;
+}
+
diff --git a/src/stats.h b/src/stats.h
new file mode 100644 (file)
index 0000000..12fb41e
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef STATS_H
+#define STATS_H
+
+#include <QStringList>
+
+class Stats
+{
+public:
+    Stats();
+
+    QStringList getLabels() const;
+    QList<float> getValues() const;
+
+    void setLabels(QStringList &value);
+    void setValues(QList<float> &value);
+
+private:
+    QStringList labels;
+    QList<float> values;
+};
+
+#endif // STATS_H
diff --git a/src/statsmodel.cpp b/src/statsmodel.cpp
new file mode 100644 (file)
index 0000000..52090c4
--- /dev/null
@@ -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<int, QByteArray> 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<QList<float>>(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 (file)
index 0000000..2c9991a
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef STATSMODEL_H
+#define STATSMODEL_H
+
+#include <QAbstractListModel>
+#include <QStringList>
+
+#include "stats.h"
+
+class StatsModel : public QAbstractListModel
+{
+    Q_OBJECT
+public:
+    enum StatsRoles {
+        ValuesRole = Qt::UserRole + 1,
+        LabelsRole
+    };
+
+    QHash<int, QByteArray> 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<int, QByteArray> roles;
+    QList<Stats> stats;
+};
+
+#endif // STATSMODEL_H