QElectroTech  0.70
qetgraphicsitem.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 "qetgraphicsitem.h"
19 #include "diagram.h"
20 
27 QetGraphicsItem::QetGraphicsItem(QGraphicsItem *parent):
28  QGraphicsObject(parent),
29  is_movable_(true),
30  m_first_move(true),
31  snap_to_grid_(true)
32 {}
33 
35 {}
36 
42  return(qobject_cast<Diagram *>(scene()));
43 }
44 
50 void QetGraphicsItem::setPos(const QPointF &p) {
51  QPointF pp = Diagram::snapToGrid(p);
52  if (pp == pos() || !is_movable_)
53  return;
54  QGraphicsItem::setPos(pp);
55 }
56 
63 void QetGraphicsItem::setPos(qreal x, qreal y) {
64  setPos(QPointF(x, y));
65 }
66 
72  return m_state;
73 }
74 
80 void QetGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
81 {
82  if (event->button() == Qt::LeftButton)
83  {
84  m_first_move = true;
85  if (event->modifiers() & Qt::ControlModifier) {
86  setSelected(!isSelected());
87  }
88  }
89 
90  QGraphicsItem::mousePressEvent(event);
91 }
92 
98 void QetGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
99 {
100  editProperty();
101  event->accept();
102 }
103 
109 void QetGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
110 {
111  if (isSelected() && event->buttons() & Qt::LeftButton)
112  {
113  //Item is moving
114  if(diagram() && m_first_move) {
115  //It's the first movement, we signal it to parent diagram
117  }
118 
119  //we apply the mouse movement
120  QPointF old_pos = pos();
121  if (m_first_move) {
122  m_mouse_to_origin_movement = old_pos - event -> buttonDownScenePos(Qt::LeftButton);
123  }
124  QPointF expected_pos = event->scenePos() + m_mouse_to_origin_movement;
125  setPos(expected_pos); // setPos() will snap the expected position to the grid
126 
127  //we calcul the real movement apply by setPos()
128  QPointF effective_movement = pos() - old_pos;
129  if (diagram()) {
130  //we signal the real movement apply to diagram,
131  //who he apply to other selected item
132  diagram()->elementsMover().continueMovement(effective_movement);
133  }
134  event->accept();
135  }
136  else {
137  event->ignore();
138  }
139 
140  if (m_first_move) {
141  m_first_move = false;
142  }
143 }
144 
150 void QetGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
151 {
152  if (diagram()) {
154  event->accept();
155  }
156 }
void continueMovement(const QPointF &)
ElementsMover::continueMovement Add a move to the current movement.
~QetGraphicsItem() override=0
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
QetGraphicsItem::mouseMoveEvent handle mouse movement.
Diagram * diagram() const
QetGraphicsItem::diagram return the diagram of this item.
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
QetGraphicsItem::mouseReleaseEvent handle mouse release click.
void mousePressEvent(QGraphicsSceneMouseEvent *event) override
QetGraphicsItem::mousePressEvent handle the mouse click.
void endMovement()
ElementsMover::endMovement Ended the current movement by creating an undo added to the undostack of t...
QPointF m_mouse_to_origin_movement
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
QetGraphicsItem::mouseDoubleClickEvent handle the mouse double click.
ElementsMover & elementsMover()
Definition: diagram.cpp:1499
QET::GraphicsItemState m_state
QET::GraphicsItemState state() const
QetGraphicsItem::state.
int beginMovement(Diagram *, QGraphicsItem *=nullptr)
ElementsMover::beginMovement Start a new movement.
QetGraphicsItem(QGraphicsItem *parent=nullptr)
QetGraphicsItem::QetGraphicsItem Default constructor.
virtual void editProperty()
GraphicsItemState
Definition: qet.h:36
static QPointF snapToGrid(const QPointF &p)
Diagram::snapToGrid Return a nearest snap point of p.
Definition: diagram.cpp:1653
virtual void setPos(const QPointF &p)
QetGraphicsItem::setPos set the position of the item to p.