ImagePropertiesWidget : use QPropertyUndoCommand instead of ItemResizerCommand

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4088 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2015-08-06 17:08:45 +00:00
parent 7e81cf3c41
commit 3535ee8001
4 changed files with 12 additions and 165 deletions

View File

@@ -18,9 +18,8 @@
#include "imagepropertieswidget.h"
#include "ui_imagepropertieswidget.h"
#include "diagramimageitem.h"
#include <QUndoCommand>
#include "itemresizercommand.h"
#include "diagram.h"
#include "QPropertyUndoCommand/qpropertyundocommand.h"
/**
* @brief ImagePropertiesWidget::ImagePropertiesWidget
@@ -57,13 +56,9 @@ void ImagePropertiesWidget::setImageItem(DiagramImageItem *image)
this->setEnabled(true);
if (m_image == image) return;
if (m_image)
{
disconnect(m_image, SIGNAL(destroyed()), this, SLOT(imageWasDeleted()));
disconnect(m_image, &QGraphicsObject::scaleChanged, this, &ImagePropertiesWidget::updateUi);
}
m_image = image;
connect(m_image, SIGNAL(destroyed()), this, SLOT(imageWasDeleted()));
connect(m_image, &QGraphicsObject::scaleChanged, this, &ImagePropertiesWidget::updateUi);
m_movable = image->isMovable();
m_scale = m_image->scale();
@@ -82,7 +77,9 @@ void ImagePropertiesWidget::apply()
{
if (m_live_edit) disconnect(m_image, &QGraphicsObject::scaleChanged, this, &ImagePropertiesWidget::updateUi);
m_image->diagram()->undoStack().push(associatedUndo());
QUndoCommand *undo = associatedUndo();
if (undo)
m_image->diagram()->undoStack().push(undo);
if (m_live_edit) connect(m_image, &QGraphicsObject::scaleChanged, this, &ImagePropertiesWidget::updateUi);
}
@@ -130,13 +127,19 @@ bool ImagePropertiesWidget::setLiveEdit(bool live_edit)
/**
* @brief ImagePropertiesWidget::associatedUndo
* @return the change in an undo command (ItemResizerCommand)
* @return the change in an undo command (ItemResizerCommand).
* If there is no change return nullptr
*/
QUndoCommand* ImagePropertiesWidget::associatedUndo()
{
qreal value = ui->m_scale_slider->value();
value /= 100;
return new ItemResizerCommand(m_image, m_scale, value, tr("une image"));
if (m_scale == value) return nullptr;
QPropertyUndoCommand *undo = new QPropertyUndoCommand(m_image, "scale", m_scale, value);
undo->enableAnimation();
undo->setText(tr("Modifier la taille d'une image"));
return undo;
}
/**
@@ -161,10 +164,6 @@ void ImagePropertiesWidget::on_m_scale_slider_valueChanged(int value)
m_image->setScale(scale / 100);
}
void ImagePropertiesWidget::imageWasDeleted() {
m_image = nullptr;
}
/**
* @brief ImagePropertiesWidget::on_m_lock_pos_cb_clicked
* Set movable or not the image according to corresponding check box

View File

@@ -18,7 +18,6 @@
#ifndef IMAGEPROPERTIESWIDGET_H
#define IMAGEPROPERTIESWIDGET_H
#include <QWidget>
#include "PropertiesEditor/propertieseditorwidget.h"
class DiagramImageItem;
@@ -50,8 +49,6 @@ class ImagePropertiesWidget : public PropertiesEditorWidget
private slots:
void on_m_scale_slider_valueChanged(int value);
void imageWasDeleted();
void on_m_lock_pos_cb_clicked();
private:

View File

@@ -1,97 +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 "itemresizercommand.h"
#include "qetgraphicsitem.h"
#include "diagram.h"
/**
* @brief ItemResizerCommand::ItemResizerCommand
* Change the size of @qgi
* @param qgi item to resize
* @param old_ old size
* @param new_ new size
* @param text text to display
* @param parent undo parent
*/
ItemResizerCommand::ItemResizerCommand (QetGraphicsItem *qgi, const qreal &old_, const qreal &new_, const QString &text, QUndoCommand *parent):
QUndoCommand(parent),
m_qgi (qgi),
m_old_size (old_),
m_new_size (new_),
m_diagram (qgi->diagram()),
m_text (text),
m_first_redo(true)
{
m_animation.setTargetObject(m_qgi);
m_animation.setPropertyName("scale");
m_animation.setStartValue(m_old_size);
m_animation.setEndValue(m_new_size);
}
/**
* @brief ItemResizerCommand::~ItemResizerCommand
*/
ItemResizerCommand::~ItemResizerCommand() {}
/**
* @brief ItemResizerCommand::mergeWith
* Try to merge this command with other command
* @param other
* @return true if was merged, else false
*/
bool ItemResizerCommand::mergeWith(const QUndoCommand *other)
{
if (id() != other->id() || other->childCount()) return false;
ItemResizerCommand const *undo = static_cast<const ItemResizerCommand *>(other);
if (m_qgi != undo->m_qgi) return false;
m_new_size = undo->m_new_size;
m_animation.setEndValue(m_new_size);
return true;
}
/**
* @brief ItemResizerCommand::undo
*/
void ItemResizerCommand::undo()
{
m_diagram -> showMe();
m_animation.setDirection(QAnimationGroup::Backward);
m_animation.start();
QUndoCommand::undo();
}
/**
* @brief ItemResizerCommand::redo
*/
void ItemResizerCommand::redo()
{
m_diagram -> showMe();
setText(m_old_size < m_new_size ? QObject::tr("Agrandire %1 à %2 %").arg(m_text).arg(m_new_size*100) :
QObject::tr("Réduire %1 à %2 %").arg(m_text).arg(m_new_size*100));
if(m_first_redo)
{
m_qgi -> setScale(m_new_size);
m_first_redo = false;
}
else
{
m_animation.setDirection(QAnimationGroup::Forward);
m_animation.start();
}
QUndoCommand::redo();
}

View File

@@ -1,52 +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 ITEMRESIZERCOMMAND_H
#define ITEMRESIZERCOMMAND_H
#include <QUndoCommand>
#include <QPropertyAnimation>
class QetGraphicsItem;
class Diagram;
/**
* @brief The ItemResizerCommand class
* This Undo command manage the scale of QetGraphicsItem.
*/
class ItemResizerCommand : public QUndoCommand
{
public:
ItemResizerCommand (QetGraphicsItem *qgi, const qreal &old_, const qreal &new_,const QString &text, QUndoCommand *parent = 0);
virtual ~ItemResizerCommand();
public:
virtual int id() const {return 3;}
virtual bool mergeWith(const QUndoCommand *other);
virtual void undo();
virtual void redo();
private:
QetGraphicsItem *m_qgi;
qreal m_old_size, m_new_size;
Diagram *m_diagram;
QString m_text;
QPropertyAnimation m_animation;
bool m_first_redo;
};
#endif // ITEMRESIZERCOMMAND_H