[qet] [4067] ellipse editor : use QPropertyUndoCommand instead of ChangePartCommand

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


Revision: 4067
Author:   blacksun
Date:     2015-07-23 11:57:09 +0200 (Thu, 23 Jul 2015)
Log Message:
-----------
ellipse editor : use QPropertyUndoCommand instead of ChangePartCommand

Modified Paths:
--------------
    trunk/sources/editor/arceditor.cpp
    trunk/sources/editor/ellipseeditor.cpp
    trunk/sources/editor/ellipseeditor.h
    trunk/sources/editor/graphicspart/abstractpartellipse.cpp
    trunk/sources/editor/graphicspart/abstractpartellipse.h

Modified: trunk/sources/editor/arceditor.cpp
===================================================================
--- trunk/sources/editor/arceditor.cpp	2015-07-23 09:32:21 UTC (rev 4066)
+++ trunk/sources/editor/arceditor.cpp	2015-07-23 09:57:09 UTC (rev 4067)
@@ -135,10 +135,16 @@
 {
 	if (m_locked) return;
 	m_locked = true;
-	QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "startAngle", part->property("startAngle"), ((start_angle -> value() * -1) + 90) * 16);
-	undo->setText("Modifier l'angle de depart d'un arc");
-	undo->enableAnimation();
-	elementScene()->undoStack().push(undo);
+	double value = ((start_angle -> value() * -1) + 90) * 16;
+
+	if (value != part->property("startAngle"))
+	{
+		QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "startAngle", part->property("startAngle"), value);
+		undo->setText("Modifier l'angle de depart d'un arc");
+		undo->enableAnimation();
+		elementScene()->undoStack().push(undo);
+	}
+
 	m_locked = false;
 }
 
@@ -150,10 +156,16 @@
 {
 	if (m_locked) return;
 	m_locked = true;
-	QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "spanAngle", part->property("spanAngle"), angle -> value() * -16);
-	undo->setText("Modifier l'angle d'un arc");
-	undo->enableAnimation();
-	elementScene()->undoStack().push(undo);
+	double value = angle -> value() * -16;
+
+	if (value != part->property("spanAngle"))
+	{
+		QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "spanAngle", part->property("spanAngle"), value);
+		undo->setText("Modifier l'angle d'un arc");
+		undo->enableAnimation();
+		elementScene()->undoStack().push(undo);
+	}
+
 	m_locked = false;
 }
 
@@ -167,10 +179,15 @@
 	m_locked = true;
 	QPointF point = part->mapFromScene(x->value() - h->value()/2, y->value() - v->value()/2);
 	QRectF rect(point, QSizeF(h->value(), v->value()));
-	QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
-	undo->setText("Modifier un arc");
-	undo->enableAnimation();
-	elementScene()->undoStack().push(undo);
+
+	if (rect != part->property("rect"))
+	{
+		QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
+		undo->setText("Modifier un arc");
+		undo->enableAnimation();
+		elementScene()->undoStack().push(undo);
+	}
+
 	m_locked = false;
 }
 

Modified: trunk/sources/editor/ellipseeditor.cpp
===================================================================
--- trunk/sources/editor/ellipseeditor.cpp	2015-07-23 09:32:21 UTC (rev 4066)
+++ trunk/sources/editor/ellipseeditor.cpp	2015-07-23 09:57:09 UTC (rev 4067)
@@ -18,6 +18,8 @@
 #include "ellipseeditor.h"
 #include "styleeditor.h"
 #include "partellipse.h"
+#include "QPropertyUndoCommand/qpropertyundocommand.h"
+#include "elementscene.h"
 
 /**
 	Constructeur
@@ -27,7 +29,8 @@
 */
 EllipseEditor::EllipseEditor(QETElementEditor *editor, PartEllipse *ellipse, QWidget *parent) :
 	ElementItemEditor(editor, parent),
