Terminal strip: natural sort for automatic ordering and terminal lists

"Position automatique" sorted labels as plain strings, so XAUZ1:10
came before XAUZ1:2. Add QETUtils::naturalLessThan (digit runs compared
by value, case insensitive; QCollator numeric mode is ignored by some
Qt backends) and use it in SortTerminalStripCommand, FreeTerminalModel
and the terminal strip tree dock.
This commit is contained in:
Laurent Trinques
2026-10-08 16:19:44 +02:00
parent 7c6a0411d4
commit b719502060
5 changed files with 88 additions and 44 deletions
@@ -19,6 +19,7 @@
#include "../terminalstrip.h"
#include "../physicalterminal.h"
#include "../realterminal.h"
#include "../../utils/qetutils.h"
SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) :
QUndoCommand(parent),
@@ -46,46 +47,16 @@ void SortTerminalStripCommand::redo()
void SortTerminalStripCommand::sort()
{
std::sort(m_new_order.begin(), m_new_order.end(), [](QSharedPointer<PhysicalTerminal> arg1, QSharedPointer<PhysicalTerminal> arg2)
auto label_of = [](const QSharedPointer<PhysicalTerminal> &t) -> QString
{
const QRegularExpression rx(QStringLiteral("^\\d+"));
return t->realTerminalCount() ? t->realTerminals().constLast()->label()
: QString();
};
QString str1;
QString str2;
int int1 =-1;
int int2 =-1;
if (arg1->realTerminalCount())
{
str1 = arg1->realTerminals().constLast()->label();
auto match = rx.match(str1);
if (match.hasMatch()) {
int1 = match.captured(0).toInt();
}
}
if (arg2->realTerminalCount())
{
str2 = arg2->realTerminals().constLast()->label();
auto match = rx.match(str2);
if (match.hasMatch()) {
int2 = match.captured(0).toInt();
}
}
//Sort as numbers if both string
//start at least by a digit and
//the number of each string are different.
//Else sort as string
if (int1 >= 0 &&
int2 >= 0 &&
int1 != int2) {
return int1<int2;
}
else {
return str1<str2;
}
std::stable_sort(m_new_order.begin(), m_new_order.end(),
[&label_of](const QSharedPointer<PhysicalTerminal> &arg1,
const QSharedPointer<PhysicalTerminal> &arg2)
{
return QETUtils::naturalLessThan(label_of(arg1), label_of(arg2));
});
}