[qet] [2587] DiagramImageItem inherits now from QetGraphicsItem.

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


Revision: 2587
Author:   blacksun
Date:     2013-10-27 19:59:44 +0100 (Sun, 27 Oct 2013)
Log Message:
-----------
DiagramImageItem inherits now from QetGraphicsItem.
This is the first step of a long changement.
The final goal is the image text and element (and may be futur thing) inherits from QetGraphicsItem.
QetGraphicsItem is an habstract class.
At the end, diagram must manipulate only QetGraphicsItem and not the herited class

Modified Paths:
--------------
    branches/devel/sources/diagram.cpp
    branches/devel/sources/diagramcommands.cpp
    branches/devel/sources/diagramcontent.cpp
    branches/devel/sources/diagramview.cpp
    branches/devel/sources/diagramview.h
    branches/devel/sources/elementsmover.cpp

Added Paths:
-----------
    branches/devel/sources/qetgraphicsitem/
    branches/devel/sources/qetgraphicsitem/diagramimageitem.cpp
    branches/devel/sources/qetgraphicsitem/diagramimageitem.h
    branches/devel/sources/qetgraphicsitem/qetgraphicsitem.cpp
    branches/devel/sources/qetgraphicsitem/qetgraphicsitem.h

Removed Paths:
-------------
    branches/devel/sources/diagramimageitem.cpp
    branches/devel/sources/diagramimageitem.h

Modified: branches/devel/sources/diagram.cpp
===================================================================
--- branches/devel/sources/diagram.cpp	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagram.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -30,7 +30,7 @@
 #include "ghostelement.h"
 #include "independenttextitem.h"
 #include "qetapp.h"
-#include "diagramimageitem.h"
+#include "qetgraphicsitem/diagramimageitem.h"
 
 const int   Diagram::xGrid  = 10;
 const int   Diagram::yGrid  = 10;
