QElectroTech  0.70
eseventaddrect.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 "eseventaddrect.h"
21 #include "elementscene.h"
22 #include "partrectangle.h"
23 #include "editorcommands.h"
24 
30  ESEventInterface(scene),
31  m_rect(nullptr)
32 {}
33 
38  if (m_running || m_abort)
39  delete m_rect;
40 }
41 
47 bool ESEventAddRect::mousePressEvent(QGraphicsSceneMouseEvent *event)
48 {
49  if (event -> button() == Qt::LeftButton)
50  {
51  if(!m_running) m_running = true;
52  QPointF pos = m_scene->snapToGrid(event -> scenePos());
53 
54  //create new rectangle, pos isn't define,
55  //so m_rect.pos = 0,0 , that mean event.scenePos is in same coordinate of item
56  //we don't need to map point for m_rect
57  if (!m_rect)
58  {
60  m_scene -> addItem(m_rect);
61  m_rect -> setRect(QRectF(pos, pos));
62  return true;
63  }
64 
65  //Add rectangle to scene
66  m_rect -> setRect(m_rect -> rect().normalized());
67  m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Rectangle"), m_scene, m_rect));
68 
69  //Set m_rect to nullptr for create new rectangle at next mouse press
70  m_rect = nullptr;
71 
72  return true;
73  }
74  return false;
75 }
76 
82 bool ESEventAddRect::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
83  updateHelpCross(event -> scenePos());
84  if (!m_rect) return false;
85 
86  QRectF rect(m_rect->rect().topLeft(), m_scene->snapToGrid(event -> scenePos()));
87  m_rect -> setRect(rect);
88  return true;
89 }
90 
96 bool ESEventAddRect::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
97  if (event -> button() == Qt::RightButton) {
98  if (m_rect) {delete m_rect; m_rect = nullptr;}
99  else {m_running = false;}
100  return true;
101  }
102  return false;
103 }
ElementScene * m_scene
QETElementEditor * m_editor
PartRectangle * m_rect
ESEventAddRect(ElementScene *scene)
ESEventAddRect::ESEventAddRect.
QIcon PartRectangle
Definition: qeticons.cpp:132
~ESEventAddRect() override
ESEventAddRect::~ESEventAddRect.
void updateHelpCross(const QPointF &p)
QIcon tr
Definition: qeticons.cpp:204
bool mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddRect::mouseMoveEvent.
bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddRect::mouseReleaseEvent.
QPointF snapToGrid(QPointF point)
QRectF rect
PartRectangle::rect.
Definition: partrectangle.h:34
bool mousePressEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddRect::mousePressEvent.