##// END OF EJS Templates
QCustomPlot notify the graph widget when the xRange changed
Alexandre Leroux -
r227:8492fb1ed747
parent child
Show More
@@ -1,47 +1,51
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 Variable;
16 class Variable;
16
17
17 namespace Ui {
18 namespace Ui {
18 class VisualizationGraphWidget;
19 class VisualizationGraphWidget;
19 } // namespace Ui
20 } // namespace Ui
20
21
21 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
22 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
22 Q_OBJECT
23 Q_OBJECT
23
24
24 public:
25 public:
25 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
26 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
26 virtual ~VisualizationGraphWidget();
27 virtual ~VisualizationGraphWidget();
27
28
28 void addVariable(std::shared_ptr<Variable> variable);
29 void addVariable(std::shared_ptr<Variable> variable);
29
30
30 // IVisualizationWidget interface
31 // IVisualizationWidget interface
31 void accept(IVisualizationWidgetVisitor *visitor) override;
32 void accept(IVisualizationWidgetVisitor *visitor) override;
32 bool canDrop(const Variable &variable) const override;
33 bool canDrop(const Variable &variable) const override;
33 void close() override;
34 void close() override;
34 QString name() const override;
35 QString name() const override;
35
36
36 private:
37 private:
37 Ui::VisualizationGraphWidget *ui;
38 Ui::VisualizationGraphWidget *ui;
38
39
39 class VisualizationGraphWidgetPrivate;
40 class VisualizationGraphWidgetPrivate;
40 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
41 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
41
42
42 private slots:
43 private slots:
44
45 void onRangeChanged(const QCPRange &t1, const QCPRange &t2);
46
43 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
47 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
44 void onMouseWheel(QWheelEvent *event) noexcept;
48 void onMouseWheel(QWheelEvent *event) noexcept;
45 };
49 };
46
50
47 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
51 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
@@ -1,111 +1,122
1 #include "Visualization/VisualizationGraphWidget.h"
1 #include "Visualization/VisualizationGraphWidget.h"
2 #include "Visualization/GraphPlottablesFactory.h"
2 #include "Visualization/GraphPlottablesFactory.h"
3 #include "Visualization/IVisualizationWidgetVisitor.h"
3 #include "Visualization/IVisualizationWidgetVisitor.h"
4 #include "ui_VisualizationGraphWidget.h"
4 #include "ui_VisualizationGraphWidget.h"
5
5
6 #include <Variable/Variable.h>
6 #include <Variable/Variable.h>
7
8 #include <unordered_map>
7 #include <unordered_map>
9
8
10 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
9 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
11
10
12 namespace {
11 namespace {
13
12
14 /// Key pressed to enable zoom on horizontal axis
13 /// Key pressed to enable zoom on horizontal axis
15 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
14 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
16
15
17 /// Key pressed to enable zoom on vertical axis
16 /// Key pressed to enable zoom on vertical axis
18 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
17 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
19
18
20 } // namespace
19 } // namespace
21
20
22 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
21 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
23
22
24 // 1 variable -> n qcpplot
23 // 1 variable -> n qcpplot
25 std::unordered_map<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMap;
24 std::unordered_map<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMap;
26 };
25 };
27
26
28 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
27 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
29 : QWidget{parent},
28 : QWidget{parent},
30 ui{new Ui::VisualizationGraphWidget},
29 ui{new Ui::VisualizationGraphWidget},
31 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
30 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
32 {
31 {
33 ui->setupUi(this);
32 ui->setupUi(this);
34
33
35 // qcpplot title
34 // qcpplot title
36 ui->widget->plotLayout()->insertRow(0);
35 ui->widget->plotLayout()->insertRow(0);
37 ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name});
36 ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name});
38
37
39 // Set qcpplot properties :
38 // Set qcpplot properties :
40 // - Drag (on x-axis) and zoom are enabled
39 // - Drag (on x-axis) and zoom are enabled
41 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
40 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
42 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
41 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
43 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
42 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
44 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
43 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
44 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
45 &QCPAxis::rangeChanged),
46 this, &VisualizationGraphWidget::onRangeChanged);
45 }
47 }
46
48
49
47 VisualizationGraphWidget::~VisualizationGraphWidget()
50 VisualizationGraphWidget::~VisualizationGraphWidget()
48 {
51 {
49 delete ui;
52 delete ui;
50 }
53 }
51
54
52 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
55 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
53 {
56 {
54 // Uses delegate to create the qcpplot components according to the variable
57 // Uses delegate to create the qcpplot components according to the variable
55 auto createdPlottables = GraphPlottablesFactory::create(variable, *ui->widget);
58 auto createdPlottables = GraphPlottablesFactory::create(variable, *ui->widget);
56
59
57 for (auto createdPlottable : qAsConst(createdPlottables)) {
60 for (auto createdPlottable : qAsConst(createdPlottables)) {
58 impl->m_VariableToPlotMap.insert({variable, createdPlottable});
61 impl->m_VariableToPlotMap.insert({variable, createdPlottable});
59 }
62 }
60 }
63 }
61
64
62 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
65 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
63 {
66 {
64 if (visitor) {
67 if (visitor) {
65 visitor->visit(this);
68 visitor->visit(this);
66 }
69 }
67 else {
70 else {
68 qCCritical(LOG_VisualizationGraphWidget())
71 qCCritical(LOG_VisualizationGraphWidget())
69 << tr("Can't visit widget : the visitor is null");
72 << tr("Can't visit widget : the visitor is null");
70 }
73 }
71 }
74 }
72
75
73 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
76 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
74 {
77 {
75 /// @todo : for the moment, a graph can always accomodate a variable
78 /// @todo : for the moment, a graph can always accomodate a variable
76 Q_UNUSED(variable);
79 Q_UNUSED(variable);
77 return true;
80 return true;
78 }
81 }
79
82
80 void VisualizationGraphWidget::close()
83 void VisualizationGraphWidget::close()
81 {
84 {
82 // The main view cannot be directly closed.
85 // The main view cannot be directly closed.
83 return;
86 return;
84 }
87 }
85
88
86 QString VisualizationGraphWidget::name() const
89 QString VisualizationGraphWidget::name() const
87 {
90 {
88 if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) {
91 if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) {
89 return title->text();
92 return title->text();
90 }
93 }
91 else {
94 else {
92 return QString{};
95 return QString{};
93 }
96 }
94 }
97 }
95
98
99 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
100 {
101 for (auto it = impl->m_VariableToPlotMap.cbegin(); it != impl->m_VariableToPlotMap.cend();
102 ++it) {
103 it->first->onXRangeChanged(SqpDateTime{t2.lower, t2.upper});
104 }
105 }
106
96 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
107 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
97 {
108 {
98 auto zoomOrientations = QFlags<Qt::Orientation>{};
109 auto zoomOrientations = QFlags<Qt::Orientation>{};
99
110
100 // Lambda that enables a zoom orientation if the key modifier related to this orientation has
111 // Lambda that enables a zoom orientation if the key modifier related to this orientation has
101 // been pressed
112 // been pressed
102 auto enableOrientation
113 auto enableOrientation
103 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
114 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
104 auto orientationEnabled = event->modifiers().testFlag(modifier);
115 auto orientationEnabled = event->modifiers().testFlag(modifier);
105 zoomOrientations.setFlag(orientation, orientationEnabled);
116 zoomOrientations.setFlag(orientation, orientationEnabled);
106 };
117 };
107 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
118 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
108 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
119 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
109
120
110 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
121 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
111 }
122 }
General Comments 0
You need to be logged in to leave comments. Login now