[qet] [4069] polygon editor : use QPropertyUndoCommand instead of ChangePartCommand

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


Revision: 4069
Author:   blacksun
Date:     2015-07-24 13:11:50 +0200 (Fri, 24 Jul 2015)
Log Message:
-----------
polygon editor : use QPropertyUndoCommand instead of ChangePartCommand

Modified Paths:
--------------
    trunk/sources/editor/editorcommands.cpp
    trunk/sources/editor/editorcommands.h
    trunk/sources/editor/graphicspart/partpolygon.cpp
    trunk/sources/editor/graphicspart/partpolygon.h
    trunk/sources/editor/polygoneditor.cpp
    trunk/sources/editor/polygoneditor.h

Modified: trunk/sources/editor/editorcommands.cpp
===================================================================
--- trunk/sources/editor/editorcommands.cpp	2015-07-23 10:52:56 UTC (rev 4068)
+++ trunk/sources/editor/editorcommands.cpp	2015-07-24 11:11:50 UTC (rev 4069)
@@ -362,40 +362,6 @@
 
 /**
 	Constructeur
-	@param p Polygone edite
-	@param o_points points avant le changement
-	@param n_points points apres le changement
-	@param parent QUndoCommand parent
-*/
-ChangePolygonPointsCommand::ChangePolygonPointsCommand(
-	PartPolygon *p,
-	const QVector<QPointF> &o_points,
-	const QVector<QPointF> &n_points,
-	QUndoCommand *parent
-) :
-	ElementEditionCommand(QObject::tr("modification points polygone", "undo caption"), 0, 0, parent),
-	polygon(p),
-	old_points(o_points),
-	new_points(n_points)
-{
-}
-
-/// Destructeur
-ChangePolygonPointsCommand::~ChangePolygonPointsCommand() {
-}
-
-/// Annule le changement
-void ChangePolygonPointsCommand::undo() {
-	polygon -> setPolygon(old_points);
-}
-
-/// Refait le changement
-void ChangePolygonPointsCommand::redo() {
-	polygon -> setPolygon(new_points);
-}
-
-/**
-	Constructeur
 	@param element_scene Element edite
 	@param before Listes des noms avant changement
 	@param after Listes des noms apres changement

Modified: trunk/sources/editor/editorcommands.h
===================================================================
--- trunk/sources/editor/editorcommands.h	2015-07-23 10:52:56 UTC (rev 4068)
+++ trunk/sources/editor/editorcommands.h	2015-07-24 11:11:50 UTC (rev 4069)
@@ -18,12 +18,10 @@
 #ifndef EDITOR_COMMANDS_H
 #define EDITOR_COMMANDS_H
 #include "customelementpart.h"
-#include "partpolygon.h"
 #include "elementview.h"
 #include "elementscene.h"
 #include "elementcontent.h"
 #include "qgimanager.h"
-#include <QtWidgets>
 
 /**
  * @brief The ElementEditionCommand class
@@ -202,32 +200,6 @@
 };
 
 /**
-	This command changes the points of a polygon when editing an electrical
-	element.
-*/
-class ChangePolygonPointsCommand : public ElementEditionCommand {
-	// constructors, destructor
-	public:
-	ChangePolygonPointsCommand(PartPolygon *, const QVector<QPointF> &, const QVector<QPointF> &, QUndoCommand * = 0);
-	virtual ~ChangePolygonPointsCommand();
-	private:
-	ChangePolygonPointsCommand(const ChangePolygonPointsCommand &);
-	
-	// methods
-	public:
-	virtual void undo();
-	virtual void redo();
-	
-	// attributes
-	/// Changed polygon
-	PartPolygon *polygon;
-	/// Former points
-	QVector<QPointF> old_points;
-	/// New points
-	QVector<QPointF> new_points;
-};
-
-/**
 	This command changes the translated names of an electrical element.
 */
 class ChangeNamesCommand : public ElementEditionCommand {

Modified: trunk/sources/editor/graphicspart/partpolygon.cpp
===================================================================
--- trunk/sources/editor/graphicspart/partpolygon.cpp	2015-07-23 10:52:56 UTC (rev 4068)
+++ trunk/sources/editor/graphicspart/partpolygon.cpp	2015-07-24 11:11:50 UTC (rev 4069)
@@ -199,6 +199,7 @@
 	if (m_polygon == polygon) return;
 	prepareGeometryChange();
 	m_polygon = polygon;
+	emit polygonChanged();
 }
 
 /**
@@ -239,6 +240,14 @@
 	}
 }
 
+void PartPolygon::setClosed(bool close)
+{
+	if (m_closed == close) return;
+	prepareGeometryChange();
+	m_closed = close;
+	emit closedChange();
+}
+
 /**
  * @brief PartPolygon::mousePressEvent
  * Handle mouse press event
@@ -274,6 +283,7 @@
 		QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos()));
 		prepareGeometryChange();
 		m_polygon.replace(m_handler_index, pos_);
+		emit polygonChanged();
 	}
 	else
 		CustomElementGraphicPart::mouseMoveEvent(event);

Modified: trunk/sources/editor/graphicspart/partpolygon.h
===================================================================
--- trunk/sources/editor/graphicspart/partpolygon.h	2015-07-23 10:52:56 UTC (rev 4068)
+++ trunk/sources/editor/graphicspart/partpolygon.h	2015-07-24 11:11:50 UTC (rev 4069)
@@ -45,6 +45,10 @@
 	private:
 		PartPolygon(const PartPolygon &);
 
+	signals:
+		void closedChange();
+		void polygonChanged();
+
 		// methods
 	public:
 		enum { Type = UserType + 1105 };
@@ -78,7 +82,7 @@
 		void removeLastPoint ();	
 
 		bool isClosed  () const {return m_closed;}
-		void setClosed (bool c) {m_closed = c;}
+		void setClosed (bool close);
 
 	protected:
 		virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);

Modified: trunk/sources/editor/polygoneditor.cpp
===================================================================
--- trunk/sources/editor/polygoneditor.cpp	2015-07-23 10:52:56 UTC (rev 4068)
+++ trunk/sources/editor/polygoneditor.cpp	2015-07-24 11:11:50 UTC (rev 4069)
@@ -18,9 +18,9 @@
 #include "polygoneditor.h"
 #include "partpolygon.h"
 #include "elementscene.h"
-#include "editorcommands.h"
 #include "qetmessagebox.h"
 #include "styleeditor.h"
+#include "QPropertyUndoCommand/qpropertyundocommand.h"
 
 /**
 	Constructeur
@@ -59,44 +59,39 @@
 }
 
 /**
-	Met a jour le polygone a partir des donnees du formulaire : points et etat ferme ou non
-*/
-void PolygonEditor::updatePolygon() {
-	updatePolygonPoints();
-	updatePolygonClosedState();
-}
-
-/**
 	Met a jour les points du polygone et cree un objet d'annulation
 */
