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 "partterminal.h"
21 #include "editorcommands.h"
22 #include "elementscene.h"
23 #include "eseventaddterminal.h"
24 
25 /**
26  * @brief ESEventAddTerminal::ESEventAddTerminal
27  * @param scene
28  */
ESEventAddTerminal(ElementScene * scene)29 ESEventAddTerminal::ESEventAddTerminal(ElementScene *scene) :
30 	ESEventInterface(scene)
31 {
32 	m_terminal = new PartTerminal(m_editor);
33 	m_scene -> addItem(m_terminal);
34 	m_running  = true;
35 }
36 
37 /**
38  * @brief ESEventAddTerminal::~ESEventAddTerminal
39  */
~ESEventAddTerminal()40 ESEventAddTerminal::~ESEventAddTerminal() {
41 	delete m_terminal;
42 }
43 
44 /**
45  * @brief ESEventAddTerminal::mouseMoveEvent
46  * @param event
47  * @return
48  */
mouseMoveEvent(QGraphicsSceneMouseEvent * event)49 bool ESEventAddTerminal::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
50 	QPointF pos = m_scene -> snapToGrid(event -> scenePos());
51 	updateHelpCross(pos);
52 	m_terminal -> setPos(pos);
53 	return true;
54 }
55 
56 /**
57  * @brief ESEventAddTerminal::mouseReleaseEvent
58  * @param event
59  * @return
60  */
mouseReleaseEvent(QGraphicsSceneMouseEvent * event)61 bool ESEventAddTerminal::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
62 	if (event -> button() == Qt::LeftButton) {
63 		m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Borne"), m_scene, m_terminal));
64 
65 		//Set new terminal with same rotation
66 		Qet::Orientation ori = m_terminal -> orientation();
67 		m_terminal = new PartTerminal(m_editor);
68 		m_scene -> addItem(m_terminal);
69 		m_terminal -> setOrientation(ori);
70 		m_terminal -> setPos(m_scene -> snapToGrid(event -> scenePos()));
71 
72 		return true;
73 	}
74 	else if (event -> button() == Qt::RightButton) {
75 		m_running = false;
76 		return true;
77 	}
78 
79 	return false;
80 }
81 
82 /**
83  * @brief ESEventAddTerminal::keyPressEvent
84  * @param event
85  * @return
86  */
keyPressEvent(QKeyEvent * event)87 bool ESEventAddTerminal::keyPressEvent(QKeyEvent *event) {
88 	if (event -> key() == Qt::Key_Space) {
89 		switch (m_terminal->orientation()) {
90 			case Qet::North :
91 				m_terminal -> setOrientation(Qet::East);
92 				break;
93 			case Qet::East :
94 				m_terminal -> setOrientation(Qet::South);
95 				break;
96 			case Qet::South :
97 				m_terminal -> setOrientation(Qet::West);
98 				break;
99 			case Qet::West :
100 				m_terminal -> setOrientation(Qet::North);
101 				break;
102 			default :
103 				m_terminal -> setOrientation(Qet::North);
104 				break;
105 		}
106 		return true;
107 	}
108 	return (ESEventInterface::keyPressEvent(event));
109 }
110