@@ -555,7 +555,7 @@
 
 	QList<DiagramImageItem *> added_images;
 	foreach (QDomElement image_xml, QET::findInDomElement(root, "images", "image")) {
-		DiagramImageItem *dii = new DiagramImageItem (this);
+		DiagramImageItem *dii = new DiagramImageItem ();
 		dii -> fromXml(image_xml);
 		addItem(dii);
 		added_images << dii;

Modified: branches/devel/sources/diagramcommands.cpp
===================================================================
--- branches/devel/sources/diagramcommands.cpp	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagramcommands.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -25,7 +25,7 @@
 #include "qgimanager.h"
 #include "diagram.h"
 #include "diagramtextitem.h"
-#include "diagramimageitem.h"
+#include "qetgraphicsitem/diagramimageitem.h"
 
 /**
 	Constructeur

Modified: branches/devel/sources/diagramcontent.cpp
===================================================================
--- branches/devel/sources/diagramcontent.cpp	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagramcontent.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -20,7 +20,7 @@
 #include "element.h"
 #include "independenttextitem.h"
 #include "conductor.h"
-#include "diagramimageitem.h"
+#include "qetgraphicsitem/diagramimageitem.h"
 
 /**
 	Constructeur par defaut. Ne contient rien.

Deleted: branches/devel/sources/diagramimageitem.cpp
===================================================================
--- branches/devel/sources/diagramimageitem.cpp	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagramimageitem.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -1,323 +0,0 @@
-/*
-	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),
-	first_move_(false)
-{
-	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
-#if QT_VERSION >= 0x040600
-	setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
-#endif
-}
-
-/**
- * @brief DiagramImageItem::DiagramImageItem
- * @param parent
- * @param parent_diagram
- */
-DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, Diagram *parent_diagram) :
-	QGraphicsPixmapItem(pixmap, 0, parent_diagram),
-	first_move_(false)
-{
-	setTransformOriginPoint(boundingRect().center());
-	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
-#if QT_VERSION >= 0x040600
-	setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
-#endif
-}
-
-/// 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()));
-}
-
-/**
-	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_angle) {
-	qreal applied_rotation = QET::correctAngle(rotation_angle);
-	applyRotation(applied_rotation - 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);
-	applyRotation(applied_added_rotation);
-}
-
-/**
-	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 le clic sur le champ de texte
-	@param e Objet decrivant l'evenement souris
-*/
-void DiagramImageItem::mousePressEvent(QGraphicsSceneMouseEvent *e) {
-	first_move_ = true;
-	if (e -> modifiers() & Qt::ControlModifier) {
-		setSelected(!isSelected());
-	}
-	QGraphicsItem::mousePressEvent(e);
-}
-
-/**
-	Gere les double-clics sur ce champ de image.
-	@param event un QGraphicsSceneMouseEvent decrivant le double-clic
-*/
-void DiagramImageItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
-	Q_UNUSED (event);
-	edit();
-}
-
-/**
- * @brief DiagramImageItem::mouseMoveEvent
- * Gere les mouvements de souris lies a l'image
- * @param e Objet decrivant l'evenement souris
- */
-void DiagramImageItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
-	if (isSelected() && e -> buttons() & Qt::LeftButton) {
-		//Image is moving
-		if(diagram()) {
-			if (first_move_) {
-				//It's the first movement, we signal it to parent diagram
-				diagram() -> beginMoveElements(this);
-			}
-		}
-
-		//we apply the mouse movement
-		QPointF old_pos = pos();
-		if (first_move_) {
-			mouse_to_origin_movement_ = old_pos - e -> buttonDownScenePos(Qt::LeftButton);
-		}
-		QPointF expected_pos = e-> scenePos() + mouse_to_origin_movement_;
-		setPos(expected_pos); // setPos() will snap the expected position to the grid
-
-		//we calcul the real movement apply by setPos()
-		QPointF effective_movement = pos() - old_pos;
-		if (diagram()) {
-			//we signal the real movement apply to diagram,
-			//who he apply to other selected item
-			diagram() -> continueMoveElements(effective_movement);
-		}
-	} else e -> ignore();
-
-	if (first_move_) first_move_ = false;
-}
-
-/**
- * @brief DiagramImageItem::mouseReleaseEvent
- * 	Gere le relachement de souris
- *  Cette methode a ete reimplementee pour tenir a jour la liste
- *  des images à deplacer au niveau du schema.
- * @param e Objet decrivant l'evenement souris
- */
-void DiagramImageItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
-	if (diagram()) {
-		diagram() -> endMoveElements();
-	}
-	if (!(e -> modifiers() & Qt::ControlModifier)) {
-		QGraphicsItem::mouseReleaseEvent(e);
-	}
-}
-
-/**
-	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) {
-	setRotation(QET::correctAngle(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));
-}
-
-/**
- * @brief DiagramImageItem::setScale
- * @param scale the value of @scale must be betwen 1 and 200
- */
-void DiagramImageItem::PreviewScale(int scale) {
-	if (scale >= 1 && scale <= 200) {
-		qreal new_scale = scale;
-		new_scale /= 100;
-		setScale(new_scale);
-	}
-}
-
-/**
- * @brief Edit the image with ....
- */
-void DiagramImageItem::edit() {
-	if (diagram() -> isReadOnly()) return;
-	//the range for scale image and divisor factor
-	int min_range = 1;
-	int max_range = 200;
-	int factor_range = 100;
-
-	//the dialog
-	QDialog property_dialog;
-	property_dialog.setWindowTitle(tr("\311diter les propri\351t\351s d'une image", "window title"));
-	//the main layout
-	QVBoxLayout *dialog_layout = new QVBoxLayout(&property_dialog);
-
-	//GroupBox for resizer image
-	QGroupBox *resize_groupe = new QGroupBox(tr("Dimension de l'image", "image size"));
-	dialog_layout -> addWidget(resize_groupe);
-	QHBoxLayout *resize_layout = new QHBoxLayout(resize_groupe);
-
-	//slider
-	QSlider *slider = new QSlider(Qt::Horizontal, &property_dialog);
-	slider->setRange(min_range, max_range);
-	qreal scale_= scale();
-	slider -> setValue(scale_*factor_range);
-	//spinbox
-	QSpinBox *spin_box = new QSpinBox(&property_dialog);
-	spin_box -> setRange(min_range, max_range);
-	spin_box -> setValue(scale_*factor_range);
-	spin_box -> setSuffix(" %");
-	//synchro slider with spinbox
-	connect(slider, SIGNAL(valueChanged(int)), spin_box, SLOT(setValue(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 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) diagram()->undoStack().push(new ImageResizerCommand(this, scale_, new_scale));
-	}
-	//...or not
-	else setScale(scale_);
-	return;
-}
-
-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
-*/
-bool DiagramImageItem::fromXml(const QDomElement &e) {
-	if (e.tagName() != "image") return (false);
-	QDomNode image_node = e.firstChild();
-	if (!image_node.isText()) return (false);
-
-	//load xml text image to QByteArray
-	QByteArray array;
-	array = QByteArray::fromBase64(e.text().toAscii());
-
-	//Set QPixmap from the @array
-	QPixmap pixmap;
-	pixmap.loadFromData(array);
-	setPixmap(pixmap);
-
-	setScale(e.attribute("size").toDouble());
-	setRotationAngle(e.attribute("rotation").toDouble());
-	setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
-
-	return (true);
-}
-
-/**
-	@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("image");
-	//write some attribute
-	result.setAttribute("x", QString("%1").arg(pos().x()));
-	result.setAttribute("y", QString("%1").arg(pos().y()));
-	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;
-	QBuffer buffer(&array);
-	buffer.open(QIODevice::ReadWrite);
-	pixmap().save(&buffer, "PNG");
-	QDomText base64 = document.createTextNode(array.toBase64());
-	result.appendChild(base64);
-
-	return(result);
-}

Deleted: branches/devel/sources/diagramimageitem.h
===================================================================
--- branches/devel/sources/diagramimageitem.h	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagramimageitem.h	2013-10-27 18:59:44 UTC (rev 2587)
@@ -1,78 +0,0 @@
-/*
-	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 QObject, public QGraphicsPixmapItem {
-	Q_OBJECT
-	// constructors, destructor
-	public:
-	DiagramImageItem(Diagram * = 0);
-	DiagramImageItem(const QPixmap &, Diagram * = 0);
-	virtual ~DiagramImageItem();
-	
-	// attributes
-	public:
-	enum { Type = UserType + 1007 };
-	
-	// 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 bool fromXml(const QDomElement &);
-	virtual QDomElement toXml(QDomDocument &) const;
-	
-	virtual void setPos(const QPointF &);
-	virtual void setPos(qreal, qreal);
-	void setRotationAngle(const qreal &);
-	void rotateBy(const qreal &);
-	void edit();
-	void setPixmap(const QPixmap &pixmap);
-	
-	protected:
-	void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
-	void mousePressEvent(QGraphicsSceneMouseEvent *e);
-	void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *);
-	void mouseMoveEvent(QGraphicsSceneMouseEvent *);
-	void mouseReleaseEvent(QGraphicsSceneMouseEvent *);
-	void applyRotation(const qreal &);
-	
-	signals:
-	/// signal emitted after image was changed
-	void diagramImageChanged(DiagramImageItem *, const QString &, const QString &);
-	
-	private slots:
-	void PreviewScale(int);
-	
-	private:
-	bool first_move_;
-	QPointF mouse_to_origin_movement_;
-};
-#endif

Modified: branches/devel/sources/diagramview.cpp
===================================================================
--- branches/devel/sources/diagramview.cpp	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagramview.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -26,7 +26,7 @@
 #include "conductortextitem.h"
 #include "elementtextitem.h"
 #include "independenttextitem.h"
-#include "diagramimageitem.h"
+#include "qetgraphicsitem/diagramimageitem.h"
 #include "titleblockpropertieswidget.h"
 #include "templatelocation.h"
 #include "qetapp.h"
@@ -232,6 +232,8 @@
 		e -> acceptProposedAction();
 	} else if (e -> mimeData() -> hasFormat("application/x-qet-titleblock-uri")) {
 		e -> acceptProposedAction();
+	} else if (e -> mimeData() -> hasText()) {
+		e -> acceptProposedAction();
 	} else {
 		e -> ignore();
 	}
@@ -264,6 +266,8 @@
 		handleElementDrop(e);
 	} else if (e -> mimeData() -> hasFormat("application/x-qet-titleblock-uri")) {
 		handleTitleBlockDrop(e);
+	} else if (e -> mimeData() -> hasText()) {
+		handleTextDrop(e);
 	}
 }
 
@@ -301,6 +305,18 @@
 }
 
 /**
+ * @brief DiagramView::handleTextDrop
+ *handle the drop of text, html markup are automatically detected.
+ * @param e the QDropEvent describing the current drag'n drop
+ */
+void DiagramView::handleTextDrop(QDropEvent *e) {
+	if (e -> mimeData() -> hasText()) {
+		if (e -> mimeData() -> hasHtml()) addDiagramTextAtPos(e->pos()) -> setHtml(e -> mimeData() -> text());
+		else addDiagramTextAtPos(e -> pos(), e -> mimeData() -> text());
+	}
+}
+
+/**
 	Set the Diagram in visualisation mode
 */
 void DiagramView::setVisualisationMode() {
@@ -1278,7 +1294,7 @@
 	if (images.count() != 1) return;
 	DiagramImageItem *image;
 	if ((image = qgraphicsitem_cast<DiagramImageItem *> (images.first()))) {
-		image -> edit();
+		image -> editProperty();
 	}
 }
 
@@ -1311,11 +1327,14 @@
 	@param pos Position du champ de texte ajoute
 	@return le champ de texte ajoute
 */
-IndependentTextItem *DiagramView::addDiagramTextAtPos(const QPointF &pos) {
+IndependentTextItem *DiagramView::addDiagramTextAtPos(const QPointF &pos, const QString text) {
 	if (!isInteractive() || scene -> isReadOnly()) return(0);
 	
 	// cree un nouveau champ de texte
-	IndependentTextItem *iti = new IndependentTextItem("_");
+	IndependentTextItem *iti;
+	if (text.isEmpty()) {
+		iti = new IndependentTextItem("_");
+	} else iti = new IndependentTextItem(text);
 	
 	// le place a la position pos en gerant l'annulation
 	scene -> undoStack().push(new AddTextCommand(scene, iti, pos));

Modified: branches/devel/sources/diagramview.h
===================================================================
--- branches/devel/sources/diagramview.h	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/diagramview.h	2013-10-27 18:59:44 UTC (rev 2587)
@@ -76,7 +76,7 @@
 	void editText();
 	void addImage();
 	void editImage();
-	IndependentTextItem *addDiagramTextAtPos(const QPointF &);
+	IndependentTextItem *addDiagramTextAtPos(const QPointF &, const QString text = 0);
 	DiagramImageItem *addDiagramImageAtPos(const QPointF &);
 	
 	protected:
@@ -102,6 +102,7 @@
 	void dropEvent(QDropEvent *);
 	void handleElementDrop(QDropEvent *);
 	void handleTitleBlockDrop(QDropEvent *);
+	void handleTextDrop(QDropEvent *);
 	QRectF viewedSceneRect() const;
 	bool mustIntegrateElement(const ElementsLocation &) const;
 	bool mustIntegrateTitleBlockTemplate(const TitleBlockTemplateLocation &) const;

Modified: branches/devel/sources/elementsmover.cpp
===================================================================
--- branches/devel/sources/elementsmover.cpp	2013-10-26 11:51:46 UTC (rev 2586)
+++ branches/devel/sources/elementsmover.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -22,7 +22,7 @@
 #include "diagramcommands.h"
 #include "element.h"
 #include "independenttextitem.h"
-#include "diagramimageitem.h"
+#include "qetgraphicsitem/diagramimageitem.h"
 
 /**
 	Constructeur

Added: branches/devel/sources/qetgraphicsitem/diagramimageitem.cpp
===================================================================
--- branches/devel/sources/qetgraphicsitem/diagramimageitem.cpp	                        (rev 0)
+++ branches/devel/sources/qetgraphicsitem/diagramimageitem.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -0,0 +1,254 @@
+/*
+	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"
+
+/**
+ * @brief DiagramImageItem::DiagramImageItem
+ * Constructor without pixmap
+ * @param parent_item the parent graphics item
+ */
+DiagramImageItem::DiagramImageItem(QetGraphicsItem *parent_item):
+	QetGraphicsItem(parent_item)
+{
+	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
+#if QT_VERSION >= 0x040600
+	setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
+#endif
+}
+
+/**
+ * @brief DiagramImageItem::DiagramImageItem
+ * Constructor with pixmap
+ * @param pixmap the pixmap to be draw
+ * @param parent_item the parent graphic item
+ */
+DiagramImageItem::DiagramImageItem(const QPixmap &pixmap, QetGraphicsItem *parent_item):
+	QetGraphicsItem(parent_item),
+	pixmap_(pixmap)
+{
+	setTransformOriginPoint(boundingRect().center());
+	setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
+#if QT_VERSION >= 0x040600
+	setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
+#endif
+}
+
+/**
+ * @brief DiagramImageItem::~DiagramImageItem
+ * Destructor
+ */
+DiagramImageItem::~DiagramImageItem() {
+}
+
+/**
+	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_angle) {
+	qreal applied_rotation = QET::correctAngle(rotation_angle);
+	applyRotation(applied_rotation - 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);
+	applyRotation(applied_added_rotation);
+}
+
+/**
+ * @brief DiagramImageItem::paint
+ * Draw the pixmap.
+ * @param painter the Qpainter to use for draw the pixmap
+ * @param option the style option
+ * @param widget the QWidget where we draw the pixmap
+ */
+void DiagramImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+	painter -> drawPixmap(pixmap_.rect(),pixmap_);
+
+	if (isSelected()) {
+		painter -> save();
+		// Annulation des renderhints
+		painter -> setRenderHint(QPainter::Antialiasing,          false);
+		painter -> setRenderHint(QPainter::TextAntialiasing,      false);
+		painter -> setRenderHint(QPainter::SmoothPixmapTransform, false);
+		// Dessin du cadre de selection en noir a partir du boundingrect
+		QPen t(Qt::black);
+		t.setStyle(Qt::DashLine);
+		painter -> setPen(t);
+		painter -> drawRect(boundingRect());
+		painter -> restore();
+	}
+}
+
+/**
+	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) {
+	setRotation(QET::correctAngle(rotation()+angle));
+}
+
+/**
+ * @brief DiagramImageItem::setScale
+ * @param scale the value of @scale must be betwen 1 and 200
+ */
+void DiagramImageItem::PreviewScale(int scale) {
+	if (scale >= 1 && scale <= 200) {
+		qreal new_scale = scale;
+		new_scale /= 100;
+		setScale(new_scale);
+	}
+}
+
+/**
+ * @brief Edit the image with ....
+ */
+void DiagramImageItem::editProperty() {
+	if (diagram() -> isReadOnly()) return;
+	//the range for scale image and divisor factor
+	int min_range = 1;
+	int max_range = 200;
+	int factor_range = 100;
+
+	//the dialog
+	QDialog property_dialog;
+	property_dialog.setWindowTitle(tr("\311diter les propri\351t\351s d'une image", "window title"));
+	//the main layout
+	QVBoxLayout *dialog_layout = new QVBoxLayout(&property_dialog);
+
+	//GroupBox for resizer image
+	QGroupBox *resize_groupe = new QGroupBox(tr("Dimension de l'image", "image size"));
+	dialog_layout -> addWidget(resize_groupe);
+	QHBoxLayout *resize_layout = new QHBoxLayout(resize_groupe);
+
+	//slider
+	QSlider *slider = new QSlider(Qt::Horizontal, &property_dialog);
+	slider->setRange(min_range, max_range);
+	qreal scale_= scale();
+	slider -> setValue(scale_*factor_range);
+	//spinbox
+	QSpinBox *spin_box = new QSpinBox(&property_dialog);
+	spin_box -> setRange(min_range, max_range);
+	spin_box -> setValue(scale_*factor_range);
+	spin_box -> setSuffix(" %");
+	//synchro slider with spinbox
+	connect(slider, SIGNAL(valueChanged(int)), spin_box, SLOT(setValue(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 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) diagram()->undoStack().push(new ImageResizerCommand(this, scale_, new_scale));
+	}
+	//...or not
+	else setScale(scale_);
+	return;
+}
+
+/**
+ * @brief DiagramImageItem::setPixmap
+ * Set the new pixmap to be draw
+ * @param pixmap the new pixmap
+ */
+void DiagramImageItem::setPixmap(const QPixmap &pixmap) {
+	pixmap_ = pixmap;
+	setTransformOriginPoint(boundingRect().center());
+}
+
+/**
+ * @brief DiagramImageItem::boundingRect
+ * the outer bounds of the item as a rectangle,
+ * if no pixmap are set, return a default QRectF
+ * @return a QRectF represent the bounding rectangle
+ */
+QRectF DiagramImageItem::boundingRect() const {
+	if (!pixmap_.isNull()) {
+		return (QRectF(pixmap_.rect()));
+	} else {
+		QRectF bound;
+		return (bound);
+	}
+}
+
+/**
+	Load the image from this xml element
+	@param e xml element that define an image
+*/
+bool DiagramImageItem::fromXml(const QDomElement &e) {
+	if (e.tagName() != "image") return (false);
+	QDomNode image_node = e.firstChild();
+	if (!image_node.isText()) return (false);
+
+	//load xml image to QByteArray
+	QByteArray array;
+	array = QByteArray::fromBase64(e.text().toAscii());
+
+	//Set QPixmap from the @array
+	QPixmap pixmap;
+	pixmap.loadFromData(array);
+	setPixmap(pixmap);
+
+	setScale(e.attribute("size").toDouble());
+	setRotationAngle(e.attribute("rotation").toDouble());
+	setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
+
+	return (true);
+}
+
+/**
+	@param document Le document XML a utiliser
+	@return L'element XML representant l'image
+*/
+QDomElement DiagramImageItem::toXml(QDomDocument &document) const {
+	QDomElement result = document.createElement("image");
+	//write some attribute
+	result.setAttribute("x", QString("%1").arg(pos().x()));
+	result.setAttribute("y", QString("%1").arg(pos().y()));
+	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;
+	QBuffer buffer(&array);
+	buffer.open(QIODevice::ReadWrite);
+	pixmap_.save(&buffer, "PNG");
+	QDomText base64 = document.createTextNode(array.toBase64());
+	result.appendChild(base64);
+
+	return(result);
+}

Added: branches/devel/sources/qetgraphicsitem/diagramimageitem.h
===================================================================
--- branches/devel/sources/qetgraphicsitem/diagramimageitem.h	                        (rev 0)
+++ branches/devel/sources/qetgraphicsitem/diagramimageitem.h	2013-10-27 18:59:44 UTC (rev 2587)
@@ -0,0 +1,70 @@
+/*
+	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 "qetgraphicsitem.h"
+
+/**
+	This class represents a selectable, movable and editable image on a
+	diagram.
+	@see QGraphicsItem::GraphicsItemFlags
+*/
+class DiagramImageItem : public QetGraphicsItem {
+	Q_OBJECT
+
+	// constructors, destructor
+	public:
+	DiagramImageItem(QetGraphicsItem * = 0);
+	DiagramImageItem(const QPixmap &pixmap, QetGraphicsItem * = 0);
+	virtual ~DiagramImageItem();
+	
+	// attributes
+	public:
+	enum { Type = UserType + 1007 };
+	
+	// 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; }
+	
+	virtual bool fromXml(const QDomElement &);
+	virtual QDomElement toXml(QDomDocument &) const;
+	void setRotationAngle(const qreal &);
+	void rotateBy(const qreal &);
+	virtual void editProperty();
+	void setPixmap(const QPixmap &pixmap);
+	virtual QRectF boundingRect() const;
+	
+	protected:
+	virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
+	void applyRotation(const qreal &);
+	
+	signals:
+
+	private slots:
+	void PreviewScale(int);
+	
+	protected:
+	QPixmap pixmap_;
+};
+#endif

Added: branches/devel/sources/qetgraphicsitem/qetgraphicsitem.cpp
===================================================================
--- branches/devel/sources/qetgraphicsitem/qetgraphicsitem.cpp	                        (rev 0)
+++ branches/devel/sources/qetgraphicsitem/qetgraphicsitem.cpp	2013-10-27 18:59:44 UTC (rev 2587)
@@ -0,0 +1,133 @@
+/*
+	Copyright 2006-2013 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 "qetgraphicsitem.h"
+
+/**
+ * @brief QetGraphicsItem::QetGraphicsItem Default constructor
+ * @param uuid, uuid of the item
+ * @param diagram, diagram aka QGraphicsScene of the item
+ * @param parent, Parent Item
+ */
+QetGraphicsItem::QetGraphicsItem(QetGraphicsItem *parent):
+	QGraphicsObject(parent),
+	first_move_(false)
+{
+}
+
+QetGraphicsItem::~QetGraphicsItem()
+{}
+
+/**
+ * @brief QetGraphicsItem::diagram
+ *return the diagram of this item
+ */
+Diagram* QetGraphicsItem::diagram() const{
+	return(qobject_cast<Diagram *>(scene()));
+}
+
+/**
+ * @brief QetGraphicsItem::setPos
+ *set the position of the item to p
+ * @param p the new position of item
+ */
+void QetGraphicsItem::setPos(const QPointF &p) {
+	if (p == pos()) return;
+	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;
+		QGraphicsItem::setPos(p_x, p_y);
+	} else QGraphicsItem::setPos(p);
+}
+
+/**
+ * @brief QetGraphicsItem::setPos
+ *set the position of the item
+ * @param x new abscisse of item
+ * @param y new ordonne of item
+ */
+void QetGraphicsItem::setPos(qreal x, qreal y) {
+	setPos(QPointF(x, y));
+}
+
+/**
+ * @brief QetGraphicsItem::mousePressEvent
+ *handle the mouse click
+ * @param e
+ */
+void QetGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *e) {
+	first_move_ = true;
+	if (e -> modifiers() & Qt::ControlModifier) {
+		setSelected(!isSelected());
+	}
+	QGraphicsItem::mousePressEvent(e);
+}
+
+/**
+ * @brief QetGraphicsItem::mouseDoubleClickEvent
+ *handle the mouse double click
+ * @param e
+ */
+void QetGraphicsItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e) {
+	Q_UNUSED (e);
+	editProperty();
+}
+
+/**
+ * @brief QetGraphicsItem::mouseMoveEvent
+ *handle mouse movement
+ * @param e
+ */
+void QetGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
+	if (isSelected() && e -> buttons() & Qt::LeftButton) {
+		//Item is moving
+		if(diagram() && first_move_) {
+			//It's the first movement, we signal it to parent diagram
+			diagram() -> beginMoveElements(this);
+		}
+
+		//we apply the mouse movement
+		QPointF old_pos = pos();
+		if (first_move_) {
+			mouse_to_origin_movement_ = old_pos - e -> buttonDownScenePos(Qt::LeftButton);
+		}
+		QPointF expected_pos = e -> scenePos() + mouse_to_origin_movement_;
+		setPos(expected_pos); // setPos() will snap the expected position to the grid
+
+		//we calcul the real movement apply by setPos()
+		QPointF effective_movement = pos() - old_pos;
+		if (diagram()) {
+			//we signal the real movement apply to diagram,
+			//who he apply to other selected item
+			diagram() -> continueMoveElements(effective_movement);
+		}
+	} else e -> ignore();
+
+	if (first_move_) first_move_ = false;
+}
+
+/**
+ * @brief QetGraphicsItem::mouseReleaseEvent
+ *handle mouse release click
+ * @param e
+ */
+void QetGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
+	if (diagram()) diagram() -> endMoveElements();
+	if (!(e -> modifiers() & Qt::ControlModifier)) QGraphicsItem::mouseReleaseEvent(e);
+}

