Extend terminal numbering dialog with letter numbering and strip selection

This commit is contained in:
Kellermorph
2026-08-13 13:51:13 +02:00
parent 10cc162c4e
commit 458c9921c2
4 changed files with 211 additions and 63 deletions
+81 -6
View File
@@ -5,18 +5,57 @@
#include "../qetgraphicsitem/element.h"
#include "../undocommand/changeelementinformationcommand.h"
#include <QUndoCommand>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QSet>
#include <algorithm>
/**
* @brief TerminalNumberingDialog::TerminalNumberingDialog
* Constructor
* @param parent
* @param project Pointer to the current project (used to populate terminal strips)
*/
TerminalNumberingDialog::TerminalNumberingDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TerminalNumberingDialog)
TerminalNumberingDialog::TerminalNumberingDialog(QWidget *parent, QETProject *project) :
QDialog(parent),
ui(new Ui::TerminalNumberingDialog),
m_project(project)
{
ui->setupUi(this);
// Connect radio button to enable/disable the "also number letters" checkbox
connect(ui->rb_type_alpha, &QRadioButton::toggled, ui->cb_also_alpha, &QCheckBox::setEnabled);
// Collect all unique terminal strip prefixes from the project
if (m_project) {
QSet<QString> prefixes;
foreach (Diagram *diagram, m_project->diagrams()) {
foreach (QGraphicsItem *qgi, diagram->items()) {
if (Element *elmt = qgraphicsitem_cast<Element *>(qgi)) {
if (elmt->elementData().m_type == ElementData::Terminal) {
QString label = elmt->actualLabel();
if (label.isEmpty()) continue;
int colonIndex = label.lastIndexOf(':');
if (colonIndex != -1) {
QString prefix = label.left(colonIndex);
prefixes.insert(prefix);
}
}
}
}
}
// Sort prefixes alphabetically and create checkboxes
QStringList sortedPrefixes = prefixes.values();
sortedPrefixes.sort(Qt::CaseInsensitive);
foreach (const QString &prefix, sortedPrefixes) {
QCheckBox *cb = new QCheckBox(prefix);
cb->setChecked(true);
ui->verticalLayout_strips_content->addWidget(cb);
m_stripCheckboxes.insert(prefix, cb);
}
}
}
/**
@@ -46,11 +85,37 @@ bool TerminalNumberingDialog::isAlphanumeric() const
return ui->rb_type_alpha->isChecked();
}
/**
* @brief TerminalNumberingDialog::alsoNumberLetters
* @return true if the "also number letters" checkbox is checked
*/
bool TerminalNumberingDialog::alsoNumberLetters() const
{
return ui->cb_also_alpha->isChecked();
}
/**
* @brief TerminalNumberingDialog::excludedStrips
* @return List of terminal strip prefixes that should be excluded from numbering
*/
QStringList TerminalNumberingDialog::excludedStrips() const
{
QStringList excluded;
QMapIterator<QString, QCheckBox*> it(m_stripCheckboxes);
while (it.hasNext()) {
it.next();
if (!it.value()->isChecked()) {
excluded.append(it.key());
}
}
return excluded;
}
/**
* @brief TerminalNumberingDialog::getUndoCommand
* Scans the given project for terminals, sorts them according to user preferences
* (X/Y axis, alphanumeric rules), and generates an undo command containing all label changes.
* * @param project Pointer to the current QETProject
* @param project Pointer to the current QETProject
* @return QUndoCommand* containing the modifications, or nullptr if no changes are needed.
*/
QUndoCommand* TerminalNumberingDialog::getUndoCommand(QETProject *project) const {
@@ -58,6 +123,8 @@ QUndoCommand* TerminalNumberingDialog::getUndoCommand(QETProject *project) const
bool axisX = isXAxisPriority();
bool alpha = isAlphanumeric();
bool alsoAlpha = alsoNumberLetters();
QStringList excluded = excludedStrips();
// 1. Helper structure to store and sort terminal data
struct TermInfo {
@@ -97,6 +164,9 @@ QUndoCommand* TerminalNumberingDialog::getUndoCommand(QETProject *project) const
suffix = label.mid(colonIndex + 1);
}
// Skip excluded terminal strips
if (excluded.contains(prefix)) continue;
// If user chose purely numeric, skip terminals with alphabetical suffixes
if (!alpha && !suffix.isEmpty()) {
bool isNum;
@@ -154,8 +224,13 @@ QUndoCommand* TerminalNumberingDialog::getUndoCommand(QETProject *project) const
// If it was a number (e.g., "1") or empty, update it with the new counter
newLabel = ti.prefix + ":" + QString::number(newNum);
} else {
// If it was alphabetical (e.g., "N", "PE"), keep the original text but consume the count!
newLabel = ti.prefix + ":" + ti.suffix;
// If it was alphabetical (e.g., "N", "PE"), keep the original text
// If "also number letters" is enabled, append the counter
if (alsoAlpha) {
newLabel = ti.prefix + ":" + ti.suffix + QString::number(newNum);
} else {
newLabel = ti.prefix + ":" + ti.suffix;
}
}
DiagramContext oldInfo = ti.elmt->elementInformations();