QetShapeItem : use QPropertyUndoCommand instead of QetShapeGeometryCommand

Remove the class : QetShapeGeometryCommand


git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4074 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-07-27 09:20:38 +00:00
parent 871d50c014
commit fae003f6c4
4 changed files with 70 additions and 266 deletions

View File

@@ -21,7 +21,7 @@
#include "qet.h" #include "qet.h"
#include "shapegraphicsitempropertieswidget.h" #include "shapegraphicsitempropertieswidget.h"
#include "PropertiesEditor/propertieseditordialog.h" #include "PropertiesEditor/propertieseditordialog.h"
#include "qetshapegeometrycommand.h" #include "QPropertyUndoCommand/qpropertyundocommand.h"
/** /**
* @brief QetShapeItem::QetShapeItem * @brief QetShapeItem::QetShapeItem
@@ -39,7 +39,6 @@ QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem
m_P2 (p2), m_P2 (p2),
m_hovered(false), m_hovered(false),
m_mouse_grab_handler(false), m_mouse_grab_handler(false),
m_undo_command(nullptr),
m_handler(10) m_handler(10)
{ {
if (type == Polyline) m_polygon << m_P1 << m_P2; if (type == Polyline) m_polygon << m_P1 << m_P2;
@@ -47,10 +46,7 @@ QetShapeItem::QetShapeItem(QPointF p1, QPointF p2, ShapeType type, QGraphicsItem
setAcceptHoverEvents(true); setAcceptHoverEvents(true);
} }
QetShapeItem::~QetShapeItem() QetShapeItem::~QetShapeItem() {}
{
if (m_undo_command) delete m_undo_command;
}
/** /**
* @brief QetShapeItem::setStyle * @brief QetShapeItem::setStyle
@@ -83,22 +79,21 @@ void QetShapeItem::setP2(const QPointF &P2)
prepareGeometryChange(); prepareGeometryChange();
m_P2 = P2; m_P2 = P2;
} }
setTransformOriginPoint(boundingRect().center());
} }
/** /**
* @brief QetShapeItem::setLine * @brief QetShapeItem::setLine
* Set item geometry to line (only available for line shape) * Set item geometry to line (only available for line shape)
* @param line * @param line
* @return : true when shape is a Line, else false
*/ */
void QetShapeItem::setLine(const QLineF &line) bool QetShapeItem::setLine(const QLineF &line)
{ {
if (Q_LIKELY(m_shapeType == Line)) if (Q_UNLIKELY(m_shapeType != Line)) return false;
{ prepareGeometryChange();
prepareGeometryChange(); m_P1 = line.p1();
m_P1 = line.p1(); m_P2 = line.p2();
m_P2 = line.p2(); return true;
}
} }
/** /**
@@ -138,8 +133,7 @@ bool QetShapeItem::setPolygon(const QPolygonF &polygon)
* @brief QetShapeItem::pointCount * @brief QetShapeItem::pointCount
* @return the number of point in the polygon * @return the number of point in the polygon
*/ */
int QetShapeItem::pointsCount() const int QetShapeItem::pointsCount() const {
{
return m_polygon.size(); return m_polygon.size();
} }
@@ -148,10 +142,10 @@ int QetShapeItem::pointsCount() const
* Add a new point to the curent polygon * Add a new point to the curent polygon
* @param P the new point. * @param P the new point.
*/ */
void QetShapeItem::setNextPoint(QPointF P) { void QetShapeItem::setNextPoint(QPointF P)
{
prepareGeometryChange(); prepareGeometryChange();
m_polygon.append(Diagram::snapToGrid(P)); m_polygon.append(Diagram::snapToGrid(P));
setTransformOriginPoint(boundingRect().center());
} }
/** /**
@@ -295,10 +289,8 @@ void QetShapeItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti
* @param event * @param event
*/ */
void QetShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) { void QetShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
Q_UNUSED(event);
m_hovered = true; m_hovered = true;
update(); QetGraphicsItem::hoverEnterEvent(event);
} }
/** /**
@@ -307,10 +299,8 @@ void QetShapeItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
* @param event * @param event
*/ */
void QetShapeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) { void QetShapeItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
Q_UNUSED(event);
m_hovered = false; m_hovered = false;
update(); QetGraphicsItem::hoverLeaveEvent(event);
} }
/** /**
@@ -337,14 +327,9 @@ void QetShapeItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{ {
//User click on an handler //User click on an handler
m_mouse_grab_handler = true; m_mouse_grab_handler = true;
m_old_P1 = m_P1;
switch (m_shapeType) m_old_P2 = m_P2;
{ m_old_polygon = m_polygon;
case Line: m_undo_command = new QetShapeGeometryCommand(this, QLineF(m_P1, m_P2)); break;
case Rectangle: m_undo_command = new QetShapeGeometryCommand(this, QRectF(m_P1, m_P2)); break;
case Ellipse: m_undo_command = new QetShapeGeometryCommand(this, QRectF(m_P1, m_P2)); break;
case Polyline: m_undo_command = new QetShapeGeometryCommand(this, m_polygon); break;
}
return; return;
} }
} }
@@ -367,26 +352,19 @@ void QetShapeItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
switch (m_shapeType) switch (m_shapeType)
{ {
case Line: { case Line:
prepareGeometryChange(); prepareGeometryChange();
m_vector_index == 0 ? m_P1 = new_pos : m_P2 = new_pos; m_vector_index == 0 ? m_P1 = new_pos : m_P2 = new_pos;
}
break; break;
case Rectangle: case Rectangle: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break;
setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); case Ellipse: setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index)); break;
case Ellipse: case Polyline:
setRect(m_handler.rectForPosAtIndex(QRectF(m_P1, m_P2), new_pos, m_vector_index));
break;
case Polyline: {
prepareGeometryChange(); prepareGeometryChange();
m_polygon.replace(m_vector_index, new_pos); m_polygon.replace(m_vector_index, new_pos);
}
break; break;
} //End switch } //End switch
return; return;
} }
@@ -403,21 +381,29 @@ void QetShapeItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
if (m_mouse_grab_handler) if (m_mouse_grab_handler)
{ {
m_mouse_grab_handler = false; m_mouse_grab_handler = false;
switch(m_shapeType)
{
case Line: m_undo_command->setNewLine(QLineF(m_P1, m_P2)); break;
case Rectangle: m_undo_command->setNewRect(QRectF(m_P1, m_P2)); break;
case Ellipse: m_undo_command->setNewRect(QRectF(m_P1, m_P2)); break;
case Polyline : m_undo_command->setNewPolygon(m_polygon); break;
}
if (diagram()) if (diagram())
{ {
diagram()->undoStack().push(m_undo_command); QPropertyUndoCommand *undo = nullptr;
m_undo_command = nullptr; if ((m_shapeType & (Line | Rectangle | Ellipse)) && (m_P1 != m_old_P1 || m_P2 != m_old_P2))
{
switch(m_shapeType)
{
case Line: undo = new QPropertyUndoCommand(this, "line",QLineF(m_old_P1, m_old_P2), QLineF(m_P1, m_P2)); break;
case Rectangle: undo = new QPropertyUndoCommand(this, "rect",QRectF(m_old_P1, m_old_P2), QRectF(m_P1, m_P2)); break;
case Ellipse: undo = new QPropertyUndoCommand(this, "rect",QRectF(m_old_P1, m_old_P2), QRectF(m_P1, m_P2)); break;
case Polyline: break;
}
if (undo) undo->enableAnimation();
}
else if (m_shapeType == Polyline && (m_polygon != m_old_polygon))
undo = new QPropertyUndoCommand(this, "polygon", m_old_polygon, m_polygon);
if(undo)
{
undo->setText(tr("Modifier %1").arg(name()));
diagram()->undoStack().push(undo);
}
} }
else
delete m_undo_command;
} }
QetGraphicsItem::mouseReleaseEvent(event); QetGraphicsItem::mouseReleaseEvent(event);
@@ -496,23 +482,14 @@ QDomElement QetShapeItem::toXml(QDomDocument &document) const
* @param filepath file path of the the dxf document * @param filepath file path of the the dxf document
* @return true if draw success * @return true if draw success
*/ */
bool QetShapeItem::toDXF(const QString &filepath) { bool QetShapeItem::toDXF(const QString &filepath)
switch (m_shapeType) { {
case Line: switch (m_shapeType)
Createdxf::drawLine(filepath, QLineF(mapToScene(m_P1), mapToScene(m_P2)), 0); {
return true; case Line: Createdxf::drawLine (filepath, QLineF(mapToScene(m_P1), mapToScene(m_P2)), 0); return true;
break; case Rectangle: Createdxf::drawRectangle(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); return true;
case Rectangle: case Ellipse: Createdxf::drawEllipse (filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); return true;
Createdxf::drawRectangle(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0); default: return false;
return true;
break;
case Ellipse:
Createdxf::drawEllipse(filepath, QRectF(mapToScene(m_P1), mapToScene(m_P2)).normalized(), 0);
return true;
break;
default:
return false;
break;
} }
} }
@@ -532,14 +509,12 @@ void QetShapeItem::editProperty()
* @brief QetShapeItem::name * @brief QetShapeItem::name
* @return the name of the curent shape. * @return the name of the curent shape.
*/ */
QString QetShapeItem::name() const QString QetShapeItem::name() const {
{ switch (m_shapeType) {
switch (m_shapeType) case Line: return tr("une ligne");
{ case Rectangle: return tr("un rectangle");
case Line: return tr("une ligne"); break; case Ellipse: return tr("une éllipse");
case Rectangle: return tr("un rectangle"); break; case Polyline: return tr("une polyligne");
case Ellipse: return tr("une éllipse"); break; default: return tr("une shape");
case Polyline: return tr("une polyligne"); break;
default: return tr("une shape"); break;
} }
} }

