VisualizationTabWidget.cpp
129 lines
| 3.4 KiB
| text/x-c
|
CppLexer
r95 | #include "Visualization/VisualizationTabWidget.h" | |||
Alexandre Leroux
|
r207 | #include "Visualization/IVisualizationWidgetVisitor.h" | ||
r58 | #include "ui_VisualizationTabWidget.h" | |||
r118 | #include "Visualization/VisualizationZoneWidget.h" | |||
Alexandre Leroux
|
r219 | Q_LOGGING_CATEGORY(LOG_VisualizationTabWidget, "VisualizationTabWidget") | ||
Alexandre Leroux
|
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); | ||||
} | ||||
Alexandre Leroux
|
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); | ||||
} | ||||
} | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r201 | } // namespace | ||
Alexandre Leroux
|
r198 | struct VisualizationTabWidget::VisualizationTabWidgetPrivate { | ||
explicit VisualizationTabWidgetPrivate(const QString &name) : m_Name{name} {} | ||||
r118 | ||||
Alexandre Leroux
|
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); | ||||
Alexandre Leroux
|
r267 | |||
// Widget is deleted when closed | ||||
setAttribute(Qt::WA_DeleteOnClose); | ||||
r58 | } | |||
VisualizationTabWidget::~VisualizationTabWidget() | ||||
{ | ||||
delete ui; | ||||
} | ||||
r118 | ||||
void VisualizationTabWidget::addZone(VisualizationZoneWidget *zoneWidget) | ||||
{ | ||||
Alexandre Leroux
|
r307 | tabLayout().addWidget(zoneWidget); | ||
r118 | } | |||
Alexandre Leroux
|
r201 | VisualizationZoneWidget *VisualizationTabWidget::createZone(std::shared_ptr<Variable> variable) | ||
r118 | { | |||
Alexandre Leroux
|
r307 | auto zoneWidget = new VisualizationZoneWidget{defaultZoneName(tabLayout()), this}; | ||
r118 | this->addZone(zoneWidget); | |||
Alexandre Leroux
|
r201 | // Creates a new graph into the zone | ||
zoneWidget->createGraph(variable); | ||||
r118 | return zoneWidget; | |||
} | ||||
Alexandre Leroux
|
r207 | void VisualizationTabWidget::accept(IVisualizationWidgetVisitor *visitor) | ||
r118 | { | |||
Alexandre Leroux
|
r208 | if (visitor) { | ||
visitor->visitEnter(this); | ||||
Alexandre Leroux
|
r738 | // Apply visitor to zone children: widgets different from zones are not visited (no action) | ||
processZones(tabLayout(), [visitor](VisualizationZoneWidget &zoneWidget) { | ||||
zoneWidget.accept(visitor); | ||||
}); | ||||
Alexandre Leroux
|
r208 | |||
visitor->visitLeave(this); | ||||
} | ||||
Alexandre Leroux
|
r219 | else { | ||
qCCritical(LOG_VisualizationTabWidget()) << tr("Can't visit widget : the visitor is null"); | ||||
} | ||||
r118 | } | |||
Alexandre Leroux
|
r209 | bool VisualizationTabWidget::canDrop(const Variable &variable) const | ||
{ | ||||
// A tab can always accomodate a variable | ||||
Q_UNUSED(variable); | ||||
return true; | ||||
} | ||||
Alexandre Leroux
|
r327 | bool VisualizationTabWidget::contains(const Variable &variable) const | ||
{ | ||||
Q_UNUSED(variable); | ||||
return false; | ||||
} | ||||
r119 | QString VisualizationTabWidget::name() const | |||
r118 | { | |||
Alexandre Leroux
|
r198 | return impl->m_Name; | ||
r118 | } | |||
Alexandre Leroux
|
r307 | |||
Alexandre Leroux
|
r738 | void VisualizationTabWidget::closeEvent(QCloseEvent *event) | ||
{ | ||||
// Closes zones in the tab | ||||
processZones(tabLayout(), [](VisualizationZoneWidget &zoneWidget) { zoneWidget.close(); }); | ||||
QWidget::closeEvent(event); | ||||
} | ||||
Alexandre Leroux
|
r307 | QLayout &VisualizationTabWidget::tabLayout() const noexcept | ||
{ | ||||
return *ui->scrollAreaWidgetContents->layout(); | ||||
} | ||||