diff --git a/sources/TerminalStrip/realterminal.cpp b/sources/TerminalStrip/realterminal.cpp index 4ad4bcd..c7f4827 100644 --- a/sources/TerminalStrip/realterminal.cpp +++ b/sources/TerminalStrip/realterminal.cpp @@ -21,6 +21,50 @@ #include "physicalterminal.h" #include "terminalcolor.h" #include "../qetgraphicsitem/conductor.h" +#include "../qetgraphicsitem/terminal.h" +#include "../diagram.h" +#include "../cable/cable.h" +#include "../cable/cablemanager.h" + +namespace +{ + /** + * The cable cores wired to one terminal element, worked out from + * the cables of the project (PR #1438). A core knows its + * conductor, and CableManager::refreshLabels() writes the cable + * and the core into the properties of that conductor, so reading + * the conductors of the element is enough. + * Each pair is (cable designation, core colour or number). + */ + QList> wiredCores(Element *element) + { + QList> cores; + if (!element || !element->diagram()) { + return cores; + } + const auto project = element->diagram()->project(); + for (const auto terminal : element->terminals()) + { + for (const auto conductor : terminal->conductors()) + { + const auto properties = conductor->ownProperties(); + const auto cable = CableManager::cableByUuid(project, properties.m_cable_uuid); + if (!cable || cable->designation().isEmpty()) { + continue; + } + auto wire = cable->colorOfCore(properties.m_cable_slot); + if (wire.isEmpty() && properties.m_cable_slot >= 0) { + wire = QString::number(properties.m_cable_slot + 1); + } + const QPair core(cable->designation(), wire); + if (!cores.contains(core)) { + cores.append(core); + } + } + } + return cores; + } +} /** * @brief RealTerminal @@ -161,18 +205,52 @@ QString RealTerminal::Xref() const * @return The name of the cable (hose) connected to this terminal. * The value is stored in the informations of the element. */ -QString RealTerminal::cable() const { +QString RealTerminal::cable() const +{ + const auto cores = wiredCores(m_element.data()); + if (!cores.isEmpty()) + { + QStringList cables; + for (const auto &core : cores) { + if (!cables.contains(core.first)) { + cables.append(core.first); + } + } + return cables.join(QStringLiteral(", ")); + } + if (m_element) { return m_element->elementData().m_informations.value(cableInfoKey()).toString(); } return QString(); } +/** + * @brief RealTerminal::cableIsAutomatic + * @return true when the cable and the core of this terminal come from + * a cable of the project wired to it, false when they are the values + * typed in by the user in the terminal strip editor. + */ +bool RealTerminal::cableIsAutomatic() const { + return !wiredCores(m_element.data()).isEmpty(); +} + /** * @brief RealTerminal::cableWire * @return The color / number of the wire of the cable, connected to this terminal. */ -QString RealTerminal::cableWire() const { +QString RealTerminal::cableWire() const +{ + const auto cores = wiredCores(m_element.data()); + if (!cores.isEmpty()) + { + QStringList wires; + for (const auto &core : cores) { + wires.append(core.second); + } + return wires.join(QStringLiteral(", ")); + } + if (m_element) { return m_element->elementData().m_informations.value(cableWireInfoKey()).toString(); } diff --git a/sources/TerminalStrip/realterminal.h b/sources/TerminalStrip/realterminal.h index b149694..0668f49 100644 --- a/sources/TerminalStrip/realterminal.h +++ b/sources/TerminalStrip/realterminal.h @@ -66,6 +66,7 @@ class RealTerminal QString Xref() const; QString cable() const; QString cableWire() const; + bool cableIsAutomatic() const; bool isShield() const; QColor color() const; QColor stripeColor() const; diff --git a/sources/TerminalStrip/ui/modelTerminalData.h b/sources/TerminalStrip/ui/modelTerminalData.h index 97ad958..b289c36 100644 --- a/sources/TerminalStrip/ui/modelTerminalData.h +++ b/sources/TerminalStrip/ui/modelTerminalData.h @@ -35,6 +35,7 @@ struct modelRealTerminalData mrtd.Xref_ = real_t->Xref(); mrtd.cable_ = real_t->cable(); mrtd.cable_wire = real_t->cableWire(); + mrtd.cable_auto_ = real_t->cableIsAutomatic(); mrtd.conductor_ = real_t->conductor(); mrtd.led_ = real_t->isLed(); mrtd.shield_ = real_t->isShield(); @@ -57,6 +58,7 @@ struct modelRealTerminalData QString Xref_; QString cable_; QString cable_wire; + bool cable_auto_ = false; //True when cable_ and cable_wire come from the cables of the project QString conductor_; bool led_ = false; bool shield_ = false; diff --git a/sources/TerminalStrip/ui/terminalstripeditor.cpp b/sources/TerminalStrip/ui/terminalstripeditor.cpp index ceafd9c..46f67b3 100644 --- a/sources/TerminalStrip/ui/terminalstripeditor.cpp +++ b/sources/TerminalStrip/ui/terminalstripeditor.cpp @@ -215,8 +215,13 @@ void TerminalStripEditor::apply() current_data.m_informations.addValue(key, value); } }; - set_info(RealTerminal::cableInfoKey(), data_.cable_); - set_info(RealTerminal::cableWireInfoKey(), data_.cable_wire); + //A cable worked out from the cables of the project is + //never written in the element: it would stay there, + //out of date, once the cable is renamed or removed + if (!data_.cable_auto_) { + set_info(RealTerminal::cableInfoKey(), data_.cable_); + set_info(RealTerminal::cableWireInfoKey(), data_.cable_wire); + } set_info(RealTerminal::shieldInfoKey(), data_.shield_ ? QStringLiteral("true") : QString()); //The color and the section are stored with the dot as decimal separator, whatever the locale set_info(RealTerminal::colorInfoKey(), TerminalColor::toSpec(data_.color_, data_.stripe_color_)); diff --git a/sources/TerminalStrip/ui/terminalstripmodel.cpp b/sources/TerminalStrip/ui/terminalstripmodel.cpp index 0296472..c92aebc 100644 --- a/sources/TerminalStrip/ui/terminalstripmodel.cpp +++ b/sources/TerminalStrip/ui/terminalstripmodel.cpp @@ -430,8 +430,12 @@ Qt::ItemFlags TerminalStripModel::flags(const QModelIndex &index) const Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; auto c = index.column(); + //The cable cells are only edited by hand when no cable + //of the project is wired to the terminal + if ((c == CABLE_CELL || c == CABLE_WIRE_CELL) + && !dataAtRow(index.row()).cable_auto_) + flags = flags | Qt::ItemIsEditable; if (c == LABEL_CELL || c == TYPE_CELL || c == FUNCTION_CELL - || c == CABLE_CELL || c == CABLE_WIRE_CELL || c == COLOR_CELL || c == SECTION_CELL || c == WIDTH_CELL) flags = flags | Qt::ItemIsEditable; if (c == LED_CELL || c == SHIELD_CELL) {