diff --git a/dev_doc/ID_of_QUndoCommand.txt b/dev_doc/ID_of_QUndoCommand.txt index 5ae0d08d3..d89ee1d32 100755 --- a/dev_doc/ID_of_QUndoCommand.txt +++ b/dev_doc/ID_of_QUndoCommand.txt @@ -1,2 +1,3 @@ ChangeElementInformationCommand = 1 LinkElementCommand = 2 +ItemResizerCommand = 3 diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 5478f927a..4d43dff5c 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -1054,49 +1054,6 @@ void ChangeSeveralConductorsPropertiesCommand::redo() { } } -/** - * @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, qreal &old_, qreal &new_, const QString &text, QUndoCommand *parent): - QUndoCommand(parent), - m_qgi ( qgi ), - old_size ( old_ ), - new_size ( new_ ), - diagram ( qgi->diagram() ), - m_text ( text ) -{} - -/** - * @brief ItemResizerCommand::~ItemResizerCommand - */ -ItemResizerCommand::~ItemResizerCommand() {} - -/** - * @brief ItemResizerCommand::undo - */ -void ItemResizerCommand::undo() { - diagram -> showMe(); - m_qgi -> setScale(old_size); - QUndoCommand::undo(); -} - -/** - * @brief ItemResizerCommand::redo - */ -void ItemResizerCommand::redo() { - diagram -> showMe(); - if (old_size setScale(new_size); - QUndoCommand::redo(); -} - /** * @brief ChangeShapeStyleCommand::ChangeShapeStyleCommand Constructor diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index 0b87c549e..369b032d7 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -510,25 +510,6 @@ class ChangeSeveralConductorsPropertiesCommand : public QUndoCommand { bool new_settings_set; }; -class ItemResizerCommand : public QUndoCommand { - //constructor and destructor - public: - ItemResizerCommand (QetGraphicsItem *qgi, qreal &old_, qreal &new_,const QString &text, QUndoCommand *parent = 0); - virtual ~ItemResizerCommand(); - - //methods - public: - virtual void undo(); - virtual void redo(); - - //attributes - private: - QetGraphicsItem *m_qgi; - qreal old_size, new_size; - Diagram *diagram; - QString m_text; -}; - class ChangeShapeStyleCommand : public QUndoCommand { //constructor and destructor diff --git a/sources/qetgraphicsitem/qetshapeitem.cpp b/sources/qetgraphicsitem/qetshapeitem.cpp index a5da07223..274425bef 100644 --- a/sources/qetgraphicsitem/qetshapeitem.cpp +++ b/sources/qetgraphicsitem/qetshapeitem.cpp @@ -16,6 +16,7 @@ along with QElectroTech. If not, see . */ #include "qetshapeitem.h" +#include "itemresizercommand.h" #include "diagramcommands.h" #include "createdxf.h" #include "diagram.h" diff --git a/sources/ui/imagepropertieswidget.cpp b/sources/ui/imagepropertieswidget.cpp index 3d7de8353..0ec0b70b4 100644 --- a/sources/ui/imagepropertieswidget.cpp +++ b/sources/ui/imagepropertieswidget.cpp @@ -18,8 +18,9 @@ #include "imagepropertieswidget.h" #include "ui_imagepropertieswidget.h" #include "diagramimageitem.h" -#include "diagramcommands.h" #include +#include "itemresizercommand.h" +#include "diagram.h" /** * @brief ImagePropertiesWidget::ImagePropertiesWidget @@ -56,10 +57,14 @@ 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(); updateUi(); @@ -74,8 +79,14 @@ void ImagePropertiesWidget::apply() if(!m_image) return; if (m_image->diagram()) + { + if (m_live_edit) disconnect(m_image, &QGraphicsObject::scaleChanged, this, &ImagePropertiesWidget::updateUi); + m_image->diagram()->undoStack().push(associatedUndo()); + if (m_live_edit) connect(m_image, &QGraphicsObject::scaleChanged, this, &ImagePropertiesWidget::updateUi); + } + m_scale = m_image->scale(); } @@ -92,6 +103,31 @@ void ImagePropertiesWidget::reset() updateUi(); } +/** + * @brief ImagePropertiesWidget::setLiveEdit + * @param live_edit true -> enable live edit + * false -> disable live edit + * @return always true + */ +bool ImagePropertiesWidget::setLiveEdit(bool live_edit) +{ + if (m_live_edit == live_edit) return true; + m_live_edit = live_edit; + + if (m_live_edit) + { + connect (ui->m_scale_slider, &QSlider::sliderReleased, this, &ImagePropertiesWidget::apply); + connect (ui->m_scale_sb, &QSpinBox::editingFinished, this, &ImagePropertiesWidget::apply); + } + else + { + disconnect (ui->m_scale_slider, &QSlider::sliderReleased, this, &ImagePropertiesWidget::apply); + disconnect (ui->m_scale_sb, &QSpinBox::editingFinished, this, &ImagePropertiesWidget::apply); + } + + return true; +} + /** * @brief ImagePropertiesWidget::associatedUndo * @return the change in an undo command (ItemResizerCommand) @@ -109,8 +145,9 @@ QUndoCommand* ImagePropertiesWidget::associatedUndo() */ void ImagePropertiesWidget::updateUi() { - ui->m_scale_slider->setValue(m_scale * 100); - ui->m_lock_pos_cb->setChecked(!m_movable); + if (!m_image) return; + ui->m_scale_slider->setValue(m_image->scale() * 100); + ui->m_lock_pos_cb->setChecked(!m_image->isMovable()); } /** @@ -120,8 +157,8 @@ void ImagePropertiesWidget::updateUi() */ void ImagePropertiesWidget::on_m_scale_slider_valueChanged(int value) { - qreal scale = value; - m_image->setScale(scale / 100); + qreal scale = value; + m_image->setScale(scale / 100); } void ImagePropertiesWidget::imageWasDeleted() { diff --git a/sources/ui/imagepropertieswidget.h b/sources/ui/imagepropertieswidget.h index b5e7288e9..1ee9ced33 100644 --- a/sources/ui/imagepropertieswidget.h +++ b/sources/ui/imagepropertieswidget.h @@ -42,6 +42,7 @@ class ImagePropertiesWidget : public PropertiesEditorWidget void apply(); void reset(); + bool setLiveEdit(bool live_edit); QUndoCommand* associatedUndo(); private: diff --git a/sources/undocommand/itemresizercommand.cpp b/sources/undocommand/itemresizercommand.cpp new file mode 100644 index 000000000..955007d85 --- /dev/null +++ b/sources/undocommand/itemresizercommand.cpp @@ -0,0 +1,97 @@ +/* + 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 . +*/ +#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, qreal &old_, 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()) return false; + ItemResizerCommand const *undo = static_cast(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(); +} diff --git a/sources/undocommand/itemresizercommand.h b/sources/undocommand/itemresizercommand.h new file mode 100644 index 000000000..258376bea --- /dev/null +++ b/sources/undocommand/itemresizercommand.h @@ -0,0 +1,52 @@ +/* + 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 . +*/ +#ifndef ITEMRESIZERCOMMAND_H +#define ITEMRESIZERCOMMAND_H + +#include +#include + +class QetGraphicsItem; +class Diagram; + +/** + * @brief The ItemResizerCommand class + * This Undo command manage the scale of QetGraphicsItem. + */ +class ItemResizerCommand : public QUndoCommand +{ + public: + ItemResizerCommand (QetGraphicsItem *qgi, qreal &old_, 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