[qet] [3466] Element editor: add polygon is managed by an esevent

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


Revision: 3466
Author:   blacksun
Date:     2014-11-08 12:14:56 +0100 (Sat, 08 Nov 2014)
Log Message:
-----------
Element editor: add polygon is managed by an esevent

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

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

Modified: trunk/sources/editor/elementscene.cpp
===================================================================
--- trunk/sources/editor/elementscene.cpp	2014-11-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/elementscene.cpp	2014-11-08 11:14:56 UTC (rev 3466)
@@ -49,7 +49,6 @@
 	decorator_(0)
 {
 	setItemIndexMethod(NoIndex);
-	current_polygon = NULL;
 	setGrid(1, 1);
 	initPasteArea();
 	undo_stack.setClean();
@@ -61,6 +60,7 @@
 /// Destructeur
 ElementScene::~ElementScene() {
 	delete decorator_lock_;
+	if (m_event_interface) delete m_event_interface;
 }
 
 /**
@@ -80,15 +80,6 @@
 }
 
 /**
-	Passe la scene en mode "ajout de polygone"
-*/
-void ElementScene::slot_addPolygon() {
-	behavior = Polygon;
-	if (m_event_interface) delete m_event_interface; m_event_interface = nullptr;
-}
-
-
-/**
 	Passe la scene en mode "ajout de texte statique"
 */
 void ElementScene::slot_addText() {
@@ -139,7 +130,6 @@
 	QPointF event_pos = e -> scenePos();
 	if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos);
 	
-	if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL;
 	if (behavior == PasteArea) {
 		QRectF current_rect(paste_area_ -> rect());
 		current_rect.moveCenter(event_pos);
@@ -148,8 +138,6 @@
 	}
 	
 	QRectF temp_rect;
-	QPointF temp_point;
-	QPolygonF temp_polygon;
 	if (e -> buttons() & Qt::LeftButton) {
 		switch(behavior) {
 			case Arc:
@@ -157,23 +145,12 @@
 				temp_rect.setBottomRight(event_pos);
 				current_arc -> setRect(temp_rect);
 				break;
-			case Polygon:
-				if (current_polygon == NULL) break;
-				temp_polygon = current_polygon -> polygon();
-				temp_polygon.pop_back();
-				temp_polygon << event_pos;
-				current_polygon -> setPolygon(temp_polygon);
-				break;
 			case Normal:
 			default:
 				QGraphicsScene::mouseMoveEvent(e);
 		}
-	} else if (behavior == Polygon && current_polygon != NULL) {
-		temp_polygon = current_polygon -> polygon();
-		temp_polygon.pop_back();
-		temp_polygon << event_pos;
-		current_polygon -> setPolygon(temp_polygon);
-	} else QGraphicsScene::mouseMoveEvent(e);
+	}
+	else QGraphicsScene::mouseMoveEvent(e);
 }
 
 /**
@@ -194,8 +171,6 @@
 	QPointF event_pos = e -> scenePos();
 	if (mustSnapToGrid(e)) event_pos = snapToGrid(event_pos);
 	
-	if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL;
-	QPolygonF temp_polygon;
 	if (e -> button() & Qt::LeftButton) {
 		switch(behavior) {
 			case Arc:
@@ -203,16 +178,6 @@
 				current_arc -> setRect(QRectF(event_pos, QSizeF(0.0, 0.0)));
 				current_arc -> setProperty("antialias", true);
 				break;
-			case Polygon:
-				if (current_polygon == NULL) {
-					current_polygon = new PartPolygon(element_editor, 0, this);
-					temp_polygon = QPolygonF(0);
-				} else temp_polygon = current_polygon -> polygon();
-				// au debut, on insere deux points
-				if (!temp_polygon.count()) temp_polygon << event_pos;
-				temp_polygon << event_pos;
-				current_polygon -> setPolygon(temp_polygon);
-				break;
 			case Normal:
 			default:
 				QGraphicsScene::mousePressEvent(e);
@@ -242,7 +207,6 @@
 	PartTerminal *terminal;
 	PartText *text;
 	PartTextField *textfield;
-	if (behavior != Polygon && current_polygon != NULL) current_polygon = NULL;
 	
 	if (behavior == PasteArea) {
 		defined_paste_area_ = paste_area_ -> rect();
@@ -288,16 +252,25 @@
 				QGraphicsScene::mouseReleaseEvent(e);
 				moving_parts_ = false;
 		}
-	} else if (e -> button() & Qt::RightButton) {
-		if (behavior == Polygon && current_polygon != NULL) {
-			undo_stack.push(new AddPartCommand(tr("polygone"), this, current_polygon));
-			current_polygon = NULL;
-			emit(partsAdded());
-			endCurrentBehavior(e);
-		} else QGraphicsScene::mouseReleaseEvent(e);
-	} else QGraphicsScene::mouseReleaseEvent(e);
+	}
+	else QGraphicsScene::mouseReleaseEvent(e);
 }
 
+void ElementScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+	if (m_event_interface) {
+		if (m_event_interface -> mouseDoubleClickEvent(event)) {
+			if (m_event_interface->isFinish()) {
+				emit(partsAdded());
+				emit(needNormalMode());
+				delete m_event_interface; m_event_interface = nullptr;
+			}
+			return;
+		}
+	}
+
+	QGraphicsScene::mouseDoubleClickEvent(event);
+}
+
 /**
  * @brief ElementScene::keyPressEvent
  * manage key press event

Modified: trunk/sources/editor/elementscene.h
===================================================================
--- trunk/sources/editor/elementscene.h	2014-11-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/elementscene.h	2014-11-08 11:14:56 UTC (rev 3466)
@@ -28,7 +28,6 @@
 class ElementEditionCommand;
 class ElementPrimitiveDecorator;
 class QETElementEditor;
-class PartPolygon;
 class PartArc;
 class ESEventInterface;
 class QKeyEvent;
@@ -43,7 +42,7 @@
 	
 	// enum
 	public:
-	enum Behavior { Normal, Circle, Polygon, Text, Terminal, Arc, TextField, PasteArea };
+	enum Behavior { Normal, Circle, Text, Terminal, Arc, TextField, PasteArea };
 	enum ItemOption {
 		SortByZValue = 1,
 		IncludeTerminals = 2,
@@ -88,7 +87,6 @@
 		/// Variables related to drawing
 		ESEventInterface *m_event_interface;
 		Behavior behavior;
-		PartPolygon *current_polygon;
 		PartArc *current_arc;
 		QETElementEditor *element_editor;
 	
@@ -141,10 +139,11 @@
 	QETElementEditor* editor() const;
 	
 	protected:
-		virtual void mouseMoveEvent    (QGraphicsSceneMouseEvent *);
-		virtual void mousePressEvent   (QGraphicsSceneMouseEvent *);
-		virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent *);
-		virtual void keyPressEvent     (QKeyEvent *event);
+		virtual void mouseMoveEvent         (QGraphicsSceneMouseEvent *);
+		virtual void mousePressEvent       (QGraphicsSceneMouseEvent *);
+		virtual void mouseReleaseEvent     (QGraphicsSceneMouseEvent *);
+		virtual void mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event);
+		virtual void keyPressEvent         (QKeyEvent *event);
 
 	virtual void drawForeground(QPainter *, const QRectF &);
 	virtual void endCurrentBehavior(const QGraphicsSceneMouseEvent *);
@@ -165,7 +164,6 @@
 	public slots:
 	void slot_move();
 	void slot_addCircle();
-	void slot_addPolygon();
 	void slot_addText();
 	void slot_addArc();
 	void slot_addTerminal();

Modified: trunk/sources/editor/esevent/eseventaddellipse.h
===================================================================
--- trunk/sources/editor/esevent/eseventaddellipse.h	2014-11-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/esevent/eseventaddellipse.h	2014-11-08 11:14:56 UTC (rev 3466)
@@ -25,6 +25,10 @@
 class PartEllipse;
 class QGraphicsSceneMouseEvent;
 
+/**
+ * @brief The ESEventAddEllipse class
+ * This ESEvent manage creation of ellpise in an ElementScene
+ */
 class ESEventAddEllipse : public ESEventInterface
 {
 	public:

Added: trunk/sources/editor/esevent/eseventaddpolygon.cpp
===================================================================
--- trunk/sources/editor/esevent/eseventaddpolygon.cpp	                        (rev 0)
+++ trunk/sources/editor/esevent/eseventaddpolygon.cpp	2014-11-08 11:14:56 UTC (rev 3466)
@@ -0,0 +1,117 @@
+/*
+	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 "eseventaddpolygon.h"
+#include "elementscene.h"
+#include "partpolygon.h"
+#include "editorcommands.h"
+
+/**
+ * @brief ESEventAddPolygon::ESEventAddPolygon
+ * @param scene
+ */
+ESEventAddPolygon::ESEventAddPolygon(ElementScene *scene) :
+	ESEventInterface(scene),
+	m_polygon(nullptr)
+{}
+
+/**
+ * @brief ESEventAddPolygon::~ESEventAddPolygon
+ */
+ESEventAddPolygon::~ESEventAddPolygon() {
+	if (m_running || m_abort)
+		delete m_polygon;
+}
+
+/**
+ * @brief ESEventAddPolygon::mousePressEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddPolygon::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+	if (event -> button() == Qt::LeftButton) {
+		if(!m_running) m_running = true;
+		QPointF pos = m_scene->snapToGrid(event -> scenePos());
+
+		//create new polygon
+		if (!m_polygon) {
+			m_polygon = new PartPolygon(m_editor, 0, m_scene);
+			m_polygon -> addPoint(pos);
+		}
+
+		m_polygon -> addPoint(pos);
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @brief ESEventAddPolygon::mouseMoveEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+	updateHelpCross(event -> scenePos());
+	if (!m_polygon) return false;
+
+	m_polygon -> setLastPoint(m_scene -> snapToGrid(event -> scenePos()));
+	return true;
+}
+
+/**
+ * @brief ESEventAddPolygon::mouseReleaseEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddPolygon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+	if (event -> button() == Qt::RightButton) {
+		if (m_polygon) {
+			m_polygon -> removeLastPoint();
+
+			if (m_polygon -> polygon().size() > 1)
+				{ m_polygon -> setLastPoint(m_scene -> snapToGrid(event -> scenePos())); }
+			else
+				{ delete m_polygon; m_polygon = nullptr; }
+		}
+		else
+			{ m_running = false; }
+
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @brief ESEventAddPolygon::mouseDoubleClickEvent
+ * @param event
+ * @return
+ */
+bool ESEventAddPolygon::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+	if (event -> button() == Qt::LeftButton) {
+		if (m_polygon) {
+			m_polygon -> addPoint(m_scene -> snapToGrid(event -> scenePos()));
+			m_scene   -> undoStack().push(new AddPartCommand(QObject::tr("Polygone"), m_scene, m_polygon));
+
+			//Set m_polygon to nullptr for create new polygon at next mouse press
+			m_polygon = nullptr;
+			return true;
+		}
+	}
+	return false;
+}

