ellipse editor : use QPropertyUndoCommand instead of ChangePartCommand

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4067 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-07-23 09:57:09 +00:00
parent a00bfdf00f
commit 65d4b08c86
5 changed files with 106 additions and 161 deletions

View File

@@ -135,10 +135,16 @@ void ArcEditor::updateArcS()
{ {
if (m_locked) return; if (m_locked) return;
m_locked = true; m_locked = true;
QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "startAngle", part->property("startAngle"), ((start_angle -> value() * -1) + 90) * 16); double value = ((start_angle -> value() * -1) + 90) * 16;
undo->setText("Modifier l'angle de depart d'un arc");
undo->enableAnimation(); if (value != part->property("startAngle"))
elementScene()->undoStack().push(undo); {
QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "startAngle", part->property("startAngle"), value);
undo->setText("Modifier l'angle de depart d'un arc");
undo->enableAnimation();
elementScene()->undoStack().push(undo);
}
m_locked = false; m_locked = false;
} }
@@ -150,10 +156,16 @@ void ArcEditor::updateArcA()
{ {
if (m_locked) return; if (m_locked) return;
m_locked = true; m_locked = true;
QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "spanAngle", part->property("spanAngle"), angle -> value() * -16); double value = angle -> value() * -16;
undo->setText("Modifier l'angle d'un arc");
undo->enableAnimation(); if (value != part->property("spanAngle"))
elementScene()->undoStack().push(undo); {
QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "spanAngle", part->property("spanAngle"), value);
undo->setText("Modifier l'angle d'un arc");
undo->enableAnimation();
elementScene()->undoStack().push(undo);
}
m_locked = false; m_locked = false;
} }
@@ -167,10 +179,15 @@ void ArcEditor::updateArcRect()
m_locked = true; m_locked = true;
QPointF point = part->mapFromScene(x->value() - h->value()/2, y->value() - v->value()/2); QPointF point = part->mapFromScene(x->value() - h->value()/2, y->value() - v->value()/2);
QRectF rect(point, QSizeF(h->value(), v->value())); QRectF rect(point, QSizeF(h->value(), v->value()));
QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
undo->setText("Modifier un arc"); if (rect != part->property("rect"))
undo->enableAnimation(); {
elementScene()->undoStack().push(undo); QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
undo->setText("Modifier un arc");
undo->enableAnimation();
elementScene()->undoStack().push(undo);
}
m_locked = false; m_locked = false;
} }

View File

