##// END OF EJS Templates
Merge branch 'feature/ThreadFix' into develop
perrinel -
r371:684b9afd5f2a merge
parent child
Show More
@@ -1,82 +1,108
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 #include <QReadLocker>
5 #include <QReadWriteLock>
4 6 #include <QVector>
5
6 7 /**
7 8 * @brief The ArrayData class represents a dataset for a data series.
8 9 *
9 10 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
10 11 * template-parameter.
11 12 *
12 13 * @tparam Dim the dimension of the ArrayData (one or two)
13 14 * @sa IDataSeries
14 15 */
15 16 template <int Dim>
16 17 class ArrayData {
17 18 public:
18 19 /**
19 20 * Ctor for a unidimensional ArrayData
20 21 * @param nbColumns the number of values the ArrayData will hold
21 22 */
22 23 template <int D = Dim, typename = std::enable_if_t<D == 1> >
23 24 explicit ArrayData(int nbColumns) : m_Data{1, QVector<double>{}}
24 25 {
26 QWriteLocker locker(&m_Lock);
25 27 m_Data[0].resize(nbColumns);
26 28 }
27 29
28 30 /**
31 * Ctor for a unidimensional ArrayData
32 * @param nbColumns the number of values the ArrayData will hold
33 */
34 explicit ArrayData(const ArrayData &other)
35 {
36 QReadLocker otherLocker(&other.m_Lock);
37 QWriteLocker locker(&m_Lock);
38 m_Data = other.m_Data;
39 }
40
41 /**
29 42 * Sets a data at a specified index. The index has to be valid to be effective
30 43 * @param index the index to which the data will be set
31 44 * @param data the data to set
32 45 * @remarks this method is only available for a unidimensional ArrayData
33 46 */
34 47 template <int D = Dim, typename = std::enable_if_t<D == 1> >
35 48 void setData(int index, double data) noexcept
36 49 {
50 QWriteLocker locker(&m_Lock);
37 51 if (index >= 0 && index < m_Data.at(0).size()) {
38 52 m_Data[0].replace(index, data);
39 53 }
40 54 }
41 55
42 56 /**
43 57 * @return the data as a vector
44 58 * @remarks this method is only available for a unidimensional ArrayData
45 59 */
46 60 template <int D = Dim, typename = std::enable_if_t<D == 1> >
47 const QVector<double> &data() const noexcept
61 QVector<double> data() const noexcept
48 62 {
63 QReadLocker locker(&m_Lock);
49 64 return m_Data[0];
50 65 }
51 66
52 67 /**
53 68 * @return the data as a vector
54 69 * @remarks this method is only available for a unidimensional ArrayData
55 70 */
56 71 template <int D = Dim, typename = std::enable_if_t<D == 1> >
57 const QVector<double> &data(double tStart, double tEnd) const noexcept
72 QVector<double> data(double tStart, double tEnd) const noexcept
58 73 {
74 QReadLocker locker(&m_Lock);
59 75 return m_Data.at(tStart);
60 76 }
61 77
62 78 // TODO Comment
63 79 template <int D = Dim, typename = std::enable_if_t<D == 1> >
64 80 void merge(const ArrayData<1> &arrayData)
65 81 {
82 QWriteLocker locker(&m_Lock);
66 83 if (!m_Data.empty()) {
84 QReadLocker otherLocker(&arrayData.m_Lock);
67 85 m_Data[0] += arrayData.data();
68 86 }
69 87 }
70 88
71 89 template <int D = Dim, typename = std::enable_if_t<D == 1> >
72 int size()
90 int size() const
73 91 {
92 QReadLocker locker(&m_Lock);
74 93 return m_Data[0].size();
75 94 }
76 95
96 void clear()
97 {
98 QWriteLocker locker(&m_Lock);
99 m_Data.clear();
100 }
101
77 102
78 103 private:
79 104 QVector<QVector<double> > m_Data;
105 mutable QReadWriteLock m_Lock;
80 106 };
81 107
82 108 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,93 +1,107
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 #include <QReadLocker>
10 #include <QReadWriteLock>
9 11 #include <memory>
10 12
11
12 13 Q_DECLARE_LOGGING_CATEGORY(LOG_DataSeries)
13 14 Q_LOGGING_CATEGORY(LOG_DataSeries, "DataSeries")
14 15
15 16
16 17 /**
17 18 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
18 19 *
19 20 * It proposes to set a dimension for the values ​​data
20 21 *
21 22 * @tparam Dim The dimension of the values data
22 23 *
23 24 */
24 25 template <int Dim>
25 26 class DataSeries : public IDataSeries {
26 27 public:
27 28 /// @sa IDataSeries::xAxisData()
28 29 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
29 30 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
30 31
31 32 /// @sa IDataSeries::xAxisUnit()
32 33 Unit xAxisUnit() const override { return m_XAxisUnit; }
33 34
34 35 /// @return the values dataset
35 36 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
36 37 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
37 38
38 39 /// @sa IDataSeries::valuesUnit()
39 40 Unit valuesUnit() const override { return m_ValuesUnit; }
40 41
42 void clear()
43 {
44 m_XAxisData->clear();
45 m_ValuesData->clear();
46 }
47
41 48 /// @sa IDataSeries::merge()
42 49 void merge(IDataSeries *dataSeries) override
43 50 {
44 51 if (auto dimDataSeries = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
45 52 m_XAxisData->merge(*dimDataSeries->xAxisData());
46 53 m_ValuesData->merge(*dimDataSeries->valuesData());
54 dimDataSeries->clear();
47 55 }
48 56 else {
49 57 qCWarning(LOG_DataSeries())
50 58 << QObject::tr("Dection of a type of IDataSeries we cannot merge with !");
51 59 }
52 60 }
53 61
62 virtual void lockRead() { m_Lock.lockForRead(); }
63 virtual void lockWrite() { m_Lock.lockForWrite(); }
64 virtual void unlock() { m_Lock.unlock(); }
65
54 66 protected:
55 67 /// Protected ctor (DataSeries is abstract)
56 68 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
57 69 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
58 70 : m_XAxisData{xAxisData},
59 71 m_XAxisUnit{xAxisUnit},
60 72 m_ValuesData{valuesData},
61 73 m_ValuesUnit{valuesUnit}
62 74 {
63 75 }
64 76
65 77 /// Copy ctor
66 78 explicit DataSeries(const DataSeries<Dim> &other)
67 79 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
68 80 m_XAxisUnit{other.m_XAxisUnit},
69 81 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
70 82 m_ValuesUnit{other.m_ValuesUnit}
71 83 {
72 84 }
73 85
74 86 /// Assignment operator
75 87 template <int D>
76 88 DataSeries &operator=(DataSeries<D> other)
77 89 {
78 90 std::swap(m_XAxisData, other.m_XAxisData);
79 91 std::swap(m_XAxisUnit, other.m_XAxisUnit);
80 92 std::swap(m_ValuesData, other.m_ValuesData);
81 93 std::swap(m_ValuesUnit, other.m_ValuesUnit);
82 94
83 95 return *this;
84 96 }
85 97
86 98 private:
87 99 std::shared_ptr<ArrayData<1> > m_XAxisData;
88 100 Unit m_XAxisUnit;
89 101 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
90 102 Unit m_ValuesUnit;
103
104 QReadWriteLock m_Lock;
91 105 };
92 106
93 107 #endif // SCIQLOP_DATASERIES_H
@@ -1,59 +1,63
1 1 #ifndef SCIQLOP_IDATASERIES_H
2 2 #define SCIQLOP_IDATASERIES_H
3 3
4 4 #include <Common/MetaTypes.h>
5 5
6 6 #include <memory>
7 7
8 8 #include <QString>
9 9
10 10 template <int Dim>
11 11 class ArrayData;
12 12
13 13 struct Unit {
14 14 explicit Unit(const QString &name = {}, bool timeUnit = false)
15 15 : m_Name{name}, m_TimeUnit{timeUnit}
16 16 {
17 17 }
18 18
19 19 QString m_Name; ///< Unit name
20 20 bool m_TimeUnit; ///< The unit is a unit of time
21 21 };
22 22
23 23 /**
24 24 * @brief The IDataSeries aims to declare a data series.
25 25 *
26 26 * A data series is an entity that contains at least :
27 27 * - one dataset representing the x-axis
28 28 * - one dataset representing the values
29 29 *
30 30 * Each dataset is represented by an ArrayData, and is associated with a unit.
31 31 *
32 32 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
33 33 * IDataSeries. The x-axis dataset is always unidimensional.
34 34 *
35 35 * @sa ArrayData
36 36 */
37 37 class IDataSeries {
38 38 public:
39 39 virtual ~IDataSeries() noexcept = default;
40 40
41 41 /// Returns the x-axis dataset
42 42 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
43 43
44 44 /// Returns the x-axis dataset (as const)
45 45 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
46 46
47 47 virtual Unit xAxisUnit() const = 0;
48 48
49 49 virtual Unit valuesUnit() const = 0;
50 50
51 51 virtual void merge(IDataSeries *dataSeries) = 0;
52 52
53 53 virtual std::unique_ptr<IDataSeries> clone() const = 0;
54
55 virtual void lockRead() = 0;
56 virtual void lockWrite() = 0;
57 virtual void unlock() = 0;
54 58 };
55 59
56 60 // Required for using shared_ptr in signals/slots
57 61 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
58 62
59 63 #endif // SCIQLOP_IDATASERIES_H
@@ -1,94 +1,101
1 1 #include "Variable/Variable.h"
2 2
3 3 #include <Data/IDataSeries.h>
4 4 #include <Data/SqpDateTime.h>
5 5
6 #include <QReadWriteLock>
7 #include <QThread>
8
6 9 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
7 10
8 11 struct Variable::VariablePrivate {
9 12 explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission,
10 13 const SqpDateTime &dateTime)
11 14 : m_Name{name},
12 15 m_Unit{unit},
13 16 m_Mission{mission},
14 17 m_DateTime{dateTime},
15 18 m_DataSeries{nullptr}
16 19 {
17 20 }
18 21
19 22 QString m_Name;
20 23 QString m_Unit;
21 24 QString m_Mission;
22 25
23 26 SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache.
24 27 std::unique_ptr<IDataSeries> m_DataSeries;
25 28 };
26 29
27 30 Variable::Variable(const QString &name, const QString &unit, const QString &mission,
28 31 const SqpDateTime &dateTime)
29 32 : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission, dateTime)}
30 33 {
31 34 }
32 35
33 36 QString Variable::name() const noexcept
34 37 {
35 38 return impl->m_Name;
36 39 }
37 40
38 41 QString Variable::mission() const noexcept
39 42 {
40 43 return impl->m_Mission;
41 44 }
42 45
43 46 QString Variable::unit() const noexcept
44 47 {
45 48 return impl->m_Unit;
46 49 }
47 50
48 51 SqpDateTime Variable::dateTime() const noexcept
49 52 {
50 53 return impl->m_DateTime;
51 54 }
52 55
53 56 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
54 57 {
55 58 impl->m_DateTime = dateTime;
56 59 }
57 60
58 61 void Variable::setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
59 62 {
63 qCDebug(LOG_Variable()) << "Variable::setDataSeries" << QThread::currentThread()->objectName();
60 64 if (!dataSeries) {
61 65 /// @todo ALX : log
62 66 return;
63 67 }
64 68
65 69 // Inits the data series of the variable
66 70 if (!impl->m_DataSeries) {
67 71 impl->m_DataSeries = dataSeries->clone();
68 72 }
69 73 else {
74 dataSeries->lockWrite();
75 impl->m_DataSeries->lockWrite();
70 76 impl->m_DataSeries->merge(dataSeries.get());
71
77 impl->m_DataSeries->unlock();
78 dataSeries->unlock();
72 79 emit updated();
73 80 }
74 81 }
75 82
76 83 IDataSeries *Variable::dataSeries() const noexcept
77 84 {
78 85 return impl->m_DataSeries.get();
79 86 }
80 87
81 88 bool Variable::contains(const SqpDateTime &dateTime) const noexcept
82 89 {
83 90 return impl->m_DateTime.contains(dateTime);
84 91 }
85 92
86 93 bool Variable::intersect(const SqpDateTime &dateTime) const noexcept
87 94 {
88 95 return impl->m_DateTime.intersect(dateTime);
89 96 }
90 97
91 98 bool Variable::isInside(const SqpDateTime &dateTime) const noexcept
92 99 {
93 100 return dateTime.contains(SqpDateTime{impl->m_DateTime.m_TStart, impl->m_DateTime.m_TEnd});
94 101 }
@@ -1,220 +1,228
1 1 #include "Variable/VariableCacheController.h"
2 2
3 3 #include "Variable/Variable.h"
4 4 #include <unordered_map>
5 5
6 #include <QThread>
6 7 Q_LOGGING_CATEGORY(LOG_VariableCacheController, "VariableCacheController")
7 8
8 9 struct VariableCacheController::VariableCacheControllerPrivate {
9 10
10 11 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> >
11 12 m_VariableToSqpDateTimeListMap;
12 13
13 14 void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
14 15 QVector<SqpDateTime> &notInCache, int cacheIndex,
15 16 double currentTStart);
16 17
17 18 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
18 19 QVector<SqpDateTime> &notInCache, int cacheIndex,
19 20 double currentTStart);
20 21
21 22
22 23 void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
23 24 int cacheIndex);
24 25 };
25 26
26 27
27 28 VariableCacheController::VariableCacheController(QObject *parent)
28 29 : QObject{parent}, impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()}
29 30 {
30 31 }
31 32
32 33 void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable,
33 34 const SqpDateTime &dateTime)
34 35 {
36 qCDebug(LOG_VariableCacheController()) << "VariableCacheController::addDateTime"
37 << QThread::currentThread()->objectName();
35 38 if (variable) {
36 39 auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable);
37 40 if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) {
38 41 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
39 42 }
40 43 else {
41 44
42 45 // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure
43 46 // that the list is ordered : l(0) < l(1). We assume also a < b
44 47 // (with a & b of type SqpDateTime) means ts(b) > te(a)
45 48
46 49 // The algorithm will try the merge of two interval:
47 50 // - dateTime will be compare with the first interval of the list:
48 51 // A: if it is inferior, it will be inserted and it's finished.
49 52 // B: if it is in intersection, it will be merge then the merged one
50 53 // will be compared to the next interval. The old one is remove from the list
51 54 // C: if it is superior, we do the same with the next interval of the list
52 55
53 56 try {
54 57 impl->addDateTimeRecurse(dateTime,
55 58 impl->m_VariableToSqpDateTimeListMap.at(variable), 0);
56 59 }
57 60 catch (const std::out_of_range &e) {
58 61 qCWarning(LOG_VariableCacheController()) << "addDateTime" << e.what();
59 62 }
60 63 }
61 64 }
62 65 }
63 66
64 67 void VariableCacheController::clear(std::shared_ptr<Variable> variable) noexcept
65 68 {
66 69 if (!variable) {
67 70 qCCritical(LOG_VariableCacheController()) << "Can't clear variable cache: variable is null";
68 71 return;
69 72 }
70 73
71 74 auto nbEntries = impl->m_VariableToSqpDateTimeListMap.erase(variable);
72 75
73 76 auto clearCacheMessage
74 77 = (nbEntries != 0)
75 78 ? tr("Variable cache cleared for variable %1").arg(variable->name())
76 79 : tr("No deletion of variable cache: no cache was associated with the variable");
77 80 qCDebug(LOG_VariableCacheController()) << clearCacheMessage;
78 81 }
79 82
80 83 QVector<SqpDateTime>
81 84 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
82 85 const SqpDateTime &dateTime)
83 86 {
87 qCDebug(LOG_VariableCacheController())
88 << "VariableCacheController::provideNotInCacheDateTimeList"
89 << QThread::currentThread()->objectName();
84 90 auto notInCache = QVector<SqpDateTime>{};
85 91
86 92 // This algorithm is recursif. The idea is to localise the start time then the end time in the
87 93 // list of date time request associated to the variable
88 94 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
89 95 // (with a & b of type SqpDateTime) means ts(b) > te(a)
90 96 auto it = impl->m_VariableToSqpDateTimeListMap.find(variable);
91 97 if (it != impl->m_VariableToSqpDateTimeListMap.end()) {
92 98 impl->addInCacheDataByStart(dateTime, it->second, notInCache, 0, dateTime.m_TStart);
93 99 }
94 100 else {
95 101 notInCache << dateTime;
96 102 }
97 103
98 104 return notInCache;
99 105 }
100 106
101 107 QVector<SqpDateTime>
102 108 VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept
103 109 {
110 qCDebug(LOG_VariableCacheController()) << "VariableCacheController::dateCacheList"
111 << QThread::currentThread()->objectName();
104 112 try {
105 113 return impl->m_VariableToSqpDateTimeListMap.at(variable);
106 114 }
107 115 catch (const std::out_of_range &e) {
108 116 qCWarning(LOG_VariableCacheController()) << e.what();
109 117 return QVector<SqpDateTime>{};
110 118 }
111 119 }
112 120
113 121 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
114 122 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
115 123 {
116 124 const auto dateTimeListSize = dateTimeList.count();
117 125 if (cacheIndex >= dateTimeListSize) {
118 126 dateTimeList.push_back(dateTime);
119 127 // there is no anymore interval to compore, we can just push_back it
120 128 return;
121 129 }
122 130
123 131 auto currentDateTime = dateTimeList[cacheIndex];
124 132
125 133 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
126 134 // The compared one is < to current one compared, we can insert it
127 135 dateTimeList.insert(cacheIndex, dateTime);
128 136 }
129 137 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
130 138 // The compared one is > to current one compared we can comparet if to the next one
131 139 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
132 140 }
133 141 else {
134 142 // Merge cases: we need to merge the two interval, remove the old one from the list then
135 143 // rerun the algo from this index with the merged interval
136 144 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
137 145 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
138 146 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
139 147
140 148 dateTimeList.remove(cacheIndex);
141 149 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
142 150 }
143 151 }
144 152
145 153
146 154 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
147 155 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
148 156 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
149 157 {
150 158 const auto dateTimeListSize = dateTimeList.count();
151 159 if (cacheIndex >= dateTimeListSize) {
152 160 if (currentTStart < dateTime.m_TEnd) {
153 161
154 162 // te localised after all other interval: The last interval is [currentTsart, te]
155 163 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
156 164 }
157 165 return;
158 166 }
159 167
160 168 auto currentDateTimeJ = dateTimeList[cacheIndex];
161 169 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
162 170 // te localised between to interval: The last interval is [currentTsart, te]
163 171 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
164 172 }
165 173 else {
166 174 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
167 175 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
168 176 // te not localised before the current interval: we need to look at the next interval
169 177 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
170 178 currentDateTimeJ.m_TEnd);
171 179 }
172 180 }
173 181 }
174 182
175 183 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
176 184 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
177 185 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
178 186 {
179 187 const auto dateTimeListSize = dateTimeList.count();
180 188 if (cacheIndex >= dateTimeListSize) {
181 189 // ts localised after all other interval: The last interval is [ts, te]
182 190 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
183 191 return;
184 192 }
185 193
186 194 auto currentDateTimeI = dateTimeList[cacheIndex];
187 195 if (currentTStart < currentDateTimeI.m_TStart) {
188 196
189 197 // ts localised between to interval: let's localized te
190 198 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart);
191 199 }
192 200 else if (currentTStart < currentDateTimeI.m_TEnd) {
193 201 if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) {
194 202 // ts not localised before the current interval: we need to look at the next interval
195 203 // We can assume now current tstart is the last interval tend, because data between them
196 204 // are
197 205 // in the cache
198 206 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
199 207 currentDateTimeI.m_TEnd);
200 208 }
201 209 }
202 210 else {
203 211 // ts not localised before the current interval: we need to look at the next interval
204 212 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart);
205 213 }
206 214 }
207 215
208 216
209 217 void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) const
210 218 {
211 219 auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.find(variable);
212 220 if (variableDateTimeList != impl->m_VariableToSqpDateTimeListMap.end()) {
213 221 qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache")
214 222 << variableDateTimeList->second;
215 223 }
216 224 else {
217 225 qCWarning(LOG_VariableCacheController())
218 226 << tr("Cannot display a variable that is not in the cache");
219 227 }
220 228 }
@@ -1,207 +1,211
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 12 #include <QMutex>
13 13 #include <QThread>
14 14 #include <QtCore/QItemSelectionModel>
15 15
16 16 #include <unordered_map>
17 17
18 18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 19
20 20 namespace {
21 21
22 22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 23 /// will be deleted when the timerange is recovered from SciQlop
24 24 std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 25 const SqpDateTime &dateTime) noexcept
26 26 {
27 27 auto parameters = DataProviderParameters{dateTime};
28 28
29 29 return provider.retrieveData(parameters);
30 30 }
31 31
32 32 } // namespace
33 33
34 34 struct VariableController::VariableControllerPrivate {
35 35 explicit VariableControllerPrivate(VariableController *parent)
36 36 : m_WorkingMutex{},
37 37 m_VariableModel{new VariableModel{parent}},
38 38 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
39 39 m_VariableCacheController{std::make_unique<VariableCacheController>()}
40 40 {
41 41 }
42 42
43 43 QMutex m_WorkingMutex;
44 44 /// Variable model. The VariableController has the ownership
45 45 VariableModel *m_VariableModel;
46 46 QItemSelectionModel *m_VariableSelectionModel;
47 47
48 48
49 49 TimeController *m_TimeController{nullptr};
50 50 std::unique_ptr<VariableCacheController> m_VariableCacheController;
51 51
52 52 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
53 53 m_VariableToProviderMap;
54 54 };
55 55
56 56 VariableController::VariableController(QObject *parent)
57 57 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
58 58 {
59 59 qCDebug(LOG_VariableController()) << tr("VariableController construction")
60 60 << QThread::currentThread();
61 61 }
62 62
63 63 VariableController::~VariableController()
64 64 {
65 65 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
66 66 << QThread::currentThread();
67 67 this->waitForFinish();
68 68 }
69 69
70 70 VariableModel *VariableController::variableModel() noexcept
71 71 {
72 72 return impl->m_VariableModel;
73 73 }
74 74
75 75 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
76 76 {
77 77 return impl->m_VariableSelectionModel;
78 78 }
79 79
80 80 void VariableController::setTimeController(TimeController *timeController) noexcept
81 81 {
82 82 impl->m_TimeController = timeController;
83 83 }
84 84
85 85 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
86 86 {
87 87 if (!variable) {
88 88 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
89 89 return;
90 90 }
91 91
92 92 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
93 93 // make some treatments before the deletion
94 94 emit variableAboutToBeDeleted(variable);
95 95
96 96 // Deletes provider
97 97 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
98 98 qCDebug(LOG_VariableController())
99 99 << tr("Number of providers deleted for variable %1: %2")
100 100 .arg(variable->name(), QString::number(nbProvidersDeleted));
101 101
102 102 // Clears cache
103 103 impl->m_VariableCacheController->clear(variable);
104 104
105 105 // Deletes from model
106 106 impl->m_VariableModel->deleteVariable(variable);
107 107 }
108 108
109 109 void VariableController::deleteVariables(
110 110 const QVector<std::shared_ptr<Variable> > &variables) noexcept
111 111 {
112 112 for (auto variable : qAsConst(variables)) {
113 113 deleteVariable(variable);
114 114 }
115 115 }
116 116
117 117 void VariableController::createVariable(const QString &name,
118 118 std::shared_ptr<IDataProvider> provider) noexcept
119 119 {
120 120
121 121 if (!impl->m_TimeController) {
122 122 qCCritical(LOG_VariableController())
123 123 << tr("Impossible to create variable: The time controller is null");
124 124 return;
125 125 }
126 126
127 127
128 128 /// @todo : for the moment :
129 129 /// - the provider is only used to retrieve data from the variable for its initialization, but
130 130 /// it will be retained later
131 131 /// - default data are generated for the variable, without taking into account the timerange set
132 132 /// in sciqlop
133 133 auto dateTime = impl->m_TimeController->dateTime();
134 134 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
135 135
136 136 // store the provider
137 137 impl->m_VariableToProviderMap[newVariable] = provider;
138 138
139 139 auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ](
140 140 auto dataSeriesAcquired, auto dateTimeToPutInCache)
141 141 {
142 142 if (auto variable = varW.lock()) {
143 143 impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache);
144 144 variable->setDataSeries(dataSeriesAcquired);
145 145 }
146 146 };
147 147
148 148 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
149 149 this->onRequestDataLoading(newVariable, dateTime);
150 150 }
151 151 }
152 152
153 153 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
154 154 {
155 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
156 << QThread::currentThread()->objectName();
155 157 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
156 158
157 159 for (const auto &selectedRow : qAsConst(selectedRows)) {
158 160 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
159 161 selectedVariable->setDateTime(dateTime);
160 162 this->onRequestDataLoading(selectedVariable, dateTime);
161 163 }
162 164 }
163 165 }
164 166
165 167
166 168 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
167 169 const SqpDateTime &dateTime)
168 170 {
171 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
172 << QThread::currentThread()->objectName();
169 173 // we want to load data of the variable for the dateTime.
170 174 // First we check if the cache contains some of them.
171 175 // For the other, we ask the provider to give them.
172 176 if (variable) {
173 177
174 178 auto dateTimeListNotInCache
175 179 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
176 180
177 181 if (!dateTimeListNotInCache.empty()) {
178 182 // Ask the provider for each data on the dateTimeListNotInCache
179 183 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
180 184 std::move(dateTimeListNotInCache));
181 185 }
182 186 else {
183 187 emit variable->updated();
184 188 }
185 189 }
186 190 else {
187 191 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
188 192 }
189 193 }
190 194
191 195
192 196 void VariableController::initialize()
193 197 {
194 198 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
195 199 impl->m_WorkingMutex.lock();
196 200 qCDebug(LOG_VariableController()) << tr("VariableController init END");
197 201 }
198 202
199 203 void VariableController::finalize()
200 204 {
201 205 impl->m_WorkingMutex.unlock();
202 206 }
203 207
204 208 void VariableController::waitForFinish()
205 209 {
206 210 QMutexLocker locker{&impl->m_WorkingMutex};
207 211 }
@@ -1,119 +1,124
1 1 #include "SqpApplication.h"
2 2
3 3 #include <Data/IDataProvider.h>
4 4 #include <DataSource/DataSourceController.h>
5 5 #include <QThread>
6 6 #include <Time/TimeController.h>
7 7 #include <Variable/Variable.h>
8 8 #include <Variable/VariableController.h>
9 9 #include <Visualization/VisualizationController.h>
10 10
11 11 Q_LOGGING_CATEGORY(LOG_SqpApplication, "SqpApplication")
12 12
13 13 class SqpApplication::SqpApplicationPrivate {
14 14 public:
15 15 SqpApplicationPrivate()
16 16 : m_DataSourceController{std::make_unique<DataSourceController>()},
17 17 m_TimeController{std::make_unique<TimeController>()},
18 18 m_VariableController{std::make_unique<VariableController>()},
19 19 m_VisualizationController{std::make_unique<VisualizationController>()}
20 20 {
21 QThread::currentThread()->setObjectName("MainThread");
22 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
23 m_DataSourceControllerThread.setObjectName("DataSourceControllerThread");
24 m_VariableController->moveToThread(&m_VariableControllerThread);
25 m_VariableControllerThread.setObjectName("VariableControllerThread");
26 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
27 m_VisualizationControllerThread.setObjectName("VisualizationControllerThread");
28
21 29 // /////////////////////////////// //
22 30 // Connections between controllers //
23 31 // /////////////////////////////// //
24 32
25 33 // VariableController <-> DataSourceController
26 34 connect(m_DataSourceController.get(),
27 35 SIGNAL(variableCreationRequested(const QString &, std::shared_ptr<IDataProvider>)),
28 36 m_VariableController.get(),
29 37 SLOT(createVariable(const QString &, std::shared_ptr<IDataProvider>)));
30 38
31 39 // VariableController <-> VisualizationController
32 40 connect(m_VariableController.get(),
33 41 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)),
34 42 m_VisualizationController.get(),
35 43 SIGNAL(variableAboutToBeDeleted(std::shared_ptr<Variable>)), Qt::DirectConnection);
36 44
37 m_DataSourceController->moveToThread(&m_DataSourceControllerThread);
38 m_VariableController->moveToThread(&m_VariableControllerThread);
39 m_VisualizationController->moveToThread(&m_VisualizationControllerThread);
40 45
41 46 // Additionnal init
42 47 m_VariableController->setTimeController(m_TimeController.get());
43 48 }
44 49
45 50 virtual ~SqpApplicationPrivate()
46 51 {
47 52 qCInfo(LOG_SqpApplication()) << tr("SqpApplicationPrivate destruction");
48 53 m_DataSourceControllerThread.quit();
49 54 m_DataSourceControllerThread.wait();
50 55
51 56 m_VariableControllerThread.quit();
52 57 m_VariableControllerThread.wait();
53 58
54 59 m_VisualizationControllerThread.quit();
55 60 m_VisualizationControllerThread.wait();
56 61 }
57 62
58 63 std::unique_ptr<DataSourceController> m_DataSourceController;
59 64 std::unique_ptr<VariableController> m_VariableController;
60 65 std::unique_ptr<TimeController> m_TimeController;
61 66 std::unique_ptr<VisualizationController> m_VisualizationController;
62 67 QThread m_DataSourceControllerThread;
63 68 QThread m_VariableControllerThread;
64 69 QThread m_VisualizationControllerThread;
65 70 };
66 71
67 72
68 73 SqpApplication::SqpApplication(int &argc, char **argv)
69 74 : QApplication{argc, argv}, impl{spimpl::make_unique_impl<SqpApplicationPrivate>()}
70 75 {
71 76 qCInfo(LOG_SqpApplication()) << tr("SqpApplication construction");
72 77
73 78 connect(&impl->m_DataSourceControllerThread, &QThread::started,
74 79 impl->m_DataSourceController.get(), &DataSourceController::initialize);
75 80 connect(&impl->m_DataSourceControllerThread, &QThread::finished,
76 81 impl->m_DataSourceController.get(), &DataSourceController::finalize);
77 82
78 83 connect(&impl->m_VariableControllerThread, &QThread::started, impl->m_VariableController.get(),
79 84 &VariableController::initialize);
80 85 connect(&impl->m_VariableControllerThread, &QThread::finished, impl->m_VariableController.get(),
81 86 &VariableController::finalize);
82 87
83 88 connect(&impl->m_VisualizationControllerThread, &QThread::started,
84 89 impl->m_VisualizationController.get(), &VisualizationController::initialize);
85 90 connect(&impl->m_VisualizationControllerThread, &QThread::finished,
86 91 impl->m_VisualizationController.get(), &VisualizationController::finalize);
87 92
88 93 impl->m_DataSourceControllerThread.start();
89 94 impl->m_VariableControllerThread.start();
90 95 impl->m_VisualizationControllerThread.start();
91 96 }
92 97
93 98 SqpApplication::~SqpApplication()
94 99 {
95 100 }
96 101
97 102 void SqpApplication::initialize()
98 103 {
99 104 }
100 105
101 106 DataSourceController &SqpApplication::dataSourceController() noexcept
102 107 {
103 108 return *impl->m_DataSourceController;
104 109 }
105 110
106 111 TimeController &SqpApplication::timeController() noexcept
107 112 {
108 113 return *impl->m_TimeController;
109 114 }
110 115
111 116 VariableController &SqpApplication::variableController() noexcept
112 117 {
113 118 return *impl->m_VariableController;
114 119 }
115 120
116 121 VisualizationController &SqpApplication::visualizationController() noexcept
117 122 {
118 123 return *impl->m_VisualizationController;
119 124 }
@@ -1,155 +1,160
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 #include <QElapsedTimer>
9
10 8 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
11 9
12 10 namespace {
13 11
12 class SqpDataContainer : public QCPGraphDataContainer {
13 public:
14 void appendGraphDataUnsorted(const QCPGraphData &data) { mData.append(data); }
15 };
16
17
14 18 /// Format for datetimes on a axis
15 19 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
16 20
17 21 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
18 22 /// non-time data
19 23 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
20 24 {
21 25 if (isTimeAxis) {
22 26 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
23 27 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
24 28
25 29 return dateTicker;
26 30 }
27 31 else {
28 32 // default ticker
29 33 return QSharedPointer<QCPAxisTicker>::create();
30 34 }
31 35 }
32 36
33 37 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
34 38 const SqpDateTime &dateTime)
35 39 {
36 QElapsedTimer timer;
37 timer.start();
40 qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData"
41 << QThread::currentThread()->objectName();
38 42 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
39 43 // Clean the graph
40 44 // NAIVE approch
41 const auto &xData = scalarSeries.xAxisData()->data();
42 const auto &valuesData = scalarSeries.valuesData()->data();
45 scalarSeries.lockRead();
46 {
47 const auto xData = scalarSeries.xAxisData()->data();
48 const auto valuesData = scalarSeries.valuesData()->data();
43 49 const auto count = xData.count();
44 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count();
45 auto xValue = QVector<double>(count);
46 auto vValue = QVector<double>(count);
50 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache"
51 << xData.count();
52
53 auto dataContainer = qcpGraph->data();
54 dataContainer->clear();
55 auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create();
56 qcpGraph->setData(sqpDataContainer);
47 57
48 int n = 0;
49 58 for (auto i = 0; i < count; ++i) {
50 59 const auto x = xData[i];
51 60 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
52 xValue[n] = x;
53 vValue[n] = valuesData[i];
54 ++n;
61 sqpDataContainer->appendGraphDataUnsorted(QCPGraphData(x, valuesData[i]));
55 62 }
56 63 }
57
58 xValue.resize(n);
59 vValue.resize(n);
60
64 sqpDataContainer->sort();
61 65 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
62 << xValue.count();
66 << sqpDataContainer->size();
67 }
68 scalarSeries.unlock();
63 69
64 qcpGraph->setData(xValue, vValue);
65 70
66 71 // Display all data
67 72 component->rescaleAxes();
68 73 component->parentPlot()->replot();
69 74 }
70 75 else {
71 76 /// @todo DEBUG
72 77 }
73 78 }
74 79
75 80 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
76 81 const SqpDateTime &dateTime)
77 82 {
78 83 auto component = plot.addGraph();
79 84
80 85 if (component) {
81 86 // // Graph data
82 87 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
83 88 true);
84 89
85 90 updateScalarData(component, scalarSeries, dateTime);
86 91
87 92 // Axes properties
88 93 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
89 94 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
90 95
91 96 auto setAxisProperties = [](auto axis, const auto &unit) {
92 97 // label (unit name)
93 98 axis->setLabel(unit.m_Name);
94 99
95 100 // ticker (depending on the type of unit)
96 101 axis->setTicker(axisTicker(unit.m_TimeUnit));
97 102 };
98 103 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
99 104 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
100 105
101 106 // Display all data
102 107 component->rescaleAxes();
103 108 plot.replot();
104 109 }
105 110 else {
106 111 qCDebug(LOG_VisualizationGraphHelper())
107 112 << QObject::tr("Can't create graph for the scalar series");
108 113 }
109 114
110 115 return component;
111 116 }
112 117
113 118 } // namespace
114 119
115 120 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
116 121 QCustomPlot &plot) noexcept
117 122 {
118 123 auto result = QVector<QCPAbstractPlottable *>{};
119 124
120 125 if (variable) {
121 126 // Gets the data series of the variable to call the creation of the right components
122 127 // according to its type
123 128 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
124 129 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
125 130 }
126 131 else {
127 132 qCDebug(LOG_VisualizationGraphHelper())
128 133 << QObject::tr("Can't create graph plottables : unmanaged data series type");
129 134 }
130 135 }
131 136 else {
132 137 qCDebug(LOG_VisualizationGraphHelper())
133 138 << QObject::tr("Can't create graph plottables : the variable is null");
134 139 }
135 140
136 141 return result;
137 142 }
138 143
139 144 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
140 145 IDataSeries *dataSeries, const SqpDateTime &dateTime)
141 146 {
142 147 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
143 148 if (plotableVect.size() == 1) {
144 149 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
145 150 }
146 151 else {
147 152 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
148 153 "Can't update Data of a scalarSeries because there is not only one component "
149 154 "associated");
150 155 }
151 156 }
152 157 else {
153 158 /// @todo DEBUG
154 159 }
155 160 }
@@ -1,285 +1,285
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,
53 53 static_cast<void (QCPAxis::*)(const QCPRange &)>(&QCPAxis::rangeChanged), this,
54 54 &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 60
61 61 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
62 62 &VariableController::onRequestDataLoading);
63 63 }
64 64
65 65
66 66 VisualizationGraphWidget::~VisualizationGraphWidget()
67 67 {
68 68 delete ui;
69 69 }
70 70
71 71 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
72 72 {
73 73 // Uses delegate to create the qcpplot components according to the variable
74 74 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
75 75
76 76 for (auto createdPlottable : qAsConst(createdPlottables)) {
77 77 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
78 78 }
79 79
80 80 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
81 81 }
82
83 82 void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable)
84 83 {
85 84
86 85 // when adding a variable, we need to set its time range to the current graph range
87 86 auto grapheRange = ui->widget->xAxis->range();
88 87 auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper};
89 88 variable->setDateTime(dateTime);
90 89
91 90 auto variableDateTimeWithTolerance = dateTime;
92 91
93 92 // add 10% tolerance for each side
94 93 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
95 94 variableDateTimeWithTolerance.m_TStart -= tolerance;
96 95 variableDateTimeWithTolerance.m_TEnd += tolerance;
97 96
98 97 // Uses delegate to create the qcpplot components according to the variable
99 98 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
100 99
101 100 for (auto createdPlottable : qAsConst(createdPlottables)) {
102 101 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
103 102 }
104 103
105 104 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
106 105
107 106 // CHangement detected, we need to ask controller to request data loading
108 107 emit requestDataLoading(variable, variableDateTimeWithTolerance);
109 108 }
110 109
111 110 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
112 111 {
113 112 // Each component associated to the variable :
114 113 // - is removed from qcpplot (which deletes it)
115 114 // - is no longer referenced in the map
116 115 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
117 116 for (auto it = componentsIt.first; it != componentsIt.second;) {
118 117 ui->widget->removePlottable(it->second);
119 118 it = impl->m_VariableToPlotMultiMap.erase(it);
120 119 }
121 120
122 121 // Updates graph
123 122 ui->widget->replot();
124 123 }
125 124
126 125 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
127 126 {
128 127 if (visitor) {
129 128 visitor->visit(this);
130 129 }
131 130 else {
132 131 qCCritical(LOG_VisualizationGraphWidget())
133 132 << tr("Can't visit widget : the visitor is null");
134 133 }
135 134 }
136 135
137 136 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
138 137 {
139 138 /// @todo : for the moment, a graph can always accomodate a variable
140 139 Q_UNUSED(variable);
141 140 return true;
142 141 }
143 142
144 143 bool VisualizationGraphWidget::contains(const Variable &variable) const
145 144 {
146 145 // Finds the variable among the keys of the map
147 146 auto variablePtr = &variable;
148 147 auto findVariable
149 148 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
150 149
151 150 auto end = impl->m_VariableToPlotMultiMap.cend();
152 151 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
153 152 return it != end;
154 153 }
155 154
156 155 QString VisualizationGraphWidget::name() const
157 156 {
158 157 return ui->graphNameLabel->text();
159 158 }
160 159
161 160 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
162 161 {
163 162 QMenu graphMenu{};
164 163
165 164 // Iterates on variables (unique keys)
166 165 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
167 166 end = impl->m_VariableToPlotMultiMap.cend();
168 167 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
169 168 // 'Remove variable' action
170 169 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
171 170 [ this, var = it->first ]() { removeVariable(var); });
172 171 }
173 172
174 173 if (!graphMenu.isEmpty()) {
175 174 graphMenu.exec(mapToGlobal(pos));
176 175 }
177 176 }
178 177
179 178 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1)
180 179 {
181 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
180 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged")
181 << QThread::currentThread()->objectName();
182 182
183 183 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
184 184 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
185 185
186 186 auto variable = it->first;
187 187 auto dateTime = SqpDateTime{t1.lower, t1.upper};
188 188
189 189 if (!variable->contains(dateTime)) {
190 190
191 191 auto variableDateTimeWithTolerance = dateTime;
192 192 if (!variable->isInside(dateTime)) {
193 193 auto variableDateTime = variable->dateTime();
194 194 if (variableDateTime.m_TStart < dateTime.m_TStart) {
195 195 qCDebug(LOG_VisualizationGraphWidget()) << tr("TDetection pan to right:");
196 196
197 197 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
198 198 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
199 199 // Tolerance have to be added to the right
200 200 // add 10% tolerance for right (end) side
201 201 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
202 202 variableDateTimeWithTolerance.m_TEnd += tolerance;
203 203 }
204 204 else if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
205 205 qCDebug(LOG_VisualizationGraphWidget()) << tr("Detection pan to left: ");
206 206 auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart;
207 207 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
208 208 // Tolerance have to be added to the left
209 209 // add 10% tolerance for left (start) side
210 210 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
211 211 variableDateTimeWithTolerance.m_TStart -= tolerance;
212 212 }
213 213 else {
214 214 qCWarning(LOG_VisualizationGraphWidget())
215 215 << tr("Detection anormal zoom detection: ");
216 216 }
217 217 }
218 218 else {
219 219 qCDebug(LOG_VisualizationGraphWidget()) << tr("Detection zoom out: ");
220 220 // add 10% tolerance for each side
221 221 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
222 222 variableDateTimeWithTolerance.m_TStart -= tolerance;
223 223 variableDateTimeWithTolerance.m_TEnd += tolerance;
224 224 }
225 225 variable->setDateTime(dateTime);
226 226
227 227 // CHangement detected, we need to ask controller to request data loading
228 228 emit requestDataLoading(variable, variableDateTimeWithTolerance);
229 229 }
230 230 else {
231 231 qCDebug(LOG_VisualizationGraphWidget()) << tr("Detection zoom in: ");
232 232 }
233 233 }
234 234 }
235 235
236 236 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
237 237 {
238 238 auto zoomOrientations = QFlags<Qt::Orientation>{};
239 239
240 240 // Lambda that enables a zoom orientation if the key modifier related to this orientation
241 241 // has
242 242 // been pressed
243 243 auto enableOrientation
244 244 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
245 245 auto orientationEnabled = event->modifiers().testFlag(modifier);
246 246 zoomOrientations.setFlag(orientation, orientationEnabled);
247 247 };
248 248 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
249 249 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
250 250
251 251 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
252 252 }
253 253
254 254 void VisualizationGraphWidget::onDataCacheVariableUpdated()
255 255 {
256 256 // NOTE:
257 257 // We don't want to call the method for each component of a variable unitarily, but for
258 258 // all
259 259 // its components at once (eg its three components in the case of a vector).
260 260
261 261 // The unordered_multimap does not do this easily, so the question is whether to:
262 262 // - use an ordered_multimap and the algos of std to group the values by key
263 263 // - use a map (unique keys) and store as values directly the list of components
264 264
265 265 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
266 266 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
267 267 auto variable = it->first;
268 268 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
269 269 variable->dataSeries(), variable->dateTime());
270 270 }
271 271 }
272 272
273 273 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
274 274 {
275 275 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
276 276
277 277 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
278 278
279 279 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
280 280 abstractPlotableVect.push_back(it->second);
281 281 }
282 282
283 283 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
284 284 variable->dateTime());
285 285 }
1 NO CONTENT: modified file
@@ -1,47 +1,50
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 8 #include <QDateTime>
9 #include <QThread>
9 10
10 11 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
11 12
12 13 std::shared_ptr<IDataSeries>
13 14 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
14 15 {
15 16 auto dateTime = parameters.m_Time;
16 17
17 18 auto dataIndex = 0;
18 19
19 20 // Gets the timerange from the parameters
20 21 double freq = 100.0;
21 22 double start = dateTime.m_TStart * freq; // 100 htz
22 23 double end = dateTime.m_TEnd * freq; // 100 htz
23 24
24 25 // We assure that timerange is valid
25 26 if (end < start) {
26 27 std::swap(start, end);
27 28 }
28 29
29 30 // Generates scalar series containing cosinus values (one value per second)
30 31 auto scalarSeries
31 32 = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
32 33
33 34 for (auto time = start; time < end; ++time, ++dataIndex) {
34 35 const auto timeOnFreq = time / freq;
35 36 scalarSeries->setData(dataIndex, timeOnFreq, std::cos(timeOnFreq));
36 37 }
37 38 return scalarSeries;
38 39 }
39 40
40 41 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
41 42 {
43 qCDebug(LOG_CosinusProvider()) << "CosinusProvider::requestDataLoading"
44 << QThread::currentThread()->objectName();
42 45 // NOTE: Try to use multithread if possible
43 46 for (const auto &dateTime : dateTimeList) {
44 47 auto scalarSeries = this->retrieveData(DataProviderParameters{dateTime});
45 48 emit dataProvided(scalarSeries, dateTime);
46 49 }
47 50 }
General Comments 0
You need to be logged in to leave comments. Login now