mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-09-27 12:34:14 +02:00
8e2a29deaf
ConductorCreator inserts a conductor and only afterwards calls refreshText(), which resolves the auto-numbering formula into properties.text. The project database inserted its row while text was still the raw formula, and refreshText() writes the resolved text without emitting propertiesChange -- the signal the database listens for -- so nothing corrects the row. Measured: with a conductor auto-numbering "W%sequ_1" selected, two wired conductors read W1 and W2 on the live objects and in the saved file, but "W%sequ_1" and "W%sequ_1" in conductor.text and in wiring_list_view.wire_number. A full updateDB() corrects it, so the data was right and only the cache was stale. Anything that reads the database between creating a conductor and the next rebuild -- the wiring list, a BOM export, a custom query -- sees the formula, not the number. Update the row after refreshText(). The row change deliberately emits no dataBaseUpdated(), as updateConductor() already documents, so this adds no model re-queries. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
244 lines
7.1 KiB
C++
244 lines
7.1 KiB
C++
/*
|
|
Copyright 2006-2026 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#include "conductorcreator.h"
|
|
|
|
#include "../conductorautonumerotation.h"
|
|
#include "../dataBase/projectdatabase.h"
|
|
#include "../diagram.h"
|
|
#include "../qetproject.h"
|
|
#include "../undocommand/addgraphicsobjectcommand.h"
|
|
#include "../qetgraphicsitem/conductor.h"
|
|
#include "../qetgraphicsitem/element.h"
|
|
#include "../qetgraphicsitem/terminal.h"
|
|
#include "../ui/potentialselectordialog.h"
|
|
#include "qgraphicsitem.h"
|
|
|
|
#include <QPolygonF>
|
|
|
|
/**
|
|
@brief ConductorCreator::ConductorCreator
|
|
Create an electrical potential between all terminals of terminals_list.
|
|
the terminals of the list must be in the same diagram.
|
|
@param d Diagram
|
|
@param terminals_list QList<Terminal *>
|
|
*/
|
|
ConductorCreator::ConductorCreator(Diagram *d, QList<Terminal *> terminals_list) :
|
|
m_terminals_list(terminals_list)
|
|
{
|
|
if (m_terminals_list.size() <= 1) {
|
|
return;
|
|
}
|
|
m_properties = m_terminals_list.first()->diagram()->defaultConductorProperties;
|
|
|
|
if (!setUpPropertieToUse()) {
|
|
return;
|
|
}
|
|
Terminal *hub_terminal = hubTerminal();
|
|
|
|
d->undoStack().beginMacro(QObject::tr("Création de conducteurs"));
|
|
|
|
QList<Conductor *> c_list;
|
|
for (Terminal *t : m_terminals_list)
|
|
{
|
|
if (t == hub_terminal) {
|
|
continue;
|
|
}
|
|
|
|
Conductor *cond = new Conductor(hub_terminal, t);
|
|
cond->setProperties(m_properties);
|
|
cond->setSequenceNum(m_sequential_number);
|
|
d->undoStack().push(new AddGraphicsObjectCommand(cond, d));
|
|
|
|
c_list.append(cond);
|
|
}
|
|
d->undoStack().endMacro();
|
|
|
|
for(Conductor *c : c_list) {
|
|
c->refreshText();
|
|
//refreshText() resolves an auto-numbering formula into
|
|
//properties.text without emitting propertiesChange, which is
|
|
//what the project database listens to. The row was inserted
|
|
//while text was still the raw formula ("W%sequ_1"), so without
|
|
//this the wiring list and BOM read the formula, not "W1",
|
|
//until something forces a full rebuild.
|
|
if (d->project() && d->project()->dataBase()) {
|
|
d->project()->dataBase()->updateConductor(c);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
@brief ConductorCreator::create
|
|
Create an electrical potential between the terminals of the diagram d, contained in the polygon
|
|
@param d Diagram
|
|
@param polygon : polygon in diagram coordinate
|
|
*/
|
|
void ConductorCreator::create(Diagram *d, const QPolygonF &polygon)
|
|
{
|
|
QList<Terminal *> t_list;
|
|
|
|
for (QGraphicsItem *item : d->items(polygon))
|
|
{
|
|
if (item->type() == Terminal::Type) {
|
|
t_list.append(qgraphicsitem_cast<Terminal *>(item));
|
|
}
|
|
}
|
|
|
|
if (t_list.size() <= 1) {
|
|
return;
|
|
} else {
|
|
ConductorCreator cc(d, t_list);
|
|
}
|
|
}
|
|
|
|
/**
|
|
@brief ConductorCreator::needsPotentialChoice
|
|
Whether creating a potential between these terminals would ask the user
|
|
to choose which of several existing potentials to inherit from -- that
|
|
is, whether the constructor would reach PotentialSelectorDialog.
|
|
|
|
This exists for callers with nobody there to answer: the dialog is a
|
|
plain QDialog::exec(), not routed through QET::QetMessageBox, so its
|
|
non-interactive mode does not cover it and a headless caller would hang
|
|
on it indefinitely. Such a caller can check this first and decline.
|
|
Exposed here, rather than reimplemented by the caller, so the condition
|
|
cannot drift away from the one setUpPropertieToUse() actually applies.
|
|
@param terminals_list the terminals a potential would be created between
|
|
@return true if the constructor would open the dialog
|
|
*/
|
|
bool ConductorCreator::needsPotentialChoice(const QList<Terminal *> &terminals_list)
|
|
{
|
|
if (terminals_list.size() <= 1) {
|
|
return false;
|
|
}
|
|
return existingPotential(terminals_list).size() >= 2;
|
|
}
|
|
|
|
/**
|
|
@brief ConductorCreator::propertieToUse
|
|
@return true if the caller should proceed with conductor creation,
|
|
false if the user cancelled the potential-selection dialog (in which
|
|
case no properties were chosen and creation must be aborted rather
|
|
than proceeding with blank/default properties).
|
|
*/
|
|
bool ConductorCreator::setUpPropertieToUse()
|
|
{
|
|
QList<Conductor *> potentials = existingPotential(m_terminals_list);
|
|
|
|
//There is an existing potential
|
|
//we get one of them
|
|
if (!potentials.isEmpty())
|
|
{
|
|
if (potentials.size() >= 2)
|
|
{
|
|
QList <ConductorProperties> cp_list;
|
|
for(Conductor *c : potentials) {
|
|
cp_list.append(c->properties());
|
|
}
|
|
|
|
bool cancelled = false;
|
|
m_properties = PotentialSelectorDialog::chosenProperties(cp_list, nullptr, &cancelled);
|
|
if (cancelled) {
|
|
return false;
|
|
}
|
|
for (Conductor *c : potentials) {
|
|
if (c->properties() == m_properties) {
|
|
m_sequential_number = c->sequenceNum();
|
|
}
|
|
}
|
|
}
|
|
else if (potentials.size() == 1)
|
|
{
|
|
m_properties = potentials.first()->properties();
|
|
m_sequential_number = potentials.first()->sequenceNum();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//get a new properties
|
|
ConductorAutoNumerotation::newProperties(m_terminals_list.first()->diagram(), m_properties, m_sequential_number);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
@brief ConductorCreator::existingPotential
|
|
Return the list of existing potential of
|
|
the terminal list
|
|
@param terminals_list the terminals to inspect
|
|
@return c_list QList<Conductor *>
|
|
*/
|
|
QList<Conductor *> ConductorCreator::existingPotential(const QList<Terminal *> &terminals_list)
|
|
{
|
|
QList<Conductor *> c_list;
|
|
QList<Terminal *> t_exclude;
|
|
|
|
for (Terminal *t : terminals_list)
|
|
{
|
|
if (t_exclude.contains(t)) {
|
|
continue;
|
|
}
|
|
|
|
if (!t->conductors().isEmpty())
|
|
{
|
|
c_list.append(t->conductors().first());
|
|
|
|
//We must check if m_terminals_list contains a terminal
|
|
//in the same potential of c, and if true, exclude this terminal from the search.
|
|
for (Conductor *c : t->conductors().first()->relatedPotentialConductors(false))
|
|
{
|
|
if (terminals_list.contains(c->terminal1)) {
|
|
t_exclude.append(c->terminal1);
|
|
} else if (terminals_list.contains(c->terminal2)) {
|
|
t_exclude.append(c->terminal2);
|
|
}
|
|
}
|
|
}
|
|
else if (t->parentElement()->linkType() & Element::AllReport && !t->parentElement()->isFree())
|
|
{
|
|
Element *linked_report = t->parentElement()->linkedElements().first();
|
|
if (!linked_report->conductors().isEmpty()) {
|
|
c_list.append(linked_report->conductors().first());
|
|
}
|
|
}
|
|
}
|
|
|
|
return c_list;
|
|
}
|
|
|
|
/**
|
|
@brief ConductorCreator::hubTerminal
|
|
@return hub_terminal
|
|
*/
|
|
Terminal *ConductorCreator::hubTerminal()
|
|
{
|
|
Terminal *hub_terminal = m_terminals_list.first();
|
|
|
|
for (Terminal *tt : m_terminals_list)
|
|
{
|
|
if (tt->scenePos().x() < hub_terminal->scenePos().x()) {
|
|
hub_terminal = tt;
|
|
} else if (tt->scenePos().x() == hub_terminal->scenePos().x()) {
|
|
if (tt->scenePos().y() < hub_terminal->scenePos().y()) {
|
|
hub_terminal = tt;
|
|
}
|
|
}
|
|
}
|
|
|
|
return hub_terminal;
|
|
}
|