@@ -18,6 +18,8 @@
#include "ellipseeditor.h" #include "ellipseeditor.h"
#include "styleeditor.h" #include "styleeditor.h"
#include "partellipse.h" #include "partellipse.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
#include "elementscene.h"
/** /**
Constructeur Constructeur
@@ -27,7 +29,8 @@
*/ */
EllipseEditor::EllipseEditor(QETElementEditor *editor, PartEllipse *ellipse, QWidget *parent) : EllipseEditor::EllipseEditor(QETElementEditor *editor, PartEllipse *ellipse, QWidget *parent) :
ElementItemEditor(editor, parent), ElementItemEditor(editor, parent),
part(ellipse) part(ellipse),
m_locked(false)
{ {
style_ = new StyleEditor(editor); style_ = new StyleEditor(editor);
@@ -74,20 +77,28 @@ EllipseEditor::~EllipseEditor() {
@param new_part Nouvelle primitive a editer @param new_part Nouvelle primitive a editer
@return true si l'editeur a accepter d'editer la primitive, false sinon @return true si l'editeur a accepter d'editer la primitive, false sinon
*/ */
bool EllipseEditor::setPart(CustomElementPart *new_part) { bool EllipseEditor::setPart(CustomElementPart *new_part)
if (!new_part) { {
if (!new_part)
{
if (part)
disconnect(part, &PartEllipse::rectChanged, this, &EllipseEditor::updateForm);
part = 0; part = 0;
style_ -> setPart(0); style_ -> setPart(0);
return(true); return(true);
} }
if (PartEllipse *part_ellipse = dynamic_cast<PartEllipse *>(new_part)) { if (PartEllipse *part_ellipse = dynamic_cast<PartEllipse *>(new_part))
{
if (part == part_ellipse) return true;
if (part)
disconnect(part, &PartEllipse::rectChanged, this, &EllipseEditor::updateForm);
part = part_ellipse; part = part_ellipse;
style_ -> setPart(part); style_ -> setPart(part);
updateForm(); updateForm();
connect(part, &PartEllipse::rectChanged, this, &EllipseEditor::updateForm);
return(true); return(true);
} else {
return(false);
} }
return(false);
} }
/** /**
@@ -97,36 +108,36 @@ CustomElementPart *EllipseEditor::currentPart() const {
return(part); return(part);
} }
/** void EllipseEditor::editingFinished()
Met a jour l'ellipse a partir des donnees du formulaire {
*/ if (m_locked) return;
void EllipseEditor::updateEllipse() { m_locked = true;
if (!part) return; QPointF point = part->mapFromScene(x->value() - h->value()/2, y->value() - v->value()/2);
part -> setProperty("centerX", x -> value()); QRectF rect(point, QSizeF(h->value(), v->value()));
part -> setProperty("centerY", y -> value());
part -> setProperty("diameter_h", h -> value());
part -> setProperty("diameter_v", v -> value());
}
/// Met a jour l'abscisse du centre de l'ellipse et cree un objet d'annulation if (rect != part->property("rect"))
void EllipseEditor::updateEllipseX() { addChangePartCommand(tr("abscisse"), part, "centerX", x -> value()); } {
/// Met a jour l'ordonnee du centre de l'ellipse et cree un objet d'annulation QPropertyUndoCommand *undo= new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
void EllipseEditor::updateEllipseY() { addChangePartCommand(tr("ordonnée"), part, "centerY", y -> value()); } undo->setText("Modifier une ellipse");
/// Met a jour le diametre horizontal de l'ellipse et cree un objet d'annulation undo->enableAnimation();
void EllipseEditor::updateEllipseH() { addChangePartCommand(tr("diamètre horizontal"), part, "diameter_h", h -> value()); } elementScene()->undoStack().push(undo);
/// Met a jour le diametre vertical de l'ellipse et cree un objet d'annulation }
void EllipseEditor::updateEllipseV() { addChangePartCommand(tr("diamètre vertical"), part, "diameter_v", v -> value()); }
m_locked = false;
}
/** /**
Met a jour le formulaire d'edition Met a jour le formulaire d'edition
*/ */
void EllipseEditor::updateForm() { void EllipseEditor::updateForm()
{
if (!part) return; if (!part) return;
activeConnections(false); activeConnections(false);
x->setValue(part->property("centerX").toReal()); QRectF rect = part->property("rect").toRectF();
y->setValue(part->property("centerY").toReal()); x->setValue(part->mapToScene(rect.topLeft()).x() + (rect.width()/2));
h->setValue(part->property("diameter_h").toReal()); y->setValue(part->mapToScene(rect.topLeft()).y() + (rect.height()/2));
v->setValue(part->property("diameter_v").toReal()); h->setValue(rect.width());
v->setValue(rect.height());
activeConnections(true); activeConnections(true);
} }
@@ -134,16 +145,20 @@ void EllipseEditor::updateForm() {
Active ou desactive les connexionx signaux/slots entre les widgets internes. Active ou desactive les connexionx signaux/slots entre les widgets internes.
@param active true pour activer les connexions, false pour les desactiver @param active true pour activer les connexions, false pour les desactiver
*/ */
void EllipseEditor::activeConnections(bool active) { void EllipseEditor::activeConnections(bool active)
if (active) { {
connect(x, SIGNAL(editingFinished()), this, SLOT(updateEllipseX())); if (active)
connect(y, SIGNAL(editingFinished()), this, SLOT(updateEllipseY())); {
connect(h, SIGNAL(editingFinished()), this, SLOT(updateEllipseH())); connect(x, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
connect(v, SIGNAL(editingFinished()), this, SLOT(updateEllipseV())); connect(y, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
} else { connect(h, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
disconnect(x, SIGNAL(editingFinished()), this, SLOT(updateEllipseX())); connect(v, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
disconnect(y, SIGNAL(editingFinished()), this, SLOT(updateEllipseY())); }
disconnect(h, SIGNAL(editingFinished()), this, SLOT(updateEllipseH())); else
disconnect(v, SIGNAL(editingFinished()), this, SLOT(updateEllipseV())); {
disconnect(x, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
disconnect(y, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
disconnect(h, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
disconnect(v, SIGNAL(editingFinished()), this, SLOT(editingFinished()));
} }
} }

View File

@@ -17,42 +17,44 @@
*/ */
#ifndef ELLIPSE_EDITOR_H #ifndef ELLIPSE_EDITOR_H
#define ELLIPSE_EDITOR_H #define ELLIPSE_EDITOR_H
#include <QtWidgets>
#include "elementitemeditor.h" #include "elementitemeditor.h"
class PartEllipse; class PartEllipse;
class StyleEditor; class StyleEditor;
class QDoubleSpinBox;
/** /**
This class provides a widget to edit ellipses within the element editor. This class provides a widget to edit ellipses within the element editor.
*/ */
class EllipseEditor : public ElementItemEditor { class EllipseEditor : public ElementItemEditor
{
Q_OBJECT Q_OBJECT
// constructors, destructor
// constructors, destructor
public: public:
EllipseEditor(QETElementEditor *, PartEllipse * = 0, QWidget * = 0); EllipseEditor(QETElementEditor *, PartEllipse * = 0, QWidget * = 0);
virtual ~EllipseEditor(); virtual ~EllipseEditor();
private: private:
EllipseEditor(const EllipseEditor &); EllipseEditor(const EllipseEditor &);
// attributes // attributes
private: private:
PartEllipse *part; PartEllipse *part;
StyleEditor *style_; StyleEditor *style_;
QDoubleSpinBox *x, *y, *h, *v; QDoubleSpinBox *x, *y, *h, *v;
bool m_locked;
// methods // methods
public: public:
virtual bool setPart(CustomElementPart *); virtual bool setPart(CustomElementPart *);
virtual CustomElementPart *currentPart() const; virtual CustomElementPart *currentPart() const;
public slots: public slots:
void updateEllipse(); void editingFinished();
void updateEllipseX(); void updateForm();
void updateEllipseY();
void updateEllipseH();
void updateEllipseV();
void updateForm();
private: private:
void activeConnections(bool); void activeConnections(bool);
}; };
#endif #endif

View File

@@ -159,74 +159,3 @@ void AbstractPartEllipse::setSpanAngle(const int &span_angle)
m_span_angle = span_angle; m_span_angle = span_angle;
emit spanAngleChanged(); emit spanAngleChanged();
} }
/**
* @brief AbstractPartEllipse::setCenterX
* Like setCenter but Y keep unchanged
* See setCenter(const QPointF &center)
* @param x
*/
void AbstractPartEllipse::setCenterX(const qreal x)
{
QPointF pos = mapToParent(m_rect.center());
pos.setX(x);
setCenter(pos);
}
/**
* @brief AbstractPartEllipse::setCenterY
* Like setCenter but X keep unchanged
* See setCenter(const QPointF &center)
* @param y
*/
void AbstractPartEllipse::setCenterY(const qreal y)
{
QPointF pos = mapToParent(m_rect.center());
pos.setY(y);
setCenter(pos);
}
/**
* @brief AbstractPartEllipse::setCenter
* This is a convenience method to setPos().
* Adjust the position of this item,
* so that the center of the rectangle is at the given position(position in parent coordinates).
* @param center
*/
void AbstractPartEllipse::setCenter(const QPointF &center)
{
QPointF pos = center - m_rect.center();
setPos(pos);
}
/**
* @brief AbstractPartEllipse::setWidth
* Set new width for rectangle.
* The center of rectangle is unchanged,
* The coordinates of the left side and right side of the rectangle change
* @param w
*/
void AbstractPartEllipse::setWidth(const qreal w)
{
qreal new_width = qAbs(w);
QRectF current_rect = rect();
current_rect.translate((new_width - current_rect.width()) / -2.0, 0.0);
current_rect.setWidth(new_width);
setRect(current_rect);
}
/**
* @brief AbstractPartEllipse::setHeight
* Set new height for rectangle
* The center of rectangle is unchanged
* The coordinates of the top side and bottom side of the rectangle change
* @param h
*/
void AbstractPartEllipse::setHeight(const qreal h)
{
qreal new_height = qAbs(h);
QRectF current_rect = rect();
current_rect.translate(0.0, (new_height - current_rect.height()) / -2.0);
current_rect.setHeight(new_height);
setRect(current_rect);
}

View File

@@ -35,10 +35,6 @@ class AbstractPartEllipse : public CustomElementGraphicPart
Q_OBJECT Q_OBJECT
Q_PROPERTY(int startAngle READ startAngle WRITE setStartAngle) Q_PROPERTY(int startAngle READ startAngle WRITE setStartAngle)
Q_PROPERTY(int spanAngle READ spanAngle WRITE setSpanAngle) Q_PROPERTY(int spanAngle READ spanAngle WRITE setSpanAngle)
Q_PROPERTY(qreal centerX READ centerX WRITE setCenterX)
Q_PROPERTY(qreal centerY READ centerY WRITE setCenterY)
Q_PROPERTY(qreal diameter_h READ width WRITE setWidth)
Q_PROPERTY(qreal diameter_v READ height WRITE setHeight)
Q_PROPERTY(QRectF rect READ rect WRITE setRect) Q_PROPERTY(QRectF rect READ rect WRITE setRect)
// constructors, destructor // constructors, destructor
@@ -74,20 +70,6 @@ class AbstractPartEllipse : public CustomElementGraphicPart
int spanAngle () const {return m_span_angle;} int spanAngle () const {return m_span_angle;}
void setSpanAngle (const int &span_angle); void setSpanAngle (const int &span_angle);
qreal centerX() const {return mapToScene(rect().center()).x() ;}
void setCenterX(const qreal x);
qreal centerY() const {return mapToScene(rect().center()).y();}
void setCenterY(const qreal y);
void setCenter (const QPointF &center);
qreal width() const {return rect().width();}
void setWidth(const qreal w);
qreal height() const {return rect().height();}
void setHeight (const qreal h);
protected: protected:
QList<QPointF> saved_points_; QList<QPointF> saved_points_;
QRectF m_rect; QRectF m_rect;