From 2d96f9af06b94c7748cb290dcd42b28269d10fc1 2017-09-11 09:44:38 From: Alexandre Leroux Date: 2017-09-11 09:44:38 Subject: [PATCH] Sets sames margin sides for graphs in a same zone --- diff --git a/gui/include/Visualization/IGraphSynchronizer.h b/gui/include/Visualization/IGraphSynchronizer.h new file mode 100644 index 0000000..cd05317 --- /dev/null +++ b/gui/include/Visualization/IGraphSynchronizer.h @@ -0,0 +1,24 @@ +#ifndef SCIQLOP_IGRAPHSYNCHRONIZER_H +#define SCIQLOP_IGRAPHSYNCHRONIZER_H + +class VisualizationGraphWidget; + +/** + * @brief The IVisualizationSynchronizer interface represents a delegate used to manage the + * synchronization between graphs, applying them processes or properties to ensure their + * synchronization + */ +class IGraphSynchronizer { + +public: + virtual ~IGraphSynchronizer() = default; + + /** + * Adds a graph as a new synchronized element, and sets its properties so that its + * synchronization is maintained with all other graphs managed by the synchonizer + * @param graph the graph to add in the synchronization + */ + virtual void addGraph(VisualizationGraphWidget &graph) const = 0; +}; + +#endif // SCIQLOP_IGRAPHSYNCHRONIZER_H diff --git a/gui/include/Visualization/QCustomPlotSynchronizer.h b/gui/include/Visualization/QCustomPlotSynchronizer.h new file mode 100644 index 0000000..3d8a397 --- /dev/null +++ b/gui/include/Visualization/QCustomPlotSynchronizer.h @@ -0,0 +1,26 @@ +#ifndef SCIQLOP_QCUSTOMPLOTSYNCHRONIZER_H +#define SCIQLOP_QCUSTOMPLOTSYNCHRONIZER_H + +#include "Visualization/IGraphSynchronizer.h" + +#include + +/** + * @brief The QCustomPlotSynchronizer class is an implementation of IGraphSynchronizer that handles + * graphs using QCustomPlot elements + * @sa IGraphSynchronizer + * @sa QCustomPlot + */ +class QCustomPlotSynchronizer : public IGraphSynchronizer { +public: + explicit QCustomPlotSynchronizer(); + + /// @sa IGraphSynchronizer::addGraph() + virtual void addGraph(VisualizationGraphWidget &graph) const override; + +private: + class QCustomPlotSynchronizerPrivate; + spimpl::unique_impl_ptr impl; +}; + +#endif // SCIQLOP_QCUSTOMPLOTSYNCHRONIZER_H diff --git a/gui/include/Visualization/VisualizationGraphWidget.h b/gui/include/Visualization/VisualizationGraphWidget.h index c396c82..3d2892f 100644 --- a/gui/include/Visualization/VisualizationGraphWidget.h +++ b/gui/include/Visualization/VisualizationGraphWidget.h @@ -24,6 +24,7 @@ class VisualizationGraphWidget; class VisualizationGraphWidget : public QWidget, public IVisualizationWidget { Q_OBJECT + friend class QCustomPlotSynchronizer; friend class VisualizationGraphRenderingDelegate; public: diff --git a/gui/meson.build b/gui/meson.build index db498e7..a8ece56 100644 --- a/gui/meson.build +++ b/gui/meson.build @@ -53,6 +53,7 @@ gui_sources = [ 'src/Visualization/VisualizationWidget.cpp', 'src/Visualization/VisualizationZoneWidget.cpp', 'src/Visualization/qcustomplot.cpp', + 'src/Visualization/QCustomPlotSynchronizer.cpp', 'src/Visualization/operations/GenerateVariableMenuOperation.cpp', 'src/Visualization/operations/MenuBuilder.cpp', 'src/Visualization/operations/RemoveVariableOperation.cpp', diff --git a/gui/src/Visualization/QCustomPlotSynchronizer.cpp b/gui/src/Visualization/QCustomPlotSynchronizer.cpp new file mode 100644 index 0000000..34796d1 --- /dev/null +++ b/gui/src/Visualization/QCustomPlotSynchronizer.cpp @@ -0,0 +1,30 @@ +#include "Visualization/QCustomPlotSynchronizer.h" + +#include "Visualization/VisualizationGraphWidget.h" +#include "Visualization/qcustomplot.h" + +struct QCustomPlotSynchronizer::QCustomPlotSynchronizerPrivate { + explicit QCustomPlotSynchronizerPrivate() + : m_MarginGroup{std::make_unique(nullptr)} + { + } + + /// Sets the same margin sides for all added plot elements + std::unique_ptr m_MarginGroup; +}; + +QCustomPlotSynchronizer::QCustomPlotSynchronizer() + : impl{spimpl::make_unique_impl()} +{ +} + +void QCustomPlotSynchronizer::addGraph(VisualizationGraphWidget &graph) const +{ + // Adds all plot elements of the graph to the margin group: all these elements will then have + // the same margin sides + auto &plot = graph.plot(); + for (auto axisRect : plot.axisRects()) { + // Sames margin sides at left and right + axisRect->setMarginGroup(QCP::msLeft | QCP::msRight, impl->m_MarginGroup.get()); + } +} diff --git a/gui/src/Visualization/VisualizationZoneWidget.cpp b/gui/src/Visualization/VisualizationZoneWidget.cpp index 561d20d..cb254e2 100644 --- a/gui/src/Visualization/VisualizationZoneWidget.cpp +++ b/gui/src/Visualization/VisualizationZoneWidget.cpp @@ -1,7 +1,7 @@ #include "Visualization/VisualizationZoneWidget.h" - #include "Visualization/IVisualizationWidgetVisitor.h" +#include "Visualization/QCustomPlotSynchronizer.h" #include "Visualization/VisualizationGraphWidget.h" #include "ui_VisualizationZoneWidget.h" @@ -38,8 +38,13 @@ QString defaultGraphName(const QLayout &layout) struct VisualizationZoneWidget::VisualizationZoneWidgetPrivate { - explicit VisualizationZoneWidgetPrivate() : m_SynchronisationGroupId{QUuid::createUuid()} {} + explicit VisualizationZoneWidgetPrivate() + : m_SynchronisationGroupId{QUuid::createUuid()}, + m_Synchronizer{std::make_unique()} + { + } QUuid m_SynchronisationGroupId; + std::unique_ptr m_Synchronizer; }; VisualizationZoneWidget::VisualizationZoneWidget(const QString &name, QWidget *parent) @@ -68,6 +73,9 @@ VisualizationZoneWidget::~VisualizationZoneWidget() void VisualizationZoneWidget::addGraph(VisualizationGraphWidget *graphWidget) { + // Synchronize new graph with others in the zone + impl->m_Synchronizer->addGraph(*graphWidget); + ui->visualizationZoneFrame->layout()->addWidget(graphWidget); }