From 030ee50565dff08cb295c41cf2724960de25e312 2017-12-13 11:17:21 From: Thibaud Rabillard Date: 2017-12-13 11:17:21 Subject: [PATCH] Ensures graph and zone names are not duplicated in the visualization --- diff --git a/gui/src/Visualization/VisualizationTabWidget.cpp b/gui/src/Visualization/VisualizationTabWidget.cpp index 0ae5135..757fdef 100644 --- a/gui/src/Visualization/VisualizationTabWidget.cpp +++ b/gui/src/Visualization/VisualizationTabWidget.cpp @@ -18,20 +18,6 @@ Q_LOGGING_CATEGORY(LOG_VisualizationTabWidget, "VisualizationTabWidget") 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(layout.itemAt(i)->widget())) { - count++; - } - } - - return QObject::tr("Zone %1").arg(count + 1); -} - /** * Applies a function to all zones of the tab represented by its layout * @param layout the layout that contains zones @@ -43,13 +29,31 @@ 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(item->widget())) { + = qobject_cast(item->widget())) { fun(*visualizationZoneWidget); } } } } +/// Generates a default name for a new zone, according to the number of zones already displayed in +/// the tab +QString defaultZoneName(QLayout &layout) +{ + QSet existingNames; + processZones(layout, + [&existingNames](auto &zoneWidget) { existingNames.insert(zoneWidget.name()); }); + + int zoneNum = 1; + QString name; + do { + name = QObject::tr("Zone ").append(QString::number(zoneNum)); + ++zoneNum; + } while (existingNames.contains(name)); + + return name; +} + } // namespace struct VisualizationTabWidget::VisualizationTabWidgetPrivate { diff --git a/gui/src/Visualization/VisualizationZoneWidget.cpp b/gui/src/Visualization/VisualizationZoneWidget.cpp index 64f7413..6c10a82 100644 --- a/gui/src/Visualization/VisualizationZoneWidget.cpp +++ b/gui/src/Visualization/VisualizationZoneWidget.cpp @@ -27,21 +27,6 @@ Q_LOGGING_CATEGORY(LOG_VisualizationZoneWidget, "VisualizationZoneWidget") namespace { - -/// Generates a default name for a new graph, according to the number of graphs already displayed in -/// the zone -QString defaultGraphName(const QLayout &layout) -{ - auto count = 0; - for (auto i = 0; i < layout.count(); ++i) { - if (dynamic_cast(layout.itemAt(i)->widget())) { - count++; - } - } - - return QObject::tr("Graph %1").arg(count + 1); -} - /** * Applies a function to all graphs of the zone represented by its layout * @param layout the layout that contains graphs @@ -60,6 +45,24 @@ void processGraphs(QLayout &layout, Fun fun) } } +/// Generates a default name for a new graph, according to the number of graphs already displayed in +/// the zone +QString defaultGraphName(QLayout &layout) +{ + QSet existingNames; + processGraphs( + layout, [&existingNames](auto &graphWidget) { existingNames.insert(graphWidget.name()); }); + + int zoneNum = 1; + QString name; + do { + name = QObject::tr("Graph ").append(QString::number(zoneNum)); + ++zoneNum; + } while (existingNames.contains(name)); + + return name; +} + } // namespace struct VisualizationZoneWidget::VisualizationZoneWidgetPrivate {