Add a push button to automatically reorder the terminal strip

This commit is contained in:
joshua
2021-09-22 23:02:33 +02:00
parent 377f8b1521
commit 1699ad9dd8
7 changed files with 311 additions and 74 deletions

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#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<int2;
}
else {
return str1<str2;
}
});
}

View File

@@ -0,0 +1,49 @@
/*
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 <http://www.gnu.org/licenses/>.
*/
#ifndef SORTTERMINALSTRIPCOMMAND_H
#define SORTTERMINALSTRIPCOMMAND_H
#include <QUndoCommand>
#include <QPointer>
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<TerminalStrip> m_strip;
QVector<PhysicalTerminalData> m_old_order,
m_new_order;
};
#endif // SORTTERMINALSTRIPCOMMAND_H

View File

@@ -473,7 +473,7 @@ int TerminalStrip::physicalTerminalCount() const {
* @param index * @param index
* @return The data of the physical terminal at index \p 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; PhysicalTerminalData ptd;
@@ -491,6 +491,56 @@ PhysicalTerminalData TerminalStrip::physicalTerminalData(int index)
return ptd; 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<PhysicalTerminalData> TerminalStrip::physicalTerminalData() const
{
QVector<PhysicalTerminalData> v_;
for (auto i = 0 ; i<physicalTerminalCount() ; ++i) {
v_.append(physicalTerminalData(i));
}
return v_;
}
/**
* @brief TerminalStrip::setSortedTo
* Sort the physical terminal owned by this strip in the same order
* as \p sorted_vector.
* \p sorted_vector must contain exaclty the same physical terminal as this strip
* else this function do nothing.
*
* To avoid any mistake, you should call TerminalStrip::physicalTerminalData()
* sort the returned vector and call this function with sorted vector, then you are sure
* the vector contain the same values, no more no less.
*
* @param sorted_vector
* @return true is successfully sorted.
*/
bool TerminalStrip::setOrderTo(QVector<PhysicalTerminalData> sorted_vector)
{
if (sorted_vector.size() != m_physical_terminals.size()) {
return false;
}
QVector<QSharedPointer<PhysicalTerminal>> 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 * @brief TerminalStrip::terminalElement
* @return A vector of all terminal element owned by this strip * @return A vector of all terminal element owned by this strip
@@ -598,7 +648,7 @@ QSharedPointer<RealTerminal> TerminalStrip::realTerminal(Element *terminal)
* @return the physical terminal linked to \p terminal. * @return the physical terminal linked to \p terminal.
* The returned QSharedPointer can be null. * The returned QSharedPointer can be null.
*/ */
QSharedPointer<PhysicalTerminal> TerminalStrip::physicalTerminal(QSharedPointer<RealTerminal> terminal) QSharedPointer<PhysicalTerminal> TerminalStrip::physicalTerminal(QSharedPointer<RealTerminal> terminal) const
{ {
shared_physical_terminal pt; shared_physical_terminal pt;
@@ -623,7 +673,7 @@ Element *TerminalStrip::elementForRealTerminal(QSharedPointer<RealTerminal> rt)
return rt.data()->element(); return rt.data()->element();
} }
RealTerminalData TerminalStrip::realTerminalData(QSharedPointer<RealTerminal> real_terminal) RealTerminalData TerminalStrip::realTerminalData(QSharedPointer<RealTerminal> real_terminal) const
{ {
RealTerminalData rtd; RealTerminalData rtd;

View File

@@ -72,6 +72,10 @@ class TerminalStrip : public QObject
friend class TerminalStripModel; friend class TerminalStripModel;
Q_OBJECT Q_OBJECT
public:
signals:
void orderChanged(); //Emitted when the order of the physical terminal is changed
public: public:
TerminalStrip(QETProject *project); TerminalStrip(QETProject *project);
@@ -100,8 +104,9 @@ class TerminalStrip : public QObject
bool haveTerminal (Element *terminal); bool haveTerminal (Element *terminal);
int physicalTerminalCount() const; int physicalTerminalCount() const;
PhysicalTerminalData physicalTerminalData(int index) const;
PhysicalTerminalData physicalTerminalData(int index); QVector<PhysicalTerminalData> physicalTerminalData() const;
bool setOrderTo(QVector<PhysicalTerminalData> sorted_vector);
QVector<QPointer<Element>> terminalElement() const; QVector<QPointer<Element>> terminalElement() const;
@@ -113,8 +118,8 @@ class TerminalStrip : public QObject
private: private:
QSharedPointer<RealTerminal> realTerminal(Element *terminal); QSharedPointer<RealTerminal> realTerminal(Element *terminal);
QSharedPointer<PhysicalTerminal> physicalTerminal(QSharedPointer<RealTerminal> terminal); QSharedPointer<PhysicalTerminal> physicalTerminal(QSharedPointer<RealTerminal> terminal) const;
RealTerminalData realTerminalData(QSharedPointer<RealTerminal> real_terminal); RealTerminalData realTerminalData(QSharedPointer<RealTerminal> real_terminal) const;
private: private:
TerminalStripData m_data; TerminalStripData m_data;

View File

@@ -30,6 +30,7 @@
#include "../../qeticons.h" #include "../../qeticons.h"
#include "terminalstripmodel.h" #include "terminalstripmodel.h"
#include "../diagram.h" #include "../diagram.h"
#include "../UndoCommand/sortterminalstripcommand.h"
#include <QTreeWidgetItem> #include <QTreeWidgetItem>
@@ -273,6 +274,10 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_)
return; return;
} }
if (m_current_strip) {
disconnect(m_current_strip, &TerminalStrip::orderChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked);
}
if (!strip_) if (!strip_)
{ {
ui->m_installation_le ->clear(); ui->m_installation_le ->clear();
@@ -302,6 +307,8 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_)
m_model = new TerminalStripModel(strip_, this); m_model = new TerminalStripModel(strip_, this);
ui->m_table_widget->setModel(m_model); 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(); 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));
}
}

