From 1699ad9dd884d07aef7a0dc93508c52a20421724 Mon Sep 17 00:00:00 2001 From: joshua Date: Wed, 22 Sep 2021 23:02:33 +0200 Subject: [PATCH] Add a push button to automatically reorder the terminal strip --- .../UndoCommand/sortterminalstripcommand.cpp | 88 ++++++++++ .../UndoCommand/sortterminalstripcommand.h | 49 ++++++ sources/TerminalStrip/terminalstrip.cpp | 56 +++++- sources/TerminalStrip/terminalstrip.h | 13 +- .../TerminalStrip/ui/terminalstripeditor.cpp | 18 ++ .../TerminalStrip/ui/terminalstripeditor.h | 1 + .../TerminalStrip/ui/terminalstripeditor.ui | 160 ++++++++++-------- 7 files changed, 311 insertions(+), 74 deletions(-) create mode 100644 sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp create mode 100644 sources/TerminalStrip/UndoCommand/sortterminalstripcommand.h diff --git a/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp new file mode 100644 index 000000000..e6187d3f7 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/sortterminalstripcommand.cpp @@ -0,0 +1,88 @@ +/* + Copyright 2006-2021 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 . +*/ +#include "sortterminalstripcommand.h" +#include "../terminalstrip.h" + +SortTerminalStripCommand::SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent) : + QUndoCommand(parent), + m_strip(strip) +{ + setText(QObject::tr("Trier le bornier %1").arg(m_strip->name())); + m_old_order = m_new_order = m_strip->physicalTerminalData(); + sort(); +} + +void SortTerminalStripCommand::undo() +{ + if (m_strip) { + m_strip->setOrderTo(m_old_order); + } +} + +void SortTerminalStripCommand::redo() +{ + if (m_strip) { + m_strip->setOrderTo(m_new_order); + } +} + +void SortTerminalStripCommand::sort() +{ + std::sort(m_new_order.begin(), m_new_order.end(), [](PhysicalTerminalData arg1, PhysicalTerminalData arg2) + { + QRegularExpression rx(QStringLiteral("^\\d+")); + + QString str1; + QString str2; + int int1 =-1; + int int2 =-1; + + if (arg1.real_terminals_vector.count()) + { + str1 = arg1.real_terminals_vector.first().label_; + + auto match = rx.match(str1); + if (match.hasMatch()) { + int1 = match.captured(0).toInt(); + } + } + + if (arg2.real_terminals_vector.count()) + { + str2 = arg2.real_terminals_vector.first().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. +*/ +#ifndef SORTTERMINALSTRIPCOMMAND_H +#define SORTTERMINALSTRIPCOMMAND_H + +#include +#include + +class TerminalStrip; +struct PhysicalTerminalData; + +/** + * @brief The SortTerminalStripCommand class + * An undo command use to sort the terminals element who + * compose a terminal strip + */ +class SortTerminalStripCommand : public QUndoCommand +{ + public: + SortTerminalStripCommand(TerminalStrip *strip, QUndoCommand *parent = nullptr); + + void undo() override; + void redo() override; + + private: + void sort(); + + private: + QPointer m_strip; + QVector m_old_order, + m_new_order; +}; + +#endif // SORTTERMINALSTRIPCOMMAND_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 4a11234ee..7b7d0a146 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -473,7 +473,7 @@ int TerminalStrip::physicalTerminalCount() const { * @param index * @return The data of the physical terminal at index \p index */ -PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) +PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) const { PhysicalTerminalData ptd; @@ -491,6 +491,56 @@ PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) return ptd; } +/** + * @brief TerminalStrip::physicalTerminalData + * @return A vector of all physical terminal data owned by this terminal strip. + * The order of the vector is the same as the order of the terminal in the strip + */ +QVector TerminalStrip::physicalTerminalData() const +{ + QVector v_; + for (auto i = 0 ; i sorted_vector) +{ + if (sorted_vector.size() != m_physical_terminals.size()) { + return false; + } + + QVector> new_order; + for (auto ptd : sorted_vector) + { + if (m_physical_terminals.contains(ptd.physical_terminal)) { + new_order.append(ptd.physical_terminal); + } else { + return false; + } + } + + m_physical_terminals = new_order; + emit orderChanged(); + return true; +} + /** * @brief TerminalStrip::terminalElement * @return A vector of all terminal element owned by this strip @@ -598,7 +648,7 @@ QSharedPointer TerminalStrip::realTerminal(Element *terminal) * @return the physical terminal linked to \p terminal. * The returned QSharedPointer can be null. */ -QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer terminal) +QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer terminal) const { shared_physical_terminal pt; @@ -623,7 +673,7 @@ Element *TerminalStrip::elementForRealTerminal(QSharedPointer rt) return rt.data()->element(); } -RealTerminalData TerminalStrip::realTerminalData(QSharedPointer real_terminal) +RealTerminalData TerminalStrip::realTerminalData(QSharedPointer real_terminal) const { RealTerminalData rtd; diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index e89182344..b56785532 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -72,6 +72,10 @@ class TerminalStrip : public QObject friend class TerminalStripModel; Q_OBJECT + public: + signals: + void orderChanged(); //Emitted when the order of the physical terminal is changed + public: TerminalStrip(QETProject *project); @@ -100,8 +104,9 @@ class TerminalStrip : public QObject bool haveTerminal (Element *terminal); int physicalTerminalCount() const; - - PhysicalTerminalData physicalTerminalData(int index); + PhysicalTerminalData physicalTerminalData(int index) const; + QVector physicalTerminalData() const; + bool setOrderTo(QVector sorted_vector); QVector> terminalElement() const; @@ -113,8 +118,8 @@ class TerminalStrip : public QObject private: QSharedPointer realTerminal(Element *terminal); - QSharedPointer physicalTerminal(QSharedPointer terminal); - RealTerminalData realTerminalData(QSharedPointer real_terminal); + QSharedPointer physicalTerminal(QSharedPointer terminal) const; + RealTerminalData realTerminalData(QSharedPointer real_terminal) const; private: TerminalStripData m_data; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index b15f59d65..80dea35ba 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -30,6 +30,7 @@ #include "../../qeticons.h" #include "terminalstripmodel.h" #include "../diagram.h" +#include "../UndoCommand/sortterminalstripcommand.h" #include @@ -273,6 +274,10 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_) return; } + if (m_current_strip) { + disconnect(m_current_strip, &TerminalStrip::orderChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); + } + if (!strip_) { ui->m_installation_le ->clear(); @@ -302,6 +307,8 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_) m_model = new TerminalStripModel(strip_, this); ui->m_table_widget->setModel(m_model); + + connect(m_current_strip, &TerminalStrip::orderChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); } } @@ -456,3 +463,14 @@ void TerminalStripEditor::on_m_dialog_button_box_clicked(QAbstractButton *button on_m_reload_pb_clicked(); } + +/** + * @brief TerminalStripEditor::on_m_auto_pos_pb_clicked + */ +void TerminalStripEditor::on_m_auto_ordering_pb_clicked() +{ + if (m_project && m_current_strip) { + m_project->undoStack()->push(new SortTerminalStripCommand(m_current_strip)); + } +} + diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 375084a1f..b00910437 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -57,6 +57,7 @@ class TerminalStripEditor : public QDialog void on_m_reload_pb_clicked(); void on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); void on_m_dialog_button_box_clicked(QAbstractButton *button); + void on_m_auto_ordering_pb_clicked(); private: Ui::TerminalStripEditor *ui; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index da0d41634..35dba993c 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -13,72 +13,8 @@ Gestionnaire de borniers - - - - - - 0 - - - 0 - - - 0 - - - 0 - - - - - Ajouter un bornier - - - - :/ico/16x16/list-add.png:/ico/16x16/list-add.png - - - - - - - Supprimer le bornier - - - - :/ico/16x16/list-remove.png:/ico/16x16/list-remove.png - - - - - - - - - - - :/ico/16x16/view-refresh.png:/ico/16x16/view-refresh.png - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - + + Qt::Horizontal @@ -156,7 +92,7 @@ true - true + false @@ -239,6 +175,96 @@ + + + + + + + Position automatique + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Ajouter un bornier + + + + :/ico/16x16/list-add.png:/ico/16x16/list-add.png + + + + + + + Supprimer le bornier + + + + :/ico/16x16/list-remove.png:/ico/16x16/list-remove.png + + + + + + + + + + + :/ico/16x16/view-refresh.png:/ico/16x16/view-refresh.png + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + +