QElectroTech  0.70
elementtextsmover.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2012 Xavier Guerrin
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 "elementtextsmover.h"
19 #include "diagram.h"
21 #include "dynamicelementtextitem.h"
22 #include "elementtextitemgroup.h"
23 #include <QObject>
24 
29 
36  return(!m_movement_running);
37 }
38 
46 int ElementTextsMover::beginMovement(Diagram *diagram, QGraphicsItem *driver_item)
47 {
48  if (m_movement_running || !diagram)
49  return(-1);
50 
51  m_diagram = diagram;
52  m_movement_driver = driver_item;
53  m_items_hash.clear();
55 
56  for(QGraphicsItem *item : diagram->selectedItems())
57  {
58  if(item->type() == DynamicElementTextItem::Type)
59  {
60  m_items_hash.insert(item, item->pos());
61  m_text_count++;
62  }
63  else if(item->type() == QGraphicsItemGroup::Type)
64  {
65  if(dynamic_cast<ElementTextItemGroup *>(item))
66  {
67  m_items_hash.insert(item, item->pos());
68  m_group_count++;
69  }
70  }
71  }
72 
73 
74  if(m_items_hash.isEmpty())
75  return -1;
76 
77  m_movement_running = true;
78 
79  return m_items_hash.size();
80 }
81 
82 void ElementTextsMover::continueMovement(QGraphicsSceneMouseEvent *event)
83 {
85  return;
86 
87  for(QGraphicsItem *qgi : m_items_hash.keys())
88  {
89  if(qgi == m_movement_driver)
90  continue;
91 
92  QPointF current_parent_pos;
93  QPointF button_down_parent_pos;
94 
95  current_parent_pos = qgi->mapToParent(qgi->mapFromScene(event->scenePos()));
96  button_down_parent_pos = qgi->mapToParent(qgi->mapFromScene(event->buttonDownScenePos(Qt::LeftButton)));
97 
98  QPointF new_pos = m_items_hash.value(qgi) + current_parent_pos - button_down_parent_pos;
99  event->modifiers() == Qt::ControlModifier ? qgi->setPos(new_pos) : qgi->setPos(Diagram::snapToGrid(new_pos));
100  }
101 }
102 
108 {
109  //No movement or no items to move
110  if(!m_movement_running || m_items_hash.isEmpty())
111  return;
112 
113  //Movement is null
114  QGraphicsItem *qgi = m_items_hash.keys().first();
115  if(qgi->pos() == m_items_hash.value(qgi))
116  return;
117 
118  QUndoCommand *undo = new QUndoCommand(undoText());
119 
120  for (QGraphicsItem *qgi : m_items_hash.keys())
121  {
122  if(QObject *object = dynamic_cast<QObject *>(qgi))
123  {
124  QPropertyUndoCommand *child_undo = new QPropertyUndoCommand(object, "pos", m_items_hash.value(qgi), qgi->pos(), undo);
125  child_undo->enableAnimation();
126  }
127  }
128 
129  m_diagram->undoStack().push(undo);
130 
131  m_movement_running = false;
132 }
133 
135 {
136  QString undo_text;
137 
138  if(m_text_count == 1)
139  undo_text.append(QObject::tr("Déplacer un texte d'élément"));
140  else if(m_text_count > 1)
141  undo_text.append(QObject::tr("Déplacer %1 textes d'élément").arg(m_items_hash.size()));
142 
143  if(m_group_count >= 1)
144  {
145  if(undo_text.isEmpty())
146  undo_text.append(QObject::tr("Déplacer"));
147  else
148  undo_text.append(QObject::tr(" et"));
149 
150  if(m_group_count == 1)
151  undo_text.append(QObject::tr(" un groupe de texte"));
152  else
153  undo_text.append(QObject::tr((" %1 groupes de textes")).arg(m_group_count));
154  }
155 
156  return undo_text;
157 }
The QPropertyUndoCommand class This undo command manage QProperty of a QObject. This undo command can...
int beginMovement(Diagram *diagram, QGraphicsItem *driver_item=nullptr)
ElementTextsMover::beginMovement Begin a movement.
void enableAnimation(bool animate=true)
QPropertyUndoCommand::enableAnimation True to enable animation.
QString undoText() const
QHash< QGraphicsItem *, QPointF > m_items_hash
QIcon tr
Definition: qeticons.cpp:204
void continueMovement(QGraphicsSceneMouseEvent *event)
QGraphicsItem * m_movement_driver
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:1653
QUndoStack & undoStack()
Definition: diagram.h:337
ElementTextsMover()
ElementTextsMover::ElementTextsMover.
bool isReady() const
ElementTextsMover::isReady.
void endMovement()
ElementTextsMover::endMovement Finish the movement by pushing an undo command to the parent diagram o...