[qet] [4074] QetShapeItem : use QPropertyUndoCommand instead of QetShapeGeometryCommand

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


Revision: 4074
Author:   blacksun
Date:     2015-07-27 11:20:38 +0200 (Mon, 27 Jul 2015)
Log Message:
-----------
QetShapeItem : use QPropertyUndoCommand instead of QetShapeGeometryCommand
Remove the class : QetShapeGeometryCommand

Modified Paths:
--------------
    trunk/sources/qetgraphicsitem/qetshapeitem.cpp
    trunk/sources/qetgraphicsitem/qetshapeitem.h

Removed Paths:
-------------
    trunk/sources/undocommand/qetshapegeometrycommand.cpp
    trunk/sources/undocommand/qetshapegeometrycommand.h

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2015-07-24 13:41:51 UTC (rev 4073)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2015-07-27 09:20:38 UTC (rev 4074)
@@ -21,7 +21,7 @@
 #include "qet.h"
 #include "shapegraphicsitempropertieswidget.h"
 #include "PropertiesEditor/propertieseditordialog.h"
-#include "qetshapegeometrycommand.h"
+#include "QPropertyUndoCommand/qpropertyundocommand.h"
 
 /**
  * @brief QetShapeItem::QetShapeItem
@@ -39,7 +39,6 @@
 	m_P2 (p2),
 	m_hovered(false),
 	m_mouse_grab_handler(false),
-	m_undo_command(nullptr),
 	m_handler(10)
 {
 	if (type == Polyline) m_polygon << m_P1 << m_P2;
@@ -47,10 +46,7 @@
 	setAcceptHoverEvents(true);
 }
 
-QetShapeItem::~QetShapeItem()
-{
-	if (m_undo_command) delete m_undo_command;
-}
+QetShapeItem::~QetShapeItem() {}
 
 /**
  * @brief QetShapeItem::setStyle
@@ -83,22 +79,21 @@
 		prepareGeometryChange();
 		m_P2 = P2;
 	}
-	setTransformOriginPoint(boundingRect().center());
 }
 
 /**
  * @brief QetShapeItem::setLine
  * Set item geometry to line (only available for line shape)
  * @param line
+ * @return  : true when shape is a Line, else false
  */
-void QetShapeItem::setLine(const QLineF &line)
+bool QetShapeItem::setLine(const QLineF &line)
 {
-	if (Q_LIKELY(m_shapeType == Line))
-	{
-		prepareGeometryChange();
-		m_P1 = line.p1();
-		m_P2 = line.p2();
-	}
+	if (Q_UNLIKELY(m_shapeType != Line)) return false;
+	prepareGeometryChange();
+	m_P1 = line.p1();
+	m_P2 = line.p2();
+	return true;
 }
 
 /**
@@ -138,8 +133,7 @@
  * @brief QetShapeItem::pointCount
  * @return the number of point in the polygon
  */
