@@ -0,0 +1,33 | |||
|
1 | #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H | |
|
2 | #define SCIQLOP_VARIABLECACHECONTROLLER_H | |
|
3 | ||
|
4 | #include <QObject> | |
|
5 | ||
|
6 | #include <Data/SqpDateTime.h> | |
|
7 | ||
|
8 | #include <Common/spimpl.h> | |
|
9 | ||
|
10 | class Variable; | |
|
11 | ||
|
12 | /// This class aims to store in the cash all of the dateTime already requested to the variable. | |
|
13 | class VariableCacheController : public QObject { | |
|
14 | Q_OBJECT | |
|
15 | public: | |
|
16 | explicit VariableCacheController(QObject *parent = 0); | |
|
17 | ||
|
18 | ||
|
19 | void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); | |
|
20 | ||
|
21 | /// Return all of the SqpDataTime part of the dateTime whose are not in the cache | |
|
22 | QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, | |
|
23 | const SqpDateTime &dateTime); | |
|
24 | ||
|
25 | ||
|
26 | QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept; | |
|
27 | ||
|
28 | private: | |
|
29 | class VariableCacheControllerPrivate; | |
|
30 | spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl; | |
|
31 | }; | |
|
32 | ||
|
33 | #endif // SCIQLOP_VARIABLECACHECONTROLLER_H |
@@ -0,0 +1,173 | |||
|
1 | #include "Variable/VariableCacheController.h" | |
|
2 | ||
|
3 | #include "Variable/Variable.h" | |
|
4 | #include <unordered_map> | |
|
5 | ||
|
6 | struct VariableCacheController::VariableCacheControllerPrivate { | |
|
7 | ||
|
8 | std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> > | |
|
9 | m_VariableToSqpDateTimeListMap; | |
|
10 | ||
|
11 | void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
|
12 | QVector<SqpDateTime> ¬InCache, int cacheIndex, | |
|
13 | double currentTStart); | |
|
14 | ||
|
15 | void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
|
16 | QVector<SqpDateTime> ¬InCache, int cacheIndex, | |
|
17 | double currentTStart); | |
|
18 | ||
|
19 | ||
|
20 | void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
|
21 | int cacheIndex); | |
|
22 | }; | |
|
23 | ||
|
24 | ||
|
25 | VariableCacheController::VariableCacheController(QObject *parent) | |
|
26 | : QObject(parent), impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()} | |
|
27 | { | |
|
28 | } | |
|
29 | ||
|
30 | void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable, | |
|
31 | const SqpDateTime &dateTime) | |
|
32 | { | |
|
33 | if (variable) { | |
|
34 | auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable); | |
|
35 | if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) { | |
|
36 | impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime); | |
|
37 | } | |
|
38 | else { | |
|
39 | ||
|
40 | // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure | |
|
41 | // that the list is ordered : l(0) < l(1). We assume also a < b | |
|
42 | // (with a & b of type SqpDateTime) means ts(b) > te(a) | |
|
43 | ||
|
44 | // The algorithm will try the merge of two interval: | |
|
45 | // - dateTime will be compare with the first interval of the list: | |
|
46 | // A: if it is inferior, it will be inserted and it's finished. | |
|
47 | // B: if it is in intersection, it will be merge then the merged one | |
|
48 | // will be compared to the next interval. The old one is remove from the list | |
|
49 | // C: if it is superior, we do the same with the next interval of the list | |
|
50 | ||
|
51 | int cacheIndex = 0; | |
|
52 | impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable), | |
|
53 | cacheIndex); | |
|
54 | } | |
|
55 | } | |
|
56 | } | |
|
57 | ||
|
58 | QVector<SqpDateTime> | |
|
59 | VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, | |
|
60 | const SqpDateTime &dateTime) | |
|
61 | { | |
|
62 | auto notInCache = QVector<SqpDateTime>{}; | |
|
63 | ||
|
64 | // This algorithm is recursif. The idea is to localise the start time then the end time in the | |
|
65 | // list of date time request associated to the variable | |
|
66 | // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b | |
|
67 | // (with a & b of type SqpDateTime) means ts(b) > te(a) | |
|
68 | ||
|
69 | impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable), | |
|
70 | notInCache, 0, dateTime.m_TStart); | |
|
71 | ||
|
72 | return notInCache; | |
|
73 | } | |
|
74 | ||
|
75 | QVector<SqpDateTime> | |
|
76 | VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept | |
|
77 | { | |
|
78 | return impl->m_VariableToSqpDateTimeListMap.at(variable); | |
|
79 | } | |
|
80 | ||
|
81 | void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse( | |
|
82 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex) | |
|
83 | { | |
|
84 | const auto dateTimeListSize = dateTimeList.count(); | |
|
85 | if (cacheIndex >= dateTimeListSize) { | |
|
86 | dateTimeList.push_back(dateTime); | |
|
87 | // there is no anymore interval to compore, we can just push_back it | |
|
88 | return; | |
|
89 | } | |
|
90 | ||
|
91 | auto currentDateTime = dateTimeList[cacheIndex]; | |
|
92 | ||
|
93 | if (dateTime.m_TEnd < currentDateTime.m_TStart) { | |
|
94 | // The compared one is < to current one compared, we can insert it | |
|
95 | dateTimeList.insert(cacheIndex, dateTime); | |
|
96 | } | |
|
97 | ||
|
98 | else if (dateTime.m_TStart > currentDateTime.m_TEnd) { | |
|
99 | // The compared one is > to current one compared we can comparet if to the next one | |
|
100 | addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex); | |
|
101 | } | |
|
102 | else { | |
|
103 | // Merge cases: we need to merge the two interval, remove the old one from the list then | |
|
104 | // rerun the algo from this index with the merged interval | |
|
105 | auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart); | |
|
106 | auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd); | |
|
107 | auto mergeDateTime = SqpDateTime{mTStart, mTEnd}; | |
|
108 | ||
|
109 | dateTimeList.remove(cacheIndex); | |
|
110 | addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex); | |
|
111 | } | |
|
112 | } | |
|
113 | ||
|
114 | ||
|
115 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd( | |
|
116 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
|
117 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) | |
|
118 | { | |
|
119 | const auto dateTimeListSize = dateTimeList.count(); | |
|
120 | if (cacheIndex >= dateTimeListSize) { | |
|
121 | if (currentTStart < dateTime.m_TEnd) { | |
|
122 | ||
|
123 | // te localised after all other interval: The last interval is [currentTsart, te] | |
|
124 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); | |
|
125 | } | |
|
126 | return; | |
|
127 | } | |
|
128 | ||
|
129 | auto currentDateTimeJ = dateTimeList[cacheIndex]; | |
|
130 | if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) { | |
|
131 | // te localised between to interval: The last interval is [currentTsart, te] | |
|
132 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); | |
|
133 | } | |
|
134 | else { | |
|
135 | notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart}); | |
|
136 | if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) { | |
|
137 | // te not localised before the current interval: we need to look at the next interval | |
|
138 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex, | |
|
139 | currentDateTimeJ.m_TEnd); | |
|
140 | } | |
|
141 | } | |
|
142 | } | |
|
143 | ||
|
144 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart( | |
|
145 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
|
146 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) | |
|
147 | { | |
|
148 | const auto dateTimeListSize = dateTimeList.count(); | |
|
149 | if (cacheIndex >= dateTimeListSize) { | |
|
150 | // ts localised after all other interval: The last interval is [ts, te] | |
|
151 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); | |
|
152 | return; | |
|
153 | } | |
|
154 | ||
|
155 | auto currentDateTimeI = dateTimeList[cacheIndex]; | |
|
156 | auto cacheIndexJ = cacheIndex; | |
|
157 | if (currentTStart < currentDateTimeI.m_TStart) { | |
|
158 | ||
|
159 | // ts localised between to interval: let's localized te | |
|
160 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndexJ, currentTStart); | |
|
161 | } | |
|
162 | else if (dateTime.m_TStart < currentDateTimeI.m_TEnd) { | |
|
163 | // ts not localised before the current interval: we need to look at the next interval | |
|
164 | // We can assume now current tstart is the last interval tend, because data between them are | |
|
165 | // in the cache | |
|
166 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, | |
|
167 | currentDateTimeI.m_TEnd); | |
|
168 | } | |
|
169 | else { | |
|
170 | // ts not localised before the current interval: we need to look at the next interval | |
|
171 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart); | |
|
172 | } | |
|
173 | } |
@@ -0,0 +1,343 | |||
|
1 | #include <Variable/Variable.h> | |
|
2 | #include <Variable/VariableCacheController.h> | |
|
3 | ||
|
4 | #include <QObject> | |
|
5 | #include <QtTest> | |
|
6 | ||
|
7 | #include <memory> | |
|
8 | ||
|
9 | class TestVariableCacheController : public QObject { | |
|
10 | Q_OBJECT | |
|
11 | ||
|
12 | private slots: | |
|
13 | void testProvideNotInCacheDateTimeList(); | |
|
14 | ||
|
15 | void testAddDateTime(); | |
|
16 | }; | |
|
17 | ||
|
18 | ||
|
19 | void TestVariableCacheController::testProvideNotInCacheDateTimeList() | |
|
20 | { | |
|
21 | VariableCacheController variableCacheController{}; | |
|
22 | ||
|
23 | auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}}; | |
|
24 | auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; | |
|
25 | auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()), | |
|
26 | static_cast<double>(te0.toMSecsSinceEpoch())}; | |
|
27 | ||
|
28 | auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}}; | |
|
29 | auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}}; | |
|
30 | auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()), | |
|
31 | static_cast<double>(te1.toMSecsSinceEpoch())}; | |
|
32 | ||
|
33 | auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}}; | |
|
34 | auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}}; | |
|
35 | auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()), | |
|
36 | static_cast<double>(te2.toMSecsSinceEpoch())}; | |
|
37 | ||
|
38 | auto var0 = std::make_shared<Variable>("", "", "", sqp0); | |
|
39 | ||
|
40 | variableCacheController.addDateTime(var0, sqp0); | |
|
41 | variableCacheController.addDateTime(var0, sqp1); | |
|
42 | variableCacheController.addDateTime(var0, sqp2); | |
|
43 | ||
|
44 | // first case [ts,te] < ts0 | |
|
45 | auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; | |
|
46 | auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}}; | |
|
47 | auto sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
48 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
49 | ||
|
50 | ||
|
51 | auto notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
52 | ||
|
53 | QCOMPARE(notInCach.size(), 1); | |
|
54 | auto notInCashSqp = notInCach.first(); | |
|
55 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
56 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
57 | ||
|
58 | ||
|
59 | // second case ts < ts0 && ts0 < te <= te0 | |
|
60 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; | |
|
61 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; | |
|
62 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
63 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
64 | ||
|
65 | ||
|
66 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
67 | ||
|
68 | QCOMPARE(notInCach.size(), 1); | |
|
69 | notInCashSqp = notInCach.first(); | |
|
70 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
71 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
72 | ||
|
73 | // 3th case ts < ts0 && te0 < te <= ts1 | |
|
74 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; | |
|
75 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; | |
|
76 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
77 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
78 | ||
|
79 | ||
|
80 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
81 | ||
|
82 | QCOMPARE(notInCach.size(), 2); | |
|
83 | notInCashSqp = notInCach.first(); | |
|
84 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
85 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
86 | ||
|
87 | notInCashSqp = notInCach.at(1); | |
|
88 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
89 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
90 | ||
|
91 | // 4th case ts < ts0 && ts1 < te <= te1 | |
|
92 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; | |
|
93 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}}; | |
|
94 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
95 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
96 | ||
|
97 | ||
|
98 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
99 | ||
|
100 | QCOMPARE(notInCach.size(), 2); | |
|
101 | notInCashSqp = notInCach.first(); | |
|
102 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
103 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
104 | ||
|
105 | notInCashSqp = notInCach.at(1); | |
|
106 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
107 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch())); | |
|
108 | ||
|
109 | // 5th case ts < ts0 && te3 < te | |
|
110 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}}; | |
|
111 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}}; | |
|
112 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
113 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
114 | ||
|
115 | ||
|
116 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
117 | ||
|
118 | QCOMPARE(notInCach.size(), 4); | |
|
119 | notInCashSqp = notInCach.first(); | |
|
120 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
121 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
122 | ||
|
123 | notInCashSqp = notInCach.at(1); | |
|
124 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
125 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch())); | |
|
126 | ||
|
127 | notInCashSqp = notInCach.at(2); | |
|
128 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch())); | |
|
129 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch())); | |
|
130 | ||
|
131 | notInCashSqp = notInCach.at(3); | |
|
132 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch())); | |
|
133 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
134 | ||
|
135 | ||
|
136 | // 6th case ts2 < ts | |
|
137 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 45, 0, 0}}; | |
|
138 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}}; | |
|
139 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
140 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
141 | ||
|
142 | ||
|
143 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
144 | ||
|
145 | QCOMPARE(notInCach.size(), 1); | |
|
146 | notInCashSqp = notInCach.first(); | |
|
147 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
148 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
149 | ||
|
150 | // 7th case ts = te0 && te < ts1 | |
|
151 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; | |
|
152 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; | |
|
153 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
154 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
155 | ||
|
156 | ||
|
157 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
158 | ||
|
159 | QCOMPARE(notInCach.size(), 1); | |
|
160 | notInCashSqp = notInCach.first(); | |
|
161 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
162 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
163 | ||
|
164 | // 8th case ts0 < ts < te0 && te < ts1 | |
|
165 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; | |
|
166 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; | |
|
167 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
168 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
169 | ||
|
170 | ||
|
171 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
172 | ||
|
173 | QCOMPARE(notInCach.size(), 1); | |
|
174 | notInCashSqp = notInCach.first(); | |
|
175 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
176 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
177 | ||
|
178 | // 9th case ts0 < ts < te0 && ts1 < te < te1 | |
|
179 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}}; | |
|
180 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}}; | |
|
181 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
182 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
183 | ||
|
184 | ||
|
185 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
186 | ||
|
187 | QCOMPARE(notInCach.size(), 1); | |
|
188 | notInCashSqp = notInCach.first(); | |
|
189 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
190 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch())); | |
|
191 | ||
|
192 | // 10th case te1 < ts < te < ts2 | |
|
193 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 9, 0, 0}}; | |
|
194 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}}; | |
|
195 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
196 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
197 | ||
|
198 | ||
|
199 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
200 | ||
|
201 | QCOMPARE(notInCach.size(), 1); | |
|
202 | notInCashSqp = notInCach.first(); | |
|
203 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
204 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
205 | ||
|
206 | // 11th case te0 < ts < ts1 && te3 < te | |
|
207 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; | |
|
208 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}}; | |
|
209 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
210 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
211 | ||
|
212 | ||
|
213 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
214 | ||
|
215 | QCOMPARE(notInCach.size(), 3); | |
|
216 | notInCashSqp = notInCach.first(); | |
|
217 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
218 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch())); | |
|
219 | ||
|
220 | notInCashSqp = notInCach.at(1); | |
|
221 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch())); | |
|
222 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch())); | |
|
223 | ||
|
224 | notInCashSqp = notInCach.at(2); | |
|
225 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch())); | |
|
226 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
227 | ||
|
228 | // 12th case te0 < ts < ts1 && te3 < te | |
|
229 | ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}}; | |
|
230 | te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}}; | |
|
231 | sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()), | |
|
232 | static_cast<double>(te.toMSecsSinceEpoch())}; | |
|
233 | ||
|
234 | notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp); | |
|
235 | ||
|
236 | QCOMPARE(notInCach.size(), 2); | |
|
237 | notInCashSqp = notInCach.first(); | |
|
238 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch())); | |
|
239 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch())); | |
|
240 | ||
|
241 | notInCashSqp = notInCach.at(1); | |
|
242 | QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch())); | |
|
243 | QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch())); | |
|
244 | } | |
|
245 | ||
|
246 | ||
|
247 | void TestVariableCacheController::testAddDateTime() | |
|
248 | { | |
|
249 | VariableCacheController variableCacheController{}; | |
|
250 | ||
|
251 | auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}}; | |
|
252 | auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; | |
|
253 | auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()), | |
|
254 | static_cast<double>(te0.toMSecsSinceEpoch())}; | |
|
255 | ||
|
256 | auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}}; | |
|
257 | auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}}; | |
|
258 | auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()), | |
|
259 | static_cast<double>(te1.toMSecsSinceEpoch())}; | |
|
260 | ||
|
261 | auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}}; | |
|
262 | auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}}; | |
|
263 | auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()), | |
|
264 | static_cast<double>(te2.toMSecsSinceEpoch())}; | |
|
265 | ||
|
266 | auto ts01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; | |
|
267 | auto te01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}}; | |
|
268 | auto sqp01 = SqpDateTime{static_cast<double>(ts01.toMSecsSinceEpoch()), | |
|
269 | static_cast<double>(te01.toMSecsSinceEpoch())}; | |
|
270 | ||
|
271 | auto ts3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 14, 0, 0}}; | |
|
272 | auto te3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 16, 0, 0}}; | |
|
273 | auto sqp3 = SqpDateTime{static_cast<double>(ts3.toMSecsSinceEpoch()), | |
|
274 | static_cast<double>(te3.toMSecsSinceEpoch())}; | |
|
275 | ||
|
276 | auto ts03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}}; | |
|
277 | auto te03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}}; | |
|
278 | auto sqp03 = SqpDateTime{static_cast<double>(ts03.toMSecsSinceEpoch()), | |
|
279 | static_cast<double>(te03.toMSecsSinceEpoch())}; | |
|
280 | ||
|
281 | ||
|
282 | auto var0 = std::make_shared<Variable>("", "", "", sqp0); | |
|
283 | ||
|
284 | ||
|
285 | // First case: add the first interval to the variable :sqp0 | |
|
286 | variableCacheController.addDateTime(var0, sqp0); | |
|
287 | auto dateCacheList = variableCacheController.dateCacheList(var0); | |
|
288 | QCOMPARE(dateCacheList.count(), 1); | |
|
289 | auto dateCache = dateCacheList.at(0); | |
|
290 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
291 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
292 | ||
|
293 | // 2nd case: add a second interval : sqp1 > sqp0 | |
|
294 | variableCacheController.addDateTime(var0, sqp1); | |
|
295 | dateCacheList = variableCacheController.dateCacheList(var0); | |
|
296 | QCOMPARE(dateCacheList.count(), 2); | |
|
297 | dateCache = dateCacheList.at(0); | |
|
298 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
299 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch())); | |
|
300 | ||
|
301 | dateCache = dateCacheList.at(1); | |
|
302 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts1.toMSecsSinceEpoch())); | |
|
303 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch())); | |
|
304 | ||
|
305 | // 3th case: merge sqp0 & sqp1 with sqp01 | |
|
306 | variableCacheController.addDateTime(var0, sqp01); | |
|
307 | dateCacheList = variableCacheController.dateCacheList(var0); | |
|
308 | QCOMPARE(dateCacheList.count(), 1); | |
|
309 | dateCache = dateCacheList.at(0); | |
|
310 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
311 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch())); | |
|
312 | ||
|
313 | ||
|
314 | // 4th case: add a second interval : sqp1 > sqp0 | |
|
315 | variableCacheController.addDateTime(var0, sqp2); | |
|
316 | variableCacheController.addDateTime(var0, sqp3); | |
|
317 | dateCacheList = variableCacheController.dateCacheList(var0); | |
|
318 | QCOMPARE(dateCacheList.count(), 3); | |
|
319 | dateCache = dateCacheList.at(0); | |
|
320 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
321 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch())); | |
|
322 | ||
|
323 | dateCache = dateCacheList.at(1); | |
|
324 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts3.toMSecsSinceEpoch())); | |
|
325 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te3.toMSecsSinceEpoch())); | |
|
326 | ||
|
327 | dateCache = dateCacheList.at(2); | |
|
328 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts2.toMSecsSinceEpoch())); | |
|
329 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te2.toMSecsSinceEpoch())); | |
|
330 | ||
|
331 | ||
|
332 | // 5th case: merge all interval | |
|
333 | variableCacheController.addDateTime(var0, sqp03); | |
|
334 | dateCacheList = variableCacheController.dateCacheList(var0); | |
|
335 | QCOMPARE(dateCacheList.count(), 1); | |
|
336 | dateCache = dateCacheList.at(0); | |
|
337 | QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch())); | |
|
338 | QCOMPARE(dateCache.m_TEnd, static_cast<double>(te03.toMSecsSinceEpoch())); | |
|
339 | } | |
|
340 | ||
|
341 | ||
|
342 | QTEST_MAIN(TestVariableCacheController) | |
|
343 | #include "TestVariableCacheController.moc" |
@@ -1,56 +1,82 | |||
|
1 | 1 | #ifndef SCIQLOP_ARRAYDATA_H |
|
2 | 2 | #define SCIQLOP_ARRAYDATA_H |
|
3 | 3 | |
|
4 | 4 | #include <QVector> |
|
5 | 5 | |
|
6 | 6 | /** |
|
7 | 7 | * @brief The ArrayData class represents a dataset for a data series. |
|
8 | 8 | * |
|
9 | 9 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim |
|
10 | 10 | * template-parameter. |
|
11 | 11 | * |
|
12 | 12 | * @tparam Dim the dimension of the ArrayData (one or two) |
|
13 | 13 | * @sa IDataSeries |
|
14 | 14 | */ |
|
15 | 15 | template <int Dim> |
|
16 | 16 | class ArrayData { |
|
17 | 17 | public: |
|
18 | 18 | /** |
|
19 | 19 | * Ctor for a unidimensional ArrayData |
|
20 | 20 | * @param nbColumns the number of values the ArrayData will hold |
|
21 | 21 | */ |
|
22 | 22 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
23 | 23 | explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}} |
|
24 | 24 | { |
|
25 | 25 | m_Data[0].resize(nbColumns); |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | /** |
|
29 | 29 | * Sets a data at a specified index. The index has to be valid to be effective |
|
30 | 30 | * @param index the index to which the data will be set |
|
31 | 31 | * @param data the data to set |
|
32 | 32 | * @remarks this method is only available for a unidimensional ArrayData |
|
33 | 33 | */ |
|
34 | 34 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
35 | 35 | void setData(int index, double data) noexcept |
|
36 | 36 | { |
|
37 | 37 | if (index >= 0 && index < m_Data.at(0).size()) { |
|
38 | 38 | m_Data[0].replace(index, data); |
|
39 | 39 | } |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | /** |
|
43 | 43 | * @return the data as a vector |
|
44 | 44 | * @remarks this method is only available for a unidimensional ArrayData |
|
45 | 45 | */ |
|
46 | 46 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
47 | 47 | QVector<double> data() const noexcept |
|
48 | 48 | { |
|
49 | 49 | return m_Data.at(0); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | /** | |
|
53 | * @return the data as a vector | |
|
54 | * @remarks this method is only available for a unidimensional ArrayData | |
|
55 | */ | |
|
56 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
57 | const QVector<double> &data(double tStart, double tEnd) const noexcept | |
|
58 | { | |
|
59 | return m_Data.at(tStart); | |
|
60 | } | |
|
61 | ||
|
62 | // TODO Comment | |
|
63 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
64 | void merge(ArrayData<1> *arrayData) | |
|
65 | { | |
|
66 | if (!m_Data.empty()) { | |
|
67 | m_Data[0] += arrayData->data(); | |
|
68 | } | |
|
69 | } | |
|
70 | ||
|
71 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
|
72 | int size() | |
|
73 | { | |
|
74 | return m_Data[0].size(); | |
|
75 | } | |
|
76 | ||
|
77 | ||
|
52 | 78 | private: |
|
53 | 79 | QVector<QVector<double> > m_Data; |
|
54 | 80 | }; |
|
55 | 81 | |
|
56 | 82 | #endif // SCIQLOP_ARRAYDATA_H |
@@ -1,50 +1,59 | |||
|
1 | 1 | #ifndef SCIQLOP_DATASERIES_H |
|
2 | 2 | #define SCIQLOP_DATASERIES_H |
|
3 | 3 | |
|
4 | 4 | #include <Data/ArrayData.h> |
|
5 | 5 | #include <Data/IDataSeries.h> |
|
6 | 6 | |
|
7 | 7 | #include <memory> |
|
8 | 8 | |
|
9 | 9 | /** |
|
10 | 10 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. |
|
11 | 11 | * |
|
12 | 12 | * It proposes to set a dimension for the values ββdata |
|
13 | 13 | * |
|
14 | 14 | * @tparam Dim The dimension of the values data |
|
15 | 15 | * |
|
16 | 16 | */ |
|
17 | 17 | template <int Dim> |
|
18 | 18 | class DataSeries : public IDataSeries { |
|
19 | 19 | public: |
|
20 | 20 | /// @sa IDataSeries::xAxisData() |
|
21 | 21 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } |
|
22 | 22 | |
|
23 | 23 | /// @sa IDataSeries::xAxisUnit() |
|
24 | 24 | Unit xAxisUnit() const override { return m_XAxisUnit; } |
|
25 | 25 | |
|
26 | 26 | /// @return the values dataset |
|
27 | 27 | std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } |
|
28 | 28 | |
|
29 | 29 | /// @sa IDataSeries::valuesUnit() |
|
30 | 30 | Unit valuesUnit() const override { return m_ValuesUnit; } |
|
31 | 31 | |
|
32 | /// @sa IDataSeries::merge() | |
|
33 | void merge(IDataSeries *dataSeries) override | |
|
34 | { | |
|
35 | if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { | |
|
36 | m_XAxisData->merge(dimDataSeries->xAxisData().get()); | |
|
37 | m_ValuesData->merge(dimDataSeries->valuesData().get()); | |
|
38 | } | |
|
39 | } | |
|
40 | ||
|
32 | 41 | protected: |
|
33 | 42 | /// Protected ctor (DataSeries is abstract) |
|
34 | 43 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, |
|
35 | 44 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) |
|
36 | 45 | : m_XAxisData{xAxisData}, |
|
37 | 46 | m_XAxisUnit{xAxisUnit}, |
|
38 | 47 | m_ValuesData{valuesData}, |
|
39 | 48 | m_ValuesUnit{valuesUnit} |
|
40 | 49 | { |
|
41 | 50 | } |
|
42 | 51 | |
|
43 | 52 | private: |
|
44 | 53 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
|
45 | 54 | Unit m_XAxisUnit; |
|
46 | 55 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; |
|
47 | 56 | Unit m_ValuesUnit; |
|
48 | 57 | }; |
|
49 | 58 | |
|
50 | 59 | #endif // SCIQLOP_DATASERIES_H |
@@ -1,30 +1,39 | |||
|
1 | 1 | #ifndef SCIQLOP_IDATAPROVIDER_H |
|
2 | 2 | #define SCIQLOP_IDATAPROVIDER_H |
|
3 | 3 | |
|
4 | 4 | #include <memory> |
|
5 | 5 | |
|
6 | 6 | #include <QObject> |
|
7 | 7 | |
|
8 | #include <Data/SqpDateTime.h> | |
|
9 | ||
|
8 | 10 | class DataProviderParameters; |
|
9 | 11 | class IDataSeries; |
|
10 | 12 | |
|
11 | 13 | /** |
|
12 | 14 | * @brief The IDataProvider interface aims to declare a data provider. |
|
13 | 15 | * |
|
14 | 16 | * A data provider is an entity that generates data and returns it according to various parameters |
|
15 | 17 | * (time interval, product to retrieve the data, etc.) |
|
16 | 18 | * |
|
17 | 19 | * @sa IDataSeries |
|
18 | 20 | */ |
|
19 | class IDataProvider { | |
|
21 | class IDataProvider : public QObject { | |
|
22 | ||
|
23 | Q_OBJECT | |
|
20 | 24 | public: |
|
21 | 25 | virtual ~IDataProvider() noexcept = default; |
|
22 | 26 | |
|
23 | 27 | virtual std::unique_ptr<IDataSeries> |
|
24 | 28 | retrieveData(const DataProviderParameters ¶meters) const = 0; |
|
25 | }; | |
|
26 | 29 | |
|
30 | ||
|
31 | virtual void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) = 0; | |
|
32 | ||
|
33 | signals: | |
|
34 | void dataProvided(std::shared_ptr<IDataSeries> dateSerie, SqpDateTime dateTime); | |
|
35 | }; | |
|
27 | 36 | // Required for using shared_ptr in signals/slots |
|
28 | 37 | Q_DECLARE_METATYPE(std::shared_ptr<IDataProvider>) |
|
29 | 38 | |
|
30 | 39 | #endif // SCIQLOP_IDATAPROVIDER_H |
@@ -1,47 +1,54 | |||
|
1 | 1 | #ifndef SCIQLOP_IDATASERIES_H |
|
2 | 2 | #define SCIQLOP_IDATASERIES_H |
|
3 | 3 | |
|
4 | #include <QString> | |
|
5 | 4 | |
|
6 | 5 | #include <memory> |
|
7 | 6 | |
|
7 | #include <QObject> | |
|
8 | #include <QString> | |
|
9 | ||
|
8 | 10 | template <int Dim> |
|
9 | 11 | class ArrayData; |
|
10 | 12 | |
|
11 | 13 | struct Unit { |
|
12 | 14 | explicit Unit(const QString &name = {}, bool timeUnit = false) |
|
13 | 15 | : m_Name{name}, m_TimeUnit{timeUnit} |
|
14 | 16 | { |
|
15 | 17 | } |
|
16 | 18 | |
|
17 | 19 | QString m_Name; ///< Unit name |
|
18 | 20 | bool m_TimeUnit; ///< The unit is a unit of time |
|
19 | 21 | }; |
|
20 | 22 | |
|
21 | 23 | /** |
|
22 | 24 | * @brief The IDataSeries aims to declare a data series. |
|
23 | 25 | * |
|
24 | 26 | * A data series is an entity that contains at least : |
|
25 | 27 | * - one dataset representing the x-axis |
|
26 | 28 | * - one dataset representing the values |
|
27 | 29 | * |
|
28 | 30 | * Each dataset is represented by an ArrayData, and is associated with a unit. |
|
29 | 31 | * |
|
30 | 32 | * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the |
|
31 | 33 | * IDataSeries. The x-axis dataset is always unidimensional. |
|
32 | 34 | * |
|
33 | 35 | * @sa ArrayData |
|
34 | 36 | */ |
|
35 | 37 | class IDataSeries { |
|
36 | 38 | public: |
|
37 | 39 | virtual ~IDataSeries() noexcept = default; |
|
38 | 40 | |
|
39 | 41 | /// Returns the x-axis dataset |
|
40 | 42 | virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0; |
|
41 | 43 | |
|
42 | 44 | virtual Unit xAxisUnit() const = 0; |
|
43 | 45 | |
|
44 | 46 | virtual Unit valuesUnit() const = 0; |
|
47 | ||
|
48 | virtual void merge(IDataSeries *dataSeries) = 0; | |
|
45 | 49 | }; |
|
46 | 50 | |
|
51 | // Required for using shared_ptr in signals/slots | |
|
52 | Q_DECLARE_METATYPE(std::shared_ptr<IDataSeries>) | |
|
53 | ||
|
47 | 54 | #endif // SCIQLOP_IDATASERIES_H |
@@ -1,14 +1,23 | |||
|
1 | 1 | #ifndef SCIQLOP_SQPDATETIME_H |
|
2 | 2 | #define SCIQLOP_SQPDATETIME_H |
|
3 | 3 | |
|
4 | #include <QObject> | |
|
4 | 5 | /** |
|
5 | 6 | * @brief The SqpDateTime struct holds the information of time parameters |
|
6 | 7 | */ |
|
7 | 8 | struct SqpDateTime { |
|
8 | 9 | /// Start time |
|
9 | 10 | double m_TStart; |
|
10 | 11 | /// End time |
|
11 | 12 | double m_TEnd; |
|
13 | ||
|
14 | bool contains(const SqpDateTime &dateTime) | |
|
15 | { | |
|
16 | return (m_TStart <= dateTime.m_TStart && m_TEnd >= dateTime.m_TEnd); | |
|
17 | } | |
|
12 | 18 | }; |
|
13 | 19 | |
|
20 | // Required for using shared_ptr in signals/slots | |
|
21 | Q_DECLARE_METATYPE(SqpDateTime) | |
|
22 | ||
|
14 | 23 | #endif // SCIQLOP_SQPDATETIME_H |
@@ -1,35 +1,54 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLE_H |
|
2 | 2 | #define SCIQLOP_VARIABLE_H |
|
3 | 3 | |
|
4 |
#include < |
|
|
4 | #include <Data/SqpDateTime.h> | |
|
5 | ||
|
5 | 6 | |
|
7 | #include <QLoggingCategory> | |
|
6 | 8 | #include <QObject> |
|
7 | 9 | |
|
10 | #include <Common/spimpl.h> | |
|
11 | ||
|
12 | Q_DECLARE_LOGGING_CATEGORY(LOG_Variable) | |
|
13 | ||
|
8 | 14 | class IDataSeries; |
|
9 | 15 | class QString; |
|
10 | 16 | |
|
11 | 17 | /** |
|
12 | 18 | * @brief The Variable class represents a variable in SciQlop. |
|
13 | 19 | */ |
|
14 | class Variable { | |
|
20 | class Variable : public QObject { | |
|
21 | ||
|
22 | Q_OBJECT | |
|
23 | ||
|
15 | 24 | public: |
|
16 |
explicit Variable(const QString &name, const QString &unit, const QString &mission |
|
|
25 | explicit Variable(const QString &name, const QString &unit, const QString &mission, | |
|
26 | const SqpDateTime &dateTime); | |
|
17 | 27 | |
|
18 | 28 | QString name() const noexcept; |
|
19 | 29 | QString mission() const noexcept; |
|
20 | 30 | QString unit() const noexcept; |
|
21 | ||
|
22 | void addDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept; | |
|
31 | SqpDateTime dateTime() const noexcept; | |
|
23 | 32 | |
|
24 | 33 | /// @return the data of the variable, nullptr if there is no data |
|
25 | 34 | IDataSeries *dataSeries() const noexcept; |
|
26 | 35 | |
|
36 | bool contains(SqpDateTime dateTime); | |
|
37 | void setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept; | |
|
38 | ||
|
39 | public slots: | |
|
40 | void onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept; | |
|
41 | ||
|
42 | signals: | |
|
43 | void dataCacheUpdated(); | |
|
44 | ||
|
45 | ||
|
27 | 46 | private: |
|
28 | 47 | class VariablePrivate; |
|
29 | 48 | spimpl::unique_impl_ptr<VariablePrivate> impl; |
|
30 | 49 | }; |
|
31 | 50 | |
|
32 | 51 | // Required for using shared_ptr in signals/slots |
|
33 | 52 | Q_DECLARE_METATYPE(std::shared_ptr<Variable>) |
|
34 | 53 | |
|
35 | 54 | #endif // SCIQLOP_VARIABLE_H |
@@ -1,51 +1,58 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLECONTROLLER_H |
|
2 | 2 | #define SCIQLOP_VARIABLECONTROLLER_H |
|
3 | 3 | |
|
4 | #include <Data/SqpDateTime.h> | |
|
5 | ||
|
4 | 6 | #include <QLoggingCategory> |
|
5 | 7 | #include <QObject> |
|
6 | 8 | |
|
7 | 9 | #include <Common/spimpl.h> |
|
8 | 10 | |
|
11 | ||
|
9 | 12 | class IDataProvider; |
|
10 | 13 | class TimeController; |
|
11 | 14 | class Variable; |
|
12 | 15 | class VariableModel; |
|
13 | 16 | |
|
14 | 17 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController) |
|
15 | 18 | |
|
16 | 19 | /** |
|
17 | 20 | * @brief The VariableController class aims to handle the variables in SciQlop. |
|
18 | 21 | */ |
|
19 | 22 | class VariableController : public QObject { |
|
20 | 23 | Q_OBJECT |
|
21 | 24 | public: |
|
22 | 25 | explicit VariableController(QObject *parent = 0); |
|
23 | 26 | virtual ~VariableController(); |
|
24 | 27 | |
|
25 | 28 | VariableModel *variableModel() noexcept; |
|
26 | 29 | |
|
27 | 30 | void setTimeController(TimeController *timeController) noexcept; |
|
28 | 31 | |
|
32 | ||
|
33 | /// Request the data loading of the variable whithin dateTime | |
|
34 | void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); | |
|
35 | ||
|
29 | 36 | signals: |
|
30 | 37 | /// Signal emitted when a variable has been created |
|
31 | 38 | void variableCreated(std::shared_ptr<Variable> variable); |
|
32 | 39 | |
|
33 | 40 | public slots: |
|
34 | 41 | /** |
|
35 | 42 | * Creates a new variable and adds it to the model |
|
36 | 43 | * @param name the name of the new variable |
|
37 | 44 | * @param provider the data provider for the new variable |
|
38 | 45 | */ |
|
39 | 46 | void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept; |
|
40 | 47 | |
|
41 | 48 | void initialize(); |
|
42 | 49 | void finalize(); |
|
43 | 50 | |
|
44 | 51 | private: |
|
45 | 52 | void waitForFinish(); |
|
46 | 53 | |
|
47 | 54 | class VariableControllerPrivate; |
|
48 | 55 | spimpl::unique_impl_ptr<VariableControllerPrivate> impl; |
|
49 | 56 | }; |
|
50 | 57 | |
|
51 | 58 | #endif // SCIQLOP_VARIABLECONTROLLER_H |
@@ -1,44 +1,49 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLEMODEL_H |
|
2 | 2 | #define SCIQLOP_VARIABLEMODEL_H |
|
3 | 3 | |
|
4 | #include <Common/spimpl.h> | |
|
4 | ||
|
5 | #include <Data/SqpDateTime.h> | |
|
5 | 6 | |
|
6 | 7 | #include <QAbstractTableModel> |
|
7 | 8 | #include <QLoggingCategory> |
|
8 | 9 | |
|
10 | #include <Common/spimpl.h> | |
|
11 | ||
|
9 | 12 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) |
|
10 | 13 | |
|
11 | 14 | class IDataSeries; |
|
12 | 15 | class Variable; |
|
13 | 16 | |
|
14 | 17 | /** |
|
15 | 18 | * @brief The VariableModel class aims to hold the variables that have been created in SciQlop |
|
16 | 19 | */ |
|
17 | 20 | class VariableModel : public QAbstractTableModel { |
|
18 | 21 | public: |
|
19 | 22 | explicit VariableModel(QObject *parent = nullptr); |
|
20 | 23 | |
|
21 | 24 | /** |
|
22 | 25 | * Creates a new variable in the model |
|
23 | 26 | * @param name the name of the new variable |
|
27 | * @param dateTime the dateTime of the new variable | |
|
24 | 28 | * @param defaultDataSeries the default data of the new variable |
|
25 | 29 | * @return the pointer to the new variable |
|
26 | 30 | */ |
|
27 | 31 | std::shared_ptr<Variable> |
|
28 |
createVariable(const QString &name, st |
|
|
32 | createVariable(const QString &name, const SqpDateTime &dateTime, | |
|
33 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept; | |
|
29 | 34 | |
|
30 | 35 | // /////////////////////////// // |
|
31 | 36 | // QAbstractTableModel methods // |
|
32 | 37 | // /////////////////////////// // |
|
33 | 38 | virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
34 | 39 | virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
35 | 40 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
|
36 | 41 | virtual QVariant headerData(int section, Qt::Orientation orientation, |
|
37 | 42 | int role = Qt::DisplayRole) const override; |
|
38 | 43 | |
|
39 | 44 | private: |
|
40 | 45 | class VariableModelPrivate; |
|
41 | 46 | spimpl::unique_impl_ptr<VariableModelPrivate> impl; |
|
42 | 47 | }; |
|
43 | 48 | |
|
44 | 49 | #endif // SCIQLOP_VARIABLEMODEL_H |
@@ -1,48 +1,89 | |||
|
1 | 1 | #include "Variable/Variable.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/IDataSeries.h> |
|
4 | #include <Data/SqpDateTime.h> | |
|
5 | ||
|
6 | Q_LOGGING_CATEGORY(LOG_Variable, "Variable") | |
|
4 | 7 | |
|
5 | 8 | struct Variable::VariablePrivate { |
|
6 |
explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission |
|
|
7 | : m_Name{name}, m_Unit{unit}, m_Mission{mission}, m_DataSeries{nullptr} | |
|
9 | explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission, | |
|
10 | const SqpDateTime &dateTime) | |
|
11 | : m_Name{name}, | |
|
12 | m_Unit{unit}, | |
|
13 | m_Mission{mission}, | |
|
14 | m_DateTime{dateTime}, | |
|
15 | m_DataSeries{nullptr} | |
|
8 | 16 | { |
|
9 | 17 | } |
|
10 | 18 | |
|
11 | 19 | QString m_Name; |
|
12 | 20 | QString m_Unit; |
|
13 | 21 | QString m_Mission; |
|
22 | ||
|
23 | SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache. | |
|
14 | 24 | std::unique_ptr<IDataSeries> m_DataSeries; |
|
15 | 25 | }; |
|
16 | 26 | |
|
17 |
Variable::Variable(const QString &name, const QString &unit, const QString &mission |
|
|
18 | : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission)} | |
|
27 | Variable::Variable(const QString &name, const QString &unit, const QString &mission, | |
|
28 | const SqpDateTime &dateTime) | |
|
29 | : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission, dateTime)} | |
|
19 | 30 | { |
|
20 | 31 | } |
|
21 | 32 | |
|
22 | 33 | QString Variable::name() const noexcept |
|
23 | 34 | { |
|
24 | 35 | return impl->m_Name; |
|
25 | 36 | } |
|
26 | 37 | |
|
27 | 38 | QString Variable::mission() const noexcept |
|
28 | 39 | { |
|
29 | 40 | return impl->m_Mission; |
|
30 | 41 | } |
|
31 | 42 | |
|
32 | 43 | QString Variable::unit() const noexcept |
|
33 | 44 | { |
|
34 | 45 | return impl->m_Unit; |
|
35 | 46 | } |
|
36 | 47 | |
|
37 | void Variable::addDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept | |
|
48 | SqpDateTime Variable::dateTime() const noexcept | |
|
49 | { | |
|
50 | return impl->m_DateTime; | |
|
51 | } | |
|
52 | ||
|
53 | void Variable::setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept | |
|
38 | 54 | { |
|
39 | 55 | if (!impl->m_DataSeries) { |
|
40 | 56 | impl->m_DataSeries = std::move(dataSeries); |
|
41 | 57 | } |
|
42 | /// @todo : else, merge the two data series (if possible) | |
|
58 | } | |
|
59 | ||
|
60 | void Variable::onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept | |
|
61 | { | |
|
62 | if (impl->m_DataSeries) { | |
|
63 | impl->m_DataSeries->merge(dataSeries.get()); | |
|
64 | ||
|
65 | emit dataCacheUpdated(); | |
|
66 | } | |
|
43 | 67 | } |
|
44 | 68 | |
|
45 | 69 | IDataSeries *Variable::dataSeries() const noexcept |
|
46 | 70 | { |
|
47 | 71 | return impl->m_DataSeries.get(); |
|
48 | 72 | } |
|
73 | ||
|
74 | bool Variable::contains(SqpDateTime dateTime) | |
|
75 | { | |
|
76 | if (!impl->m_DateTime.contains(dateTime)) { | |
|
77 | // The current variable dateTime isn't enough to display the dateTime requested. | |
|
78 | // We have to update it to the new dateTime requested. | |
|
79 | // the correspondant new data to display will be given by the cache if possible and the | |
|
80 | // provider if necessary. | |
|
81 | qCInfo(LOG_Variable()) << "NEW DATE NEEDED"; | |
|
82 | ||
|
83 | impl->m_DateTime = dateTime; | |
|
84 | ||
|
85 | return false; | |
|
86 | } | |
|
87 | ||
|
88 | return true; | |
|
89 | } |
@@ -1,110 +1,164 | |||
|
1 | #include <Variable/Variable.h> | |
|
2 | #include <Variable/VariableCacheController.h> | |
|
1 | 3 | #include <Variable/VariableController.h> |
|
2 | 4 | #include <Variable/VariableModel.h> |
|
3 | 5 | |
|
4 | 6 | #include <Data/DataProviderParameters.h> |
|
5 | 7 | #include <Data/IDataProvider.h> |
|
6 | 8 | #include <Data/IDataSeries.h> |
|
7 | 9 | #include <Time/TimeController.h> |
|
8 | 10 | |
|
9 | 11 | #include <QDateTime> |
|
12 | #include <QElapsedTimer> | |
|
10 | 13 | #include <QMutex> |
|
11 | 14 | #include <QThread> |
|
12 | 15 | |
|
16 | #include <unordered_map> | |
|
17 | ||
|
13 | 18 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") |
|
14 | 19 | |
|
15 | 20 | namespace { |
|
16 | 21 | |
|
17 | 22 | /// @todo Generates default dataseries, according to the provider passed in parameter. This method |
|
18 | 23 | /// will be deleted when the timerange is recovered from SciQlop |
|
19 | 24 | std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, |
|
20 | 25 | const SqpDateTime &dateTime) noexcept |
|
21 | 26 | { |
|
22 | 27 | auto parameters = DataProviderParameters{dateTime}; |
|
23 | 28 | |
|
24 | 29 | return provider.retrieveData(parameters); |
|
25 | 30 | } |
|
26 | 31 | |
|
27 | 32 | } // namespace |
|
28 | 33 | |
|
29 | 34 | struct VariableController::VariableControllerPrivate { |
|
30 | 35 | explicit VariableControllerPrivate(VariableController *parent) |
|
31 | : m_WorkingMutex{}, m_VariableModel{new VariableModel{parent}} | |
|
36 | : m_WorkingMutex{}, | |
|
37 | m_VariableModel{new VariableModel{parent}}, | |
|
38 | m_VariableCacheController{std::make_unique<VariableCacheController>()} | |
|
32 | 39 | { |
|
33 | 40 | } |
|
34 | 41 | |
|
35 | 42 | QMutex m_WorkingMutex; |
|
36 | 43 | /// Variable model. The VariableController has the ownership |
|
37 | 44 | VariableModel *m_VariableModel; |
|
38 | 45 | |
|
46 | ||
|
39 | 47 | TimeController *m_TimeController{nullptr}; |
|
48 | std::unique_ptr<VariableCacheController> m_VariableCacheController; | |
|
49 | ||
|
50 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | |
|
51 | m_VariableToProviderMap; | |
|
40 | 52 | }; |
|
41 | 53 | |
|
42 | 54 | VariableController::VariableController(QObject *parent) |
|
43 | 55 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} |
|
44 | 56 | { |
|
45 | 57 | qCDebug(LOG_VariableController()) << tr("VariableController construction") |
|
46 | 58 | << QThread::currentThread(); |
|
47 | 59 | } |
|
48 | 60 | |
|
49 | 61 | VariableController::~VariableController() |
|
50 | 62 | { |
|
51 | 63 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") |
|
52 | 64 | << QThread::currentThread(); |
|
53 | 65 | this->waitForFinish(); |
|
54 | 66 | } |
|
55 | 67 | |
|
56 | 68 | VariableModel *VariableController::variableModel() noexcept |
|
57 | 69 | { |
|
58 | 70 | return impl->m_VariableModel; |
|
59 | 71 | } |
|
60 | 72 | |
|
61 | 73 | void VariableController::setTimeController(TimeController *timeController) noexcept |
|
62 | 74 | { |
|
63 | 75 | impl->m_TimeController = timeController; |
|
64 | 76 | } |
|
65 | 77 | |
|
66 | 78 | void VariableController::createVariable(const QString &name, |
|
67 | 79 | std::shared_ptr<IDataProvider> provider) noexcept |
|
68 | 80 | { |
|
69 | // TORM | |
|
70 | // auto dateTime = SqpDateTime{ | |
|
71 | // // Remarks : we don't use toSecsSinceEpoch() here (method is for Qt 5.8 or above) | |
|
72 | // static_cast<double>(QDateTime{QDate{2017, 01, 01}, QTime{12, 00}}.toMSecsSinceEpoch() | |
|
73 | // / 1000.), | |
|
74 | // static_cast<double>(QDateTime{QDate{2017, 01, 01}, QTime{12, 01}}.toMSecsSinceEpoch()) | |
|
75 | // / 1000.}; | |
|
76 | 81 | |
|
77 | 82 | if (!impl->m_TimeController) { |
|
78 | 83 | qCCritical(LOG_VariableController()) |
|
79 | 84 | << tr("Impossible to create variable: The time controller is null"); |
|
80 | 85 | return; |
|
81 | 86 | } |
|
82 | 87 | |
|
83 | 88 | |
|
84 | 89 | /// @todo : for the moment : |
|
85 | 90 | /// - the provider is only used to retrieve data from the variable for its initialization, but |
|
86 | 91 | /// it will be retained later |
|
87 | 92 | /// - default data are generated for the variable, without taking into account the timerange set |
|
88 | 93 | /// in sciqlop |
|
94 | auto dateTime = impl->m_TimeController->dateTime(); | |
|
89 | 95 | if (auto newVariable = impl->m_VariableModel->createVariable( |
|
90 |
name, generateDefaultDataSeries(*provider, |
|
|
96 | name, dateTime, generateDefaultDataSeries(*provider, dateTime))) { | |
|
97 | ||
|
98 | // store the provider | |
|
99 | impl->m_VariableToProviderMap[newVariable] = provider; | |
|
100 | qRegisterMetaType<std::shared_ptr<IDataSeries> >(); | |
|
101 | qRegisterMetaType<SqpDateTime>(); | |
|
102 | connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(), | |
|
103 | &Variable::onAddDataSeries); | |
|
104 | ||
|
105 | ||
|
106 | // store in cache | |
|
107 | impl->m_VariableCacheController->addDateTime(newVariable, dateTime); | |
|
108 | ||
|
109 | // notify the creation | |
|
91 | 110 | emit variableCreated(newVariable); |
|
92 | 111 | } |
|
93 | 112 | } |
|
94 | 113 | |
|
114 | ||
|
115 | void VariableController::requestDataLoading(std::shared_ptr<Variable> variable, | |
|
116 | const SqpDateTime &dateTime) | |
|
117 | { | |
|
118 | // we want to load data of the variable for the dateTime. | |
|
119 | // First we check if the cache contains some of them. | |
|
120 | // For the other, we ask the provider to give them. | |
|
121 | if (variable) { | |
|
122 | ||
|
123 | QElapsedTimer timer; | |
|
124 | timer.start(); | |
|
125 | qCInfo(LOG_VariableController()) << "The slow s0 operation took" << timer.elapsed() | |
|
126 | << "milliseconds"; | |
|
127 | auto dateTimeListNotInCache | |
|
128 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); | |
|
129 | qCInfo(LOG_VariableController()) << "The slow s1 operation took" << timer.elapsed() | |
|
130 | << "milliseconds"; | |
|
131 | ||
|
132 | // Ask the provider for each data on the dateTimeListNotInCache | |
|
133 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading(dateTimeListNotInCache); | |
|
134 | ||
|
135 | qCInfo(LOG_VariableController()) << "The slow s2 operation took" << timer.elapsed() | |
|
136 | << "milliseconds"; | |
|
137 | ||
|
138 | // store in cache | |
|
139 | impl->m_VariableCacheController->addDateTime(variable, dateTime); | |
|
140 | qCInfo(LOG_VariableController()) << "The slow s3 operation took" << timer.elapsed() | |
|
141 | << "milliseconds"; | |
|
142 | } | |
|
143 | else { | |
|
144 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | |
|
145 | } | |
|
146 | } | |
|
147 | ||
|
148 | ||
|
95 | 149 | void VariableController::initialize() |
|
96 | 150 | { |
|
97 | 151 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
98 | 152 | impl->m_WorkingMutex.lock(); |
|
99 | 153 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
100 | 154 | } |
|
101 | 155 | |
|
102 | 156 | void VariableController::finalize() |
|
103 | 157 | { |
|
104 | 158 | impl->m_WorkingMutex.unlock(); |
|
105 | 159 | } |
|
106 | 160 | |
|
107 | 161 | void VariableController::waitForFinish() |
|
108 | 162 | { |
|
109 | 163 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
110 | 164 | } |
@@ -1,120 +1,120 | |||
|
1 | 1 | #include <Variable/Variable.h> |
|
2 | 2 | #include <Variable/VariableModel.h> |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataSeries.h> |
|
5 | 5 | |
|
6 | 6 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
7 | 7 | |
|
8 | 8 | namespace { |
|
9 | 9 | |
|
10 | 10 | // Column indexes |
|
11 | 11 | const auto NAME_COLUMN = 0; |
|
12 | 12 | const auto UNIT_COLUMN = 1; |
|
13 | 13 | const auto MISSION_COLUMN = 2; |
|
14 | 14 | const auto NB_COLUMNS = 3; |
|
15 | 15 | |
|
16 | 16 | } // namespace |
|
17 | 17 | |
|
18 | 18 | struct VariableModel::VariableModelPrivate { |
|
19 | 19 | /// Variables created in SciQlop |
|
20 | 20 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
21 | 21 | }; |
|
22 | 22 | |
|
23 | 23 | VariableModel::VariableModel(QObject *parent) |
|
24 | 24 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
25 | 25 | { |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | std::shared_ptr<Variable> |
|
29 | VariableModel::createVariable(const QString &name, | |
|
29 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, | |
|
30 | 30 | std::unique_ptr<IDataSeries> defaultDataSeries) noexcept |
|
31 | 31 | { |
|
32 | 32 | auto insertIndex = rowCount(); |
|
33 | 33 | beginInsertRows({}, insertIndex, insertIndex); |
|
34 | 34 | |
|
35 | 35 | /// @todo For the moment, the other data of the variable is initialized with default values |
|
36 | auto variable | |
|
37 | = std::make_shared<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission")); | |
|
38 |
variable-> |
|
|
36 | auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"), | |
|
37 | QStringLiteral("mission"), dateTime); | |
|
38 | variable->setDataSeries(std::move(defaultDataSeries)); | |
|
39 | 39 | |
|
40 | 40 | impl->m_Variables.push_back(variable); |
|
41 | 41 | |
|
42 | 42 | endInsertRows(); |
|
43 | 43 | |
|
44 | 44 | return variable; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
48 | 48 | { |
|
49 | 49 | Q_UNUSED(parent); |
|
50 | 50 | |
|
51 | 51 | return NB_COLUMNS; |
|
52 | 52 | } |
|
53 | 53 | |
|
54 | 54 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
55 | 55 | { |
|
56 | 56 | Q_UNUSED(parent); |
|
57 | 57 | |
|
58 | 58 | return impl->m_Variables.size(); |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
62 | 62 | { |
|
63 | 63 | if (!index.isValid()) { |
|
64 | 64 | return QVariant{}; |
|
65 | 65 | } |
|
66 | 66 | |
|
67 | 67 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
68 | 68 | return QVariant{}; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | if (role == Qt::DisplayRole) { |
|
72 | 72 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
73 | 73 | switch (index.column()) { |
|
74 | 74 | case NAME_COLUMN: |
|
75 | 75 | return variable->name(); |
|
76 | 76 | case UNIT_COLUMN: |
|
77 | 77 | return variable->unit(); |
|
78 | 78 | case MISSION_COLUMN: |
|
79 | 79 | return variable->mission(); |
|
80 | 80 | default: |
|
81 | 81 | // No action |
|
82 | 82 | break; |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | qWarning(LOG_VariableModel()) |
|
86 | 86 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
87 | 87 | } |
|
88 | 88 | else { |
|
89 | 89 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
90 | 90 | } |
|
91 | 91 | } |
|
92 | 92 | |
|
93 | 93 | return QVariant{}; |
|
94 | 94 | } |
|
95 | 95 | |
|
96 | 96 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
97 | 97 | { |
|
98 | 98 | if (role != Qt::DisplayRole) { |
|
99 | 99 | return QVariant{}; |
|
100 | 100 | } |
|
101 | 101 | |
|
102 | 102 | if (orientation == Qt::Horizontal) { |
|
103 | 103 | switch (section) { |
|
104 | 104 | case NAME_COLUMN: |
|
105 | 105 | return tr("Name"); |
|
106 | 106 | case UNIT_COLUMN: |
|
107 | 107 | return tr("Unit"); |
|
108 | 108 | case MISSION_COLUMN: |
|
109 | 109 | return tr("Mission"); |
|
110 | 110 | default: |
|
111 | 111 | // No action |
|
112 | 112 | break; |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | qWarning(LOG_VariableModel()) |
|
116 | 116 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
117 | 117 | } |
|
118 | 118 | |
|
119 | 119 | return QVariant{}; |
|
120 | 120 | } |
@@ -1,128 +1,139 | |||
|
1 | 1 | # |
|
2 | 2 | # use_clangformat.cmake |
|
3 | 3 | # |
|
4 | 4 | # The following functions are defined in this document: |
|
5 | 5 | # |
|
6 | 6 | |
|
7 | 7 | # ADD_CLANGFORMAT_TARGETS(<globExpression> ... |
|
8 | 8 | # [ADD_TO_ALL] |
|
9 | 9 | # [NAME <name>] |
|
10 | 10 | # [NAME_ALL <nameall>] |
|
11 | 11 | # [STYLE style] |
|
12 | 12 | # [RECURSE]) |
|
13 | 13 | # |
|
14 | 14 | # Two custom targets will be created: |
|
15 | 15 | # * format_reports is run as part of the build, and is not rerun unless one of |
|
16 | 16 | # the file formatted is modified (only created if ADD_TO_ALL is provided); |
|
17 | 17 | # * format must be explicitely called (make format) and is rerun even if the |
|
18 | 18 | # files to format have not been modified. To achieve this behavior, the commands |
|
19 | 19 | # used in this target pretend to produce a file without actually producing it. |
|
20 | 20 | # Because the output file is not there after the run, the command will be rerun |
|
21 | 21 | # again at the next target build. |
|
22 | 22 | # |
|
23 | 23 | # If ADD_TO_ALL is provided then a target will be added to the default build |
|
24 | 24 | # targets so that each time a source file is compiled, it is formatted with |
|
25 | 25 | # clang-format. |
|
26 | 26 | # |
|
27 | 27 | # NAME and NAME_ALL customize the name of the targets (format and format_reports |
|
28 | 28 | # by default respectively). |
|
29 | 29 | # |
|
30 | 30 | # STYLE sets the style used by clang-format (default to "file"). |
|
31 | 31 | # |
|
32 | 32 | # RECURSE selects if the glob expressions should be applied recursively or not. |
|
33 | 33 | FUNCTION(ADD_CLANGFORMAT_TARGETS) |
|
34 | 34 | # Default values |
|
35 | 35 | SET(target "format") |
|
36 | 36 | SET(target_all "format_all") |
|
37 | 37 | SET(style "file") |
|
38 | 38 | SET(recurse OFF) |
|
39 | 39 | SET(addToAll OFF) |
|
40 | 40 | SET(globs) |
|
41 | 41 | |
|
42 | 42 | # Parse the options |
|
43 | 43 | MATH(EXPR lastIdx "${ARGC} - 1") |
|
44 | 44 | SET(i 0) |
|
45 | 45 | WHILE(i LESS ${ARGC}) |
|
46 | 46 | SET(arg "${ARGV${i}}") |
|
47 | 47 | IF("${arg}" STREQUAL "ADD_TO_ALL") |
|
48 | 48 | SET(addToAll ON) |
|
49 | 49 | ELSEIF("${arg}" STREQUAL "NAME") |
|
50 | 50 | clangformat_incr(i) |
|
51 | 51 | SET(target "${ARGV${i}}") |
|
52 | 52 | ELSEIF("${arg}" STREQUAL "NAME_ALL") |
|
53 | 53 | clangformat_incr(i) |
|
54 | 54 | SET(target_all "${ARGV${i}}") |
|
55 | 55 | ELSEIF("${arg}" STREQUAL "STYLE") |
|
56 | 56 | clangformat_incr(i) |
|
57 | 57 | SET(style "${ARGV${i}}") |
|
58 | 58 | ELSEIF("${arg}" STREQUAL "RECURSE") |
|
59 | 59 | SET(recurse ON) |
|
60 | 60 | ELSE() |
|
61 | 61 | LIST(APPEND globs ${arg}) |
|
62 | 62 | ENDIF() |
|
63 | 63 | clangformat_incr(i) |
|
64 | 64 | ENDWHILE() |
|
65 | 65 | |
|
66 | ||
|
66 | 67 | # Retrieve source files to format |
|
67 | 68 | IF(recurse) |
|
68 | 69 | FILE(GLOB_RECURSE srcs ${globs}) |
|
69 | 70 | ELSE() |
|
70 | 71 | FILE(GLOB srcs ${globs}) |
|
71 | 72 | ENDIF() |
|
72 | 73 | |
|
73 | 74 | IF(NOT CLANGFORMAT_EXECUTABLE) |
|
74 | 75 | MESSAGE(FATAL_ERROR "Unable to find clang-format executable") |
|
75 | 76 | ENDIF() |
|
76 | 77 | |
|
77 | 78 | # Format each source file with clang-format |
|
78 | 79 | SET(touchedFiles) |
|
79 | 80 | SET(fakedTouchedFiles) |
|
80 | 81 | SET(reportNb 0) |
|
81 | 82 | # Create the directory where the touched files will be saved |
|
82 | 83 | SET(formatDirectory "${CMAKE_CURRENT_BINARY_DIR}/format") |
|
83 | 84 | FILE(MAKE_DIRECTORY ${formatDirectory}) |
|
85 | # STRING(REPLACE "*.ui" "" srcs ${srcs}) | |
|
86 | FOREACH(item ${globs}) | |
|
87 | STRING(REGEX MATCH ".*\.ui$" item ${item}) | |
|
88 | IF(item) | |
|
89 | LIST(APPEND UIS ${item}) | |
|
90 | ENDIF(item) | |
|
91 | ENDFOREACH(item ${globs}) | |
|
92 | ||
|
93 | LIST(REMOVE_ITEM srcs ${UIS}) | |
|
94 | message("format lang: ${srcs}" ) | |
|
84 | 95 | FOREACH (s ${srcs}) |
|
85 | 96 | SET(touchedFile ${formatDirectory}/format_touchedfile_${reportNb}) |
|
86 | 97 | IF(addToAll) |
|
87 | 98 | ADD_CUSTOM_COMMAND( |
|
88 | 99 | OUTPUT ${touchedFile} |
|
89 | 100 | COMMAND ${CLANGFORMAT_EXECUTABLE} |
|
90 | 101 | -style="${style}" |
|
91 | 102 | -i |
|
92 | 103 | ${s} |
|
93 | 104 | # Create a file so that this command is executed only if the source |
|
94 | 105 | # file is modified |
|
95 | 106 | COMMAND ${CMAKE_COMMAND} -E touch ${touchedFile} |
|
96 | 107 | DEPENDS ${s} |
|
97 | 108 | COMMENT "Formatting code with clang-format of ${s}" |
|
98 | 109 | ) |
|
99 | 110 | ENDIF() |
|
100 | 111 | |
|
101 | 112 | SET(fakedTouchedFile ${formatDirectory}/format_fakedtouchedfile_${reportNb}) |
|
102 | 113 | ADD_CUSTOM_COMMAND( |
|
103 | 114 | OUTPUT ${fakedTouchedFile} |
|
104 | 115 | COMMAND ${CLANGFORMAT_EXECUTABLE} |
|
105 | 116 | -style="${style}" |
|
106 | 117 | -i |
|
107 | 118 | ${s} |
|
108 | 119 | DEPENDS ${s} |
|
109 | 120 | COMMENT "Formatting code with clang-format of ${s}" |
|
110 | 121 | ) |
|
111 | 122 | |
|
112 | 123 | LIST(APPEND touchedFiles ${touchedFile}) |
|
113 | 124 | LIST(APPEND fakedTouchedFiles ${fakedTouchedFile}) |
|
114 | 125 | clangformat_incr(reportNb) |
|
115 | 126 | ENDFOREACH() |
|
116 | 127 | |
|
117 | 128 | # Create the custom targets that will trigger the custom command created |
|
118 | 129 | # previously |
|
119 | 130 | IF(addToAll) |
|
120 | 131 | ADD_CUSTOM_TARGET(${target_all} ALL DEPENDS ${touchedFiles}) |
|
121 | 132 | ENDIF() |
|
122 | 133 | ADD_CUSTOM_TARGET(${target} DEPENDS ${fakedTouchedFiles}) |
|
123 | 134 | |
|
124 | 135 | ENDFUNCTION(ADD_CLANGFORMAT_TARGETS) |
|
125 | 136 | |
|
126 | 137 | macro(clangformat_incr var_name) |
|
127 | 138 | math(EXPR ${var_name} "${${var_name}} + 1") |
|
128 | 139 | endmacro() |
@@ -1,38 +1,39 | |||
|
1 | 1 | #ifndef SCIQLOP_DATASOURCEWIDGET_H |
|
2 | 2 | #define SCIQLOP_DATASOURCEWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include <QWidget> |
|
5 | 5 | |
|
6 | 6 | namespace Ui { |
|
7 | 7 | class DataSourceWidget; |
|
8 | 8 | } // Ui |
|
9 | 9 | |
|
10 | 10 | class DataSourceItem; |
|
11 | 11 | |
|
12 | 12 | /** |
|
13 | 13 | * @brief The DataSourceWidget handles the graphical representation (as a tree) of the data sources |
|
14 | 14 | * attached to SciQlop. |
|
15 | 15 | */ |
|
16 | 16 | class DataSourceWidget : public QWidget { |
|
17 | 17 | Q_OBJECT |
|
18 | 18 | |
|
19 | 19 | public: |
|
20 | 20 | explicit DataSourceWidget(QWidget *parent = 0); |
|
21 | virtual ~DataSourceWidget() noexcept; | |
|
21 | 22 | |
|
22 | 23 | public slots: |
|
23 | 24 | /** |
|
24 | 25 | * Adds a data source. An item associated to the data source is created and then added to the |
|
25 | 26 | * representation tree |
|
26 | 27 | * @param dataSource the data source to add. The pointer has to be not null |
|
27 | 28 | */ |
|
28 | 29 | void addDataSource(DataSourceItem *dataSource) noexcept; |
|
29 | 30 | |
|
30 | 31 | private: |
|
31 | 32 | Ui::DataSourceWidget *ui; |
|
32 | 33 | |
|
33 | 34 | private slots: |
|
34 | 35 | /// Slot called when right clicking on an item in the tree (displays a menu) |
|
35 | 36 | void onTreeMenuRequested(const QPoint &pos) noexcept; |
|
36 | 37 | }; |
|
37 | 38 | |
|
38 | 39 | #endif // SCIQLOP_DATASOURCEWIDGET_H |
@@ -1,32 +1,38 | |||
|
1 | 1 | #ifndef SCIQLOP_GRAPHPLOTTABLESFACTORY_H |
|
2 | 2 | #define SCIQLOP_GRAPHPLOTTABLESFACTORY_H |
|
3 | 3 | |
|
4 | #include <Data/SqpDateTime.h> | |
|
5 | ||
|
4 | 6 | #include <QLoggingCategory> |
|
5 | 7 | #include <QVector> |
|
6 | 8 | |
|
7 | 9 | #include <memory> |
|
8 | 10 | |
|
9 | 11 | Q_DECLARE_LOGGING_CATEGORY(LOG_GraphPlottablesFactory) |
|
10 | 12 | |
|
13 | class IDataSeries; | |
|
11 | 14 | class QCPAbstractPlottable; |
|
12 | 15 | class QCustomPlot; |
|
13 | 16 | class Variable; |
|
14 | 17 | |
|
15 | 18 | /** |
|
16 | 19 | * @brief The GraphPlottablesFactory class aims to create the QCustomPlot components relative to a |
|
17 | 20 | * variable, depending on the data series of this variable |
|
18 | 21 | */ |
|
19 | 22 | struct GraphPlottablesFactory { |
|
20 | 23 | /** |
|
21 | 24 | * Creates (if possible) the QCustomPlot components relative to the variable passed in |
|
22 | 25 | * parameter, and adds these to the plot passed in parameter. |
|
23 | 26 | * @param variable the variable for which to create the components |
|
24 | 27 | * @param plot the plot in which to add the created components. It takes ownership of these |
|
25 | 28 | * components. |
|
26 | 29 | * @return the list of the components created |
|
27 | 30 | */ |
|
28 | 31 | static QVector<QCPAbstractPlottable *> create(std::shared_ptr<Variable> variable, |
|
29 | 32 | QCustomPlot &plot) noexcept; |
|
33 | ||
|
34 | static void updateData(QVector<QCPAbstractPlottable *> plotableVect, IDataSeries *dataSeries, | |
|
35 | const SqpDateTime &dateTime); | |
|
30 | 36 | }; |
|
31 | 37 | |
|
32 | 38 | #endif // SCIQLOP_GRAPHPLOTTABLESFACTORY_H |
@@ -1,47 +1,56 | |||
|
1 | 1 | #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
|
2 | 2 | #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
|
3 | 3 | |
|
4 | 4 | #include "Visualization/IVisualizationWidget.h" |
|
5 | 5 | |
|
6 | 6 | #include <QLoggingCategory> |
|
7 | 7 | #include <QWidget> |
|
8 | 8 | |
|
9 | 9 | #include <memory> |
|
10 | 10 | |
|
11 | 11 | #include <Common/spimpl.h> |
|
12 | 12 | |
|
13 | 13 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget) |
|
14 | 14 | |
|
15 | class QCPRange; | |
|
15 | 16 | class Variable; |
|
16 | 17 | |
|
17 | 18 | namespace Ui { |
|
18 | 19 | class VisualizationGraphWidget; |
|
19 | 20 | } // namespace Ui |
|
20 | 21 | |
|
21 | 22 | class VisualizationGraphWidget : public QWidget, public IVisualizationWidget { |
|
22 | 23 | Q_OBJECT |
|
23 | 24 | |
|
24 | 25 | public: |
|
25 | 26 | explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0); |
|
26 | 27 | virtual ~VisualizationGraphWidget(); |
|
27 | 28 | |
|
28 | 29 | void addVariable(std::shared_ptr<Variable> variable); |
|
29 | 30 | |
|
30 | 31 | // IVisualizationWidget interface |
|
31 | 32 | void accept(IVisualizationWidgetVisitor *visitor) override; |
|
32 | 33 | bool canDrop(const Variable &variable) const override; |
|
33 | 34 | void close() override; |
|
34 | 35 | QString name() const override; |
|
35 | 36 | |
|
37 | void updateDisplay(std::shared_ptr<Variable> variable); | |
|
38 | ||
|
39 | ||
|
36 | 40 | private: |
|
37 | 41 | Ui::VisualizationGraphWidget *ui; |
|
38 | 42 | |
|
39 | 43 | class VisualizationGraphWidgetPrivate; |
|
40 | 44 | spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl; |
|
41 | 45 | |
|
42 | 46 | private slots: |
|
47 | ||
|
48 | void onRangeChanged(const QCPRange &t1, const QCPRange &t2); | |
|
49 | ||
|
43 | 50 | /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done |
|
44 | 51 | void onMouseWheel(QWheelEvent *event) noexcept; |
|
52 | ||
|
53 | void onDataCacheVariableUpdated(); | |
|
45 | 54 | }; |
|
46 | 55 | |
|
47 | 56 | #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H |
@@ -1,72 +1,77 | |||
|
1 | 1 | #include <DataSource/DataSourceWidget.h> |
|
2 | 2 | |
|
3 | 3 | #include <ui_DataSourceWidget.h> |
|
4 | 4 | |
|
5 | 5 | #include <DataSource/DataSourceItem.h> |
|
6 | 6 | #include <DataSource/DataSourceTreeWidgetItem.h> |
|
7 | 7 | |
|
8 | 8 | #include <QMenu> |
|
9 | 9 | |
|
10 | 10 | namespace { |
|
11 | 11 | |
|
12 | 12 | /// Number of columns displayed in the tree |
|
13 | 13 | const auto TREE_NB_COLUMNS = 1; |
|
14 | 14 | |
|
15 | 15 | /// Header labels for the tree |
|
16 | 16 | const auto TREE_HEADER_LABELS = QStringList{QObject::tr("Name")}; |
|
17 | 17 | |
|
18 | 18 | /** |
|
19 | 19 | * Creates the item associated to a data source |
|
20 | 20 | * @param dataSource the data source for which to create the item |
|
21 | 21 | * @return the new item |
|
22 | 22 | */ |
|
23 | 23 | DataSourceTreeWidgetItem *createTreeWidgetItem(DataSourceItem *dataSource) |
|
24 | 24 | { |
|
25 | 25 | // Creates item for the data source |
|
26 | 26 | auto item = new DataSourceTreeWidgetItem{dataSource}; |
|
27 | 27 | |
|
28 | 28 | // Generates items for the children of the data source |
|
29 | 29 | for (auto i = 0; i < dataSource->childCount(); ++i) { |
|
30 | 30 | item->addChild(createTreeWidgetItem(dataSource->child(i))); |
|
31 | 31 | } |
|
32 | 32 | |
|
33 | 33 | return item; |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | } // namespace |
|
37 | 37 | |
|
38 | 38 | DataSourceWidget::DataSourceWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::DataSourceWidget} |
|
39 | 39 | { |
|
40 | 40 | ui->setupUi(this); |
|
41 | 41 | |
|
42 | 42 | // Set tree properties |
|
43 | 43 | ui->treeWidget->setColumnCount(TREE_NB_COLUMNS); |
|
44 | 44 | ui->treeWidget->setHeaderLabels(TREE_HEADER_LABELS); |
|
45 | 45 | ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
46 | 46 | |
|
47 | 47 | // Connection to show a menu when right clicking on the tree |
|
48 | 48 | connect(ui->treeWidget, &QTreeWidget::customContextMenuRequested, this, |
|
49 | 49 | &DataSourceWidget::onTreeMenuRequested); |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | DataSourceWidget::~DataSourceWidget() noexcept | |
|
53 | { | |
|
54 | delete ui; | |
|
55 | } | |
|
56 | ||
|
52 | 57 | void DataSourceWidget::addDataSource(DataSourceItem *dataSource) noexcept |
|
53 | 58 | { |
|
54 | 59 | // Creates the item associated to the source and adds it to the tree widget. The tree widget |
|
55 | 60 | // takes the ownership of the item |
|
56 | 61 | if (dataSource) { |
|
57 | 62 | ui->treeWidget->addTopLevelItem(createTreeWidgetItem(dataSource)); |
|
58 | 63 | } |
|
59 | 64 | } |
|
60 | 65 | |
|
61 | 66 | void DataSourceWidget::onTreeMenuRequested(const QPoint &pos) noexcept |
|
62 | 67 | { |
|
63 | 68 | // Retrieves the selected item in the tree, and build the menu from its actions |
|
64 | 69 | if (auto selectedItem = dynamic_cast<DataSourceTreeWidgetItem *>(ui->treeWidget->itemAt(pos))) { |
|
65 | 70 | QMenu treeMenu{}; |
|
66 | 71 | treeMenu.addActions(selectedItem->actions()); |
|
67 | 72 | |
|
68 | 73 | if (!treeMenu.isEmpty()) { |
|
69 | 74 | treeMenu.exec(mapToGlobal(pos)); |
|
70 | 75 | } |
|
71 | 76 | } |
|
72 | 77 | } |
@@ -1,91 +1,151 | |||
|
1 | 1 | #include "Visualization/GraphPlottablesFactory.h" |
|
2 | 2 | #include "Visualization/qcustomplot.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/ScalarSeries.h> |
|
5 | 5 | |
|
6 | 6 | #include <Variable/Variable.h> |
|
7 | 7 | |
|
8 | #include <QElapsedTimer> | |
|
9 | ||
|
8 | 10 | Q_LOGGING_CATEGORY(LOG_GraphPlottablesFactory, "GraphPlottablesFactory") |
|
9 | 11 | |
|
10 | 12 | namespace { |
|
11 | 13 | |
|
12 | 14 | /// Format for datetimes on a axis |
|
13 | 15 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); |
|
14 | 16 | |
|
15 | 17 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or |
|
16 | 18 | /// non-time data |
|
17 | 19 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) |
|
18 | 20 | { |
|
19 | 21 | if (isTimeAxis) { |
|
20 | 22 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); |
|
21 | 23 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); |
|
22 | 24 | |
|
23 | 25 | return dateTicker; |
|
24 | 26 | } |
|
25 | 27 | else { |
|
26 | 28 | // default ticker |
|
27 | 29 | return QSharedPointer<QCPAxisTicker>::create(); |
|
28 | 30 | } |
|
29 | 31 | } |
|
30 | 32 | |
|
31 |
QCPAbstractPlottable *c |
|
|
33 | void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, | |
|
34 | const SqpDateTime &dateTime) | |
|
35 | { | |
|
36 | QElapsedTimer timer; | |
|
37 | timer.start(); | |
|
38 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { | |
|
39 | // Clean the graph | |
|
40 | qCDebug(LOG_GraphPlottablesFactory()) << "The slow s1 operation took" << timer.elapsed() | |
|
41 | << "milliseconds"; | |
|
42 | // NAIVE approch | |
|
43 | const auto &xData = scalarSeries.xAxisData()->data(); | |
|
44 | const auto &valuesData = scalarSeries.valuesData()->data(); | |
|
45 | ||
|
46 | auto xValue = QVector<double>(); | |
|
47 | auto vValue = QVector<double>(); | |
|
48 | ||
|
49 | const auto count = xData.count(); | |
|
50 | auto ite = 0; | |
|
51 | for (auto i = 0; i < count; ++i) { | |
|
52 | const auto x = xData.at(i); | |
|
53 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { | |
|
54 | xValue.push_back(x); | |
|
55 | vValue.push_back(valuesData.at(i)); | |
|
56 | ++ite; | |
|
57 | } | |
|
58 | } | |
|
59 | ||
|
60 | qcpGraph->setData(xValue, vValue); | |
|
61 | ||
|
62 | qCDebug(LOG_GraphPlottablesFactory()) << "The slow s2 operation took" << timer.elapsed() | |
|
63 | << "milliseconds"; | |
|
64 | } | |
|
65 | else { | |
|
66 | /// @todo DEBUG | |
|
67 | } | |
|
68 | } | |
|
69 | ||
|
70 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, | |
|
71 | const SqpDateTime &dateTime) | |
|
32 | 72 | { |
|
33 | 73 | auto component = plot.addGraph(); |
|
34 | 74 | |
|
35 | 75 | if (component) { |
|
36 | // Graph data | |
|
76 | // // Graph data | |
|
37 | 77 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), |
|
38 | 78 | true); |
|
39 | 79 | |
|
80 | updateScalarData(component, scalarSeries, dateTime); | |
|
81 | ||
|
40 | 82 | // Axes properties |
|
41 | 83 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
42 | 84 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
43 | 85 | |
|
44 | 86 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
45 | 87 | // label (unit name) |
|
46 | 88 | axis->setLabel(unit.m_Name); |
|
47 | 89 | |
|
48 | 90 | // ticker (depending on the type of unit) |
|
49 | 91 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
50 | 92 | }; |
|
51 | 93 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); |
|
52 | 94 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); |
|
53 | 95 | |
|
54 | 96 | // Display all data |
|
55 | 97 | component->rescaleAxes(); |
|
56 | 98 | |
|
57 | 99 | plot.replot(); |
|
58 | 100 | } |
|
59 | 101 | else { |
|
60 | 102 | qCDebug(LOG_GraphPlottablesFactory()) |
|
61 | 103 | << QObject::tr("Can't create graph for the scalar series"); |
|
62 | 104 | } |
|
63 | 105 | |
|
64 | 106 | return component; |
|
65 | 107 | } |
|
66 | 108 | |
|
67 | 109 | } // namespace |
|
68 | 110 | |
|
69 | 111 | QVector<QCPAbstractPlottable *> GraphPlottablesFactory::create(std::shared_ptr<Variable> variable, |
|
70 | 112 | QCustomPlot &plot) noexcept |
|
71 | 113 | { |
|
72 | 114 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
73 | 115 | |
|
74 | 116 | if (variable) { |
|
75 | 117 | // Gets the data series of the variable to call the creation of the right components |
|
76 | 118 | // according to its type |
|
77 | 119 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { |
|
78 | result.append(createScalarSeriesComponent(*scalarSeries, plot)); | |
|
120 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); | |
|
79 | 121 | } |
|
80 | 122 | else { |
|
81 | 123 | qCDebug(LOG_GraphPlottablesFactory()) |
|
82 | 124 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
83 | 125 | } |
|
84 | 126 | } |
|
85 | 127 | else { |
|
86 | 128 | qCDebug(LOG_GraphPlottablesFactory()) |
|
87 | 129 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
88 | 130 | } |
|
89 | 131 | |
|
90 | 132 | return result; |
|
91 | 133 | } |
|
134 | ||
|
135 | void GraphPlottablesFactory::updateData(QVector<QCPAbstractPlottable *> plotableVect, | |
|
136 | IDataSeries *dataSeries, const SqpDateTime &dateTime) | |
|
137 | { | |
|
138 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | |
|
139 | if (plotableVect.size() == 1) { | |
|
140 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); | |
|
141 | } | |
|
142 | else { | |
|
143 | qCCritical(LOG_GraphPlottablesFactory()) << QObject::tr( | |
|
144 | "Can't update Data of a scalarSeries because there is not only one component " | |
|
145 | "associated"); | |
|
146 | } | |
|
147 | } | |
|
148 | else { | |
|
149 | /// @todo DEBUG | |
|
150 | } | |
|
151 | } |
@@ -1,111 +1,165 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphWidget.h" |
|
2 | 2 | #include "Visualization/GraphPlottablesFactory.h" |
|
3 | 3 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
4 | 4 | #include "ui_VisualizationGraphWidget.h" |
|
5 | 5 | |
|
6 | #include <Data/ArrayData.h> | |
|
7 | #include <Data/IDataSeries.h> | |
|
8 | #include <SqpApplication.h> | |
|
6 | 9 | #include <Variable/Variable.h> |
|
10 | #include <Variable/VariableController.h> | |
|
7 | 11 | |
|
8 | 12 | #include <unordered_map> |
|
9 | 13 | |
|
10 | 14 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") |
|
11 | 15 | |
|
12 | 16 | namespace { |
|
13 | 17 | |
|
14 | 18 | /// Key pressed to enable zoom on horizontal axis |
|
15 | 19 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; |
|
16 | 20 | |
|
17 | 21 | /// Key pressed to enable zoom on vertical axis |
|
18 | 22 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; |
|
19 | 23 | |
|
20 | 24 | } // namespace |
|
21 | 25 | |
|
22 | 26 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { |
|
23 | 27 | |
|
24 | 28 | // 1 variable -> n qcpplot |
|
25 |
std::unordered_map<std::shared_ptr<Variable>, QCPAbstractPlottable *> |
|
|
29 | std::unordered_multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> | |
|
30 | m_VariableToPlotMultiMap; | |
|
26 | 31 | }; |
|
27 | 32 | |
|
28 | 33 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) |
|
29 | 34 | : QWidget{parent}, |
|
30 | 35 | ui{new Ui::VisualizationGraphWidget}, |
|
31 | 36 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} |
|
32 | 37 | { |
|
33 | 38 | ui->setupUi(this); |
|
34 | 39 | |
|
35 | 40 | // qcpplot title |
|
36 | 41 | ui->widget->plotLayout()->insertRow(0); |
|
37 | 42 | ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name}); |
|
38 | 43 | |
|
39 | 44 | // Set qcpplot properties : |
|
40 | 45 | // - Drag (on x-axis) and zoom are enabled |
|
41 | 46 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation |
|
42 | 47 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); |
|
43 | 48 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); |
|
44 | 49 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); |
|
50 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( | |
|
51 | &QCPAxis::rangeChanged), | |
|
52 | this, &VisualizationGraphWidget::onRangeChanged); | |
|
45 | 53 | } |
|
46 | 54 | |
|
55 | ||
|
47 | 56 | VisualizationGraphWidget::~VisualizationGraphWidget() |
|
48 | 57 | { |
|
49 | 58 | delete ui; |
|
50 | 59 | } |
|
51 | 60 | |
|
52 | 61 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) |
|
53 | 62 | { |
|
54 | 63 | // Uses delegate to create the qcpplot components according to the variable |
|
55 | 64 | auto createdPlottables = GraphPlottablesFactory::create(variable, *ui->widget); |
|
56 | 65 | |
|
57 | 66 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
58 | impl->m_VariableToPlotMap.insert({variable, createdPlottable}); | |
|
67 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); | |
|
59 | 68 | } |
|
69 | ||
|
70 | connect(variable.get(), SIGNAL(dataCacheUpdated()), this, SLOT(onDataCacheVariableUpdated())); | |
|
60 | 71 | } |
|
61 | 72 | |
|
62 | 73 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
63 | 74 | { |
|
64 | 75 | if (visitor) { |
|
65 | 76 | visitor->visit(this); |
|
66 | 77 | } |
|
67 | 78 | else { |
|
68 | 79 | qCCritical(LOG_VisualizationGraphWidget()) |
|
69 | 80 | << tr("Can't visit widget : the visitor is null"); |
|
70 | 81 | } |
|
71 | 82 | } |
|
72 | 83 | |
|
73 | 84 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const |
|
74 | 85 | { |
|
75 | 86 | /// @todo : for the moment, a graph can always accomodate a variable |
|
76 | 87 | Q_UNUSED(variable); |
|
77 | 88 | return true; |
|
78 | 89 | } |
|
79 | 90 | |
|
80 | 91 | void VisualizationGraphWidget::close() |
|
81 | 92 | { |
|
82 | 93 | // The main view cannot be directly closed. |
|
83 | 94 | return; |
|
84 | 95 | } |
|
85 | 96 | |
|
86 | 97 | QString VisualizationGraphWidget::name() const |
|
87 | 98 | { |
|
88 | 99 | if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) { |
|
89 | 100 | return title->text(); |
|
90 | 101 | } |
|
91 | 102 | else { |
|
92 | 103 | return QString{}; |
|
93 | 104 | } |
|
94 | 105 | } |
|
95 | 106 | |
|
107 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) | |
|
108 | { | |
|
109 | ||
|
110 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged"); | |
|
111 | ||
|
112 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |
|
113 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | |
|
114 | auto variable = it->first; | |
|
115 | auto tolerance = 0.1 * (t2.upper - t2.lower); | |
|
116 | auto dateTime = SqpDateTime{t2.lower - tolerance, t2.upper + tolerance}; | |
|
117 | ||
|
118 | qCInfo(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged") | |
|
119 | << variable->dataSeries()->xAxisData()->size(); | |
|
120 | if (!variable->contains(dateTime)) { | |
|
121 | sqpApp->variableController().requestDataLoading(variable, dateTime); | |
|
122 | } | |
|
123 | } | |
|
124 | } | |
|
125 | ||
|
96 | 126 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept |
|
97 | 127 | { |
|
98 | 128 | auto zoomOrientations = QFlags<Qt::Orientation>{}; |
|
99 | 129 | |
|
100 | 130 | // Lambda that enables a zoom orientation if the key modifier related to this orientation has |
|
101 | 131 | // been pressed |
|
102 | 132 | auto enableOrientation |
|
103 | 133 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { |
|
104 | 134 | auto orientationEnabled = event->modifiers().testFlag(modifier); |
|
105 | 135 | zoomOrientations.setFlag(orientation, orientationEnabled); |
|
106 | 136 | }; |
|
107 | 137 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); |
|
108 | 138 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); |
|
109 | 139 | |
|
110 | 140 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); |
|
111 | 141 | } |
|
142 | ||
|
143 | void VisualizationGraphWidget::onDataCacheVariableUpdated() | |
|
144 | { | |
|
145 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); | |
|
146 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { | |
|
147 | auto variable = it->first; | |
|
148 | GraphPlottablesFactory::updateData(QVector<QCPAbstractPlottable *>{} << it->second, | |
|
149 | variable->dataSeries(), variable->dateTime()); | |
|
150 | } | |
|
151 | } | |
|
152 | ||
|
153 | void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable) | |
|
154 | { | |
|
155 | auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable); | |
|
156 | ||
|
157 | auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{}; | |
|
158 | ||
|
159 | for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) { | |
|
160 | abstractPlotableVect.push_back(it->second); | |
|
161 | } | |
|
162 | ||
|
163 | GraphPlottablesFactory::updateData(abstractPlotableVect, variable->dataSeries(), | |
|
164 | variable->dateTime()); | |
|
165 | } |
@@ -1,16 +1,26 | |||
|
1 | 1 | #ifndef SCIQLOP_COSINUSPROVIDER_H |
|
2 | 2 | #define SCIQLOP_COSINUSPROVIDER_H |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataProvider.h> |
|
5 | 5 | |
|
6 | #include <QLoggingCategory> | |
|
7 | ||
|
8 | Q_DECLARE_LOGGING_CATEGORY(LOG_CosinusProvider) | |
|
9 | ||
|
6 | 10 | /** |
|
7 | 11 | * @brief The CosinusProvider class is an example of how a data provider can generate data |
|
8 | 12 | */ |
|
9 | 13 | class CosinusProvider : public IDataProvider { |
|
10 | 14 | public: |
|
11 | 15 | /// @sa IDataProvider::retrieveData() |
|
12 | 16 | std::unique_ptr<IDataSeries> |
|
13 | 17 | retrieveData(const DataProviderParameters ¶meters) const override; |
|
18 | ||
|
19 | void requestDataLoading(const QVector<SqpDateTime> &dateTimeList) override; | |
|
20 | ||
|
21 | ||
|
22 | private: | |
|
23 | std::shared_ptr<IDataSeries> retrieveDataSeries(const SqpDateTime &dateTime); | |
|
14 | 24 | }; |
|
15 | 25 | |
|
16 | 26 | #endif // SCIQLOP_COSINUSPROVIDER_H |
@@ -1,32 +1,71 | |||
|
1 | 1 | #include "CosinusProvider.h" |
|
2 | 2 | |
|
3 | 3 | #include <Data/DataProviderParameters.h> |
|
4 | 4 | #include <Data/ScalarSeries.h> |
|
5 | 5 | |
|
6 | 6 | #include <cmath> |
|
7 | 7 | |
|
8 | Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider") | |
|
9 | ||
|
8 | 10 | std::unique_ptr<IDataSeries> |
|
9 | 11 | CosinusProvider::retrieveData(const DataProviderParameters ¶meters) const |
|
10 | 12 | { |
|
11 | 13 | auto dateTime = parameters.m_Time; |
|
12 | 14 | |
|
13 | 15 | // Gets the timerange from the parameters |
|
14 | 16 | auto start = dateTime.m_TStart; |
|
15 | 17 | auto end = dateTime.m_TEnd; |
|
16 | 18 | |
|
17 | 19 | // We assure that timerange is valid |
|
18 | 20 | if (end < start) { |
|
19 | 21 | std::swap(start, end); |
|
20 | 22 | } |
|
21 | 23 | |
|
22 | 24 | // Generates scalar series containing cosinus values (one value per second) |
|
23 | 25 | auto scalarSeries |
|
24 | 26 | = std::make_unique<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{}); |
|
25 | 27 | |
|
26 | 28 | auto dataIndex = 0; |
|
27 | 29 | for (auto time = start; time < end; ++time, ++dataIndex) { |
|
28 | 30 | scalarSeries->setData(dataIndex, time, std::cos(time)); |
|
29 | 31 | } |
|
30 | 32 | |
|
31 | 33 | return scalarSeries; |
|
32 | 34 | } |
|
35 | ||
|
36 | void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList) | |
|
37 | { | |
|
38 | // NOTE: Try to use multithread if possible | |
|
39 | foreach (const auto &dateTime, dateTimeList) { | |
|
40 | ||
|
41 | auto scalarSeries = this->retrieveDataSeries(dateTime); | |
|
42 | ||
|
43 | emit dataProvided(scalarSeries, dateTime); | |
|
44 | } | |
|
45 | } | |
|
46 | ||
|
47 | ||
|
48 | std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTime &dateTime) | |
|
49 | { | |
|
50 | ||
|
51 | // Gets the timerange from the parameters | |
|
52 | auto start = dateTime.m_TStart; | |
|
53 | auto end = dateTime.m_TEnd; | |
|
54 | ||
|
55 | // We assure that timerange is valid | |
|
56 | if (end < start) { | |
|
57 | std::swap(start, end); | |
|
58 | } | |
|
59 | ||
|
60 | // Generates scalar series containing cosinus values (one value per second) | |
|
61 | auto scalarSeries | |
|
62 | = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{}); | |
|
63 | ||
|
64 | auto dataIndex = 0; | |
|
65 | for (auto time = start; time < end; ++time, ++dataIndex) { | |
|
66 | scalarSeries->setData(dataIndex, time, std::cos(time)); | |
|
67 | } | |
|
68 | ||
|
69 | ||
|
70 | return scalarSeries; | |
|
71 | } |
General Comments 0
You need to be logged in to leave comments.
Login now