mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-08-05 20:54:13 +02:00
Add alphabetical auto-numbering (a, b, ... z, aa, ab, ...) (#579)
Adds a real base-26 incrementing part type to the autonumbering engine, alongside the 14 existing NumStrategy leaves. Unlike StringNum (a fixed, non-incrementing text segment), AlphaNum::next()/previous() carry/borrow entirely within the part's own value -- the composition loop in NumerotationContextCommands doesn't need to change, since (unlike #578's wrap-and-carry) nothing here needs to signal an adjacent part. - incrementAlpha()/decrementAlpha() implement the spreadsheet-column-name algorithm: increment carries right-to-left on 'z'/'Z' overflow, prepending a new leading letter if the whole value overflows (z -> aa, az -> ba). decrement is the exact inverse, including the symmetric shrink case (aa -> z) once every position has borrowed. A single letter already at "a"/"A" has no representable predecessor and is clamped rather than turned into "z" -- caught via manual testing, since the initial implementation mutated the string in the borrow loop before checking whether to clamp, silently discarding the original value. - Registered in NumerotationContext::validRegExpNum() but deliberately not in validRegExpNumber(), so addValue() doesn't force alphabetic values through int conversion. - New "Cyclique"-adjacent "Alphabétique" entry in numparteditorw's type dropdown, with its own letters-only QRegularExpressionValidator; the increase spinbox is disabled since the step is always exactly one letter, not a configurable amount. Also wires the new part type through to actual element/conductor labels, which turned out to be required for the feature to do anything visible beyond folio numbering (which applies a NumerotationContext's represented string directly). Element and conductor numbering instead go through a separate formula-substitution layer (autonum::sequentialNumbers + %sequ_/%seqt_/%seqh_-style placeholders in AssignVariables::assignSequence()) that numerotationContextToFormula() auto-populates. Without a matching placeholder, an "alpha" part would silently vanish from the generated formula and never reach the label, even though the underlying counter was advancing correctly: - sequentialNumbers gained an `alpha` QStringList member (copy ctor, operator=, operator==, toXml/fromXml, clear()). - numerotationContextToFormula() emits a new %seqa_N placeholder for alpha parts, the same way %sequ_N is emitted for unit parts. - setSequential()/setSequentialToList() populate seqStruct.alpha, passing the raw string through as-is rather than the .toInt()-based formatting used for the numeric part types. - AssignVariables::assignSequence() substitutes %seqa_N from seqStruct.alpha, mirroring the existing %sequ_N/%seqt_N/%seqh_N substitutions. No "alphafolio" variant was added, matching the discussion's scope (only unit/ten/hundred have folio-anchored variants). Verified against production code via the numbering config dialog's own Suivant/Précédent buttons: from "a", 25 clicks reached "z"; one more produced "aa"; 25 more reached "az"; one more produced "ba" (carry). Reversed: "ba"->"az"->(25 clicks)->"aa"->"z" (shrink)->(25 clicks)->"a". One more "previous" at "a" correctly stayed at "a" after the clamp fix. Also confirmed the Formule field auto-updates to "%seqa_1" the instant the type is switched to "Alphabétique", confirming the formula-generation wiring works live in the UI, not just at the engine level.
This commit is contained in:
@@ -204,6 +204,10 @@ void NumerotationContextCommands::setNumStrategy(const QString &str) {
|
||||
strategy_ = new StringNum (diagram_);
|
||||
return;
|
||||
}
|
||||
else if (str == "alpha") {
|
||||
strategy_ = new AlphaNum (diagram_);
|
||||
return;
|
||||
}
|
||||
else if (str == "idfolio") {
|
||||
strategy_ = new IdFolioNum (diagram_);
|
||||
return;
|
||||
@@ -604,6 +608,120 @@ NumerotationContext StringNum::previous(const NumerotationContext &nc, const int
|
||||
return (nextString(nc, i));
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
/**
|
||||
* @brief incrementAlpha
|
||||
* Base-26 letter increment (a, b, ... z, aa, ab, ... az, ba, ...),
|
||||
* the same algorithm as incrementing a spreadsheet column name.
|
||||
* Carries right-to-left on 'z'/'Z' overflow; if the whole string
|
||||
* overflows, a new leading letter is prepended (lowercase 'a').
|
||||
* @param value : current alphabetic value; treated as "a" if empty.
|
||||
* @return the next value.
|
||||
*/
|
||||
QString incrementAlpha(QString value)
|
||||
{
|
||||
if (value.isEmpty()) {
|
||||
return QStringLiteral("a");
|
||||
}
|
||||
|
||||
int i = value.length() - 1;
|
||||
while (i >= 0 && (value.at(i) == QLatin1Char('z') || value.at(i) == QLatin1Char('Z'))) {
|
||||
value[i] = value.at(i).isUpper() ? QLatin1Char('A') : QLatin1Char('a');
|
||||
--i;
|
||||
}
|
||||
if (i < 0) {
|
||||
value.prepend(QLatin1Char('a'));
|
||||
} else {
|
||||
value[i] = QChar(value.at(i).unicode() + 1);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief decrementAlpha
|
||||
* Inverse of incrementAlpha(): borrows right-to-left on 'a'/'A'
|
||||
* underflow. Symmetric shrink case (e.g. "aa" -> "z"): once every
|
||||
* position has borrowed, the leading letter is dropped rather than
|
||||
* left as an extra 'z'. A single-letter value already at "a"/"A" has
|
||||
* no representable predecessor and is left unchanged, the same way
|
||||
* the numeric parts don't clamp but a blank label would be worse
|
||||
* here than a value that stops decreasing.
|
||||
* @param value : current alphabetic value; treated as "a" if empty.
|
||||
* @return the previous value.
|
||||
*/
|
||||
QString decrementAlpha(QString value)
|
||||
{
|
||||
if (value.isEmpty()) {
|
||||
return QStringLiteral("a");
|
||||
}
|
||||
if (value.length() == 1) {
|
||||
//A single letter has no representable predecessor once it
|
||||
//reaches "a"/"A" -- clamp rather than mutate, since the loop
|
||||
//below would otherwise turn it into "z"/"Z" (borrowing past
|
||||
//the only position there is).
|
||||
if (value.at(0) == QLatin1Char('a') || value.at(0) == QLatin1Char('A')) {
|
||||
return value;
|
||||
}
|
||||
return QChar(value.at(0).unicode() - 1);
|
||||
}
|
||||
|
||||
int i = value.length() - 1;
|
||||
while (i >= 0 && (value.at(i) == QLatin1Char('a') || value.at(i) == QLatin1Char('A'))) {
|
||||
value[i] = value.at(i).isUpper() ? QLatin1Char('Z') : QLatin1Char('z');
|
||||
--i;
|
||||
}
|
||||
if (i < 0) {
|
||||
//Every position borrowed: the whole value was "a...a", whose
|
||||
//predecessor is one fewer "z" (e.g. "aa" -> "z").
|
||||
value.remove(0, 1);
|
||||
} else {
|
||||
value[i] = QChar(value.at(i).unicode() - 1);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Constructor
|
||||
*/
|
||||
AlphaNum::AlphaNum (Diagram *d):
|
||||
NumStrategy (d)
|
||||
{}
|
||||
|
||||
/**
|
||||
@brief AlphaNum::toRepresentedString
|
||||
@return the represented string of str
|
||||
*/
|
||||
QString AlphaNum::toRepresentedString(const QString str) const
|
||||
{
|
||||
return (str);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief AlphaNum::next
|
||||
@return the next NumerotationContext nc at position i
|
||||
*/
|
||||
NumerotationContext AlphaNum::next (const NumerotationContext &nc, const int i) const
|
||||
{
|
||||
QStringList strl = nc.itemAt(i);
|
||||
NumerotationContext newnc;
|
||||
newnc.addValue(strl.at(0), incrementAlpha(strl.at(1)), strl.at(2).toInt());
|
||||
return (newnc);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief AlphaNum::previous
|
||||
@return the previous NumerotationContext nc at posiiton i
|
||||
*/
|
||||
NumerotationContext AlphaNum::previous(const NumerotationContext &nc, const int i) const
|
||||
{
|
||||
QStringList strl = nc.itemAt(i);
|
||||
NumerotationContext newnc;
|
||||
newnc.addValue(strl.at(0), decrementAlpha(strl.at(1)), strl.at(2).toInt());
|
||||
return (newnc);
|
||||
}
|
||||
|
||||
/**
|
||||
Constructor
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user