QElectroTech  0.70
editorcommands.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 "editorcommands.h"
19 
25  QUndoCommand(parent),
26  m_scene(scene),
27  m_view(view)
28 {
29 }
30 
36 ElementEditionCommand::ElementEditionCommand(const QString &text, ElementScene *scene, ElementView *view, QUndoCommand *parent):
37  QUndoCommand(text, parent),
38  m_scene(scene),
39  m_view(view)
40 {
41 }
42 
47 }
48 
53  return(m_scene);
54 }
55 
60  m_scene = scene;
61 }
62 
67  return(m_view);
68 }
69 
74  m_view = view;
75 }
76 
77 /*** DeletePartsCommand ***/
85  ElementScene *scene,
86  const QList<QGraphicsItem *>& parts,
87  QUndoCommand *parent
88 ) :
89  ElementEditionCommand(QObject::tr("suppression", "undo caption"), scene, nullptr, parent),
90  deleted_parts(parts)
91 {
92  foreach(QGraphicsItem *qgi, deleted_parts) {
93  m_scene -> qgiManager().manage(qgi);
94  }
95 }
96 
99  foreach(QGraphicsItem *qgi, deleted_parts) {
100  m_scene -> qgiManager().release(qgi);
101  }
102 }
103 
106  m_scene -> blockSignals(true);
107  foreach(QGraphicsItem *qgi, deleted_parts) {
108  m_scene -> addItem(qgi);
109  }
110  m_scene -> blockSignals(false);
111 }
112 
115  m_scene -> blockSignals(true);
116  foreach(QGraphicsItem *qgi, deleted_parts) {
117  m_scene -> removeItem(qgi);
118  }
119  m_scene -> blockSignals(false);
120 }
121 
122 /*** CutPartsCommand ***/
130  ElementView *view,
131  const ElementContent &c,
132  QUndoCommand *parent
133 ) :
134  ElementEditionCommand(view ? view -> scene() : nullptr, view, parent),
135  content_(c),
136  uses_offset(false),
137  first_redo(true)
138 {
139  setText(QObject::tr("coller"));
140  m_scene -> qgiManager().manage(content_);
141 }
142 
145  m_scene -> qgiManager().release(content_);
146 }
147 
150  // enleve les parties
151  m_scene -> blockSignals(true);
152  foreach(QGraphicsItem *part, content_) {
153  m_scene -> removeItem(part);
154  }
155  m_scene -> blockSignals(false);
156  if (uses_offset) {
157  m_view -> offset_paste_count_ = old_offset_paste_count_;
158  m_view -> start_top_left_corner_ = old_start_top_left_corner_;
159  }
160  m_view -> adjustSceneRect();
161 }
162 
165  if (first_redo) first_redo = false;
166  else {
167  // pose les parties
168  m_scene -> blockSignals(true);
169  foreach(QGraphicsItem *part, content_) {
170  m_scene -> addItem(part);
171  }
172  m_scene -> blockSignals(false);
173  if (uses_offset) {
174  m_view -> offset_paste_count_ = new_offset_paste_count_;
175  m_view -> start_top_left_corner_ = new_start_top_left_corner_;
176  }
177  }
178  m_scene -> slot_select(content_);
179  m_view -> adjustSceneRect();
180 }
181 
187 void PastePartsCommand::setOffset(int old_offset_pc, const QPointF &old_start_tlc, int new_offset_pc, const QPointF &new_start_tlc) {
188  old_offset_paste_count_ = old_offset_pc;
189  old_start_top_left_corner_ = old_start_tlc;
190  new_offset_paste_count_ = new_offset_pc;
191  new_start_top_left_corner_ = new_start_tlc;
192  uses_offset = true;
193 }
194 
195 /*** CutPartsCommand ***/
203  ElementScene *scene,
204  const QList<QGraphicsItem *>& parts,
205  QUndoCommand *parent
206 ) :
207  DeletePartsCommand(scene, parts, parent)
208 {
209  setText(QString(QObject::tr("couper des parties", "undo caption")));
210 }
211 
214 }
215 
216 /*** MovePartsCommand ***/
225  const QPointF &m,
226  ElementScene *scene,
227  const QList<QGraphicsItem *>& parts,
228  QUndoCommand *parent
229 ) :
230  ElementEditionCommand(QObject::tr("déplacement", "undo caption"), scene, nullptr, parent),
231  movement(m),
232  first_redo(true)
233 {
234  moved_parts = parts;
235 }
236 
239 }
240 
243  foreach(QGraphicsItem *qgi, moved_parts) qgi -> moveBy(-movement.x(), -movement.y());
244 }
245 
248  // le premier appel a redo, lors de la construction de l'objet, ne doit pas se faire
249  if (first_redo) {
250  first_redo = false;
251  return;
252  }
253  foreach(QGraphicsItem *qgi, moved_parts) qgi -> moveBy(movement.x(), movement.y());
254 }
255 
256 /*** AddPartCommand ***/
265  const QString &name,
266  ElementScene *scene,
267  QGraphicsItem *p,
268  QUndoCommand *parent
269 ) :
270  ElementEditionCommand(QString(QObject::tr("ajout %1", "undo caption")).arg(name), scene, nullptr, parent),
271  part(p),
272  first_redo(true)
273 {
274  m_scene -> qgiManager().manage(part);
275 }
276 
279  m_scene -> qgiManager().release(part);
280 }
281 
284  m_scene -> removeItem(part);
285 }
286 
289  // le premier appel a redo, lors de la construction de l'objet, ne doit pas se faire
290  if (first_redo) {
291  if (!part -> zValue()) {
292  // the added part has no specific zValue already defined, we put it
293  // above existing items (but still under terminals)
294  QList<QGraphicsItem *> existing_items = m_scene -> zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot);
295  qreal z = existing_items.count() ? existing_items.last() -> zValue() + 1 : 1;
296  part -> setZValue(z);
297  }
298  m_scene -> clearSelection();
299  first_redo = false;
300  return;
301  }
302  m_scene -> addItem(part);
303 }
304 
313  ElementScene *element_scene,
314  const NamesList &before,
315  const NamesList &after,
316  QUndoCommand *parent
317 ) :
318  ElementEditionCommand(QObject::tr("modification noms", "undo caption"), element_scene, nullptr, parent),
319  names_before(before),
320  names_after(after)
321 {
322 }
323 
326 }
327 
330  m_scene -> setNames(names_before);
331 }
332 
335  m_scene -> setNames(names_after);
336 }
337 
345  ElementScene *elmt,
347  QUndoCommand *parent
348 ) :
349  ElementEditionCommand(elmt, nullptr, parent),
350  m_option(o)
351 {
352  // retrieve all primitives but terminals
353  QList<QGraphicsItem *> items_list = m_scene -> zItems(ElementScene::SortByZValue | ElementScene::SelectedOrNot);
354 
355  // prend un snapshot des zValues
356  foreach(QGraphicsItem *qgi, items_list) undo_hash.insert(qgi, qgi -> zValue());
357 
358  // choisit le nom en fonction du traitement
359  if (m_option == QET::BringForward) {
360  setText(QObject::tr("amener au premier plan", "undo caption"));
361  applyBringForward(items_list);
362  } else if (m_option == QET::Raise) {
363  setText(QObject::tr("rapprocher", "undo caption"));
364  applyRaise(items_list);
365  } else if (m_option == QET::Lower) {
366  setText(QObject::tr("éloigner", "undo caption"));
367  applyLower(items_list);
368  } else if (m_option == QET::SendBackward) {
369  setText(QObject::tr("envoyer au fond", "undo caption"));
370  applySendBackward(items_list);
371  }
372 }
373 
376 }
377 
380  foreach(QGraphicsItem *qgi, undo_hash.keys()) qgi -> setZValue(undo_hash[qgi]);
381 }
382 
385  foreach(QGraphicsItem *qgi, redo_hash.keys()) qgi -> setZValue(redo_hash[qgi]);
386 }
387 
392 void ChangeZValueCommand::applyBringForward(const QList<QGraphicsItem *> &items_list) {
393  QList<QGraphicsItem *> non_selected_items = items_list;
394  QList<QGraphicsItem *> selected_items;
395  foreach(QGraphicsItem *qgi, non_selected_items) {
396  if (qgi -> isSelected()) {
397  selected_items << qgi;
398  non_selected_items.removeAt(non_selected_items.indexOf(qgi));
399  }
400  }
401  int z = 1;
402  foreach(QGraphicsItem *qgi, non_selected_items) redo_hash.insert(qgi, z ++);
403  foreach(QGraphicsItem *qgi, selected_items) redo_hash.insert(qgi, z ++);
404 }
405 
410 void ChangeZValueCommand::applyRaise(const QList<QGraphicsItem *> &items_list) {
411  QList<QGraphicsItem *> my_items_list = items_list;
412 
413  for (int i = my_items_list.count() - 2 ; i >= 0 ; -- i) {
414  if (my_items_list[i] -> isSelected()) {
415  if (!my_items_list[i +1] -> isSelected()) {
416  my_items_list.swap(i, i + 1);
417  }
418  }
419  }
420  int z = 1;
421  foreach(QGraphicsItem *qgi, my_items_list) redo_hash.insert(qgi, z ++);
422 }
423 
428 void ChangeZValueCommand::applyLower(const QList<QGraphicsItem *> &items_list) {
429  QList<QGraphicsItem *> my_items_list = items_list;
430 
431  for (int i = 1 ; i < my_items_list.count() ; ++ i) {
432  if (my_items_list[i] -> isSelected()) {
433  if (!my_items_list[i - 1] -> isSelected()) {
434  my_items_list.swap(i, i - 1);
435  }
436  }
437  }
438 
439  int z = 1;
440  foreach(QGraphicsItem *qgi, my_items_list) redo_hash.insert(qgi, z ++);
441 }
442 
447 void ChangeZValueCommand::applySendBackward(const QList<QGraphicsItem *> &items_list) {
448  QList<QGraphicsItem *> non_selected_items = items_list;
449  QList<QGraphicsItem *> selected_items;
450  foreach(QGraphicsItem *qgi, non_selected_items) {
451  if (qgi -> isSelected()) {
452  selected_items << qgi;
453  non_selected_items.removeAt(non_selected_items.indexOf(qgi));
454  }
455  }
456  int z = 1;
457  foreach(QGraphicsItem *qgi, selected_items) redo_hash.insert(qgi, z ++);
458  foreach(QGraphicsItem *qgi, non_selected_items) redo_hash.insert(qgi, z ++);
459 }
460 
468 ChangeInformationsCommand::ChangeInformationsCommand(ElementScene *elmt, const QString &old_infos, const QString &new_infos, QUndoCommand *parent) :
469  ElementEditionCommand(QObject::tr("modification informations complementaires", "undo caption"), elmt, nullptr, parent),
470  old_informations_(old_infos),
471  new_informations_(new_infos)
472 {
473 }
474 
477 }
478 
481  m_scene -> setInformations(old_informations_);
482 }
483 
486  m_scene -> setInformations(new_informations_);
487 }
488 
494 ScalePartsCommand::ScalePartsCommand(ElementScene *scene, QUndoCommand * parent) :
495  ElementEditionCommand(scene, nullptr, parent),
496  first_redo(true)
497 {}
498 
503 }
504 
510 }
511 
516  if (first_redo) {
517  first_redo = false;
518  return;
519  }
521 }
522 
527  return(m_scene);
528 }
529 
533 void ScalePartsCommand::setScaledPrimitives(const QList<CustomElementPart *> &primitives) {
534  scaled_primitives_ = primitives;
535  adjustText();
536 }
537 
541 QList<CustomElementPart *> ScalePartsCommand::scaledPrimitives() const {
542  return(scaled_primitives_);
543 }
544 
550 void ScalePartsCommand::setTransformation(const QRectF &original_rect, const QRectF &new_rect) {
551  original_rect_ = original_rect;
552  new_rect_ = new_rect;
553 }
554 
560 QPair<QRectF, QRectF> ScalePartsCommand::transformation() {
561  return(QPair<QRectF, QRectF>(original_rect_, new_rect_));
562 }
563 
567 void ScalePartsCommand::scale(const QRectF &before, const QRectF &after) {
568  if (!scaled_primitives_.count()) return;
569  if (before == after) return;
570  if (!before.width() || !before.height()) return; // cowardly flee division by zero FIXME?
571 
572  foreach (CustomElementPart *part_item, scaled_primitives_) {
573  part_item -> startUserTransformation(before);
574  part_item -> handleUserTransformation(before, after);
575  }
576 }
577 
582  if (scaled_primitives_.count() == 1) {
583  setText(QObject::tr("redimensionnement %1", "undo caption -- %1 is the resized primitive type name").arg(scaled_primitives_.first() -> name()));
584  } else {
585  setText(QObject::tr("redimensionnement de %1 primitives", "undo caption -- %1 always > 1").arg(scaled_primitives_.count()));
586  }
587 }
596 ChangePropertiesCommand::ChangePropertiesCommand(ElementScene *scene, const QString& type, const DiagramContext& info, const DiagramContext& elmt_info, QUndoCommand *parent) :
597  ElementEditionCommand(scene, nullptr, parent)
598 {
599  m_type << scene->m_elmt_type << type;
600  m_kind_info << scene->m_elmt_kindInfo << info;
601  m_elmt_info << scene->m_elmt_information << elmt_info;
602  setText(QObject::tr("Modifier les propriétés"));
603 }
604 
606 
608 {
609  m_scene->m_elmt_type = m_type.first();
612 }
613 
615 {
616  m_scene->m_elmt_type = m_type.last();
619 }
620 
621 
622 
void redo() override
QPair< QRectF, QRectF > transformation()
void setElementScene(ElementScene *)
void applyBringForward(const QList< QGraphicsItem *> &)
void redo() override
Refait les changements de zValue.
~ElementEditionCommand() override
void redo() override
Refait le deplacement.
ElementScene * elementScene() const
Send item one layer below their current one; zValues are decremented.
Definition: qet.h:46
void undo() override
annule le coller
DiagramContext m_elmt_kindInfo
element type
Definition: elementscene.h:69
Raise item one layer above their current one; zValues are incremented.
Definition: qet.h:45
ElementScene * elementScene() const
~ChangeNamesCommand() override
Destructeur.
~ScalePartsCommand() override
QPointF old_start_top_left_corner_
ElementEditionCommand(ElementScene *=nullptr, ElementView *=nullptr, QUndoCommand *=nullptr)
QPointF new_start_top_left_corner_
QList< QString > m_type
void undo() override
Annule le deplacement.
void setScaledPrimitives(const QList< CustomElementPart *> &)
void redo() override
Refait l&#39;ajout.
ElementView * m_view
QList< QGraphicsItem * > deleted_parts
Deleted primitives.
void undo() override
Annule l&#39;ajout.
QString old_informations_
Former information.
ElementView * elementView() const
ElementScene * m_scene
Element editor/view/scene the command should take place on.
Bring item to the foreground so they have the highest zValue.
Definition: qet.h:44
void setElementView(ElementView *)
ChangeInformationsCommand(ElementScene *, const QString &, const QString &, QUndoCommand *=nullptr)
QString new_informations_
New information.
ElementContent content_
Pasted content.
void undo() override
Annule les changements de zValue.
bool first_redo
Prevent the first call to redo()
void redo() override
Refait le changement.
CutPartsCommand(ElementScene *, const QList< QGraphicsItem *> &, QUndoCommand *=nullptr)
~AddPartCommand() override
Destructeur.
QRectF original_rect_
original rect items fit in
The ElementEditionCommand class ElementEditionCommand is the base class for all commands classes invo...
~ChangeZValueCommand() override
Destructeur.
QList< CustomElementPart * > scaledPrimitives() const
~ChangeInformationsCommand() override
Destructeur.
~PastePartsCommand() override
Destructeur.
void undo() override
Annule le changement d&#39;autorisation pour les connexions internes.
QPointF movement
applied movement
bool first_redo
Prevent the first call to redo()
QGraphicsItem * part
Added primitive.
QList< CustomElementPart * > scaled_primitives_
List of moved primitives.
QIcon tr
Definition: qeticons.cpp:204
void undo() override
Annule le changement.
QHash< QGraphicsItem *, qreal > redo_hash
associates impacted primitives with their new zValues
DepthOption
List the various kind of changes for the zValue.
Definition: qet.h:43
ChangePropertiesCommand(ElementScene *scene, const QString &type, const DiagramContext &info, const DiagramContext &elmt_info, QUndoCommand *parent=nullptr)
ChangePropertiesCommand::ChangePropertiesCommand Change the properties of the drawed element...
NamesList names_before
List of former names.
void redo() override
Refait le changement d&#39;autorisation pour les connexions internes.
QList< DiagramContext > m_kind_info
~CutPartsCommand() override
Destructeur.
QRectF new_rect_
new rect items should fit in
ChangeNamesCommand(ElementScene *, const NamesList &, const NamesList &, QUndoCommand *=nullptr)
DeletePartsCommand(ElementScene *, const QList< QGraphicsItem *> &, QUndoCommand *=nullptr)
PastePartsCommand(ElementView *, const ElementContent &, QUndoCommand *=nullptr)
QString m_elmt_type
Extra informations.
Definition: elementscene.h:68
void redo() override
Supprime les parties.
AddPartCommand(const QString &, ElementScene *, QGraphicsItem *, QUndoCommand *=nullptr)
QList< QGraphicsItem * > moved_parts
List of moved primitives.
void applyLower(const QList< QGraphicsItem *> &)
void undo() override
DiagramContext m_elmt_information
Definition: elementscene.h:69
ChangeZValueCommand(ElementScene *, QET::DepthOption, QUndoCommand *=nullptr)
void redo() override
refait le coller
virtual void setOffset(int, const QPointF &, int, const QPointF &)
void applySendBackward(const QList< QGraphicsItem *> &)
void setTransformation(const QRectF &, const QRectF &)
ScalePartsCommand(ElementScene *=nullptr, QUndoCommand *=nullptr)
~MovePartsCommand() override
Destructeur.
NamesList names_after
List of new names.
QList< QGraphicsItem * > ElementContent
Send item to the background so they have the lowest zValue.
Definition: qet.h:47
int old_offset_paste_count_
Data required to undo a copy/paste with offset.
void undo() override
Restaure les parties supprimees.
QET::DepthOption m_option
kind of treatment to apply
void scale(const QRectF &before, const QRectF &after)
QHash< QGraphicsItem *, qreal > undo_hash
associates impacted primitives with their former zValues
MovePartsCommand(const QPointF &, ElementScene *, const QList< QGraphicsItem *> &, QUndoCommand *=nullptr)
bool first_redo
Prevent the first call to redo()
void setElementInfo(const DiagramContext &dc)
bool first_redo
Prevent the first call to redo()
void applyRaise(const QList< QGraphicsItem *> &)
~DeletePartsCommand() override
Destructeur : detruit egalement les parties supprimees.
QList< DiagramContext > m_elmt_info