QElectroTech  0.70
diagramcommands.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 "diagramcommands.h"
22 #include "diagram.h"
24 #include "qgimanager.h"
25 #include "diagram.h"
28 #include "elementtextitemgroup.h"
29 #include <QPropertyAnimation>
30 
31 QString itemText(const QetGraphicsItem *item) {
32  return item->name();
33 }
34 
35 QString itemText(const IndependentTextItem *item) {
36  Q_UNUSED(item);
37  return QObject::tr("un champ texte");
38 }
39 
40 QString itemText(const Conductor *item) {
41  Q_UNUSED(item);
42  return QObject::tr("un conducteur");
43 }
44 
52 PasteDiagramCommand::PasteDiagramCommand( Diagram *dia, const DiagramContent &c, QUndoCommand *parent) :
53  QUndoCommand(parent),
54  content(c),
55  diagram(dia),
56  filter(DiagramContent::Elements|DiagramContent::TextFields|DiagramContent::Images|DiagramContent::ConductorsToMove | DiagramContent::Shapes),
57  first_redo(true)
58 {
59  setText(QObject::tr("coller %1", "undo caption - %1 is a sentence listing the content to paste").arg(content.sentence(filter)));
60  diagram -> qgiManager().manage(content.items(filter));
61 }
62 
68  diagram -> qgiManager().release(content.items(filter));
69 }
70 
76 {
77  diagram -> showMe();
78 
79  foreach(QGraphicsItem *item, content.items(filter))
80  diagram->removeItem(item);
81 }
82 
88 {
89  diagram -> showMe();
90  QSettings settings;
91 
92  if (first_redo)
93  {
94  first_redo = false;
95 
96  //this is the first paste, we do some actions for the new element
97  const QList <Element *> elmts_list = content.m_elements;
98  for (Element *e : elmts_list)
99  {
100  //make new uuid, because old uuid are the uuid of the copied element
101  e -> newUuid();
102 
103  if (settings.value("diagramcommands/erase-label-on-copy", true).toBool())
104  {
105  //Reset the information about the label, the comment and location
106  DiagramContext dc = e->elementInformations();
107  dc.addValue("formula", "");
108  dc.addValue("label", "");
109  dc.addValue("comment", "");
110  dc.addValue("location", "");
111  e->setElementInformations(dc);
112 
113  //Reset the text of conductors
114  const QList <Conductor *> conductors_list = content.m_conductors_to_move;
115  for (Conductor *c : conductors_list)
116  {
117  ConductorProperties cp = c -> properties();
118  cp.text = c->diagram() ? c -> diagram() -> defaultConductorProperties.text : "_";
119  c -> setProperties(cp);
120  }
121  }
122  }
123  }
124  else
125  {
126  const QList <QGraphicsItem *> qgis_list = content.items(filter);
127  for (QGraphicsItem *item : qgis_list) {
128  diagram->addItem(item);
129  }
130  }
131 
132  const QList <QGraphicsItem *> qgis_list = content.items();
133  for (QGraphicsItem *qgi : qgis_list)
134  qgi -> setSelected(true);
135 }
136 
144  Diagram *dia,
145  const DiagramContent &content,
146  QUndoCommand *parent
147 ) :
148  DeleteQGraphicsItemCommand(dia, content, parent)
149 {
150  setText(
151  QString(
152  QObject::tr(
153  "couper %1",
154  "undo caption - %1 is a sentence listing the content to cut"
155  ).arg(content.sentence(DiagramContent::All))
156  )
157  );
158 }
159 
162 }
163 
173  Diagram *dia,
174  const DiagramContent &diagram_content,
175  const QPointF &m,
176  QUndoCommand *parent
177 ) :
178  QUndoCommand (parent),
179  diagram (dia),
180  content_to_move (diagram_content),
181  movement (m),
182  m_anim_group (nullptr),
183  first_redo (true)
184 {
185  QString moved_content_sentence = content_to_move.sentence(
193  );
194 
195  setText(
196  QString(
197  QObject::tr(
198  "déplacer %1",
199  "undo caption - %1 is a sentence listing the moved content"
200  ).arg(moved_content_sentence)
201  )
202  );
203 }
204 
210  delete m_anim_group;
211 }
212 
217  diagram -> showMe();
218  m_anim_group->setDirection(QAnimationGroup::Forward);
219  m_anim_group->start();
220  QUndoCommand::undo();
221 }
222 
227  diagram -> showMe();
228  if (first_redo) {
229  first_redo = false;
230  move(-movement);
231  }
232  else {
233  m_anim_group->setDirection(QAnimationGroup::Backward);
234  m_anim_group->start();
235  }
236  QUndoCommand::redo();
237 }
238 
244 void MoveElementsCommand::move(const QPointF &actual_movement)
245 {
246  typedef DiagramContent dc;
247 
248  //Move every movable items, except conductor
249  for (QGraphicsItem *qgi : content_to_move.items(dc::Elements | dc::TextFields | dc::Images | dc::Shapes | dc::TextGroup | dc::ElementTextFields))
250  {
251  //If curent item have parent, and parent item is in content_to_move
252  //we don't apply movement to this item, because this item will be moved by is parent.
253  if (qgi->parentItem())
254  if (content_to_move.items().contains(qgi->parentItem()))
255  continue;
256 
257  if(qgi->toGraphicsObject())
258  setupAnimation(qgi->toGraphicsObject(), "pos", qgi->pos(), qgi->pos() + actual_movement);
259  else if(qgi->type() == QGraphicsItemGroup::Type) //ElementTextItemGroup is a QObject but not a QGraphicsObject
260  {
261  if(ElementTextItemGroup *etig = dynamic_cast<ElementTextItemGroup *>(qgi))
262  setupAnimation(etig, "pos", etig->pos(), etig->pos() + actual_movement);
263  }
264  else qgi -> setPos(qgi->pos() + actual_movement);
265  }
266 
267  // Move some conductors
269  setupAnimation(conductor, "pos", conductor->pos(), conductor->pos() + actual_movement);
270 
271  // Recalcul the path of other conductor
273  setupAnimation(conductor, "animPath", 1, 1);
274 }
275 
284 void MoveElementsCommand::setupAnimation(QObject *target, const QByteArray &propertyName, const QVariant& start, const QVariant& end) {
285  //create animation group if not yet.
286  if (m_anim_group == nullptr) m_anim_group = new QParallelAnimationGroup();
287  QPropertyAnimation *animation = new QPropertyAnimation(target, propertyName);
288  animation->setDuration(300);
289  animation->setStartValue(start);
290  animation->setEndValue(end);
291  animation->setEasingCurve(QEasingCurve::OutQuad);
292  m_anim_group->addAnimation(animation);
293 }
294 
304  Diagram *diagram,
305  QUndoCommand *parent
306 ) :
307  QUndoCommand(parent),
308  diagram(diagram),
309  first_redo(true)
310 {
311 }
312 
315 }
316 
319  diagram -> showMe();
320  foreach(ConductorTextItem *cti, texts_to_move_.keys()) {
321  QPointF movement = texts_to_move_[cti].first;
322  bool was_already_moved = texts_to_move_[cti].second;
323 
324  cti -> forceMovedByUser(was_already_moved);
325  if (was_already_moved) {
326  cti -> setPos(cti -> pos() - movement);
327  }
328  }
329 }
330 
333  diagram -> showMe();
334  if (first_redo) {
335  first_redo = false;
336  } else {
337  foreach(ConductorTextItem *cti, texts_to_move_.keys()) {
338  QPointF movement = texts_to_move_[cti].first;
339 
340  cti -> forceMovedByUser(true);
341  cti -> setPos(cti -> pos() + movement);
342  }
343  }
344 }
345 
353 void MoveConductorsTextsCommand::addTextMovement(ConductorTextItem *text_item, const QPointF &old_pos, const QPointF &new_pos, bool already_moved) {
354  // si le champ de texte est deja connu de l'objet d'annulation, il sera ignore
355  if (texts_to_move_.contains(text_item)) return;
356 
357  // on memorise le champ de texte, en l'associant au mouvement effectue et a son etat avant le deplacement
358  texts_to_move_.insert(text_item, qMakePair(new_pos - old_pos, already_moved));
359 
360  // met a jour la description de l'objet d'annulation
362 }
363 
368  QString moved_content_sentence = QET::ElementsAndConductorsSentence(0, 0, texts_to_move_.count());
369 
370  setText(
371  QString(
372  QObject::tr(
373  "déplacer %1",
374  "undo caption - %1 is a sentence listing the moved content"
375  ).arg(moved_content_sentence)
376  )
377  );
378 }
379 
388  DiagramTextItem *dti,
389  const QString &before,
390  const QString &after,
391  QUndoCommand *parent
392 ) :
393  QUndoCommand(QObject::tr("modifier le texte", "undo caption"), parent),
394  text_item(dti),
395  text_before(before),
396  text_after(after),
397  first_redo(true),
398  diagram(dti->diagram())
399 {
400 }
401 
404 }
405 
408  diagram -> showMe();
409  text_item -> setHtml(text_before);
410 }
411 
416 {
417  diagram -> showMe();
419 }
420 
430  Conductor *c,
431  const ConductorProfile &old_p,
432  const ConductorProfile &new_p,
433  Qt::Corner path_t,
434  QUndoCommand *parent
435 ) :
436  QUndoCommand(QObject::tr("modifier un conducteur", "undo caption"), parent),
437  conductor(c),
438  old_profile(old_p),
439  new_profile(new_p),
440  path_type(path_t),
441  first_redo(true),
442  diagram (c->diagram())
443 {
444 }
445 
448 }
449 
452  diagram -> showMe();
453  conductor -> setProfile(old_profile, path_type);
454  conductor -> textItem() -> setPos(text_pos_before_mov_);
455 }
456 
459  diagram -> showMe();
460  if (first_redo) {
461  first_redo = false;
462  } else {
463  conductor -> setProfile(new_profile, path_type);
464  conductor -> textItem() -> setPos(text_pos_after_mov_);
465  }
466 }
467 
474 void ChangeConductorCommand::setConductorTextItemMove(const QPointF &pos_before, const QPointF &pos_after) {
475  text_pos_before_mov_ = pos_before;
476  text_pos_after_mov_ = pos_after;
477 }
478 
485  const QHash<Conductor *, ConductorProfilesGroup> &cp,
486  QUndoCommand *parent
487 ) :
488  QUndoCommand(parent),
489  conductors_profiles(cp),
490  diagram(cp.keys().first()->diagram())
491 {
492  setText(
493  QObject::tr(
494  "Réinitialiser %1",
495  "undo caption - %1 is a sentence listing the reset content"
496  ).arg(QET::ElementsAndConductorsSentence(0, cp.count()))
497  );
498 }
499 
504 }
505 
510  diagram -> showMe();
511  foreach(Conductor *c, conductors_profiles.keys()) {
512  c -> setProfiles(conductors_profiles[c]);
513  }
514 }
515 
520  diagram -> showMe();
521  foreach(Conductor *c, conductors_profiles.keys()) {
522  c -> textItem() -> forceMovedByUser (false);
523  c -> textItem() -> forceRotateByUser (false);
524  c -> setProfiles(ConductorProfilesGroup());
525  }
526 }
527 
535 ChangeBorderCommand::ChangeBorderCommand(Diagram *dia, const BorderProperties &old_bp, const BorderProperties &new_bp, QUndoCommand *parent) :
536  QUndoCommand(QObject::tr("modifier les dimensions du folio", "undo caption"), parent),
537  diagram(dia),
538  old_properties(old_bp),
539  new_properties(new_bp)
540 {
541 }
542 
545 }
546 
549  diagram -> showMe();
550  diagram -> border_and_titleblock.importBorder(old_properties);
551 }
552 
555  diagram -> showMe();
556  diagram -> border_and_titleblock.importBorder(new_properties);
557 }
QString itemText(const QetGraphicsItem *item)
Diagram * diagram
modified diagram
void setHtml(const QString &text)
~ResetConductorCommand() override
ResetConductorCommand::~ResetConductorCommand.
int filter
filter stating what kinds of items should be pasted
void undo() override
Annule la modification du conducteur.
QPointF movement
applied movement
ChangeDiagramTextCommand(DiagramTextItem *, const QString &before, const QString &after, QUndoCommand *=nullptr)
Conductor * conductor
changed conductor
QParallelAnimationGroup * m_anim_group
animation group
QString sentence(int=All) const
DiagramContent::sentence.
QString text_before
former text
~ChangeBorderCommand() override
Destructeur.
Qt::Corner path_type
Path type of the modified conductor.
QList< Conductor * > m_conductors_to_move
Diagram * diagram
diagram the movement takes place on.
virtual void setConductorTextItemMove(const QPointF &, const QPointF &)
MoveConductorsTextsCommand(Diagram *, QUndoCommand *=nullptr)
~CutDiagramCommand() override
Destructeur.
bool addValue(const QString &, const QVariant &, bool show=true)
QList< QGraphicsItem * > items(int=All) const
DiagramContent::items.
QHash< Qt::Corner, ConductorProfile > ConductorProfilesGroup
Definition: conductor.h:37
void undo() override
annule la modification de texte
virtual void removeItem(QGraphicsItem *item)
Diagram::removeItem Réimplemented from QGraphicsScene::removeItem(QGraphicsItem *item) Do some specif...
Definition: diagram.cpp:1133
virtual void move(const QPointF &)
MoveElementsCommand::move Move item and conductor to .
ChangeConductorCommand(Conductor *, const ConductorProfile &, const ConductorProfile &, Qt::Corner, QUndoCommand *=nullptr)
bool first_redo
prevent the first call to redo()
void redo() override
MoveElementsCommand::redo.
void undo() override
annule le deplacement
void undo() override
PasteDiagramCommand::undo Undo this command.
virtual void addTextMovement(ConductorTextItem *, const QPointF &, const QPointF &, bool=false)
Diagram * diagram
diagram the movement takes place on.
ResetConductorCommand(const QHash< Conductor *, ConductorProfilesGroup > &, QUndoCommand *=nullptr)
ResetConductorCommand::ResetConductorCommand.
QString ElementsAndConductorsSentence(int, int, int=0, int=0, int=0, int=0)
Definition: qet.cpp:240
void redo() override
Refait la modification du conducteur.
CutDiagramCommand(Diagram *, const DiagramContent &, QUndoCommand *=nullptr)
ConductorProfile new_profile
profile after the change
~ChangeDiagramTextCommand() override
destructeur
MoveElementsCommand(Diagram *, const DiagramContent &, const QPointF &m, QUndoCommand *=nullptr)
MoveElementsCommand::MoveElementsCommand Constructor.
bool first_redo
prevent the first call to redo()
QIcon tr
Definition: qeticons.cpp:204
void redo() override
ChangeDiagramTextCommand::redo.
~MoveElementsCommand() override
MoveElementsCommand::~MoveElementsCommand Destructor.
ChangeBorderCommand(Diagram *, const BorderProperties &, const BorderProperties &, QUndoCommand *=nullptr)
DiagramTextItem * text_item
modified text item
~ChangeConductorCommand() override
Destructeur.
The ElementTextItemGroup class This class represent a group of element text Texts in the group can be...
BorderProperties old_properties
properties before the change
void undo() override
Annule les changements apportes au schema.
PasteDiagramCommand(Diagram *, const DiagramContent &, QUndoCommand *=nullptr)
PasteDiagramCommand::PasteDiagramCommand Constructor.
QList< Element * > m_elements
QPointF pos
Definition: conductor.h:46
QList< Conductor * > m_conductors_to_update
DiagramContent content_to_move
moved content
QHash< ConductorTextItem *, QPair< QPointF, bool > > texts_to_move_
text items to be moved
QString text_after
new text
DiagramContent content
pasted content
void redo() override
Refait les changements apportes au schema.
void redo() override
PasteDiagramCommand::redo Redo this commnand.
void redo() override
ResetConductorCommand::redo.
void undo() override
ResetConductorCommand::undo.
QPointF text_pos_before_mov_
position of the text item before the change
void undo() override
MoveElementsCommand::undo.
BorderProperties new_properties
properties after the change
QPointF text_pos_after_mov_
position of the text item after the change
~MoveConductorsTextsCommand() override
Destructeur.
~PasteDiagramCommand() override
PasteDiagramCommand::~PasteDiagramCommand Destructor.
ConductorProfile old_profile
profile before the change
virtual QString name() const
virtual void addItem(QGraphicsItem *item)
Diagram::addItem Réimplemented from QGraphicsScene::addItem(QGraphicsItem *item) Do some specific ope...
Definition: diagram.cpp:1108
void setupAnimation(QObject *target, const QByteArray &propertyName, const QVariant &start, const QVariant &end)
MoveElementsCommand::setupAnimation Set up the animation for this undo command.
Diagram * diagram
diagram content is pasted onto
bool first_redo
prevent the first call to redo()
QHash< Conductor *, ConductorProfilesGroup > conductors_profiles
impacted conductors along with their former profiles
void redo() override
refait le deplacement
bool first_redo
prevent the first call to redo()