shapegraphicsitempropertieswidget : use QPropertyUndoCommand instead of ChangeShapeStyleCommand.

Remove the class ChangeShapeStyleCommand.


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4087 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-08-06 16:34:38 +00:00
parent 7e9de6470b
commit 7e81cf3c41
6 changed files with 35 additions and 155 deletions

View File

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

View File

@@ -18,6 +18,7 @@
#ifndef QETSHAPEITEM_H #ifndef QETSHAPEITEM_H
#define QETSHAPEITEM_H #define QETSHAPEITEM_H
#include <QPen>
#include "qetgraphicsitem.h" #include "qetgraphicsitem.h"
#include "QetGraphicsItemModeler/qetgraphicshandlerutility.h" #include "QetGraphicsItemModeler/qetgraphicshandlerutility.h"
@@ -33,12 +34,13 @@ class QetShapeItem : public QetGraphicsItem
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
Q_PROPERTY(QRectF rect READ rect WRITE setRect) Q_PROPERTY(QRectF rect READ rect WRITE setRect)
Q_PROPERTY(QLineF line READ line WRITE setLine) Q_PROPERTY(QLineF line READ line WRITE setLine)
Q_PROPERTY(QPolygonF polygon READ polygon WRITE setPolygon) Q_PROPERTY(QPolygonF polygon READ polygon WRITE setPolygon)
signals: signals:
void styleChanged(); void penChanged();
public: public:
Q_ENUMS(ShapeType) Q_ENUMS(ShapeType)
@@ -56,8 +58,8 @@ class QetShapeItem : public QetGraphicsItem
virtual int type() const { return Type; } virtual int type() const { return Type; }
///METHODS ///METHODS
void setStyle(Qt::PenStyle); QPen pen() const {return m_pen;}
Qt::PenStyle penStyle() const { return m_shapeStyle;} void setPen(const QPen &pen);
ShapeType shapeType() const {return m_shapeType;} ShapeType shapeType() const {return m_shapeType;}
virtual bool fromXml (const QDomElement &); virtual bool fromXml (const QDomElement &);
@@ -95,7 +97,7 @@ class QetShapeItem : public QetGraphicsItem
///ATTRIBUTES ///ATTRIBUTES
private: private:
ShapeType m_shapeType; ShapeType m_shapeType;
Qt::PenStyle m_shapeStyle; QPen m_pen;
QPointF m_P1, m_P2, m_old_P1, m_old_P2; QPointF m_P1, m_P2, m_old_P1, m_old_P2;
QPolygonF m_polygon, m_old_polygon; QPolygonF m_polygon, m_old_polygon;
bool m_hovered, bool m_hovered,

View File

@@ -19,7 +19,7 @@
#include "ui_shapegraphicsitempropertieswidget.h" #include "ui_shapegraphicsitempropertieswidget.h"
#include "qetshapeitem.h" #include "qetshapeitem.h"
#include "diagram.h" #include "diagram.h"
#include "changeshapestylecommand.h" #include "QPropertyUndoCommand/qpropertyundocommand.h"
/** /**
* @brief ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget * @brief ShapeGraphicsItemPropertiesWidget::ShapeGraphicsItemPropertiesWidget
@@ -56,12 +56,11 @@ void ShapeGraphicsItemPropertiesWidget::setItem(QetShapeItem *shape)
if (shape == m_shape) return; if (shape == m_shape) return;
if (m_shape) if (m_shape)
disconnect(m_shape, &QetShapeItem::styleChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi); disconnect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
m_shape = shape; 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(); updateUi();
} }
@@ -73,25 +72,21 @@ void ShapeGraphicsItemPropertiesWidget::setItem(QetShapeItem *shape)
void ShapeGraphicsItemPropertiesWidget::apply() void ShapeGraphicsItemPropertiesWidget::apply()
{ {
if (m_live_edit) 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 (m_shape->diagram())
if (QUndoCommand *undo = associatedUndo()) if (QUndoCommand *undo = associatedUndo())
m_shape->diagram()->undoStack().push(undo); m_shape->diagram()->undoStack().push(undo);
m_old_pen_style = m_shape->penStyle();
if (m_live_edit) if (m_live_edit)
connect(m_shape, &QetShapeItem::styleChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi); connect(m_shape, &QetShapeItem::penChanged, this, &ShapeGraphicsItemPropertiesWidget::updateUi);
} }
/** /**
* @brief ShapeGraphicsItemPropertiesWidget::reset * @brief ShapeGraphicsItemPropertiesWidget::reset
* Reset the change * Reset the change
*/ */
void ShapeGraphicsItemPropertiesWidget::reset() void ShapeGraphicsItemPropertiesWidget::reset() {
{
m_shape->setStyle(m_old_pen_style);
updateUi(); updateUi();
} }
@@ -103,10 +98,14 @@ void ShapeGraphicsItemPropertiesWidget::reset()
*/ */
QUndoCommand* ShapeGraphicsItemPropertiesWidget::associatedUndo() const QUndoCommand* ShapeGraphicsItemPropertiesWidget::associatedUndo() const
{ {
Qt::PenStyle new_style = Qt::PenStyle(ui->m_style_cb->currentIndex() + 1); QPen old_pen = m_shape->pen();
if (new_style != m_old_pen_style) return (new ChangeShapeStyleCommand(m_shape, m_old_pen_style, new_style)); 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 @@ QUndoCommand* ShapeGraphicsItemPropertiesWidget::associatedUndo() const
*/ */
void ShapeGraphicsItemPropertiesWidget::updateUi() 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()); ui->m_lock_pos_cb->setChecked(!m_shape->isMovable());
} }

View File

@@ -55,8 +55,6 @@ class ShapeGraphicsItemPropertiesWidget : public PropertiesEditorWidget
private: private:
Ui::ShapeGraphicsItemPropertiesWidget *ui; Ui::ShapeGraphicsItemPropertiesWidget *ui;
QetShapeItem *m_shape; QetShapeItem *m_shape;
Qt::PenStyle m_old_pen_style;
}; };
#endif // SHAPEGRAPHICSITEMPROPERTIESWIDGET_H #endif // SHAPEGRAPHICSITEMPROPERTIESWIDGET_H

View File

@@ -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();
}

View File

@@ -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