[qet] [2381] add object for image manipulation

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 2381
Author:   cfdev
Date:     2013-07-07 21:29:42 +0200 (Sun, 07 Jul 2013)
Log Message:
-----------
add object for image manipulation

Modified Paths:
--------------
    branches/0.4/sources/diagramview.cpp
    branches/0.4/sources/diagramview.h

Added Paths:
-----------
    branches/0.4/sources/diagramimageitem.cpp
    branches/0.4/sources/diagramimageitem.h

Added: branches/0.4/sources/diagramimageitem.cpp
===================================================================
--- branches/0.4/sources/diagramimageitem.cpp	                        (rev 0)
+++ branches/0.4/sources/diagramimageitem.cpp	2013-07-07 19:29:42 UTC (rev 2381)
@@ -0,0 +1,318 @@
+/*
+	Copyright 2006-2013 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 "DiagramImageItem.h"
+#include "diagramcommands.h"
+#include "qet.h"
+#include "qetapp.h"
+
+DiagramImageItem::DiagramImageItem(Diagram *parent_diagram) :
+	QGraphicsPixmapItem(0, parent_diagram),
+	rotation_angle_(0.0)
+{
+	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
+#if QT_VERSION >= 0x040600
+	setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
+#endif
+	//connect(this, SIGNAL(lostFocus()), this, SLOT(setNonFocusable()));
+}
+
+/**
+ * @brief DiagramImageItem::DiagramImageItem
+ * @param parent
+ * @param parent_diagram
+ */
+DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, Diagram *parent_diagram) :
+	QGraphicsPixmapItem(pixmap, 0, parent_diagram),
+	rotation_angle_(0.0)
+{
+	setCursor(Qt::PointingHandCursor);
+	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
+#if QT_VERSION >= 0x040600
+	setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
+#endif
+	//connect(this, SIGNAL(lostFocus()), this, SLOT(setNonFocusable()));
+}
+
+/// Destructeur
+DiagramImageItem::~DiagramImageItem() {
+}
+
+/**
+	@return le Diagram auquel ce image appartient, ou 0 si ce image n'est
+	rattache a aucun schema
+*/
+Diagram *DiagramImageItem::diagram() const {
+	return(qobject_cast<Diagram *>(scene()));
+}
+
+/**
+	@return l'angle de rotation actuel de ce image
+*/
+qreal DiagramImageItem::rotationAngle() const {
+	return(rotation_angle_);
+}
+
+/**
+	Permet de tourner le image a un angle donne de maniere absolue.
+	Un angle de 0 degres correspond a un image horizontal non retourne.
+	@param rotation Nouvel angle de rotation de ce image
+	@see applyRotation
+*/
+void DiagramImageItem::setRotationAngle(const qreal &rotation) {
+	qreal applied_rotation = QET::correctAngle(rotation);
+	applyRotation(applied_rotation - rotation_angle_);
+	rotation_angle_ = applied_rotation;
+}
+
+/**
+	Permet de tourner le image de maniere relative.
+	L'angle added_rotation est ajoute a l'orientation actuelle du image.
+	@param added_rotation Angle a ajouter a la rotation actuelle
+	@see applyRotation
+*/
+void DiagramImageItem::rotateBy(const qreal &added_rotation) {
+	qreal applied_added_rotation = QET::correctAngle(added_rotation);
+	rotation_angle_ = QET::correctAngle(rotation_angle_ + applied_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
+	avoir desactive l'antialiasing.
+	@param painter Le QPainter a utiliser pour dessiner le champ de image
+	@param option Les options de style pour le champ de image
+	@param widget Le QWidget sur lequel on dessine 
+*/
+void DiagramImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+	painter -> setRenderHint(QPainter::Antialiasing, false);
+	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
+*/
+void DiagramImageItem::mousePressEvent(QGraphicsSceneMouseEvent *e) {
+	//todo
+}
+
+/**
+	Gere les double-clics sur ce champ de image.
+	@param event un QGraphicsSceneMouseEvent decrivant le double-clic
+*/
+void DiagramImageItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+	/*if (!(textInteractionFlags() & Qt::imageditable)) {
+		// rend le champ de image editable
+		setTextInteractionFlags(Qt::imageditorInteraction);
+		
+		// edite le champ de image
+		setFocus(Qt::MouseFocusReason);
+	} else {
+		QGraphicsPixmapItem::mouseDoubleClickEvent(event);
+	}*/
+}
+
+/**
+	Effectue la rotation du image en elle-meme
+	Pour les DiagramImageItem, la rotation s'effectue autour du point (0, 0).
+	Cette methode peut toutefois etre redefinie dans des classes filles
+	@param angle Angle de la rotation a effectuer
+*/
+void DiagramImageItem::applyRotation(const qreal &angle) {
+	// un simple appel a QGraphicsPixmapItem::setRotation suffit
+	QGraphicsPixmapItem::setRotation(QGraphicsPixmapItem::rotation() + angle);
+}
+
+/**
+	Change la position du champ de image en veillant a ce qu'il
+	reste sur la grille du schema auquel il appartient.
+	@param p Nouvelles coordonnees de l'element
+*/
+void DiagramImageItem::setPos(const QPointF &p) {
+	if (p == pos()) return;
+	// pas la peine de positionner sur la grille si l'element n'est pas sur un Diagram
+	if (scene()) {
+		// arrondit l'abscisse a 10 px pres
+		int p_x = qRound(p.x() / (Diagram::xGrid * 1.0)) * Diagram::xGrid;
+		// arrondit l'ordonnee a 10 px pres
+		int p_y = qRound(p.y() / (Diagram::yGrid * 1.0)) * Diagram::yGrid;
+		QGraphicsPixmapItem::setPos(p_x, p_y);
+	} else QGraphicsPixmapItem::setPos(p);
+}
+
+/**
+	Change la position du champ de image en veillant a ce que l'il
+	reste sur la grille du schema auquel il appartient.
+	@param x Nouvelle abscisse de l'element
+	@param y Nouvelle ordonnee de l'element
+*/
+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());
+}
+
+/// Rend le champ de image non focusable
+void DiagramImageItem::setNonFocusable() {
+	setFlag(QGraphicsPixmapItem::ItemIsFocusable, false);
+}
+
+/**
+ * @brief Edit the image with ....
+ */
+void DiagramImageItem::edit() {
+	// waiting
+}
+
+/**
+	Permet de lire le texte a mettre dans le champ a partir d'un element XML.
+	Cette methode se base sur la position du champ pour assigner ou non la
+	valeur a ce champ.
+	@param e L'element XML representant le champ de texte
+*/
+void DiagramImageItem::fromXml(const QDomElement &e) {
+	setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
+	setRotationAngle(e.attribute("rotation").toDouble());
+}
+
+/**
+	@param document Le document XML a utiliser
+	@return L'element XML representant ce champ de texte
+*/
+QDomElement DiagramImageItem::toXml(QDomDocument &document) const {
+	QDomElement result = document.createElement("input");
+	result.setAttribute("x", QString("%1").arg(pos().x()));
+	result.setAttribute("y", QString("%1").arg(pos().y()));
+	if (rotationAngle()) {
+		result.setAttribute("rotation", QString("%1").arg(rotationAngle()));
+	}
+	return(result);
+}

