diff --git a/sources/qetgraphicsitem/customelement.cpp b/sources/qetgraphicsitem/customelement.cpp index a7a3244c7..3376cd240 100644 --- a/sources/qetgraphicsitem/customelement.cpp +++ b/sources/qetgraphicsitem/customelement.cpp @@ -249,10 +249,19 @@ QList CustomElement::terminals() const { return(m_terminals); } -/// @return la liste des conducteurs rattaches a cet element -QList CustomElement::conductors() const { +/** + * @brief CustomElement::conductors + * @return The list of conductor docked to this element + * the list is sorted according to the position of the terminal where the conductor is docked + * from top to bottom, and left to right. + */ +QList CustomElement::conductors() const +{ QList conductors; - foreach(Terminal *t, m_terminals) conductors << t -> conductors(); + + for(Terminal *t : m_terminals) + conductors << t -> conductors(); + return(conductors); } @@ -803,6 +812,16 @@ Terminal *CustomElement::parseTerminal(QDomElement &e) { Terminal *new_terminal = new Terminal(terminalx, terminaly, terminalo, this); new_terminal -> setZValue(420); // valeur arbitraire pour maintenir les bornes au-dessus des champs de texte m_terminals << new_terminal; + + //Sort from top to bottom and left to rigth + std::sort(m_terminals.begin(), m_terminals.end(), [](Terminal *a, Terminal *b) + { + if(a->dockConductor().y() == b->dockConductor().y()) + return (a->dockConductor().x() < b->dockConductor().x()); + else + return (a->dockConductor().y() < b->dockConductor().y()); + }); + return(new_terminal); } diff --git a/sources/ui/multipastedialog.cpp b/sources/ui/multipastedialog.cpp index a1e82b6af..082a1f283 100644 --- a/sources/ui/multipastedialog.cpp +++ b/sources/ui/multipastedialog.cpp @@ -72,6 +72,7 @@ void MultiPasteDialog::updatePreview() } } m_pasted_content.clear(); + m_pasted_content_list.clear(); QPointF offset(ui->m_x_sb->value(), ui->m_y_sb->value()); QPointF pos = m_origin+offset; @@ -82,6 +83,7 @@ void MultiPasteDialog::updatePreview() m_diagram->fromXml(m_document, pos, false, &dc); m_pasted_content += dc; + m_pasted_content_list << dc; pos += offset; } @@ -109,77 +111,94 @@ void MultiPasteDialog::on_m_button_box_accepted() m_diagram->clearSelection(); m_diagram->undoStack().push(new PasteDiagramCommand(m_diagram, m_pasted_content)); - //Auto-connection - if(ui->m_auto_connection_cb->isChecked()) + for(DiagramContent dc : m_pasted_content_list) { - for(Element *elmt : m_pasted_content.m_elements) + QList pasted_elements = dc.m_elements; + //Sort the list element by there pos (top -> bottom) + std::sort(pasted_elements.begin(), pasted_elements.end(), [](Element *a, Element *b){return (a->pos().y() < b->pos().y());}); + + //Auto-connection + if(ui->m_auto_connection_cb->isChecked()) { - while (!elmt->AlignedFreeTerminals().isEmpty()) + for(Element *elmt : pasted_elements) { - QPair pair = elmt->AlignedFreeTerminals().takeFirst(); - - Conductor *conductor = new Conductor(pair.first, pair.second); - m_diagram->undoStack().push(new AddItemCommand(conductor, m_diagram, QPointF())); - - //Autonum the new conductor, the undo command associated for this, have for parent undo_object - ConductorAutoNumerotation can (conductor, m_diagram); - can.numerate(); - if (m_diagram->freezeNewConductors() || m_diagram->project()->isFreezeNewConductors()) { - conductor->setFreezeLabel(true); - } - } - } - } - - //Set up the label of element - //Instead of use the current autonum of project, - //we try to fetch the same formula of the pasted element, in the several autonum of the project - //for apply the good formula for each elements - if(ui->m_auto_num_cb->isChecked()) - { - for(Element *elmt : m_pasted_content.m_elements) - { - QString formula = elmt->elementInformations()["formula"].toString(); - if(!formula.isEmpty()) - { - QHash autonums = m_diagram->project()->elementAutoNum(); - QHashIterator hash_iterator(autonums); - - while(hash_iterator.hasNext()) + while (!elmt->AlignedFreeTerminals().isEmpty()) { - hash_iterator.next(); - if(autonum::numerotationContextToFormula(hash_iterator.value()) == formula) - { - m_diagram->project()->setCurrrentElementAutonum(hash_iterator.key()); - elmt->setUpFormula(); + QPair pair = elmt->AlignedFreeTerminals().takeFirst(); + + Conductor *conductor = new Conductor(pair.first, pair.second); + m_diagram->undoStack().push(new AddItemCommand(conductor, m_diagram, QPointF())); + + //Autonum the new conductor, the undo command associated for this, have for parent undo_object + ConductorAutoNumerotation can (conductor, m_diagram); + can.numerate(); + if (m_diagram->freezeNewConductors() || m_diagram->project()->isFreezeNewConductors()) { + conductor->setFreezeLabel(true); } } } } - } - //Like elements, we compare formula of pasted conductor with the autonums available in the project. - if(ui->m_auto_num_cond_cb->isChecked()) - { - for(Conductor *c : m_pasted_content.conductors()) + + //Set up the label of element + //Instead of use the current autonum of project, + //we try to fetch the same formula of the pasted element, in the several autonum of the project + //for apply the good formula for each elements + if(ui->m_auto_num_cb->isChecked()) { - QString formula = c->properties().m_formula; - if(!formula.isEmpty()) + for(Element *elmt : pasted_elements) { - QHash autonums = m_diagram->project()->conductorAutoNum(); - QHashIterator hash_iterator(autonums); - - while (hash_iterator.hasNext()) + QString formula = elmt->elementInformations()["formula"].toString(); + if(!formula.isEmpty()) { - hash_iterator.next(); - if(autonum::numerotationContextToFormula(hash_iterator.value()) == formula) + QHash autonums = m_diagram->project()->elementAutoNum(); + QHashIterator hash_iterator(autonums); + + while(hash_iterator.hasNext()) { - m_diagram->project()->setCurrentConductorAutoNum(hash_iterator.key()); - c->rSequenceNum().clear(); - ConductorAutoNumerotation can(c, m_diagram); - can.numerate(); - if (m_diagram->freezeNewConductors() || m_diagram->project()->isFreezeNewConductors()) + hash_iterator.next(); + if(autonum::numerotationContextToFormula(hash_iterator.value()) == formula) { - c->setFreezeLabel(true); + m_diagram->project()->setCurrrentElementAutonum(hash_iterator.key()); + elmt->setUpFormula(); + } + } + } + } + } + //Like elements, we compare formula of pasted conductor with the autonums available in the project. + if(ui->m_auto_num_cond_cb->isChecked()) + { + //This list is to ensure we not numerate twice the same conductor + QList numerated; + //Start with the element at top + for(Element *elmt : pasted_elements) + { + for(Conductor *c : elmt->conductors()) + { + if(numerated.contains(c)) + continue; + else + numerated << c; + QString formula = c->properties().m_formula; + if(!formula.isEmpty()) + { + QHash autonums = m_diagram->project()->conductorAutoNum(); + QHashIterator hash_iterator(autonums); + + while (hash_iterator.hasNext()) + { + hash_iterator.next(); + if(autonum::numerotationContextToFormula(hash_iterator.value()) == formula) + { + m_diagram->project()->setCurrentConductorAutoNum(hash_iterator.key()); + c->rSequenceNum().clear(); + ConductorAutoNumerotation can(c, m_diagram); + can.numerate(); + if (m_diagram->freezeNewConductors() || m_diagram->project()->isFreezeNewConductors()) + { + c->setFreezeLabel(true); + } + } } } }