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