diff --git a/sources/diagramcommands.cpp b/sources/diagramcommands.cpp index 7b4c34a7c..43401dae0 100644 --- a/sources/diagramcommands.cpp +++ b/sources/diagramcommands.cpp @@ -1071,3 +1071,38 @@ void ChangeSeveralConductorsPropertiesCommand::redo() { } } } + +/** + * @brief ImageResizerCommand::ImageResizerCommand Constructor + * @param image + * @param old_ old size of image + * @param new_ new size of image + * @param parent undocommand parent + */ +ImageResizerCommand::ImageResizerCommand (DiagramImageItem *image, qreal &old_, qreal &new_, QUndoCommand *parent): + QUndoCommand(parent), + image_(image), + old_size (old_), + new_size (new_) +{} + +/** + * @brief ImageResizerCommand::~ImageResizerCommand destructor + */ +ImageResizerCommand::~ImageResizerCommand() {} + +/** + * @brief ImageResizerCommand::undo set the old size + */ +void ImageResizerCommand::undo() { + image_ -> setScale(old_size); +} + +/** + * @brief ImageResizerCommand::redo set the new size + */ +void ImageResizerCommand::redo() { + if (old_size setScale(new_size); +} diff --git a/sources/diagramcommands.h b/sources/diagramcommands.h index 7cb621613..688ec7cbd 100644 --- a/sources/diagramcommands.h +++ b/sources/diagramcommands.h @@ -561,4 +561,22 @@ class ChangeSeveralConductorsPropertiesCommand : public QUndoCommand { /// track whether post-change properties were set bool new_settings_set; }; + +class ImageResizerCommand : public QUndoCommand { + //constructor and destructor + public: + ImageResizerCommand (DiagramImageItem *image, qreal &old_, qreal &new_, QUndoCommand *parent = 0); + virtual ~ImageResizerCommand(); + + //methods + public: + virtual void undo(); + virtual void redo(); + + //attributes + private: + DiagramImageItem *image_; + qreal old_size, new_size; +}; + #endif diff --git a/sources/diagramimageitem.cpp b/sources/diagramimageitem.cpp index 89c480960..f1f85a8da 100644 --- a/sources/diagramimageitem.cpp +++ b/sources/diagramimageitem.cpp @@ -28,7 +28,6 @@ DiagramImageItem::DiagramImageItem(Diagram *parent_diagram) : #if QT_VERSION >= 0x040600 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); #endif - //connect(this, SIGNAL(lostFocus()), this, SLOT(setNonFocusable())); } /** @@ -40,12 +39,11 @@ DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, Diagram *parent_diagra QGraphicsPixmapItem(pixmap, 0, parent_diagram), first_move_(false) { - setCursor(Qt::PointingHandCursor); + setTransformOriginPoint(boundingRect().center()); setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable); #if QT_VERSION >= 0x040600 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); #endif - //connect(this, SIGNAL(lostFocus()), this, SLOT(setNonFocusable())); } /// Destructeur @@ -82,82 +80,6 @@ void DiagramImageItem::rotateBy(const qreal &added_rotation) { applyRotation(applied_added_rotation); } -/** - Traduit en coordonnees de la scene un mouvement / vecteur initialement - exprime en coordonnees locales. - @param movement Vecteur exprime en coordonnees locales - @return le meme vecteur, exprime en coordonnees de la scene -*/ -QPointF DiagramImageItem::mapMovementToScene(const QPointF &movement) const { - // on definit deux points en coordonnees locales - QPointF local_origin(0.0, 0.0); - QPointF local_movement_point(movement); - - // on les mappe sur la scene - QPointF scene_origin(mapToScene(local_origin)); - QPointF scene_movement_point(mapToScene(local_movement_point)); - - // on calcule le vecteur represente par ces deux points - return(scene_movement_point - scene_origin); -} - -/** - Traduit en coordonnees locales un mouvement / vecteur initialement - exprime en coordonnees de la scene. - @param movement Vecteur exprime en coordonnees de la scene - @return le meme vecteur, exprime en coordonnees locales -*/ -QPointF DiagramImageItem::mapMovementFromScene(const QPointF &movement) const { - // on definit deux points sur la scene - QPointF scene_origin(0.0, 0.0); - QPointF scene_movement_point(movement); - - // on les mappe sur ce QGraphicsItem - QPointF local_origin(mapFromScene(scene_origin)); - QPointF local_movement_point(mapFromScene(scene_movement_point)); - - // on calcule le vecteur represente par ces deux points - return(local_movement_point - local_origin); -} - -/** - Traduit en coordonnees de l'item parent un mouvement / vecteur initialement - exprime en coordonnees locales. - @param movement Vecteur exprime en coordonnees locales - @return le meme vecteur, exprime en coordonnees du parent -*/ -QPointF DiagramImageItem::mapMovementToParent(const QPointF &movement) const { - // on definit deux points en coordonnees locales - QPointF local_origin(0.0, 0.0); - QPointF local_movement_point(movement); - - // on les mappe sur la scene - QPointF parent_origin(mapToParent(local_origin)); - QPointF parent_movement_point(mapToParent(local_movement_point)); - - // on calcule le vecteur represente par ces deux points - return(parent_movement_point - parent_origin); -} - -/** - Traduit en coordonnees locales un mouvement / vecteur initialement - exprime en coordonnees du parent. - @param movement Vecteur exprime en coordonnees du parent - @return le meme vecteur, exprime en coordonnees locales -*/ -QPointF DiagramImageItem::mapMovementFromParent(const QPointF &movement) const { - // on definit deux points sur le parent - QPointF parent_origin(0.0, 0.0); - QPointF parent_movement_point(movement); - - // on les mappe sur ce QGraphicsItem - QPointF local_origin(mapFromParent(parent_origin)); - QPointF local_movement_point(mapFromParent(parent_movement_point)); - - // on calcule le vecteur represente par ces deux points - return(local_movement_point - local_origin); -} - /** Dessine le champ de image. Cette methode delegue simplement le travail a QGraphicsPixmapItem::paint apres @@ -171,37 +93,6 @@ void DiagramImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem * QGraphicsPixmapItem::paint(painter, option, widget); } -/** - Gere la prise de focus du champ de image - @param e Objet decrivant la prise de focus -*/ -void DiagramImageItem::focusInEvent(QFocusEvent *e) { - QGraphicsPixmapItem::focusInEvent(e); - - // empeche le deplacement du image pendant son edition - setFlag(QGraphicsItem::ItemIsMovable, false); -} - -/** - Gere la perte de focus du champ de image - @param e Objet decrivant la perte de focus -*/ -void DiagramImageItem::focusOutEvent(QFocusEvent *e) { - QGraphicsPixmapItem::focusOutEvent(e); - - /*// deselectionne le image - QTextCursor cursor = textCursor(); - cursor.clearSelection(); - setTextCursor(cursor); - - // hack a la con pour etre re-entrant - setTextInteractionFlags(Qt::NoTextInteraction); - - // autorise de nouveau le deplacement du image - setFlag(QGraphicsItem::ItemIsMovable, true); - QTimer::singleShot(0, this, SIGNAL(lostFocus()));*/ -} - /** Gere le clic sur le champ de texte @param e Objet decrivant l'evenement souris @@ -249,24 +140,25 @@ void DiagramImageItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { spin_box -> setSuffix(" %"); //synchro slider with spinbox connect(slider, SIGNAL(valueChanged(int)), spin_box, SLOT(setValue(int))); - connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setScale(int))); + connect(slider, SIGNAL(valueChanged(int)), this, SLOT(PreviewScale(int))); connect(spin_box, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); //add slider and spinbox to layout resize_layout -> addWidget(slider); resize_layout -> addWidget(spin_box); - //dialog butto, box + //dialog button, box QDialogButtonBox *dbb = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); dialog_layout -> addWidget(dbb); connect(dbb, SIGNAL(accepted()), &property_dialog, SLOT(accept())); connect(dbb, SIGNAL(rejected()), &property_dialog, SLOT(reject())); - + //dialog is accepted... if (property_dialog.exec() == QDialog::Accepted) { qreal new_scale = slider -> value(); new_scale /= factor_range; - if (scale_ != new_scale) QGraphicsPixmapItem::setScale(new_scale); + if (scale_ != new_scale) diagram()->undoStack().push(new ImageResizerCommand(this, scale_, new_scale)); } - else QGraphicsPixmapItem::setScale(scale_); + //...or not + else setScale(scale_); return; } @@ -328,9 +220,7 @@ void DiagramImageItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) { @param angle Angle de la rotation a effectuer */ void DiagramImageItem::applyRotation(const qreal &angle) { - // un simple appel a QGraphicsPixmapItem::setRotation suffit - setTransformOriginPoint(boundingRect().center()); - QGraphicsPixmapItem::setRotation(QGraphicsPixmapItem::rotation() + angle); + setRotation(QET::correctAngle(rotation()+angle)); } /** @@ -360,22 +250,15 @@ void DiagramImageItem::setPos(qreal x, qreal y) { setPos(QPointF(x, y)); } -/** - @return la position du champ de image -*/ -QPointF DiagramImageItem::pos() const { - return(QGraphicsPixmapItem::pos()); -} - /** * @brief DiagramImageItem::setScale * @param scale the value of @scale must be betwen 1 and 200 */ -void DiagramImageItem::setScale(int scale) { +void DiagramImageItem::PreviewScale(int scale) { if (scale >= 1 && scale <= 200) { qreal new_scale = scale; new_scale /= 100; - QGraphicsPixmapItem::setScale(new_scale); + setScale(new_scale); } } @@ -386,6 +269,11 @@ void DiagramImageItem::edit() { // waiting } +void DiagramImageItem::setPixmap(const QPixmap &pixmap) { + QGraphicsPixmapItem::setPixmap(pixmap); + setTransformOriginPoint(boundingRect().center()); +} + /** Load the image from this xml element @param e xml element that define an image @@ -404,8 +292,9 @@ bool DiagramImageItem::fromXml(const QDomElement &e) { pixmap.loadFromData(array); setPixmap(pixmap); + setScale(e.attribute("size").toDouble()); + setRotationAngle(e.attribute("rotation").toDouble()); setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble()); - if (e.hasAttribute("rotation")) setRotationAngle(e.attribute("rotation").toDouble()); return (true); } @@ -419,7 +308,8 @@ QDomElement DiagramImageItem::toXml(QDomDocument &document) const { //write some attribute result.setAttribute("x", QString("%1").arg(pos().x())); result.setAttribute("y", QString("%1").arg(pos().y())); - if (rotation()) result.setAttribute("rotation", QString("%1").arg(rotation())); + result.setAttribute("rotation", QString("%1").arg(rotation())); + result.setAttribute("size", QString("%1").arg(scale())); //write the pixmap in the xml element after he was been transformed to base64 QByteArray array; diff --git a/sources/diagramimageitem.h b/sources/diagramimageitem.h index 0bce84109..5752f7652 100644 --- a/sources/diagramimageitem.h +++ b/sources/diagramimageitem.h @@ -51,19 +51,13 @@ class DiagramImageItem : public QObject, public QGraphicsPixmapItem { virtual void setPos(const QPointF &); virtual void setPos(qreal, qreal); - virtual QPointF pos() const; void setRotationAngle(const qreal &); void rotateBy(const qreal &); void edit(); - QPointF mapMovementToScene(const QPointF &) const; - QPointF mapMovementFromScene(const QPointF &) const; - QPointF mapMovementToParent(const QPointF &) const; - QPointF mapMovementFromParent(const QPointF &) const; + void setPixmap(const QPixmap &pixmap); protected: void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); - void focusInEvent(QFocusEvent *); - void focusOutEvent(QFocusEvent *); void mousePressEvent(QGraphicsSceneMouseEvent *e); void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *); void mouseMoveEvent(QGraphicsSceneMouseEvent *); @@ -71,13 +65,11 @@ class DiagramImageItem : public QObject, public QGraphicsPixmapItem { void applyRotation(const qreal &); signals: - /// signal emitted when the image field loses focus - void lostFocus(); /// signal emitted after image was changed void diagramImageChanged(DiagramImageItem *, const QString &, const QString &); private slots: - void setScale(int); + void PreviewScale(int); private: bool first_move_; diff --git a/sources/diagramtextitem.cpp b/sources/diagramtextitem.cpp index 7286b6f44..272eac655 100644 --- a/sources/diagramtextitem.cpp +++ b/sources/diagramtextitem.cpp @@ -261,8 +261,7 @@ void DiagramTextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) { @param angle Angle de la rotation a effectuer */ void DiagramTextItem::applyRotation(const qreal &angle) { - // un simple appel a QGraphicsTextItem::setRotation suffit - QGraphicsTextItem::setRotation(QGraphicsTextItem::rotation() + angle); + setRotation(QET::correctAngle(rotation()+angle)); } /**