diff --git a/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp new file mode 100644 index 000000000..17507f159 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.cpp @@ -0,0 +1,51 @@ +/* + 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 "changeterminalstripcolor.h" +#include "../terminalstripbridge.h" + +/** + * @brief ChangeTerminalStripColor::ChangeTerminalStripColor + * @param bridge + * @param color + * @param parent + */ +ChangeTerminalStripColor::ChangeTerminalStripColor(QSharedPointer bridge, + const QColor &color, + QUndoCommand *parent): + QUndoCommand(parent), + m_bridge(bridge), + m_new_color(color) +{ + if (m_bridge) { + m_old_color = m_bridge->color(); + } +} + +void ChangeTerminalStripColor::redo() +{ + if (m_bridge) { + m_bridge->setColor(m_new_color); + } +} + +void ChangeTerminalStripColor::undo() +{ + if (m_bridge) { + m_bridge->setColor(m_old_color); + } +} diff --git a/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h new file mode 100644 index 000000000..bb7b372a5 --- /dev/null +++ b/sources/TerminalStrip/UndoCommand/changeterminalstripcolor.h @@ -0,0 +1,45 @@ +/* + 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 CHANGETERMINALSTRIPCOLOR_H +#define CHANGETERMINALSTRIPCOLOR_H + +#include +#include +#include + +class TerminalStripBridge; + +/** + * @brief The ChangeTerminalStripColor class + */ +class ChangeTerminalStripColor : public QUndoCommand +{ + public: + ChangeTerminalStripColor(QSharedPointer bridge, + const QColor &color, + QUndoCommand *parent = nullptr); + + void redo() override; + void undo() override; + + private: + QSharedPointer m_bridge; + QColor m_old_color, m_new_color; +}; + +#endif // CHANGETERMINALSTRIPCOLOR_H diff --git a/sources/TerminalStrip/terminalstrip.cpp b/sources/TerminalStrip/terminalstrip.cpp index e913dfb7a..04898f92f 100644 --- a/sources/TerminalStrip/terminalstrip.cpp +++ b/sources/TerminalStrip/terminalstrip.cpp @@ -484,7 +484,8 @@ bool TerminalStrip::setBridge(const QVector> &real_ auto bridge = bridgeFor(real_terminals); if (bridge.isNull()) { - bridge = QSharedPointer(new TerminalStripBridge(this)); + auto br_ = new TerminalStripBridge(this); + bridge = br_->sharedRef(); m_bridge.append(bridge); } diff --git a/sources/TerminalStrip/terminalstrip.h b/sources/TerminalStrip/terminalstrip.h index 5837ac7f4..eae42117f 100644 --- a/sources/TerminalStrip/terminalstrip.h +++ b/sources/TerminalStrip/terminalstrip.h @@ -52,6 +52,7 @@ class TerminalStrip : public QObject signals: void orderChanged(); //Emitted when the order of the physical terminal is changed void bridgeChanged(); + void bridgeColorChanged(QSharedPointer bridge); public: TerminalStrip(QETProject *project); diff --git a/sources/TerminalStrip/terminalstripbridge.cpp b/sources/TerminalStrip/terminalstripbridge.cpp index c6c87a029..d810ad85d 100644 --- a/sources/TerminalStrip/terminalstripbridge.cpp +++ b/sources/TerminalStrip/terminalstripbridge.cpp @@ -23,6 +23,30 @@ TerminalStripBridge::TerminalStripBridge(TerminalStrip *parent_strip) : m_strip(parent_strip) {} +/** + * @brief TerminalStripBridge::sharedRef + * @return a QSharedPointer of this + */ +QSharedPointer TerminalStripBridge::sharedRef() +{ + QSharedPointer this_shared(this->weakRef()); + if (this_shared.isNull()) + { + this_shared = QSharedPointer(this); + m_this_weak = this_shared.toWeakRef(); + } + + return this_shared; +} + +/** + * @brief TerminalStripBridge::weakRef + * @return a QWeakPointer of this, weak pointer can be null + */ +QWeakPointer TerminalStripBridge::weakRef() { + return m_this_weak; +} + /** * @brief TerminalStripBridge::color * @return The color of this bridge @@ -31,6 +55,13 @@ QColor TerminalStripBridge::color() const { return m_color; } +void TerminalStripBridge::setColor(const QColor &color) { + m_color = color; + if (m_strip) { + m_strip->bridgeColorChanged(sharedRef()); + } +} + /** * @brief TerminalStripBridge::realTerminals * @return the real terminals who are bridged by this bridge diff --git a/sources/TerminalStrip/terminalstripbridge.h b/sources/TerminalStrip/terminalstripbridge.h index 715bd7755..082e49fb0 100644 --- a/sources/TerminalStrip/terminalstripbridge.h +++ b/sources/TerminalStrip/terminalstripbridge.h @@ -28,13 +28,20 @@ class TerminalStrip; class TerminalStripBridge { + friend class TerminalStrip; + public: static QVector bridgeColor() {return QVector{Qt::red, Qt::blue, Qt::white, Qt::darkGray, Qt::black};} TerminalStripBridge(TerminalStrip *parent_strip = nullptr); + QSharedPointer sharedRef(); + QWeakPointer weakRef(); QColor color() const; + void setColor(const QColor &color); QVector> realTerminals() const; + + private: bool addTerminals(const QVector> &real_terminals); void removeTerminals(const QVector> &real_terminals); @@ -44,6 +51,7 @@ class TerminalStripBridge QVector> m_real_terminals; QColor m_color = Qt::darkGray; QUuid m_uuid = QUuid::createUuid(); + QWeakPointer m_this_weak; }; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index 7de7b958f..1b226c410 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -1,4 +1,4 @@ -/* +/* Copyright 2006-2021 The QElectroTech Team This file is part of QElectroTech. @@ -34,6 +34,7 @@ #include "../UndoCommand/groupterminalscommand.h" #include "../UndoCommand/changeterminallevel.h" #include "../UndoCommand/bridgeterminalscommand.h" +#include "../UndoCommand/changeterminalstripcolor.h" #include "../physicalterminal.h" #include "../realterminal.h" #include "../terminalstripbridge.h" @@ -429,20 +430,21 @@ void TerminalStripEditor::selectionChanged() //Enable/disable bridge and unbridge bool enable_bridge = false; bool enable_unbridge = false; + bool enable_bridge_color = false; //One column must be selected and the column must be a level column int level_ = TerminalStripModel::levelForColumn(isSingleColumnSelected()); if (level_ >= 0 && m_current_strip) { //Select only terminals of corresponding level cell selection - QVector model_real_terminal_level_vector; QVector> real_terminal_in_level_vector; for (const auto &mrtd : model_real_terminal_vector) { - if (mrtd.level_ == level_) - { - model_real_terminal_level_vector.append(mrtd); + if (mrtd.level_ == level_) { real_terminal_in_level_vector.append(mrtd.real_terminal.toStrongRef()); + if (!enable_bridge_color && mrtd.bridged_) { + enable_bridge_color = true; + } } } enable_bridge = m_current_strip->isBridgeable(real_terminal_in_level_vector); @@ -450,6 +452,7 @@ void TerminalStripEditor::selectionChanged() } ui->m_bridge_terminals_pb->setEnabled(enable_bridge); ui->m_unbridge_terminals_pb->setEnabled(enable_unbridge); + ui->m_bridge_color_cb->setEnabled(enable_bridge_color); } QSize TerminalStripEditor::setUpBridgeCellWidth() @@ -476,7 +479,7 @@ QSize TerminalStripEditor::setUpBridgeCellWidth() * If all current QModelIndex are in the same column * return the column type * @sa TerminalStripModel::Column - * @return + * @return the column or TerminalStripModel::Invalid if several column are selected */ TerminalStripModel::Column TerminalStripEditor::isSingleColumnSelected() const { @@ -501,6 +504,29 @@ TerminalStripModel::Column TerminalStripEditor::isSingleColumnSelected() const return TerminalStripModel::Invalid; } +/** + * @brief TerminalStripEditor::singleColumnData + * @return a QPair with for first value the column and for second value the data + * of selected cell of the table widget, only if the selected cells are + * in the same column. If selected cells are not in the same column the first value + * of the QPair is TerminalStripModel::Invalid. + */ +QPair > TerminalStripEditor::singleColumnData() const +{ + if (m_current_strip) + { + auto level_ = isSingleColumnSelected(); + if (level_ != TerminalStripModel::Invalid) + { + const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); + const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); + return qMakePair(level_, mrtd_vector); + } + } + + return qMakePair(TerminalStripModel::Invalid, QVector()); +} + /** * @brief TerminalStripEditor::on_m_add_terminal_strip_pb_clicked * Action when user click on add terminal strip button @@ -829,7 +855,6 @@ void TerminalStripEditor::on_m_bridge_terminals_pb_clicked() const auto index_list = ui->m_table_widget->selectionModel()->selectedIndexes(); const auto mrtd_vector = m_model->modelRealTerminalDataForIndex(index_list); - QVector > match_vector; for (const auto &mrtd : mrtd_vector) { @@ -879,6 +904,22 @@ void TerminalStripEditor::on_m_unbridge_terminals_pb_clicked() void TerminalStripEditor::on_m_bridge_color_cb_activated(const QColor &col) { - + const auto data_vector = singleColumnData(); + const auto column_ = data_vector.first; + if (column_ == TerminalStripModel::Level0 || + column_ == TerminalStripModel::Level1 || + column_ == TerminalStripModel::Level2 || + column_ == TerminalStripModel::Level3) + { + const auto level_ = TerminalStripModel::levelForColumn(column_); + for (const auto &mrtd : data_vector.second) + { + if (mrtd.level_ == level_ && mrtd.bridged_) { + auto bridge_ = mrtd.real_terminal.toStrongRef()->bridge(); + m_project->undoStack()->push(new ChangeTerminalStripColor(bridge_, col)); + break; + } + } + } } diff --git a/sources/TerminalStrip/ui/terminalstripeditor.h b/sources/TerminalStrip/ui/terminalstripeditor.h index 7f73e1f17..ed7a21c7c 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.h +++ b/sources/TerminalStrip/ui/terminalstripeditor.h @@ -55,6 +55,7 @@ class TerminalStripEditor : public QDialog void selectionChanged(); QSize setUpBridgeCellWidth(); TerminalStripModel::Column isSingleColumnSelected() const; + QPair> singleColumnData() const; private slots: void on_m_add_terminal_strip_pb_clicked(); diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 83a97718c..16085fd83 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -109,6 +109,11 @@ TerminalStripModel::TerminalStripModel(TerminalStrip *terminal_strip, QObject *p m_terminal_strip(terminal_strip) { fillPhysicalTerminalData(); + + connect(terminal_strip, &TerminalStrip::bridgeColorChanged, this, [=] { + emit dataChanged(index(0, LEVEL_0_CELL), + index(rowCount(), LEVEL_3_CELL)); + }); } int TerminalStripModel::rowCount(const QModelIndex &parent) const