VisualizationGraphWidget.cpp
95 lines
| 3.0 KiB
| text/x-c
|
CppLexer
r93 | #include "Visualization/VisualizationGraphWidget.h" | |||
Alexandre Leroux
|
r171 | #include "Visualization/GraphPlottablesFactory.h" | ||
r58 | #include "ui_VisualizationGraphWidget.h" | |||
r111 | #include <Variable/Variable.h> | |||
#include <unordered_map> | ||||
Alexandre Leroux
|
r166 | namespace { | ||
/// Key pressed to enable zoom on horizontal axis | ||||
const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; | ||||
/// Key pressed to enable zoom on vertical axis | ||||
const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; | ||||
} // namespace | ||||
r111 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { | |||
// 1 variable -> n qcpplot | ||||
Alexandre Leroux
|
r171 | std::unordered_map<std::shared_ptr<Variable>, QCPAbstractPlottable *> m_VariableToPlotMap; | ||
r111 | }; | |||
Alexandre Leroux
|
r182 | VisualizationGraphWidget::VisualizationGraphWidget(const QString name, QWidget *parent) | ||
r113 | : QWidget{parent}, | |||
ui{new Ui::VisualizationGraphWidget}, | ||||
r111 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} | |||
r58 | { | |||
ui->setupUi(this); | ||||
Alexandre Leroux
|
r165 | |||
Alexandre Leroux
|
r182 | // qcpplot title | ||
ui->widget->plotLayout()->insertRow(0); | ||||
ui->widget->plotLayout()->addElement(0, 0, new QCPTextElement{ui->widget, name}); | ||||
Alexandre Leroux
|
r165 | // Set qcpplot properties : | ||
Alexandre Leroux
|
r167 | // - Drag (on x-axis) and zoom are enabled | ||
Alexandre Leroux
|
r166 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | ||
Alexandre Leroux
|
r165 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); | ||
Alexandre Leroux
|
r167 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | ||
Alexandre Leroux
|
r166 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | ||
r58 | } | |||
VisualizationGraphWidget::~VisualizationGraphWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r111 | ||||
void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable) | ||||
{ | ||||
Alexandre Leroux
|
r171 | // Uses delegate to create the qcpplot components according to the variable | ||
Alexandre Leroux
|
r174 | auto createdPlottables = GraphPlottablesFactory::create(variable, *ui->widget); | ||
Alexandre Leroux
|
r171 | |||
for (auto createdPlottable : qAsConst(createdPlottables)) { | ||||
impl->m_VariableToPlotMap.insert({variable, createdPlottable}); | ||||
} | ||||
r111 | } | |||
void VisualizationGraphWidget::accept(IVisualizationWidget *visitor) | ||||
{ | ||||
// TODO: manage the visitor | ||||
} | ||||
void VisualizationGraphWidget::close() | ||||
{ | ||||
// The main view cannot be directly closed. | ||||
return; | ||||
} | ||||
r112 | QString VisualizationGraphWidget::name() const | |||
r111 | { | |||
Alexandre Leroux
|
r182 | if (auto title = dynamic_cast<QCPTextElement *>(ui->widget->plotLayout()->elementAt(0))) { | ||
return title->text(); | ||||
} | ||||
else { | ||||
return QString{}; | ||||
} | ||||
r111 | } | |||
Alexandre Leroux
|
r166 | |||
void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept | ||||
{ | ||||
auto zoomOrientations = QFlags<Qt::Orientation>{}; | ||||
// Lambda that enables a zoom orientation if the key modifier related to this orientation has | ||||
// been pressed | ||||
auto enableOrientation | ||||
= [&zoomOrientations, event](const auto &orientation, const auto &modifier) { | ||||
auto orientationEnabled = event->modifiers().testFlag(modifier); | ||||
zoomOrientations.setFlag(orientation, orientationEnabled); | ||||
}; | ||||
enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); | ||||
enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); | ||||
ui->widget->axisRect()->setRangeZoom(zoomOrientations); | ||||
} | ||||