mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +01:00
polygon editor : use QPropertyUndoCommand instead of ChangePartCommand
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4069 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -360,40 +360,6 @@ void ChangePartCommand::redo()
|
||||
ElementEditionCommand::redo();
|
||||
}
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@param p Polygone edite
|
||||
@param o_points points avant le changement
|
||||
@param n_points points apres le changement
|
||||
@param parent QUndoCommand parent
|
||||
*/
|
||||
ChangePolygonPointsCommand::ChangePolygonPointsCommand(
|
||||
PartPolygon *p,
|
||||
const QVector<QPointF> &o_points,
|
||||
const QVector<QPointF> &n_points,
|
||||
QUndoCommand *parent
|
||||
) :
|
||||
ElementEditionCommand(QObject::tr("modification points polygone", "undo caption"), 0, 0, parent),
|
||||
polygon(p),
|
||||
old_points(o_points),
|
||||
new_points(n_points)
|
||||
{
|
||||
}
|
||||
|
||||
/// Destructeur
|
||||
ChangePolygonPointsCommand::~ChangePolygonPointsCommand() {
|
||||
}
|
||||
|
||||
/// Annule le changement
|
||||
void ChangePolygonPointsCommand::undo() {
|
||||
polygon -> setPolygon(old_points);
|
||||
}
|
||||
|
||||
/// Refait le changement
|
||||
void ChangePolygonPointsCommand::redo() {
|
||||
polygon -> setPolygon(new_points);
|
||||
}
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@param element_scene Element edite
|
||||
|
||||
@@ -18,12 +18,10 @@
|
||||
#ifndef EDITOR_COMMANDS_H
|
||||
#define EDITOR_COMMANDS_H
|
||||
#include "customelementpart.h"
|
||||
#include "partpolygon.h"
|
||||
#include "elementview.h"
|
||||
#include "elementscene.h"
|
||||
#include "elementcontent.h"
|
||||
#include "qgimanager.h"
|
||||
#include <QtWidgets>
|
||||
|
||||
/**
|
||||
* @brief The ElementEditionCommand class
|
||||
@@ -201,32 +199,6 @@ class ChangePartCommand : public ElementEditionCommand
|
||||
QVariant m_new_value;
|
||||
};
|
||||
|
||||
/**
|
||||
This command changes the points of a polygon when editing an electrical
|
||||
element.
|
||||
*/
|
||||
class ChangePolygonPointsCommand : public ElementEditionCommand {
|
||||
// constructors, destructor
|
||||
public:
|
||||
ChangePolygonPointsCommand(PartPolygon *, const QVector<QPointF> &, const QVector<QPointF> &, QUndoCommand * = 0);
|
||||
virtual ~ChangePolygonPointsCommand();
|
||||
private:
|
||||
ChangePolygonPointsCommand(const ChangePolygonPointsCommand &);
|
||||
|
||||
// methods
|
||||
public:
|
||||
virtual void undo();
|
||||
virtual void redo();
|
||||
|
||||
// attributes
|
||||
/// Changed polygon
|
||||
PartPolygon *polygon;
|
||||
/// Former points
|
||||
QVector<QPointF> old_points;
|
||||
/// New points
|
||||
QVector<QPointF> new_points;
|
||||
};
|
||||
|
||||
/**
|
||||
This command changes the translated names of an electrical element.
|
||||
*/
|
||||
|
||||
@@ -199,6 +199,7 @@ void PartPolygon::setPolygon(const QPolygonF &polygon)
|
||||
if (m_polygon == polygon) return;
|
||||
prepareGeometryChange();
|
||||
m_polygon = polygon;
|
||||
emit polygonChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,6 +240,14 @@ void PartPolygon::removeLastPoint()
|
||||
}
|
||||
}
|
||||
|
||||
void PartPolygon::setClosed(bool close)
|
||||
{
|
||||
if (m_closed == close) return;
|
||||
prepareGeometryChange();
|
||||
m_closed = close;
|
||||
emit closedChange();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief PartPolygon::mousePressEvent
|
||||
* Handle mouse press event
|
||||
@@ -274,6 +283,7 @@ void PartPolygon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
||||
QPointF pos_ = event->modifiers() == Qt::ControlModifier ? event->pos() : mapFromScene(elementScene()->snapToGrid(event->scenePos()));
|
||||
prepareGeometryChange();
|
||||
m_polygon.replace(m_handler_index, pos_);
|
||||
emit polygonChanged();
|
||||
}
|
||||
else
|
||||
CustomElementGraphicPart::mouseMoveEvent(event);
|
||||
|
||||
@@ -45,6 +45,10 @@ class PartPolygon : public CustomElementGraphicPart
|
||||
private:
|
||||
PartPolygon(const PartPolygon &);
|
||||
|
||||
signals:
|
||||
void closedChange();
|
||||
void polygonChanged();
|
||||
|
||||
// methods
|
||||
public:
|
||||
enum { Type = UserType + 1105 };
|
||||
@@ -78,7 +82,7 @@ class PartPolygon : public CustomElementGraphicPart
|
||||
void removeLastPoint ();
|
||||
|
||||
bool isClosed () const {return m_closed;}
|
||||
void setClosed (bool c) {m_closed = c;}
|
||||
void setClosed (bool close);
|
||||
|
||||
protected:
|
||||
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
#include "polygoneditor.h"
|
||||
#include "partpolygon.h"
|
||||
#include "elementscene.h"
|
||||
#include "editorcommands.h"
|
||||
#include "qetmessagebox.h"
|
||||
#include "styleeditor.h"
|
||||
#include "QPropertyUndoCommand/qpropertyundocommand.h"
|
||||
|
||||
/**
|
||||
Constructeur
|
||||
@@ -58,45 +58,40 @@ PolygonEditor::PolygonEditor(QETElementEditor *editor, PartPolygon *p, QWidget *
|
||||
PolygonEditor::~PolygonEditor() {
|
||||
}
|
||||
|
||||
/**
|
||||
Met a jour le polygone a partir des donnees du formulaire : points et etat ferme ou non
|
||||
*/
|
||||
void PolygonEditor::updatePolygon() {
|
||||
updatePolygonPoints();
|
||||
updatePolygonClosedState();
|
||||
}
|
||||
|
||||
/**
|
||||
Met a jour les points du polygone et cree un objet d'annulation
|
||||
*/
|
||||
void PolygonEditor::updatePolygonPoints() {
|
||||
void PolygonEditor::updatePolygonPoints()
|
||||
{
|
||||
if (!part) return;
|
||||
QVector<QPointF> points = getPointsFromTree();
|
||||
if (points.count() < 2) {
|
||||
QET::QetMessageBox::warning(
|
||||
this,
|
||||
tr("Erreur", "message box title"),
|
||||
tr("Le polygone doit comporter au moins deux points.", "message box content")
|
||||
);
|
||||
QPolygonF points = getPointsFromTree();
|
||||
if (points.count() < 2)
|
||||
{
|
||||
QET::QetMessageBox::warning(this, tr("Erreur", "message box title"), tr("Le polygone doit comporter au moins deux points.", "message box content"));
|
||||
return;
|
||||
}
|
||||
undoStack().push(new ChangePolygonPointsCommand(part, part -> polygon(), points));
|
||||
|
||||
if (points != part->polygon())
|
||||
{
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "polygon", part->property("polygon"), points);
|
||||
undo->setText(tr("Modifier un polygone"));
|
||||
undoStack().push(undo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Met a jour l'etat ferme ou non du polygone
|
||||
*/
|
||||
void PolygonEditor::updatePolygonClosedState() {
|
||||
void PolygonEditor::updatePolygonClosedState()
|
||||
{
|
||||
if (!part) return;
|
||||
undoStack().push(
|
||||
new ChangePartCommand(
|
||||
tr("fermeture du polygone"),
|
||||
part,
|
||||
"closed",
|
||||
QVariant(!close_polygon.isChecked()),
|
||||
QVariant(close_polygon.isChecked())
|
||||
)
|
||||
);
|
||||
bool close = close_polygon.isChecked();
|
||||
if (close != part->isClosed())
|
||||
{
|
||||
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "closed", part->property("closed"), close);
|
||||
undo->setText(tr("Modifier un polygone"));
|
||||
undoStack().push(undo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,20 +121,35 @@ void PolygonEditor::updateForm() {
|
||||
@param new_part Nouvelle primitive a editer
|
||||
@return true si l'editeur a accepter d'editer la primitive, false sinon
|
||||
*/
|
||||
bool PolygonEditor::setPart(CustomElementPart *new_part) {
|
||||
if (!new_part) {
|
||||
bool PolygonEditor::setPart(CustomElementPart *new_part)
|
||||
{
|
||||
if (!new_part)
|
||||
{
|
||||
if (part)
|
||||
{
|
||||
disconnect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm);
|
||||
disconnect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm);
|
||||
}
|
||||
part = 0;
|
||||
style_ -> setPart(0);
|
||||
return(true);
|
||||
}
|
||||
if (PartPolygon *part_polygon = dynamic_cast<PartPolygon *>(new_part)) {
|
||||
if (PartPolygon *part_polygon = dynamic_cast<PartPolygon *>(new_part))
|
||||
{
|
||||
if (part == part_polygon) return true;
|
||||
if (part)
|
||||
{
|
||||
disconnect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm);
|
||||
disconnect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm);
|
||||
}
|
||||
part = part_polygon;
|
||||
style_ -> setPart(part);
|
||||
updateForm();
|
||||
connect(part, &PartPolygon::polygonChanged, this, &PolygonEditor::updateForm);
|
||||
connect(part, &PartPolygon::closedChange, this, &PolygonEditor::updateForm);
|
||||
return(true);
|
||||
} else {
|
||||
return(false);
|
||||
}
|
||||
return(false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -57,7 +57,6 @@ class PolygonEditor : public ElementItemEditor {
|
||||
QVector<QPointF> getPointsFromTree();
|
||||
|
||||
public slots:
|
||||
void updatePolygon();
|
||||
void updatePolygonPoints();
|
||||
void updatePolygonClosedState();
|
||||
void updateForm();
|
||||
|
||||
Reference in New Issue
Block a user