Added: branches/devel/sources/qetgraphicsitem/qetgraphicsitem.h
===================================================================
--- branches/devel/sources/qetgraphicsitem/qetgraphicsitem.h	                        (rev 0)
+++ branches/devel/sources/qetgraphicsitem/qetgraphicsitem.h	2013-10-27 18:59:44 UTC (rev 2587)
@@ -0,0 +1,58 @@
+/*
+	Copyright 2006-2013 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 QETGRAPHICSITEM_H
+#define QETGRAPHICSITEM_H
+
+#include "diagram.h"
+
+class QetGraphicsItem : public QGraphicsObject {
+	Q_OBJECT
+
+	public:
+	//constructor destructor
+	QetGraphicsItem(QetGraphicsItem *parent = 0);
+	virtual ~QetGraphicsItem();
+
+	//abstarct methode	
+	virtual bool fromXml(const QDomElement &document) = 0;
+	virtual QDomElement toXml(QDomDocument &document) const = 0;
+	virtual void editProperty () = 0;
+
+	//public methode
+	Diagram* diagram() const;
+	virtual void setPos(const QPointF &p);
+	virtual void setPos(qreal x, qreal y);
+
+	signals:
+
+	public slots:
+
+	//protected method
+	protected:
+	virtual void mousePressEvent(QGraphicsSceneMouseEvent *e);
+	virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *e);
+	virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
+	virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
+
+	protected:
+	bool first_move_;
+	QPointF mouse_to_origin_movement_;
+
+};
+
+#endif // QETGRAPHICSITEM_H


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