QElectroTech  0.70
eseventaddellipse.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 "eseventaddellipse.h"
21 #include "partellipse.h"
22 #include "editorcommands.h"
23 #include "elementscene.h"
24 
30  ESEventInterface(scene),
31  m_ellipse(nullptr)
32 {}
33 
38  if (m_running || m_abort){
39  delete m_ellipse;
40  }
41 }
42 
48 bool ESEventAddEllipse::mousePressEvent(QGraphicsSceneMouseEvent *event) {
49  if (event -> button() == Qt::LeftButton) {
50  if(!m_running) m_running = true;
51  QPointF pos = m_scene->snapToGrid(event -> scenePos());
52 
53  //create new ellpise
54  if (!m_ellipse) {
56  m_scene -> addItem(m_ellipse);
57  m_ellipse -> setPos(pos);
58  m_origin = m_new_pos = pos;
59  return true;
60  }
61 
62  //Add ellipse to scene
63  m_ellipse -> setRect(m_ellipse -> rect().normalized());
64  m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Ellipse"), m_scene, m_ellipse));
65 
66  //Set m_ellipse to nullptr for create new ellipse at next mouse press
67  m_ellipse = nullptr;
68 
69  return true;
70  }
71  return false;
72 }
73 
79 bool ESEventAddEllipse::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
80  updateHelpCross(event -> scenePos());
81  if (!m_ellipse) return false;
82 
83  QPointF pos = m_scene -> snapToGrid(event -> scenePos());
84  if (pos == m_new_pos) return true;
85  m_new_pos = pos;
86 
87  qreal width = (m_new_pos.x() - m_origin.x())*2;
88  qreal height = (m_new_pos.y() - m_origin.y())*2;
89  //calculates the position of the rectangle so that its center is at position (0,0) of m_ellipse
90  QPointF center(-width/2, -height/2);
91 
92  m_ellipse -> setRect(QRectF(center, QSizeF(width, height)));
93  return true;
94 }
95 
101 bool ESEventAddEllipse::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
102  if (event -> button() == Qt::RightButton) {
103  if (m_ellipse) {delete m_ellipse; m_ellipse = nullptr;}
104  else {m_running = false;}
105  return true;
106  }
107  return false;
108 }
QIcon PartEllipse
Definition: qeticons.cpp:129
ElementScene * m_scene
QETElementEditor * m_editor
bool mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddRect::mouseMoveEvent.
bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddEllipse::mouseReleaseEvent.
bool mousePressEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddEllipse::mousePressEvent.
PartEllipse * m_ellipse
void updateHelpCross(const QPointF &p)
QIcon tr
Definition: qeticons.cpp:204
ESEventAddEllipse(ElementScene *scene)
ESEventAddEllipse::ESEventAddEllipse.
~ESEventAddEllipse() override
ESEventAddEllipse::~ESEventAddEllipse.
QPointF snapToGrid(QPointF point)