VisualizationTabWidget.cpp
220 lines
| 6.6 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationTabWidget.h" | |||
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r58 | #include "ui_VisualizationTabWidget.h" | |||
r842 | #include "Visualization/VisualizationGraphWidget.h" | |||
r847 | #include "Visualization/VisualizationZoneWidget.h" | |||
r842 | ||||
r844 | #include "Variable/VariableController.h" | |||
r850 | #include "Common/MimeTypesDef.h" | |||
r842 | #include "DragDropHelper.h" | |||
r847 | #include "SqpApplication.h" | |||
r118 | ||||
|
r219 | Q_LOGGING_CATEGORY(LOG_VisualizationTabWidget, "VisualizationTabWidget") | ||
|
r201 | namespace { | ||
/// Generates a default name for a new zone, according to the number of zones already displayed in | ||||
/// the tab | ||||
QString defaultZoneName(const QLayout &layout) | ||||
{ | ||||
auto count = 0; | ||||
for (auto i = 0; i < layout.count(); ++i) { | ||||
if (dynamic_cast<VisualizationZoneWidget *>(layout.itemAt(i)->widget())) { | ||||
count++; | ||||
} | ||||
} | ||||
return QObject::tr("Zone %1").arg(count + 1); | ||||
} | ||||
|
r738 | /** | ||
* Applies a function to all zones of the tab represented by its layout | ||||
* @param layout the layout that contains zones | ||||
* @param fun the function to apply to each zone | ||||
*/ | ||||
template <typename Fun> | ||||
void processZones(QLayout &layout, Fun fun) | ||||
{ | ||||
for (auto i = 0; i < layout.count(); ++i) { | ||||
if (auto item = layout.itemAt(i)) { | ||||
if (auto visualizationZoneWidget | ||||
= dynamic_cast<VisualizationZoneWidget *>(item->widget())) { | ||||
fun(*visualizationZoneWidget); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
|
r201 | } // namespace | ||
|
r198 | struct VisualizationTabWidget::VisualizationTabWidgetPrivate { | ||
explicit VisualizationTabWidgetPrivate(const QString &name) : m_Name{name} {} | ||||
r118 | ||||
|
r198 | QString m_Name; | ||
}; | ||||
VisualizationTabWidget::VisualizationTabWidget(const QString &name, QWidget *parent) | ||||
: QWidget{parent}, | ||||
ui{new Ui::VisualizationTabWidget}, | ||||
impl{spimpl::make_unique_impl<VisualizationTabWidgetPrivate>(name)} | ||||
r58 | { | |||
ui->setupUi(this); | ||||
|
r267 | |||
r850 | ui->dragDropContainer->setAcceptedMimeTypes({MIME_TYPE_GRAPH, MIME_TYPE_ZONE}); | |||
r847 | connect(ui->dragDropContainer, &VisualizationDragDropContainer::dropOccured, this, | |||
&VisualizationTabWidget::dropMimeData); | ||||
r843 | sqpApp->dragDropHelper().addDragDropScrollArea(ui->scrollArea); | |||
r842 | ||||
|
r267 | // Widget is deleted when closed | ||
setAttribute(Qt::WA_DeleteOnClose); | ||||
r58 | } | |||
VisualizationTabWidget::~VisualizationTabWidget() | ||||
{ | ||||
r843 | sqpApp->dragDropHelper().removeDragDropScrollArea(ui->scrollArea); | |||
r58 | delete ui; | |||
} | ||||
r118 | ||||
void VisualizationTabWidget::addZone(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
r842 | ui->dragDropContainer->addDragWidget(zoneWidget); | |||
} | ||||
void VisualizationTabWidget::insertZone(int index, VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
r847 | ui->dragDropContainer->insertDragWidget(index, zoneWidget); | |||
r118 | } | |||
|
r201 | VisualizationZoneWidget *VisualizationTabWidget::createZone(std::shared_ptr<Variable> variable) | ||
r118 | { | |||
r842 | return createZone({variable}, -1); | |||
} | ||||
r847 | VisualizationZoneWidget * | |||
VisualizationTabWidget::createZone(const QList<std::shared_ptr<Variable> > &variables, int index) | ||||
r842 | { | |||
r844 | auto zoneWidget = createEmptyZone(index); | |||
r118 | ||||
|
r201 | // Creates a new graph into the zone | ||
r842 | zoneWidget->createGraph(variables, index); | |||
|
r201 | |||
r118 | return zoneWidget; | |||
} | ||||
r844 | VisualizationZoneWidget *VisualizationTabWidget::createEmptyZone(int index) | |||
{ | ||||
r847 | auto zoneWidget | |||
= new VisualizationZoneWidget{defaultZoneName(*ui->dragDropContainer->layout()), this}; | ||||
r844 | this->insertZone(index, zoneWidget); | |||
return zoneWidget; | ||||
} | ||||
|
r207 | void VisualizationTabWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r118 | { | |||
|
r208 | if (visitor) { | ||
visitor->visitEnter(this); | ||||
|
r738 | // Apply visitor to zone children: widgets different from zones are not visited (no action) | ||
processZones(tabLayout(), [visitor](VisualizationZoneWidget &zoneWidget) { | ||||
zoneWidget.accept(visitor); | ||||
}); | ||||
|
r208 | |||
visitor->visitLeave(this); | ||||
} | ||||
|
r219 | else { | ||
qCCritical(LOG_VisualizationTabWidget()) << tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r118 | } | |||
|
r209 | bool VisualizationTabWidget::canDrop(const Variable &variable) const | ||
{ | ||||
// A tab can always accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return true; | ||||
} | ||||
|
r327 | bool VisualizationTabWidget::contains(const Variable &variable) const | ||
{ | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
r119 | QString VisualizationTabWidget::name() const | |||
r118 | { | |||
|
r198 | return impl->m_Name; | ||
r118 | } | |||
|
r307 | |||
|
r738 | void VisualizationTabWidget::closeEvent(QCloseEvent *event) | ||
{ | ||||
// Closes zones in the tab | ||||
processZones(tabLayout(), [](VisualizationZoneWidget &zoneWidget) { zoneWidget.close(); }); | ||||
QWidget::closeEvent(event); | ||||
} | ||||
|
r307 | QLayout &VisualizationTabWidget::tabLayout() const noexcept | ||
{ | ||||
r842 | return *ui->dragDropContainer->layout(); | |||
} | ||||
void VisualizationTabWidget::dropMimeData(int index, const QMimeData *mimeData) | ||||
{ | ||||
r847 | auto &helper = sqpApp->dragDropHelper(); | |||
r850 | if (mimeData->hasFormat(MIME_TYPE_GRAPH)) { | |||
r847 | auto graphWidget = static_cast<VisualizationGraphWidget *>(helper.getCurrentDragWidget()); | |||
auto parentDragDropContainer | ||||
= qobject_cast<VisualizationDragDropContainer *>(graphWidget->parentWidget()); | ||||
r842 | Q_ASSERT(parentDragDropContainer); | |||
auto nbGraph = parentDragDropContainer->countDragWidget(); | ||||
r844 | ||||
r847 | const auto &variables = graphWidget->variables(); | |||
r844 | ||||
r847 | if (!variables.isEmpty()) { | |||
// Abort the requests for the variables (if any) | ||||
// Commented, because it's not sure if it's needed or not | ||||
// for (const auto& var : variables) | ||||
r845 | //{ | |||
// sqpApp->variableController().onAbortProgressRequested(var); | ||||
//} | ||||
r847 | if (nbGraph == 1) { | |||
// This is the only graph in the previous zone, close the zone | ||||
r844 | graphWidget->parentZoneWidget()->close(); | |||
} | ||||
r847 | else { | |||
// Close the graph | ||||
r844 | graphWidget->close(); | |||
} | ||||
createZone(variables, index); | ||||
r842 | } | |||
r847 | else { | |||
// The graph is empty, create an empty zone and move the graph inside | ||||
r842 | ||||
r844 | auto parentZoneWidget = graphWidget->parentZoneWidget(); | |||
parentDragDropContainer->layout()->removeWidget(graphWidget); | ||||
auto zoneWidget = createEmptyZone(index); | ||||
zoneWidget->addGraph(graphWidget); | ||||
r847 | // Close the old zone if it was the only graph inside | |||
if (nbGraph == 1) { | ||||
r844 | parentZoneWidget->close(); | |||
r847 | } | |||
r844 | } | |||
r842 | } | |||
r850 | else if (mimeData->hasFormat(MIME_TYPE_ZONE)) { | |||
r847 | // Simple move of the zone, no variable operation associated | |||
auto zoneWidget = static_cast<VisualizationZoneWidget *>(helper.getCurrentDragWidget()); | ||||
r842 | auto parentDragDropContainer = zoneWidget->parentWidget(); | |||
parentDragDropContainer->layout()->removeWidget(zoneWidget); | ||||
ui->dragDropContainer->insertDragWidget(index, zoneWidget); | ||||
} | ||||
|
r307 | } | ||