diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 17f218ab9..945f236be 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -85,12 +85,28 @@ class RealTerminal ElementData::TerminalType type() const { if (m_element) { - return m_element->elementData().m_terminal_type; + return m_element->elementData().terminalType(); } else { return ElementData::TTGeneric; } } + ElementData::TerminalFunction function() const { + if (m_element) { + return m_element->elementData().terminalFunction(); + } else { + return ElementData::TFGeneric; + } + } + + bool led() const { + if (m_element) { + return m_element->elementData().terminalLed(); + } else { + return false; + } + } + /** * @brief elementUuid * @return if this real terminal is an element @@ -452,16 +468,6 @@ int TerminalStrip::physicalTerminalCount() const { return m_physical_terminals.size(); } -/** - * @brief TerminalStrip::realTerminalCount - * @return the number of real terminal. - * A real terminal is a part of a physical terminal. - */ -int TerminalStrip::realTerminalCount() const -{ - return m_real_terminals.size(); -} - TerminalStripIndex TerminalStrip::index(int index) { TerminalStripIndex tsi_; @@ -484,6 +490,29 @@ TerminalStripIndex TerminalStrip::index(int index) return tsi_; } +/** + * @brief TerminalStrip::physicalTerminalData + * @param index + * @return The data of the physical terminal at index \p index + */ +PhysicalTerminalData TerminalStrip::physicalTerminalData(int index) +{ + PhysicalTerminalData ptd; + + if (index < m_physical_terminals.size()) + { + auto physical_terminal = m_physical_terminals.at(index); + ptd.physical_terminal = physical_terminal; + ptd.pos_ = index; + for (auto real_terminal : physical_terminal->terminals()) { + auto rtd = realTerminalData(real_terminal); + ptd.real_terminals_vector.append(rtd); + } + } + + return ptd; +} + /** * @brief TerminalStrip::terminalElement * @return A vector of all terminal element owned by this strip @@ -566,31 +595,6 @@ bool TerminalStrip::fromXml(QDomElement &xml_element) return true; } -RealTerminalData TerminalStrip::realTerminalData(int real_terminal_index) -{ - RealTerminalData rtd; - if (m_real_terminals.isEmpty() || - real_terminal_index >= m_real_terminals.size()) { - return rtd; - } - - auto real_terminal = m_real_terminals.at(real_terminal_index); - auto physical_terminal = physicalTerminal(real_terminal); - - rtd.m_real_terminal = m_real_terminals.at(real_terminal_index); - rtd.pos_ = m_physical_terminals.indexOf(physical_terminal); - rtd.level_ = physical_terminal->levelOf(real_terminal); - rtd.label_ = real_terminal->label(); - - if (real_terminal->isElement()) { - rtd.Xref_ = autonum::AssignVariables::genericXref(real_terminal->element()); - } - rtd.type_ = real_terminal->type(); - rtd.is_element = real_terminal->isElement(); - - return rtd; -} - /** * @brief TerminalStrip::realTerminal * @param terminal @@ -632,6 +636,36 @@ QSharedPointer TerminalStrip::physicalTerminal(QSharedPointer< return pt; } +/** + * @brief TerminalStrip::elementForRealTerminal + * @param rt + * @return the element associated to \p rt, the returned element can be nullptr; + */ +Element *TerminalStrip::elementForRealTerminal(QSharedPointer rt) const { + return rt.data()->element(); +} + +RealTerminalData TerminalStrip::realTerminalData(QSharedPointer real_terminal) +{ + RealTerminalData rtd; + + auto physical_terminal = physicalTerminal(real_terminal); + + rtd.m_real_terminal = real_terminal; + rtd.level_ = physical_terminal->levelOf(real_terminal); + rtd.label_ = real_terminal->label(); + + if (real_terminal->isElement()) { + rtd.Xref_ = autonum::AssignVariables::genericXref(real_terminal->element()); + } + rtd.type_ = real_terminal->type(); + rtd.function_ = real_terminal->function(); + rtd.led_ = real_terminal->led(); + rtd.is_element = real_terminal->isElement(); + + return rtd; +} + /************************************************************************************/ /************************************************************************************/ /************************************************************************************/ diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 61ae60fa4..94e55de8b 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -31,12 +31,12 @@ class TerminalStripIndex; class TerminalElement; + struct RealTerminalData { QSharedPointer m_real_terminal; - int pos_ = 0, - level_ = 0; + int level_ = 0; QString label_, Xref_, @@ -45,9 +45,18 @@ struct RealTerminalData conductor_; ElementData::TerminalType type_; + ElementData::TerminalFunction function_; bool led_ = false, is_element = false; + +}; + +struct PhysicalTerminalData +{ + QVector real_terminals_vector; + int pos_ = -1; + QSharedPointer physical_terminal; }; /** @@ -90,9 +99,9 @@ class TerminalStrip : public QObject bool haveTerminal (Element *terminal); int physicalTerminalCount() const; - int realTerminalCount() const; TerminalStripIndex index(int index = 0); - RealTerminalData realTerminalData(int real_terminal_index); + + PhysicalTerminalData physicalTerminalData(int index); QVector> terminalElement() const; @@ -100,9 +109,12 @@ class TerminalStrip : public QObject QDomElement toXml(QDomDocument &parent_document); bool fromXml(QDomElement &xml_element); + Element *elementForRealTerminal(QSharedPointer rt) const; + private: QSharedPointer realTerminal(Element *terminal); QSharedPointer physicalTerminal(QSharedPointer terminal); + RealTerminalData realTerminalData(QSharedPointer real_terminal); private: TerminalStripData m_data; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index b83e8c8a4..ef80ff185 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -25,9 +25,11 @@ #include "../UndoCommand/addterminalstripcommand.h" #include "../UndoCommand/addterminaltostripcommand.h" #include "../UndoCommand/changeterminalstripdata.h" +#include "../undocommand/changeelementdatacommand.h" #include "terminalstriptreewidget.h" #include "../../qeticons.h" #include "terminalstripmodel.h" +#include "../diagram.h" #include @@ -42,10 +44,34 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : m_project(project) { ui->setupUi(this); + ui->m_table_widget->setItemDelegate(new TerminalStripModelDelegate(ui->m_terminal_strip_tw)); ui->m_remove_terminal_strip_pb->setDisabled(true); buildTree(); ui->m_terminal_strip_tw->expandRecursively(ui->m_terminal_strip_tw->rootIndex()); setUpUndoConnections(); + + //Go the diagram of double clicked terminal + connect(ui->m_table_widget, &QAbstractItemView::doubleClicked, [this](auto index) + { + Element *elmt = nullptr; + if (this->m_model->isXrefCell(index, &elmt)) + { + 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().first()->fitInView(fit_view, Qt::KeepAspectRatioByExpanding); + } + } + } + }); + connect(ui->m_table_widget, &QAbstractItemView::entered, [this](auto index) { + qDebug() <<"entered"; + }); } /** @@ -385,20 +411,46 @@ void TerminalStripEditor::on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetI setCurrentStrip(strip_); } -void TerminalStripEditor::on_m_apply_data_pb_clicked(QAbstractButton *button) +void TerminalStripEditor::on_m_dialog_button_box_clicked(QAbstractButton *button) { Q_UNUSED(button) - if (m_current_strip) - { - TerminalStripData data; - data.m_installation = ui->m_installation_le->text(); - data.m_location = ui->m_location_le->text(); - data.m_name = ui->m_name_le->text(); - data.m_comment = ui->m_comment_le->text(); - data.m_description = ui->m_description_te->toPlainText(); + auto role = ui->m_dialog_button_box->buttonRole(button); - m_project->undoStack()->push(new ChangeTerminalStripData(m_current_strip, data, nullptr)); + if (role == QDialogButtonBox::ApplyRole) + { + if (m_current_strip) + { + m_project->undoStack()->beginMacro(tr("Modifier des propriétés de borniers")); + + TerminalStripData data; + data.m_installation = ui->m_installation_le->text(); + data.m_location = ui->m_location_le->text(); + data.m_name = ui->m_name_le->text(); + data.m_comment = ui->m_comment_le->text(); + data.m_description = ui->m_description_te->toPlainText(); + + m_project->undoStack()->push(new ChangeTerminalStripData(m_current_strip, data, nullptr)); + + if (m_model) + { + for (auto modified_data : m_model->modifiedRealTerminalData()) + { + auto element = m_current_strip->elementForRealTerminal(modified_data.m_real_terminal); + if (element) { + auto current_data = element->elementData(); + current_data.setTerminalType(modified_data.type_); + current_data.setTerminalFunction(modified_data.function_); + current_data.setTerminalLED(modified_data.led_); + current_data.m_informations.addValue(QStringLiteral("label"), modified_data.label_); + + m_project->undoStack()->push(new ChangeElementDataCommand(element, current_data)); + } + } + } + + m_project->undoStack()->endMacro(); + } } on_m_reload_pb_clicked(); diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 6de89291b..375084a1f 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -56,7 +56,7 @@ class TerminalStripEditor : public QDialog void on_m_remove_terminal_strip_pb_clicked(); void on_m_reload_pb_clicked(); void on_m_terminal_strip_tw_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous); - void on_m_apply_data_pb_clicked(QAbstractButton *button); + void on_m_dialog_button_box_clicked(QAbstractButton *button); private: Ui::TerminalStripEditor *ui; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index 34a789b84..da0d41634 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -6,15 +6,15 @@ 0 0 - 951 - 491 + 1260 + 579 Gestionnaire de borniers - - + + @@ -78,17 +78,11 @@ - + Qt::Horizontal - - 4 - - - false - @@ -114,141 +108,134 @@ - + - + 1 0 - - 0 - - - - Disposition - - - - - - - 0 - 0 - - - - true + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + 1 + 0 + + + + 0 + + + + Disposition - - false - - - - - - - - Propriétés - - - - - - Description - - - - - - - Installation : - - - - - - - Commentaire - - - - - - - - - - Qt::Horizontal - - - - - - - - - - Nom : - - - - - - - - - - - - - Localisation : - - - - - - - - - - - 0 - - - 0 - - - 0 - - - 0 - + - + + + + 0 + 0 + + + + true + + + true + + + + + + + + Propriétés + + + + + + Nom : + + + + + + + + + + Description + + + + + + + + + + + + + Commentaire + + + + + + + + + + Installation : + + + + + Qt::Horizontal - - - 40 - 20 - - - + - - - - QDialogButtonBox::Apply + + + + + + + Localisation : - - - + + + + + + QDialogButtonBox::Apply|QDialogButtonBox::Reset + + + + diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index b81ef3d8d..55d928925 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -17,7 +17,31 @@ */ #include "terminalstripmodel.h" #include "../terminalstrip.h" +#include "../../qetgraphicsitem/element.h" #include +#include +#include +#include +#include + +/** + * Some const int who describe what a column contain + */ +const int POS_CELL = 0; +const int LEVEL_CELL = 1; +const int LABEL_CELL = 2; +const int XREF_CELL = 3; +const int CABLE_CELL = 4; +const int CABLE_WIRE_CELL = 5; +const int TYPE_CELL = 6; +const int FUNCTION_CELL = 7; +const int LED_CELL = 8; +const int CONDUCTOR_CELL = 9; + +const int ROW_COUNT = 9; + +static QVector UNMODIFIED_CELL_VECTOR{false, false, false, false, false, false, false, false, false, false}; + /** * @brief TerminalStripModel::TerminalStripModel @@ -39,63 +63,336 @@ int TerminalStripModel::rowCount(const QModelIndex &parent) const return 0; } - return m_terminal_strip->realTerminalCount(); + auto count = 0; + for (const auto &ptd : m_physical_terminal_data) { + count += ptd.real_terminals_vector.size(); + } + + return count; } int TerminalStripModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) - return 9; + return ROW_COUNT; } QVariant TerminalStripModel::data(const QModelIndex &index, int role) const { - if (role != Qt::DisplayRole || - index.row() >= m_real_terminal_data.size()) { + if (index.row() >= rowCount(QModelIndex())) { return QVariant(); } - auto rtd = m_real_terminal_data.at(index.row()); - switch (index.column()) { - case 0 : return rtd.pos_; - case 1 : return rtd.level_; - case 2 : return rtd.label_; - case 3 : return rtd.Xref_; - case 4 : return rtd.cable_; - case 5 : return rtd.cable_wire_; - case 6 : return ElementData::translatedTerminalType(rtd.type_); - case 7 : return rtd.led_; - case 8 : return rtd.conductor_; - default : return QVariant(); + const auto rtd = dataAtRow(index.row()); + + + if (role == Qt::DisplayRole) + { + switch (index.column()) { + case POS_CELL : return index.row(); + case LEVEL_CELL : return rtd.level_; + case LABEL_CELL : return rtd.label_; + case XREF_CELL : return rtd.Xref_; + case CABLE_CELL : return rtd.cable_; + case CABLE_WIRE_CELL : return rtd.cable_wire_; + case TYPE_CELL : return ElementData::translatedTerminalType(rtd.type_); + case FUNCTION_CELL : return ElementData::translatedTerminalFunction(rtd.function_); + case CONDUCTOR_CELL : return rtd.conductor_; + default : return QVariant(); + } } + else if (role == Qt::EditRole) + { + switch (index.column()) { + case LABEL_CELL : return rtd.label_; + default: return QVariant(); + + } + } + else if (role == Qt::CheckStateRole && + index.column() == LED_CELL) + { + return rtd.led_ ? Qt::Checked : Qt::Unchecked; + } + else if (role == Qt::BackgroundRole && index.column() <= CONDUCTOR_CELL ) + { + if (m_modified_cell.contains(rtd.m_real_terminal) && + m_modified_cell.value(rtd.m_real_terminal).at(index.column())) + { + return QBrush(Qt::yellow); + } + } + + return QVariant(); +} + +bool TerminalStripModel::setData(const QModelIndex &index, const QVariant &value, int role) +{ + auto rtd = dataAtRow(index.row()); + bool modified_ = false; + int modified_cell = -1; + auto column_ = index.column(); + + if (column_ == LED_CELL && + role == Qt::CheckStateRole) + { + rtd.led_ = value.toBool(); + modified_ = true; + modified_cell = LED_CELL; + } + else if (column_ == TYPE_CELL && + role == Qt::EditRole) + { + rtd.type_ = value.value(); + modified_ = true; + modified_cell = TYPE_CELL; + } + else if (column_ == FUNCTION_CELL && + role == Qt::EditRole) + { + rtd.function_ = value.value(); + modified_ = true; + modified_cell = FUNCTION_CELL; + } + else if (column_ == LABEL_CELL && + role == Qt::EditRole) + { + rtd.label_ = value.toString(); + modified_ = true; + modified_cell = LABEL_CELL; + } + + //Set the modification to the terminal data + if (modified_) + { + replaceDataAtRow(rtd, index.row()); + + if (rtd.m_real_terminal) + { + QVector vector_; + if (m_modified_cell.contains(rtd.m_real_terminal)) { + vector_ = m_modified_cell.value(rtd.m_real_terminal); + } else { + vector_ = UNMODIFIED_CELL_VECTOR; + } + + vector_.replace(modified_cell, true); + m_modified_cell.insert(rtd.m_real_terminal, vector_); + } + return true; + } + + return false; } QVariant TerminalStripModel::headerData(int section, Qt::Orientation orientation, int role) const { - if (orientation != Qt::Horizontal || - role != Qt::DisplayRole) { - return QVariant(); + if (role == Qt::DisplayRole) + { + if (orientation == Qt::Horizontal) + { + switch (section) { + case POS_CELL: return tr("Position"); + case LEVEL_CELL: return tr("Étage"); + case LABEL_CELL: return tr("Label"); + case XREF_CELL: return tr("Référence croisé"); + case CABLE_CELL: return tr("Câble"); + case CABLE_WIRE_CELL: return tr("Couleur / numéro de fil câble"); + case TYPE_CELL: return tr("Type"); + case FUNCTION_CELL : return tr("Fonction"); + case LED_CELL: return tr("led"); + case CONDUCTOR_CELL: return tr("Numéro de conducteur"); + default : return QVariant(); + } + } } - switch (section) { - case 0: return tr("Position"); - case 1: return tr("Étage"); - case 2: return tr("Label"); - case 3: return tr("Référence croisé"); - case 4: return tr("Câble"); - case 5: return tr("Couleur / numéro de fil câble"); - case 6: return tr("Type"); - case 7: return tr("led"); - case 8: return tr("Numéro de conducteur"); - default : return QVariant(); + return QVariant(); +} + +Qt::ItemFlags TerminalStripModel::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 TerminalStripModel::modifiedRealTerminalData + * @return the modified real terminal data + */ +QVector TerminalStripModel::modifiedRealTerminalData() const +{ + QVector returned_vector; + + const auto modified_real_terminal = m_modified_cell.keys(); + + for (const auto &ptd : m_physical_terminal_data) { + for (const auto &rtd : ptd.real_terminals_vector) { + if (modified_real_terminal.contains(rtd.m_real_terminal)) { + returned_vector.append(rtd); + } + } + } + + return returned_vector; +} + +/** + * @brief TerminalStripModel::isXrefCell + * @param index + * @param elmt : Pointer of a pointer element + * @return true if the index is the Xref cell, if true the pointer \p element + * will be set to the element associated to the cell. + */ +bool TerminalStripModel::isXrefCell(const QModelIndex &index, Element **element) +{ + if (index.model() == this && index.isValid()) + { + if (index.column() == XREF_CELL) + { + if (index.row() < rowCount()) + { + const auto data = dataAtRow(index.row()); + *element = m_terminal_strip->elementForRealTerminal(data.m_real_terminal); + + } + return true; + } + } + + return false; } void TerminalStripModel::fillRealTerminalData() { + //Get all physical terminal if (m_terminal_strip) { - for (int i=0 ; i < m_terminal_strip->realTerminalCount() ; ++i) { - m_real_terminal_data.append(m_terminal_strip->realTerminalData(i)); + for (auto i=0 ; i < m_terminal_strip->physicalTerminalCount() ; ++i) { + m_physical_terminal_data.append(m_terminal_strip->physicalTerminalData(i)); + } + } +} + +RealTerminalData TerminalStripModel::dataAtRow(int row) const +{ + if (row > rowCount(QModelIndex())) { + return RealTerminalData(); + } + else + { + auto current_row = 0; + for (const auto &physical_data : m_physical_terminal_data) + { + for (const auto &real_data : physical_data.real_terminals_vector) + { + if (current_row == row) { + return real_data; + } else { + ++current_row; + } + } + } + } + + return RealTerminalData(); +} + +/** + * @brief TerminalStripModel::replaceDataAtRow + * Replace the data at row \p row by \p data + * @param data + * @param row + */ +void TerminalStripModel::replaceDataAtRow(RealTerminalData data, int row) +{ + if (row > rowCount(QModelIndex())) { + return; + } + else + { + auto current_row = 0; + auto current_physical = 0; + + for (const auto &physical_data : qAsConst(m_physical_terminal_data)) + { + auto current_real = 0; + for (int i=0 ; isetObjectName("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("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 TerminalStripModelDelegate::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/terminalstripmodel.h b/sources/TerminalStrip/ui/terminalstripmodel.h index 576cfa457..92afffcc9 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -21,6 +21,8 @@ #include #include #include +#include + #include "../terminalstrip.h" class TerminalStrip; @@ -34,14 +36,40 @@ class TerminalStripModel : public QAbstractTableModel virtual int rowCount (const QModelIndex &parent = QModelIndex()) const override; virtual int columnCount (const QModelIndex &parent = QModelIndex()) const override; virtual QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const override; + virtual bool setData (const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; virtual QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + virtual Qt::ItemFlags flags (const QModelIndex &index) const override; + + QVector modifiedRealTerminalData() const; + + bool isXrefCell(const QModelIndex &index, Element **element = nullptr); private: void fillRealTerminalData(); + RealTerminalData dataAtRow(int row) const; + void replaceDataAtRow(RealTerminalData data, int row); private: QPointer m_terminal_strip; - QVector m_real_terminal_data; + QVector m_physical_terminal_data; + QHash, QVector> m_modified_cell; +}; + +class TerminalStripModelDelegate : public QStyledItemDelegate +{ + Q_OBJECT + + public: + TerminalStripModelDelegate(QObject *parent = Q_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 // TERMINALSTRIPMODEL_H diff --git a/sources/properties/elementdata.cpp b/sources/properties/elementdata.cpp index 1f1f0c859..992cc61f7 100644 --- a/sources/properties/elementdata.cpp +++ b/sources/properties/elementdata.cpp @@ -122,6 +122,84 @@ QDomElement ElementData::kindInfoToXml(QDomDocument &document) return returned_elmt; } +/** + * @brief ElementData::setTerminalType + * Override the terminal type by \p t_type + * @param t_type + */ +void ElementData::setTerminalType(ElementData::TerminalType t_type) +{ + if (t_type == m_terminal_type) { + m_terminal_type_is_override = false; + } else { + m_override_terminal_type = t_type; + m_terminal_type_is_override = true; + } +} + +/** + * @brief ElementData::terminalType + * @return the terminal type or overrided terminal type if set + */ +ElementData::TerminalType ElementData::terminalType() const +{ + return m_terminal_type_is_override ? + m_override_terminal_type : + m_terminal_type; +} + +/** + * @brief ElementData::setTerminalFunction + * Override the terminal function by \p t_function + * @param t_function + */ +void ElementData::setTerminalFunction(ElementData::TerminalFunction t_function) +{ + if (t_function == m_terminal_function) { + m_terminal_function_is_override = false; + } else { + m_override_terminal_function = t_function; + m_terminal_function_is_override = true; + } +} + +/** + * @brief ElementData::terminalFunction + * @return the terminal function or overrided terminal function if set + */ +ElementData::TerminalFunction ElementData::terminalFunction() const +{ + return m_terminal_function_is_override ? + m_override_terminal_function : + m_terminal_function; +} + +/** + * @brief ElementData::setTerminalLED + * Override the terminal led by \p led + * @param led + */ +void ElementData::setTerminalLED(bool led) +{ + if (led == m_terminal_led) { + m_terminal_led_is_override = false; + } else { + m_override_terminal_led = led; + m_terminal_led_is_override = true; + } +} + +/** + * @brief ElementData::terminalLed + * @return if terminal have led or overrided led if set + */ +bool ElementData::terminalLed() const +{ + return m_terminal_led_is_override ? + m_override_terminal_led : + m_terminal_led; +} + bool ElementData::operator==(const ElementData &data) const { if (data.m_type != m_type) { @@ -141,8 +219,41 @@ bool ElementData::operator==(const ElementData &data) const } } else if (data.m_type == ElementData::Terminale) { - if (data.m_terminal_type != m_terminal_type || - data.m_terminal_function != m_terminal_function) { + //Check terminal type or overrided terminal type + if (data.m_terminal_type_is_override != m_terminal_type_is_override) { + return false; + } + + if (m_terminal_type_is_override) { + if(data.m_override_terminal_type != m_override_terminal_type) { + return false; + } + } else if (data.m_terminal_type != m_terminal_type) { + return false; + } + + //Check terminal led or override terminal led + if (data.m_terminal_led_is_override != m_terminal_led_is_override) { + return false; + } + if (m_terminal_led_is_override) { + if (data.m_override_terminal_led != m_override_terminal_led) { + return false; + } + } else if (data.m_terminal_led != m_terminal_led) { + return false; + } + + //Check terminal function or overrided terminal function + if (data.m_terminal_function_is_override != m_terminal_function_is_override) { + return false; + } + if (m_terminal_function_is_override) { + if (data.m_override_terminal_function != m_override_terminal_function) { + return false; + } + } + else if (data.m_terminal_function != m_terminal_function) { return false; } } @@ -343,15 +454,15 @@ QString ElementData::translatedTerminalType(ElementData::TerminalType type) { switch (type) { case ElementData::TTGeneric : - return QObject::tr("generique", "generic terminal element type"); + return QObject::tr("Générique", "generic terminal element type"); case ElementData::TTFuse : - return QObject::tr("fusible", "fuse terminal element type"); + return QObject::tr("Fusible", "fuse terminal element type"); case ElementData::TTSectional: - return QObject::tr("sectionable", "sectional terminal element type"); + return QObject::tr("Sectionable", "sectional terminal element type"); case ElementData::TTDiode: - return QObject::tr("diode", "diode terminal element type"); + return QObject::tr("Diode", "diode terminal element type"); case ElementData::TTGround: - return QObject::tr("terre", "ground terminal element type"); + return QObject::tr("Terre", "ground terminal element type"); } } @@ -383,6 +494,15 @@ ElementData::TerminalFunction ElementData::terminalFunctionFromString(const QStr return ElementData::TFGeneric; } +QString ElementData::translatedTerminalFunction(ElementData::TerminalFunction function) +{ + switch (function) { + case TFGeneric : return QObject::tr("Générique", "generic terminal element function"); + case TFPhase : return QObject::tr("Phase", "phase terminal element function" ); + case TFNeutral : return QObject::tr("Neutre", "neutral terminal element function"); + } +} + void ElementData::kindInfoFromXml(const QDomElement &xml_element) { if (m_type == ElementData::Master || diff --git a/sources/properties/elementdata.h b/sources/properties/elementdata.h index e0f9696ec..d2f7819ee 100644 --- a/sources/properties/elementdata.h +++ b/sources/properties/elementdata.h @@ -91,6 +91,15 @@ class ElementData : public PropertiesInterface bool fromXml(const QDomElement &xml_element) override; QDomElement kindInfoToXml(QDomDocument &document); + void setTerminalType(ElementData::TerminalType t_type); + ElementData::TerminalType terminalType() const; + + void setTerminalFunction(ElementData::TerminalFunction t_function); + ElementData::TerminalFunction terminalFunction() const; + + void setTerminalLED(bool led); + bool terminalLed() const; + bool operator==(const ElementData &data) const; bool operator!=(const ElementData &data) const; @@ -112,6 +121,7 @@ class ElementData : public PropertiesInterface static QString terminalFunctionToString(ElementData::TerminalFunction function); static ElementData::TerminalFunction terminalFunctionFromString(const QString &string); + static QString translatedTerminalFunction(ElementData::TerminalFunction function); // must be public, because this class is a private member // of Element/ element editor and they must access this data @@ -130,6 +140,17 @@ class ElementData : public PropertiesInterface NamesList m_names_list; QString m_drawing_information; + private: + ElementData::TerminalType m_override_terminal_type = ElementData::TTGeneric; + bool m_terminal_type_is_override = false; + + ElementData::TerminalFunction m_override_terminal_function = ElementData::TFGeneric; + bool m_terminal_function_is_override = false; + + bool m_terminal_led = false; + bool m_terminal_led_is_override = false; + bool m_override_terminal_led = false; + private: void kindInfoFromXml(const QDomElement &xml_element); }; diff --git a/sources/qetgraphicsitem/element.cpp b/sources/qetgraphicsitem/element.cpp index 177457cd2..0696a1bd0 100644 --- a/sources/qetgraphicsitem/element.cpp +++ b/sources/qetgraphicsitem/element.cpp @@ -1314,6 +1314,26 @@ ElementData Element::elementData() const return m_data; } +/** + * @brief Element::setElementData + * Set new data for this element. + * If m_information of \p data is changed, emit elementInfoChange + * @param data + */ +void Element::setElementData(ElementData data) +{ + auto old_info = m_data.m_informations; + m_data = data; + + if (old_info != m_data.m_informations) { + m_data.m_informations.addValue(QStringLiteral("label"), actualLabel()); //Update the label if there is a formula + if (diagram()) { + diagram()->project()->dataBase()->elementInfoChanged(this); + } + emit elementInfoChange(old_info, m_data.m_informations); + } +} + /** @brief comparPos Compare position of the two elements. Compare 3 points: diff --git a/sources/qetgraphicsitem/element.h b/sources/qetgraphicsitem/element.h index ca4d8fa69..a732569ef 100644 --- a/sources/qetgraphicsitem/element.h +++ b/sources/qetgraphicsitem/element.h @@ -105,6 +105,7 @@ class Element : public QetGraphicsItem virtual void setElementInformations(DiagramContext dc); ElementData elementData() const; + void setElementData (ElementData data); /** * @brief kindInformations diff --git a/sources/undocommand/changeelementdatacommand.cpp b/sources/undocommand/changeelementdatacommand.cpp new file mode 100644 index 000000000..ca70809f5 --- /dev/null +++ b/sources/undocommand/changeelementdatacommand.cpp @@ -0,0 +1,39 @@ +/* + 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 "changeelementdatacommand.h" +#include "../qetgraphicsitem/element.h" + +ChangeElementDataCommand::ChangeElementDataCommand(Element *element, ElementData new_data) : + m_element(element), + m_old_data(element->elementData()), + m_new_data(new_data) +{ + setText(QObject::tr("Modifier les propriétés d'un élement")); +} + +void ChangeElementDataCommand::undo() { + if (m_element) { + m_element.data()->setElementData(m_old_data); + } +} + +void ChangeElementDataCommand::redo() { + if (m_element) { + m_element.data()->setElementData(m_new_data); + } +} diff --git a/sources/undocommand/changeelementdatacommand.h b/sources/undocommand/changeelementdatacommand.h new file mode 100644 index 000000000..ecc4c590f --- /dev/null +++ b/sources/undocommand/changeelementdatacommand.h @@ -0,0 +1,38 @@ +/* + 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 CHANGEELEMENTDATACOMMAND_H +#define CHANGEELEMENTDATACOMMAND_H + +#include <../properties/elementdata.h> +#include + +class Element; + +class ChangeElementDataCommand : public QUndoCommand +{ + public: + ChangeElementDataCommand(Element *element, ElementData new_data); + void undo() override; + void redo() override; + + private: + QPointer m_element; + ElementData m_old_data, m_new_data; +}; + +#endif // CHANGEELEMENTDATACOMMAND_H