##// END OF EJS Templates
Merge branch 'feature/DateTimeOfVariableAtInit' into develop
perrinel -
r277:bc65aa8d09b9 merge
parent child
Show More
@@ -1,82 +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 QVector<double> data() const noexcept
47 const QVector<double> &data() const noexcept
48 48 {
49 return m_Data.at(0);
49 return m_Data[0];
50 50 }
51 51
52 52 /**
53 53 * @return the data as a vector
54 54 * @remarks this method is only available for a unidimensional ArrayData
55 55 */
56 56 template <int D = Dim, typename = std::enable_if_t<D == 1> >
57 57 const QVector<double> &data(double tStart, double tEnd) const noexcept
58 58 {
59 59 return m_Data.at(tStart);
60 60 }
61 61
62 62 // TODO Comment
63 63 template <int D = Dim, typename = std::enable_if_t<D == 1> >
64 void merge(ArrayData<1> *arrayData)
64 void merge(const ArrayData<1> &arrayData)
65 65 {
66 66 if (!m_Data.empty()) {
67 m_Data[0] += arrayData->data();
67 m_Data[0] += arrayData.data();
68 68 }
69 69 }
70 70
71 71 template <int D = Dim, typename = std::enable_if_t<D == 1> >
72 72 int size()
73 73 {
74 74 return m_Data[0].size();
75 75 }
76 76
77 77
78 78 private:
79 79 QVector<QVector<double> > m_Data;
80 80 };
81 81
82 82 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,70 +1,70
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 <QLoggingCategory>
8 8
9 9 #include <memory>
10 10
11 11
12 12 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSeries)
13 13 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries")
14 14
15 15
16 16 /**
17 17 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
18 18 *
19 19 * It proposes to set a dimension for the values ​​data
20 20 *
21 21 * @tparam Dim The dimension of the values data
22 22 *
23 23 */
24 24 template <int Dim>
25 25 class DataSeries : public IDataSeries {
26 26 public:
27 27 /// @sa IDataSeries::xAxisData()
28 28 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
29 29
30 30 /// @sa IDataSeries::xAxisUnit()
31 31 Unit xAxisUnit() const override { return m_XAxisUnit; }
32 32
33 33 /// @return the values dataset
34 34 std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
35 35
36 36 /// @sa IDataSeries::valuesUnit()
37 37 Unit valuesUnit() const override { return m_ValuesUnit; }
38 38
39 39 /// @sa IDataSeries::merge()
40 40 void merge(IDataSeries *dataSeries) override
41 41 {
42 42 if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
43 m_XAxisData->merge(dimDataSeries->xAxisData().get());
44 m_ValuesData->merge(dimDataSeries->valuesData().get());
43 m_XAxisData->merge(*dimDataSeries->xAxisData());
44 m_ValuesData->merge(*dimDataSeries->valuesData());
45 45 }
46 46 else {
47 47 qCWarning(LOG_DataSeries())
48 48 << QObject::tr("Dection of a type of IDataSeries we cannot merge with !");
49 49 }
50 50 }
51 51
52 52 protected:
53 53 /// Protected ctor (DataSeries is abstract)
54 54 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
55 55 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
56 56 : m_XAxisData{xAxisData},
57 57 m_XAxisUnit{xAxisUnit},
58 58 m_ValuesData{valuesData},
59 59 m_ValuesUnit{valuesUnit}
60 60 {
61 61 }
62 62
63 63 private:
64 64 std::shared_ptr<ArrayData<1> > m_XAxisData;
65 65 Unit m_XAxisUnit;
66 66 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
67 67 Unit m_ValuesUnit;
68 68 };
69 69
70 70 #endif // SCIQLOP_DATASERIES_H
@@ -1,28 +1,42
1 1 #ifndef SCIQLOP_SQPDATETIME_H
2 2 #define SCIQLOP_SQPDATETIME_H
3 3
4 4 #include <QObject>
5
6 #include <QDateTime>
7 #include <QDebug>
8
5 9 /**
6 10 * @brief The SqpDateTime struct holds the information of time parameters
7 11 */
8 12 struct SqpDateTime {
9 13 /// Start time
10 14 double m_TStart;
11 15 /// End time
12 16 double m_TEnd;
13 17
14 18 bool contains(const SqpDateTime &dateTime)
15 19 {
16 20 return (m_TStart <= dateTime.m_TStart && m_TEnd >= dateTime.m_TEnd);
17 21 }
18 22
19 23 bool intersect(const SqpDateTime &dateTime)
20 24 {
21 25 return (m_TEnd >= dateTime.m_TStart && m_TStart <= dateTime.m_TEnd);
22 26 }
23 27 };
24 28
29 inline QDebug operator<<(QDebug d, SqpDateTime obj)
30 {
31 auto tendDateTimeStart = QDateTime::fromMSecsSinceEpoch(obj.m_TStart * 1000);
32 auto tendDateTimeEnd = QDateTime::fromMSecsSinceEpoch(obj.m_TEnd * 1000);
33
34 // QDebug << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd;
35 d << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd;
36 return d;
37 }
38
25 39 // Required for using shared_ptr in signals/slots
26 40 Q_DECLARE_METATYPE(SqpDateTime)
27 41
28 42 #endif // SCIQLOP_SQPDATETIME_H
@@ -1,56 +1,56
1 1 #ifndef SCIQLOP_VARIABLE_H
2 2 #define SCIQLOP_VARIABLE_H
3 3
4 4 #include <Data/SqpDateTime.h>
5 5
6 6
7 7 #include <QLoggingCategory>
8 8 #include <QObject>
9 9
10 10 #include <Common/spimpl.h>
11 11
12 12 Q_DECLARE_LOGGING_CATEGORY(LOG_Variable)
13 13
14 14 class IDataSeries;
15 15 class QString;
16 16
17 17 /**
18 18 * @brief The Variable class represents a variable in SciQlop.
19 19 */
20 20 class Variable : public QObject {
21 21
22 22 Q_OBJECT
23 23
24 24 public:
25 25 explicit Variable(const QString &name, const QString &unit, const QString &mission,
26 26 const SqpDateTime &dateTime);
27 27
28 28 QString name() const noexcept;
29 29 QString mission() const noexcept;
30 30 QString unit() const noexcept;
31 31 SqpDateTime dateTime() const noexcept;
32 32 void setDateTime(const SqpDateTime &dateTime) noexcept;
33 33
34 34 /// @return the data of the variable, nullptr if there is no data
35 35 IDataSeries *dataSeries() const noexcept;
36 36
37 37 bool contains(const SqpDateTime &dateTime);
38 38 bool intersect(const SqpDateTime &dateTime);
39 39 void setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept;
40 40
41 41 public slots:
42 42 void onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
43 43
44 44 signals:
45 void dataCacheUpdated();
45 void updated();
46 46
47 47
48 48 private:
49 49 class VariablePrivate;
50 50 spimpl::unique_impl_ptr<VariablePrivate> impl;
51 51 };
52 52
53 53 // Required for using shared_ptr in signals/slots
54 54 Q_DECLARE_METATYPE(std::shared_ptr<Variable>)
55 55
56 56 #endif // SCIQLOP_VARIABLE_H
@@ -1,33 +1,40
1 1 #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECACHECONTROLLER_H
3 3
4 4 #include <QObject>
5 5
6 6 #include <Data/SqpDateTime.h>
7 7
8 #include <QLoggingCategory>
9
8 10 #include <Common/spimpl.h>
9 11
10 12 class Variable;
11 13
14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheController)
15
16
12 17 /// This class aims to store in the cache all of the dateTime already requested to the variable.
13 18 class VariableCacheController : public QObject {
14 19 Q_OBJECT
15 20 public:
16 21 explicit VariableCacheController(QObject *parent = 0);
17 22
18 23
19 24 void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
20 25
21 26 /// Return all of the SqpDataTime part of the dateTime whose are not in the cache
22 27 QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
23 28 const SqpDateTime &dateTime);
24 29
25 30
26 31 QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept;
27 32
33 void displayCache(std::shared_ptr<Variable> variable);
34
28 35 private:
29 36 class VariableCacheControllerPrivate;
30 37 spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl;
31 38 };
32 39
33 40 #endif // SCIQLOP_VARIABLECACHECONTROLLER_H
@@ -1,58 +1,57
1 1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECONTROLLER_H
3 3
4 4 #include <Data/SqpDateTime.h>
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QObject>
8 8
9 9 #include <Common/spimpl.h>
10 10
11 11
12 12 class IDataProvider;
13 13 class TimeController;
14 14 class Variable;
15 15 class VariableModel;
16 16
17 17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
18 18
19 19 /**
20 20 * @brief The VariableController class aims to handle the variables in SciQlop.
21 21 */
22 22 class VariableController : public QObject {
23 23 Q_OBJECT
24 24 public:
25 25 explicit VariableController(QObject *parent = 0);
26 26 virtual ~VariableController();
27 27
28 28 VariableModel *variableModel() noexcept;
29 29
30 30 void setTimeController(TimeController *timeController) noexcept;
31 31
32 32
33 /// Request the data loading of the variable whithin dateTime
34 void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
35
36 33 signals:
37 34 /// Signal emitted when a variable has been created
38 35 void variableCreated(std::shared_ptr<Variable> variable);
39 36
40 37 public slots:
38 /// Request the data loading of the variable whithin dateTime
39 void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
41 40 /**
42 41 * Creates a new variable and adds it to the model
43 42 * @param name the name of the new variable
44 43 * @param provider the data provider for the new variable
45 44 */
46 45 void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept;
47 46
48 47 void initialize();
49 48 void finalize();
50 49
51 50 private:
52 51 void waitForFinish();
53 52
54 53 class VariableControllerPrivate;
55 54 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
56 55 };
57 56
58 57 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,87 +1,87
1 1 #include "Variable/Variable.h"
2 2
3 3 #include <Data/IDataSeries.h>
4 4 #include <Data/SqpDateTime.h>
5 5
6 6 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
7 7
8 8 struct Variable::VariablePrivate {
9 9 explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission,
10 10 const SqpDateTime &dateTime)
11 11 : m_Name{name},
12 12 m_Unit{unit},
13 13 m_Mission{mission},
14 14 m_DateTime{dateTime},
15 15 m_DataSeries{nullptr}
16 16 {
17 17 }
18 18
19 19 QString m_Name;
20 20 QString m_Unit;
21 21 QString m_Mission;
22 22
23 23 SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache.
24 24 std::unique_ptr<IDataSeries> m_DataSeries;
25 25 };
26 26
27 27 Variable::Variable(const QString &name, const QString &unit, const QString &mission,
28 28 const SqpDateTime &dateTime)
29 29 : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission, dateTime)}
30 30 {
31 31 }
32 32
33 33 QString Variable::name() const noexcept
34 34 {
35 35 return impl->m_Name;
36 36 }
37 37
38 38 QString Variable::mission() const noexcept
39 39 {
40 40 return impl->m_Mission;
41 41 }
42 42
43 43 QString Variable::unit() const noexcept
44 44 {
45 45 return impl->m_Unit;
46 46 }
47 47
48 48 SqpDateTime Variable::dateTime() const noexcept
49 49 {
50 50 return impl->m_DateTime;
51 51 }
52 52
53 53 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
54 54 {
55 55 impl->m_DateTime = dateTime;
56 56 }
57 57
58 58 void Variable::setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept
59 59 {
60 60 if (!impl->m_DataSeries) {
61 61 impl->m_DataSeries = std::move(dataSeries);
62 62 }
63 63 }
64 64
65 65 void Variable::onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
66 66 {
67 67 if (impl->m_DataSeries) {
68 68 impl->m_DataSeries->merge(dataSeries.get());
69 69
70 emit dataCacheUpdated();
70 emit updated();
71 71 }
72 72 }
73 73
74 74 IDataSeries *Variable::dataSeries() const noexcept
75 75 {
76 76 return impl->m_DataSeries.get();
77 77 }
78 78
79 79 bool Variable::contains(const SqpDateTime &dateTime)
80 80 {
81 81 return impl->m_DateTime.contains(dateTime);
82 82 }
83 83
84 84 bool Variable::intersect(const SqpDateTime &dateTime)
85 85 {
86 86 return impl->m_DateTime.intersect(dateTime);
87 87 }
@@ -1,170 +1,183
1 1 #include "Variable/VariableCacheController.h"
2 2
3 3 #include "Variable/Variable.h"
4 4 #include <unordered_map>
5 5
6 Q_LOGGING_CATEGORY(LOG_VariableCacheController, "VariableCacheController")
7
6 8 struct VariableCacheController::VariableCacheControllerPrivate {
7 9
8 10 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> >
9 11 m_VariableToSqpDateTimeListMap;
10 12
11 13 void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
12 14 QVector<SqpDateTime> &notInCache, int cacheIndex,
13 15 double currentTStart);
14 16
15 17 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
16 18 QVector<SqpDateTime> &notInCache, int cacheIndex,
17 19 double currentTStart);
18 20
19 21
20 22 void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
21 23 int cacheIndex);
22 24 };
23 25
24 26
25 27 VariableCacheController::VariableCacheController(QObject *parent)
26 28 : QObject{parent}, impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()}
27 29 {
28 30 }
29 31
30 32 void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable,
31 33 const SqpDateTime &dateTime)
32 34 {
33 35 if (variable) {
34 36 auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable);
35 37 if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) {
36 38 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
37 39 }
38 40 else {
39 41
40 42 // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure
41 43 // that the list is ordered : l(0) < l(1). We assume also a < b
42 44 // (with a & b of type SqpDateTime) means ts(b) > te(a)
43 45
44 46 // The algorithm will try the merge of two interval:
45 47 // - dateTime will be compare with the first interval of the list:
46 48 // A: if it is inferior, it will be inserted and it's finished.
47 49 // B: if it is in intersection, it will be merge then the merged one
48 50 // will be compared to the next interval. The old one is remove from the list
49 51 // C: if it is superior, we do the same with the next interval of the list
50 52
51 53 impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
52 54 0);
53 55 }
54 56 }
55 57 }
56 58
57 59 QVector<SqpDateTime>
58 60 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
59 61 const SqpDateTime &dateTime)
60 62 {
61 63 auto notInCache = QVector<SqpDateTime>{};
62 64
63 65 // This algorithm is recursif. The idea is to localise the start time then the end time in the
64 66 // list of date time request associated to the variable
65 67 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
66 68 // (with a & b of type SqpDateTime) means ts(b) > te(a)
67 69
68 70 impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
69 71 notInCache, 0, dateTime.m_TStart);
70 72
71 73 return notInCache;
72 74 }
73 75
74 76 QVector<SqpDateTime>
75 77 VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept
76 78 {
77 79 return impl->m_VariableToSqpDateTimeListMap.at(variable);
78 80 }
79 81
80 82 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
81 83 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
82 84 {
83 85 const auto dateTimeListSize = dateTimeList.count();
84 86 if (cacheIndex >= dateTimeListSize) {
85 87 dateTimeList.push_back(dateTime);
86 88 // there is no anymore interval to compore, we can just push_back it
87 89 return;
88 90 }
89 91
90 92 auto currentDateTime = dateTimeList[cacheIndex];
91 93
92 94 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
93 95 // The compared one is < to current one compared, we can insert it
94 96 dateTimeList.insert(cacheIndex, dateTime);
95 97 }
96 98 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
97 99 // The compared one is > to current one compared we can comparet if to the next one
98 100 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
99 101 }
100 102 else {
101 103 // Merge cases: we need to merge the two interval, remove the old one from the list then
102 104 // rerun the algo from this index with the merged interval
103 105 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
104 106 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
105 107 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
106 108
107 109 dateTimeList.remove(cacheIndex);
108 110 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
109 111 }
110 112 }
111 113
112 114
113 115 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
114 116 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
115 117 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
116 118 {
117 119 const auto dateTimeListSize = dateTimeList.count();
118 120 if (cacheIndex >= dateTimeListSize) {
119 121 if (currentTStart < dateTime.m_TEnd) {
120 122
121 123 // te localised after all other interval: The last interval is [currentTsart, te]
122 124 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
123 125 }
124 126 return;
125 127 }
126 128
127 129 auto currentDateTimeJ = dateTimeList[cacheIndex];
128 130 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
129 131 // te localised between to interval: The last interval is [currentTsart, te]
130 132 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
131 133 }
132 134 else {
133 135 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
134 136 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
135 137 // te not localised before the current interval: we need to look at the next interval
136 138 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
137 139 currentDateTimeJ.m_TEnd);
138 140 }
139 141 }
140 142 }
141 143
142 144 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
143 145 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
144 146 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
145 147 {
146 148 const auto dateTimeListSize = dateTimeList.count();
147 149 if (cacheIndex >= dateTimeListSize) {
148 150 // ts localised after all other interval: The last interval is [ts, te]
149 151 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
150 152 return;
151 153 }
152 154
153 155 auto currentDateTimeI = dateTimeList[cacheIndex];
154 156 if (currentTStart < currentDateTimeI.m_TStart) {
155 157
156 158 // ts localised between to interval: let's localized te
157 159 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart);
158 160 }
159 else if (dateTime.m_TStart < currentDateTimeI.m_TEnd) {
160 // ts not localised before the current interval: we need to look at the next interval
161 // We can assume now current tstart is the last interval tend, because data between them are
162 // in the cache
163 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
164 currentDateTimeI.m_TEnd);
161 else if (currentTStart < currentDateTimeI.m_TEnd) {
162 if (dateTime.m_TEnd > 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
165 // are
166 // in the cache
167 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
168 currentDateTimeI.m_TEnd);
169 }
165 170 }
166 171 else {
167 172 // ts not localised before the current interval: we need to look at the next interval
168 173 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart);
169 174 }
170 175 }
176
177
178 void VariableCacheController::displayCache(std::shared_ptr<Variable> variable)
179 {
180 auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.at(variable);
181 qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache")
182 << variableDateTimeList;
183 }
@@ -1,164 +1,157
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3 #include <Variable/VariableController.h>
4 4 #include <Variable/VariableModel.h>
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <Data/IDataProvider.h>
8 8 #include <Data/IDataSeries.h>
9 9 #include <Time/TimeController.h>
10 10
11 11 #include <QDateTime>
12 #include <QElapsedTimer>
13 12 #include <QMutex>
14 13 #include <QThread>
15 14
16 15 #include <unordered_map>
17 16
18 17 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 18
20 19 namespace {
21 20
22 21 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 22 /// will be deleted when the timerange is recovered from SciQlop
24 23 std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 24 const SqpDateTime &dateTime) noexcept
26 25 {
27 26 auto parameters = DataProviderParameters{dateTime};
28 27
29 28 return provider.retrieveData(parameters);
30 29 }
31 30
32 31 } // namespace
33 32
34 33 struct VariableController::VariableControllerPrivate {
35 34 explicit VariableControllerPrivate(VariableController *parent)
36 35 : m_WorkingMutex{},
37 36 m_VariableModel{new VariableModel{parent}},
38 37 m_VariableCacheController{std::make_unique<VariableCacheController>()}
39 38 {
40 39 }
41 40
42 41 QMutex m_WorkingMutex;
43 42 /// Variable model. The VariableController has the ownership
44 43 VariableModel *m_VariableModel;
45 44
46 45
47 46 TimeController *m_TimeController{nullptr};
48 47 std::unique_ptr<VariableCacheController> m_VariableCacheController;
49 48
50 49 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
51 50 m_VariableToProviderMap;
52 51 };
53 52
54 53 VariableController::VariableController(QObject *parent)
55 54 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
56 55 {
57 56 qCDebug(LOG_VariableController()) << tr("VariableController construction")
58 57 << QThread::currentThread();
59 58 }
60 59
61 60 VariableController::~VariableController()
62 61 {
63 62 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
64 63 << QThread::currentThread();
65 64 this->waitForFinish();
66 65 }
67 66
68 67 VariableModel *VariableController::variableModel() noexcept
69 68 {
70 69 return impl->m_VariableModel;
71 70 }
72 71
73 72 void VariableController::setTimeController(TimeController *timeController) noexcept
74 73 {
75 74 impl->m_TimeController = timeController;
76 75 }
77 76
78 77 void VariableController::createVariable(const QString &name,
79 78 std::shared_ptr<IDataProvider> provider) noexcept
80 79 {
81 80
82 81 if (!impl->m_TimeController) {
83 82 qCCritical(LOG_VariableController())
84 83 << tr("Impossible to create variable: The time controller is null");
85 84 return;
86 85 }
87 86
88 87
89 88 /// @todo : for the moment :
90 89 /// - the provider is only used to retrieve data from the variable for its initialization, but
91 90 /// it will be retained later
92 91 /// - default data are generated for the variable, without taking into account the timerange set
93 92 /// in sciqlop
94 93 auto dateTime = impl->m_TimeController->dateTime();
95 94 if (auto newVariable = impl->m_VariableModel->createVariable(
96 95 name, dateTime, generateDefaultDataSeries(*provider, dateTime))) {
97 96
98 97 // store the provider
99 98 impl->m_VariableToProviderMap[newVariable] = provider;
100 99 qRegisterMetaType<std::shared_ptr<IDataSeries> >();
101 100 qRegisterMetaType<SqpDateTime>();
102 101 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
103 102 &Variable::onAddDataSeries);
104 103
105 104
106 105 // store in cache
107 106 impl->m_VariableCacheController->addDateTime(newVariable, dateTime);
108 107
109 108 // notify the creation
110 109 emit variableCreated(newVariable);
111 110 }
112 111 }
113 112
114 113
115 void VariableController::requestDataLoading(std::shared_ptr<Variable> variable,
116 const SqpDateTime &dateTime)
114 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
115 const SqpDateTime &dateTime)
117 116 {
118 117 // we want to load data of the variable for the dateTime.
119 118 // First we check if the cache contains some of them.
120 119 // For the other, we ask the provider to give them.
121 120 if (variable) {
122 121
123 QElapsedTimer timer;
124 timer.start();
125 qCInfo(LOG_VariableController()) << "TORM: The slow s0 operation took" << timer.elapsed()
126 << "milliseconds";
127 122 auto dateTimeListNotInCache
128 123 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
129 qCInfo(LOG_VariableController()) << "TORM: The slow s1 operation took" << timer.elapsed()
130 << "milliseconds";
131 124
132 // Ask the provider for each data on the dateTimeListNotInCache
133 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(dateTimeListNotInCache);
134
135 qCInfo(LOG_VariableController()) << "TORM: 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()) << "TORM: The slow s3 operation took" << timer.elapsed()
141 << "milliseconds";
125 if (!dateTimeListNotInCache.empty()) {
126 // Ask the provider for each data on the dateTimeListNotInCache
127 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
128 std::move(dateTimeListNotInCache));
129 // store in cache
130 impl->m_VariableCacheController->addDateTime(variable, dateTime);
131 }
132 else {
133 emit variable->updated();
134 }
142 135 }
143 136 else {
144 137 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
145 138 }
146 139 }
147 140
148 141
149 142 void VariableController::initialize()
150 143 {
151 144 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
152 145 impl->m_WorkingMutex.lock();
153 146 qCDebug(LOG_VariableController()) << tr("VariableController init END");
154 147 }
155 148
156 149 void VariableController::finalize()
157 150 {
158 151 impl->m_WorkingMutex.unlock();
159 152 }
160 153
161 154 void VariableController::waitForFinish()
162 155 {
163 156 QMutexLocker locker{&impl->m_WorkingMutex};
164 157 }
@@ -1,343 +1,353
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3
4 4 #include <QObject>
5 5 #include <QtTest>
6 6
7 7 #include <memory>
8 8
9 9 class TestVariableCacheController : public QObject {
10 10 Q_OBJECT
11 11
12 12 private slots:
13 13 void testProvideNotInCacheDateTimeList();
14 14
15 15 void testAddDateTime();
16 16 };
17 17
18 18
19 19 void TestVariableCacheController::testProvideNotInCacheDateTimeList()
20 20 {
21 21 VariableCacheController variableCacheController{};
22 22
23 23 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
24 24 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
25 25 auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()),
26 26 static_cast<double>(te0.toMSecsSinceEpoch())};
27 27
28 28 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
29 29 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
30 30 auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()),
31 31 static_cast<double>(te1.toMSecsSinceEpoch())};
32 32
33 33 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
34 34 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
35 35 auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()),
36 36 static_cast<double>(te2.toMSecsSinceEpoch())};
37 37
38 38 auto var0 = std::make_shared<Variable>("", "", "", sqp0);
39 39
40 40 variableCacheController.addDateTime(var0, sqp0);
41 41 variableCacheController.addDateTime(var0, sqp1);
42 42 variableCacheController.addDateTime(var0, sqp2);
43 43
44 44 // first case [ts,te] < ts0
45 45 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
46 46 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
47 47 auto sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
48 48 static_cast<double>(te.toMSecsSinceEpoch())};
49 49
50 50
51 51 auto notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
52 52
53 53 QCOMPARE(notInCach.size(), 1);
54 54 auto notInCacheSqp = notInCach.first();
55 55 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
56 56 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
57 57
58 58
59 59 // second case ts < ts0 && ts0 < te <= te0
60 60 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
61 61 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
62 62 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
63 63 static_cast<double>(te.toMSecsSinceEpoch())};
64 64
65 65
66 66 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
67 67
68 68 QCOMPARE(notInCach.size(), 1);
69 69 notInCacheSqp = notInCach.first();
70 70 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
71 71 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
72 72
73 73 // 3th case ts < ts0 && te0 < te <= ts1
74 74 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
75 75 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
76 76 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
77 77 static_cast<double>(te.toMSecsSinceEpoch())};
78 78
79 79
80 80 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
81 81
82 82 QCOMPARE(notInCach.size(), 2);
83 83 notInCacheSqp = notInCach.first();
84 84 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
85 85 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
86 86
87 87 notInCacheSqp = notInCach.at(1);
88 88 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
89 89 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
90 90
91 91 // 4th case ts < ts0 && ts1 < te <= te1
92 92 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
93 93 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
94 94 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
95 95 static_cast<double>(te.toMSecsSinceEpoch())};
96 96
97 97
98 98 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
99 99
100 100 QCOMPARE(notInCach.size(), 2);
101 101 notInCacheSqp = notInCach.first();
102 102 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
103 103 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
104 104
105 105 notInCacheSqp = notInCach.at(1);
106 106 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
107 107 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
108 108
109 109 // 5th case ts < ts0 && te3 < te
110 110 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
111 111 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
112 112 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
113 113 static_cast<double>(te.toMSecsSinceEpoch())};
114 114
115 115
116 116 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
117 117
118 118 QCOMPARE(notInCach.size(), 4);
119 119 notInCacheSqp = notInCach.first();
120 120 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
121 121 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
122 122
123 123 notInCacheSqp = notInCach.at(1);
124 124 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
125 125 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
126 126
127 127 notInCacheSqp = notInCach.at(2);
128 128 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
129 129 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
130 130
131 131 notInCacheSqp = notInCach.at(3);
132 132 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
133 133 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
134 134
135 135
136 136 // 6th case ts2 < ts
137 137 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 45, 0, 0}};
138 138 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
139 139 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
140 140 static_cast<double>(te.toMSecsSinceEpoch())};
141 141
142 142
143 143 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
144 144
145 145 QCOMPARE(notInCach.size(), 1);
146 146 notInCacheSqp = notInCach.first();
147 147 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
148 148 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
149 149
150 150 // 7th case ts = te0 && te < ts1
151 151 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
152 152 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
153 153 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
154 154 static_cast<double>(te.toMSecsSinceEpoch())};
155 155
156 156
157 157 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
158 158
159 159 QCOMPARE(notInCach.size(), 1);
160 160 notInCacheSqp = notInCach.first();
161 161 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
162 162 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
163 163
164 164 // 8th case ts0 < ts < te0 && te < ts1
165 165 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
166 166 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
167 167 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
168 168 static_cast<double>(te.toMSecsSinceEpoch())};
169 169
170 170
171 171 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
172 172
173 173 QCOMPARE(notInCach.size(), 1);
174 174 notInCacheSqp = notInCach.first();
175 175 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
176 176 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
177 177
178 178 // 9th case ts0 < ts < te0 && ts1 < te < te1
179 179 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
180 180 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
181 181 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
182 182 static_cast<double>(te.toMSecsSinceEpoch())};
183 183
184 184
185 185 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
186 186
187 187 QCOMPARE(notInCach.size(), 1);
188 188 notInCacheSqp = notInCach.first();
189 189 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
190 190 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
191 191
192 192 // 10th case te1 < ts < te < ts2
193 193 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 9, 0, 0}};
194 194 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
195 195 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
196 196 static_cast<double>(te.toMSecsSinceEpoch())};
197 197
198 198
199 199 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
200 200
201 201 QCOMPARE(notInCach.size(), 1);
202 202 notInCacheSqp = notInCach.first();
203 203 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
204 204 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
205 205
206 206 // 11th case te0 < ts < ts1 && te3 < te
207 207 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
208 208 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
209 209 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
210 210 static_cast<double>(te.toMSecsSinceEpoch())};
211 211
212 212
213 213 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
214 214
215 215 QCOMPARE(notInCach.size(), 3);
216 216 notInCacheSqp = notInCach.first();
217 217 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
218 218 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
219 219
220 220 notInCacheSqp = notInCach.at(1);
221 221 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
222 222 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
223 223
224 224 notInCacheSqp = notInCach.at(2);
225 225 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
226 226 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
227 227
228 228 // 12th case te0 < ts < ts1 && te3 < te
229 229 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
230 230 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
231 231 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
232 232 static_cast<double>(te.toMSecsSinceEpoch())};
233 233
234 234 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
235 235
236 236 QCOMPARE(notInCach.size(), 2);
237 237 notInCacheSqp = notInCach.first();
238 238 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
239 239 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
240 240
241 241 notInCacheSqp = notInCach.at(1);
242 242 QCOMPARE(notInCacheSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
243 243 QCOMPARE(notInCacheSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
244
245
246 // 12th case ts0 < ts < te0
247 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 10, 0}};
248 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 50, 0}};
249 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
250 static_cast<double>(te.toMSecsSinceEpoch())};
251
252 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
253 QCOMPARE(notInCach.size(), 0);
244 254 }
245 255
246 256
247 257 void TestVariableCacheController::testAddDateTime()
248 258 {
249 259 VariableCacheController variableCacheController{};
250 260
251 261 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
252 262 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
253 263 auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()),
254 264 static_cast<double>(te0.toMSecsSinceEpoch())};
255 265
256 266 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
257 267 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
258 268 auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()),
259 269 static_cast<double>(te1.toMSecsSinceEpoch())};
260 270
261 271 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
262 272 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
263 273 auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()),
264 274 static_cast<double>(te2.toMSecsSinceEpoch())};
265 275
266 276 auto ts01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
267 277 auto te01 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
268 278 auto sqp01 = SqpDateTime{static_cast<double>(ts01.toMSecsSinceEpoch()),
269 279 static_cast<double>(te01.toMSecsSinceEpoch())};
270 280
271 281 auto ts3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 14, 0, 0}};
272 282 auto te3 = QDateTime{QDate{2017, 01, 01}, QTime{2, 16, 0, 0}};
273 283 auto sqp3 = SqpDateTime{static_cast<double>(ts3.toMSecsSinceEpoch()),
274 284 static_cast<double>(te3.toMSecsSinceEpoch())};
275 285
276 286 auto ts03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
277 287 auto te03 = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
278 288 auto sqp03 = SqpDateTime{static_cast<double>(ts03.toMSecsSinceEpoch()),
279 289 static_cast<double>(te03.toMSecsSinceEpoch())};
280 290
281 291
282 292 auto var0 = std::make_shared<Variable>("", "", "", sqp0);
283 293
284 294
285 295 // First case: add the first interval to the variable :sqp0
286 296 variableCacheController.addDateTime(var0, sqp0);
287 297 auto dateCacheList = variableCacheController.dateCacheList(var0);
288 298 QCOMPARE(dateCacheList.count(), 1);
289 299 auto dateCache = dateCacheList.at(0);
290 300 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
291 301 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
292 302
293 303 // 2nd case: add a second interval : sqp1 > sqp0
294 304 variableCacheController.addDateTime(var0, sqp1);
295 305 dateCacheList = variableCacheController.dateCacheList(var0);
296 306 QCOMPARE(dateCacheList.count(), 2);
297 307 dateCache = dateCacheList.at(0);
298 308 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
299 309 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te0.toMSecsSinceEpoch()));
300 310
301 311 dateCache = dateCacheList.at(1);
302 312 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts1.toMSecsSinceEpoch()));
303 313 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
304 314
305 315 // 3th case: merge sqp0 & sqp1 with sqp01
306 316 variableCacheController.addDateTime(var0, sqp01);
307 317 dateCacheList = variableCacheController.dateCacheList(var0);
308 318 QCOMPARE(dateCacheList.count(), 1);
309 319 dateCache = dateCacheList.at(0);
310 320 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
311 321 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
312 322
313 323
314 324 // 4th case: add a second interval : sqp1 > sqp0
315 325 variableCacheController.addDateTime(var0, sqp2);
316 326 variableCacheController.addDateTime(var0, sqp3);
317 327 dateCacheList = variableCacheController.dateCacheList(var0);
318 328 QCOMPARE(dateCacheList.count(), 3);
319 329 dateCache = dateCacheList.at(0);
320 330 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
321 331 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te1.toMSecsSinceEpoch()));
322 332
323 333 dateCache = dateCacheList.at(1);
324 334 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts3.toMSecsSinceEpoch()));
325 335 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te3.toMSecsSinceEpoch()));
326 336
327 337 dateCache = dateCacheList.at(2);
328 338 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts2.toMSecsSinceEpoch()));
329 339 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te2.toMSecsSinceEpoch()));
330 340
331 341
332 342 // 5th case: merge all interval
333 343 variableCacheController.addDateTime(var0, sqp03);
334 344 dateCacheList = variableCacheController.dateCacheList(var0);
335 345 QCOMPARE(dateCacheList.count(), 1);
336 346 dateCache = dateCacheList.at(0);
337 347 QCOMPARE(dateCache.m_TStart, static_cast<double>(ts0.toMSecsSinceEpoch()));
338 348 QCOMPARE(dateCache.m_TEnd, static_cast<double>(te03.toMSecsSinceEpoch()));
339 349 }
340 350
341 351
342 352 QTEST_MAIN(TestVariableCacheController)
343 353 #include "TestVariableCacheController.moc"
@@ -1,59 +1,63
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 15 class QCPRange;
16 class SqpDateTime;
16 17 class Variable;
17 18
18 19 namespace Ui {
19 20 class VisualizationGraphWidget;
20 21 } // namespace Ui
21 22
22 23 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
23 24 Q_OBJECT
24 25
25 26 public:
26 27 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
27 28 virtual ~VisualizationGraphWidget();
28 29
29 30 void addVariable(std::shared_ptr<Variable> variable);
30 31 /// Removes a variable from the graph
31 32 void removeVariable(std::shared_ptr<Variable> variable) noexcept;
32 33
33 34 // IVisualizationWidget interface
34 35 void accept(IVisualizationWidgetVisitor *visitor) override;
35 36 bool canDrop(const Variable &variable) const override;
36 37 QString name() const override;
37 38
38 39 void updateDisplay(std::shared_ptr<Variable> variable);
39 40
41 signals:
42 void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
43
40 44
41 45 private:
42 46 Ui::VisualizationGraphWidget *ui;
43 47
44 48 class VisualizationGraphWidgetPrivate;
45 49 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
46 50
47 51 private slots:
48 52 /// Slot called when right clicking on the graph (displays a menu)
49 53 void onGraphMenuRequested(const QPoint &pos) noexcept;
50 54
51 55 void onRangeChanged(const QCPRange &t1, const QCPRange &t2);
52 56
53 57 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
54 58 void onMouseWheel(QWheelEvent *event) noexcept;
55 59
56 60 void onDataCacheVariableUpdated();
57 61 };
58 62
59 63 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
@@ -1,151 +1,153
1 1 #include "Visualization/VisualizationGraphHelper.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 8 #include <QElapsedTimer>
9 9
10 10 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
11 11
12 12 namespace {
13 13
14 14 /// Format for datetimes on a axis
15 15 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
16 16
17 17 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
18 18 /// non-time data
19 19 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
20 20 {
21 21 if (isTimeAxis) {
22 22 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
23 23 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
24 24
25 25 return dateTicker;
26 26 }
27 27 else {
28 28 // default ticker
29 29 return QSharedPointer<QCPAxisTicker>::create();
30 30 }
31 31 }
32 32
33 33 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
34 34 const SqpDateTime &dateTime)
35 35 {
36 36 QElapsedTimer timer;
37 37 timer.start();
38 38 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
39 39 // Clean the graph
40 qCDebug(LOG_VisualizationGraphHelper()) << "The slow s1 operation took" << timer.elapsed()
41 << "milliseconds";
42 40 // NAIVE approch
43 41 const auto &xData = scalarSeries.xAxisData()->data();
44 42 const auto &valuesData = scalarSeries.valuesData()->data();
43 const auto count = xData.count();
44 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count();
45 45
46 auto xValue = QVector<double>();
47 auto vValue = QVector<double>();
46 auto xValue = QVector<double>(count);
47 auto vValue = QVector<double>(count);
48 48
49 const auto count = xData.count();
50 auto ite = 0;
49 int n = 0;
51 50 for (auto i = 0; i < count; ++i) {
52 const auto x = xData.at(i);
51 const auto x = xData[i];
53 52 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
54 xValue.push_back(x);
55 vValue.push_back(valuesData.at(i));
56 ++ite;
53 xValue[n] = x;
54 vValue[n] = valuesData[i];
55 ++n;
57 56 }
58 57 }
59 58
60 qcpGraph->setData(xValue, vValue);
59 xValue.resize(n);
60 vValue.resize(n);
61 61
62 qCDebug(LOG_VisualizationGraphHelper()) << "The slow s2 operation took" << timer.elapsed()
63 << "milliseconds";
62 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
63 << xValue.count();
64
65 qcpGraph->setData(xValue, vValue);
64 66 }
65 67 else {
66 68 /// @todo DEBUG
67 69 }
68 70 }
69 71
70 72 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
71 73 const SqpDateTime &dateTime)
72 74 {
73 75 auto component = plot.addGraph();
74 76
75 77 if (component) {
76 78 // // Graph data
77 79 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
78 80 true);
79 81
80 82 updateScalarData(component, scalarSeries, dateTime);
81 83
82 84 // Axes properties
83 85 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
84 86 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
85 87
86 88 auto setAxisProperties = [](auto axis, const auto &unit) {
87 89 // label (unit name)
88 90 axis->setLabel(unit.m_Name);
89 91
90 92 // ticker (depending on the type of unit)
91 93 axis->setTicker(axisTicker(unit.m_TimeUnit));
92 94 };
93 95 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
94 96 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
95 97
96 98 // Display all data
97 99 component->rescaleAxes();
98 100
99 101 plot.replot();
100 102 }
101 103 else {
102 104 qCDebug(LOG_VisualizationGraphHelper())
103 105 << QObject::tr("Can't create graph for the scalar series");
104 106 }
105 107
106 108 return component;
107 109 }
108 110
109 111 } // namespace
110 112
111 113 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
112 114 QCustomPlot &plot) noexcept
113 115 {
114 116 auto result = QVector<QCPAbstractPlottable *>{};
115 117
116 118 if (variable) {
117 119 // Gets the data series of the variable to call the creation of the right components
118 120 // according to its type
119 121 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
120 122 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
121 123 }
122 124 else {
123 125 qCDebug(LOG_VisualizationGraphHelper())
124 126 << QObject::tr("Can't create graph plottables : unmanaged data series type");
125 127 }
126 128 }
127 129 else {
128 130 qCDebug(LOG_VisualizationGraphHelper())
129 131 << QObject::tr("Can't create graph plottables : the variable is null");
130 132 }
131 133
132 134 return result;
133 135 }
134 136
135 137 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
136 138 IDataSeries *dataSeries, const SqpDateTime &dateTime)
137 139 {
138 140 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
139 141 if (plotableVect.size() == 1) {
140 142 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
141 143 }
142 144 else {
143 145 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
144 146 "Can't update Data of a scalarSeries because there is not only one component "
145 147 "associated");
146 148 }
147 149 }
148 150 else {
149 151 /// @todo DEBUG
150 152 }
151 153 }
@@ -1,237 +1,236
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 3 #include "Visualization/VisualizationGraphHelper.h"
4 4 #include "ui_VisualizationGraphWidget.h"
5 5
6 6 #include <Data/ArrayData.h>
7 7 #include <Data/IDataSeries.h>
8 8 #include <SqpApplication.h>
9 9 #include <Variable/Variable.h>
10 10 #include <Variable/VariableController.h>
11 11
12 12 #include <unordered_map>
13 13
14 14 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
15 15
16 16 namespace {
17 17
18 18 /// Key pressed to enable zoom on horizontal axis
19 19 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
20 20
21 21 /// Key pressed to enable zoom on vertical axis
22 22 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
23 23
24 24 } // namespace
25 25
26 26 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
27 27
28 28 // 1 variable -> n qcpplot
29 29 std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap;
30 30 };
31 31
32 32 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
33 33 : QWidget{parent},
34 34 ui{new Ui::VisualizationGraphWidget},
35 35 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
36 36 {
37 37 ui->setupUi(this);
38 38
39 39 ui->graphNameLabel->setText(name);
40 40
41 41 // 'Close' options : widget is deleted when closed
42 42 setAttribute(Qt::WA_DeleteOnClose);
43 43 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
44 44 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
45 45
46 46 // Set qcpplot properties :
47 47 // - Drag (on x-axis) and zoom are enabled
48 48 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
49 49 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
50 50 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
51 51 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
52 52 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
53 53 &QCPAxis::rangeChanged),
54 54 this, &VisualizationGraphWidget::onRangeChanged);
55 55
56 56 // Activates menu when right clicking on the graph
57 57 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
58 58 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
59 59 &VisualizationGraphWidget::onGraphMenuRequested);
60
61 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
62 &VariableController::onRequestDataLoading);
60 63 }
61 64
62 65
63 66 VisualizationGraphWidget::~VisualizationGraphWidget()
64 67 {
65 68 delete ui;
66 69 }
67 70
68 71 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
69 72 {
70 73 // Uses delegate to create the qcpplot components according to the variable
71 74 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
72 75
73 76 for (auto createdPlottable : qAsConst(createdPlottables)) {
74 77 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
75 78 }
76 79
77 connect(variable.get(), SIGNAL(dataCacheUpdated()), this, SLOT(onDataCacheVariableUpdated()));
80 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
78 81 }
79 82
80 83 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
81 84 {
82 85 // Each component associated to the variable :
83 86 // - is removed from qcpplot (which deletes it)
84 87 // - is no longer referenced in the map
85 88 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
86 89 for (auto it = componentsIt.first; it != componentsIt.second;) {
87 90 ui->widget->removePlottable(it->second);
88 91 it = impl->m_VariableToPlotMultiMap.erase(it);
89 92 }
90 93
91 94 // Updates graph
92 95 ui->widget->replot();
93 96 }
94 97
95 98 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
96 99 {
97 100 if (visitor) {
98 101 visitor->visit(this);
99 102 }
100 103 else {
101 104 qCCritical(LOG_VisualizationGraphWidget())
102 105 << tr("Can't visit widget : the visitor is null");
103 106 }
104 107 }
105 108
106 109 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
107 110 {
108 111 /// @todo : for the moment, a graph can always accomodate a variable
109 112 Q_UNUSED(variable);
110 113 return true;
111 114 }
112 115
113 116 QString VisualizationGraphWidget::name() const
114 117 {
115 118 return ui->graphNameLabel->text();
116 119 }
117 120
118 121 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
119 122 {
120 123 QMenu graphMenu{};
121 124
122 125 // Iterates on variables (unique keys)
123 126 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
124 127 end = impl->m_VariableToPlotMultiMap.cend();
125 128 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
126 129 // 'Remove variable' action
127 130 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
128 131 [ this, var = it->first ]() { removeVariable(var); });
129 132 }
130 133
131 134 if (!graphMenu.isEmpty()) {
132 135 graphMenu.exec(mapToGlobal(pos));
133 136 }
134 137 }
135 138
136 139 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
137 140 {
138 141
139 142 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
140 143
141 144 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
142 145 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
143 146
144 147 auto variable = it->first;
145 qCInfo(LOG_VisualizationGraphWidget())
146 << tr("TORM: VisualizationGraphWidget::onRangeChanged")
147 << variable->dataSeries()->xAxisData()->size();
148 148 auto dateTime = SqpDateTime{t2.lower, t2.upper};
149 149
150 150 if (!variable->contains(dateTime)) {
151 151
152 152 auto variableDateTimeWithTolerance = dateTime;
153 153 if (variable->intersect(dateTime)) {
154 154 auto variableDateTime = variable->dateTime();
155 155 if (variableDateTime.m_TStart < dateTime.m_TStart) {
156 156
157 157 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
158 158 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
159 159 // Tolerance have to be added to the right
160 160 // add 10% tolerance for right (end) side
161 161 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
162 162 variableDateTimeWithTolerance.m_TEnd += tolerance;
163 163 }
164 164 if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
165 165 auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart;
166 166 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
167 167 // Tolerance have to be added to the left
168 168 // add 10% tolerance for left (start) side
169 169 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
170 170 variableDateTimeWithTolerance.m_TStart -= tolerance;
171 171 }
172 172 }
173 173 else {
174 174 // add 10% tolerance for each side
175 175 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
176 176 variableDateTimeWithTolerance.m_TStart -= tolerance;
177 177 variableDateTimeWithTolerance.m_TEnd += tolerance;
178 178 }
179 179 variable->setDateTime(dateTime);
180 180
181 181 // CHangement detected, we need to ask controller to request data loading
182 sqpApp->variableController().requestDataLoading(variable,
183 variableDateTimeWithTolerance);
182 emit requestDataLoading(variable, variableDateTimeWithTolerance);
184 183 }
185 184 }
186 185 }
187 186
188 187 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
189 188 {
190 189 auto zoomOrientations = QFlags<Qt::Orientation>{};
191 190
192 191 // Lambda that enables a zoom orientation if the key modifier related to this orientation
193 192 // has
194 193 // been pressed
195 194 auto enableOrientation
196 195 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
197 196 auto orientationEnabled = event->modifiers().testFlag(modifier);
198 197 zoomOrientations.setFlag(orientation, orientationEnabled);
199 198 };
200 199 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
201 200 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
202 201
203 202 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
204 203 }
205 204
206 205 void VisualizationGraphWidget::onDataCacheVariableUpdated()
207 206 {
208 207 // NOTE:
209 208 // We don't want to call the method for each component of a variable unitarily, but for
210 209 // all
211 210 // its components at once (eg its three components in the case of a vector).
212 211
213 212 // The unordered_multimap does not do this easily, so the question is whether to:
214 213 // - use an ordered_multimap and the algos of std to group the values by key
215 214 // - use a map (unique keys) and store as values directly the list of components
216 215
217 216 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
218 217 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
219 218 auto variable = it->first;
220 219 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
221 220 variable->dataSeries(), variable->dateTime());
222 221 }
223 222 }
224 223
225 224 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
226 225 {
227 226 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
228 227
229 228 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
230 229
231 230 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
232 231 abstractPlotableVect.push_back(it->second);
233 232 }
234 233
235 234 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
236 235 variable->dateTime());
237 236 }
@@ -1,71 +1,74
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 #include <QDateTime>
9
8 10 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
9 11
10 12 std::unique_ptr<IDataSeries>
11 13 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
12 14 {
13 15 auto dateTime = parameters.m_Time;
14 16
15 17 // Gets the timerange from the parameters
16 18 auto start = dateTime.m_TStart;
17 19 auto end = dateTime.m_TEnd;
18 20
21
19 22 // We assure that timerange is valid
20 23 if (end < start) {
21 24 std::swap(start, end);
22 25 }
23 26
24 27 // Generates scalar series containing cosinus values (one value per second)
25 28 auto scalarSeries
26 29 = std::make_unique<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
27 30
28 31 auto dataIndex = 0;
29 32 for (auto time = start; time < end; ++time, ++dataIndex) {
30 33 scalarSeries->setData(dataIndex, time, std::cos(time));
31 34 }
32 35
33 36 return scalarSeries;
34 37 }
35 38
36 39 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
37 40 {
38 41 // NOTE: Try to use multithread if possible
39 42 for (const auto &dateTime : dateTimeList) {
40 43
41 44 auto scalarSeries = this->retrieveDataSeries(dateTime);
42 45
43 46 emit dataProvided(scalarSeries, dateTime);
44 47 }
45 48 }
46 49
47 50
48 51 std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTime &dateTime)
49 52 {
53 auto dataIndex = 0;
50 54
51 55 // Gets the timerange from the parameters
52 auto start = dateTime.m_TStart;
53 auto end = dateTime.m_TEnd;
56 double freq = 100.0;
57 double start = dateTime.m_TStart * freq; // 100 htz
58 double end = dateTime.m_TEnd * freq; // 100 htz
54 59
55 60 // We assure that timerange is valid
56 61 if (end < start) {
57 62 std::swap(start, end);
58 63 }
59 64
60 65 // Generates scalar series containing cosinus values (one value per second)
61 66 auto scalarSeries
62 67 = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
63 68
64 auto dataIndex = 0;
65 69 for (auto time = start; time < end; ++time, ++dataIndex) {
66 scalarSeries->setData(dataIndex, time, std::cos(time));
70 const auto timeOnFreq = time / freq;
71 scalarSeries->setData(dataIndex, timeOnFreq, std::cos(timeOnFreq));
67 72 }
68
69
70 73 return scalarSeries;
71 74 }
General Comments 0
You need to be logged in to leave comments. Login now