Rectangle editor widget : use QPropertyUndoCommand instead of ChangePartCommand

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4064 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-07-22 13:19:43 +00:00
parent f24b1dd8d8
commit 6a902ca530
7 changed files with 115 additions and 151 deletions

View File

@@ -16,7 +16,7 @@
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "qpropertyundocommand.h"
#include <QAnimationGroup>
#include <QPropertyAnimation>
/**
* @brief QPropertyUndoCommand::QPropertyUndoCommand
@@ -33,12 +33,7 @@ QPropertyUndoCommand::QPropertyUndoCommand(QObject *object, const char *property
m_old_value(old_value),
m_new_value(new_value),
m_animate(false)
{
m_animation.setTargetObject(object);
m_animation.setPropertyName(property_name);
m_animation.setStartValue(old_value);
m_animation.setEndValue(new_value);
}
{}
/**
* @brief QPropertyUndoCommand::QPropertyUndoCommand
@@ -55,21 +50,15 @@ QPropertyUndoCommand::QPropertyUndoCommand(QObject *object, const char *property
m_property_name(property_name),
m_old_value(old_value),
m_animate(false)
{
m_animation.setTargetObject(object);
m_animation.setPropertyName(property_name);
m_animation.setStartValue(old_value);
}
{}
/**
* @brief QPropertyUndoCommand::setNewValue
* Set the new value of the property (set with redo) to @new_value
* @param new_value
*/
void QPropertyUndoCommand::setNewValue(const QVariant &new_value)
{
void QPropertyUndoCommand::setNewValue(const QVariant &new_value) {
m_new_value = new_value;
m_animation.setEndValue(new_value);
}
/**
@@ -93,7 +82,6 @@ bool QPropertyUndoCommand::mergeWith(const QUndoCommand *other)
QPropertyUndoCommand const *undo = static_cast<const QPropertyUndoCommand *>(other);
if (m_object != undo->m_object || m_property_name != undo->m_property_name) return false;
m_new_value = undo->m_new_value;
m_animation.setEndValue(m_new_value);
return true;
}
@@ -107,8 +95,10 @@ void QPropertyUndoCommand::redo()
{
if (m_animate)
{
m_animation.setDirection(QAnimationGroup::Forward);
m_animation.start();
QPropertyAnimation *animation = new QPropertyAnimation(m_object, m_property_name);
animation->setStartValue(m_old_value);
animation->setEndValue(m_new_value);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
else
m_object->setProperty(m_property_name, m_new_value);
@@ -127,8 +117,10 @@ void QPropertyUndoCommand::undo()
{
if (m_animate)
{
m_animation.setDirection(QAnimationGroup::Backward);
m_animation.start();
QPropertyAnimation *animation = new QPropertyAnimation(m_object, m_property_name);
animation->setStartValue(m_new_value);
animation->setEndValue(m_old_value);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
else
m_object->setProperty(m_property_name, m_old_value);

View File

@@ -20,7 +20,6 @@
#include <QUndoCommand>
#include <QVariant>
#include <QPropertyAnimation>
class QObject;
@@ -50,7 +49,6 @@ class QPropertyUndoCommand : public QUndoCommand
const char *m_property_name;
QVariant m_old_value, m_new_value;
bool m_animate;
QPropertyAnimation m_animation;
};
#endif // QPROPERTYUNDOCOMMAND_H

View File

@@ -83,7 +83,7 @@ bool ESEventAddRect::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
updateHelpCross(event -> scenePos());
if (!m_rect) return false;
QRectF rect(m_rect->rectTopLeft(), m_scene->snapToGrid(event -> scenePos()));
QRectF rect(m_rect->rect().topLeft(), m_scene->snapToGrid(event -> scenePos()));
m_rect -> setRect(rect);
return true;
}

View File

@@ -129,47 +129,7 @@ void PartRectangle::setRect(const QRectF &rect)
if (rect == m_rect) return;
prepareGeometryChange();
m_rect = rect;
}
/**
* @brief PartRectangle::rectTopLeft
* @return the rectangle top left in item coordinate
*/
QPointF PartRectangle::rectTopLeft() const {
return m_rect.topLeft();
}
/**
* @brief PartRectangle::setRectTopLeft
* @param point, set the rectangle top left in item coordinate.
* The rectangle size is unchanged
*/
void PartRectangle::setRectTopLeft(const QPointF &point) {
m_rect.moveTopLeft(point);
}
/**
* @brief PartRectangle::setWidth
* Sets the width of the rectangle to the given width.
* The right edge is changed, but not the left one.
* @param w new value
*/
void PartRectangle::setWidth(qreal w)
{
prepareGeometryChange();
m_rect.setWidth(qAbs(w));
}
/**
* @brief PartRectangle::setHeight
* Sets the height of the rectangle to the given height.
* The bottom edge is changed, but not the top one.
* @param h new value
*/
void PartRectangle::setHeight(qreal h)
{
prepareGeometryChange();
m_rect.setHeight(qAbs(h));
emit rectChanged();
}
/**

View File

@@ -32,9 +32,6 @@ class PartRectangle : public CustomElementGraphicPart
{
Q_OBJECT
Q_PROPERTY(QPointF rectTopLeft READ rectTopLeft WRITE setRectTopLeft)
Q_PROPERTY(qreal width READ width WRITE setWidth)
Q_PROPERTY(qreal height READ height WRITE setHeight)
Q_PROPERTY(QRectF rect READ rect WRITE setRect)
// constructors, destructor
@@ -45,6 +42,9 @@ class PartRectangle : public CustomElementGraphicPart
private:
PartRectangle(const PartRectangle &);
signals:
void rectChanged();
// methods
public:
enum { Type = UserType + 1109 };
@@ -63,15 +63,6 @@ class PartRectangle : public CustomElementGraphicPart
QRectF rect() const;
void setRect(const QRectF &rect);
QPointF rectTopLeft () const;
void setRectTopLeft (const QPointF &point);
qreal width () const {return rect().width();}
void setWidth (qreal w);
qreal height () const { return rect().height();}
void setHeight (qreal h);
virtual QRectF sceneGeometricRect() const;
virtual QPointF sceneTopLeft() const;

View File

@@ -18,6 +18,8 @@
#include "rectangleeditor.h"
#include "partrectangle.h"
#include "styleeditor.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
#include "elementscene.h"
/**
Constructeur
@@ -27,7 +29,8 @@
*/
RectangleEditor::RectangleEditor(QETElementEditor *editor, PartRectangle *rect, QWidget *parent) :
ElementItemEditor(editor, parent),
part(rect)
part(rect),
m_locked(false)
{
style_ = new StyleEditor(editor);
@@ -67,31 +70,41 @@ RectangleEditor::~RectangleEditor() {
}
/**
Permet de specifier a cet editeur quelle primitive il doit editer. A noter
qu'un editeur peut accepter ou refuser d'editer une primitive.
L'editeur de rectangle acceptera d'editer la primitive new_part s'il s'agit
d'un objet de la classe PartRectangle.
@param new_part Nouvelle primitive a editer
@return true si l'editeur a accepter d'editer la primitive, false sinon
* @brief RectangleEditor::setPart
* Specifie to this editor the part to edit.
* Note that an editor can accept or refuse to edit a part. This editor accept only partRectangle.
* @param new_part
* @return
*/
bool RectangleEditor::setPart(CustomElementPart *new_part) {
if (!new_part) {
bool RectangleEditor::setPart(CustomElementPart *new_part)
{
if (!new_part)
{
if (part)
disconnect(part, &PartRectangle::rectChanged, this, &RectangleEditor::updateForm);
part = 0;
style_ -> setPart(0);
return(true);
}
if (PartRectangle *part_rectangle = dynamic_cast<PartRectangle *>(new_part)) {
if (PartRectangle *part_rectangle = dynamic_cast<PartRectangle *>(new_part))
{
if (part == part_rectangle) return true;
if (part)
disconnect(part, &PartRectangle::rectChanged, this, &RectangleEditor::updateForm);
part = part_rectangle;
style_ -> setPart(part);
updateForm();
connect(part, &PartRectangle::rectChanged, this, &RectangleEditor::updateForm);
return(true);
} else {
return(false);
}
return(false);
}
/**
@return la primitive actuellement editee, ou 0 si ce widget n'en edite pas
* @brief RectangleEditor::currentPart
* @return the curent edited part, or 0 if there is no edited part
*/
CustomElementPart *RectangleEditor::currentPart() const {
return(part);
@@ -106,52 +119,63 @@ QPointF RectangleEditor::editedTopLeft() const {
}
/**
Met a jour le rectangle a partir des donnees du formulaire
* @brief RectangleEditor::updateForm
* Update the values displayed by this widget
*/
void RectangleEditor::updateRectangle() {
if (!part) return;
part -> setProperty("rectTopLeft", editedTopLeft());
part -> setProperty("width", w -> value());
part -> setProperty("height", h -> value());
}
/// Met a jour l'abscisse du coin superieur gauche du rectangle et cree un objet d'annulation
void RectangleEditor::updateRectangleX() { addChangePartCommand(tr("abscisse"), part, "rectTopLeft", editedTopLeft());}
/// Met a jour l'ordonnee du coin superieur gauche du rectangle et cree un objet d'annulation
void RectangleEditor::updateRectangleY() { addChangePartCommand(tr("ordonnée"), part, "rectTopLeft", editedTopLeft());}
/// Met a jour la largeur du rectangle et cree un objet d'annulation
void RectangleEditor::updateRectangleW() { addChangePartCommand(tr("largeur"), part, "width", w -> value());}
/// Met a jour la hauteur du rectangle et cree un objet d'annulation
void RectangleEditor::updateRectangleH() { addChangePartCommand(tr("hauteur"), part, "height", h -> value());}
/**
Met a jour le formulaire d'edition
*/
void RectangleEditor::updateForm() {
void RectangleEditor::updateForm()
{
if (!part) return;
activeConnections(false);
QPointF p = part->mapToScene(part->property("rectTopLeft").toPointF());
QRectF rect = part->property("rect").toRectF();
QPointF p = part->mapToScene(rect.topLeft());
x->setValue(p.x());
y->setValue(p.y());
w->setValue(part->property("width").toReal());
h->setValue(part->property("height").toReal());
w->setValue(rect.width());
h->setValue(rect.height());
activeConnections(true);
}
/**
Active ou desactive les connexionx signaux/slots entre les widgets internes.
@param active true pour activer les connexions, false pour les desactiver
* @brief RectangleEditor::editingFinished
* Slot called when a editor widget is finish to be edited
* Update the geometry of the rectangle according to value of editing widget.
*/
void RectangleEditor::activeConnections(bool active) {
if (active) {
connect(x, SIGNAL(editingFinished()), this, SLOT(updateRectangleX()));
connect(y, SIGNAL(editingFinished()), this, SLOT(updateRectangleY()));
connect(w, SIGNAL(editingFinished()), this, SLOT(updateRectangleW()));
connect(h, SIGNAL(editingFinished()), this, SLOT(updateRectangleH()));
} else {
disconnect(x, SIGNAL(editingFinished()), this, SLOT(updateRectangleX()));
disconnect(y, SIGNAL(editingFinished()), this, SLOT(updateRectangleY()));
disconnect(w, SIGNAL(editingFinished()), this, SLOT(updateRectangleW()));
disconnect(h, SIGNAL(editingFinished()), this, SLOT(updateRectangleH()));
void RectangleEditor::editingFinished()
{
if (m_locked) return;
m_locked = true;
QRectF rect(editedTopLeft(), QSizeF(w->value(), h->value()));
QPropertyUndoCommand *undo = new QPropertyUndoCommand(part, "rect", part->property("rect"), rect);
undo->setText(tr("Modifier un rectangle"));
undo->enableAnimation();
elementScene()->undoStack().push(undo);
m_locked = false;
}
/**
* @brief RectangleEditor::activeConnections
* Enable/disable connection between editor widget and slot editingFinished
* True == enable | false == disable
* @param active
*/
void RectangleEditor::activeConnections(bool active)
{
if (active)
{
connect(x, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
connect(y, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
connect(w, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
connect(h, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
}
else
{
disconnect(x, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
disconnect(y, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
disconnect(w, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
disconnect(h, &QDoubleSpinBox::editingFinished, this, &RectangleEditor::editingFinished);
}
}

View File

@@ -24,8 +24,10 @@ class StyleEditor;
/**
This class provides a widget to edit rectangles within the element editor.
*/
class RectangleEditor : public ElementItemEditor {
class RectangleEditor : public ElementItemEditor
{
Q_OBJECT
// constructors, destructor
public:
RectangleEditor(QETElementEditor *, PartRectangle * = 0, QWidget * = 0);
@@ -38,6 +40,7 @@ class RectangleEditor : public ElementItemEditor {
PartRectangle *part;
StyleEditor *style_;
QDoubleSpinBox *x, *y, *w, *h;
bool m_locked;
// methods
public:
@@ -46,12 +49,8 @@ class RectangleEditor : public ElementItemEditor {
QPointF editedTopLeft () const;
public slots:
void updateRectangle();
void updateRectangleX();
void updateRectangleY();
void updateRectangleW();
void updateRectangleH();
void updateForm();
void editingFinished();
private:
void activeConnections(bool);