[qet] [4087] shapegraphicsitempropertieswidget : use QPropertyUndoCommand instead of ChangeShapeStyleCommand.

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


Revision: 4087
Author:   blacksun
Date:     2015-08-06 18:34:38 +0200 (Thu, 06 Aug 2015)
Log Message:
-----------
shapegraphicsitempropertieswidget : use QPropertyUndoCommand instead of ChangeShapeStyleCommand.
Remove the class ChangeShapeStyleCommand.

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

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

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.cpp
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2015-08-04 19:55:31 UTC (rev 4086)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.cpp	2015-08-06 16:34:38 UTC (rev 4087)
@@ -34,7 +34,6 @@
 QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem *parent) :
 	QetGraphicsItem(parent),
 	m_shapeType(type),
-	m_shapeStyle(Qt::DashLine),
 	m_P1 (p1),
 	m_P2 (p2),
 	m_hovered(false),
@@ -44,20 +43,23 @@
 	if (type == Polygon) m_polygon << m_P1 << m_P2;
 	setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
 	setAcceptHoverEvents(true);
+	m_pen.setStyle(Qt::DashLine);
+	m_pen.setWidthF(1);
 }
 
 QetShapeItem::~QetShapeItem() {}
 
 /**
- * @brief QetShapeItem::setStyle
- * Set the new style of pen for thi item
- * @param newStyle
+ * @brief QetShapeItem::setPen
+ * Set the pen to use for draw the shape
+ * @param pen
  */
-void QetShapeItem::setStyle(Qt::PenStyle newStyle)
+void QetShapeItem::setPen(const QPen &pen)
 {
-	m_shapeStyle = newStyle;
+	if (m_pen == pen) return;
+	m_pen = pen;
 	update();
-	emit styleChanged();
+	emit penChanged();
 }
 
 /**
@@ -233,9 +235,10 @@
 {
 	Q_UNUSED(option); Q_UNUSED(widget);
 
-	QPen pen(m_shapeStyle);
+	painter->save();
 	painter -> setRenderHint(QPainter::Antialiasing, false);
-	pen.setWidthF(1);
+	m_pen.setColor(isSelected()? Qt::red : Qt::black);
+	painter -> setPen(m_pen);
 
 		//Draw hovered shadow
 	if (m_hovered)
@@ -248,12 +251,7 @@
 		painter -> drawPath (shape());
 		painter -> restore  ();
 	}
-		//Draw red if selected
-	if (isSelected())
-		pen.setColor(Qt::red);
 
-	painter -> setPen(pen);
-
 		//Draw the shape and handlers if is selected
 	switch (m_shapeType)
 	{
@@ -281,6 +279,7 @@
 				m_handler.drawHandler(painter, m_polygon);
 			break;
 	}
+	painter->restore();
 }
 
 /**
@@ -458,7 +457,7 @@
 	if (e.tagName() != "shape") return (false);
 
 	is_movable_ = (e.attribute("is_movable").toInt());
-	m_shapeStyle = Qt::PenStyle(e.attribute("style","0").toInt());
+	m_pen.setStyle(Qt::PenStyle(e.attribute("style","0").toInt()));
 
 	QString type = e.attribute("type");
 		//Compatibility for version older than N°4075, shape type was stored with an int
@@ -506,7 +505,7 @@
 		//write some attribute
 	QMetaEnum me = metaObject()->enumerator(metaObject()->indexOfEnumerator("ShapeType"));
 	result.setAttribute("type", me.valueToKey(m_shapeType));
-	result.setAttribute("style", QString::number(m_shapeStyle));
+	result.setAttribute("style", QString::number(m_pen.style()));
 	result.setAttribute("is_movable", bool(is_movable_));
 	if (m_shapeType != Polygon)
 	{

Modified: trunk/sources/qetgraphicsitem/qetshapeitem.h
===================================================================
--- trunk/sources/qetgraphicsitem/qetshapeitem.h	2015-08-04 19:55:31 UTC (rev 4086)
+++ trunk/sources/qetgraphicsitem/qetshapeitem.h	2015-08-06 16:34:38 UTC (rev 4087)
@@ -18,6 +18,7 @@
 #ifndef QETSHAPEITEM_H
 #define QETSHAPEITEM_H
 
+#include <QPen>
 #include "qetgraphicsitem.h"
 #include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
 
@@ -33,12 +34,13 @@
 {
 	Q_OBJECT
 
+		Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
 		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();
+		void penChanged();
 
 	public:
 		Q_ENUMS(ShapeType)
@@ -56,8 +58,8 @@
 		virtual int type() const { return Type; }
 
 			///METHODS
-		void setStyle(Qt::PenStyle);
-		Qt::PenStyle penStyle() const { return m_shapeStyle;}
+		QPen pen() const {return m_pen;}
+		void setPen(const QPen &pen);
 		ShapeType shapeType() const {return m_shapeType;}
 
 		virtual bool	    fromXml (const QDomElement &);
@@ -95,7 +97,7 @@
 		///ATTRIBUTES
 	private:
 		ShapeType    m_shapeType;
-		Qt::PenStyle m_shapeStyle;
+		QPen		 m_pen;
 		QPointF		 m_P1, m_P2, m_old_P1, m_old_P2;
 		QPolygonF	 m_polygon, m_old_polygon;
 		bool         m_hovered,

Modified: trunk/sources/ui/shapegraphicsitempropertieswidget.cpp
===================================================================
--- trunk/sources/ui/shapegraphicsitempropertieswidget.cpp	2015-08-04 19:55:31 UTC (rev 4086)
+++ trunk/sources/ui/shapegraphicsitempropertieswidget.cpp	2015-08-06 16:34:38 UTC (rev 4087)
@@ -19,7 +19,7 @@
 #include "ui_shapegraphicsitempropertieswidget.h"
 #include "qetshapeitem.h"
 #include "diagram.h"
-#include "changeshapestylecommand.h"
+#include "QPropertyUndoCommand/qpropertyundocommand.h"
 
 /**
  * @brief ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget
@@ -56,12 +56,11 @@
 	if (shape == m_shape) return;
 
 	if (m_shape)
-		disconnect(m_shape, &QetShapeItem::styleChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
+		disconnect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
 
 	m_shape = shape;
-	connect(m_shape, &QetShapeItem::styleChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
+	connect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
 
-	m_old_pen_style = m_shape->penStyle();
 	updateUi();
 }
 
@@ -73,25 +72,21 @@
 void ShapeGraphicsItemPropertiesWidget::apply()
 {
 	if (m_live_edit)
-		disconnect(m_shape, &QetShapeItem::styleChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
+		disconnect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
 
 	if (m_shape->diagram())
 		if (QUndoCommand *undo = associatedUndo())
 			m_shape->diagram()->undoStack().push(undo);
 
-	m_old_pen_style = m_shape->penStyle();
-
 	if (m_live_edit)
-		connect(m_shape, &QetShapeItem::styleChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
+		connect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
 }
 
 /**
  * @brief ShapeGraphicsItemPropertiesWidget::reset
  * Reset the change
  */
