/
RideCache.h
160 lines (121 loc) · 4.52 KB
/
RideCache.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2014 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _GC_RideCache_h
#define _GC_RideCache_h 1
#include "GoldenCheetah.h"
#include "MainWindow.h"
#include "RideFile.h"
#include "RideItem.h"
#include "PDModel.h"
#include <QVector>
#include <QThread>
#include <QFuture>
#include <QFutureWatcher>
#if QT_VERSION > 0x050000
# include <QtConcurrent>
#else
# include <QtConcurrentRun>
#endif
class Context;
class RideCacheBackgroundRefresh;
class Specification;
class AthleteBest;
class RideCacheModel;
class RideCache : public QObject
{
Q_OBJECT
public:
RideCache(Context *context);
~RideCache();
// table models
RideCacheModel *model() { return model_; }
// query the cache
int count() const { return rides_.count(); }
RideItem *getRide(QString filename);
RideItem *getRide(QDateTime dateTime);
QList<QDateTime> getAllDates();
QStringList getAllFilenames();
// get an aggregate applying the passed spec
QString getAggregate(QString name, Specification spec, bool useMetricUnits, bool nofmt=false);
// get top n bests
QList<AthleteBest> getBests(QString symbol, int n, Specification specification, bool useMetricUnits=true);
// metadata
QHash<QString,int> getRankedValues(QString name); // metadata
QStringList getDistinctValues(QString name); // metadata
// Count of activities matching specification
void getRideTypeCounts(Specification specification, int& nActivities,
int& nRides, int& nRuns, int& nSwims);
// Check if metric is relevant for some activity matching specification
enum SportRestriction { AnySport, OnlyRides, OnlyRuns, OnlySwims };
bool isMetricRelevantForRides(Specification specification,
const RideMetric* metric,
SportRestriction sport=AnySport);
// is running ?
bool isRunning() { return future.isRunning(); }
// the ride list
QVector<RideItem*>&rides() { return rides_; }
// add/remove a ride to the list
void addRide(QString name, bool dosignal, bool useTempActivities, bool planned);
void removeCurrentRide();
// export metrics in CSV format
void writeAsCSV(QString filename);
// the background refresher !
void refresh();
double progress() { return progress_; }
// PD Model refreshing (temporary move)
void refreshCPModelMetrics();
public slots:
// restore / dump cache to disk (json)
void load();
void save();
// user updated options/preferences
void configChanged(qint32);
// background refresh progress update
void progressing(int);
// cancel background processing because about to exit
void cancel();
// item telling us it changed
void itemChanged();
// clear deleted objects
void garbageCollect();
signals:
void modelProgress(int, int); // let others know when we're refreshing the model estimates
// us telling the world the item changed
void itemChanged(RideItem*);
protected:
friend class ::RideCacheBackgroundRefresh;
Context *context;
QDir directory, plannedDirectory;
QVector<RideItem*> rides_, reverse_, delete_;
RideCacheModel *model_;
bool exiting;
bool refreshingEstimates;
double progress_; // percent
QFuture<void> future;
QFutureWatcher<void> watcher;
};
class AthleteBest
{
public:
double nvalue;
QString value; // formatted value
QDate date;
// for qsort
bool operator< (AthleteBest right) const { return (nvalue < right.nvalue); }
};
#endif // _GC_RideCache_h