View File

@@ -23,7 +23,6 @@
class QDomElement; class QDomElement;
class QDomDocument; class QDomDocument;
class QetShapeGeometryCommand;
/** /**
* @brief The QetShapeItem class * @brief The QetShapeItem class
@@ -34,15 +33,19 @@ class QetShapeItem : public QetGraphicsItem
{ {
Q_OBJECT Q_OBJECT
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: signals:
void styleChanged(); void styleChanged();
public: public:
Q_ENUMS(ShapeType) Q_ENUMS(ShapeType)
enum ShapeType {Line =0, enum ShapeType {Line =1,
Rectangle =1, Rectangle =2,
Ellipse =2, Ellipse =4,
Polyline =3 }; Polyline =8 };
enum { Type = UserType + 1008 }; enum { Type = UserType + 1008 };
@@ -65,8 +68,11 @@ class QetShapeItem : public QetGraphicsItem
virtual QString name() const; virtual QString name() const;
void setP2 (const QPointF &P2); void setP2 (const QPointF &P2);
void setLine (const QLineF &line); QLineF line() const{return QLineF(m_P1, m_P2);}
bool setLine (const QLineF &line);
QRectF rect() const{return QRectF(m_P1, m_P2);}
bool setRect (const QRectF &rect); bool setRect (const QRectF &rect);
QPolygonF polygon() const {return m_polygon;}
bool setPolygon (const QPolygonF &polygon); bool setPolygon (const QPolygonF &polygon);
//Methods available for polygon shape //Methods available for polygon shape
@@ -89,12 +95,11 @@ class QetShapeItem : public QetGraphicsItem
private: private:
ShapeType m_shapeType; ShapeType m_shapeType;
Qt::PenStyle m_shapeStyle; Qt::PenStyle m_shapeStyle;
QPointF m_P1, m_P2; QPointF m_P1, m_P2, m_old_P1, m_old_P2;
QPolygonF m_polygon; QPolygonF m_polygon, m_old_polygon;
bool m_hovered, bool m_hovered,
m_mouse_grab_handler; m_mouse_grab_handler;
int m_vector_index; int m_vector_index;
QetShapeGeometryCommand *m_undo_command;
QetGraphicsHandlerUtility m_handler; QetGraphicsHandlerUtility m_handler;
}; };
#endif // QETSHAPEITEM_H #endif // QETSHAPEITEM_H

View File

@@ -1,122 +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 "qetshapegeometrycommand.h"
#include "qetshapeitem.h"
#include <QObject>
/**
* @brief QetShapeGeometryCommand::QetShapeGeometryCommand
* Constructor for a line shape
* @param item : item to change the geometry
* @param old_line : old line
* @param parent : parent undo command
*/
QetShapeGeometryCommand::QetShapeGeometryCommand(QetShapeItem *item, const QLineF &old_line, QUndoCommand *parent) :
QUndoCommand(parent),
m_shape_item(item),
m_old_line(old_line)
{
setText(QObject::tr("Modifier la géometrie de : %1").arg(m_shape_item->name()));
}
/**
* @brief QetShapeGeometryCommand::QetShapeGeometryCommand
* Constructor for a rectangle or ellipse shape
* @param item : item to change the geometry
* @param old_rect : old rectangle
* @param parent : parent undo command
*/
QetShapeGeometryCommand::QetShapeGeometryCommand(QetShapeItem *item, const QRectF &old_rect, QUndoCommand *parent):
QUndoCommand(parent),
m_shape_item(item),
m_old_rect(old_rect)
{
setText(QObject::tr("Modifier la géometrie de : %1").arg(m_shape_item->name()));
}
/**
* @brief QetShapeGeometryCommand::QetShapeGeometryCommand
* Constructor for polygon shape
* @param item : item to change the geometry
* @param old_polygon : old polygon
* @param parent : parent undo command
*/
QetShapeGeometryCommand::QetShapeGeometryCommand(QetShapeItem *item, const QPolygonF &old_polygon, QUndoCommand *parent):
QUndoCommand(parent),
m_shape_item(item),
m_old_polygon(old_polygon)
{
setText(QObject::tr("Modifier la géometrie de : %1").arg(m_shape_item->name()));
}
/**
* @brief QetShapeGeometryCommand::mergeWith
* Try to merge this undo command with @other
* @param other
* @return true if the two command was merged
*/
bool QetShapeGeometryCommand::mergeWith(const QUndoCommand *other)
{
if (other->id() != id() || other->childCount()) return false;
const QetShapeGeometryCommand *other_undo = static_cast<const QetShapeGeometryCommand*>(other);
if (other_undo->m_shape_item != m_shape_item) return false;
switch (m_shape_item->shapeType())
{
case QetShapeItem::Line: m_new_line = other_undo->m_new_line; break;
case QetShapeItem::Rectangle: m_new_rect = other_undo->m_new_rect; break;
case QetShapeItem::Ellipse: m_new_rect = other_undo->m_new_rect; break;
case QetShapeItem::Polyline: m_new_polygon = other_undo->m_new_polygon; break;
}
return true;
}
/**
* @brief QetShapeGeometryCommand::redo
* Redo this command
*/
void QetShapeGeometryCommand::redo()
{
switch (m_shape_item->shapeType())
{
case QetShapeItem::Line: m_shape_item->setLine(m_new_line); break;
case QetShapeItem::Rectangle: m_shape_item->setRect(m_new_rect); break;
case QetShapeItem::Ellipse: m_shape_item->setRect(m_new_rect); break;
case QetShapeItem::Polyline: m_shape_item->setPolygon(m_new_polygon); break;
}
QUndoCommand::redo();
}
/**
* @brief QetShapeGeometryCommand::undo
* Undo this command
*/
void QetShapeGeometryCommand::undo()
{
switch (m_shape_item->shapeType())
{
case QetShapeItem::Line: m_shape_item->setLine(m_old_line); break;
case QetShapeItem::Rectangle: m_shape_item->setRect(m_old_rect); break;
case QetShapeItem::Ellipse: m_shape_item->setRect(m_old_rect); break;
case QetShapeItem::Polyline: m_shape_item->setPolygon(m_old_polygon); break;
}
QUndoCommand::undo();
}

