mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-08-05 20:54:13 +02:00
Add wrap-and-carry (cyclic/modulo) auto-numbering (#578)
Adds a configurable wrap-at-N counter type to the autonumbering engine (NumerotationContext + NumerotationContextCommands), covering PLC/rack-style addressing conventions like "e0.0...e0.7, e1.0...e1.7" (8 channels per card) generally, rather than hardcoding octal specifically. - New "wrap" part type (WrapNum, alongside the existing UnitNum/TenNum/ HundredNum strategies) stores a modulus in addition to the existing value/increase/initialvalue fields. Its own next()/previous() only wraps its own value modulo the configured modulus -- carrying into (or borrowing from) the adjacent part requires visibility across parts, which only the composition loop has. - NumerotationContextCommands::next()/previous() gained carry()/borrow() helpers: when a wrap part's own next() would reach/exceed its modulus (or go below 0 on previous()), the nearest preceding numeric part is bumped by exactly one unit, skipping non-numeric parts (e.g. a "." string separator). Wrap parts chain correctly if adjacent (e.g. seconds wrapping into minutes wrapping into hours). - For the leading part of a wrap-and-carry pair to stay fixed except when carried into (i.e. actually produce "e0.0...e0.7, e1.0..." rather than advancing on every step under its own strategy), its own increase must be 0. The increase spinbox's minimum was 1, which made this configuration impossible through the UI -- lowered to 0 and documented with a tooltip, since this wasn't obvious from the UI alone. - NumerotationContext gained a 5th pipe-separated field (modulus) in its serialized string form, defaulting to 0 (non-wrapping) for every existing part type; toXml()/fromXml() persist it as a "modulus" XML attribute the same way "initialvalue" is already persisted for unitfolio/tenfolio/hundredfolio. - New "Cyclique (modulo)" entry in the part-type dropdown (numparteditorw), available for element, conductor, and folio autonumbering alike, since all three already go through NumerotationContextCommands. Verified in the running app via the numbering config dialog's own Suivant/Précédent buttons (which call the production NumerotationContextCommands::next()/previous() directly): a two-part context (unit, increase=0 + wrap mod 8) produced exactly e0.0→...→e0.7→e1.0→...→e1.7 on repeated "next", and the exact reverse (with correct borrowing) on repeated "previous".
This commit is contained in:
@@ -48,6 +48,15 @@ NumerotationContext NumerotationContextCommands::next()
|
||||
QStringList str = context_.itemAt(i);
|
||||
setNumStrategy(str.at(0));
|
||||
contextnum << strategy_ -> next(context_, i);
|
||||
|
||||
//Wrap-and-carry: str still holds the pre-increment value, so
|
||||
//this checks the same condition WrapNum::next() used to decide
|
||||
//whether to wrap its own value back to 0.
|
||||
if (str.at(0) == "wrap" && str.size() > 4) {
|
||||
int modulus = str.at(4).toInt();
|
||||
if (modulus > 0 && (str.at(1).toInt() + str.at(2).toInt()) >= modulus)
|
||||
carry(contextnum, i - 1);
|
||||
}
|
||||
}
|
||||
return contextnum;
|
||||
}
|
||||
@@ -64,10 +73,80 @@ NumerotationContext NumerotationContextCommands::previous()
|
||||
QStringList str = context_.itemAt(i);
|
||||
setNumStrategy(str.at(0));
|
||||
contextnum << strategy_ -> previous(context_, i);
|
||||
|
||||
if (str.at(0) == "wrap" && str.size() > 4) {
|
||||
int modulus = str.at(4).toInt();
|
||||
if (modulus > 0 && (str.at(1).toInt() - str.at(2).toInt()) < 0)
|
||||
borrow(contextnum, i - 1);
|
||||
}
|
||||
}
|
||||
return contextnum;
|
||||
}
|
||||
|
||||
/**
|
||||
@brief NumerotationContextCommands::carry
|
||||
Add one unit to the nearest numeric part at or before from_index in
|
||||
contextnum, skipping non-numeric parts (e.g. a "." string separator)
|
||||
along the way. If that part is itself a wrap part and this pushes it
|
||||
to (or past) its own modulus, it wraps back to 0 and the carry
|
||||
cascades further back -- so wrap parts can be chained (e.g. seconds
|
||||
wrapping into minutes wrapping into hours).
|
||||
@param contextnum the context being built by next(); already contains
|
||||
entries for every index <= from_index
|
||||
@param from_index index to start looking from, going backwards
|
||||
*/
|
||||
void NumerotationContextCommands::carry(NumerotationContext &contextnum, int from_index)
|
||||
{
|
||||
for (int j = from_index; j >= 0; --j) {
|
||||
QStringList strl = contextnum.itemAt(j);
|
||||
if (!contextnum.keyIsNumber(strl.at(0)))
|
||||
continue;
|
||||
|
||||
int value = strl.at(1).toInt() + 1;
|
||||
if (strl.at(0) == "wrap" && strl.size() > 4) {
|
||||
int modulus = strl.at(4).toInt();
|
||||
if (modulus > 0 && value >= modulus) {
|
||||
contextnum.replaceValue(j, QString::number(value - modulus));
|
||||
carry(contextnum, j - 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
contextnum.replaceValue(j, QString::number(value));
|
||||
return;
|
||||
}
|
||||
//No preceding numeric part: the carry has nowhere to go and is dropped,
|
||||
//same as any other counter in this engine has no overflow tracking
|
||||
//beyond the parts the user actually configured.
|
||||
}
|
||||
|
||||
/**
|
||||
@brief NumerotationContextCommands::borrow
|
||||
Inverse of carry(): subtract one unit from the nearest numeric part at
|
||||
or before from_index. If that part is itself a wrap part and this
|
||||
takes it below 0, it wraps to (modulus - 1) and the borrow cascades
|
||||
further back.
|
||||
*/
|
||||
void NumerotationContextCommands::borrow(NumerotationContext &contextnum, int from_index)
|
||||
{
|
||||
for (int j = from_index; j >= 0; --j) {
|
||||
QStringList strl = contextnum.itemAt(j);
|
||||
if (!contextnum.keyIsNumber(strl.at(0)))
|
||||
continue;
|
||||
|
||||
int value = strl.at(1).toInt() - 1;
|
||||
if (strl.at(0) == "wrap" && strl.size() > 4) {
|
||||
int modulus = strl.at(4).toInt();
|
||||
if (modulus > 0 && value < 0) {
|
||||
contextnum.replaceValue(j, QString::number(value + modulus));
|
||||
borrow(contextnum, j - 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
contextnum.replaceValue(j, QString::number(value));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@brief NumerotationContextCommands::toFinalString
|
||||
@return the string represented by the numerotation context
|
||||
@@ -117,6 +196,10 @@ void NumerotationContextCommands::setNumStrategy(const QString &str) {
|
||||
strategy_ = new HundredFNum (diagram_);
|
||||
return;
|
||||
}
|
||||
else if (str == "wrap") {
|
||||
strategy_ = new WrapNum (diagram_);
|
||||
return;
|
||||
}
|
||||
else if (str == "string") {
|
||||
strategy_ = new StringNum (diagram_);
|
||||
return;
|
||||
@@ -431,6 +514,62 @@ NumerotationContext HundredFNum::previous(const NumerotationContext &nc, const i
|
||||
return (previousNumber(nc, i));
|
||||
}
|
||||
|
||||
/**
|
||||
Constructor
|
||||
*/
|
||||
WrapNum::WrapNum (Diagram *d):
|
||||
NumStrategy (d)
|
||||
{}
|
||||
|
||||
/**
|
||||
@brief WrapNum::toRepresentedString
|
||||
@return the represented string of num
|
||||
*/
|
||||
QString WrapNum::toRepresentedString(const QString num) const
|
||||
{
|
||||
return (num);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief WrapNum::next
|
||||
Wraps this part's own value back to 0 every `modulus` values (carrying
|
||||
into the adjacent part is handled by NumerotationContextCommands::next(),
|
||||
which has visibility into the other parts).
|
||||
@return the next NumerotationContext nc at position i
|
||||
*/
|
||||
NumerotationContext WrapNum::next (const NumerotationContext &nc, const int i) const
|
||||
{
|
||||
QStringList strl = nc.itemAt(i);
|
||||
NumerotationContext newnc;
|
||||
int increase = strl.at(2).toInt();
|
||||
int modulus = strl.size() > 4 ? strl.at(4).toInt() : 0;
|
||||
int new_value = strl.at(1).toInt() + increase;
|
||||
if (modulus > 0)
|
||||
new_value %= modulus;
|
||||
newnc.addValue(strl.at(0), QString::number(new_value), increase, strl.at(3).toInt(), modulus);
|
||||
return (newnc);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief WrapNum::previous
|
||||
@return the previous NumerotationContext nc at posiiton i
|
||||
*/
|
||||
NumerotationContext WrapNum::previous(const NumerotationContext &nc, const int i) const
|
||||
{
|
||||
QStringList strl = nc.itemAt(i);
|
||||
NumerotationContext newnc;
|
||||
int increase = strl.at(2).toInt();
|
||||
int modulus = strl.size() > 4 ? strl.at(4).toInt() : 0;
|
||||
int new_value = strl.at(1).toInt() - increase;
|
||||
if (modulus > 0) {
|
||||
new_value %= modulus;
|
||||
if (new_value < 0)
|
||||
new_value += modulus;
|
||||
}
|
||||
newnc.addValue(strl.at(0), QString::number(new_value), increase, strl.at(3).toInt(), modulus);
|
||||
return (newnc);
|
||||
}
|
||||
|
||||
/**
|
||||
Constructor
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user