-int QetShapeItem::pointsCount() const
-{
+int QetShapeItem::pointsCount() const {
 	return m_polygon.size();
 }
 
@@ -148,10 +142,10 @@
  * Add a new point to the curent polygon
  * @param P the new point.
  */
-void QetShapeItem::setNextPoint(QPointF P) {
+void QetShapeItem::setNextPoint(QPointF P)
+{
 	prepareGeometryChange();
 	m_polygon.append(Diagram::snapToGrid(P));
-	setTransformOriginPoint(boundingRect().center());
 }
 
 /**
@@ -295,10 +289,8 @@
  * @param event
  */
 void QetShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
-	Q_UNUSED(event);
-
 	m_hovered = true;
-	update();
+	QetGraphicsItem::hoverEnterEvent(event);
 }
 
 /**
@@ -307,10 +299,8 @@
  * @param event
  */
 void QetShapeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
-	Q_UNUSED(event);
-
 	m_hovered = false;
-	update();
+	QetGraphicsItem::hoverLeaveEvent(event);
 }
 
 /**
@@ -337,14 +327,9 @@
 		{
 				//User click on an handler
 			m_mouse_grab_handler = true;
-
-			switch (m_shapeType)
-			{
-				case Line:      m_undo_command = new QetShapeGeometryCommand(this, QLineF(m_P1, m_P2)); break;
-				case Rectangle: m_undo_command = new QetShapeGeometryCommand(this, QRectF(m_P1, m_P2)); break;
-				case Ellipse:   m_undo_command = new QetShapeGeometryCommand(this, QRectF(m_P1, m_P2)); break;
-				case Polyline:  m_undo_command = new QetShapeGeometryCommand(this, m_polygon);          break;
-			}
+			m_old_P1 = m_P1;
+			m_old_P2 = m_P2;
+			m_old_polygon = m_polygon;
 			return;
 		}
 	}
@@ -367,26 +352,19 @@
 
 		switch (m_shapeType)
 		{
-			case Line: {
+			case Line:
 				prepareGeometryChange();
 				m_vector_index == 0 ? m_P1 = new_pos : m_P2 = new_pos;
-			}
 				break;
 
-			case Rectangle:
-				setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
+			case Rectangle: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break;
+			case Ellipse:   setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break;
 
-			case Ellipse:
-				setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
-				break;
-
-			case Polyline: {
+			case Polyline:
 				prepareGeometryChange();
 				m_polygon.replace(m_vector_index, new_pos);
-			}
 				break;
 		}	//End switch
-
 		return;
 	}
 
@@ -403,21 +381,29 @@
 	if (m_mouse_grab_handler)
 	{
 		m_mouse_grab_handler = false;
-		switch(m_shapeType)
-		{
-			case Line:      m_undo_command->setNewLine(QLineF(m_P1, m_P2)); break;
-			case Rectangle: m_undo_command->setNewRect(QRectF(m_P1, m_P2)); break;
-			case Ellipse:   m_undo_command->setNewRect(QRectF(m_P1, m_P2)); break;
-			case Polyline : m_undo_command->setNewPolygon(m_polygon);       break;
-		}
-
 		if (diagram())
 		{
-			diagram()->undoStack().push(m_undo_command);
-			m_undo_command = nullptr;
+			QPropertyUndoCommand *undo = nullptr;
+			if ((m_shapeType & (Line | Rectangle | Ellipse)) && (m_P1 != m_old_P1 || m_P2 != m_old_P2))
+			{
+				switch(m_shapeType)
+				{
+					case Line:      undo = new QPropertyUndoCommand(this, "line",QLineF(m_old_P1, m_old_P2), QLineF(m_P1, m_P2)); break;
+					case Rectangle: undo = new QPropertyUndoCommand(this, "rect",QRectF(m_old_P1, m_old_P2), QRectF(m_P1, m_P2)); break;
+					case Ellipse:   undo = new QPropertyUndoCommand(this, "rect",QRectF(m_old_P1, m_old_P2), QRectF(m_P1, m_P2)); break;
+					case Polyline: break;
+				}
+				if (undo) undo->enableAnimation();
+			}
+			else if (m_shapeType == Polyline && (m_polygon != m_old_polygon))
+				undo = new QPropertyUndoCommand(this, "polygon", m_old_polygon, m_polygon);
+
+			if(undo)
+			{
+				undo->setText(tr("Modifier %1").arg(name()));
+				diagram()->undoStack().push(undo);
+			}
 		}
-		else
-			delete m_undo_command;
 	}
 
 	QetGraphicsItem::mouseReleaseEvent(event);
@@ -496,23 +482,14 @@
  * @param filepath file path of the the dxf document
  * @return true if draw success
  */