-void PolygonEditor::updatePolygonPoints() {
+void PolygonEditor::updatePolygonPoints()
+{
 	if (!part) return;
-	QVector<QPointF> points = getPointsFromTree();
-	if (points.count() < 2) {
-		QET::QetMessageBox::warning(
-			this,
-			tr("Erreur", "message box title"),
-			tr("Le polygone doit comporter au moins deux points.", "message box content")
-		);
+	QPolygonF points = getPointsFromTree();
+	if (points.count() < 2)
+	{
+		QET::QetMessageBox::warning(this, tr("Erreur", "message box title"), tr("Le polygone doit comporter au moins deux points.", "message box content"));
 		return;
 	}
-	undoStack().push(new ChangePolygonPointsCommand(part, part -> polygon(), points));
+
+	if (points != part->polygon())
+	{
+		QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "polygon", part->property("polygon"), points);
+		undo->setText(tr("Modifier un polygone"));
+		undoStack().push(undo);
+	}
 }
 
 /**
 	Met a jour l'etat ferme ou non du polygone
 */
-void PolygonEditor::updatePolygonClosedState() {
+void PolygonEditor::updatePolygonClosedState()
+{
 	if (!part) return;
-	undoStack().push(
-		new ChangePartCommand(
-			tr("fermeture du polygone"),
-			part,
-			"closed",
-			QVariant(!close_polygon.isChecked()),
-			QVariant(close_polygon.isChecked())
-		)
-	);
+	bool close = close_polygon.isChecked();
+	if (close != part->isClosed())
+	{
+		QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "closed", part->property("closed"), close);
+		undo->setText(tr("Modifier un polygone"));
+		undoStack().push(undo);
+	}
 }
 
 /**
@@ -126,20 +121,35 @@
 	@param new_part Nouvelle primitive a editer
 	@return true si l'editeur a accepter d'editer la primitive, false sinon
 */
-bool PolygonEditor::setPart(CustomElementPart *new_part) {
-	if (!new_part) {
+bool PolygonEditor::setPart(CustomElementPart *new_part)
+{
+	if (!new_part)
+	{
+		if (part)
+		{
+			disconnect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm);
+			disconnect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm);
+		}
 		part = 0;
 		style_ -> setPart(0);
 		return(true);
 	}
-	if (PartPolygon *part_polygon = dynamic_cast<PartPolygon *>(new_part)) {
+	if (PartPolygon *part_polygon = dynamic_cast<PartPolygon *>(new_part))
+	{
+		if (part == part_polygon) return true;
+		if (part)
+		{
+			disconnect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm);
+			disconnect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm);
+		}
 		part = part_polygon;
 		style_ -> setPart(part);
 		updateForm();
+		connect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm);
+		connect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm);
 		return(true);
-	} else {
-		return(false);
 	}
+	return(false);
 }
 
 /**

Modified: trunk/sources/editor/polygoneditor.h
===================================================================
--- trunk/sources/editor/polygoneditor.h	2015-07-23 10:52:56 UTC (rev 4068)
+++ trunk/sources/editor/polygoneditor.h	2015-07-24 11:11:50 UTC (rev 4069)
@@ -57,7 +57,6 @@
 	QVector<QPointF> getPointsFromTree();
 	
 	public slots:
-	void updatePolygon();
 	void updatePolygonPoints();
 	void updatePolygonClosedState();
 	void updateForm();


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