Added: branches/0.4/sources/diagramimageitem.h
===================================================================
--- branches/0.4/sources/diagramimageitem.h	                        (rev 0)
+++ branches/0.4/sources/diagramimageitem.h	2013-07-07 19:29:42 UTC (rev 2381)
@@ -0,0 +1,85 @@
+/*
+	Copyright 2006-2013 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 DIAGRAM_IMAGE_ITEM_H
+#define DIAGRAM_IMAGE_ITEM_H
+#include <QtGui>
+#include "diagram.h"
+/**
+	This class represents a selectable, movable and editable image on a
+	diagram.
+	@see QGraphicsItem::GraphicsItemFlags
+*/
+class DiagramImageItem : public QGraphicsPixmapItem {
+	Q_OBJECT
+	// constructors, destructor
+	public:
+	DiagramImageItem(Diagram * = 0);
+	DiagramImageItem(const QPixmap &, Diagram * = 0);
+	virtual ~DiagramImageItem();
+	
+	// attributes
+	public:
+	enum { Type = UserType + 1004 };
+	
+	// methods
+	public:
+	/**
+		Enable the use of qgraphicsitem_cast to safely cast a QGraphicsItem into a
+		DiagramImageItem
+		@return the QGraphicsItem type
+	*/
+	virtual int type() const { return Type; }
+	Diagram *diagram() const;
+	
+	virtual void fromXml(const QDomElement &);
+	virtual QDomElement toXml(QDomDocument &) const;
+	
+	virtual void setPos(const QPointF &);
+	virtual void setPos(qreal, qreal);
+	virtual QPointF pos() const;
+	qreal rotationAngle() 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;
+	
+	private:
+	void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
+	void focusInEvent(QFocusEvent *);
+	void focusOutEvent(QFocusEvent *);
+	void mousePressEvent(QGraphicsSceneMouseEvent *e);
+	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *);
+	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 &);
+	
+	public slots:
+	void setNonFocusable();
+	
+	private:
+	/// angle of rotation of the text field
+	qreal rotation_angle_;
+};
+#endif

