From 9cbc3a2265f0d0b7c1bb2f324a7a6d6241946324 Mon Sep 17 00:00:00 2001 From: joshua Date: Sat, 12 Mar 2022 19:07:49 +0100 Subject: [PATCH] Add table widget and item model for free terminal WIP, the change made in the table widget can't be applied. --- .../TerminalStrip/ui/freeterminaleditor.cpp | 81 ++++ sources/TerminalStrip/ui/freeterminaleditor.h | 50 ++ .../TerminalStrip/ui/freeterminaleditor.ui | 141 ++++++ .../TerminalStrip/ui/freeterminalmodel.cpp | 352 ++++++++++++++ sources/TerminalStrip/ui/freeterminalmodel.h | 83 ++++ sources/TerminalStrip/ui/modelTerminalData.h | 76 +++ .../TerminalStrip/ui/terminalstripeditor.cpp | 18 +- .../TerminalStrip/ui/terminalstripeditor.h | 4 + .../TerminalStrip/ui/terminalstripeditor.ui | 456 +++++++++--------- .../TerminalStrip/ui/terminalstripmodel.cpp | 32 +- sources/TerminalStrip/ui/terminalstripmodel.h | 35 +- sources/utils/qetutils.cpp | 45 ++ sources/utils/qetutils.h | 2 + 13 files changed, 1084 insertions(+), 291 deletions(-) create mode 100644 sources/TerminalStrip/ui/freeterminaleditor.cpp create mode 100644 sources/TerminalStrip/ui/freeterminaleditor.h create mode 100644 sources/TerminalStrip/ui/freeterminaleditor.ui create mode 100644 sources/TerminalStrip/ui/freeterminalmodel.cpp create mode 100644 sources/TerminalStrip/ui/freeterminalmodel.h create mode 100644 sources/TerminalStrip/ui/modelTerminalData.h diff --git a/sources/TerminalStrip/ui/freeterminaleditor.cpp b/sources/TerminalStrip/ui/freeterminaleditor.cpp new file mode 100644 index 000000000..eced54d6f --- /dev/null +++ b/sources/TerminalStrip/ui/freeterminaleditor.cpp @@ -0,0 +1,81 @@ +/* + Copyright 2006-2022 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 "freeterminaleditor.h" +#include "ui_freeterminaleditor.h" +#include "../../elementprovider.h" +#include "freeterminalmodel.h" +#include "../../diagram.h" + +/** + * @brief FreeTerminalEditor::FreeTerminalEditor + * @param project + * @param parent + */ +FreeTerminalEditor::FreeTerminalEditor(QETProject *project, QWidget *parent) : + QWidget(parent), + ui(new Ui::FreeTerminalEditor), + m_project(project) +{ + ui->setupUi(this); + ui->m_table_view->setItemDelegate(new FreeTerminalModelDelegate(ui->m_table_view)); + + m_model = new FreeTerminalModel(m_project, this); + ui->m_table_view->setModel(m_model); + ui->m_table_view->setCurrentIndex(m_model->index(0,0)); + + connect(ui->m_table_view, &QAbstractItemView::doubleClicked, this, [=](const QModelIndex &index) + { + if (m_model->columnTypeForIndex(index) == FreeTerminalModel::XRef) + { + auto mrtd = m_model->dataAtRow(index.row()); + if (mrtd.element_) + { + auto elmt = mrtd.element_; + auto diagram = elmt->diagram(); + if (diagram) + { + diagram->showMe(); + if (diagram->views().size()) + { + auto fit_view = elmt->sceneBoundingRect(); + fit_view.adjust(-200,-200,200,200); + diagram->views().at(0)->fitInView(fit_view, Qt::KeepAspectRatioByExpanding); + } + } + } + } + }); +} + +/** + * @brief FreeTerminalEditor::~FreeTerminalEditor + */ +FreeTerminalEditor::~FreeTerminalEditor() +{ + delete ui; +} + +/** + * @brief FreeTerminalEditor::reload + * Reload the editor to be up to date with + * the current state of the project. + * Every not applied change will be lost. + */ +void FreeTerminalEditor::reload() { + m_model->clear(); +} diff --git a/sources/TerminalStrip/ui/freeterminaleditor.h b/sources/TerminalStrip/ui/freeterminaleditor.h new file mode 100644 index 000000000..41978f94f --- /dev/null +++ b/sources/TerminalStrip/ui/freeterminaleditor.h @@ -0,0 +1,50 @@ +/* + Copyright 2006-2022 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 . +*/ +#ifndef FREETERMINALEDITOR_H +#define FREETERMINALEDITOR_H + +#include + +class QETProject; +class RealTerminal; +class FreeTerminalModel; +class QTableView; + +namespace Ui { + class FreeTerminalEditor; +} + +class FreeTerminalEditor : public QWidget +{ + Q_OBJECT + + public: + explicit FreeTerminalEditor(QETProject *project, QWidget *parent = nullptr); + ~FreeTerminalEditor(); + + void reload(); + + private: + void selectionChanged(); + + private: + Ui::FreeTerminalEditor *ui; + QETProject *m_project = nullptr; + FreeTerminalModel *m_model = nullptr; +}; +#endif // FREETERMINALEDITOR_H diff --git a/sources/TerminalStrip/ui/freeterminaleditor.ui b/sources/TerminalStrip/ui/freeterminaleditor.ui new file mode 100644 index 000000000..027e20eff --- /dev/null +++ b/sources/TerminalStrip/ui/freeterminaleditor.ui @@ -0,0 +1,141 @@ + + + FreeTerminalEditor + + + + 0 + 0 + 751 + 333 + + + + Form + + + + + + + Générique + + + + + Phase + + + + + Neutre + + + + + + + + Déplacer dans : + + + + + + + Qt::Horizontal + + + + + + + + Générique + + + + + Fusible + + + + + Sectionnable + + + + + Diode + + + + + Terre + + + + + + + + Fonction : + + + + + + + Type : + + + + + + + LED : + + + + + + + + Sans + + + + + Avec + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + true + + + + + + + + diff --git a/sources/TerminalStrip/ui/freeterminalmodel.cpp b/sources/TerminalStrip/ui/freeterminalmodel.cpp new file mode 100644 index 000000000..1ff845036 --- /dev/null +++ b/sources/TerminalStrip/ui/freeterminalmodel.cpp @@ -0,0 +1,352 @@ +/* + Copyright 2006-2022 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 + +#include "freeterminalmodel.h" +#include "../../elementprovider.h" +#include "../../utils/qetutils.h" +#include "../../qetgraphicsitem/terminalelement.h" +#include "../realterminal.h" + +const int LABEL_CELL = 0; +const int XREF_CELL = 1; +const int TYPE_CELL = 2; +const int FUNCTION_CELL = 3; +const int LED_CELL = 4; + +const int ROW_COUNT = 5; + +static QVector UNMODIFIED_CELL_VECTOR{false, false, false, false, false}; + +FreeTerminalModel::Column FreeTerminalModel::columnTypeForIndex(const QModelIndex &index) +{ + if (index.isValid()) + { + switch (index.column()) { + case 0 : return Label; + case 1 : return XRef; + case 2 : return Type; + case 3 : return Function; + case 4 : return Led; + default : return Invalid; + } + } + return Invalid; +} + +/** + * @brief FreeTerminalModel::FreeTerminalModel + * @param project + * @param parent + */ +FreeTerminalModel::FreeTerminalModel(QETProject *project, QObject *parent) : + QAbstractTableModel(parent), + m_project(project) +{ + fillTerminalVector(); +} + +/** + * @brief FreeTerminalModel::rowCount + * @param parent + */ +int FreeTerminalModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + + return m_terminal_vector.size(); +} + +/** + * @brief FreeTerminalModel::columnCount + * @param parent + */ +int FreeTerminalModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return ROW_COUNT; +} + +/** + * @brief FreeTerminalModel::data + * @param index + * @param role + * @return + */ +QVariant FreeTerminalModel::data(const QModelIndex &index, int role) const +{ + if (index.row() >= rowCount(QModelIndex())) { + return QVariant(); + } + + const auto real_t_data = m_real_t_data.at(index.row()); + + if (role == Qt::DisplayRole) + { + + switch (index.column()) + { + case LABEL_CELL: return real_t_data.label_; + case XREF_CELL : return real_t_data.Xref_; + case TYPE_CELL : return ElementData::translatedTerminalType(real_t_data.type_); + case FUNCTION_CELL: return ElementData::translatedTerminalFunction(real_t_data.function_); + default : return QVariant(); + } + } + else if (role == Qt::EditRole) + { + switch (index.column()) { + case LABEL_CELL : return real_t_data.label_; + default : return QVariant(); + } + } + else if (role == Qt::CheckStateRole && + index.column() == LED_CELL) { + return real_t_data.led_ ? Qt::Checked : Qt::Unchecked; + } + else if (role == Qt::BackgroundRole && index.column() <= LED_CELL) + { + if (m_modified_cell.contains(real_t_data.real_terminal) && + m_modified_cell.value(real_t_data.real_terminal).at(index.column())) + { + return QBrush(Qt::yellow); + } + } + + return QVariant(); +} + +bool FreeTerminalModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + auto mrtd = dataAtRow(index.row()); + bool modified_ = false; + int modified_cell = -1; + auto column_ = index.column(); + + + if (column_ == LABEL_CELL && + role == Qt::EditRole && + mrtd.label_ != value.toString()) + { + mrtd.label_ = value.toString(); + modified_ = true; + modified_cell = LABEL_CELL; + } + else if (column_ == TYPE_CELL && + role == Qt::EditRole) + { + mrtd.type_ = value.value(); + modified_ = true; + modified_cell = TYPE_CELL; + } + else if (column_ == FUNCTION_CELL && + role == Qt::EditRole) + { + mrtd.function_ = value.value(); + modified_ = true; + modified_cell = FUNCTION_CELL; + } + else if (column_ == LED_CELL) + { + mrtd.led_ = value.toBool(); + modified_ = true; + modified_cell = LED_CELL; + } + + //Set the modification to the terminal data + if (modified_) + { + m_real_t_data.replace(index.row(), mrtd); + + QVector vector_; + if (m_modified_cell.contains(mrtd.real_terminal)) { + vector_ = m_modified_cell.value(mrtd.real_terminal); + } else { + vector_ = UNMODIFIED_CELL_VECTOR; + } + + vector_.replace(modified_cell, true); + m_modified_cell.insert(mrtd.real_terminal, vector_); + emit dataChanged(index, index); + return true; + } + + return false; +} + +/** + * @brief FreeTerminalModel::headerData + * @param section + * @param orientation + * @param role + * @return + */ +QVariant FreeTerminalModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole) + { + if (orientation == Qt::Horizontal) + { + switch (section) { + case LABEL_CELL: return tr("Label"); + case XREF_CELL: return tr("Référence croisé"); + case TYPE_CELL: return tr("Type"); + case FUNCTION_CELL: return tr("Fonction"); + case LED_CELL: return tr("led"); + default : return QVariant(); + } + } + } + + return QVariant(); +} + +Qt::ItemFlags FreeTerminalModel::flags(const QModelIndex &index) const +{ + Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; + + auto c = index.column(); + if (c == LABEL_CELL || c == TYPE_CELL || c == FUNCTION_CELL) + flags = flags | Qt::ItemIsEditable; + if (c == LED_CELL) { + flags = flags | Qt::ItemIsUserCheckable; + } + return flags; +} + +/** + * @brief FreeTerminalModel::clear + * Clear the model and set it as the current + * state of the project + */ +void FreeTerminalModel::clear() +{ + beginResetModel(); + m_terminal_vector.clear(); + m_real_t_data.clear(); + m_modified_cell.clear(); + fillTerminalVector(); + endResetModel(); +} + +/** + * @brief FreeTerminalModel::modifiedModelRealTerminalData + * @return a vector of modified terminal. + */ +QVector FreeTerminalModel::modifiedModelRealTerminalData() const +{ + QVector returned_vector; + + for (const auto &real_t_data : m_real_t_data) { + if (m_modified_cell.contains(real_t_data.real_terminal)) { + returned_vector.append(real_t_data); + } + } + + return returned_vector; +} + +/** + * @brief FreeTerminalModel::dataAtRow + * @param row + * @return the current data at row @a row + */ +modelRealTerminalData FreeTerminalModel::dataAtRow(int row) const +{ + if (row >= m_terminal_vector.size()) { + return modelRealTerminalData(); + } else { + return m_real_t_data.at(row); + } +} + +/** + * @brief FreeTerminalModel::fillTerminalVector + */ +void FreeTerminalModel::fillTerminalVector() +{ + ElementProvider provider_(m_project); + auto free_terminal_vector = provider_.freeTerminal(); + + std::sort(free_terminal_vector.begin(), free_terminal_vector.end(), + [](TerminalElement *a, TerminalElement *b) + { + return QETUtils::sortBeginIntString(a->actualLabel(), b->actualLabel()); + }); + + for (const auto &terminal_ : free_terminal_vector) { + m_terminal_vector.append(terminal_->realTerminal()); + m_real_t_data.append(modelRealTerminalData::data(terminal_->realTerminal())); + } +} + +/**************************************************************** + * A little delegate for add a combobox to edit type and function + ****************************************************************/ + +FreeTerminalModelDelegate::FreeTerminalModelDelegate(QObject *parent) : + QStyledItemDelegate(parent) +{} + +QWidget *FreeTerminalModelDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + if (index.column() == TYPE_CELL) { + auto qcb = new QComboBox(parent); + qcb->setObjectName(QLatin1String("terminal_type")); + qcb->addItem(ElementData::translatedTerminalType(ElementData::TTGeneric), ElementData::TTGeneric); + qcb->addItem(ElementData::translatedTerminalType(ElementData::TTFuse), ElementData::TTFuse); + qcb->addItem(ElementData::translatedTerminalType(ElementData::TTSectional), ElementData::TTSectional); + qcb->addItem(ElementData::translatedTerminalType(ElementData::TTDiode), ElementData::TTDiode); + qcb->addItem(ElementData::translatedTerminalType(ElementData::TTGround), ElementData::TTGround); + + return qcb; + } + if (index.column() == FUNCTION_CELL) { + auto qcb = new QComboBox(parent); + qcb->setObjectName(QLatin1String("terminal_function")); + qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFGeneric), ElementData::TFGeneric); + qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFPhase), ElementData::TFPhase); + qcb->addItem(ElementData::translatedTerminalFunction(ElementData::TFNeutral), ElementData::TFNeutral); + + return qcb; + } + + return QStyledItemDelegate::createEditor(parent, option, index); +} + +void FreeTerminalModelDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const +{ + if (index.isValid()) + { + if (editor->objectName() == QLatin1String("terminal_type")) + { + if (auto qcb = dynamic_cast(editor)) { + model->setData(index, qcb->currentData(), Qt::EditRole); + } + } + else if (editor->objectName() == QLatin1String("terminal_function")) + { + if (auto qcb = dynamic_cast(editor)) { + model->setData(index, qcb->currentData(), Qt::EditRole); + } + } + else { + QStyledItemDelegate::setModelData(editor, model, index); + } + } +} diff --git a/sources/TerminalStrip/ui/freeterminalmodel.h b/sources/TerminalStrip/ui/freeterminalmodel.h new file mode 100644 index 000000000..5e135e173 --- /dev/null +++ b/sources/TerminalStrip/ui/freeterminalmodel.h @@ -0,0 +1,83 @@ +/* + Copyright 2006-2022 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 . +*/ +#ifndef FREETERMINALMODEL_H +#define FREETERMINALMODEL_H + +#include +#include +#include + +#include "modelTerminalData.h" +#include "../../qetproject.h" + +class RealTerminal; + +/** + * @brief The FreeTerminalModel class + */ +class FreeTerminalModel : public QAbstractTableModel +{ + public: + enum Column { + Label = 0, + XRef = 1, + Type = 2, + Function = 3, + Led = 4, + Invalid = 99 + }; + + static FreeTerminalModel::Column columnTypeForIndex(const QModelIndex &index); + Q_OBJECT + + public: + explicit FreeTerminalModel(QETProject *project, QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent) const override; + int columnCount(const QModelIndex &parent) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role) override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; + + void clear(); + QVector modifiedModelRealTerminalData() const; + modelRealTerminalData dataAtRow(int row) const; + + private: + void fillTerminalVector(); + + private: + QPointer m_project; + QVector> m_terminal_vector; + QVector m_real_t_data; + QHash, QVector> m_modified_cell; +}; + +class FreeTerminalModelDelegate : public QStyledItemDelegate +{ + Q_OBJECT + + public: + FreeTerminalModelDelegate(QObject *parent = nullptr); + + QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; +}; + +#endif // FREETERMINALMODEL_H diff --git a/sources/TerminalStrip/ui/modelTerminalData.h b/sources/TerminalStrip/ui/modelTerminalData.h new file mode 100644 index 000000000..fd58b66f5 --- /dev/null +++ b/sources/TerminalStrip/ui/modelTerminalData.h @@ -0,0 +1,76 @@ +/* + 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 . +*/ +#ifndef MODELTERMINALDATA_H +#define MODELTERMINALDATA_H + +#include +#include "../../qetgraphicsitem/element.h" +#include "../realterminal.h" + +struct modelRealTerminalData +{ + static modelRealTerminalData data(const QSharedPointer &real_t) + { + modelRealTerminalData mrtd; + if (!real_t.isNull()) + { + mrtd.level_ = real_t->level(); + mrtd.label_ = real_t->label(); + mrtd.Xref_ = real_t->Xref(); + mrtd.cable_ = real_t->cable(); + mrtd.cable_wire = real_t->cableWire(); + mrtd.conductor_ = real_t->conductor(); + mrtd.led_ = real_t->isLed(); + mrtd.type_ = real_t->type(); + mrtd.function_ = real_t->function(); + mrtd.element_ = real_t->element(); + mrtd.real_terminal = real_t.toWeakRef(); + mrtd.bridged_ = real_t->isBridged(); + } + + return mrtd; + } + + int level_ = -1; + QString label_; + QString Xref_; + QString cable_; + QString cable_wire; + QString conductor_; + bool led_ = false; + bool bridged_ = false; + + ElementData::TerminalType type_ = ElementData::TerminalType::TTGeneric; + ElementData::TerminalFunction function_ = ElementData::TerminalFunction::TFGeneric; + QPointer element_; + + QWeakPointer real_terminal; +}; + +struct modelPhysicalTerminalData +{ + QVector real_data; + int pos_ = -1; + QUuid uuid_; +}; + +inline bool operator == (const modelPhysicalTerminalData &data_1, const modelPhysicalTerminalData &data_2) { + return data_1.uuid_ == data_2.uuid_; +} + +#endif // MODELTERMINALDATA_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index b5bd0eb9e..f0e7b6d49 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -20,7 +20,7 @@ #include "terminalstripcreatordialog.h" #include "../../qetproject.h" #include "../terminalstrip.h" -#include "../elementprovider.h" +#include "../../elementprovider.h" #include "../qetgraphicsitem/terminalelement.h" #include "../UndoCommand/addterminalstripcommand.h" #include "../UndoCommand/addterminaltostripcommand.h" @@ -38,6 +38,7 @@ #include "../physicalterminal.h" #include "../realterminal.h" #include "../terminalstripbridge.h" +#include "freeterminaleditor.h" #include @@ -66,11 +67,16 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : //Setup the bridge color ui->m_bridge_color_cb->setColors(TerminalStripBridge::bridgeColor().toList()); + m_free_terminal_editor = new FreeTerminalEditor(project, this); + ui->verticalLayout_2->insertWidget(1, m_free_terminal_editor); + setUpUndoConnections(); //Call for update the state of child widgets selectionChanged(); + updateWidget(); + //Go the diagram of double clicked terminal connect(ui->m_table_widget, &QAbstractItemView::doubleClicked, this, [=](const QModelIndex &index) { @@ -350,6 +356,8 @@ void TerminalStripEditor::setCurrentStrip(TerminalStrip *strip_) connect(m_current_strip, &TerminalStrip::bridgeChanged, this, &TerminalStripEditor::on_m_reload_pb_clicked); connect(ui->m_table_widget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TerminalStripEditor::selectionChanged); } + + updateWidget(); } /** @@ -537,6 +545,12 @@ QPair > TerminalStrip return qMakePair(TerminalStripModel::Invalid, QVector()); } +void TerminalStripEditor::updateWidget() +{ + ui->m_tab_widget->setVisible(m_current_strip); + m_free_terminal_editor->setHidden(m_current_strip); +} + /** * @brief TerminalStripEditor::on_m_add_terminal_strip_pb_clicked * Action when user click on add terminal strip button @@ -614,6 +628,8 @@ void TerminalStripEditor::on_m_reload_pb_clicked() if (item) { ui->m_terminal_strip_tw->setCurrentItem(item); } + + m_free_terminal_editor->reload(); } /** diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index ac622380d..d91edbd07 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -31,6 +31,7 @@ class TerminalStrip; class QTreeWidgetItem; class TerminalElement; class QAbstractButton; +class FreeTerminalEditor; /** * @brief The TerminalStripEditor class @@ -56,6 +57,7 @@ class TerminalStripEditor : public QDialog QSize setUpBridgeCellWidth(); TerminalStripModel::Column isSingleColumnSelected() const; QPair> singleColumnData() const; + void updateWidget(); private slots: void on_m_add_terminal_strip_pb_clicked(); @@ -83,6 +85,8 @@ class TerminalStripEditor : public QDialog QHash> m_uuid_strip_H; TerminalStrip *m_current_strip = nullptr; TerminalStripModel *m_model = nullptr; + FreeTerminalEditor *m_free_terminal_editor = nullptr; + }; #endif // TERMINALSTRIPEDITOR_H diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index 410adace0..d94948a96 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -14,6 +14,234 @@ Gestionnaire de borniers + + + + + + + Position automatique + + + + + + + Degrouper les bornes + + + + + + + Couleur pont : + + + + + + + + Générique + + + + + Phase + + + + + Neutre + + + + + + + + Ponter les bornes + + + + + + + LED : + + + + + + + + + + Fonction : + + + + + + + Grouper les bornes + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + Sans + + + + + Avec + + + + + + + + + Générique + + + + + Fusible + + + + + Sectionnable + + + + + Diode + + + + + Terre + + + + + + + + Qt::Horizontal + + + + + + + + + + Étage : + + + + + + + Type : + + + + + + + Déponter les bornes + + + + + + + + + + + 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 + + + + + + + @@ -175,234 +403,6 @@ - - - - - - - Étage : - - - - - - - - Générique - - - - - Fusible - - - - - Sectionnable - - - - - Diode - - - - - Terre - - - - - - - - LED : - - - - - - - - - - Qt::Horizontal - - - - - - - Qt::Vertical - - - - 20 - 40 - - - - - - - - Position automatique - - - - - - - Fonction : - - - - - - - Couleur pont : - - - - - - - Grouper les bornes - - - - - - - Degrouper les bornes - - - - - - - Type : - - - - - - - - Sans - - - - - Avec - - - - - - - - - - - - Générique - - - - - Phase - - - - - Neutre - - - - - - - - Ponter les bornes - - - - - - - Déponter les bornes - - - - - - - - - - - 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 - - - - - - - diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 4387b8a96..8f1266d71 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -306,8 +306,7 @@ Qt::ItemFlags TerminalStripModel::flags(const QModelIndex &index) const /** * @brief TerminalStripModel::modifiedRealTerminalData - * @return a vector of QPair of modified terminal. - * the first value of the QPair is the original data, the second value is the edited data + * @return a vector of modified terminal. */ QVector TerminalStripModel::modifiedmodelRealTerminalData() const { @@ -495,7 +494,7 @@ void TerminalStripModel::fillPhysicalTerminalData() { if (!real_t.isNull()) { - mptd.real_data.append(modelRealData(real_t)); + mptd.real_data.append(modelRealTerminalData::data(real_t)); } } @@ -684,7 +683,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const modelRealTerminalData previous_data; do { - current_real_terminal = modelRealData(m_terminal_strip->previousRealTerminal(current_real_terminal.real_terminal)); + current_real_terminal = modelRealTerminalData::data(m_terminal_strip->previousRealTerminal(current_real_terminal.real_terminal)); if (current_real_terminal.level_ == -1) { break; @@ -719,7 +718,7 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const modelRealTerminalData next_data; do { - current_real_terminal = modelRealData(m_terminal_strip->nextRealTerminal(current_real_terminal.real_terminal)); + current_real_terminal = modelRealTerminalData::data(m_terminal_strip->nextRealTerminal(current_real_terminal.real_terminal)); if (current_real_terminal.level_ == -1) { break; @@ -758,29 +757,6 @@ QPixmap TerminalStripModel::bridgePixmapFor(const QModelIndex &index) const return QPixmap(); } -modelRealTerminalData TerminalStripModel::modelRealData(const QWeakPointer &real_terminal) -{ - modelRealTerminalData mrtd; - const auto real_t = real_terminal.toStrongRef(); - if (!real_terminal.isNull()) - { - mrtd.level_ = real_t->level(); - mrtd.label_ = real_t->label(); - mrtd.Xref_ = real_t->Xref(); - mrtd.cable_ = real_t->cable(); - mrtd.cable_wire = real_t->cableWire(); - mrtd.conductor_ = real_t->conductor(); - mrtd.led_ = real_t->isLed(); - mrtd.type_ = real_t->type(); - mrtd.function_ = real_t->function(); - mrtd.element_ = real_t->element(); - mrtd.real_terminal = real_terminal; - mrtd.bridged_ = real_t->isBridged(); - } - - return mrtd; -} - /*********************************************************** * A little delegate for add a combobox to edit type * and a spinbox to edit the level of a terminal diff --git a/sources/TerminalStrip/ui/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 5e0db6865..40ffaf8f4 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -26,7 +26,7 @@ #include #include "../terminalstrip.h" -#include "../../qetgraphicsitem/element.h" +#include "modelTerminalData.h" //Code to use QColor as key for QHash inline uint qHash(const QColor &key, uint seed) { @@ -43,37 +43,6 @@ inline uint qHash(const QPointer &key, uint seed) { class TerminalStrip; - -struct modelRealTerminalData -{ - int level_ = -1; - QString label_; - QString Xref_; - QString cable_; - QString cable_wire; - QString conductor_; - bool led_ = false; - bool bridged_ = false; - - ElementData::TerminalType type_ = ElementData::TerminalType::TTGeneric; - ElementData::TerminalFunction function_ = ElementData::TerminalFunction::TFGeneric; - QPointer element_; - - QWeakPointer real_terminal; - -}; - -struct modelPhysicalTerminalData -{ - QVector real_data; - int pos_ = -1; - QUuid uuid_; -}; - -inline bool operator == (const modelPhysicalTerminalData &data_1, const modelPhysicalTerminalData &data_2) { - return data_1.uuid_ == data_2.uuid_; -} - class TerminalStripModel : public QAbstractTableModel { public: @@ -124,8 +93,6 @@ class TerminalStripModel : public QAbstractTableModel modelRealTerminalData realDataAtIndex(int index) const; QPixmap bridgePixmapFor(const QModelIndex &index) const; - static modelRealTerminalData modelRealData(const QWeakPointer &real_terminal); - private: QPointer m_terminal_strip; QHash, QVector> m_modified_cell; diff --git a/sources/utils/qetutils.cpp b/sources/utils/qetutils.cpp index 7bb3c92b6..769b0766e 100644 --- a/sources/utils/qetutils.cpp +++ b/sources/utils/qetutils.cpp @@ -87,3 +87,48 @@ qreal QETUtils::graphicsHandlerSize(QGraphicsItem *item) //Default value return 10; } + +/** + * @brief QETUtils::sortBeginIntString + * Sort the string @a str_a and @a str_b and take in + * count if string begin with an int to sort it + * as int and not as string in this case. + * For exemple if we have to sort the string : + * "3str", 10str", "100str", "2str", "20str". + * The default behavior when sorting QString with the comparaison operator will be: + * "10str" "100str" "2str", "20str", "3str" + * When sorting with this function, the result will be : + * "10str", "2str", "3str", "20str", "100str" + * @param str_a + * @param str_b + * @return + */ +bool QETUtils::sortBeginIntString(const QString &str_a, const QString &str_b) +{ + const QRegularExpression rx(QStringLiteral("^\\d+")); + int int_a =-1; + int int_b =-1; + + auto match_a = rx.match(str_a); + if (match_a.hasMatch()) { + int_a = match_a.captured(0).toInt(); + } + + auto match_b = rx.match(str_b); + if (match_b.hasMatch()) { + int_b = match_b.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 (int_a >= 0 && + int_b >= 0 && + int_a != int_b) { + return int_a QVector> sharedVectorToWeak(const QVector> &vector) {