Added: trunk/sources/editor/esevent/eseventaddpolygon.h
===================================================================
--- trunk/sources/editor/esevent/eseventaddpolygon.h	                        (rev 0)
+++ trunk/sources/editor/esevent/eseventaddpolygon.h	2014-11-08 11:14:56 UTC (rev 3466)
@@ -0,0 +1,46 @@
+/*
+	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 ESEVENTADDPOLYGON_H
+#define ESEVENTADDPOLYGON_H
+
+#include "eseventinterface.h"
+
+class ElementScene;
+class PartPolygon;
+class QGraphicsSceneMouseEvent;
+
+/**
+ * @brief The ESEventAddPolygon class
+ * This ESEvent manage creation of polygon in an ElementScene
+ */
+class ESEventAddPolygon : public ESEventInterface
+{
+	public:
+		ESEventAddPolygon(ElementScene *scene);
+		virtual ~ESEventAddPolygon();
+
+		virtual bool mousePressEvent       (QGraphicsSceneMouseEvent *event);
+		virtual bool mouseMoveEvent        (QGraphicsSceneMouseEvent *event);
+		virtual bool mouseReleaseEvent     (QGraphicsSceneMouseEvent *event);
+		virtual bool mouseDoubleClickEvent (QGraphicsSceneMouseEvent *event);
+
+	private:
+		PartPolygon *m_polygon;
+};
+
+#endif // ESEVENTADDPOLYGON_H