Modified: branches/0.4/sources/diagramview.cpp
===================================================================
--- branches/0.4/sources/diagramview.cpp	2013-07-06 22:25:24 UTC (rev 2380)
+++ branches/0.4/sources/diagramview.cpp	2013-07-07 19:29:42 UTC (rev 2381)
@@ -26,6 +26,7 @@
 #include "conductortextitem.h"
 #include "elementtextitem.h"
 #include "independenttextitem.h"
+#include "DiagramImageItem.h"
 #include "titleblockpropertieswidget.h"
 #include "templatelocation.h"
 #include "qetapp.h"
@@ -37,6 +38,7 @@
 #include "qeticons.h"
 #include "qetmessagebox.h"
 #include "qtextorientationspinboxwidget.h"
+
 #include <QGraphicsPixmapItem>
 #include <QGraphicsSceneMouseEvent>
 
@@ -1255,16 +1257,27 @@
 * @param pos
 * @return
 */
-IndependentTextItem *DiagramView::addDiagramImageAtPos(const QPointF &pos) {
+DiagramImageItem *DiagramView::addDiagramImageAtPos(const QPointF &pos) {
 
 	if (!isInteractive() || scene -> isReadOnly()) return(0);
-	addImage();
+	/*addImage();
 	QGraphicsPixmapItem *pixItem = new QGraphicsPixmapItem( QPixmap::fromImage(image_to_add_) );
 	pixItem->setFlag(QGraphicsItem::ItemIsMovable,true);
 	pixItem->setFlag(QGraphicsItem::ItemIsSelectable,true);
 	pixItem->setCursor(Qt::PointingHandCursor);
-	scene ->addItem(pixItem);
-
+	scene ->addItem(pixItem);*/
+	
+// cree un nouveau champ image
+	DiagramImageItem *Imageitem = new DiagramImageItem( QPixmap::fromImage(image_to_add_) );
+	
+	// le place a la position pos en gerant l'annulation
+	//scene -> undoStack().push(new AddImageCommand(scene, Imageitem, pos));
+	adjustSceneRect();
+	
+	// emet le signal ImageAdded
+	emit(ImageAdded(false));
+	
+	return(Imageitem);
 }
 
 /**

Modified: branches/0.4/sources/diagramview.h
===================================================================
--- branches/0.4/sources/diagramview.h	2013-07-06 22:25:24 UTC (rev 2380)
+++ branches/0.4/sources/diagramview.h	2013-07-07 19:29:42 UTC (rev 2381)
@@ -24,6 +24,7 @@
 class Diagram;
 class Element;
 class IndependentTextItem;
+class DiagramImageItem;
 class QETDiagramEditor;
 /**
 	This class provides a widget to render an electric diagram in an editable,
@@ -75,7 +76,7 @@
 	void editText();
 	void addImage();
 	IndependentTextItem *addDiagramTextAtPos(const QPointF &);
-	IndependentTextItem *addDiagramImageAtPos(const QPointF &);
+	DiagramImageItem *addDiagramImageAtPos(const QPointF &);
 	
 	protected:
 	virtual void mouseDoubleClickEvent(QMouseEvent *);
@@ -124,6 +125,8 @@
 	void editElementRequired(const ElementsLocation &);
 	/// Signal emitted when users want to edit and/or duplicate an existing title block template
 	void editTitleBlockTemplate(const QString &, bool);
+	/// Signal emitted after a image was added
+	void ImageAdded(bool);
 	
 	public slots:
 	void selectNothing();


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/