[qet] [3470] element editor: add arc is managed by ESEvent

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 3470
Author:   blacksun
Date:     2014-11-09 22:58:46 +0100 (Sun, 09 Nov 2014)
Log Message:
-----------
element editor: add arc is managed by ESEvent

Modified Paths:
--------------
    trunk/sources/editor/elementscene.cpp
    trunk/sources/editor/elementscene.h
    trunk/sources/editor/esevent/eseventaddellipse.cpp
    trunk/sources/editor/qetelementeditor.cpp
    trunk/sources/editor/qetelementeditor.h

Added Paths:
-----------
    trunk/sources/editor/esevent/eseventaddarc.cpp
    trunk/sources/editor/esevent/eseventaddarc.h

Modified: trunk/sources/editor/elementscene.cpp
===================================================================
--- trunk/sources/editor/elementscene.cpp	2014-11-09 09:25:37 UTC (rev 3469)
+++ trunk/sources/editor/elementscene.cpp	2014-11-09 21:58:46 UTC (rev 3470)
@@ -96,14 +96,6 @@
 }
 
 /**
-	Passe la scene en mode "ajout d'arc de cercle"
-*/
-void ElementScene::slot_addArc() {
-	behavior = Arc;
-	if (m_event_interface) delete m_event_interface; m_event_interface = nullptr;
-}
-
-/**
 	Passe la scene en mode "ajout de champ de texte"
 */
 void ElementScene::slot_addTextField() {
@@ -136,21 +128,8 @@
 		paste_area_ -> setRect(current_rect);
 		return;
 	}