View File

@@ -57,6 +57,7 @@ class TerminalStripEditor : public QDialog
void on_m_reload_pb_clicked(); void on_m_reload_pb_clicked();
void on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); void on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);
void on_m_dialog_button_box_clicked(QAbstractButton *button); void on_m_dialog_button_box_clicked(QAbstractButton *button);
void on_m_auto_ordering_pb_clicked();
private: private:
Ui::TerminalStripEditor *ui; Ui::TerminalStripEditor *ui;

View File

@@ -13,72 +13,8 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Gestionnaire de borniers</string> <string>Gestionnaire de borniers</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="0,1"> <layout class="QGridLayout" name="gridLayout">
<item> <item row="1" column="0">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="m_add_terminal_strip_pb">
<property name="text">
<string>Ajouter un bornier</string>
</property>
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/16x16/list-add.png</normaloff>:/ico/16x16/list-add.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_remove_terminal_strip_pb">
<property name="text">
<string>Supprimer le bornier</string>
</property>
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/16x16/list-remove.png</normaloff>:/ico/16x16/list-remove.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_reload_pb">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/16x16/view-refresh.png</normaloff>:/ico/16x16/view-refresh.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter"> <widget class="QSplitter" name="splitter">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
@@ -156,7 +92,7 @@
<bool>true</bool> <bool>true</bool>
</attribute> </attribute>
<attribute name="verticalHeaderVisible"> <attribute name="verticalHeaderVisible">
<bool>true</bool> <bool>false</bool>
</attribute> </attribute>
</widget> </widget>
</item> </item>
@@ -239,6 +175,96 @@
</widget> </widget>
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QPushButton" name="m_auto_ordering_pb">
<property name="text">
<string>Position automatique</string>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="m_add_terminal_strip_pb">
<property name="text">
<string>Ajouter un bornier</string>
</property>
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/16x16/list-add.png</normaloff>:/ico/16x16/list-add.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_remove_terminal_strip_pb">
<property name="text">
<string>Supprimer le bornier</string>
</property>
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/16x16/list-remove.png</normaloff>:/ico/16x16/list-remove.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_reload_pb">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../../../qelectrotech.qrc">
<normaloff>:/ico/16x16/view-refresh.png</normaloff>:/ico/16x16/view-refresh.png</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<customwidgets> <customwidgets>