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