-void ShapeGraphicsItemPropertiesWidget::reset()
-{
-	m_shape->setStyle(m_old_pen_style);
+void ShapeGraphicsItemPropertiesWidget::reset() {
 	updateUi();
 }
 
@@ -103,10 +98,14 @@
  */
 QUndoCommand* ShapeGraphicsItemPropertiesWidget::associatedUndo() const
 {
-	Qt::PenStyle new_style = Qt::PenStyle(ui->m_style_cb->currentIndex() + 1);
-	if (new_style != m_old_pen_style) return (new ChangeShapeStyleCommand(m_shape, m_old_pen_style, new_style));
+	QPen old_pen = m_shape->pen();
+	QPen new_pen = old_pen;
+	new_pen.setStyle(Qt::PenStyle(ui->m_style_cb->currentIndex() + 1));
+	if (new_pen == old_pen) return nullptr;
 
-	return nullptr;
+	QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_shape, "pen", old_pen, new_pen);
+	undo->setText(tr("Modifier le type de trait d'une forme"));
+	return undo;
 }
 
 /**
@@ -114,7 +113,7 @@
  */
 void ShapeGraphicsItemPropertiesWidget::updateUi()
 {
-	ui->m_style_cb->setCurrentIndex(static_cast<int>(m_shape->penStyle()) - 1);
+	ui->m_style_cb->setCurrentIndex(static_cast<int>(m_shape->pen().style()) - 1);
 	ui->m_lock_pos_cb->setChecked(!m_shape->isMovable());
 }
 

