VisualizationWidget.cpp
201 lines
| 6.8 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationWidget.h" | |||
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r1078 | #include "Visualization/VisualizationActionManager.h" | |||
|
r176 | #include "Visualization/VisualizationGraphWidget.h" | ||
r1049 | #include "Visualization/VisualizationSelectionZoneManager.h" | |||
r95 | #include "Visualization/VisualizationTabWidget.h" | |||
|
r176 | #include "Visualization/VisualizationZoneWidget.h" | ||
|
r736 | #include "Visualization/operations/FindVariableOperation.h" | ||
|
r248 | #include "Visualization/operations/GenerateVariableMenuOperation.h" | ||
|
r334 | #include "Visualization/operations/RemoveVariableOperation.h" | ||
r437 | #include "Visualization/operations/RescaleAxeOperation.h" | |||
r118 | #include "Visualization/qcustomplot.h" | |||
r58 | #include "ui_VisualizationWidget.h" | |||
r88 | ||||
r1075 | #include "DragAndDrop/DragDropGuiController.h" | |||
r886 | #include "SqpApplication.h" | |||
r88 | #include <QToolButton> | |||
r1049 | #include <memory> | |||
r88 | Q_LOGGING_CATEGORY(LOG_VisualizationWidget, "VisualizationWidget") | |||
r58 | ||||
r1049 | struct VisualizationWidget::VisualizationWidgetPrivate { | |||
std::unique_ptr<VisualizationSelectionZoneManager> m_ZoneSelectionManager = nullptr; | ||||
r1078 | VisualizationActionManager m_ActionManager; | |||
r1049 | ||||
VisualizationWidgetPrivate() | ||||
: m_ZoneSelectionManager(std::make_unique<VisualizationSelectionZoneManager>()) | ||||
{ | ||||
} | ||||
}; | ||||
r58 | VisualizationWidget::VisualizationWidget(QWidget *parent) | |||
r1049 | : QWidget{parent}, | |||
ui{new Ui::VisualizationWidget}, | ||||
impl{spimpl::make_unique_impl<VisualizationWidgetPrivate>()} | ||||
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 | ||||
|
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); | ||||
}; | ||||
|
r188 | auto addTabView = [this, enableMinimumCornerWidgetSize]() { | ||
|
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) { | |||
|
r188 | enableMinimumCornerWidgetSize(false); | ||
r186 | } | |||
r88 | qCInfo(LOG_VisualizationWidget()) << tr("add the tab of index %1").arg(index); | |||
}; | ||||
|
r188 | auto removeTabView = [this, enableMinimumCornerWidgetSize](int index) { | ||
r186 | if (ui->tabWidget->count() == 1) { | |||
|
r188 | enableMinimumCornerWidgetSize(true); | ||
r186 | } | |||
|
r267 | // Removes widget from tab and closes it | ||
auto widget = ui->tabWidget->widget(index); | ||||
r88 | ui->tabWidget->removeTab(index); | |||
|
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); | ||||
|
r199 | |||
r1075 | sqpApp->dragDropGuiController().addDragDropTabBar(ui->tabWidget->tabBar()); | |||
r886 | ||||
r1078 | impl->m_ActionManager.installSelectionZoneActions(); | |||
|
r199 | // Adds default tab | ||
addTabView(); | ||||
r58 | } | |||
VisualizationWidget::~VisualizationWidget() | ||||
{ | ||||
r1075 | sqpApp->dragDropGuiController().removeDragDropTabBar(ui->tabWidget->tabBar()); | |||
r58 | delete ui; | |||
} | ||||
r118 | ||||
r1049 | VisualizationSelectionZoneManager &VisualizationWidget::selectionZoneManager() const | |||
{ | ||||
return *impl->m_ZoneSelectionManager.get(); | ||||
} | ||||
|
r207 | void VisualizationWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r118 | { | |||
|
r208 | if (visitor) { | ||
visitor->visitEnter(this); | ||||
// Apply visitor for tab children | ||||
for (auto i = 0; i < ui->tabWidget->count(); ++i) { | ||||
|
r220 | // Widgets different from tabs are not visited (no action) | ||
|
r208 | if (auto visualizationTabWidget | ||
= dynamic_cast<VisualizationTabWidget *>(ui->tabWidget->widget(i))) { | ||||
visualizationTabWidget->accept(visitor); | ||||
} | ||||
} | ||||
visitor->visitLeave(this); | ||||
} | ||||
|
r219 | else { | ||
qCCritical(LOG_VisualizationWidget()) << tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r118 | } | |||
|
r209 | bool VisualizationWidget::canDrop(const Variable &variable) const | ||
{ | ||||
// The main widget can never accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
|
r327 | bool VisualizationWidget::contains(const Variable &variable) const | ||
{ | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
r119 | QString VisualizationWidget::name() const | |||
r118 | { | |||
return QStringLiteral("MainView"); | ||||
} | ||||
|
r175 | |||
|
r288 | void VisualizationWidget::attachVariableMenu( | ||
QMenu *menu, const QVector<std::shared_ptr<Variable> > &variables) noexcept | ||||
|
r175 | { | ||
|
r288 | // Menu is generated only if there is a single variable | ||
if (variables.size() == 1) { | ||||
if (auto variable = variables.first()) { | ||||
|
r736 | // Gets the containers of the variable | ||
FindVariableOperation findVariableOperation{variable}; | ||||
accept(&findVariableOperation); | ||||
auto variableContainers = findVariableOperation.result(); | ||||
|
r288 | // Generates the actions that make it possible to visualize the variable | ||
|
r736 | GenerateVariableMenuOperation generateVariableMenuOperation{ | ||
menu, variable, std::move(variableContainers)}; | ||||
|
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"); | ||||
} | ||||
|
r175 | } | ||
|
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); | ||||
} | ||||
|
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); | ||||
} | ||||