##// END OF EJS Templates
Adds name to a graph...
Alexandre Leroux -
r196:8ddc77b998a0
parent child
Show More
@@ -1,43 +1,43
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 <QWidget>
7 7
8 8 #include <memory>
9 9
10 10 #include <Common/spimpl.h>
11 11
12 12 class Variable;
13 13
14 14 namespace Ui {
15 15 class VisualizationGraphWidget;
16 16 } // namespace Ui
17 17
18 18 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
19 19 Q_OBJECT
20 20
21 21 public:
22 explicit VisualizationGraphWidget(QWidget *parent = 0);
22 explicit VisualizationGraphWidget(const QString name = {}, QWidget *parent = 0);
23 23 virtual ~VisualizationGraphWidget();
24 24
25 25 void addVariable(std::shared_ptr<Variable> variable);
26 26
27 27 // IVisualizationWidget interface
28 28 void accept(IVisualizationWidget *visitor) override;
29 29 void close() override;
30 30 QString name() const;
31 31
32 32 private:
33 33 Ui::VisualizationGraphWidget *ui;
34 34
35 35 class VisualizationGraphWidgetPrivate;
36 36 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
37 37
38 38 private slots:
39 39 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
40 40 void onMouseWheel(QWheelEvent *event) noexcept;
41 41 };
42 42
43 43 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
@@ -1,86 +1,95
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/GraphPlottablesFactory.h"
3 3 #include "ui_VisualizationGraphWidget.h"
4 4
5 5 #include <Variable/Variable.h>
6 6
7 7 #include <unordered_map>
8 8
9 9 namespace {
10 10
11 11 /// Key pressed to enable zoom on horizontal axis
12 12 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
13 13
14 14 /// Key pressed to enable zoom on vertical axis
15 15 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
16 16
17 17 } // namespace
18 18
19 19 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
20 20
21 21 // 1 variable -> n qcpplot
22 22 std::unordered_map<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMap;
23 23 };
24 24
25 VisualizationGraphWidget::VisualizationGraphWidget(QWidget *parent)
25 VisualizationGraphWidget::VisualizationGraphWidget(const QString name, QWidget *parent)
26 26 : QWidget{parent},
27 27 ui{new Ui::VisualizationGraphWidget},
28 28 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()}
29 29 {
30 30 ui->setupUi(this);
31 31
32 // qcpplot title
33 ui->widget->plotLayout()->insertRow(0);
34 ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name});
35
32 36 // Set qcpplot properties :
33 37 // - Drag (on x-axis) and zoom are enabled
34 38 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
35 39 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
36 40 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
37 41 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
38 42 }
39 43
40 44 VisualizationGraphWidget::~VisualizationGraphWidget()
41 45 {
42 46 delete ui;
43 47 }
44 48
45 49 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
46 50 {
47 51 // Uses delegate to create the qcpplot components according to the variable
48 52 auto createdPlottables = GraphPlottablesFactory::create(variable, *ui->widget);
49 53
50 54 for (auto createdPlottable : qAsConst(createdPlottables)) {
51 55 impl->m_VariableToPlotMap.insert({variable, createdPlottable});
52 56 }
53 57 }
54 58
55 59 void VisualizationGraphWidget::accept(IVisualizationWidget *visitor)
56 60 {
57 61 // TODO: manage the visitor
58 62 }
59 63
60 64 void VisualizationGraphWidget::close()
61 65 {
62 66 // The main view cannot be directly closed.
63 67 return;
64 68 }
65 69
66 70 QString VisualizationGraphWidget::name() const
67 71 {
68 return QStringLiteral("MainView");
72 if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) {
73 return title->text();
74 }
75 else {
76 return QString{};
77 }
69 78 }
70 79
71 80 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
72 81 {
73 82 auto zoomOrientations = QFlags<Qt::Orientation>{};
74 83
75 84 // Lambda that enables a zoom orientation if the key modifier related to this orientation has
76 85 // been pressed
77 86 auto enableOrientation
78 87 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
79 88 auto orientationEnabled = event->modifiers().testFlag(modifier);
80 89 zoomOrientations.setFlag(orientation, orientationEnabled);
81 90 };
82 91 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
83 92 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
84 93
85 94 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
86 95 }
General Comments 0
You need to be logged in to leave comments. Login now