Files
qelectrotech-source-mirror/sources/conductorautonumerotation.cpp
T
ispyisail 2e0fe44174 Cover auto-numbering counter changes with undo/redo
Placing an auto-numbered element or conductor advances a shared
NumerotationContext counter (QETProject::addConductorAutoNum/
addElementAutoNum) as a side effect that sat entirely outside the undo
stack. Undoing the placement removed the visible number but left the
counter advanced, so every undo of an auto-numbered placement silently
burned a number, with no way to get it back short of a manual reset.

Adds SetAutoNumContextCommand, a small QUndoCommand storing the old/new
NumerotationContext and calling the matching add*AutoNum() setter on
undo()/redo() -- the same shape QPropertyUndoCommand already uses next
to it in ConductorAutoNumerotation::applyText().

Wires it into the two conductor call sites (the static newProperties(),
and numerateNewConductor(), both in ConductorAutoNumerotation) and the
element call site (Element::setUpFormula(), called from
DiagramEventAddElement::addElement() when a new element is dropped onto
a diagram). setUpFormula() now takes an optional parent QUndoCommand;
addElement() calls it before pushing its own undo_object so the counter
change lands in the same undo macro as the element's placement -- one
Ctrl+Z reverts both together, instead of leaving the counter adrift.

The project-properties config dialog's own add*AutoNum() calls (editing
the numbering rule itself, not a side effect of placing something) are
deliberately left untouched, as are the load-time folio-sequential
bookkeeping calls in Diagram::loadElmtFolioSeq()/loadCndFolioSeq() and
the bulk folio-renumbering passes in QETProject -- none of those run as
part of an undoable user gesture.

Implements the scope proposed in discussion #608.
2026-08-03 13:19:51 +12:00

