VisualizationWidget.cpp
178 lines
| 6.1 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationWidget.h" | |||
Alexandre Leroux
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
Alexandre Leroux
|
r176 | #include "Visualization/VisualizationGraphWidget.h" | ||
r95 | #include "Visualization/VisualizationTabWidget.h" | |||
Alexandre Leroux
|
r176 | #include "Visualization/VisualizationZoneWidget.h" | ||
Alexandre Leroux
|
r736 | #include "Visualization/operations/FindVariableOperation.h" | ||
Alexandre Leroux
|
r248 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
Alexandre Leroux
|
r334 | #include "Visualization/operations/RemoveVariableOperation.h" | ||
r437 | #include "Visualization/operations/RescaleAxeOperation.h" | |||
r118 | #include "Visualization/qcustomplot.h" | |||
r58 | #include "ui_VisualizationWidget.h" | |||
r88 | ||||
r886 | #include "DragAndDrop/DragDropHelper.h" | |||
#include "SqpApplication.h" | ||||
r88 | #include <QToolButton> | |||
Q_LOGGING_CATEGORY(LOG_VisualizationWidget, "VisualizationWidget") | ||||
r58 | ||||
VisualizationWidget::VisualizationWidget(QWidget *parent) | ||||
r91 | : QWidget{parent}, ui{new Ui::VisualizationWidget} | |||
r58 | { | |||
ui->setupUi(this); | ||||
r88 | ||||
r91 | auto addTabViewButton = new QToolButton{ui->tabWidget}; | |||
addTabViewButton->setText(tr("Add View")); | ||||
r88 | addTabViewButton->setCursor(Qt::ArrowCursor); | |||
ui->tabWidget->setCornerWidget(addTabViewButton, Qt::TopRightCorner); | ||||
r186 | ||||
Alexandre Leroux
|
r188 | auto enableMinimumCornerWidgetSize = [this](bool enable) { | ||
r186 | ||||
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
|
r188 | auto addTabView = [this, enableMinimumCornerWidgetSize]() { | ||
Alexandre Leroux
|
r198 | auto widget = new VisualizationTabWidget{QString{"View %1"}.arg(ui->tabWidget->count() + 1), | ||
ui->tabWidget}; | ||||
auto index = ui->tabWidget->addTab(widget, widget->name()); | ||||
r186 | if (ui->tabWidget->count() > 0) { | |||
Alexandre Leroux
|
r188 | enableMinimumCornerWidgetSize(false); | ||
r186 | } | |||
r88 | qCInfo(LOG_VisualizationWidget()) << tr("add the tab of index %1").arg(index); | |||
}; | ||||
Alexandre Leroux
|
r188 | auto removeTabView = [this, enableMinimumCornerWidgetSize](int index) { | ||
r186 | if (ui->tabWidget->count() == 1) { | |||
Alexandre Leroux
|
r188 | enableMinimumCornerWidgetSize(true); | ||
r186 | } | |||
Alexandre Leroux
|
r267 | // Removes widget from tab and closes it | ||
auto widget = ui->tabWidget->widget(index); | ||||
r88 | ui->tabWidget->removeTab(index); | |||
Alexandre Leroux
|
r267 | if (widget) { | ||
widget->close(); | ||||
} | ||||
r88 | qCInfo(LOG_VisualizationWidget()) << tr("remove the tab of index %1").arg(index); | |||
r186 | ||||
r88 | }; | |||
ui->tabWidget->setTabsClosable(true); | ||||
connect(addTabViewButton, &QToolButton::clicked, addTabView); | ||||
connect(ui->tabWidget, &QTabWidget::tabCloseRequested, removeTabView); | ||||
Alexandre Leroux
|
r199 | |||
r886 | sqpApp->dragDropHelper().addDragDropTabBar(ui->tabWidget->tabBar()); | |||
Alexandre Leroux
|
r199 | // Adds default tab | ||
addTabView(); | ||||
r58 | } | |||
VisualizationWidget::~VisualizationWidget() | ||||
{ | ||||
r886 | sqpApp->dragDropHelper().removeDragDropTabBar(ui->tabWidget->tabBar()); | |||
r58 | delete ui; | |||
} | ||||
r118 | ||||
Alexandre Leroux
|
r207 | void VisualizationWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r118 | { | |||
Alexandre Leroux
|
r208 | if (visitor) { | ||
visitor->visitEnter(this); | ||||
// Apply visitor for tab children | ||||
for (auto i = 0; i < ui->tabWidget->count(); ++i) { | ||||
Alexandre Leroux
|
r220 | // Widgets different from tabs are not visited (no action) | ||
Alexandre Leroux
|
r208 | if (auto visualizationTabWidget | ||
= dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) { | ||||
visualizationTabWidget->accept(visitor); | ||||
} | ||||
} | ||||
visitor->visitLeave(this); | ||||
} | ||||
Alexandre Leroux
|
r219 | else { | ||
qCCritical(LOG_VisualizationWidget()) << tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r118 | } | |||
Alexandre Leroux
|
r209 | bool VisualizationWidget::canDrop(const Variable &variable) const | ||
{ | ||||
// The main widget can never accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
Alexandre Leroux
|
r327 | bool VisualizationWidget::contains(const Variable &variable) const | ||
{ | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
r119 | QString VisualizationWidget::name() const | |||
r118 | { | |||
return QStringLiteral("MainView"); | ||||
} | ||||
Alexandre Leroux
|
r175 | |||
Alexandre Leroux
|
r288 | void VisualizationWidget::attachVariableMenu( | ||
QMenu *menu, const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
Alexandre Leroux
|
r175 | { | ||
Alexandre Leroux
|
r288 | // Menu is generated only if there is a single variable | ||
if (variables.size() == 1) { | ||||
if (auto variable = variables.first()) { | ||||
Alexandre Leroux
|
r736 | // Gets the containers of the variable | ||
FindVariableOperation findVariableOperation{variable}; | ||||
accept(&findVariableOperation); | ||||
auto variableContainers = findVariableOperation.result(); | ||||
Alexandre Leroux
|
r288 | // Generates the actions that make it possible to visualize the variable | ||
Alexandre Leroux
|
r736 | GenerateVariableMenuOperation generateVariableMenuOperation{ | ||
menu, variable, std::move(variableContainers)}; | ||||
Alexandre Leroux
|
r288 | 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
|
r175 | } | ||
Alexandre Leroux
|
r334 | |||
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); | ||||
} | ||||
r437 | ||||
void VisualizationWidget::onRangeChanged(std::shared_ptr<Variable> variable, | ||||
r512 | const SqpRange &range) noexcept | |||
r437 | { | |||
r447 | // Calls the operation of rescaling all graph that contrains variable in the visualization | |||
r437 | auto rescaleVariableOperation = RescaleAxeOperation{variable, range}; | |||
accept(&rescaleVariableOperation); | ||||
} | ||||
Alexandre Leroux
|
r738 | |||
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); | ||||
} | ||||