View File

@@ -1,54 +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 QETSHAPEGEOMETRYCOMMAND_H
#define QETSHAPEGEOMETRYCOMMAND_H
#include <QUndoCommand>
#include <QLineF>
#include <QPolygonF>
#include <QRectF>
class QetShapeItem;
/**
* @brief The QetShapeGeometryCommand class
* This undo command class manage the geometry change of a QetShapeItem.
*/
class QetShapeGeometryCommand : public QUndoCommand
{
public:
QetShapeGeometryCommand(QetShapeItem *item, const QLineF &old_line, QUndoCommand *parent = nullptr);
QetShapeGeometryCommand(QetShapeItem *item, const QRectF &old_rect, QUndoCommand *parent = nullptr);
QetShapeGeometryCommand(QetShapeItem *item, const QPolygonF &old_polygon, QUndoCommand *parent = nullptr);
void setNewLine (const QLineF &new_line) {m_new_line = new_line;}
void setNewRect (const QRectF &new_rect) {m_new_rect = new_rect;}
void setNewPolygon (const QPolygonF &new_polygon) {m_new_polygon = new_polygon;}
int id() const {return 5;}
bool mergeWith(const QUndoCommand *other);
void redo();
void undo();
private:
QetShapeItem *m_shape_item;
QLineF m_old_line, m_new_line;
QPolygonF m_old_polygon, m_new_polygon;
QRectF m_old_rect, m_new_rect;
};
#endif // QETSHAPEGEOMETRYCOMMAND_H