##// END OF EJS Templates
Pass Variable as shared_ptr
Alexandre Leroux -
r187:60a84e67b911
parent child
Show More
@@ -1,30 +1,32
1 1 #ifndef SCIQLOP_GRAPHPLOTTABLESFACTORY_H
2 2 #define SCIQLOP_GRAPHPLOTTABLESFACTORY_H
3 3
4 4 #include <QLoggingCategory>
5 5 #include <QVector>
6 6
7 #include <memory>
8
7 9 Q_DECLARE_LOGGING_CATEGORY(LOG_GraphPlottablesFactory)
8 10
9 11 class QCPAbstractPlottable;
10 12 class QCustomPlot;
11 13 class Variable;
12 14
13 15 /**
14 16 * @brief The GraphPlottablesFactory class aims to create the QCustomPlot components relative to a
15 17 * variable, depending on the data series of this variable
16 18 */
17 19 struct GraphPlottablesFactory {
18 20 /**
19 21 * Creates (if possible) the QCustomPlot components relative to the variable passed in
20 22 * parameter, and adds these to the plot passed in parameter.
21 23 * @param variable the variable for which to create the components
22 24 * @param plot the plot in which to add the created components. It takes ownership of these
23 25 * components.
24 26 * @return the list of the components created
25 27 */
26 static QVector<QCPAbstractPlottable *> create(const Variable *variable,
28 static QVector<QCPAbstractPlottable *> create(std::shared_ptr<Variable> variable,
27 29 QCustomPlot &plot) noexcept;
28 30 };
29 31
30 32 #endif // SCIQLOP_GRAPHPLOTTABLESFACTORY_H
@@ -1,91 +1,91
1 1 #include "Visualization/GraphPlottablesFactory.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 Q_LOGGING_CATEGORY(LOG_GraphPlottablesFactory, "GraphPlottablesFactory")
9 9
10 10 namespace {
11 11
12 12 /// Format for datetimes on a axis
13 13 const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss");
14 14
15 15 /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or
16 16 /// non-time data
17 17 QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis)
18 18 {
19 19 if (isTimeAxis) {
20 20 auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create();
21 21 dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT);
22 22
23 23 return dateTicker;
24 24 }
25 25 else {
26 26 // default ticker
27 27 return QSharedPointer<QCPAxisTicker>::create();
28 28 }
29 29 }
30 30
31 31 QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot)
32 32 {
33 33 auto component = plot.addGraph();
34 34
35 35 if (component) {
36 36 // Graph data
37 37 component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(),
38 38 true);
39 39
40 40 // Axes properties
41 41 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
42 42 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
43 43
44 44 auto setAxisProperties = [](auto axis, const auto &unit) {
45 45 // label (unit name)
46 46 axis->setLabel(unit.m_Name);
47 47
48 48 // ticker (depending on the type of unit)
49 49 axis->setTicker(axisTicker(unit.m_TimeUnit));
50 50 };
51 51 setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit());
52 52 setAxisProperties(plot.yAxis, scalarSeries.valuesUnit());
53 53
54 54 // Display all data
55 55 component->rescaleAxes();
56 56
57 57 plot.replot();
58 58 }
59 59 else {
60 60 qCDebug(LOG_GraphPlottablesFactory())
61 61 << QObject::tr("Can't create graph for the scalar series");
62 62 }
63 63
64 64 return component;
65 65 }
66 66
67 67 } // namespace
68 68
69 QVector<QCPAbstractPlottable *> GraphPlottablesFactory::create(const Variable *variable,
69 QVector<QCPAbstractPlottable *> GraphPlottablesFactory::create(std::shared_ptr<Variable> variable,
70 70 QCustomPlot &plot) noexcept
71 71 {
72 72 auto result = QVector<QCPAbstractPlottable *>{};
73 73
74 74 if (variable) {
75 75 // Gets the data series of the variable to call the creation of the right components
76 76 // according to its type
77 77 if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) {
78 78 result.append(createScalarSeriesComponent(*scalarSeries, plot));
79 79 }
80 80 else {
81 81 qCDebug(LOG_GraphPlottablesFactory())
82 82 << QObject::tr("Can't create graph plottables : unmanaged data series type");
83 83 }
84 84 }
85 85 else {
86 86 qCDebug(LOG_GraphPlottablesFactory())
87 87 << QObject::tr("Can't create graph plottables : the variable is null");
88 88 }
89 89
90 90 return result;
91 91 }
@@ -1,86 +1,86
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 25 VisualizationGraphWidget::VisualizationGraphWidget(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 32 // Set qcpplot properties :
33 33 // - Drag (on x-axis) and zoom are enabled
34 34 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
35 35 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
36 36 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
37 37 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
38 38 }
39 39
40 40 VisualizationGraphWidget::~VisualizationGraphWidget()
41 41 {
42 42 delete ui;
43 43 }
44 44
45 45 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable)
46 46 {
47 47 // Uses delegate to create the qcpplot components according to the variable
48 auto createdPlottables = GraphPlottablesFactory::create(variable.get(), *ui->widget);
48 auto createdPlottables = GraphPlottablesFactory::create(variable, *ui->widget);
49 49
50 50 for (auto createdPlottable : qAsConst(createdPlottables)) {
51 51 impl->m_VariableToPlotMap.insert({variable, createdPlottable});
52 52 }
53 53 }
54 54
55 55 void VisualizationGraphWidget::accept(IVisualizationWidget *visitor)
56 56 {
57 57 // TODO: manage the visitor
58 58 }
59 59
60 60 void VisualizationGraphWidget::close()
61 61 {
62 62 // The main view cannot be directly closed.
63 63 return;
64 64 }
65 65
66 66 QString VisualizationGraphWidget::name() const
67 67 {
68 68 return QStringLiteral("MainView");
69 69 }
70 70
71 71 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
72 72 {
73 73 auto zoomOrientations = QFlags<Qt::Orientation>{};
74 74
75 75 // Lambda that enables a zoom orientation if the key modifier related to this orientation has
76 76 // been pressed
77 77 auto enableOrientation
78 78 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
79 79 auto orientationEnabled = event->modifiers().testFlag(modifier);
80 80 zoomOrientations.setFlag(orientation, orientationEnabled);
81 81 };
82 82 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
83 83 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
84 84
85 85 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
86 86 }
General Comments 0
You need to be logged in to leave comments. Login now