2021-09-22 23:02:33 +02:00
|
|
|
/*
|
2025-01-04 13:37:40 +01:00
|
|
|
Copyright 2006-2025 The QElectroTech Team
|
2024-05-02 08:39:27 +02:00
|
|
|
This file is part of QElectroTech.
|
2021-09-22 23:02:33 +02:00
|
|
|
|
2024-05-02 08:39:27 +02:00
|
|
|
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.
|
2021-09-22 23:02:33 +02:00
|
|
|
|
2024-05-02 08:39:27 +02:00
|
|
|
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.
|
2021-09-22 23:02:33 +02:00
|
|
|
|
2024-05-02 08:39:27 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
2021-09-22 23:02:33 +02:00
|
|
|
*/
|
|
|
|
#include "sortterminalstripcommand.h"
|
|
|
|
#include "../terminalstrip.h"
|
2021-12-26 17:26:00 +01:00
|
|
|
#include "../physicalterminal.h"
|
|
|
|
#include "../realterminal.h"
|
2021-09-22 23:02:33 +02:00
|
|
|
|
|
|
|
SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) :
|
|
|
|
QUndoCommand(parent),
|
|
|
|
m_strip(strip)
|
|
|
|
{
|
|
|
|
setText(QObject::tr("Trier le bornier %1").arg(m_strip->name()));
|
2021-12-26 17:26:00 +01:00
|
|
|
m_old_order = m_strip->physicalTerminal();
|
|
|
|
m_new_order = m_strip->physicalTerminal();
|
2021-09-22 23:02:33 +02:00
|
|
|
sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SortTerminalStripCommand::undo()
|
|
|
|
{
|
|
|
|
if (m_strip) {
|
2021-12-26 17:26:00 +01:00
|
|
|
m_strip->setOrderTo(m_old_order);
|
2021-09-22 23:02:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SortTerminalStripCommand::redo()
|
|
|
|
{
|
|
|
|
if (m_strip) {
|
2021-12-26 17:26:00 +01:00
|
|
|
m_strip->setOrderTo(m_new_order);
|
2021-09-22 23:02:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SortTerminalStripCommand::sort()
|
|
|
|
{
|
2021-12-23 22:17:37 +01:00
|
|
|
std::sort(m_new_order.begin(), m_new_order.end(), [](QSharedPointer<PhysicalTerminal> arg1, QSharedPointer<PhysicalTerminal> arg2)
|
2021-09-22 23:02:33 +02:00
|
|
|
{
|
2021-10-10 10:58:38 +02:00
|
|
|
const QRegularExpression rx(QStringLiteral("^\\d+"));
|
2021-09-22 23:02:33 +02:00
|
|
|
|
|
|
|
QString str1;
|
|
|
|
QString str2;
|
|
|
|
int int1 =-1;
|
|
|
|
int int2 =-1;
|
|
|
|
|
2021-12-23 22:17:37 +01:00
|
|
|
if (arg1->realTerminalCount())
|
2021-09-22 23:02:33 +02:00
|
|
|
{
|
2021-12-26 17:26:00 +01:00
|
|
|
str1 = arg1->realTerminals().constLast()->label();
|
2021-09-22 23:02:33 +02:00
|
|
|
|
|
|
|
auto match = rx.match(str1);
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
int1 = match.captured(0).toInt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 22:17:37 +01:00
|
|
|
if (arg2->realTerminalCount())
|
2021-09-22 23:02:33 +02:00
|
|
|
{
|
2021-12-26 17:26:00 +01:00
|
|
|
str2 = arg2->realTerminals().constLast()->label();
|
2021-09-22 23:02:33 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|