QElectroTech  0.70
eseventaddpolygon.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 "eseventaddpolygon.h"
21 #include "elementscene.h"
22 #include "partpolygon.h"
23 #include "editorcommands.h"
24 
30  ESEventInterface(scene),
31  m_polygon(nullptr)
32 {}
33 
38  if (m_running || m_abort)
39  delete m_polygon;
40 }
41 
47 bool ESEventAddPolygon::mousePressEvent(QGraphicsSceneMouseEvent *event) {
48  if (event -> button() == Qt::LeftButton) {
49  if(!m_running) m_running = true;
50  QPointF pos = m_scene->snapToGrid(event -> scenePos());
51 
52  //create new polygon
53  if (!m_polygon) {
55  m_scene -> addItem(m_polygon);
56  m_polygon -> addPoint(pos);
57  }
58 
59  m_polygon -> addPoint(pos);
60  return true;
61  }
62  return false;
63 }
64 
70 bool ESEventAddPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
71  updateHelpCross(event -> scenePos());
72  if (!m_polygon) return false;
73 
74  m_polygon -> setLastPoint(m_scene -> snapToGrid(event -> scenePos()));
75  return true;
76 }
77 
83 bool ESEventAddPolygon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
84  if (event -> button() == Qt::RightButton) {
85  if (m_polygon) {
86  m_polygon -> removeLastPoint();
87 
88  if (m_polygon -> polygon().size() > 1)
89  { m_polygon -> setLastPoint(m_scene -> snapToGrid(event -> scenePos())); }
90  else
91  { delete m_polygon; m_polygon = nullptr; }
92  }
93  else
94  { m_running = false; }
95 
96  return true;
97  }
98  return false;
99 }
100 
106 bool ESEventAddPolygon::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
107 {
108  if (event -> button() == Qt::LeftButton)
109  {
110  if (m_polygon)
111  {
113  if (m_polygon->polygon().first() == m_polygon->polygon().last())
114  {
116  m_polygon->setClosed(true);
117  }
118  m_scene->undoStack().push(new AddPartCommand(QObject::tr("Polygone"), m_scene, m_polygon));
119 
120  //Set m_polygon to nullptr for create new polygon at next mouse press
121  m_polygon = nullptr;
122  return true;
123  }
124  }
125  return false;
126 }
ElementScene * m_scene
QETElementEditor * m_editor
ESEventAddPolygon(ElementScene *scene)
ESEventAddPolygon::ESEventAddPolygon.
PartPolygon * m_polygon
void setClosed(bool close)
bool mousePressEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddPolygon::mousePressEvent.
bool mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddPolygon::mouseMoveEvent.
~ESEventAddPolygon() override
ESEventAddPolygon::~ESEventAddPolygon.
void updateHelpCross(const QPointF &p)
QUndoStack & undoStack()
QIcon PartPolygon
Definition: qeticons.cpp:131
void removeLastPoint()
PartPolygon::removeLastPoint Remove the last point of polygon.
QPolygonF polygon
PartPolygon::polygon.
Definition: partpolygon.h:38
QIcon tr
Definition: qeticons.cpp:204
bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddPolygon::mouseReleaseEvent.
bool mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddPolygon::mouseDoubleClickEvent.
QPointF snapToGrid(QPointF point)