QElectroTech  0.70
eseventaddarc.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 <QObject>
19 
20 #include "elementscene.h"
21 #include "partarc.h"
22 #include "editorcommands.h"
23 #include "eseventaddarc.h"
24 
30  ESEventInterface(scene),
31  m_arc(nullptr),
32  m_inverted(false)
33 {}
34 
39  if (m_running || m_abort)
40  delete m_arc;
41 }
42 
48 bool ESEventAddArc::mousePressEvent(QGraphicsSceneMouseEvent *event)
49 {
50  if (event -> button() == Qt::LeftButton)
51  {
52  if(!m_running) m_running = true;
53  QPointF pos = m_scene->snapToGrid(event -> scenePos());
54 
55  //create new arc
56  if (!m_arc)
57  {
58  m_arc = new PartArc(m_editor);
59  m_scene -> addItem(m_arc);
60  m_arc -> setPos(pos);
61  m_arc -> setProperty("startAngle", 0);
62  m_arc -> setProperty("spanAngle", 1440);
63  m_arc -> setProperty("antialias", true);
64  m_origin = pos;
65  return true;
66  }
67 
68  //At this point, m_arc is finish, we add it with an undo command
69  m_arc -> setRect(m_arc->rect().normalized());
70  m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Arc"), m_scene, m_arc));
71 
72  //Set m_arc to nullptr for create new ellipse at next mouse press
73  m_arc = nullptr;
74 
75  return true;
76  }
77 
78  return false;
79 }
80 
86 bool ESEventAddArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
87  updateHelpCross(event -> scenePos());
88  if (!m_arc) return false;
89 
90  m_mouse_pos = m_scene -> snapToGrid(event -> scenePos());
91  updateArc();
92 
93  return true;
94 }
95 
101 bool ESEventAddArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
102  if (event -> button() == Qt::RightButton) {
103  if (m_arc) {delete m_arc; m_arc = nullptr;}
104  else {m_running = false;}
105  return true;
106  }
107  return false;
108 }
109 
115 bool ESEventAddArc::keyPressEvent(QKeyEvent *event) {
116  if (m_arc && event->key() == Qt::Key_Space) {
117  m_inverted = m_inverted ? false : true;
118  updateArc();
119  return true;
120  }
121 
122  return (ESEventInterface::keyPressEvent(event));
123 }
124 
130 {
131  qreal width = (m_mouse_pos.x() - m_origin.x())*2;
132  if (width < 0) width *= -1;
133  qreal height = (m_mouse_pos.y() - m_origin.y())*2;
134  if (height < 0) height *= -1;
135 
136  QPointF pos_ = m_arc -> mapFromScene(m_origin);
137 
138  //Draw arc inverted
139  if (m_inverted)
140  {
141  //Adjust the start angle to be snapped at the origin point of draw
142  if (m_mouse_pos.y() > m_origin.y())
143  {
144  if (m_mouse_pos.x() > m_origin.x())
145  {
146  pos_.ry() -= height/2;
147  m_arc->setStartAngle(2880);
148  }
149  else
150  {
151  pos_.rx() -= width/2;
152  m_arc->setStartAngle(1440);
153  }
154  }
155  else
156  {
157  if (m_mouse_pos.x() > m_origin.x())
158  {
159  pos_.ry() -= height;
160  pos_.rx() -= width/2;
161  m_arc->setStartAngle(4320);
162  }
163  else
164  {
165  pos_.rx() -= width;
166  pos_.ry() -= height/2;
167  m_arc->setStartAngle(0);
168  }
169  }
170  }
171  //Draw arc non inverted
172  else
173  {
174  //Adjust the start angle to be snapped at the origin point of draw
175  if (m_mouse_pos.y() > m_origin.y())
176  {
177  if (m_mouse_pos.x() > m_origin.x())
178  {
179  pos_.rx() -= width/2;
180  m_arc->setStartAngle(0);
181  }
182  else
183  {
184  pos_.rx() -= width;
185  pos_.ry() -= height/2;
186  m_arc->setStartAngle(4320);
187  }
188  }
189  else
190  {
191  if (m_mouse_pos.x() > m_origin.x())
192  {
193  pos_.ry() -= height/2;
194  m_arc->setStartAngle(1440);
195  }
196  else
197  {
198  pos_.rx() -= width/2;
199  pos_.ry() -= height;
200  m_arc->setStartAngle(2880);
201  }
202  }
203  }
204 
205  m_arc -> setRect(QRectF(pos_, QSizeF(width, height)));
206 }
ElementScene * m_scene
QETElementEditor * m_editor
bool mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddArc::mouseMoveEvent.
~ESEventAddArc() override
ESEventAddArc::~ESEventAddArc.
QPointF m_origin
Definition: eseventaddarc.h:47
void setStartAngle(const int &start_angle) override
AbstractPartEllipse::setStartAngle Sets the start angle for an ellipse segment to angle...
Definition: partarc.h:60
QPointF m_mouse_pos
Definition: eseventaddarc.h:47
PartArc * m_arc
Definition: eseventaddarc.h:46
void updateHelpCross(const QPointF &p)
bool keyPressEvent(QKeyEvent *event) override
ESEventAddArc::keyPressEvent.
ESEventAddArc(ElementScene *scene)
ESEventAddArc::ESEventAddArc.
QRectF rect
AbstractPartEllipse::rect Returns the item&#39;s ellipse geometry as a QRectF.
QIcon tr
Definition: qeticons.cpp:204
virtual bool keyPressEvent(QKeyEvent *event)
ESEventInterface::keyPressEvent By default, press escape key abort the curent action.
QIcon PartArc
Definition: qeticons.cpp:127
void updateArc()
ESEventAddArc::updateArc Redraw the arc with curent value.
QPointF snapToGrid(QPointF point)
bool mousePressEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddPolygon::mousePressEvent.
bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddArc::mouseReleaseEvent.