QElectroTech  0.70
eseventaddline.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 <QGraphicsSceneMouseEvent>
19 #include <QObject>
20 
21 #include "eseventaddline.h"
22 #include "elementscene.h"
23 #include "qetelementeditor.h"
24 #include "partline.h"
25 #include "editorcommands.h"
26 
33  ESEventInterface (scene),
34  m_line (nullptr)
35 {}
36 
42  if (m_running || m_abort)
43  delete m_line;
44 }
45 
51 bool ESEventAddLine::mousePressEvent(QGraphicsSceneMouseEvent *event) {
52 
53  if (event -> button() == Qt::LeftButton) {
54 
55  if (! m_running) m_running = true;
56  QPointF pos = m_scene -> snapToGrid(event -> scenePos());
57 
58  //Create new line
59  if (!m_line) {
60  m_line = new PartLine(m_editor);
61  m_scene -> addItem(m_line);
62  m_line->setLine(QLineF(pos, pos));
63  return true;
64  }
65 
66  //Add new line to scene
67  m_scene -> undoStack().push(new AddPartCommand(QObject::tr("ligne"), m_scene, m_line));
68 
69  //Set m_line to nullptr for create new line at next mouse press
70  m_line = nullptr;
71 
72  return true;
73  }
74 
75  return false;
76 }
77 
83 bool ESEventAddLine::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
84 {
85  updateHelpCross (event -> scenePos());
86  if (!m_line) return false;
87 
88  QLineF line = m_line->line();
89  line.setP2(m_scene -> snapToGrid(event -> scenePos()));
90  m_line -> setLine(line);
91  return true;
92 }
93 
99 bool ESEventAddLine::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
100  if (event -> button() == Qt::RightButton) {
101  if (m_line) {delete m_line; m_line = nullptr;}
102  else {m_running = false;}
103  return true;
104  }
105  return false;
106 }
ElementScene * m_scene
QETElementEditor * m_editor
PartLine * m_line
bool mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddLine::mouseReleaseEvent.
QLineF line
Definition: partline.h:44
void updateHelpCross(const QPointF &p)
bool mouseMoveEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddLine::mouseMoveEvent.
void setLine(const QLineF &line)
Definition: partline.cpp:624
ESEventAddLine(ElementScene *scene)
ESEventAddLine::ESEventAddLine Constructor.
~ESEventAddLine() override
ESEventAddLine::~ESEventAddLine destructor.
bool mousePressEvent(QGraphicsSceneMouseEvent *event) override
ESEventAddLine::mousePressEvent.
QIcon tr
Definition: qeticons.cpp:204
QIcon PartLine
Definition: qeticons.cpp:130