diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index 6106415e2..28d6a14c9 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -261,6 +261,16 @@ class PhysicalTerminal m_real_terminal.append(terminal); } + /** + * @brief removeTerminal + * Remove \p terminal from the list of real terminal + * @param terminal + * @return true if sucessfully removed + */ + bool removeTerminal(shared_real_terminal terminal) { + return m_real_terminal.removeOne(terminal); + } + /** * @brief levelCount * @return the number of level of this terminal @@ -561,7 +571,7 @@ bool TerminalStrip::setOrderTo(QVector sorted_vector) * @param receiver_terminal * @return true if success */ -bool TerminalStrip::groupTerminal(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals) +bool TerminalStrip::groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals) { if (!m_physical_terminals.contains(receiver_terminal.physical_terminal)) { qDebug() << "TerminalStrip::groupTerminal : Arguments terminals don't belong to this strip. Operation aborted."; @@ -592,6 +602,39 @@ bool TerminalStrip::groupTerminal(const PhysicalTerminalData &receiver_terminal, return true; } +/** + * @brief TerminalStrip::unGroupTerminals + * Ungroup all real terminals of \p terminals_to_ungroup + * from this terminal strip + * @param terminals_to_ungroup + */ +void TerminalStrip::unGroupTerminals(const QVector &terminals_to_ungroup) +{ + bool ungrouped = false; + for (const auto &rtd_ : terminals_to_ungroup) + { + if (auto real_terminal = realTerminal(rtd_.element_)) //Get the shared real terminal + { + if (auto physical_terminal = physicalTerminal(real_terminal)) //Get the physical terminal + { + if (physical_terminal->terminals().size() > 1) //Check if physical have more than one real terminal + { + physical_terminal->removeTerminal(real_terminal); + shared_physical_terminal new_physical_terminal ( + new PhysicalTerminal(this, QVector{real_terminal})); + + m_physical_terminals.append(new_physical_terminal); + ungrouped = true; + } + } + } + } + + if (ungrouped) { + emit orderChanged(); + } +} + /** * @brief TerminalStrip::terminalElement * @return A vector of all terminal element owned by this strip diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 2731f33ac..f72c95295 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -34,7 +34,7 @@ class TerminalElement; struct RealTerminalData { - int level_ = 0; + int level_ = -1; QString label_, Xref_, @@ -107,7 +107,8 @@ class TerminalStrip : public QObject PhysicalTerminalData physicalTerminalData(int index) const; QVector physicalTerminalData() const; bool setOrderTo(QVector sorted_vector); - bool groupTerminal(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals); + bool groupTerminals(const PhysicalTerminalData &receiver_terminal, const QVector &added_terminals); + void unGroupTerminals(const QVector &terminals_to_ungroup); QVector> terminalElement() const; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 170ae9552..2383a3f1e 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -49,6 +49,7 @@ TerminalStripEditor::TerminalStripEditor(QETProject *project, QWidget *parent) : ui->m_table_widget->setItemDelegate(new TerminalStripModelDelegate(ui->m_terminal_strip_tw)); ui->m_remove_terminal_strip_pb->setDisabled(true); ui->m_group_terminals_pb->setDisabled(true); + ui->m_ungroup_pb->setDisabled(true); buildTree(); ui->m_terminal_strip_tw->expandRecursively(ui->m_terminal_strip_tw->rootIndex()); setUpUndoConnections(); @@ -348,10 +349,24 @@ void TerminalStripEditor::selectionChanged() return; } - auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); - auto terminal_vector = m_model->terminalsForIndex(index_list); + const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); + const auto terminal_vector = m_model->physicalTerminalDataForIndex(index_list); ui->m_group_terminals_pb->setEnabled(terminal_vector.size() > 1 ? true : false); + + + auto it_= std::find_if(terminal_vector.constBegin(), terminal_vector.constEnd(), [](auto &data) + { + if (data.real_terminals_vector.size() >= 2) { + return true; + } else { + return false; + } + }); + + ui->m_ungroup_pb->setDisabled(it_ == terminal_vector.constEnd()); + + } /** @@ -523,12 +538,24 @@ void TerminalStripEditor::on_m_group_terminals_pb_clicked() { if (m_model && m_current_strip) { - auto ptd_vector = m_model->terminalsForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); + auto ptd_vector = m_model->physicalTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); if (ptd_vector.size() >= 2) { auto receiver_ = ptd_vector.takeFirst(); - m_current_strip->groupTerminal(receiver_, ptd_vector); + m_current_strip->groupTerminals(receiver_, ptd_vector); } } } +/** + * @brief TerminalStripEditor::on_m_ungroup_pb_clicked + */ +void TerminalStripEditor::on_m_ungroup_pb_clicked() +{ + if (m_model && m_current_strip) + { + const auto rtd_vector = m_model->realTerminalDataForIndex(ui->m_table_widget->selectionModel()->selectedIndexes()); + m_current_strip->unGroupTerminals(rtd_vector); + } +} + diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 216a65d71..d7f9cd91f 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -62,6 +62,8 @@ class TerminalStripEditor : public QDialog void on_m_auto_ordering_pb_clicked(); void on_m_group_terminals_pb_clicked(); + void on_m_ungroup_pb_clicked(); + private: Ui::TerminalStripEditor *ui; QETProject *m_project = nullptr; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.ui b/sources/TerminalStrip/ui/terminalstripeditor.ui index f81bfbbde..912f3d271 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.ui +++ b/sources/TerminalStrip/ui/terminalstripeditor.ui @@ -178,7 +178,7 @@ - + Qt::Vertical @@ -205,6 +205,13 @@ + + + + Degrouper les bornes + + + diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 96a3de070..9d318fa41 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -276,7 +276,7 @@ bool TerminalStripModel::isXrefCell(const QModelIndex &index, Element **element) * @return A vector of PhysicalTerminalData represented by index_list. * If sereval index point to the same terminal the vector have only one PhysicalTerminalData */ -QVector TerminalStripModel::terminalsForIndex(QModelIndexList index_list) const +QVector TerminalStripModel::physicalTerminalDataForIndex(QModelIndexList index_list) const { QVector vector_; if (index_list.isEmpty()) { @@ -299,6 +299,36 @@ QVector TerminalStripModel::terminalsForIndex(QModelIndexL return vector_; } +/** + * @brief TerminalStripModel::realTerminalDataForIndex + * @param index_list + * @return + */ +QVector TerminalStripModel::realTerminalDataForIndex(QModelIndexList index_list) const +{ + QVector vector_; + if (index_list.isEmpty()) { + return vector_; + } + + QSet set_; + //We use a QSet to avoid insert several time the same terminal. + for (auto index : index_list) { + if (index.isValid()) { + set_.insert(index.row()); + } + } + + for (auto i : set_) { + const auto rtd_ = realDataAtIndex(i); + if (rtd_.level_ > -1) { + vector_.append(realDataAtIndex(i)); + } + } + + return vector_; +} + void TerminalStripModel::fillRealTerminalData() { //Get all physical terminal @@ -406,6 +436,32 @@ PhysicalTerminalData TerminalStripModel::physicalDataAtIndex(int index) const } } +/** + * @brief TerminalStripModel::realDataAtIndex + * @param index + * @return the realTerminalData at index \p index. + */ +RealTerminalData TerminalStripModel::realDataAtIndex(int index) const +{ + if (m_physical_terminal_data.isEmpty()) { + return RealTerminalData(); + } + + int current_checked_index = -1; + + for (const auto & ptd_ : qAsConst(m_physical_terminal_data)) + { + for (const auto & rtd_ : qAsConst(ptd_.real_terminals_vector)) { + ++current_checked_index; + if (current_checked_index == index) { + return rtd_; + } + } + } + + return RealTerminalData(); +} + /*********************************************************** * Alittle 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 d1e86d853..b29a50fdc 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.h +++ b/sources/TerminalStrip/ui/terminalstripmodel.h @@ -43,13 +43,15 @@ class TerminalStripModel : public QAbstractTableModel QVector modifiedRealTerminalData() const; bool isXrefCell(const QModelIndex &index, Element **element = nullptr); - QVector terminalsForIndex(QModelIndexList index_list) const; + QVector physicalTerminalDataForIndex(QModelIndexList index_list) const; + QVector realTerminalDataForIndex(QModelIndexList index_list) const; private: void fillRealTerminalData(); RealTerminalData dataAtRow(int row) const; void replaceDataAtRow(RealTerminalData data, int row); PhysicalTerminalData physicalDataAtIndex(int index) const; + RealTerminalData realDataAtIndex(int index) const; private: QPointer m_terminal_strip;