-bool QetShapeItem::toDXF(const QString &filepath) {
-	switch (m_shapeType) {
-		case Line:
-			Createdxf::drawLine(filepath, QLineF(mapToScene(m_P1), mapToScene(m_P2)), 0);
-			return true;
-			break;
-		case Rectangle:
-			Createdxf::drawRectangle(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0);
-			return true;
-			break;
-		case Ellipse:
-			Createdxf::drawEllipse(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0);
-			return true;
-			break;
-		default:
-			return false;
-			break;
+bool QetShapeItem::toDXF(const QString &filepath)
+{
+	switch (m_shapeType)
+	{
+		case Line:      Createdxf::drawLine     (filepath, QLineF(mapToScene(m_P1), mapToScene(m_P2)), 0);              return true;
+		case Rectangle: Createdxf::drawRectangle(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); return true;
+		case Ellipse:   Createdxf::drawEllipse  (filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); return true;
+		default: return false;
 	}
 }
 
@@ -532,14 +509,12 @@
  * @brief QetShapeItem::name
  * @return the name of the curent shape.
  */
-QString QetShapeItem::name() const
-{
-	switch (m_shapeType)
-	{
-		case Line:	    return tr("une ligne");	    break;
-		case Rectangle:	return tr("un rectangle");	break;
-		case Ellipse:	return tr("une éllipse");	break;
-		case Polyline:	return tr("une polyligne");	break;
-		default:	    return tr("une shape");	    break;
+QString QetShapeItem::name() const {
+	switch (m_shapeType) {
+		case Line:	    return tr("une ligne");
+		case Rectangle:	return tr("un rectangle");
+		case Ellipse:	return tr("une éllipse");
+		case Polyline:	return tr("une polyligne");
+		default:	    return tr("une shape");
 	}
 }

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.h	2015-07-24 13:41:51 UTC (rev 4073)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.h	2015-07-27 09:20:38 UTC (rev 4074)
@@ -23,7 +23,6 @@
 
 class QDomElement;
 class QDomDocument;
-class QetShapeGeometryCommand;
 
 /**
  * @brief The QetShapeItem class
@@ -34,15 +33,19 @@
 {
 	Q_OBJECT
 
+		Q_PROPERTY(QRectF rect READ rect WRITE setRect)
+		Q_PROPERTY(QLineF line READ line WRITE setLine)
+		Q_PROPERTY(QPolygonF polygon READ polygon WRITE setPolygon)
+
 	signals:
 		void styleChanged();
 
 	public:
 		Q_ENUMS(ShapeType)
-		enum ShapeType {Line	  =0,
-						Rectangle =1,
-						Ellipse	  =2,
-						Polyline  =3 };
+		enum ShapeType {Line	  =1,
+						Rectangle =2,
+						Ellipse	  =4,
+						Polyline  =8 };
 
 		enum { Type = UserType + 1008 };
 
@@ -65,8 +68,11 @@
 		virtual QString name() const;
 
 		void setP2      (const QPointF &P2);
-		void setLine    (const QLineF &line);
+		QLineF line() const{return QLineF(m_P1, m_P2);}
+		bool setLine    (const QLineF &line);
+		QRectF rect() const{return QRectF(m_P1, m_P2);}
 		bool setRect    (const QRectF &rect);
+		QPolygonF polygon() const {return m_polygon;}
 		bool setPolygon (const QPolygonF &polygon);
 
 			//Methods available for polygon shape
@@ -89,12 +95,11 @@
 	private:
 		ShapeType    m_shapeType;
 		Qt::PenStyle m_shapeStyle;
-		QPointF		 m_P1, m_P2;
-		QPolygonF	 m_polygon;
+		QPointF		 m_P1, m_P2, m_old_P1, m_old_P2;
+		QPolygonF	 m_polygon, m_old_polygon;
 		bool         m_hovered,
 					 m_mouse_grab_handler;
 		int			 m_vector_index;
-		QetShapeGeometryCommand *m_undo_command;
 		QetGraphicsHandlerUtility m_handler;
 };
 #endif // QETSHAPEITEM_H

Deleted: trunk/sources/undocommand/qetshapegeometrycommand.cpp
===================================================================
--- trunk/sources/undocommand/qetshapegeometrycommand.cpp	2015-07-24 13:41:51 UTC (rev 4073)
+++ trunk/sources/undocommand/qetshapegeometrycommand.cpp	2015-07-27 09:20:38 UTC (rev 4074)
@@ -1,122 +0,0 @@
-/*
-        Copyright 2006-2015 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 "qetshapegeometrycommand.h"
-#include "qetshapeitem.h"
-#include <QObject>
-
-/**
- * @brief QetShapeGeometryCommand::QetShapeGeometryCommand
- * Constructor for a line shape
- * @param item : item to change the geometry
- * @param old_line : old line
- * @param parent : parent undo command
- */
-QetShapeGeometryCommand::QetShapeGeometryCommand(QetShapeItem *item, const QLineF &old_line, QUndoCommand *parent) :
-    QUndoCommand(parent),
-    m_shape_item(item),
-	m_old_line(old_line)
-{
-    setText(QObject::tr("Modifier la géometrie de : %1").arg(m_shape_item->name()));
-}
-
-/**
- * @brief QetShapeGeometryCommand::QetShapeGeometryCommand
- * Constructor for a rectangle or ellipse shape
- * @param item : item to change the geometry
- * @param old_rect : old rectangle
- * @param parent : parent undo command
- */
-QetShapeGeometryCommand::QetShapeGeometryCommand(QetShapeItem *item, const QRectF &old_rect, QUndoCommand *parent):
-    QUndoCommand(parent),
-	m_shape_item(item),
-	m_old_rect(old_rect)
-{
-    setText(QObject::tr("Modifier la géometrie de : %1").arg(m_shape_item->name()));
-}
-
-/**
- * @brief QetShapeGeometryCommand::QetShapeGeometryCommand
- * Constructor for polygon shape
- * @param item : item to change the geometry
- * @param old_polygon : old polygon
- * @param parent : parent undo command
- */
-QetShapeGeometryCommand::QetShapeGeometryCommand(QetShapeItem *item, const QPolygonF &old_polygon, QUndoCommand *parent):
-    QUndoCommand(parent),
-	m_shape_item(item),
-	m_old_polygon(old_polygon)
-{
-    setText(QObject::tr("Modifier la géometrie de : %1").arg(m_shape_item->name()));
-}
-
-/**
- * @brief QetShapeGeometryCommand::mergeWith
- * Try to merge this undo command with @other
- * @param other
- * @return true if the two command was merged
- */
-bool QetShapeGeometryCommand::mergeWith(const QUndoCommand *other)
-{
-    if (other->id() != id() || other->childCount()) return false;
-    const QetShapeGeometryCommand *other_undo = static_cast<const QetShapeGeometryCommand*>(other);
-    if (other_undo->m_shape_item != m_shape_item) return false;
-
-    switch (m_shape_item->shapeType())
-    {
-		case QetShapeItem::Line:      m_new_line = other_undo->m_new_line;       break;
-		case QetShapeItem::Rectangle: m_new_rect = other_undo->m_new_rect;       break;
-		case QetShapeItem::Ellipse:   m_new_rect = other_undo->m_new_rect;       break;
-		case QetShapeItem::Polyline:  m_new_polygon = other_undo->m_new_polygon; break;
-    }
-
-    return true;
-}
-
-/**
- * @brief QetShapeGeometryCommand::redo
- * Redo this command
- */
-void QetShapeGeometryCommand::redo()
-{
-    switch (m_shape_item->shapeType())
-    {
-		case QetShapeItem::Line:      m_shape_item->setLine(m_new_line);       break;
-		case QetShapeItem::Rectangle: m_shape_item->setRect(m_new_rect);       break;
-		case QetShapeItem::Ellipse:   m_shape_item->setRect(m_new_rect);       break;
-		case QetShapeItem::Polyline:  m_shape_item->setPolygon(m_new_polygon); break;
-    }
-
-    QUndoCommand::redo();
-}
-
-/**
- * @brief QetShapeGeometryCommand::undo
- * Undo this command
- */
-void QetShapeGeometryCommand::undo()
-{
-    switch (m_shape_item->shapeType())
-    {
-		case QetShapeItem::Line:      m_shape_item->setLine(m_old_line);       break;
-		case QetShapeItem::Rectangle: m_shape_item->setRect(m_old_rect);       break;
-		case QetShapeItem::Ellipse:   m_shape_item->setRect(m_old_rect);       break;
-		case QetShapeItem::Polyline:  m_shape_item->setPolygon(m_old_polygon); break;
-    }
-
-    QUndoCommand::undo();
-}

Deleted: trunk/sources/undocommand/qetshapegeometrycommand.h
===================================================================
--- trunk/sources/undocommand/qetshapegeometrycommand.h	2015-07-24 13:41:51 UTC (rev 4073)
+++ trunk/sources/undocommand/qetshapegeometrycommand.h	2015-07-27 09:20:38 UTC (rev 4074)
@@ -1,54 +0,0 @@
-/*
-        Copyright 2006-2015 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 QETSHAPEGEOMETRYCOMMAND_H
-#define QETSHAPEGEOMETRYCOMMAND_H
-
-#include <QUndoCommand>
-#include <QLineF>
-#include <QPolygonF>
-#include <QRectF>
-class QetShapeItem;
-
-/**
- * @brief The QetShapeGeometryCommand class
- * This undo command class manage the geometry change of a QetShapeItem.
- */
-class QetShapeGeometryCommand : public QUndoCommand
-{
-    public:
-		QetShapeGeometryCommand(QetShapeItem *item, const QLineF &old_line, QUndoCommand *parent = nullptr);
-		QetShapeGeometryCommand(QetShapeItem *item, const QRectF &old_rect, QUndoCommand *parent = nullptr);
-		QetShapeGeometryCommand(QetShapeItem *item, const QPolygonF &old_polygon, QUndoCommand *parent = nullptr);
-
-		void setNewLine    (const QLineF &new_line)       {m_new_line = new_line;}
-		void setNewRect    (const QRectF &new_rect)       {m_new_rect = new_rect;}
-		void setNewPolygon (const QPolygonF &new_polygon) {m_new_polygon = new_polygon;}
-
-        int id() const {return 5;}
-        bool mergeWith(const QUndoCommand *other);
-        void redo();
-        void undo();
-
-    private:
-        QetShapeItem *m_shape_item;
-		QLineF m_old_line, m_new_line;
-        QPolygonF m_old_polygon, m_new_polygon;
-        QRectF m_old_rect, m_new_rect;
-};
-
-#endif // QETSHAPEGEOMETRYCOMMAND_H


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