-	
-	QRectF temp_rect;
-	if (e -> buttons() & Qt::LeftButton) {
-		switch(behavior) {
-			case Arc:
-				temp_rect = current_arc -> rect();
-				temp_rect.setBottomRight(event_pos);
-				current_arc -> setRect(temp_rect);
-				break;
-			case Normal:
-			default:
-				QGraphicsScene::mouseMoveEvent(e);
-		}
-	}
-	else QGraphicsScene::mouseMoveEvent(e);
+
+	QGraphicsScene::mouseMoveEvent(e);
 }
 
 /**
@@ -170,19 +149,8 @@
 	}
 	QPointF event_pos = e -> scenePos();
 	if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos);
-	
-	if (e -> button() & Qt::LeftButton) {
-		switch(behavior) {
-			case Arc:
-				current_arc = new PartArc(element_editor, 0, this);
-				current_arc -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0)));
-				current_arc -> setProperty("antialias", true);
-				break;
-			case Normal:
-			default:
-				QGraphicsScene::mousePressEvent(e);
-		}
-	} else QGraphicsScene::mousePressEvent(e);
+
+	QGraphicsScene::mousePressEvent(e);
 }
 
 /**
@@ -218,13 +186,6 @@
 	
 	if (e -> button() & Qt::LeftButton) {
 		switch(behavior) {
-			case Arc:
-				if (qgiManager().manages(current_arc)) break;
-				current_arc-> setRect(current_arc -> rect().normalized());
-				undo_stack.push(new AddPartCommand(tr("arc"), this, current_arc));
-				emit(partsAdded());
-				endCurrentBehavior(e);
-				break;
 			case Terminal:
 				terminal = new PartTerminal(element_editor, 0, this);
 				terminal -> setPos(event_pos);

Modified: trunk/sources/editor/elementscene.h
===================================================================
--- trunk/sources/editor/elementscene.h	2014-11-09 09:25:37 UTC (rev 3469)
+++ trunk/sources/editor/elementscene.h	2014-11-09 21:58:46 UTC (rev 3470)
@@ -28,7 +28,6 @@
 class ElementEditionCommand;
 class ElementPrimitiveDecorator;
 class QETElementEditor;
-class PartArc;
 class ESEventInterface;
 class QKeyEvent;
 /**
@@ -42,7 +41,7 @@
 	
 	// enum
 	public:
-	enum Behavior { Normal, Circle, Text, Terminal, Arc, TextField, PasteArea };
+	enum Behavior { Normal, Circle, Text, Terminal, TextField, PasteArea };
 	enum ItemOption {
 		SortByZValue = 1,
 		IncludeTerminals = 2,
@@ -87,7 +86,6 @@
 		/// Variables related to drawing
 		ESEventInterface *m_event_interface;
 		Behavior behavior;
-		PartArc *current_arc;
 		QETElementEditor *element_editor;
 	
 		/// Variables to manage the paste area on the scene
@@ -165,7 +163,6 @@
 	void slot_move();
 	void slot_addCircle();
 	void slot_addText();
-	void slot_addArc();
 	void slot_addTerminal();
 	void slot_addTextField();
 	void slot_select(const ElementContent &);

Added: trunk/sources/editor/esevent/eseventaddarc.cpp
===================================================================
--- trunk/sources/editor/esevent/eseventaddarc.cpp	                        (rev 0)
+++ trunk/sources/editor/esevent/eseventaddarc.cpp	2014-11-09 21:58:46 UTC (rev 3470)
@@ -0,0 +1,189 @@
+/*
+	Copyright 2006-2014 The QElectroTech Team
+	This file is part of QElectroTech.
+
+	QElectroTech is free software: you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 2 of the License, or
+	(at your option) any later version.
+
+	QElectroTech is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <QObject>
+
+#include "elementscene.h"
+#include "partarc.h"
+#include "editorcommands.h"
+#include "eseventaddarc.h"
+
+/**
+ * @brief ESEventAddArc::ESEventAddArc
+ * @param scene
+ */
+ESEventAddArc::ESEventAddArc(ElementScene *scene) :
+	ESEventInterface(scene),
+	m_arc(nullptr),
+	m_inverted(false)
+{}
+
+/**
+ * @brief ESEventAddArc::~ESEventAddArc
+ */
+ESEventAddArc::~ESEventAddArc() {
+	if (m_running || m_abort)
+		delete m_arc;
+}
+
+/**
+ * @brief ESEventAddPolygon::mousePressEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddArc::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+	if (event -> button() == Qt::LeftButton) {
+		if(!m_running) m_running = true;
+		QPointF pos = m_scene->snapToGrid(event -> scenePos());
+
+		//create new arc
+		if (!m_arc) {
+			m_arc = new PartArc(m_editor, 0, m_scene);
+			m_arc -> setRect(QRectF(pos, pos));
+			m_arc -> setAngle(90);
+			m_arc -> setProperty("antialias", true);
+			m_origin = pos;
+			return true;
+		}
+
+		//Add arc to scene
+		m_arc -> setRect(m_arc->rect().normalized());
+		m_scene -> undoStack().push(new AddPartCommand(QObject::tr("Arc"), m_scene, m_arc));
+
+		//Set m_arc to nullptr for create new ellipse at next mouse press
+		m_arc = nullptr;
+
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @brief ESEventAddArc::mouseMoveEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddArc::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+	updateHelpCross(event -> scenePos());
+	if (!m_arc) return false;
+
+	m_mouse_pos = m_scene -> snapToGrid(event -> scenePos());
+	updateArc();
+
+	return true;
+}
+
+/**
+ * @brief ESEventAddArc::mouseReleaseEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddArc::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+	if (event -> button() == Qt::RightButton) {
+		if (m_arc) {delete m_arc; m_arc = nullptr;}
+		else        {m_running = false;}
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @brief ESEventAddArc::keyPressEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddArc::keyPressEvent(QKeyEvent *event) {
+	if (m_arc && event->key() == Qt::Key_Space) {
+		m_inverted = m_inverted ? false : true;
+		updateArc();
+		return true;
+	}
+
+	return (ESEventInterface::keyPressEvent(event));
+}
+
+/**
+ * @brief ESEventAddArc::updateArc
+ * Redraw the arc with curent value
+ */
+void ESEventAddArc::updateArc() {
+
+	qreal width  = (m_mouse_pos.x() - m_origin.x())*2;
+	if (width < 0) width *= -1;
+	qreal height = (m_mouse_pos.y() - m_origin.y())*2;
+	if (height < 0) height *= -1;
+
+	QPointF pos_ = m_origin;
+
+	//Draw arc inverted
+	if (m_inverted) {
+		//Adjust the start angle to be snapped at the origin point of draw
+		if (m_mouse_pos.y() > m_origin.y()) {
+
+			if (m_mouse_pos.x() > m_origin.x()) {
+				pos_.ry() -= height/2;
+				m_arc->setStartAngle(180);
+			}
+			else {
+				pos_.rx() -= width/2;
+				m_arc->setStartAngle(90);
+			}
+		}
+		else {
+			if (m_mouse_pos.x() > m_origin.x()) {
+				pos_.ry() -= height;
+				pos_.rx() -= width/2;
+				m_arc->setStartAngle(270);
+			}
+			else {
+				pos_.rx() -= width;
+				pos_.ry() -= height/2;
+				m_arc->setStartAngle(0);
+			}
+		}
+	}
+
+	//Draw arc non inverted
+	else {
+		//Adjust the start angle to be snapped at the origin point of draw
+		if (m_mouse_pos.y() > m_origin.y()) {
+
+			if (m_mouse_pos.x() > m_origin.x()) {
+				pos_.rx() -= width/2;
+				m_arc->setStartAngle(0);
+			}
+			else {
+				pos_.rx() -= width;
+				pos_.ry() -= height/2;
+				m_arc->setStartAngle(270);
+			}
+		}
+		else {
+			if (m_mouse_pos.x() > m_origin.x()) {
+				pos_.ry() -= height/2;
+				m_arc->setStartAngle(90);
+			}
+			else {
+				pos_.rx() -= width/2;
+				pos_.ry() -= height;
+				m_arc->setStartAngle(180);
+			}
+		}
+	}
+
+	m_arc -> setRect(QRectF(pos_, QSizeF(width, height)));
+}

