1 /*
2  * wbnewitem.cpp - a class used for representing items on the whiteboard
3  *			  while they're being drawn.
4  * Copyright (C) 2008  Joonas Govenius
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  *
20  */
21 
22 #include "wbnewitem.h"
23 #include "../sxe/sxesession.h"
24 
25 #include <QSvgGenerator>
26 #include <QPainter>
27 #include <QStyleOptionGraphicsItem>
28 #include <QBuffer>
29 
WbNewItem(QGraphicsScene * s)30 WbNewItem::WbNewItem(QGraphicsScene* s) {
31 	scene = s;
32 }
33 
~WbNewItem()34 WbNewItem::~WbNewItem() {
35 }
36 
serializeToSvg(QDomDocument * doc)37 QDomNode WbNewItem::serializeToSvg(QDomDocument *doc) {
38 	if(!graphicsItem()) {
39 		return QDomDocumentFragment();
40 	}
41 
42 	// Generate the SVG using QSvgGenerator
43 	QBuffer buffer;
44 
45 	QSvgGenerator generator;
46 	generator.setOutputDevice(&buffer);
47 
48 	QPainter painter;
49 	QStyleOptionGraphicsItem options;
50 	painter.begin(&generator);
51 	graphicsItem()->paint(&painter, &options);
52 	painter.end();
53 
54 	// qDebug("Serialized SVG doc:");
55 	// qDebug(buffer.buffer());
56 
57 	// Parse the children of the new root <svg/> from the buffer to a document fragment
58 	// also add an 'id' attribute to each of the children
59 	doc->setContent(buffer.buffer());
60 	QDomDocumentFragment fragment = doc->createDocumentFragment();
61 
62 	for(QDomNode n = doc->documentElement().lastChild(); !n.isNull(); n = n.previousSibling()) {
63 		// skip <title/>, <desc/>, and <defs/>
64 		if(n.isElement() &&
65 			!(n.nodeName() == "title"
66 				|| n.nodeName() == "desc"
67 				|| n.nodeName() == "defs")) {
68 			n.toElement().setAttribute("id", "e" + SxeSession::generateUUID());
69 			fragment.insertBefore(n, QDomNode());
70 		}
71 	}
72 
73 	return fragment;
74 }
75