QElectroTech  0.70
searchandreplaceworker.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 "searchandreplaceworker.h"
19 #include "diagram.h"
22 #include "element.h"
23 #include "qetapp.h"
24 #include "independenttextitem.h"
25 #include "diagramcommands.h"
27 
28 
30 {
32 }
33 
40 void SearchAndReplaceWorker::replaceDiagram(QList<Diagram *> diagram_list)
41 {
42  if (diagram_list.isEmpty()) {
43  return;
44  }
45 
46  QETProject *project = diagram_list.first()->project();
47  for (Diagram *d : diagram_list) {
48  if (d->project() != project) {
49  return;
50  }
51  }
52 
53  QUndoStack *us = project->undoStack();
54  us->beginMacro(QObject::tr("Chercher/remplacer les propriétés de folio"));
55  for (Diagram *d : diagram_list)
56  {
57  TitleBlockProperties old_propertie = d->border_and_titleblock.exportTitleBlock();
58  TitleBlockProperties new_properties = old_propertie;
59 
60  new_properties.title = applyChange(new_properties.title, m_titleblock_properties.title);
61  new_properties.author = applyChange(new_properties.author, m_titleblock_properties.author);
62  new_properties.filename = applyChange(new_properties.filename, m_titleblock_properties.filename);
63  new_properties.plant = applyChange(new_properties.plant, m_titleblock_properties.plant);
64  new_properties.locmach = applyChange(new_properties.locmach, m_titleblock_properties.locmach);
65  new_properties.indexrev = applyChange(new_properties.indexrev, m_titleblock_properties.indexrev);
66  new_properties.folio = applyChange(new_properties.folio, m_titleblock_properties.folio);
67 
68  if (m_titleblock_properties.date.isValid())
69  {
71  new_properties.date = QDate();
72  } else {
73  new_properties.date = m_titleblock_properties.date;
74  }
75  }
76 
78 
79  if (old_propertie != new_properties) {
80  project->undoStack()->push(new ChangeTitleBlockCommand(d, old_propertie, new_properties));
81  }
82  }
83  us->endMacro();
84 }
85 
87 {
88  QList<Diagram *> list;
89  list.append(diagram);
90  replaceDiagram(list);
91 }
92 
100 void SearchAndReplaceWorker::replaceElement(QList<Element *> list)
101 {
102  if (list.isEmpty() || !list.first()->diagram()) {
103  return;
104  }
105 
106  QETProject *project_ = list.first()->diagram()->project();
107  for (Element *elmt : list)
108  {
109  if (elmt->diagram()) {
110  if (elmt->diagram()->project() != project_) {
111  return;
112  }
113  }
114  }
115 
116  project_->undoStack()->beginMacro(QObject::tr("Chercher/remplacer les propriétés d'éléments."));
117  for (Element *elmt : list)
118  {
119  //We apply change only for master, slave, and terminal element.
120  if (elmt->linkType() == Element::Master ||
121  elmt->linkType() == Element::Simple ||
122  elmt->linkType() == Element::Terminale)
123  {
124  DiagramContext old_context;
125  DiagramContext new_context = old_context = elmt->elementInformations();
126  for (QString key : QETApp::elementInfoKeys())
127  {
128  new_context.addValue(key, applyChange(old_context.value(key).toString(),
129  m_element_context.value(key).toString()));
130  }
131 
132  if (old_context != new_context)
133  {
134  ChangeElementInformationCommand *undo = new ChangeElementInformationCommand(elmt, old_context, new_context);
135  project_->undoStack()->push(undo);
136  }
137  }
138  }
139  project_->undoStack()->endMacro();
140 }
141 
143 {
144  QList<Element *>list;
145  list.append(element);
146  replaceElement(list);
147 }
148 
155 void SearchAndReplaceWorker::replaceIndiText(QList<IndependentTextItem *> list)
156 {
157  if (list.isEmpty() || !list.first()->diagram()) {
158  return;
159  }
160  QETProject *project_ = list.first()->diagram()->project();
161  for (IndependentTextItem *text : list) {
162  if (!text->diagram() ||
163  text->diagram()->project() != project_) {
164  return;
165  }
166  }
167 
168  project_->undoStack()->beginMacro(QObject::tr("Chercher/remplacer des textes independants"));
169  for (IndependentTextItem *text : list)
170  {
171  QString before = text->toPlainText();
172  text->setPlainText(m_indi_text);
173  project_->undoStack()->push(new ChangeDiagramTextCommand(text, before, m_indi_text));
174  }
175  project_->undoStack()->endMacro();
176 }
177 
179 {
180  QList<IndependentTextItem *>list;
181  list.append(text);
182  replaceIndiText(list);
183 }
184 
192 void SearchAndReplaceWorker::replaceConductor(QList<Conductor *> list)
193 {
194  if (list.isEmpty() || !list.first()->diagram()) {
195  return;
196  }
197 
198  QETProject *project_ = list.first()->diagram()->project();
199  for (Conductor *c : list) {
200  if (!c->diagram() ||
201  c->diagram()->project() != project_) {
202  return;
203  }
204  }
205 
206  project_->undoStack()->beginMacro(QObject::tr("Chercher/remplacer les propriétés de conducteurs."));
207  for (Conductor *c : list)
208  {
210 
211  if (cp != c->properties())
212  {
213  QSet <Conductor *> conductors_list = c->relatedPotentialConductors(true);
214  conductors_list << c;
215  for (Conductor *cc : conductors_list)
216  {
217  QVariant old_value, new_value;
218  old_value.setValue(cc->properties());
219  new_value.setValue(cp);
220  project_->undoStack()->push(new QPropertyUndoCommand(cc, "properties", old_value, new_value));
221  }
222  }
223  }
224  project_->undoStack()->endMacro();
225 }
226 
228 {
229  QList<Conductor *>list;
230  list.append(conductor);
231  replaceConductor(list);
232 }
233 
244 void SearchAndReplaceWorker::replaceAdvanced(QList<Diagram *> diagrams, QList<Element *> elements, QList<IndependentTextItem *> texts, QList<Conductor *> conductors)
245 {
246  QETProject *project_ = nullptr;
247 
248  //Some test to check if a least one list have one item
249  //and if all items belong to the same project
250  if (!diagrams.isEmpty()) {
251  project_ = diagrams.first()->project();
252  } else if (!elements.isEmpty() && elements.first()->diagram()) {
253  project_ = elements.first()->diagram()->project();
254  } else if (!texts.isEmpty() && texts.first()->diagram()) {
255  project_ = texts.first()->diagram()->project();
256  } else if (!conductors.isEmpty() && conductors.first()->diagram()) {
257  project_ = conductors.first()->diagram()->project();
258  } else {
259  return;
260  }
261 
262  for (Diagram *dd : diagrams) {
263  if (dd->project() != project_) {
264  return;
265  }
266  }
267  for (Element *elmt : elements) {
268  if (!elmt->diagram() || elmt->diagram()->project() != project_) {
269  return;
270  }
271  }
272  for (IndependentTextItem *text : texts) {
273  if (!text->diagram() || text->diagram()->project() != project_) {
274  return;
275  }
276  }
277  for (Conductor *cc : conductors) {
278  if (!cc->diagram() || cc->diagram()->project() != project_) {
279  return;
280  }
281  }
282  //The end of the test
283 
284  int who = m_advanced_struct.who;
285  if (who == -1) {
286  return;
287  }
288 
289  project_->undoStack()->beginMacro(QObject::tr("Rechercher / remplacer avancé"));
290  if (who == 0)
291  {
292  for (Diagram *diagram : diagrams)
293  {
294  TitleBlockProperties old_properties = diagram->border_and_titleblock.exportTitleBlock();
295  TitleBlockProperties new_properties = replaceAdvanced(diagram);
296  if (old_properties != new_properties) {
297  project_->undoStack()->push(new ChangeTitleBlockCommand(diagram, old_properties, new_properties));
298  }
299  }
300  }
301  else if (who == 1)
302  {
303  for (Element *element : elements)
304  {
305  DiagramContext old_context = element->elementInformations();
306  DiagramContext new_context = replaceAdvanced(element);
307  if (old_context != new_context) {
308  project_->undoStack()->push(new ChangeElementInformationCommand(element, old_context, new_context));
309  }
310  }
311  }
312  else if (who == 2)
313  {
314  for (Conductor *conductor : conductors)
315  {
316  ConductorProperties old_properties = conductor->properties();
317  ConductorProperties new_properties = replaceAdvanced(conductor);
318  if (old_properties != new_properties)
319  {
320  QSet <Conductor *> potential_conductors = conductor->relatedPotentialConductors(true);
321  potential_conductors << conductor;
322 
323  for (Conductor *c : potential_conductors)
324  {
325  QVariant old_value, new_value;
326  old_value.setValue(c->properties());
327  new_value.setValue(new_properties);
328  project_->undoStack()->push(new QPropertyUndoCommand(c, "properties", old_value, new_value));
329  }
330  }
331  }
332  }
333  else if (who == 3)
334  {
335  for (IndependentTextItem *text : texts)
336  {
337  QRegularExpression rx(m_advanced_struct.search);
338  QString replace = m_advanced_struct.replace;
339  QString after = text->toPlainText();
340  after = after.replace(rx, replace);
341 
342  if (after != text->toPlainText()) {
343  project_->undoStack()->push(new ChangeDiagramTextCommand(text, text->toPlainText(), after));
344  }
345  }
346  }
347  project_->undoStack()->endMacro();
348 }
349 
360 void SearchAndReplaceWorker::setupLineEdit(QLineEdit *l, QCheckBox *cb, QString str)
361 {
362  l->setText(str);
363  cb->setChecked(str == eraseText() ? true : false);
364  l->setDisabled(str == eraseText() ? true : false);
365 }
366 
368 {
370 
371  //init with invalid value the conductor properties
372  cp.text_size = 0;
373  cp.text.clear();
374  cp.m_vertical_alignment = Qt::AlignAbsolute;
375  cp.m_horizontal_alignment = Qt::AlignAbsolute;
376  cp.verti_rotate_text = -1;
377  cp.horiz_rotate_text = -1;
378  cp.color = QColor();
379  cp.style = Qt::NoPen;
380  cp.cond_size = 0;
381  cp.m_color_2 = QColor();
382  cp.m_dash_size = 0;
383 
384  return cp;
385 }
386 
394 {
395  ConductorProperties new_properties = original;
396 
397  if (change.text_size > 2) {new_properties.text_size = change.text_size;}
398  new_properties.m_formula = applyChange(new_properties.m_formula, change.m_formula);
399  new_properties.text = applyChange(new_properties.text, change.text);
400  new_properties.m_show_text = change.m_show_text;
401  new_properties.m_function = applyChange(new_properties.m_function, change.m_function);
402  new_properties.m_tension_protocol = applyChange(new_properties.m_tension_protocol, change.m_tension_protocol);
403  if(change.m_vertical_alignment == Qt::AlignLeft ||
404  change.m_vertical_alignment == Qt::AlignRight) {new_properties.m_vertical_alignment = change.m_vertical_alignment;}
405  if(change.m_horizontal_alignment == Qt::AlignTop ||
406  change.m_horizontal_alignment == Qt::AlignBottom) {new_properties.m_horizontal_alignment = change.m_horizontal_alignment;}
407  if (change.verti_rotate_text >= 0) {new_properties.verti_rotate_text = change.verti_rotate_text;}
408  if (change.horiz_rotate_text >= 0) {new_properties.horiz_rotate_text = change.horiz_rotate_text;}
409  if (change.color.isValid()) {new_properties.color = change.color;}
410  if (change.style != Qt::NoPen) {new_properties.style = change.style;}
411  if (change.cond_size >= 0.4) {new_properties.cond_size = change.cond_size;}
412  new_properties.m_bicolor = change.m_bicolor;
413  if (change.m_color_2.isValid()) {new_properties.m_color_2 = change.m_color_2;}
414  if (change.m_dash_size >= 2) {new_properties.m_dash_size = change.m_dash_size;}
415  new_properties.singleLineProperties = change.singleLineProperties;
416 
417  return new_properties;
418 }
419 
426 QString SearchAndReplaceWorker::applyChange(const QString &original, const QString &change)
427 {
428  if (change.isEmpty()) {return original;}
429  else if (change == eraseText()) {return QString();}
430  else {return change;}
431 }
432 
440 {
442 
443  if (m_advanced_struct.who == 0)
444  {
445  QRegularExpression rx(m_advanced_struct.search);
446  QString replace = m_advanced_struct.replace;
447  QString what = m_advanced_struct.what;
448  if (what == "title") {p.title = p.title.replace(rx, replace);}
449  else if (what == "author") {p.author = p.author.replace(rx, replace);}
450  else if (what == "filename") {p.filename = p.filename.replace(rx, replace);}
451  else if (what == "folio") {p.folio = p.folio.replace(rx, replace);}
452  else if (what == "plant") {p.plant = p.plant.replace(rx, replace);}
453  else if (what == "locmach") {p.locmach = p.locmach.replace(rx, replace);}
454  else if (what == "indexrev") {p.indexrev = p.indexrev.replace(rx, replace);}
455  }
456  return p;
457 }
458 
466 {
467  DiagramContext context = element->elementInformations();
468 
469  if (m_advanced_struct.who == 1)
470  {
471  QString what = m_advanced_struct.what;
472  if (context.contains(what))
473  {
474  QRegularExpression rx(m_advanced_struct.search);
475  QString replace = m_advanced_struct.replace;
476  QString value = context[what].toString();
477  context.addValue(what, value.replace(rx, replace));
478  }
479  }
480 
481  return context;
482 }
483 
491 {
492  ConductorProperties properties = conductor->properties();
493 
494  if (m_advanced_struct.who == 2)
495  {
496  QRegularExpression rx(m_advanced_struct.search);
497  QString what = m_advanced_struct.what;
498  QString replace = m_advanced_struct.replace;
499 
500  if (what == "formula") {properties.m_formula.replace(rx, replace);}
501  else if (what == "text") {properties.text.replace(rx, replace);}
502  else if (what == "function") {properties.m_function.replace(rx, replace);}
503  else if (what == "tension/protocol") {properties.m_tension_protocol.replace(rx, replace);}
504  }
505 
506  return properties;
507 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
QString locmach
Location(displayed by the default template)
Qt::Alignment m_vertical_alignment
ConductorProperties properties
Conductor::properties.
Definition: conductor.h:48
QString plant
Plant (displayed by the default template)
void replaceDiagram(QList< Diagram *> diagram_list)
SearchAndReplaceWorker::replaceDiagram Replace all properties of each diagram in , by the current titleblock propertie of this worker.
DiagramContext elementInformations() const
Definition: element.h:89
SingleLineProperties singleLineProperties
TitleBlockProperties exportTitleBlock()
QUndoStack * undoStack()
Definition: qetproject.h:164
void add(DiagramContext other)
DiagramContext::add Add all value of to this. If a key already exist, the value is replaced...
Qt::Alignment m_horizontal_alignment
QVariant value(const QString &key) const
advancedReplaceStruct m_advanced_struct
static ConductorProperties invalidConductorProperties()
QDate date
Date (displayed by the default template)
bool addValue(const QString &, const QVariant &, bool show=true)
QString filename
Filename (displayed by the default template)
The ChangeTitleBlockCommand class This command changes the title block properties for a particular di...
static ConductorProperties applyChange(const ConductorProperties &original, const ConductorProperties &change)
SearchAndReplaceWorker::applyChange.
DiagramContext context
Container for the additional, user-defined fields.
bool contains(const QString &) const
The ChangeElementInformationCommand class This class manage undo/redo to change the element informati...
void replaceIndiText(QList< IndependentTextItem *> list)
SearchAndReplaceWorker::replaceIndiText Replace all displayed text of independent text of Each must ...
QIcon tr
Definition: qeticons.cpp:204
static QStringList elementInfoKeys()
QETApp::elementInfoKeys.
Definition: qetapp.cpp:289
void replaceElement(QList< Element *> list)
SearchAndReplaceWorker::replaceElement Replace all properties of each elements in All element must b...
QString author
Author of the diagram/folio (displayed by the default template)
static void setupLineEdit(QLineEdit *l, QCheckBox *cb, QString str)
SearchAndReplaceWorker::setupLineEdit With search and replace, when the variable to edit is a text...
QString indexrev
Revision Index (displayed by the default template)
TitleBlockProperties m_titleblock_properties
void replaceConductor(QList< Conductor *> list)
SearchAndReplaceWorker::replaceConductor Replace all properties of each conductor in All conductor m...
QString folio
Folio information (displayed by the default template)
void replaceAdvanced(QList< Diagram *> diagrams=QList< Diagram *>(), QList< Element *> elements=QList< Element *>(), QList< IndependentTextItem *> texts=QList< IndependentTextItem *>(), QList< Conductor *> conductors=QList< Conductor *>())
SearchAndReplaceWorker::replaceAdvanced Apply the change of text according to the current advancedStr...
BorderTitleBlock border_and_titleblock
Diagram dimensions and title block.
Definition: diagram.h:74
ConductorProperties m_conductor_properties
QString title
Folio title (displayed by the default template)