##// END OF EJS Templates
Fix the cosinus bug....
perrinel -
r298:3a08c66e4df2
parent child
Show More
@@ -1,56 +1,56
1 #ifndef SCIQLOP_VARIABLE_H
1 #ifndef SCIQLOP_VARIABLE_H
2 #define SCIQLOP_VARIABLE_H
2 #define SCIQLOP_VARIABLE_H
3
3
4 #include <Data/SqpDateTime.h>
4 #include <Data/SqpDateTime.h>
5
5
6
6
7 #include <QLoggingCategory>
7 #include <QLoggingCategory>
8 #include <QObject>
8 #include <QObject>
9
9
10 #include <Common/spimpl.h>
10 #include <Common/spimpl.h>
11
11
12 Q_DECLARE_LOGGING_CATEGORY(LOG_Variable)
12 Q_DECLARE_LOGGING_CATEGORY(LOG_Variable)
13
13
14 class IDataSeries;
14 class IDataSeries;
15 class QString;
15 class QString;
16
16
17 /**
17 /**
18 * @brief The Variable class represents a variable in SciQlop.
18 * @brief The Variable class represents a variable in SciQlop.
19 */
19 */
20 class Variable : public QObject {
20 class Variable : public QObject {
21
21
22 Q_OBJECT
22 Q_OBJECT
23
23
24 public:
24 public:
25 explicit Variable(const QString &name, const QString &unit, const QString &mission,
25 explicit Variable(const QString &name, const QString &unit, const QString &mission,
26 const SqpDateTime &dateTime);
26 const SqpDateTime &dateTime);
27
27
28 QString name() const noexcept;
28 QString name() const noexcept;
29 QString mission() const noexcept;
29 QString mission() const noexcept;
30 QString unit() const noexcept;
30 QString unit() const noexcept;
31 SqpDateTime dateTime() const noexcept;
31 SqpDateTime dateTime() const noexcept;
32 void setDateTime(const SqpDateTime &dateTime) noexcept;
32 void setDateTime(const SqpDateTime &dateTime) noexcept;
33
33
34 /// @return the data of the variable, nullptr if there is no data
34 /// @return the data of the variable, nullptr if there is no data
35 IDataSeries *dataSeries() const noexcept;
35 IDataSeries *dataSeries() const noexcept;
36
36
37 bool contains(const SqpDateTime &dateTime);
37 bool contains(const SqpDateTime &dateTime);
38 bool intersect(const SqpDateTime &dateTime);
38 bool intersect(const SqpDateTime &dateTime);
39 void setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept;
39 void setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept;
40
40
41 public slots:
41 public slots:
42 void onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
42 void onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
43
43
44 signals:
44 signals:
45 void dataCacheUpdated();
45 void updated();
46
46
47
47
48 private:
48 private:
49 class VariablePrivate;
49 class VariablePrivate;
50 spimpl::unique_impl_ptr<VariablePrivate> impl;
50 spimpl::unique_impl_ptr<VariablePrivate> impl;
51 };
51 };
52
52
53 // Required for using shared_ptr in signals/slots
53 // Required for using shared_ptr in signals/slots
54 Q_DECLARE_METATYPE(std::shared_ptr<Variable>)
54 Q_DECLARE_METATYPE(std::shared_ptr<Variable>)
55
55
56 #endif // SCIQLOP_VARIABLE_H
56 #endif // SCIQLOP_VARIABLE_H
@@ -1,58 +1,57
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
3
3
4 #include <Data/SqpDateTime.h>
4 #include <Data/SqpDateTime.h>
5
5
6 #include <QLoggingCategory>
6 #include <QLoggingCategory>
7 #include <QObject>
7 #include <QObject>
8
8
9 #include <Common/spimpl.h>
9 #include <Common/spimpl.h>
10
10
11
11
12 class IDataProvider;
12 class IDataProvider;
13 class TimeController;
13 class TimeController;
14 class Variable;
14 class Variable;
15 class VariableModel;
15 class VariableModel;
16
16
17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
18
18
19 /**
19 /**
20 * @brief The VariableController class aims to handle the variables in SciQlop.
20 * @brief The VariableController class aims to handle the variables in SciQlop.
21 */
21 */
22 class VariableController : public QObject {
22 class VariableController : public QObject {
23 Q_OBJECT
23 Q_OBJECT
24 public:
24 public:
25 explicit VariableController(QObject *parent = 0);
25 explicit VariableController(QObject *parent = 0);
26 virtual ~VariableController();
26 virtual ~VariableController();
27
27
28 VariableModel *variableModel() noexcept;
28 VariableModel *variableModel() noexcept;
29
29
30 void setTimeController(TimeController *timeController) noexcept;
30 void setTimeController(TimeController *timeController) noexcept;
31
31
32
32
33 /// Request the data loading of the variable whithin dateTime
34 void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
35
36 signals:
33 signals:
37 /// Signal emitted when a variable has been created
34 /// Signal emitted when a variable has been created
38 void variableCreated(std::shared_ptr<Variable> variable);
35 void variableCreated(std::shared_ptr<Variable> variable);
39
36
40 public slots:
37 public slots:
38 /// Request the data loading of the variable whithin dateTime
39 void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
41 /**
40 /**
42 * Creates a new variable and adds it to the model
41 * Creates a new variable and adds it to the model
43 * @param name the name of the new variable
42 * @param name the name of the new variable
44 * @param provider the data provider for the new variable
43 * @param provider the data provider for the new variable
45 */
44 */
46 void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept;
45 void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept;
47
46
48 void initialize();
47 void initialize();
49 void finalize();
48 void finalize();
50
49
51 private:
50 private:
52 void waitForFinish();
51 void waitForFinish();
53
52
54 class VariableControllerPrivate;
53 class VariableControllerPrivate;
55 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
54 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
56 };
55 };
57
56
58 #endif // SCIQLOP_VARIABLECONTROLLER_H
57 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,87 +1,87
1 #include "Variable/Variable.h"
1 #include "Variable/Variable.h"
2
2
3 #include <Data/IDataSeries.h>
3 #include <Data/IDataSeries.h>
4 #include <Data/SqpDateTime.h>
4 #include <Data/SqpDateTime.h>
5
5
6 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
6 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
7
7
8 struct Variable::VariablePrivate {
8 struct Variable::VariablePrivate {
9 explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission,
9 explicit VariablePrivate(const QString &name, const QString &unit, const QString &mission,
10 const SqpDateTime &dateTime)
10 const SqpDateTime &dateTime)
11 : m_Name{name},
11 : m_Name{name},
12 m_Unit{unit},
12 m_Unit{unit},
13 m_Mission{mission},
13 m_Mission{mission},
14 m_DateTime{dateTime},
14 m_DateTime{dateTime},
15 m_DataSeries{nullptr}
15 m_DataSeries{nullptr}
16 {
16 {
17 }
17 }
18
18
19 QString m_Name;
19 QString m_Name;
20 QString m_Unit;
20 QString m_Unit;
21 QString m_Mission;
21 QString m_Mission;
22
22
23 SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache.
23 SqpDateTime m_DateTime; // The dateTime available in the view and loaded. not the cache.
24 std::unique_ptr<IDataSeries> m_DataSeries;
24 std::unique_ptr<IDataSeries> m_DataSeries;
25 };
25 };
26
26
27 Variable::Variable(const QString &name, const QString &unit, const QString &mission,
27 Variable::Variable(const QString &name, const QString &unit, const QString &mission,
28 const SqpDateTime &dateTime)
28 const SqpDateTime &dateTime)
29 : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission, dateTime)}
29 : impl{spimpl::make_unique_impl<VariablePrivate>(name, unit, mission, dateTime)}
30 {
30 {
31 }
31 }
32
32
33 QString Variable::name() const noexcept
33 QString Variable::name() const noexcept
34 {
34 {
35 return impl->m_Name;
35 return impl->m_Name;
36 }
36 }
37
37
38 QString Variable::mission() const noexcept
38 QString Variable::mission() const noexcept
39 {
39 {
40 return impl->m_Mission;
40 return impl->m_Mission;
41 }
41 }
42
42
43 QString Variable::unit() const noexcept
43 QString Variable::unit() const noexcept
44 {
44 {
45 return impl->m_Unit;
45 return impl->m_Unit;
46 }
46 }
47
47
48 SqpDateTime Variable::dateTime() const noexcept
48 SqpDateTime Variable::dateTime() const noexcept
49 {
49 {
50 return impl->m_DateTime;
50 return impl->m_DateTime;
51 }
51 }
52
52
53 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
53 void Variable::setDateTime(const SqpDateTime &dateTime) noexcept
54 {
54 {
55 impl->m_DateTime = dateTime;
55 impl->m_DateTime = dateTime;
56 }
56 }
57
57
58 void Variable::setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept
58 void Variable::setDataSeries(std::unique_ptr<IDataSeries> dataSeries) noexcept
59 {
59 {
60 if (!impl->m_DataSeries) {
60 if (!impl->m_DataSeries) {
61 impl->m_DataSeries = std::move(dataSeries);
61 impl->m_DataSeries = std::move(dataSeries);
62 }
62 }
63 }
63 }
64
64
65 void Variable::onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
65 void Variable::onAddDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
66 {
66 {
67 if (impl->m_DataSeries) {
67 if (impl->m_DataSeries) {
68 impl->m_DataSeries->merge(dataSeries.get());
68 impl->m_DataSeries->merge(dataSeries.get());
69
69
70 emit dataCacheUpdated();
70 emit updated();
71 }
71 }
72 }
72 }
73
73
74 IDataSeries *Variable::dataSeries() const noexcept
74 IDataSeries *Variable::dataSeries() const noexcept
75 {
75 {
76 return impl->m_DataSeries.get();
76 return impl->m_DataSeries.get();
77 }
77 }
78
78
79 bool Variable::contains(const SqpDateTime &dateTime)
79 bool Variable::contains(const SqpDateTime &dateTime)
80 {
80 {
81 return impl->m_DateTime.contains(dateTime);
81 return impl->m_DateTime.contains(dateTime);
82 }
82 }
83
83
84 bool Variable::intersect(const SqpDateTime &dateTime)
84 bool Variable::intersect(const SqpDateTime &dateTime)
85 {
85 {
86 return impl->m_DateTime.intersect(dateTime);
86 return impl->m_DateTime.intersect(dateTime);
87 }
87 }
@@ -1,164 +1,157
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableCacheController.h>
2 #include <Variable/VariableCacheController.h>
3 #include <Variable/VariableController.h>
3 #include <Variable/VariableController.h>
4 #include <Variable/VariableModel.h>
4 #include <Variable/VariableModel.h>
5
5
6 #include <Data/DataProviderParameters.h>
6 #include <Data/DataProviderParameters.h>
7 #include <Data/IDataProvider.h>
7 #include <Data/IDataProvider.h>
8 #include <Data/IDataSeries.h>
8 #include <Data/IDataSeries.h>
9 #include <Time/TimeController.h>
9 #include <Time/TimeController.h>
10
10
11 #include <QDateTime>
11 #include <QDateTime>
12 #include <QElapsedTimer>
13 #include <QMutex>
12 #include <QMutex>
14 #include <QThread>
13 #include <QThread>
15
14
16 #include <unordered_map>
15 #include <unordered_map>
17
16
18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
17 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19
18
20 namespace {
19 namespace {
21
20
22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
21 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 /// will be deleted when the timerange is recovered from SciQlop
22 /// will be deleted when the timerange is recovered from SciQlop
24 std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
23 std::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 const SqpDateTime &dateTime) noexcept
24 const SqpDateTime &dateTime) noexcept
26 {
25 {
27 auto parameters = DataProviderParameters{dateTime};
26 auto parameters = DataProviderParameters{dateTime};
28
27
29 return provider.retrieveData(parameters);
28 return provider.retrieveData(parameters);
30 }
29 }
31
30
32 } // namespace
31 } // namespace
33
32
34 struct VariableController::VariableControllerPrivate {
33 struct VariableController::VariableControllerPrivate {
35 explicit VariableControllerPrivate(VariableController *parent)
34 explicit VariableControllerPrivate(VariableController *parent)
36 : m_WorkingMutex{},
35 : m_WorkingMutex{},
37 m_VariableModel{new VariableModel{parent}},
36 m_VariableModel{new VariableModel{parent}},
38 m_VariableCacheController{std::make_unique<VariableCacheController>()}
37 m_VariableCacheController{std::make_unique<VariableCacheController>()}
39 {
38 {
40 }
39 }
41
40
42 QMutex m_WorkingMutex;
41 QMutex m_WorkingMutex;
43 /// Variable model. The VariableController has the ownership
42 /// Variable model. The VariableController has the ownership
44 VariableModel *m_VariableModel;
43 VariableModel *m_VariableModel;
45
44
46
45
47 TimeController *m_TimeController{nullptr};
46 TimeController *m_TimeController{nullptr};
48 std::unique_ptr<VariableCacheController> m_VariableCacheController;
47 std::unique_ptr<VariableCacheController> m_VariableCacheController;
49
48
50 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
49 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
51 m_VariableToProviderMap;
50 m_VariableToProviderMap;
52 };
51 };
53
52
54 VariableController::VariableController(QObject *parent)
53 VariableController::VariableController(QObject *parent)
55 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
54 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
56 {
55 {
57 qCDebug(LOG_VariableController()) << tr("VariableController construction")
56 qCDebug(LOG_VariableController()) << tr("VariableController construction")
58 << QThread::currentThread();
57 << QThread::currentThread();
59 }
58 }
60
59
61 VariableController::~VariableController()
60 VariableController::~VariableController()
62 {
61 {
63 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
62 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
64 << QThread::currentThread();
63 << QThread::currentThread();
65 this->waitForFinish();
64 this->waitForFinish();
66 }
65 }
67
66
68 VariableModel *VariableController::variableModel() noexcept
67 VariableModel *VariableController::variableModel() noexcept
69 {
68 {
70 return impl->m_VariableModel;
69 return impl->m_VariableModel;
71 }
70 }
72
71
73 void VariableController::setTimeController(TimeController *timeController) noexcept
72 void VariableController::setTimeController(TimeController *timeController) noexcept
74 {
73 {
75 impl->m_TimeController = timeController;
74 impl->m_TimeController = timeController;
76 }
75 }
77
76
78 void VariableController::createVariable(const QString &name,
77 void VariableController::createVariable(const QString &name,
79 std::shared_ptr<IDataProvider> provider) noexcept
78 std::shared_ptr<IDataProvider> provider) noexcept
80 {
79 {
81
80
82 if (!impl->m_TimeController) {
81 if (!impl->m_TimeController) {
83 qCCritical(LOG_VariableController())
82 qCCritical(LOG_VariableController())
84 << tr("Impossible to create variable: The time controller is null");
83 << tr("Impossible to create variable: The time controller is null");
85 return;
84 return;
86 }
85 }
87
86
88
87
89 /// @todo : for the moment :
88 /// @todo : for the moment :
90 /// - the provider is only used to retrieve data from the variable for its initialization, but
89 /// - the provider is only used to retrieve data from the variable for its initialization, but
91 /// it will be retained later
90 /// it will be retained later
92 /// - default data are generated for the variable, without taking into account the timerange set
91 /// - default data are generated for the variable, without taking into account the timerange set
93 /// in sciqlop
92 /// in sciqlop
94 auto dateTime = impl->m_TimeController->dateTime();
93 auto dateTime = impl->m_TimeController->dateTime();
95 if (auto newVariable = impl->m_VariableModel->createVariable(
94 if (auto newVariable = impl->m_VariableModel->createVariable(
96 name, dateTime, generateDefaultDataSeries(*provider, dateTime))) {
95 name, dateTime, generateDefaultDataSeries(*provider, dateTime))) {
97
96
98 // store the provider
97 // store the provider
99 impl->m_VariableToProviderMap[newVariable] = provider;
98 impl->m_VariableToProviderMap[newVariable] = provider;
100 qRegisterMetaType<std::shared_ptr<IDataSeries> >();
99 qRegisterMetaType<std::shared_ptr<IDataSeries> >();
101 qRegisterMetaType<SqpDateTime>();
100 qRegisterMetaType<SqpDateTime>();
102 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
101 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
103 &Variable::onAddDataSeries);
102 &Variable::onAddDataSeries);
104
103
105
104
106 // store in cache
105 // store in cache
107 impl->m_VariableCacheController->addDateTime(newVariable, dateTime);
106 impl->m_VariableCacheController->addDateTime(newVariable, dateTime);
108
107
109 // notify the creation
108 // notify the creation
110 emit variableCreated(newVariable);
109 emit variableCreated(newVariable);
111 }
110 }
112 }
111 }
113
112
114
113
115 void VariableController::requestDataLoading(std::shared_ptr<Variable> variable,
114 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
116 const SqpDateTime &dateTime)
115 const SqpDateTime &dateTime)
117 {
116 {
118 // we want to load data of the variable for the dateTime.
117 // we want to load data of the variable for the dateTime.
119 // First we check if the cache contains some of them.
118 // First we check if the cache contains some of them.
120 // For the other, we ask the provider to give them.
119 // For the other, we ask the provider to give them.
121 if (variable) {
120 if (variable) {
122
121
123 QElapsedTimer timer;
124 timer.start();
125 qCInfo(LOG_VariableController()) << "TORM: The slow s0 operation took" << timer.elapsed()
126 << "milliseconds";
127 auto dateTimeListNotInCache
122 auto dateTimeListNotInCache
128 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
123 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
129 qCInfo(LOG_VariableController()) << "TORM: The slow s1 operation took" << timer.elapsed()
130 << "milliseconds";
131
124
132 // Ask the provider for each data on the dateTimeListNotInCache
125 if (!dateTimeListNotInCache.empty()) {
133 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(dateTimeListNotInCache);
126 // Ask the provider for each data on the dateTimeListNotInCache
134
127 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
135 qCInfo(LOG_VariableController()) << "TORM: The slow s2 operation took" << timer.elapsed()
128 std::move(dateTimeListNotInCache));
136 << "milliseconds";
129 // store in cache
137
130 impl->m_VariableCacheController->addDateTime(variable, dateTime);
138 // store in cache
131 }
139 impl->m_VariableCacheController->addDateTime(variable, dateTime);
132 else {
140 qCInfo(LOG_VariableController()) << "TORM: The slow s3 operation took" << timer.elapsed()
133 emit variable->updated();
141 << "milliseconds";
134 }
142 }
135 }
143 else {
136 else {
144 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
137 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
145 }
138 }
146 }
139 }
147
140
148
141
149 void VariableController::initialize()
142 void VariableController::initialize()
150 {
143 {
151 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
144 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
152 impl->m_WorkingMutex.lock();
145 impl->m_WorkingMutex.lock();
153 qCDebug(LOG_VariableController()) << tr("VariableController init END");
146 qCDebug(LOG_VariableController()) << tr("VariableController init END");
154 }
147 }
155
148
156 void VariableController::finalize()
149 void VariableController::finalize()
157 {
150 {
158 impl->m_WorkingMutex.unlock();
151 impl->m_WorkingMutex.unlock();
159 }
152 }
160
153
161 void VariableController::waitForFinish()
154 void VariableController::waitForFinish()
162 {
155 {
163 QMutexLocker locker{&impl->m_WorkingMutex};
156 QMutexLocker locker{&impl->m_WorkingMutex};
164 }
157 }
@@ -1,59 +1,63
1 #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
1 #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
2 #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
2 #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
3
3
4 #include "Visualization/IVisualizationWidget.h"
4 #include "Visualization/IVisualizationWidget.h"
5
5
6 #include <QLoggingCategory>
6 #include <QLoggingCategory>
7 #include <QWidget>
7 #include <QWidget>
8
8
9 #include <memory>
9 #include <memory>
10
10
11 #include <Common/spimpl.h>
11 #include <Common/spimpl.h>
12
12
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget)
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget)
14
14
15 class QCPRange;
15 class QCPRange;
16 class SqpDateTime;
16 class Variable;
17 class Variable;
17
18
18 namespace Ui {
19 namespace Ui {
19 class VisualizationGraphWidget;
20 class VisualizationGraphWidget;
20 } // namespace Ui
21 } // namespace Ui
21
22
22 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
23 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
23 Q_OBJECT
24 Q_OBJECT
24
25
25 public:
26 public:
26 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
27 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
27 virtual ~VisualizationGraphWidget();
28 virtual ~VisualizationGraphWidget();
28
29
29 void addVariable(std::shared_ptr<Variable> variable);
30 void addVariable(std::shared_ptr<Variable> variable);
30 /// Removes a variable from the graph
31 /// Removes a variable from the graph
31 void removeVariable(std::shared_ptr<Variable> variable) noexcept;
32 void removeVariable(std::shared_ptr<Variable> variable) noexcept;
32
33
33 // IVisualizationWidget interface
34 // IVisualizationWidget interface
34 void accept(IVisualizationWidgetVisitor *visitor) override;
35 void accept(IVisualizationWidgetVisitor *visitor) override;
35 bool canDrop(const Variable &variable) const override;
36 bool canDrop(const Variable &variable) const override;
36 QString name() const override;
37 QString name() const override;
37
38
38 void updateDisplay(std::shared_ptr<Variable> variable);
39 void updateDisplay(std::shared_ptr<Variable> variable);
39
40
41 signals:
42 void requestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
43
40
44
41 private:
45 private:
42 Ui::VisualizationGraphWidget *ui;
46 Ui::VisualizationGraphWidget *ui;
43
47
44 class VisualizationGraphWidgetPrivate;
48 class VisualizationGraphWidgetPrivate;
45 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
49 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
46
50
47 private slots:
51 private slots:
48 /// Slot called when right clicking on the graph (displays a menu)
52 /// Slot called when right clicking on the graph (displays a menu)
49 void onGraphMenuRequested(const QPoint &pos) noexcept;
53 void onGraphMenuRequested(const QPoint &pos) noexcept;
50
54
51 void onRangeChanged(const QCPRange &t1, const QCPRange &t2);
55 void onRangeChanged(const QCPRange &t1, const QCPRange &t2);
52
56
53 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
57 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
54 void onMouseWheel(QWheelEvent *event) noexcept;
58 void onMouseWheel(QWheelEvent *event) noexcept;
55
59
56 void onDataCacheVariableUpdated();
60 void onDataCacheVariableUpdated();
57 };
61 };
58
62
59 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
63 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
@@ -1,151 +1,153
1 #include "Visualization/VisualizationGraphHelper.h"
1 #include "Visualization/VisualizationGraphHelper.h"
2 #include "Visualization/qcustomplot.h"
2 #include "Visualization/qcustomplot.h"
3
3
4 #include <Data/ScalarSeries.h>
4 #include <Data/ScalarSeries.h>
5
5
6 #include <Variable/Variable.h>
6 #include <Variable/Variable.h>
7
7
8 #include <QElapsedTimer>
8 #include <QElapsedTimer>
9
9
10 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
10 Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper")
11
11
12 namespace {
12 namespace {
13
13
14 /// Format for datetimes on a axis
14 /// Format for datetimes on a axis
15 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
15 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
16
16
17 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
17 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
18 /// non-time data
18 /// non-time data
19 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
19 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
20 {
20 {
21 if (isTimeAxis) {
21 if (isTimeAxis) {
22 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
22 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
23 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
23 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
24
24
25 return dateTicker;
25 return dateTicker;
26 }
26 }
27 else {
27 else {
28 // default ticker
28 // default ticker
29 return QSharedPointer<QCPAxisTicker>::create();
29 return QSharedPointer<QCPAxisTicker>::create();
30 }
30 }
31 }
31 }
32
32
33 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
33 void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries,
34 const SqpDateTime &dateTime)
34 const SqpDateTime &dateTime)
35 {
35 {
36 QElapsedTimer timer;
36 QElapsedTimer timer;
37 timer.start();
37 timer.start();
38 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
38 if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) {
39 // Clean the graph
39 // Clean the graph
40 qCDebug(LOG_VisualizationGraphHelper()) << "The slow s1 operation took" << timer.elapsed()
41 << "milliseconds";
42 // NAIVE approch
40 // NAIVE approch
43 const auto &xData = scalarSeries.xAxisData()->data();
41 const auto &xData = scalarSeries.xAxisData()->data();
44 const auto &valuesData = scalarSeries.valuesData()->data();
42 const auto &valuesData = scalarSeries.valuesData()->data();
43 const auto count = xData.count();
44 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count();
45
45
46 auto xValue = QVector<double>();
46 auto xValue = QVector<double>(count);
47 auto vValue = QVector<double>();
47 auto vValue = QVector<double>(count);
48
48
49 const auto count = xData.count();
49 int n = 0;
50 auto ite = 0;
51 for (auto i = 0; i < count; ++i) {
50 for (auto i = 0; i < count; ++i) {
52 const auto x = xData.at(i);
51 const auto x = xData[i];
53 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
52 if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) {
54 xValue.push_back(x);
53 xValue[n] = x;
55 vValue.push_back(valuesData.at(i));
54 vValue[n] = valuesData[i];
56 ++ite;
55 ++n;
57 }
56 }
58 }
57 }
59
58
60 qcpGraph->setData(xValue, vValue);
59 xValue.resize(n);
60 vValue.resize(n);
61
61
62 qCDebug(LOG_VisualizationGraphHelper()) << "The slow s2 operation took" << timer.elapsed()
62 qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed"
63 << "milliseconds";
63 << xValue.count();
64
65 qcpGraph->setData(xValue, vValue);
64 }
66 }
65 else {
67 else {
66 /// @todo DEBUG
68 /// @todo DEBUG
67 }
69 }
68 }
70 }
69
71
70 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
72 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot,
71 const SqpDateTime &dateTime)
73 const SqpDateTime &dateTime)
72 {
74 {
73 auto component = plot.addGraph();
75 auto component = plot.addGraph();
74
76
75 if (component) {
77 if (component) {
76 // // Graph data
78 // // Graph data
77 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
79 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
78 true);
80 true);
79
81
80 updateScalarData(component, scalarSeries, dateTime);
82 updateScalarData(component, scalarSeries, dateTime);
81
83
82 // Axes properties
84 // Axes properties
83 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
85 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
84 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
86 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
85
87
86 auto setAxisProperties = [](auto axis, const auto &unit) {
88 auto setAxisProperties = [](auto axis, const auto &unit) {
87 // label (unit name)
89 // label (unit name)
88 axis->setLabel(unit.m_Name);
90 axis->setLabel(unit.m_Name);
89
91
90 // ticker (depending on the type of unit)
92 // ticker (depending on the type of unit)
91 axis->setTicker(axisTicker(unit.m_TimeUnit));
93 axis->setTicker(axisTicker(unit.m_TimeUnit));
92 };
94 };
93 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
95 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
94 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
96 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
95
97
96 // Display all data
98 // Display all data
97 component->rescaleAxes();
99 component->rescaleAxes();
98
100
99 plot.replot();
101 plot.replot();
100 }
102 }
101 else {
103 else {
102 qCDebug(LOG_VisualizationGraphHelper())
104 qCDebug(LOG_VisualizationGraphHelper())
103 << QObject::tr("Can't create graph for the scalar series");
105 << QObject::tr("Can't create graph for the scalar series");
104 }
106 }
105
107
106 return component;
108 return component;
107 }
109 }
108
110
109 } // namespace
111 } // namespace
110
112
111 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
113 QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable,
112 QCustomPlot &plot) noexcept
114 QCustomPlot &plot) noexcept
113 {
115 {
114 auto result = QVector<QCPAbstractPlottable *>{};
116 auto result = QVector<QCPAbstractPlottable *>{};
115
117
116 if (variable) {
118 if (variable) {
117 // Gets the data series of the variable to call the creation of the right components
119 // Gets the data series of the variable to call the creation of the right components
118 // according to its type
120 // according to its type
119 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
121 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
120 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
122 result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime()));
121 }
123 }
122 else {
124 else {
123 qCDebug(LOG_VisualizationGraphHelper())
125 qCDebug(LOG_VisualizationGraphHelper())
124 << QObject::tr("Can't create graph plottables : unmanaged data series type");
126 << QObject::tr("Can't create graph plottables : unmanaged data series type");
125 }
127 }
126 }
128 }
127 else {
129 else {
128 qCDebug(LOG_VisualizationGraphHelper())
130 qCDebug(LOG_VisualizationGraphHelper())
129 << QObject::tr("Can't create graph plottables : the variable is null");
131 << QObject::tr("Can't create graph plottables : the variable is null");
130 }
132 }
131
133
132 return result;
134 return result;
133 }
135 }
134
136
135 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
137 void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect,
136 IDataSeries *dataSeries, const SqpDateTime &dateTime)
138 IDataSeries *dataSeries, const SqpDateTime &dateTime)
137 {
139 {
138 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
140 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) {
139 if (plotableVect.size() == 1) {
141 if (plotableVect.size() == 1) {
140 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
142 updateScalarData(plotableVect.at(0), *scalarSeries, dateTime);
141 }
143 }
142 else {
144 else {
143 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
145 qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr(
144 "Can't update Data of a scalarSeries because there is not only one component "
146 "Can't update Data of a scalarSeries because there is not only one component "
145 "associated");
147 "associated");
146 }
148 }
147 }
149 }
148 else {
150 else {
149 /// @todo DEBUG
151 /// @todo DEBUG
150 }
152 }
151 }
153 }
@@ -1,237 +1,236
1 #include "Visualization/VisualizationGraphWidget.h"
1 #include "Visualization/VisualizationGraphWidget.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/VisualizationGraphHelper.h"
3 #include "Visualization/VisualizationGraphHelper.h"
4 #include "ui_VisualizationGraphWidget.h"
4 #include "ui_VisualizationGraphWidget.h"
5
5
6 #include <Data/ArrayData.h>
6 #include <Data/ArrayData.h>
7 #include <Data/IDataSeries.h>
7 #include <Data/IDataSeries.h>
8 #include <SqpApplication.h>
8 #include <SqpApplication.h>
9 #include <Variable/Variable.h>
9 #include <Variable/Variable.h>
10 #include <Variable/VariableController.h>
10 #include <Variable/VariableController.h>
11
11
12 #include <unordered_map>
12 #include <unordered_map>
13
13
14 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
14 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
15
15
16 namespace {
16 namespace {
17
17
18 /// Key pressed to enable zoom on horizontal axis
18 /// Key pressed to enable zoom on horizontal axis
19 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
19 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
20
20
21 /// Key pressed to enable zoom on vertical axis
21 /// Key pressed to enable zoom on vertical axis
22 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
22 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
23
23
24 } // namespace
24 } // namespace
25
25
26 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
26 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
27
27
28 // 1 variable -> n qcpplot
28 // 1 variable -> n qcpplot
29 std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap;
29 std::multimap<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMultiMap;
30 };
30 };
31
31
32 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
32 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
33 : QWidget{parent},
33 : QWidget{parent},
34 ui{new Ui::VisualizationGraphWidget},
34 ui{new Ui::VisualizationGraphWidget},
35 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
35 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
36 {
36 {
37 ui->setupUi(this);
37 ui->setupUi(this);
38
38
39 ui->graphNameLabel->setText(name);
39 ui->graphNameLabel->setText(name);
40
40
41 // 'Close' options : widget is deleted when closed
41 // 'Close' options : widget is deleted when closed
42 setAttribute(Qt::WA_DeleteOnClose);
42 setAttribute(Qt::WA_DeleteOnClose);
43 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
43 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close);
44 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
44 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
45
45
46 // Set qcpplot properties :
46 // Set qcpplot properties :
47 // - Drag (on x-axis) and zoom are enabled
47 // - Drag (on x-axis) and zoom are enabled
48 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
48 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
49 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
49 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
50 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
50 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
51 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
51 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
52 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
52 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
53 &QCPAxis::rangeChanged),
53 &QCPAxis::rangeChanged),
54 this, &VisualizationGraphWidget::onRangeChanged);
54 this, &VisualizationGraphWidget::onRangeChanged);
55
55
56 // Activates menu when right clicking on the graph
56 // Activates menu when right clicking on the graph
57 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
57 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
58 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
58 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
59 &VisualizationGraphWidget::onGraphMenuRequested);
59 &VisualizationGraphWidget::onGraphMenuRequested);
60
61 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
62 &VariableController::onRequestDataLoading);
60 }
63 }
61
64
62
65
63 VisualizationGraphWidget::~VisualizationGraphWidget()
66 VisualizationGraphWidget::~VisualizationGraphWidget()
64 {
67 {
65 delete ui;
68 delete ui;
66 }
69 }
67
70
68 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
71 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
69 {
72 {
70 // Uses delegate to create the qcpplot components according to the variable
73 // Uses delegate to create the qcpplot components according to the variable
71 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
74 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
72
75
73 for (auto createdPlottable : qAsConst(createdPlottables)) {
76 for (auto createdPlottable : qAsConst(createdPlottables)) {
74 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
77 impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable});
75 }
78 }
76
79
77 connect(variable.get(), SIGNAL(dataCacheUpdated()), this, SLOT(onDataCacheVariableUpdated()));
80 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
78 }
81 }
79
82
80 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
83 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
81 {
84 {
82 // Each component associated to the variable :
85 // Each component associated to the variable :
83 // - is removed from qcpplot (which deletes it)
86 // - is removed from qcpplot (which deletes it)
84 // - is no longer referenced in the map
87 // - is no longer referenced in the map
85 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
88 auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
86 for (auto it = componentsIt.first; it != componentsIt.second;) {
89 for (auto it = componentsIt.first; it != componentsIt.second;) {
87 ui->widget->removePlottable(it->second);
90 ui->widget->removePlottable(it->second);
88 it = impl->m_VariableToPlotMultiMap.erase(it);
91 it = impl->m_VariableToPlotMultiMap.erase(it);
89 }
92 }
90
93
91 // Updates graph
94 // Updates graph
92 ui->widget->replot();
95 ui->widget->replot();
93 }
96 }
94
97
95 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
98 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
96 {
99 {
97 if (visitor) {
100 if (visitor) {
98 visitor->visit(this);
101 visitor->visit(this);
99 }
102 }
100 else {
103 else {
101 qCCritical(LOG_VisualizationGraphWidget())
104 qCCritical(LOG_VisualizationGraphWidget())
102 << tr("Can't visit widget : the visitor is null");
105 << tr("Can't visit widget : the visitor is null");
103 }
106 }
104 }
107 }
105
108
106 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
109 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
107 {
110 {
108 /// @todo : for the moment, a graph can always accomodate a variable
111 /// @todo : for the moment, a graph can always accomodate a variable
109 Q_UNUSED(variable);
112 Q_UNUSED(variable);
110 return true;
113 return true;
111 }
114 }
112
115
113 QString VisualizationGraphWidget::name() const
116 QString VisualizationGraphWidget::name() const
114 {
117 {
115 return ui->graphNameLabel->text();
118 return ui->graphNameLabel->text();
116 }
119 }
117
120
118 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
121 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
119 {
122 {
120 QMenu graphMenu{};
123 QMenu graphMenu{};
121
124
122 // Iterates on variables (unique keys)
125 // Iterates on variables (unique keys)
123 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
126 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
124 end = impl->m_VariableToPlotMultiMap.cend();
127 end = impl->m_VariableToPlotMultiMap.cend();
125 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
128 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
126 // 'Remove variable' action
129 // 'Remove variable' action
127 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
130 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
128 [ this, var = it->first ]() { removeVariable(var); });
131 [ this, var = it->first ]() { removeVariable(var); });
129 }
132 }
130
133
131 if (!graphMenu.isEmpty()) {
134 if (!graphMenu.isEmpty()) {
132 graphMenu.exec(mapToGlobal(pos));
135 graphMenu.exec(mapToGlobal(pos));
133 }
136 }
134 }
137 }
135
138
136 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
139 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
137 {
140 {
138
141
139 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
142 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::onRangeChanged");
140
143
141 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
144 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
142 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
145 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
143
146
144 auto variable = it->first;
147 auto variable = it->first;
145 qCInfo(LOG_VisualizationGraphWidget())
146 << tr("TORM: VisualizationGraphWidget::onRangeChanged")
147 << variable->dataSeries()->xAxisData()->size();
148 auto dateTime = SqpDateTime{t2.lower, t2.upper};
148 auto dateTime = SqpDateTime{t2.lower, t2.upper};
149
149
150 if (!variable->contains(dateTime)) {
150 if (!variable->contains(dateTime)) {
151
151
152 auto variableDateTimeWithTolerance = dateTime;
152 auto variableDateTimeWithTolerance = dateTime;
153 if (variable->intersect(dateTime)) {
153 if (variable->intersect(dateTime)) {
154 auto variableDateTime = variable->dateTime();
154 auto variableDateTime = variable->dateTime();
155 if (variableDateTime.m_TStart < dateTime.m_TStart) {
155 if (variableDateTime.m_TStart < dateTime.m_TStart) {
156
156
157 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
157 auto diffEndToKeepDelta = dateTime.m_TEnd - variableDateTime.m_TEnd;
158 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
158 dateTime.m_TStart = variableDateTime.m_TStart + diffEndToKeepDelta;
159 // Tolerance have to be added to the right
159 // Tolerance have to be added to the right
160 // add 10% tolerance for right (end) side
160 // add 10% tolerance for right (end) side
161 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
161 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
162 variableDateTimeWithTolerance.m_TEnd += tolerance;
162 variableDateTimeWithTolerance.m_TEnd += tolerance;
163 }
163 }
164 if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
164 if (variableDateTime.m_TEnd > dateTime.m_TEnd) {
165 auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart;
165 auto diffStartToKeepDelta = variableDateTime.m_TStart - dateTime.m_TStart;
166 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
166 dateTime.m_TEnd = variableDateTime.m_TEnd - diffStartToKeepDelta;
167 // Tolerance have to be added to the left
167 // Tolerance have to be added to the left
168 // add 10% tolerance for left (start) side
168 // add 10% tolerance for left (start) side
169 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
169 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
170 variableDateTimeWithTolerance.m_TStart -= tolerance;
170 variableDateTimeWithTolerance.m_TStart -= tolerance;
171 }
171 }
172 }
172 }
173 else {
173 else {
174 // add 10% tolerance for each side
174 // add 10% tolerance for each side
175 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
175 auto tolerance = 0.1 * (dateTime.m_TEnd - dateTime.m_TStart);
176 variableDateTimeWithTolerance.m_TStart -= tolerance;
176 variableDateTimeWithTolerance.m_TStart -= tolerance;
177 variableDateTimeWithTolerance.m_TEnd += tolerance;
177 variableDateTimeWithTolerance.m_TEnd += tolerance;
178 }
178 }
179 variable->setDateTime(dateTime);
179 variable->setDateTime(dateTime);
180
180
181 // CHangement detected, we need to ask controller to request data loading
181 // CHangement detected, we need to ask controller to request data loading
182 sqpApp->variableController().requestDataLoading(variable,
182 emit requestDataLoading(variable, variableDateTimeWithTolerance);
183 variableDateTimeWithTolerance);
184 }
183 }
185 }
184 }
186 }
185 }
187
186
188 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
187 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
189 {
188 {
190 auto zoomOrientations = QFlags<Qt::Orientation>{};
189 auto zoomOrientations = QFlags<Qt::Orientation>{};
191
190
192 // Lambda that enables a zoom orientation if the key modifier related to this orientation
191 // Lambda that enables a zoom orientation if the key modifier related to this orientation
193 // has
192 // has
194 // been pressed
193 // been pressed
195 auto enableOrientation
194 auto enableOrientation
196 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
195 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
197 auto orientationEnabled = event->modifiers().testFlag(modifier);
196 auto orientationEnabled = event->modifiers().testFlag(modifier);
198 zoomOrientations.setFlag(orientation, orientationEnabled);
197 zoomOrientations.setFlag(orientation, orientationEnabled);
199 };
198 };
200 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
199 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
201 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
200 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
202
201
203 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
202 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
204 }
203 }
205
204
206 void VisualizationGraphWidget::onDataCacheVariableUpdated()
205 void VisualizationGraphWidget::onDataCacheVariableUpdated()
207 {
206 {
208 // NOTE:
207 // NOTE:
209 // We don't want to call the method for each component of a variable unitarily, but for
208 // We don't want to call the method for each component of a variable unitarily, but for
210 // all
209 // all
211 // its components at once (eg its three components in the case of a vector).
210 // its components at once (eg its three components in the case of a vector).
212
211
213 // The unordered_multimap does not do this easily, so the question is whether to:
212 // The unordered_multimap does not do this easily, so the question is whether to:
214 // - use an ordered_multimap and the algos of std to group the values by key
213 // - use an ordered_multimap and the algos of std to group the values by key
215 // - use a map (unique keys) and store as values directly the list of components
214 // - use a map (unique keys) and store as values directly the list of components
216
215
217 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
216 for (auto it = impl->m_VariableToPlotMultiMap.cbegin();
218 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
217 it != impl->m_VariableToPlotMultiMap.cend(); ++it) {
219 auto variable = it->first;
218 auto variable = it->first;
220 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
219 VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second,
221 variable->dataSeries(), variable->dateTime());
220 variable->dataSeries(), variable->dateTime());
222 }
221 }
223 }
222 }
224
223
225 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
224 void VisualizationGraphWidget::updateDisplay(std::shared_ptr<Variable> variable)
226 {
225 {
227 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
226 auto abstractPlotableItPair = impl->m_VariableToPlotMultiMap.equal_range(variable);
228
227
229 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
228 auto abstractPlotableVect = QVector<QCPAbstractPlottable *>{};
230
229
231 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
230 for (auto it = abstractPlotableItPair.first; it != abstractPlotableItPair.second; ++it) {
232 abstractPlotableVect.push_back(it->second);
231 abstractPlotableVect.push_back(it->second);
233 }
232 }
234
233
235 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
234 VisualizationGraphHelper::updateData(abstractPlotableVect, variable->dataSeries(),
236 variable->dateTime());
235 variable->dateTime());
237 }
236 }
@@ -1,71 +1,74
1 #include "CosinusProvider.h"
1 #include "CosinusProvider.h"
2
2
3 #include <Data/DataProviderParameters.h>
3 #include <Data/DataProviderParameters.h>
4 #include <Data/ScalarSeries.h>
4 #include <Data/ScalarSeries.h>
5
5
6 #include <cmath>
6 #include <cmath>
7
7
8 #include <QDateTime>
9
8 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
10 Q_LOGGING_CATEGORY(LOG_CosinusProvider, "CosinusProvider")
9
11
10 std::unique_ptr<IDataSeries>
12 std::unique_ptr<IDataSeries>
11 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
13 CosinusProvider::retrieveData(const DataProviderParameters &parameters) const
12 {
14 {
13 auto dateTime = parameters.m_Time;
15 auto dateTime = parameters.m_Time;
14
16
15 // Gets the timerange from the parameters
17 // Gets the timerange from the parameters
16 auto start = dateTime.m_TStart;
18 auto start = dateTime.m_TStart;
17 auto end = dateTime.m_TEnd;
19 auto end = dateTime.m_TEnd;
18
20
21
19 // We assure that timerange is valid
22 // We assure that timerange is valid
20 if (end < start) {
23 if (end < start) {
21 std::swap(start, end);
24 std::swap(start, end);
22 }
25 }
23
26
24 // Generates scalar series containing cosinus values (one value per second)
27 // Generates scalar series containing cosinus values (one value per second)
25 auto scalarSeries
28 auto scalarSeries
26 = std::make_unique<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
29 = std::make_unique<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
27
30
28 auto dataIndex = 0;
31 auto dataIndex = 0;
29 for (auto time = start; time < end; ++time, ++dataIndex) {
32 for (auto time = start; time < end; ++time, ++dataIndex) {
30 scalarSeries->setData(dataIndex, time, std::cos(time));
33 scalarSeries->setData(dataIndex, time, std::cos(time));
31 }
34 }
32
35
33 return scalarSeries;
36 return scalarSeries;
34 }
37 }
35
38
36 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
39 void CosinusProvider::requestDataLoading(const QVector<SqpDateTime> &dateTimeList)
37 {
40 {
38 // NOTE: Try to use multithread if possible
41 // NOTE: Try to use multithread if possible
39 for (const auto &dateTime : dateTimeList) {
42 for (const auto &dateTime : dateTimeList) {
40
43
41 auto scalarSeries = this->retrieveDataSeries(dateTime);
44 auto scalarSeries = this->retrieveDataSeries(dateTime);
42
45
43 emit dataProvided(scalarSeries, dateTime);
46 emit dataProvided(scalarSeries, dateTime);
44 }
47 }
45 }
48 }
46
49
47
50
48 std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTime &dateTime)
51 std::shared_ptr<IDataSeries> CosinusProvider::retrieveDataSeries(const SqpDateTime &dateTime)
49 {
52 {
53 auto dataIndex = 0;
50
54
51 // Gets the timerange from the parameters
55 // Gets the timerange from the parameters
52 auto start = dateTime.m_TStart;
56 double freq = 100.0;
53 auto end = dateTime.m_TEnd;
57 double start = dateTime.m_TStart * freq; // 100 htz
58 double end = dateTime.m_TEnd * freq; // 100 htz
54
59
55 // We assure that timerange is valid
60 // We assure that timerange is valid
56 if (end < start) {
61 if (end < start) {
57 std::swap(start, end);
62 std::swap(start, end);
58 }
63 }
59
64
60 // Generates scalar series containing cosinus values (one value per second)
65 // Generates scalar series containing cosinus values (one value per second)
61 auto scalarSeries
66 auto scalarSeries
62 = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
67 = std::make_shared<ScalarSeries>(end - start, Unit{QStringLiteral("t"), true}, Unit{});
63
68
64 auto dataIndex = 0;
65 for (auto time = start; time < end; ++time, ++dataIndex) {
69 for (auto time = start; time < end; ++time, ++dataIndex) {
66 scalarSeries->setData(dataIndex, time, std::cos(time));
70 const auto timeOnFreq = time / freq;
71 scalarSeries->setData(dataIndex, timeOnFreq, std::cos(timeOnFreq));
67 }
72 }
68
69
70 return scalarSeries;
73 return scalarSeries;
71 }
74 }
General Comments 0
You need to be logged in to leave comments. Login now