initial kgraphinterface, by Yannick Schpilka
[kgraphinterface.git] / src / gihandler.cpp~
blobb0f1af8ff8622ed8a4204231e13fcd80bdadd838
1 /***************************************************************************
2  *   Copyright (C) 2007 by Schpilka Yannick   *
3  *   schpilka@localhost   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  *                                                                         *
20  *   In addition, as a special exception, the copyright holders give       *
21  *   permission to link the code of this program with any edition of       *
22  *   the Qt library by Trolltech AS, Norway (or with modified versions     *
23  *   of Qt that use the same license as Qt), and distribute linked         *
24  *   combinations including the two.  You must obey the GNU General        *
25  *   Public License in all respects for all of the code used other than    *
26  *   Qt.  If you modify this file, you may extend this exception to        *
27  *   your version of the file, but you are not obligated to do so.  If     *
28  *   you do not wish to do so, delete this exception statement from        *
29  *   your version.                                                         *
30  ***************************************************************************/
31 #include "gihandler.h"
32 #include "graph.h"
33 #include "node.h"
34 #include <qmessagebox.h>
36 /**Contenu possibles dans un node*/
37 #include <nodecontent.h>
38 #include <qpushbutton.h>
39 #include <qtextedit.h>
40 #include <qimage.h>
41 #include <qpixmap.h>
43 GIHandler::GIHandler(Graph *g){
44         graph = g;
45         nbr = 0;
49 GIHandler::~GIHandler(){
53 bool GIHandler::startElement(const QString &,
54                              const QString &,
55                              const QString &qName,
56                              const QXmlAttributes &att){
59         if (!giTag && qName != "gi"){
60                 erreur = QObject::tr("This file is not supported, gi required");
61                 return false;
62         }
64         if (qName == "gi"){
65                 QString v = att.value("version");
66                 if (!v.isEmpty() && v != "1.0"){
67                         erreur = QObject::tr("This file is not a GI version 1.0 file");
68                         return false;
69                 }
70                 giTag = true;
71         }
73         else if(qName =="graph"){
74                 QString v = att.value("id");
75                 if (v.isEmpty())
76                         v = "untitled";
78                 graph->setName(v);
79         }
81         else if(qName =="node"){
82                 current_node = att.value("id");
83                 c_node = new Node(graph,current_node);
84                 int x = att.value("x").toInt();
85                 int y = att.value("y").toInt();
86                 graph->add(c_node,QPoint(x,y));
87         }
89         else if(qName =="edge"){
90                 QString n1 = att.value("source");
91                 QString n2 = att.value("dest");
92                 QString type = att.value("type");
93                 int dir = att.value("direction").toInt();
94                 graph->link(n1,n2);
95         }
96         else {
97                 int t = NodeContent::typeByName( qName );
98                 if (t = -1){
99                         erreur = QObject::tr("Content type " + qName + " unknown");
100                         return false;
101                 }
102                 NodeContent *c = new NodeContent(c_node,t,nbr++);       
103                 if (c->isNull()){
104                         erreur = QObject::tr("Cannot load " + qName + "content");
105                         return false;
106                 }
107                 QString url = att.value("url");
108                 if (!url.isEmpty()){
109                         c->open( url );
110                         c_node->push(c);
111                 }
112         }
114         buffer = "";
115         return true;
118 bool GIHandler::endElement  (const QString &,
119                              const QString &,
120                              const QString &qName){
121         if (qName == "gi")
122                 graph->repaint();
123         else if(qName =="node"){
124                 current_node = "";
125                 c_node = 0;
126         }
127         return true;
130 bool GIHandler::characters  (const QString &txt){
131         buffer += txt;
132         return true;
135 bool GIHandler::fatalError (const QXmlParseException &exception){
136         QMessageBox::critical (graph,
137                                 QObject::tr("Graph Interface Parser"),
138                                 QObject::tr("Parse error at line %1, column %2:\n""%3")
139                                 .arg(exception.lineNumber())
140                                 .arg(exception.columnNumber())
141                                 .arg(exception.message()));
142         return false;
145 QString GIHandler::errorString() const{
146         return erreur;