QElectroTech  0.70
multipastedialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "multipastedialog.h"
19 #include "ui_multipastedialog.h"
20 #include "diagram.h"
21 #include "diagramcommands.h"
22 #include "element.h"
24 #include <QSettings>
25 
26 MultiPasteDialog::MultiPasteDialog(Diagram *diagram, QWidget *parent) :
27  QDialog(parent),
28  ui(new Ui::MultiPasteDialog),
29  m_diagram(diagram)
30 {
31  ui->setupUi(this);
32 
33  connect(ui->m_x_sb, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MultiPasteDialog::updatePreview);
34  connect(ui->m_y_sb, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MultiPasteDialog::updatePreview);
35  connect(ui->m_copy_count, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), this, &MultiPasteDialog::updatePreview);
36 
37  QRectF br;
38  for (QGraphicsItem *item : m_diagram->selectedItems())
39  br = br.united(item->mapToScene(item->boundingRect()).boundingRect());
40  m_origin = br.topLeft();
41 
42  m_document = m_diagram->toXml(false);
43  updatePreview();
44 }
45 
47 {
48  if(m_accept == false)
49  {
50  for(QGraphicsItem *item : m_pasted_content.items())
51  {
52  if(item->scene() && item->scene() == m_diagram)
53  {
54  m_diagram->removeItem(item);
55  delete item;
56  }
57  }
58  }
59 
60  delete ui;
61 }
62 
64 {
65  //First of all we remove all precedent items added from the previous preview.
66  for(QGraphicsItem *item : m_pasted_content.items())
67  {
68  if(item->scene() && item->scene() == m_diagram)
69  {
70  m_diagram->removeItem(item);
71  delete item;
72  }
73  }
75  m_pasted_content_list.clear();
76 
77  QPointF offset(ui->m_x_sb->value(), ui->m_y_sb->value());
78  QPointF pos = m_origin+offset;
79 
80  for(int i=0 ; i<ui->m_copy_count->value() ; i++)
81  {
82  DiagramContent dc;
83  m_diagram->fromXml(m_document, pos, false, &dc);
84 
85  m_pasted_content += dc;
87  pos += offset;
88  }
89 
92 }
93 
95 {
97  {
98  m_diagram->undoStack().beginMacro(tr("Multi-collage"));
99 
100  QSettings settings;
101  bool erase_label = settings.value("diagramcommands/erase-label-on-copy", true).toBool();
102  //Ensure when 'auto_num' is checked, the settings 'save_label' is to true.
103  //Because in the class PasteDiagramCommand, if the settings 'save_label' is to false,
104  //the function redo of PasteDiagramCommand, clear the formula and the label of the pasted element
105  //and so the auto_num below do nothing (there is not a formula to compare)
106  if(ui->m_auto_num_cb->isChecked())
107  settings.setValue("diagramcommands/erase-label-on-copy", false);
108 
109 
110 
111  m_diagram->clearSelection();
113 
115  {
116  QList<Element *> pasted_elements = dc.m_elements;
117  //Sort the list element by there pos (top -> bottom)
118  std::sort(pasted_elements.begin(), pasted_elements.end(), [](Element *a, Element *b){return (a->pos().y() < b->pos().y());});
119 
120  //Auto-connection
121  if(ui->m_auto_connection_cb->isChecked())
122  {
123  for(Element *elmt : pasted_elements)
124  {
125  while (!elmt->AlignedFreeTerminals().isEmpty())
126  {
127  QPair <Terminal *, Terminal *> pair = elmt->AlignedFreeTerminals().takeFirst();
128 
129  Conductor *conductor = new Conductor(pair.first, pair.second);
130  m_diagram->undoStack().push(new AddItemCommand<Conductor *>(conductor, m_diagram, QPointF()));
131 
132  //Autonum the new conductor, the undo command associated for this, have for parent undo_object
133  ConductorAutoNumerotation can (conductor, m_diagram);
134  can.numerate();
136  conductor->setFreezeLabel(true);
137  }
138  }
139  }
140  }
141 
142  //Set up the label of element
143  //Instead of use the current autonum of project,
144  //we try to fetch the same formula of the pasted element, in the several autonum of the project
145  //for apply the good formula for each elements
146  if(ui->m_auto_num_cb->isChecked())
147  {
148  for(Element *elmt : pasted_elements)
149  {
150  QString formula = elmt->elementInformations()["formula"].toString();
151  if(!formula.isEmpty())
152  {
153  QHash <QString, NumerotationContext> autonums = m_diagram->project()->elementAutoNum();
154  QHashIterator<QString, NumerotationContext> hash_iterator(autonums);
155 
156  while(hash_iterator.hasNext())
157  {
158  hash_iterator.next();
159  if(autonum::numerotationContextToFormula(hash_iterator.value()) == formula)
160  {
161  m_diagram->project()->setCurrrentElementAutonum(hash_iterator.key());
162  elmt->setUpFormula();
163  }
164  }
165  }
166  }
167  }
168  //Like elements, we compare formula of pasted conductor with the autonums available in the project.
169  if(ui->m_auto_num_cond_cb->isChecked())
170  {
171  //This list is to ensure we not numerate twice the same conductor
172  QList<Conductor *> numerated;
173  //Start with the element at top
174  for(Element *elmt : pasted_elements)
175  {
176  for(Conductor *c : elmt->conductors())
177  {
178  if(numerated.contains(c))
179  continue;
180  else
181  numerated << c;
182  QString formula = c->properties().m_formula;
183  if(!formula.isEmpty())
184  {
185  QHash <QString, NumerotationContext> autonums = m_diagram->project()->conductorAutoNum();
186  QHashIterator <QString, NumerotationContext> hash_iterator(autonums);
187 
188  while (hash_iterator.hasNext())
189  {
190  hash_iterator.next();
191  if(autonum::numerotationContextToFormula(hash_iterator.value()) == formula)
192  {
193  m_diagram->project()->setCurrentConductorAutoNum(hash_iterator.key());
194  c->rSequenceNum().clear();
196  can.numerate();
198  {
199  c->setFreezeLabel(true);
200  }
201  }
202  }
203  }
204  }
205  }
206  }
207  }
208 
210  m_accept = true;
211  settings.setValue("diagramcommands/erase-label-on-copy", erase_label);
212  m_diagram->undoStack().endMacro();
213  }
214 }
QIcon br
Definition: qeticons.cpp:185
void adjustSceneRect()
Diagram::adjustSceneRect Recalcul and adjust the size of the scene.
Definition: diagram.cpp:1597
QHash< QString, NumerotationContext > conductorAutoNum() const
QETProject::conductorAutoNum.
Definition: qetproject.cpp:494
bool isFreezeNewConductors()
QETProject::isFreezeNewConductors.
Definition: qetproject.cpp:706
void setCurrrentElementAutonum(QString autoNum)
QETProject::setCurrrentElementAutonum.
Definition: qetproject.cpp:540
void setFreezeLabel(bool freeze)
Conductor::setFreezeLabel Freeze this conductor label if true Unfreeze this conductor label if false...
Definition: conductor.cpp:1985
The AddItemCommand class This command add an item in a diagram The item to add is template...
void setCurrentConductorAutoNum(QString autoNum)
QETProject::setCurrentConductorAutoNum.
Definition: qetproject.cpp:569
QString numerotationContextToFormula(const NumerotationContext &nc)
numerotationContextToFormula
QIcon Conductor
Definition: qeticons.cpp:35
QDomDocument toXml(bool=true)
Definition: diagram.cpp:604
MultiPasteDialog(Diagram *diagram, QWidget *parent=nullptr)
QList< QGraphicsItem * > items(int=All) const
DiagramContent::items.
virtual void removeItem(QGraphicsItem *item)
Diagram::removeItem Réimplemented from QGraphicsScene::removeItem(QGraphicsItem *item) Do some specif...
Definition: diagram.cpp:1133
Ui::MultiPasteDialog * ui
void numerate()
ConductorAutoNumerotation::numerate execute the automatic numerotation.
bool freezeNewConductors()
Diagram::freezeNewConductors.
Definition: diagram.cpp:1589
QIcon tr
Definition: qeticons.cpp:204
DiagramContent m_pasted_content
bool fromXml(QDomDocument &, QPointF=QPointF(), bool=true, DiagramContent *=nullptr)
Definition: diagram.cpp:804
QUndoStack & undoStack()
Definition: diagram.h:337
QList< DiagramContent > m_pasted_content_list
void clear()
DiagramContent::clear Remove all items from the diagram content.
QHash< QString, NumerotationContext > elementAutoNum() const
QETProject::elementAutoNum.
Definition: qetproject.cpp:502
QETProject * project() const
Definition: diagram.cpp:1716
QDomDocument m_document
int count(int=All) const
DiagramContent::count.