VisualizationWidget.cpp
172 lines
| 5.9 KiB
| text/x-c
|
CppLexer
r93 | #include "Visualization/VisualizationWidget.h" | |||
Alexandre Leroux
|
r192 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
Alexandre Leroux
|
r163 | #include "Visualization/VisualizationGraphWidget.h" | ||
r93 | #include "Visualization/VisualizationTabWidget.h" | |||
Alexandre Leroux
|
r163 | #include "Visualization/VisualizationZoneWidget.h" | ||
Alexandre Leroux
|
r675 | #include "Visualization/operations/FindVariableOperation.h" | ||
Alexandre Leroux
|
r231 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
Alexandre Leroux
|
r308 | #include "Visualization/operations/RemoveVariableOperation.h" | ||
r403 | #include "Visualization/operations/RescaleAxeOperation.h" | |||
r111 | #include "Visualization/qcustomplot.h" | |||
r58 | #include "ui_VisualizationWidget.h" | |||
r87 | ||||
#include <QToolButton> | ||||
Q_LOGGING_CATEGORY(LOG_VisualizationWidget, "VisualizationWidget") | ||||
r58 | ||||
VisualizationWidget::VisualizationWidget(QWidget *parent) | ||||
r89 | : QWidget{parent}, ui{new Ui::VisualizationWidget} | |||
r58 | { | |||
ui->setupUi(this); | ||||
r87 | ||||
r89 | auto addTabViewButton = new QToolButton{ui->tabWidget}; | |||
addTabViewButton->setText(tr("Add View")); | ||||
r87 | addTabViewButton->setCursor(Qt::ArrowCursor); | |||
ui->tabWidget->setCornerWidget(addTabViewButton, Qt::TopRightCorner); | ||||
r173 | ||||
Alexandre Leroux
|
r175 | auto enableMinimumCornerWidgetSize = [this](bool enable) { | ||
r173 | ||||
auto tabViewCornerWidget = ui->tabWidget->cornerWidget(); | ||||
auto width = enable ? tabViewCornerWidget->width() : 0; | ||||
auto height = enable ? tabViewCornerWidget->height() : 0; | ||||
tabViewCornerWidget->setMinimumHeight(height); | ||||
tabViewCornerWidget->setMinimumWidth(width); | ||||
ui->tabWidget->setMinimumHeight(height); | ||||
ui->tabWidget->setMinimumWidth(width); | ||||
}; | ||||
Alexandre Leroux
|
r175 | auto addTabView = [this, enableMinimumCornerWidgetSize]() { | ||
Alexandre Leroux
|
r184 | auto widget = new VisualizationTabWidget{QString{"View %1"}.arg(ui->tabWidget->count() + 1), | ||
ui->tabWidget}; | ||||
auto index = ui->tabWidget->addTab(widget, widget->name()); | ||||
r173 | if (ui->tabWidget->count() > 0) { | |||
Alexandre Leroux
|
r175 | enableMinimumCornerWidgetSize(false); | ||
r173 | } | |||
r87 | qCInfo(LOG_VisualizationWidget()) << tr("add the tab of index %1").arg(index); | |||
}; | ||||
Alexandre Leroux
|
r175 | auto removeTabView = [this, enableMinimumCornerWidgetSize](int index) { | ||
r173 | if (ui->tabWidget->count() == 1) { | |||
Alexandre Leroux
|
r175 | enableMinimumCornerWidgetSize(true); | ||
r173 | } | |||
Alexandre Leroux
|
r247 | // Removes widget from tab and closes it | ||
auto widget = ui->tabWidget->widget(index); | ||||
r87 | ui->tabWidget->removeTab(index); | |||
Alexandre Leroux
|
r247 | if (widget) { | ||
widget->close(); | ||||
} | ||||
r87 | qCInfo(LOG_VisualizationWidget()) << tr("remove the tab of index %1").arg(index); | |||
r173 | ||||
r87 | }; | |||
ui->tabWidget->setTabsClosable(true); | ||||
connect(addTabViewButton, &QToolButton::clicked, addTabView); | ||||
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, removeTabView); | ||||
Alexandre Leroux
|
r185 | |||
// Adds default tab | ||||
addTabView(); | ||||
r58 | } | |||
VisualizationWidget::~VisualizationWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r111 | ||||
Alexandre Leroux
|
r192 | void VisualizationWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r111 | { | |||
Alexandre Leroux
|
r193 | if (visitor) { | ||
visitor->visitEnter(this); | ||||
// Apply visitor for tab children | ||||
for (auto i = 0; i < ui->tabWidget->count(); ++i) { | ||||
Alexandre Leroux
|
r205 | // Widgets different from tabs are not visited (no action) | ||
Alexandre Leroux
|
r193 | if (auto visualizationTabWidget | ||
= dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) { | ||||
visualizationTabWidget->accept(visitor); | ||||
} | ||||
} | ||||
visitor->visitLeave(this); | ||||
} | ||||
Alexandre Leroux
|
r204 | else { | ||
qCCritical(LOG_VisualizationWidget()) << tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r111 | } | |||
Alexandre Leroux
|
r194 | bool VisualizationWidget::canDrop(const Variable &variable) const | ||
{ | ||||
// The main widget can never accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
Alexandre Leroux
|
r301 | bool VisualizationWidget::contains(const Variable &variable) const | ||
{ | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
r112 | QString VisualizationWidget::name() const | |||
r111 | { | |||
return QStringLiteral("MainView"); | ||||
} | ||||
Alexandre Leroux
|
r162 | |||
Alexandre Leroux
|
r266 | void VisualizationWidget::attachVariableMenu( | ||
QMenu *menu, const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
Alexandre Leroux
|
r162 | { | ||
Alexandre Leroux
|
r266 | // Menu is generated only if there is a single variable | ||
if (variables.size() == 1) { | ||||
if (auto variable = variables.first()) { | ||||
Alexandre Leroux
|
r675 | // Gets the containers of the variable | ||
FindVariableOperation findVariableOperation{variable}; | ||||
accept(&findVariableOperation); | ||||
auto variableContainers = findVariableOperation.result(); | ||||
Alexandre Leroux
|
r266 | // Generates the actions that make it possible to visualize the variable | ||
Alexandre Leroux
|
r675 | GenerateVariableMenuOperation generateVariableMenuOperation{ | ||
menu, variable, std::move(variableContainers)}; | ||||
Alexandre Leroux
|
r266 | accept(&generateVariableMenuOperation); | ||
} | ||||
else { | ||||
qCCritical(LOG_VisualizationWidget()) << tr( | ||||
"Can't generate the menu relative to the visualization: the variable is null"); | ||||
} | ||||
} | ||||
else { | ||||
qCDebug(LOG_VisualizationWidget()) | ||||
<< tr("No generation of the menu related to the visualization: several variables are " | ||||
"selected"); | ||||
} | ||||
Alexandre Leroux
|
r162 | } | ||
Alexandre Leroux
|
r308 | |||
void VisualizationWidget::onVariableAboutToBeDeleted(std::shared_ptr<Variable> variable) noexcept | ||||
{ | ||||
// Calls the operation of removing all references to the variable in the visualization | ||||
auto removeVariableOperation = RemoveVariableOperation{variable}; | ||||
accept(&removeVariableOperation); | ||||
} | ||||
r403 | ||||
void VisualizationWidget::onRangeChanged(std::shared_ptr<Variable> variable, | ||||
r471 | const SqpRange &range) noexcept | |||
r403 | { | |||
r413 | // Calls the operation of rescaling all graph that contrains variable in the visualization | |||
r403 | auto rescaleVariableOperation = RescaleAxeOperation{variable, range}; | |||
accept(&rescaleVariableOperation); | ||||
} | ||||
Alexandre Leroux
|
r677 | |||
void VisualizationWidget::closeEvent(QCloseEvent *event) | ||||
{ | ||||
// Closes tabs in the widget | ||||
for (auto i = 0; i < ui->tabWidget->count(); ++i) { | ||||
if (auto visualizationTabWidget | ||||
= dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) { | ||||
visualizationTabWidget->close(); | ||||
} | ||||
} | ||||
QWidget::closeEvent(event); | ||||
} | ||||