-	part(ellipse)
+	part(ellipse),
+	m_locked(false)
 {
 	style_ = new StyleEditor(editor);
 	
@@ -74,20 +77,28 @@
 	@param new_part Nouvelle primitive a editer
 	@return true si l'editeur a accepter d'editer la primitive, false sinon
 */
-bool EllipseEditor::setPart(CustomElementPart *new_part) {
-	if (!new_part) {
+bool EllipseEditor::setPart(CustomElementPart *new_part)
+{
+	if (!new_part)
+	{
+		if (part)
+			disconnect(part, &PartEllipse::rectChanged, this, &EllipseEditor::updateForm);
 		part = 0;
 		style_ -> setPart(0);
 		return(true);
 	}
-	if (PartEllipse *part_ellipse = dynamic_cast<PartEllipse *>(new_part)) {
+	if (PartEllipse *part_ellipse = dynamic_cast<PartEllipse *>(new_part))
+	{
+		if (part == part_ellipse) return true;
+		if (part)
+			disconnect(part, &PartEllipse::rectChanged, this, &EllipseEditor::updateForm);
 		part = part_ellipse;
 		style_ -> setPart(part);
 		updateForm();
+		connect(part, &PartEllipse::rectChanged, this, &EllipseEditor::updateForm);
 		return(true);
-	} else {
-		return(false);
 	}
+	return(false);
 }
 
 /**
@@ -97,36 +108,36 @@
 	return(part);
 }
 
-/**
-	Met a jour l'ellipse a partir des donnees du formulaire
-*/
-void EllipseEditor::updateEllipse() {
-	if (!part) return;
-	part -> setProperty("centerX",     x  -> value());
-	part -> setProperty("centerY",     y  -> value());
-	part -> setProperty("diameter_h",  h  -> value());
-	part -> setProperty("diameter_v",  v  -> value());
+void EllipseEditor::editingFinished()
+{
+	if (m_locked) return;
+	m_locked = true;
+	QPointF point = part->mapFromScene(x->value() - h->value()/2, y->value() - v->value()/2);
+	QRectF rect(point, QSizeF(h->value(), v->value()));
+
+	if (rect != part->property("rect"))
+	{
+		QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
+		undo->setText("Modifier une ellipse");
+		undo->enableAnimation();
+		elementScene()->undoStack().push(undo);
+	}
+
+	m_locked = false;
 }
 
-/// Met a jour l'abscisse du centre de l'ellipse et cree un objet d'annulation
-void EllipseEditor::updateEllipseX() { addChangePartCommand(tr("abscisse"),               part, "centerX",           x -> value());       }
-/// Met a jour l'ordonnee du centre de l'ellipse et cree un objet d'annulation
-void EllipseEditor::updateEllipseY() { addChangePartCommand(tr("ordonnée"),            part, "centerY",           y -> value());       }
-/// Met a jour le diametre horizontal de l'ellipse et cree un objet d'annulation
-void EllipseEditor::updateEllipseH() { addChangePartCommand(tr("diamètre horizontal"), part, "diameter_h",  h -> value());       }
-/// Met a jour le diametre vertical de l'ellipse et cree un objet d'annulation
-void EllipseEditor::updateEllipseV() { addChangePartCommand(tr("diamètre vertical"),   part, "diameter_v",  v -> value());       }
-
 /**
 	Met a jour le formulaire d'edition
 */
-void EllipseEditor::updateForm() {
+void EllipseEditor::updateForm()
+{
 	if (!part) return;
 	activeConnections(false);
-	x->setValue(part->property("centerX").toReal());
-	y->setValue(part->property("centerY").toReal());
-	h->setValue(part->property("diameter_h").toReal());
-	v->setValue(part->property("diameter_v").toReal());
+	QRectF rect = part->property("rect").toRectF();
+	x->setValue(part->mapToScene(rect.topLeft()).x() + (rect.width()/2));
+	y->setValue(part->mapToScene(rect.topLeft()).y() + (rect.height()/2));
+	h->setValue(rect.width());
+	v->setValue(rect.height());
 	activeConnections(true);
 }
 
@@ -134,16 +145,20 @@
 	Active ou desactive les connexionx signaux/slots entre les widgets internes.
 	@param active true pour activer les connexions, false pour les desactiver
 */
-void EllipseEditor::activeConnections(bool active) {
-	if (active) {
-		connect(x, SIGNAL(editingFinished()), this, SLOT(updateEllipseX()));
-		connect(y, SIGNAL(editingFinished()), this, SLOT(updateEllipseY()));
-		connect(h, SIGNAL(editingFinished()), this, SLOT(updateEllipseH()));
-		connect(v, SIGNAL(editingFinished()), this, SLOT(updateEllipseV()));
-	} else {
-		disconnect(x, SIGNAL(editingFinished()), this, SLOT(updateEllipseX()));
-		disconnect(y, SIGNAL(editingFinished()), this, SLOT(updateEllipseY()));
-		disconnect(h, SIGNAL(editingFinished()), this, SLOT(updateEllipseH()));
-		disconnect(v, SIGNAL(editingFinished()), this, SLOT(updateEllipseV()));
+void EllipseEditor::activeConnections(bool active)
+{
+	if (active)
+	{
+		connect(x, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+		connect(y, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+		connect(h, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+		connect(v, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
 	}
+	else
+	{
+		disconnect(x, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+		disconnect(y, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+		disconnect(h, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+		disconnect(v, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
+	}
 }

Modified: trunk/sources/editor/ellipseeditor.h
===================================================================
--- trunk/sources/editor/ellipseeditor.h	2015-07-23 09:32:21 UTC (rev 4066)
+++ trunk/sources/editor/ellipseeditor.h	2015-07-23 09:57:09 UTC (rev 4067)
@@ -17,42 +17,44 @@
 */
 #ifndef ELLIPSE_EDITOR_H
 #define ELLIPSE_EDITOR_H
-#include <QtWidgets>
+
 #include "elementitemeditor.h"
+
 class PartEllipse;
 class StyleEditor;
+class QDoubleSpinBox;
+
 /**
 	This class provides a widget to edit ellipses within the element editor.
 */
-class EllipseEditor : public ElementItemEditor {
+class EllipseEditor : public ElementItemEditor
+{
 	Q_OBJECT
-	// constructors, destructor
+
+		// constructors, destructor
 	public:
-	EllipseEditor(QETElementEditor *, PartEllipse * = 0, QWidget * = 0);
-	virtual ~EllipseEditor();
+		EllipseEditor(QETElementEditor *, PartEllipse * = 0, QWidget * = 0);
+		virtual ~EllipseEditor();
 	private:
-	EllipseEditor(const EllipseEditor &);
+		EllipseEditor(const EllipseEditor &);
 	
-	// attributes
+		// attributes
 	private:
-	PartEllipse *part;
-	StyleEditor *style_;
-	QDoubleSpinBox *x, *y, *h, *v;
+		PartEllipse *part;
+		StyleEditor *style_;
+		QDoubleSpinBox *x, *y, *h, *v;
+		bool m_locked;
 	
-	// methods
+		// methods
 	public:
-	virtual bool setPart(CustomElementPart *);
-	virtual CustomElementPart *currentPart() const;
+		virtual bool setPart(CustomElementPart *);
+		virtual CustomElementPart *currentPart() const;
 	
 	public slots:
-	void updateEllipse();
-	void updateEllipseX();
-	void updateEllipseY();
-	void updateEllipseH();
-	void updateEllipseV();
-	void updateForm();
+		void editingFinished();
+		void updateForm();
 	
 	private:
-	void activeConnections(bool);
+		void activeConnections(bool);
 };
 #endif

Modified: trunk/sources/editor/graphicspart/abstractpartellipse.cpp
===================================================================
--- trunk/sources/editor/graphicspart/abstractpartellipse.cpp	2015-07-23 09:32:21 UTC (rev 4066)
+++ trunk/sources/editor/graphicspart/abstractpartellipse.cpp	2015-07-23 09:57:09 UTC (rev 4067)
@@ -159,74 +159,3 @@
 	m_span_angle = span_angle;
 	emit spanAngleChanged();
 }
-
-/**
- * @brief AbstractPartEllipse::setCenterX
- * Like setCenter but Y keep unchanged
- * See setCenter(const QPointF &center)
- * @param x
- */
-void AbstractPartEllipse::setCenterX(const qreal x)
-{
-	QPointF pos = mapToParent(m_rect.center());
-	pos.setX(x);
-	setCenter(pos);
-}
-
-/**
- * @brief AbstractPartEllipse::setCenterY
- * Like setCenter but X keep unchanged
- * See setCenter(const QPointF &center)
- * @param y
- */
-void AbstractPartEllipse::setCenterY(const qreal y)
-{
-	QPointF pos = mapToParent(m_rect.center());
-	pos.setY(y);
-	setCenter(pos);
-}
-
-/**
- * @brief AbstractPartEllipse::setCenter
- * This is a convenience method to setPos().
- * Adjust the position of this item,
- * so that the center of the rectangle is at the given position(position in parent coordinates).
- * @param center
- */
-void AbstractPartEllipse::setCenter(const QPointF &center)
-{
-	QPointF pos = center - m_rect.center();
-	setPos(pos);
-}
-
-/**
- * @brief AbstractPartEllipse::setWidth
- * Set new width for rectangle.
- * The center of rectangle is unchanged,
- * The coordinates of the left side and right side of the rectangle change
- * @param w
- */
-void AbstractPartEllipse::setWidth(const qreal w)
-{
-	qreal new_width = qAbs(w);
-	QRectF current_rect = rect();
-	current_rect.translate((new_width - current_rect.width()) / -2.0, 0.0);
-	current_rect.setWidth(new_width);
-	setRect(current_rect);
-}
-
-/**
- * @brief AbstractPartEllipse::setHeight
- * Set new height for rectangle
- * The center of rectangle is unchanged
- * The coordinates of the top side and bottom side of the rectangle change
- * @param h
- */
-void AbstractPartEllipse::setHeight(const qreal h)
-{
-	qreal new_height = qAbs(h);
-	QRectF current_rect = rect();
-	current_rect.translate(0.0, (new_height - current_rect.height()) / -2.0);
-	current_rect.setHeight(new_height);
-	setRect(current_rect);
-}

Modified: trunk/sources/editor/graphicspart/abstractpartellipse.h
===================================================================
--- trunk/sources/editor/graphicspart/abstractpartellipse.h	2015-07-23 09:32:21 UTC (rev 4066)
+++ trunk/sources/editor/graphicspart/abstractpartellipse.h	2015-07-23 09:57:09 UTC (rev 4067)
@@ -35,10 +35,6 @@
 		Q_OBJECT
 		Q_PROPERTY(int startAngle   READ startAngle WRITE setStartAngle)
 		Q_PROPERTY(int spanAngle    READ spanAngle  WRITE setSpanAngle)
-		Q_PROPERTY(qreal centerX    READ centerX    WRITE setCenterX)
-		Q_PROPERTY(qreal centerY    READ centerY    WRITE setCenterY)
-		Q_PROPERTY(qreal diameter_h READ width      WRITE setWidth)
-		Q_PROPERTY(qreal diameter_v READ height     WRITE setHeight)
 		Q_PROPERTY(QRectF rect READ rect WRITE setRect)
 
 		// constructors, destructor
@@ -74,20 +70,6 @@
 		int  spanAngle () const {return m_span_angle;}
 		void setSpanAngle (const int &span_angle);
 
-		qreal centerX() const {return mapToScene(rect().center()).x() ;}
-		void  setCenterX(const qreal x);
-
-		qreal centerY() const {return mapToScene(rect().center()).y();}
-		void  setCenterY(const qreal y);
-
-		void setCenter (const QPointF &center);
-
-		qreal width() const {return rect().width();}
-		void  setWidth(const qreal w);
-
-		qreal height() const {return rect().height();}
-		void  setHeight (const qreal h);
-
 	protected:
 		QList<QPointF> saved_points_;
 		QRectF m_rect;


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