280 lines
7.6 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "conductorautonumerotation.h"
#include "qetproject.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
#include "autoNum/assignvariables.h"
#include "autoNum/numerotationcontextcommands.h"
#include "diagram.h"
#include "qet.h"
#include "qetdiagrameditor.h"
#include "ui/potentialselectordialog.h"
#include "undocommand/setautonumcontextcommand.h"
/**
@brief ConductorAutoNumerotation::ConductorAutoNumerotation
Constructor of autonum, after create a class,
call numerate to apply the autonum.
When autonum is applyed,
they do with an undo command added to the stack of diagram.
If you give a parent_undo at constructor,
the undo command create in this class have parent_undo for parent,
and wasn't added to the stack of diagram
(it's the responsabillty of the parent_undo)
@param conductor : the conductor to apply automatic numerotation
@param diagram : the diagram of conductor
@param parent_undo : parent undo command
*/
ConductorAutoNumerotation::ConductorAutoNumerotation(
Conductor *conductor,
Diagram *diagram,
QUndoCommand *parent_undo) :
m_diagram (diagram),
m_conductor (conductor),
conductor_list (conductor -> relatedPotentialConductors().values()),
m_parent_undo (parent_undo)
{}
/**
@brief ConductorAutoNumerotation::numerate
execute the automatic numerotation
*/
void ConductorAutoNumerotation::numerate()
{
if (!m_conductor) return;
if (conductor_list.size() >= 1 ) numeratePotential();
else if (m_conductor -> properties().type == ConductorProperties::Multi)
numerateNewConductor();
}
/**
@brief ConductorAutoNumerotation::applyText
apply the text t to conductor_
and all conductors at the same potential
@param t : Conductor text
*/
void ConductorAutoNumerotation::applyText(const QString& t)
{
if (!m_conductor) return;
QVariant old_value, new_value;
ConductorProperties cp = m_conductor -> properties();
old_value.setValue(cp);
cp.text = t;
new_value.setValue(cp);
QUndoCommand *undo = nullptr;
if (m_parent_undo)
{
new QPropertyUndoCommand(
m_conductor,
"properties",
old_value,
new_value,
m_parent_undo);
undo = m_parent_undo;
}
else
{
undo = new QUndoCommand();
new QPropertyUndoCommand(
m_conductor,
"properties",
old_value,
new_value,
undo);
undo->setText(
QObject::tr(
"Modifier les propriétés d'un conducteur",
"undo caption"));
}
if (!conductor_list.isEmpty())
{
if (!m_parent_undo)
undo->setText(
QObject::tr(
"Modifier les propriétés de plusieurs conducteurs",
"undo caption"));
foreach (Conductor *cond, conductor_list)
{
ConductorProperties cp2 = cond -> properties();
old_value.setValue(cp2);
cp2.text = t;
new_value.setValue(cp2);
new QPropertyUndoCommand(
cond,
"properties",
old_value,
new_value,
undo);
}
}
if (!m_parent_undo)
m_diagram->undoStack().push(undo);
}
/**
@brief ConductorAutoNumerotation::newProperties
Create a new properties according to the current autonum rule of diagram
@param diagram : Diagram class
@param cp : ConductorProperties
@param seq : sequentialNumbers
*/
void ConductorAutoNumerotation::newProperties(
Diagram *diagram,
ConductorProperties &cp,
autonum::sequentialNumbers &seq)
{
NumerotationContext context = diagram->project()->conductorAutoNum(
diagram->conductorsAutonumName());
if (context.isEmpty()) {
return;
}
QString autoNum_name = diagram->project()->conductorCurrentAutoNum();
QString formula = autonum::numerotationContextToFormula(context);
cp.m_formula = formula;
autonum::setSequential(formula, seq, context, diagram, autoNum_name);
NumerotationContextCommands ncc (context, diagram);
NumerotationContext new_context = ncc.next();
QETProject *project = diagram->project();
auto *undo = new SetAutoNumContextCommand(
[project](const QString &k, const NumerotationContext &c) {project->addConductorAutoNum(k, c);},
autoNum_name,
context,
new_context);
undo->setText(QObject::tr("Numéroter automatiquement un conducteur", "undo caption"));
diagram->undoStack().push(undo);
}
/**
@brief ConductorAutoNumerotation::numeratePotential
Numerate a conductor on an existing potential
*/
void ConductorAutoNumerotation::numeratePotential()
{
ConductorProperties cp = conductor_list.first()->properties();
bool properties_equal = true;
foreach (const Conductor *conductor, conductor_list)
{
if (conductor->properties() != cp)
properties_equal = false;
}
// Every properties of the potential is equal,
// so we apply it to m_conductor
if (properties_equal)
{
m_conductor->setProperties(cp);
m_conductor->rSequenceNum() = conductor_list.first()->sequenceNum();
return;
}
QStringList text_list;
QStringList formula_list;
foreach (const Conductor *cc, conductor_list)
{
ConductorProperties cp = cc->properties();
text_list << cp.text;
formula_list << cp.m_formula;
}
//the texts is identicals
if (QET::eachStrIsEqual(text_list) && QET::eachStrIsEqual(formula_list))
{
QList<ConductorProperties> cp_list;
foreach(Conductor *c, conductor_list)
cp_list<<c->properties();
ConductorProperties cp = m_conductor->properties();
cp.applyForEqualAttributes(cp_list);
m_conductor->rSequenceNum() = conductor_list.first()->sequenceNum();
m_conductor->setProperties(cp);
}
//the texts isn't identicals
else
{
PotentialSelectorDialog psd(
m_conductor,
m_parent_undo,
m_conductor->diagramEditor());
psd.exec();
}
}
/**
@brief ConductorAutoNumerotation::numerateNewConductor
create and apply a new numerotation to m_conductor
*/
void ConductorAutoNumerotation::numerateNewConductor()
{
if (!m_conductor) {
return;
}
if (!m_diagram->conductorsAutonumName().isEmpty())
{
NumerotationContext context = m_diagram->project()->conductorAutoNum(
m_diagram -> conductorsAutonumName());
if (context.isEmpty())
return;
QString autoNum_name = m_diagram->project()->conductorCurrentAutoNum();
QString formula = autonum::numerotationContextToFormula(context);
ConductorProperties cp = m_conductor -> properties();
cp.m_formula = formula;
m_conductor->setProperties(cp);
autonum::setSequential(formula,
m_conductor->rSequenceNum(),
context,
m_diagram,
autoNum_name);
NumerotationContextCommands ncc (context, m_diagram);
NumerotationContext new_context = ncc.next();
QETProject *project = m_diagram->project();
auto setter = [project](const QString &k, const NumerotationContext &c) {project->addConductorAutoNum(k, c);};
if (m_parent_undo)
{
new SetAutoNumContextCommand(setter, autoNum_name, context, new_context, m_parent_undo);
}
else
{
auto *undo = new SetAutoNumContextCommand(setter, autoNum_name, context, new_context);
undo->setText(QObject::tr("Numéroter automatiquement un conducteur", "undo caption"));
m_diagram->undoStack().push(undo);
}
}
applyText(autonum::AssignVariables::formulaToLabel(
m_conductor->properties().m_formula,
m_conductor->rSequenceNum(),
m_diagram));
}