Added: trunk/sources/editor/esevent/eseventaddarc.h
===================================================================
--- trunk/sources/editor/esevent/eseventaddarc.h	                        (rev 0)
+++ trunk/sources/editor/esevent/eseventaddarc.h	2014-11-09 21:58:46 UTC (rev 3470)
@@ -0,0 +1,51 @@
+/*
+	Copyright 2006-2014 The QElectroTech Team
+	This file is part of QElectroTech.
+
+	QElectroTech is free software: you can redistribute it and/or modify
+	it under the terms of the GNU General Public License as published by
+	the Free Software Foundation, either version 2 of the License, or
+	(at your option) any later version.
+
+	QElectroTech is distributed in the hope that it will be useful,
+	but WITHOUT ANY WARRANTY; without even the implied warranty of
+	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+	GNU General Public License for more details.
+
+	You should have received a copy of the GNU General Public License
+	along with QElectroTech.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#ifndef ESEVENTADDARC_H
+#define ESEVENTADDARC_H
+
+#include "eseventinterface.h"
+
+class ElementScene;
+class PartArc;
+class QGraphicsSceneMouseEvent;
+
+/**
+ * @brief The ESEventAddArc class
+ * This ESEvent manage creation of arc in an ElementScene
+ */
+class ESEventAddArc : public ESEventInterface
+{
+	public:
+		ESEventAddArc(ElementScene *scene);
+		virtual ~ESEventAddArc();
+
+		virtual bool mousePressEvent   (QGraphicsSceneMouseEvent *event);
+		virtual bool mouseMoveEvent    (QGraphicsSceneMouseEvent *event);
+		virtual bool mouseReleaseEvent (QGraphicsSceneMouseEvent *event);
+		virtual bool keyPressEvent     (QKeyEvent *event);
+
+	private:
+		void updateArc ();
+
+	private:
+		PartArc *m_arc;
+		QPointF  m_origin, m_mouse_pos;
+		bool     m_inverted;
+};
+
+#endif // ESEVENTADDARC_H

Modified: trunk/sources/editor/esevent/eseventaddellipse.cpp
===================================================================
--- trunk/sources/editor/esevent/eseventaddellipse.cpp	2014-11-09 09:25:37 UTC (rev 3469)
+++ trunk/sources/editor/esevent/eseventaddellipse.cpp	2014-11-09 21:58:46 UTC (rev 3470)
@@ -54,7 +54,6 @@
 		if (!m_ellipse) {
 			m_ellipse = new PartEllipse(m_editor, 0, m_scene);
 			m_ellipse -> setRect(QRectF(pos, pos));
-			m_ellipse -> setProperty("antialias", true);
 			m_origin = pos;
 			return true;
 		}

Modified: trunk/sources/editor/qetelementeditor.cpp
===================================================================
--- trunk/sources/editor/qetelementeditor.cpp	2014-11-09 09:25:37 UTC (rev 3469)
+++ trunk/sources/editor/qetelementeditor.cpp	2014-11-09 21:58:46 UTC (rev 3470)
@@ -45,6 +45,7 @@
 #include "eseventaddrect.h"
 #include "eseventaddellipse.h"
 #include "eseventaddpolygon.h"
+#include "eseventaddarc.h"
 
 #include <QMessageBox>
 /*
@@ -254,7 +255,7 @@
 	connect(add_ellipse,     SIGNAL(triggered()), this, SLOT(addEllipse()));
 	connect(add_polygon,     SIGNAL(triggered()), this, SLOT(addPolygon()));
 	connect(add_text,        SIGNAL(triggered()), ce_scene, SLOT(slot_addText()));
-	connect(add_arc,         SIGNAL(triggered()), ce_scene, SLOT(slot_addArc()));
+	connect(add_arc,         SIGNAL(triggered()), this, SLOT(addArc()));
 	connect(add_terminal,    SIGNAL(triggered()), ce_scene, SLOT(slot_addTerminal()));
 	connect(add_textfield,   SIGNAL(triggered()), ce_scene, SLOT(slot_addTextField()));
 
@@ -958,6 +959,14 @@
 }
 
 /**
+ * @brief QETElementEditor::addArc
+ * Set arc creation interface to scene
+ */
+void QETElementEditor::addArc() {
+	ce_scene -> setEventInterface(new ESEventAddArc(ce_scene));
+}
+
+/**
 	Lance l'assistant de creation d'un nouvel element.
 */
 void QETElementEditor::slot_new() {

Modified: trunk/sources/editor/qetelementeditor.h
===================================================================
--- trunk/sources/editor/qetelementeditor.h	2014-11-09 09:25:37 UTC (rev 3469)
+++ trunk/sources/editor/qetelementeditor.h	2014-11-09 21:58:46 UTC (rev 3470)
@@ -132,6 +132,7 @@
 		void addRect();
 		void addEllipse();
 		void addPolygon();
+		void addArc();
 
 	void slot_new();
 	void slot_open();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/