From 7e81cf3c418b66817b3ee6f804b9e38274a34bc6 Mon Sep 17 00:00:00 2001 From: blacksun Date: Thu, 6 Aug 2015 16:34:38 +0000 Subject: [PATCH] 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 --- sources/qetgraphicsitem/qetshapeitem.cpp | 31 ++++---- sources/qetgraphicsitem/qetshapeitem.h | 10 ++- .../ui/shapegraphicsitempropertieswidget.cpp | 29 ++++---- .../ui/shapegraphicsitempropertieswidget.h | 2 - .../undocommand/changeshapestylecommand.cpp | 74 ------------------- sources/undocommand/changeshapestylecommand.h | 44 ----------- 6 files changed, 35 insertions(+), 155 deletions(-) delete mode 100644 sources/undocommand/changeshapestylecommand.cpp delete mode 100644 sources/undocommand/changeshapestylecommand.h diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index 88ea49945..6480be414 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -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 @@ QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem 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 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti { 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,11 +251,6 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti 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 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti m_handler.drawHandler(painter, m_polygon); break; } + painter->restore(); } /** @@ -458,7 +457,7 @@ bool QetShapeItem::fromXml(const QDomElement &e) 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 @@ QDomElement QetShapeItem::toXml(QDomDocument &document) const //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) { diff --git a/sources/qetgraphicsitem/qetshapeitem.h b/sources/qetgraphicsitem/qetshapeitem.h index fb6e4b77f..e9508113f 100644 --- a/sources/qetgraphicsitem/qetshapeitem.h +++ b/sources/qetgraphicsitem/qetshapeitem.h @@ -18,6 +18,7 @@ #ifndef QETSHAPEITEM_H #define QETSHAPEITEM_H +#include #include "qetgraphicsitem.h" #include "QetGraphicsItemModeler/qetgraphicshandlerutility.h" @@ -33,12 +34,13 @@ class QetShapeItem : public QetGraphicsItem { 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 @@ class QetShapeItem : public QetGraphicsItem 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 @@ class QetShapeItem : public QetGraphicsItem ///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, diff --git a/sources/ui/shapegraphicsitempropertieswidget.cpp b/sources/ui/shapegraphicsitempropertieswidget.cpp index 649e88d54..5cbe29b56 100644 --- a/sources/ui/shapegraphicsitempropertieswidget.cpp +++ b/sources/ui/shapegraphicsitempropertieswidget.cpp @@ -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 @@ void ShapeGraphicsItemPropertiesWidget::setItem(QetShapeItem *shape) 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::setItem(QetShapeItem *shape) 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 @@ void ShapeGraphicsItemPropertiesWidget::reset() */ 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 @@ QUndoCommand* ShapeGraphicsItemPropertiesWidget::associatedUndo() const */ void ShapeGraphicsItemPropertiesWidget::updateUi() { - ui->m_style_cb->setCurrentIndex(static_cast(m_shape->penStyle()) - 1); + ui->m_style_cb->setCurrentIndex(static_cast(m_shape->pen().style()) - 1); ui->m_lock_pos_cb->setChecked(!m_shape->isMovable()); } diff --git a/sources/ui/shapegraphicsitempropertieswidget.h b/sources/ui/shapegraphicsitempropertieswidget.h index 88356c488..032e496d2 100644 --- a/sources/ui/shapegraphicsitempropertieswidget.h +++ b/sources/ui/shapegraphicsitempropertieswidget.h @@ -55,8 +55,6 @@ class ShapeGraphicsItemPropertiesWidget : public PropertiesEditorWidget private: Ui::ShapeGraphicsItemPropertiesWidget *ui; QetShapeItem *m_shape; - - Qt::PenStyle m_old_pen_style; }; #endif // SHAPEGRAPHICSITEMPROPERTIESWIDGET_H diff --git a/sources/undocommand/changeshapestylecommand.cpp b/sources/undocommand/changeshapestylecommand.cpp deleted file mode 100644 index ee854c406..000000000 --- a/sources/undocommand/changeshapestylecommand.cpp +++ /dev/null @@ -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 . -*/ -#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(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(); -} diff --git a/sources/undocommand/changeshapestylecommand.h b/sources/undocommand/changeshapestylecommand.h deleted file mode 100644 index 173aa4cad..000000000 --- a/sources/undocommand/changeshapestylecommand.h +++ /dev/null @@ -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 . -*/ -#ifndef CHANGESHAPESTYLECOMMAND_H -#define CHANGESHAPESTYLECOMMAND_H - -#include - -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