@@ -1,44 +1,44 | |||
|
1 | 1 | #ifndef SCIQLOP_SQPDATETIME_H |
|
2 | 2 | #define SCIQLOP_SQPDATETIME_H |
|
3 | 3 | |
|
4 | 4 | #include <QObject> |
|
5 | 5 | |
|
6 | 6 | #include <QDateTime> |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | |
|
9 | 9 | #include <Common/MetaTypes.h> |
|
10 | 10 | |
|
11 | 11 | /** |
|
12 | 12 | * @brief The SqpDateTime struct holds the information of time parameters |
|
13 | 13 | */ |
|
14 | 14 | struct SqpDateTime { |
|
15 | 15 | /// Start time |
|
16 | 16 | double m_TStart; |
|
17 | 17 | /// End time |
|
18 | 18 | double m_TEnd; |
|
19 | 19 | |
|
20 | bool contains(const SqpDateTime &dateTime) | |
|
20 | bool contains(const SqpDateTime &dateTime) const noexcept | |
|
21 | 21 | { |
|
22 | 22 | return (m_TStart <= dateTime.m_TStart && m_TEnd >= dateTime.m_TEnd); |
|
23 | 23 | } |
|
24 | 24 | |
|
25 | bool intersect(const SqpDateTime &dateTime) | |
|
25 | bool intersect(const SqpDateTime &dateTime) const noexcept | |
|
26 | 26 | { |
|
27 | 27 | return (m_TEnd >= dateTime.m_TStart && m_TStart <= dateTime.m_TEnd); |
|
28 | 28 | } |
|
29 | 29 | }; |
|
30 | 30 | |
|
31 | 31 | inline QDebug operator<<(QDebug d, SqpDateTime obj) |
|
32 | 32 | { |
|
33 | 33 | auto tendDateTimeStart = QDateTime::fromMSecsSinceEpoch(obj.m_TStart * 1000); |
|
34 | 34 | auto tendDateTimeEnd = QDateTime::fromMSecsSinceEpoch(obj.m_TEnd * 1000); |
|
35 | 35 | |
|
36 | 36 | // QDebug << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd; |
|
37 | 37 | d << "ts: " << tendDateTimeStart << " te: " << tendDateTimeEnd; |
|
38 | 38 | return d; |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | // Required for using shared_ptr in signals/slots |
|
42 | 42 | SCIQLOP_REGISTER_META_TYPE(SQPDATETIME_REGISTRY, SqpDateTime) |
|
43 | 43 | |
|
44 | 44 | #endif // SCIQLOP_SQPDATETIME_H |
@@ -1,54 +1,55 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLE_H |
|
2 | 2 | #define SCIQLOP_VARIABLE_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/MetaTypes.h> |
|
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 | bool contains(const SqpDateTime &dateTime); | |
|
38 | bool intersect(const SqpDateTime &dateTime); | |
|
37 | bool contains(const SqpDateTime &dateTime) const noexcept; | |
|
38 | bool intersect(const SqpDateTime &dateTime) const noexcept; | |
|
39 | bool isInside(const SqpDateTime &dateTime) const noexcept; | |
|
39 | 40 | |
|
40 | 41 | public slots: |
|
41 | 42 | void setDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept; |
|
42 | 43 | |
|
43 | 44 | signals: |
|
44 | 45 | void updated(); |
|
45 | 46 | |
|
46 | 47 | private: |
|
47 | 48 | class VariablePrivate; |
|
48 | 49 | spimpl::unique_impl_ptr<VariablePrivate> impl; |
|
49 | 50 | }; |
|
50 | 51 | |
|
51 | 52 | // Required for using shared_ptr in signals/slots |
|
52 | 53 | SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_REGISTRY, std::shared_ptr<Variable>) |
|
53 | 54 | |
|
54 | 55 | #endif // SCIQLOP_VARIABLE_H |
@@ -1,51 +1,49 | |||
|
1 | 1 | #ifndef SCIQLOP_VARIABLEMODEL_H |
|
2 | 2 | #define SCIQLOP_VARIABLEMODEL_H |
|
3 | 3 | |
|
4 | 4 | |
|
5 | 5 | #include <Data/SqpDateTime.h> |
|
6 | 6 | |
|
7 | 7 | #include <QAbstractTableModel> |
|
8 | 8 | #include <QLoggingCategory> |
|
9 | 9 | |
|
10 | 10 | #include <Common/spimpl.h> |
|
11 | 11 | |
|
12 | 12 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) |
|
13 | 13 | |
|
14 | 14 | class IDataSeries; |
|
15 | 15 | class Variable; |
|
16 | 16 | |
|
17 | 17 | /** |
|
18 | 18 | * @brief The VariableModel class aims to hold the variables that have been created in SciQlop |
|
19 | 19 | */ |
|
20 | 20 | class VariableModel : public QAbstractTableModel { |
|
21 | 21 | public: |
|
22 | 22 | explicit VariableModel(QObject *parent = nullptr); |
|
23 | 23 | |
|
24 | 24 | /** |
|
25 | 25 | * Creates a new variable in the model |
|
26 | 26 | * @param name the name of the new variable |
|
27 | 27 | * @param dateTime the dateTime of the new variable |
|
28 | * @param defaultDataSeries the default data of the new variable | |
|
29 | 28 | * @return the pointer to the new variable |
|
30 | 29 | */ |
|
31 | std::shared_ptr<Variable> | |
|
32 | createVariable(const QString &name, const SqpDateTime &dateTime, | |
|
33 | std::shared_ptr<IDataSeries> defaultDataSeries) noexcept; | |
|
30 | std::shared_ptr<Variable> createVariable(const QString &name, | |
|
31 | const SqpDateTime &dateTime) noexcept; | |
|
34 | 32 | |
|
35 | 33 | std::shared_ptr<Variable> variable(int index) const; |
|
36 | 34 | |
|
37 | 35 | // /////////////////////////// // |
|
38 | 36 | // QAbstractTableModel methods // |
|
39 | 37 | // /////////////////////////// // |
|
40 | 38 | virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
41 | 39 | virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override; |
|
42 | 40 | virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
|
43 | 41 | virtual QVariant headerData(int section, Qt::Orientation orientation, |
|
44 | 42 | int role = Qt::DisplayRole) const override; |
|
45 | 43 | |
|
46 | 44 | private: |
|
47 | 45 | class VariableModelPrivate; |
|
48 | 46 | spimpl::unique_impl_ptr<VariableModelPrivate> impl; |
|
49 | 47 | }; |
|
50 | 48 | |
|
51 | 49 | #endif // SCIQLOP_VARIABLEMODEL_H |
@@ -1,89 +1,94 | |||
|
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::shared_ptr<IDataSeries> dataSeries) noexcept |
|
59 | 59 | { |
|
60 | 60 | if (!dataSeries) { |
|
61 | 61 | /// @todo ALX : log |
|
62 | 62 | return; |
|
63 | 63 | } |
|
64 | 64 | |
|
65 | 65 | // Inits the data series of the variable |
|
66 | 66 | if (!impl->m_DataSeries) { |
|
67 | 67 | impl->m_DataSeries = dataSeries->clone(); |
|
68 | 68 | } |
|
69 | 69 | else { |
|
70 | 70 | impl->m_DataSeries->merge(dataSeries.get()); |
|
71 | 71 | |
|
72 | 72 | emit updated(); |
|
73 | 73 | } |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | IDataSeries *Variable::dataSeries() const noexcept |
|
77 | 77 | { |
|
78 | 78 | return impl->m_DataSeries.get(); |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | bool Variable::contains(const SqpDateTime &dateTime) | |
|
81 | bool Variable::contains(const SqpDateTime &dateTime) const noexcept | |
|
82 | 82 | { |
|
83 | 83 | return impl->m_DateTime.contains(dateTime); |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | bool Variable::intersect(const SqpDateTime &dateTime) | |
|
86 | bool Variable::intersect(const SqpDateTime &dateTime) const noexcept | |
|
87 | 87 | { |
|
88 | 88 | return impl->m_DateTime.intersect(dateTime); |
|
89 | 89 | } |
|
90 | ||
|
91 | bool Variable::isInside(const SqpDateTime &dateTime) const noexcept | |
|
92 | { | |
|
93 | return dateTime.contains(SqpDateTime{impl->m_DateTime.m_TStart, impl->m_DateTime.m_TEnd}); | |
|
94 | } |
@@ -1,205 +1,204 | |||
|
1 | 1 | #include "Variable/VariableCacheController.h" |
|
2 | 2 | |
|
3 | 3 | #include "Variable/Variable.h" |
|
4 | 4 | #include <unordered_map> |
|
5 | 5 | |
|
6 | 6 | Q_LOGGING_CATEGORY(LOG_VariableCacheController, "VariableCacheController") |
|
7 | 7 | |
|
8 | 8 | struct VariableCacheController::VariableCacheControllerPrivate { |
|
9 | 9 | |
|
10 | 10 | std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> > |
|
11 | 11 | m_VariableToSqpDateTimeListMap; |
|
12 | 12 | |
|
13 | 13 | void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
14 | 14 | QVector<SqpDateTime> ¬InCache, int cacheIndex, |
|
15 | 15 | double currentTStart); |
|
16 | 16 | |
|
17 | 17 | void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
18 | 18 | QVector<SqpDateTime> ¬InCache, int cacheIndex, |
|
19 | 19 | double currentTStart); |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
23 | 23 | int cacheIndex); |
|
24 | 24 | }; |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | VariableCacheController::VariableCacheController(QObject *parent) |
|
28 | 28 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()} |
|
29 | 29 | { |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable, |
|
33 | 33 | const SqpDateTime &dateTime) |
|
34 | 34 | { |
|
35 | 35 | if (variable) { |
|
36 | 36 | auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable); |
|
37 | 37 | if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) { |
|
38 | 38 | impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime); |
|
39 | 39 | } |
|
40 | 40 | else { |
|
41 | 41 | |
|
42 | 42 | // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure |
|
43 | 43 | // that the list is ordered : l(0) < l(1). We assume also a < b |
|
44 | 44 | // (with a & b of type SqpDateTime) means ts(b) > te(a) |
|
45 | 45 | |
|
46 | 46 | // The algorithm will try the merge of two interval: |
|
47 | 47 | // - dateTime will be compare with the first interval of the list: |
|
48 | 48 | // A: if it is inferior, it will be inserted and it's finished. |
|
49 | 49 | // B: if it is in intersection, it will be merge then the merged one |
|
50 | 50 | // will be compared to the next interval. The old one is remove from the list |
|
51 | 51 | // C: if it is superior, we do the same with the next interval of the list |
|
52 | 52 | |
|
53 | 53 | try { |
|
54 | 54 | impl->addDateTimeRecurse(dateTime, |
|
55 | 55 | impl->m_VariableToSqpDateTimeListMap.at(variable), 0); |
|
56 | 56 | } |
|
57 | 57 | catch (const std::out_of_range &e) { |
|
58 | qCWarning(LOG_VariableCacheController()) << e.what(); | |
|
58 | qCWarning(LOG_VariableCacheController()) << "addDateTime" << e.what(); | |
|
59 | 59 | } |
|
60 | 60 | } |
|
61 | 61 | } |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | QVector<SqpDateTime> |
|
65 | 65 | VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, |
|
66 | 66 | const SqpDateTime &dateTime) |
|
67 | 67 | { |
|
68 | 68 | auto notInCache = QVector<SqpDateTime>{}; |
|
69 | 69 | |
|
70 | 70 | // This algorithm is recursif. The idea is to localise the start time then the end time in the |
|
71 | 71 | // list of date time request associated to the variable |
|
72 | 72 | // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b |
|
73 | 73 | // (with a & b of type SqpDateTime) means ts(b) > te(a) |
|
74 | ||
|
75 | try { | |
|
76 |
impl->addInCacheDataByStart(dateTime, i |
|
|
77 | notInCache, 0, dateTime.m_TStart); | |
|
74 | auto it = impl->m_VariableToSqpDateTimeListMap.find(variable); | |
|
75 | if (it != impl->m_VariableToSqpDateTimeListMap.end()) { | |
|
76 | impl->addInCacheDataByStart(dateTime, it->second, notInCache, 0, dateTime.m_TStart); | |
|
78 | 77 | } |
|
79 | catch (const std::out_of_range &e) { | |
|
80 | qCWarning(LOG_VariableCacheController()) << e.what(); | |
|
78 | else { | |
|
79 | notInCache << dateTime; | |
|
81 | 80 | } |
|
82 | 81 | |
|
83 | 82 | return notInCache; |
|
84 | 83 | } |
|
85 | 84 | |
|
86 | 85 | QVector<SqpDateTime> |
|
87 | 86 | VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept |
|
88 | 87 | { |
|
89 | 88 | try { |
|
90 | 89 | return impl->m_VariableToSqpDateTimeListMap.at(variable); |
|
91 | 90 | } |
|
92 | 91 | catch (const std::out_of_range &e) { |
|
93 | 92 | qCWarning(LOG_VariableCacheController()) << e.what(); |
|
94 | 93 | return QVector<SqpDateTime>{}; |
|
95 | 94 | } |
|
96 | 95 | } |
|
97 | 96 | |
|
98 | 97 | void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse( |
|
99 | 98 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex) |
|
100 | 99 | { |
|
101 | 100 | const auto dateTimeListSize = dateTimeList.count(); |
|
102 | 101 | if (cacheIndex >= dateTimeListSize) { |
|
103 | 102 | dateTimeList.push_back(dateTime); |
|
104 | 103 | // there is no anymore interval to compore, we can just push_back it |
|
105 | 104 | return; |
|
106 | 105 | } |
|
107 | 106 | |
|
108 | 107 | auto currentDateTime = dateTimeList[cacheIndex]; |
|
109 | 108 | |
|
110 | 109 | if (dateTime.m_TEnd < currentDateTime.m_TStart) { |
|
111 | 110 | // The compared one is < to current one compared, we can insert it |
|
112 | 111 | dateTimeList.insert(cacheIndex, dateTime); |
|
113 | 112 | } |
|
114 | 113 | else if (dateTime.m_TStart > currentDateTime.m_TEnd) { |
|
115 | 114 | // The compared one is > to current one compared we can comparet if to the next one |
|
116 | 115 | addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex); |
|
117 | 116 | } |
|
118 | 117 | else { |
|
119 | 118 | // Merge cases: we need to merge the two interval, remove the old one from the list then |
|
120 | 119 | // rerun the algo from this index with the merged interval |
|
121 | 120 | auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart); |
|
122 | 121 | auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd); |
|
123 | 122 | auto mergeDateTime = SqpDateTime{mTStart, mTEnd}; |
|
124 | 123 | |
|
125 | 124 | dateTimeList.remove(cacheIndex); |
|
126 | 125 | addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex); |
|
127 | 126 | } |
|
128 | 127 | } |
|
129 | 128 | |
|
130 | 129 | |
|
131 | 130 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd( |
|
132 | 131 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
133 | 132 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) |
|
134 | 133 | { |
|
135 | 134 | const auto dateTimeListSize = dateTimeList.count(); |
|
136 | 135 | if (cacheIndex >= dateTimeListSize) { |
|
137 | 136 | if (currentTStart < dateTime.m_TEnd) { |
|
138 | 137 | |
|
139 | 138 | // te localised after all other interval: The last interval is [currentTsart, te] |
|
140 | 139 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); |
|
141 | 140 | } |
|
142 | 141 | return; |
|
143 | 142 | } |
|
144 | 143 | |
|
145 | 144 | auto currentDateTimeJ = dateTimeList[cacheIndex]; |
|
146 | 145 | if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) { |
|
147 | 146 | // te localised between to interval: The last interval is [currentTsart, te] |
|
148 | 147 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); |
|
149 | 148 | } |
|
150 | 149 | else { |
|
151 | 150 | notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart}); |
|
152 | 151 | if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) { |
|
153 | 152 | // te not localised before the current interval: we need to look at the next interval |
|
154 | 153 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex, |
|
155 | 154 | currentDateTimeJ.m_TEnd); |
|
156 | 155 | } |
|
157 | 156 | } |
|
158 | 157 | } |
|
159 | 158 | |
|
160 | 159 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart( |
|
161 | 160 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
162 | 161 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) |
|
163 | 162 | { |
|
164 | 163 | const auto dateTimeListSize = dateTimeList.count(); |
|
165 | 164 | if (cacheIndex >= dateTimeListSize) { |
|
166 | 165 | // ts localised after all other interval: The last interval is [ts, te] |
|
167 | 166 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); |
|
168 | 167 | return; |
|
169 | 168 | } |
|
170 | 169 | |
|
171 | 170 | auto currentDateTimeI = dateTimeList[cacheIndex]; |
|
172 | 171 | if (currentTStart < currentDateTimeI.m_TStart) { |
|
173 | 172 | |
|
174 | 173 | // ts localised between to interval: let's localized te |
|
175 | 174 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart); |
|
176 | 175 | } |
|
177 | 176 | else if (currentTStart < currentDateTimeI.m_TEnd) { |
|
178 | 177 | if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) { |
|
179 | 178 | // ts not localised before the current interval: we need to look at the next interval |
|
180 | 179 | // We can assume now current tstart is the last interval tend, because data between them |
|
181 | 180 | // are |
|
182 | 181 | // in the cache |
|
183 | 182 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, |
|
184 | 183 | currentDateTimeI.m_TEnd); |
|
185 | 184 | } |
|
186 | 185 | } |
|
187 | 186 | else { |
|
188 | 187 | // ts not localised before the current interval: we need to look at the next interval |
|
189 | 188 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart); |
|
190 | 189 | } |
|
191 | 190 | } |
|
192 | 191 | |
|
193 | 192 | |
|
194 | 193 | void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) const |
|
195 | 194 | { |
|
196 | 195 | auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.find(variable); |
|
197 | 196 | if (variableDateTimeList != impl->m_VariableToSqpDateTimeListMap.end()) { |
|
198 | 197 | qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache") |
|
199 | 198 | << variableDateTimeList->second; |
|
200 | 199 | } |
|
201 | 200 | else { |
|
202 | 201 | qCWarning(LOG_VariableCacheController()) |
|
203 | 202 | << tr("Cannot display a variable that is not in the cache"); |
|
204 | 203 | } |
|
205 | 204 | } |
@@ -1,175 +1,177 | |||
|
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::createVariable(const QString &name, |
|
86 | 86 | std::shared_ptr<IDataProvider> provider) noexcept |
|
87 | 87 | { |
|
88 | 88 | |
|
89 | 89 | if (!impl->m_TimeController) { |
|
90 | 90 | qCCritical(LOG_VariableController()) |
|
91 | 91 | << tr("Impossible to create variable: The time controller is null"); |
|
92 | 92 | return; |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | /// @todo : for the moment : |
|
97 | 97 | /// - the provider is only used to retrieve data from the variable for its initialization, but |
|
98 | 98 | /// it will be retained later |
|
99 | 99 | /// - default data are generated for the variable, without taking into account the timerange set |
|
100 | 100 | /// in sciqlop |
|
101 | 101 | auto dateTime = impl->m_TimeController->dateTime(); |
|
102 | if (auto newVariable = impl->m_VariableModel->createVariable( | |
|
103 | name, dateTime, generateDefaultDataSeries(*provider, dateTime))) { | |
|
102 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { | |
|
104 | 103 | |
|
105 | 104 | // store the provider |
|
106 | 105 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
107 | connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(), | |
|
108 | &Variable::setDataSeries); | |
|
109 | 106 | |
|
107 | auto addDateTimeAcquired | |
|
108 | = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { | |
|
110 | 109 | |
|
111 | // store in cache | |
|
112 | impl->m_VariableCacheController->addDateTime(newVariable, dateTime); | |
|
110 | impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); | |
|
111 | newVariable->setDataSeries(dataSeriesAcquired); | |
|
112 | ||
|
113 | }; | |
|
114 | ||
|
115 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | |
|
116 | this->onRequestDataLoading(newVariable, dateTime); | |
|
113 | 117 | |
|
114 | 118 | // notify the creation |
|
115 | 119 | emit variableCreated(newVariable); |
|
116 | 120 | } |
|
117 | 121 | } |
|
118 | 122 | |
|
119 | 123 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) |
|
120 | 124 | { |
|
121 | 125 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
122 | 126 | |
|
123 | 127 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
124 | 128 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
125 | 129 | selectedVariable->setDateTime(dateTime); |
|
126 | 130 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
127 | 131 | } |
|
128 | 132 | } |
|
129 | 133 | } |
|
130 | 134 | |
|
131 | 135 | |
|
132 | 136 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
133 | 137 | const SqpDateTime &dateTime) |
|
134 | 138 | { |
|
135 | 139 | // we want to load data of the variable for the dateTime. |
|
136 | 140 | // First we check if the cache contains some of them. |
|
137 | 141 | // For the other, we ask the provider to give them. |
|
138 | 142 | if (variable) { |
|
139 | 143 | |
|
140 | 144 | auto dateTimeListNotInCache |
|
141 | 145 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
142 | 146 | |
|
143 | 147 | if (!dateTimeListNotInCache.empty()) { |
|
144 | 148 | // Ask the provider for each data on the dateTimeListNotInCache |
|
145 | 149 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
146 | 150 | std::move(dateTimeListNotInCache)); |
|
147 | // store in cache | |
|
148 | impl->m_VariableCacheController->addDateTime(variable, dateTime); | |
|
149 | 151 | } |
|
150 | 152 | else { |
|
151 | 153 | emit variable->updated(); |
|
152 | 154 | } |
|
153 | 155 | } |
|
154 | 156 | else { |
|
155 | 157 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
156 | 158 | } |
|
157 | 159 | } |
|
158 | 160 | |
|
159 | 161 | |
|
160 | 162 | void VariableController::initialize() |
|
161 | 163 | { |
|
162 | 164 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
163 | 165 | impl->m_WorkingMutex.lock(); |
|
164 | 166 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
165 | 167 | } |
|
166 | 168 | |
|
167 | 169 | void VariableController::finalize() |
|
168 | 170 | { |
|
169 | 171 | impl->m_WorkingMutex.unlock(); |
|
170 | 172 | } |
|
171 | 173 | |
|
172 | 174 | void VariableController::waitForFinish() |
|
173 | 175 | { |
|
174 | 176 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
175 | 177 | } |
@@ -1,155 +1,153 | |||
|
1 | 1 | #include <Variable/Variable.h> |
|
2 | 2 | #include <Variable/VariableModel.h> |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataSeries.h> |
|
5 | 5 | |
|
6 | 6 | #include <QDateTime> |
|
7 | 7 | #include <QSize> |
|
8 | 8 | |
|
9 | 9 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
10 | 10 | |
|
11 | 11 | namespace { |
|
12 | 12 | |
|
13 | 13 | // Column indexes |
|
14 | 14 | const auto NAME_COLUMN = 0; |
|
15 | 15 | const auto TSTART_COLUMN = 1; |
|
16 | 16 | const auto TEND_COLUMN = 2; |
|
17 | 17 | const auto NB_COLUMNS = 3; |
|
18 | 18 | |
|
19 | 19 | // Column properties |
|
20 | 20 | const auto DEFAULT_HEIGHT = 25; |
|
21 | 21 | const auto DEFAULT_WIDTH = 100; |
|
22 | 22 | |
|
23 | 23 | struct ColumnProperties { |
|
24 | 24 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, |
|
25 | 25 | int height = DEFAULT_HEIGHT) |
|
26 | 26 | : m_Name{name}, m_Width{width}, m_Height{height} |
|
27 | 27 | { |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | QString m_Name; |
|
31 | 31 | int m_Width; |
|
32 | 32 | int m_Height; |
|
33 | 33 | }; |
|
34 | 34 | |
|
35 | 35 | const auto COLUMN_PROPERTIES |
|
36 | 36 | = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}}, |
|
37 | 37 | {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, |
|
38 | 38 | {TEND_COLUMN, {QObject::tr("tEnd"), 180}}}; |
|
39 | 39 | |
|
40 | 40 | /// Format for datetimes |
|
41 | 41 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); |
|
42 | 42 | |
|
43 | 43 | } // namespace |
|
44 | 44 | |
|
45 | 45 | struct VariableModel::VariableModelPrivate { |
|
46 | 46 | /// Variables created in SciQlop |
|
47 | 47 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
48 | 48 | }; |
|
49 | 49 | |
|
50 | 50 | VariableModel::VariableModel(QObject *parent) |
|
51 | 51 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
52 | 52 | { |
|
53 | 53 | } |
|
54 | 54 | |
|
55 | std::shared_ptr<Variable> | |
|
56 | VariableModel::createVariable(const QString &name, const SqpDateTime &dateTime, | |
|
57 | std::shared_ptr<IDataSeries> defaultDataSeries) noexcept | |
|
55 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, | |
|
56 | const SqpDateTime &dateTime) noexcept | |
|
58 | 57 | { |
|
59 | 58 | auto insertIndex = rowCount(); |
|
60 | 59 | beginInsertRows({}, insertIndex, insertIndex); |
|
61 | 60 | |
|
62 | 61 | /// @todo For the moment, the other data of the variable is initialized with default values |
|
63 | 62 | auto variable = std::make_shared<Variable>(name, QStringLiteral("unit"), |
|
64 | 63 | QStringLiteral("mission"), dateTime); |
|
65 | variable->setDataSeries(std::move(defaultDataSeries)); | |
|
66 | 64 | |
|
67 | 65 | impl->m_Variables.push_back(variable); |
|
68 | 66 | |
|
69 | 67 | endInsertRows(); |
|
70 | 68 | |
|
71 | 69 | return variable; |
|
72 | 70 | } |
|
73 | 71 | |
|
74 | 72 | std::shared_ptr<Variable> VariableModel::variable(int index) const |
|
75 | 73 | { |
|
76 | 74 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; |
|
77 | 75 | } |
|
78 | 76 | |
|
79 | 77 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
80 | 78 | { |
|
81 | 79 | Q_UNUSED(parent); |
|
82 | 80 | |
|
83 | 81 | return NB_COLUMNS; |
|
84 | 82 | } |
|
85 | 83 | |
|
86 | 84 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
87 | 85 | { |
|
88 | 86 | Q_UNUSED(parent); |
|
89 | 87 | |
|
90 | 88 | return impl->m_Variables.size(); |
|
91 | 89 | } |
|
92 | 90 | |
|
93 | 91 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
94 | 92 | { |
|
95 | 93 | if (!index.isValid()) { |
|
96 | 94 | return QVariant{}; |
|
97 | 95 | } |
|
98 | 96 | |
|
99 | 97 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
100 | 98 | return QVariant{}; |
|
101 | 99 | } |
|
102 | 100 | |
|
103 | 101 | if (role == Qt::DisplayRole) { |
|
104 | 102 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
105 | 103 | /// Lambda function that builds the variant to return for a time value |
|
106 | 104 | auto dateTimeVariant = [](double time) { |
|
107 | 105 | auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); |
|
108 | 106 | return dateTime.toString(DATETIME_FORMAT); |
|
109 | 107 | }; |
|
110 | 108 | |
|
111 | 109 | switch (index.column()) { |
|
112 | 110 | case NAME_COLUMN: |
|
113 | 111 | return variable->name(); |
|
114 | 112 | case TSTART_COLUMN: |
|
115 | 113 | return dateTimeVariant(variable->dateTime().m_TStart); |
|
116 | 114 | case TEND_COLUMN: |
|
117 | 115 | return dateTimeVariant(variable->dateTime().m_TEnd); |
|
118 | 116 | default: |
|
119 | 117 | // No action |
|
120 | 118 | break; |
|
121 | 119 | } |
|
122 | 120 | |
|
123 | 121 | qWarning(LOG_VariableModel()) |
|
124 | 122 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
125 | 123 | } |
|
126 | 124 | else { |
|
127 | 125 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
128 | 126 | } |
|
129 | 127 | } |
|
130 | 128 | |
|
131 | 129 | return QVariant{}; |
|
132 | 130 | } |
|
133 | 131 | |
|
134 | 132 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
135 | 133 | { |
|
136 | 134 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { |
|
137 | 135 | return QVariant{}; |
|
138 | 136 | } |
|
139 | 137 | |
|
140 | 138 | if (orientation == Qt::Horizontal) { |
|
141 | 139 | auto propertiesIt = COLUMN_PROPERTIES.find(section); |
|
142 | 140 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { |
|
143 | 141 | // Role is either DisplayRole or SizeHintRole |
|
144 | 142 | return (role == Qt::DisplayRole) |
|
145 | 143 | ? QVariant{propertiesIt->m_Name} |
|
146 | 144 | : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; |
|
147 | 145 | } |
|
148 | 146 | else { |
|
149 | 147 | qWarning(LOG_VariableModel()) |
|
150 | 148 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
151 | 149 | } |
|
152 | 150 | } |
|
153 | 151 | |
|
154 | 152 | return QVariant{}; |
|
155 | 153 | } |
@@ -1,157 +1,155 | |||
|
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 | 40 | // NAIVE approch |
|
41 | 41 | const auto &xData = scalarSeries.xAxisData()->data(); |
|
42 | 42 | const auto &valuesData = scalarSeries.valuesData()->data(); |
|
43 | 43 | const auto count = xData.count(); |
|
44 | 44 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count(); |
|
45 | ||
|
46 | 45 | auto xValue = QVector<double>(count); |
|
47 | 46 | auto vValue = QVector<double>(count); |
|
48 | 47 | |
|
49 | 48 | int n = 0; |
|
50 | 49 | for (auto i = 0; i < count; ++i) { |
|
51 | 50 | const auto x = xData[i]; |
|
52 | 51 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { |
|
53 | 52 | xValue[n] = x; |
|
54 | 53 | vValue[n] = valuesData[i]; |
|
55 | 54 | ++n; |
|
56 | 55 | } |
|
57 | 56 | } |
|
58 | 57 | |
|
59 | 58 | xValue.resize(n); |
|
60 | 59 | vValue.resize(n); |
|
61 | 60 | |
|
62 | 61 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" |
|
63 | 62 | << xValue.count(); |
|
64 | 63 | |
|
65 | 64 | qcpGraph->setData(xValue, vValue); |
|
66 | 65 | |
|
67 | 66 | // Display all data |
|
68 | // component->parentPlot()->xAxis->setRange(dateTime.m_TStart, dateTime.m_TEnd); | |
|
69 | 67 | component->rescaleAxes(); |
|
70 | 68 | component->parentPlot()->replot(); |
|
71 | 69 | } |
|
72 | 70 | else { |
|
73 | 71 | /// @todo DEBUG |
|
74 | 72 | } |
|
75 | 73 | } |
|
76 | 74 | |
|
77 | 75 | QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, |
|
78 | 76 | const SqpDateTime &dateTime) |
|
79 | 77 | { |
|
80 | 78 | auto component = plot.addGraph(); |
|
81 | 79 | |
|
82 | 80 | if (component) { |
|
83 | 81 | // // Graph data |
|
84 | 82 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), |
|
85 | 83 | true); |
|
86 | 84 | |
|
87 | 85 | updateScalarData(component, scalarSeries, dateTime); |
|
88 | 86 | |
|
89 | 87 | // Axes properties |
|
90 | 88 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
91 | 89 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
92 | 90 | |
|
93 | 91 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
94 | 92 | // label (unit name) |
|
95 | 93 | axis->setLabel(unit.m_Name); |
|
96 | 94 | |
|
97 | 95 | // ticker (depending on the type of unit) |
|
98 | 96 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
99 | 97 | }; |
|
100 | 98 | setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); |
|
101 | 99 | setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); |
|
102 | 100 | |
|
103 | 101 | // Display all data |
|
104 | 102 | component->rescaleAxes(); |
|
105 | 103 | plot.replot(); |
|
106 | 104 | } |
|
107 | 105 | else { |
|
108 | 106 | qCDebug(LOG_VisualizationGraphHelper()) |
|
109 | 107 | << QObject::tr("Can't create graph for the scalar series"); |
|
110 | 108 | } |
|
111 | 109 | |
|
112 | 110 | return component; |
|
113 | 111 | } |
|
114 | 112 | |
|
115 | 113 | } // namespace |
|
116 | 114 | |
|
117 | 115 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, |
|
118 | 116 | QCustomPlot &plot) noexcept |
|
119 | 117 | { |
|
120 | 118 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
121 | 119 | |
|
122 | 120 | if (variable) { |
|
123 | 121 | // Gets the data series of the variable to call the creation of the right components |
|
124 | 122 | // according to its type |
|
125 | 123 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { |
|
126 | 124 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); |
|
127 | 125 | } |
|
128 | 126 | else { |
|
129 | 127 | qCDebug(LOG_VisualizationGraphHelper()) |
|
130 | 128 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
131 | 129 | } |
|
132 | 130 | } |
|
133 | 131 | else { |
|
134 | 132 | qCDebug(LOG_VisualizationGraphHelper()) |
|
135 | 133 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
136 | 134 | } |
|
137 | 135 | |
|
138 | 136 | return result; |
|
139 | 137 | } |
|
140 | 138 | |
|
141 | 139 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, |
|
142 | 140 | IDataSeries *dataSeries, const SqpDateTime &dateTime) |
|
143 | 141 | { |
|
144 | 142 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { |
|
145 | 143 | if (plotableVect.size() == 1) { |
|
146 | 144 | updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); |
|
147 | 145 | } |
|
148 | 146 | else { |
|
149 | 147 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( |
|
150 | 148 | "Can't update Data of a scalarSeries because there is not only one component " |
|
151 | 149 | "associated"); |
|
152 | 150 | } |
|
153 | 151 | } |
|
154 | 152 | else { |
|
155 | 153 | /// @todo DEBUG |
|
156 | 154 | } |
|
157 | 155 | } |
@@ -1,263 +1,273 | |||
|
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 | 82 | |
|
83 | 83 | void VisualizationGraphWidget::addVariableUsingGraph(std::shared_ptr<Variable> variable) |
|
84 | 84 | { |
|
85 | 85 | |
|
86 | 86 | // when adding a variable, we need to set its time range to the current graph range |
|
87 | 87 | auto grapheRange = ui->widget->xAxis->range(); |
|
88 | 88 | auto dateTime = SqpDateTime{grapheRange.lower, grapheRange.upper}; |
|
89 | 89 | variable->setDateTime(dateTime); |
|
90 | 90 | |
|
91 | 91 | auto variableDateTimeWithTolerance = dateTime; |
|
92 | 92 | |
|
93 | 93 | // add 10% tolerance for each side |
|
94 | 94 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
95 | 95 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
96 | 96 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
97 | 97 | |
|
98 | 98 | // Uses delegate to create the qcpplot components according to the variable |
|
99 | 99 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); |
|
100 | 100 | |
|
101 | 101 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
102 | 102 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
106 | 106 | |
|
107 | 107 | // CHangement detected, we need to ask controller to request data loading |
|
108 | 108 | emit requestDataLoading(variable, variableDateTimeWithTolerance); |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept |
|
112 | 112 | { |
|
113 | 113 | // Each component associated to the variable : |
|
114 | 114 | // - is removed from qcpplot (which deletes it) |
|
115 | 115 | // - is no longer referenced in the map |
|
116 | 116 | auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); |
|
117 | 117 | for (auto it = componentsIt.first; it != componentsIt.second;) { |
|
118 | 118 | ui->widget->removePlottable(it->second); |
|
119 | 119 | it = impl->m_VariableToPlotMultiMap.erase(it); |
|
120 | 120 | } |
|
121 | 121 | |
|
122 | 122 | // Updates graph |
|
123 | 123 | ui->widget->replot(); |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
127 | 127 | { |
|
128 | 128 | if (visitor) { |
|
129 | 129 | visitor->visit(this); |
|
130 | 130 | } |
|
131 | 131 | else { |
|
132 | 132 | qCCritical(LOG_VisualizationGraphWidget()) |
|
133 | 133 | << tr("Can't visit widget : the visitor is null"); |
|
134 | 134 | } |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const |
|
138 | 138 | { |
|
139 | 139 | /// @todo : for the moment, a graph can always accomodate a variable |
|
140 | 140 | Q_UNUSED(variable); |
|
141 | 141 | return true; |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | QString VisualizationGraphWidget::name() const |
|
145 | 145 | { |
|
146 | 146 | return ui->graphNameLabel->text(); |
|
147 | 147 | } |
|
148 | 148 | |
|
149 | 149 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept |
|
150 | 150 | { |
|
151 | 151 | QMenu graphMenu{}; |
|
152 | 152 | |
|
153 | 153 | // Iterates on variables (unique keys) |
|
154 | 154 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), |
|
155 | 155 | end = impl->m_VariableToPlotMultiMap.cend(); |
|
156 | 156 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
157 | 157 | // 'Remove variable' action |
|
158 | 158 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), |
|
159 | 159 | [ this, var = it->first ]() { removeVariable(var); }); |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | if (!graphMenu.isEmpty()) { |
|
163 | 163 | graphMenu.exec(mapToGlobal(pos)); |
|
164 | 164 | } |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1) |
|
168 | 168 | { |
|
169 | 169 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged"); |
|
170 | 170 | |
|
171 | 171 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); |
|
172 | 172 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
173 | 173 | |
|
174 | 174 | auto variable = it->first; |
|
175 | 175 | auto dateTime = SqpDateTime{t1.lower, t1.upper}; |
|
176 | 176 | |
|
177 | 177 | if (!variable->contains(dateTime)) { |
|
178 | 178 | |
|
179 | 179 | auto variableDateTimeWithTolerance = dateTime; |
|
180 |
if (variable->in |
|
|
180 | if (!variable->isInside(dateTime)) { | |
|
181 | 181 | auto variableDateTime = variable->dateTime(); |
|
182 | 182 | if (variableDateTime.m_TStart < dateTime.m_TStart) { |
|
183 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TDetection pan to right:"); | |
|
183 | 184 | |
|
184 | 185 | auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd; |
|
185 | 186 | dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta; |
|
186 | 187 | // Tolerance have to be added to the right |
|
187 | 188 | // add 10% tolerance for right (end) side |
|
188 | 189 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
189 | 190 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
190 | 191 | } |
|
191 | if (variableDateTime.m_TEnd > dateTime.m_TEnd) { | |
|
192 | else if (variableDateTime.m_TEnd > dateTime.m_TEnd) { | |
|
193 | qCDebug(LOG_VisualizationGraphWidget()) << tr("Detection pan to left: "); | |
|
192 | 194 | auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart; |
|
193 | 195 | dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta; |
|
194 | 196 | // Tolerance have to be added to the left |
|
195 | 197 | // add 10% tolerance for left (start) side |
|
196 | 198 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
197 | 199 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
198 | 200 | } |
|
201 | else { | |
|
202 | qCWarning(LOG_VisualizationGraphWidget()) | |
|
203 | << tr("Detection anormal zoom detection: "); | |
|
204 | } | |
|
199 | 205 | } |
|
200 | 206 | else { |
|
207 | qCDebug(LOG_VisualizationGraphWidget()) << tr("Detection zoom out: "); | |
|
201 | 208 | // add 10% tolerance for each side |
|
202 | 209 | auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart); |
|
203 | 210 | variableDateTimeWithTolerance.m_TStart -= tolerance; |
|
204 | 211 | variableDateTimeWithTolerance.m_TEnd += tolerance; |
|
205 | 212 | } |
|
206 | 213 | variable->setDateTime(dateTime); |
|
207 | 214 | |
|
208 | 215 | // CHangement detected, we need to ask controller to request data loading |
|
209 | 216 | emit requestDataLoading(variable, variableDateTimeWithTolerance); |
|
210 | 217 | } |
|
218 | else { | |
|
219 | qCDebug(LOG_VisualizationGraphWidget()) << tr("Detection zoom in: "); | |
|
220 | } | |
|
211 | 221 | } |
|
212 | 222 | } |
|
213 | 223 | |
|
214 | 224 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept |
|
215 | 225 | { |
|
216 | 226 | auto zoomOrientations = QFlags<Qt::Orientation>{}; |
|
217 | 227 | |
|
218 | 228 | // Lambda that enables a zoom orientation if the key modifier related to this orientation |
|
219 | 229 | // has |
|
220 | 230 | // been pressed |
|
221 | 231 | auto enableOrientation |
|
222 | 232 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { |
|
223 | 233 | auto orientationEnabled = event->modifiers().testFlag(modifier); |
|
224 | 234 | zoomOrientations.setFlag(orientation, orientationEnabled); |
|
225 | 235 | }; |
|
226 | 236 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); |
|
227 | 237 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); |
|
228 | 238 | |
|
229 | 239 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); |
|
230 | 240 | } |
|
231 | 241 | |
|
232 | 242 | void VisualizationGraphWidget::onDataCacheVariableUpdated() |
|
233 | 243 | { |
|
234 | 244 | // NOTE: |
|
235 | 245 | // We don't want to call the method for each component of a variable unitarily, but for |
|
236 | 246 | // all |
|
237 | 247 | // its components at once (eg its three components in the case of a vector). |
|
238 | 248 | |
|
239 | 249 | // The unordered_multimap does not do this easily, so the question is whether to: |
|
240 | 250 | // - use an ordered_multimap and the algos of std to group the values by key |
|
241 | 251 | // - use a map (unique keys) and store as values directly the list of components |
|
242 | 252 | |
|
243 | 253 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(); |
|
244 | 254 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
245 | 255 | auto variable = it->first; |
|
246 | 256 | VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, |
|
247 | 257 | variable->dataSeries(), variable->dateTime()); |
|
248 | 258 | } |
|
249 | 259 | } |
|
250 | 260 | |
|
251 | 261 | void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable) |
|
252 | 262 | { |
|
253 | 263 | auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable); |
|
254 | 264 | |
|
255 | 265 | auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{}; |
|
256 | 266 | |
|
257 | 267 | for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) { |
|
258 | 268 | abstractPlotableVect.push_back(it->second); |
|
259 | 269 | } |
|
260 | 270 | |
|
261 | 271 | VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(), |
|
262 | 272 | variable->dateTime()); |
|
263 | 273 | } |
General Comments 0
You need to be logged in to leave comments.
Login now