Modified: trunk/sources/editor/partpolygon.cpp
===================================================================
--- trunk/sources/editor/partpolygon.cpp	2014-11-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/partpolygon.cpp	2014-11-08 11:14:56 UTC (rev 3466)
@@ -174,6 +174,45 @@
 }
 
 /**
+ * @brief PartPolygon::addPoint
+ * Add new point to polygon
+ * @param point
+ */
+void PartPolygon::addPoint(const QPointF &point) {
+	QPolygonF poly = polygon();
+	poly << point;
+	setPolygon(poly);
+}
+
+/**
+ * @brief PartPolygon::setLastPoint
+ * Set the last point of polygon to @point
+ * @param point
+ */
+void PartPolygon::setLastPoint(const QPointF &point) {
+	QPolygonF poly = polygon();
+
+	if (poly.size())
+		poly.pop_back();
+
+	poly << point;
+	setPolygon(poly);
+}
+
+/**
+ * @brief PartPolygon::removeLastPoint
+ * Remove the last point of polygon
+ */
+void PartPolygon::removeLastPoint() {
+	QPolygonF poly = polygon();
+
+	if (poly.size())
+		poly.pop_back();
+
+	setPolygon(poly);
+}
+
+/**
 	@return le rectangle delimitant cette partie.
 */
 QRectF PartPolygon::boundingRect() const {

Modified: trunk/sources/editor/partpolygon.h
===================================================================
--- trunk/sources/editor/partpolygon.h	2014-11-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/partpolygon.h	2014-11-08 11:14:56 UTC (rev 3466)
@@ -58,6 +58,10 @@
 	virtual void handleUserTransformation(const QRectF &, const QRectF &);
 	virtual QET::ScalingMethod preferredScalingMethod() const;
 
+		void addPoint        (const QPointF &point);
+		void setLastPoint    (const QPointF &point);
+		void removeLastPoint ();
+
 	///PROPERTY
 	// Closed (join the first and last point by a line)
 	Q_PROPERTY(bool closed READ isClosed WRITE setClosed)

Modified: trunk/sources/editor/qetelementeditor.cpp
===================================================================
--- trunk/sources/editor/qetelementeditor.cpp	2014-11-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/qetelementeditor.cpp	2014-11-08 11:14:56 UTC (rev 3466)
@@ -44,6 +44,7 @@
 #include "eseventaddline.h"
 #include "eseventaddrect.h"
 #include "eseventaddellipse.h"
+#include "eseventaddpolygon.h"
 
 #include <QMessageBox>
 /*
@@ -251,7 +252,7 @@
 	connect(add_line,        SIGNAL(triggered()), this, SLOT(addLine()));
 	connect(add_rectangle,   SIGNAL(triggered()), this, SLOT(addRect()));
 	connect(add_ellipse,     SIGNAL(triggered()), this, SLOT(addEllipse()));
-	connect(add_polygon,     SIGNAL(triggered()), ce_scene, SLOT(slot_addPolygon()));
+	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_terminal,    SIGNAL(triggered()), ce_scene, SLOT(slot_addTerminal()));
@@ -949,6 +950,14 @@
 }
 
 /**
+ * @brief QETElementEditor::addPolygon
+ * Set polygon creation interface to scene
+ */
+void QETElementEditor::addPolygon() {
+	ce_scene -> setEventInterface(new ESEventAddPolygon(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-07 22:16:40 UTC (rev 3465)
+++ trunk/sources/editor/qetelementeditor.h	2014-11-08 11:14:56 UTC (rev 3466)
@@ -23,8 +23,10 @@
 #include "elementscene.h"
 #include "orientationset.h"
 #include "elementslocation.h"
+
 class ElementItemEditor;
 class ElementView;
+
 /**
 	This class represents an element editor, allowing users to draw, change and
 	configure a particular electrical element.
@@ -129,6 +131,7 @@
 		void addLine();
 		void addRect();
 		void addEllipse();
+		void addPolygon();
 
 	void slot_new();
 	void slot_open();


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