[qet] [2443] Now DiagramImageItem can be save/load to/from .qet file

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


Revision: 2443
Author:   blacksun
Date:     2013-08-21 22:27:58 +0200 (Wed, 21 Aug 2013)
Log Message:
-----------
Now DiagramImageItem can be save/load to/from .qet file

Modified Paths:
--------------
    branches/0.4/sources/diagram.cpp
    branches/0.4/sources/diagramimageitem.cpp

Modified: branches/0.4/sources/diagram.cpp
===================================================================
--- branches/0.4/sources/diagram.cpp	2013-08-21 04:17:54 UTC (rev 2442)
+++ branches/0.4/sources/diagram.cpp	2013-08-21 20:27:58 UTC (rev 2443)
@@ -335,6 +335,7 @@
 	QList<Element *> list_elements;
 	QList<Conductor *> list_conductors;
 	QList<DiagramTextItem *> list_texts;
+	QList<DiagramImageItem *> list_images;
 	
 	// Determine les elements a "XMLiser"
 	foreach(QGraphicsItem *qgi, items()) {
@@ -351,6 +352,9 @@
 		} else if (IndependentTextItem *iti = qgraphicsitem_cast<IndependentTextItem *>(qgi)) {
 			if (whole_content) list_texts << iti;
 			else if (iti -> isSelected()) list_texts << iti;
+		} else if (DiagramImageItem *dii = qgraphicsitem_cast<DiagramImageItem *>(qgi)) {
+			if (whole_content) list_images << dii;
+			else if (dii -> isSelected()) list_images << dii;
 		}
 	}
 	
@@ -383,6 +387,15 @@
 		}
 		racine.appendChild(inputs);
 	}
+
+	// save of images
+	if (!list_images.isEmpty()) {
+		QDomElement images = document.createElement("images");
+		foreach (DiagramImageItem *dii, list_images) {
+			images.appendChild(dii -> toXml(document));
+		}
+		racine.appendChild(images);
+	}
 	
 	// on retourne le document XML ainsi genere
 	return(document);
@@ -537,6 +550,14 @@
 		addIndependentTextItem(iti);
 		added_texts << iti;
 	}
+
+	QList<DiagramImageItem *> added_images;
+	foreach (QDomElement image_xml, QET::findInDomElement(root, "images", "image")) {
+		DiagramImageItem *dii = new DiagramImageItem (this);
+		dii -> fromXml(image_xml);
+		addItem(dii);
+		added_images << dii;
+	}
 	
 	// gere la translation des nouveaux elements et texte si celle-ci est demandee
 	if (position != QPointF()) {
@@ -546,6 +567,7 @@
 		QList<QGraphicsItem *> added_items;
 		foreach (Element *added_element, added_elements) added_items << added_element;
 		foreach (DiagramTextItem *added_text, added_texts) added_items << added_text;
+		foreach (DiagramImageItem *added_image, added_images) added_items << added_image;
 		foreach (QGraphicsItem *item, added_items) {
 			QPointF csg = item -> mapToScene(item -> boundingRect()).boundingRect().topLeft();
 			qreal px = csg.x();
@@ -567,6 +589,9 @@
 		foreach (DiagramTextItem *added_text, added_texts) {
 			added_text -> setPos(added_text -> pos().x() + diff_x, added_text -> pos().y() + diff_y);
 		}
+		foreach (DiagramImageItem *added_image, added_images) {
+			added_image -> setPos(added_image -> pos().x() + diff_x, added_image -> pos().y() + diff_y);
+		}
 	}
 	
 	// chargement de tous les Conducteurs du fichier XML
@@ -602,6 +627,7 @@
 		content_ptr -> elements         = added_elements.toSet();
 		content_ptr -> conductorsToMove = added_conductors.toSet();
 		content_ptr -> textFields       = added_texts.toSet();
+		content_ptr -> images			= added_images.toSet();
 	}
 	
 	return(true);

Modified: branches/0.4/sources/diagramimageitem.cpp
===================================================================
--- branches/0.4/sources/diagramimageitem.cpp	2013-08-21 04:17:54 UTC (rev 2442)
+++ branches/0.4/sources/diagramimageitem.cpp	2013-08-21 20:27:58 UTC (rev 2443)
@@ -345,14 +345,27 @@
 }
 
 /**
-	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
+	Load the image from this xml element
+	@param e xml element that define an image
 */
-void DiagramImageItem::fromXml(const QDomElement &e) {
+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);
+
 	setPos(e.attribute("x").toDouble(), e.attribute("y").toDouble());
-	setRotationAngle(e.attribute("rotation").toDouble());
+	if (e.hasAttribute("rotation")) setRotationAngle(e.attribute("rotation").toDouble());
+
+	return (true);
 }
 
 /**
@@ -360,11 +373,19 @@
 	@return L'element XML representant ce champ de texte
 */
 QDomElement DiagramImageItem::toXml(QDomDocument &document) const {
-	QDomElement result = document.createElement("input");
+	QDomElement result = document.createElement("image");
+	//write some attribute
 	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()));
-	}
+	if (rotationAngle()) result.setAttribute("rotation", QString("%1").arg(rotationAngle()));
+
+	//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);
 }


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