Modified: trunk/sources/ui/shapegraphicsitempropertieswidget.h
===================================================================
--- trunk/sources/ui/shapegraphicsitempropertieswidget.h	2015-08-04 19:55:31 UTC (rev 4086)
+++ trunk/sources/ui/shapegraphicsitempropertieswidget.h	2015-08-06 16:34:38 UTC (rev 4087)
@@ -55,8 +55,6 @@
 	private:
 		Ui::ShapeGraphicsItemPropertiesWidget *ui;
 		QetShapeItem *m_shape;
-
-		Qt::PenStyle m_old_pen_style;
 };
 
 #endif // SHAPEGRAPHICSITEMPROPERTIESWIDGET_H

Deleted: trunk/sources/undocommand/changeshapestylecommand.cpp
===================================================================
--- trunk/sources/undocommand/changeshapestylecommand.cpp	2015-08-04 19:55:31 UTC (rev 4086)
+++ trunk/sources/undocommand/changeshapestylecommand.cpp	2015-08-06 16:34:38 UTC (rev 4087)
@@ -1,74 +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 "changeshapestylecommand.h"
-#include "qetshapeitem.h"
-#include "diagram.h"
-
-/**
- * @brief ChangeShapeStyleCommand::ChangeShapeStyleCommand
- * Constructor
- * @param item : shape to change
- * @param old_ps : old style
- * @param new_ps : new style
- * @param parent : parent undo
- */
-ChangeShapeStyleCommand::ChangeShapeStyleCommand(QetShapeItem *item, const Qt::PenStyle &old_ps, const Qt::PenStyle new_ps, QUndoCommand *parent):
-	QUndoCommand(parent),
-	m_shape(item),
-	m_old_ps(old_ps),
-	m_new_ps(new_ps)
-{
-	setText(QObject::tr("Changer le style d'une primitive"));
-}
-
-/**
- * @brief ChangeShapeStyleCommand::mergeWith
- * Try to merge this command with other
- * @param other
- * @return true if was merged
- */
-bool ChangeShapeStyleCommand::mergeWith(const QUndoCommand *other)
-{
-	if (id() != other->id() || other->childCount()) return false;
-	ChangeShapeStyleCommand const *undo = static_cast<const ChangeShapeStyleCommand*>(other);
-	if(m_shape != undo->m_shape) return false;
-	m_new_ps = undo->m_new_ps;
-	return true;
-}
-
-/**
- * @brief ChangeShapeStyleCommand::undo
- * undo this command
- */
-void ChangeShapeStyleCommand::undo()
-{
-	if (m_shape->diagram()) m_shape->diagram()->showMe();
-	m_shape->setStyle(m_old_ps);
-	QUndoCommand::undo();
-}
-
-/**
- * @brief ChangeShapeStyleCommand::redo
- * redo this command
- */
-void ChangeShapeStyleCommand::redo()
-{
-	if (m_shape->diagram()) m_shape->diagram()->showMe();
-	m_shape->setStyle(m_new_ps);
-	QUndoCommand::redo();
-}

Deleted: trunk/sources/undocommand/changeshapestylecommand.h
===================================================================
--- trunk/sources/undocommand/changeshapestylecommand.h	2015-08-04 19:55:31 UTC (rev 4086)
+++ trunk/sources/undocommand/changeshapestylecommand.h	2015-08-06 16:34:38 UTC (rev 4087)
@@ -1,44 +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 CHANGESHAPESTYLECOMMAND_H
-#define CHANGESHAPESTYLECOMMAND_H
-
-#include <QUndoCommand>
-
-class QetShapeItem;
-
-/**
- * @brief The ChangeShapeStyleCommand class
- * This class manage undo/redo to change the shape style.
- */
-class ChangeShapeStyleCommand : public QUndoCommand
-{
-	public:
-		ChangeShapeStyleCommand(QetShapeItem *item, const Qt::PenStyle &old_ps, const Qt::PenStyle new_ps, QUndoCommand *parent = nullptr);
-
-		int id() const {return 4;}
-		bool mergeWith(const QUndoCommand *other);
-		void undo();
-		void redo();
-
-	private:
-		QetShapeItem *m_shape;
-		Qt::PenStyle m_old_ps, m_new_ps;
-};
-
-#endif // CHANGESHAPESTYLECOMMAND_H


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