3 #include <qmessagebox.h>
5 #include <qstylesheet.h>
10 #include "linkablemapobj.h"
14 static BranchObj *lastBranch;
15 static FloatObj *lastFloat;
17 extern Settings settings;
19 mapBuilderHandler::mapBuilderHandler() {}
21 mapBuilderHandler::~mapBuilderHandler() {}
23 QString mapBuilderHandler::errorProtocol() { return errorProt; }
25 bool mapBuilderHandler::startDocument()
29 laststate = StateInit;
37 QString mapBuilderHandler::parseHREF(QString href)
39 QString type=href.section(":",0,0);
40 QString path=href.section(":",1,1);
41 if (!tmpDir.endsWith("/"))
42 return tmpDir + "/" + path;
47 bool mapBuilderHandler::startElement ( const QString&, const QString&,
48 const QString& eName, const QXmlAttributes& atts )
51 //cout << "startElement <"<<eName<<"> state="<<state <<" laststate="<<laststate<<" loadMode="<<loadMode<<endl;
52 if ( state == StateInit && (eName == "vymmap") )
55 if (!atts.value( "version").isEmpty() )
57 mc->setVersion(atts.value( "version" ));
58 if (!mc->checkVersion())
59 QMessageBox::warning( 0, "Warning: Version Problem" ,
60 "<h3>Map is newer than VYM</h3>"
61 "<p>The map you are just trying to load was "
62 "saved using vym " +atts.value("version")+". "
63 "The version of this vym is " __VYM_VERSION__
64 ". If you run into problems after pressing "
65 "the ok-button below, updating vym should help.");
70 if (!atts.value( "author").isEmpty() )
72 mc->setAuthor(atts.value( "author" ) );
74 if (!atts.value( "comment").isEmpty() )
76 mc->setComment (atts.value( "comment" ) );
78 if (!atts.value( "backgroundColor").isEmpty() )
80 col.setNamedColor(atts.value("backgroundColor"));
81 mc->getCanvas()->setBackgroundColor(col);
83 if (!atts.value( "linkColorHint").isEmpty() )
85 if (atts.value("linkColorHint")=="HeadingColor")
86 me->setLinkColorHint(HeadingColor);
88 me->setLinkColorHint(DefaultColor);
90 if (!atts.value( "linkStyle").isEmpty() )
92 QString s=atts.value("linkStyle");
94 me->setLinkStyle(StyleLine);
96 if (s=="StyleParabel")
97 me->setLinkStyle(StyleParabel);
99 if (s=="StylePolyLine")
100 me->setLinkStyle(StylePolyLine);
102 me->setLinkStyle(StylePolyParabel);
104 if (!atts.value( "linkColor").isEmpty() )
106 col.setNamedColor(atts.value("linkColor"));
107 me->setLinkColor(col);
110 } else if ( eName == "select" && state == StateMap )
112 state=StateMapSelect;
113 } else if ( eName == "setting" && state == StateMap )
115 state=StateMapSetting;
116 if (loadMode==NewMap)
117 readSettingAttr (atts);
118 } else if ( eName == "mapcenter" && state == StateMap )
120 state=StateMapCenter;
121 if (loadMode==NewMap)
123 // Really use the found mapcenter as MCO in a new map
124 lastBranch=mc; // avoid empty pointer
127 // Treat the found mapcenter as a branch
128 // in an existing map
129 LinkableMapObj* lmo=me->getSelection();
130 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
131 || (typeid(*lmo) == typeid(MapCenterObj) ) )
133 lastBranch=(BranchObj*)(lmo);
134 if (loadMode==ImportAdd)
136 lastBranch->addBranch();
137 lastBranch=lastBranch->getLastBranch();
143 readBranchAttr (atts);
144 } else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter)
146 state=StateMapCenterStandardFlag;
147 } else if ( eName == "heading" && state == StateMapCenter)
149 state=StateMapCenterHeading;
150 if (!atts.value( "textColor").isEmpty() )
152 col.setNamedColor(atts.value("textColor"));
153 lastBranch->setColor(col ,false );
155 } else if ( eName == "note" && state == StateMapCenter)
156 { // only for backward compatibility (<1.4.6). Use htmlnote now.
157 state=StateMapCenterNote;
158 if (!readNoteAttr (atts) ) return false;
159 } else if ( eName == "htmlnote" && state == StateMapCenter)
163 } else if ( eName == "floatimage" && state == StateMapCenter )
165 state=StateMapCenterFloatImage;
166 lastBranch->addFloatImage();
167 lastFloat=lastBranch->getLastFloatImage();
168 if (!readFloatImageAttr(atts)) return false;
169 } else if ( eName == "branch" && state == StateMap)
171 // This is used in vymparts, which have no mapcenter!
174 LinkableMapObj* lmo=me->getSelection();
177 // If a vym part is _loaded_ (not imported),
178 // selection==lmo==NULL
179 // Treat it like ImportAdd then...
183 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
184 || (typeid(*lmo) == typeid(MapCenterObj) ) )
186 lastBranch=(BranchObj*)(lmo);
187 if (loadMode==ImportAdd)
189 lastBranch->addBranch();
190 lastBranch=lastBranch->getLastBranch();
196 readBranchAttr (atts);
197 } else if ( eName == "branch" && state == StateMapCenter)
201 lastBranch->addBranch();
202 lastBranch=lastBranch->getLastBranch();
203 readBranchAttr (atts);
204 } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch)
206 state=StateBranchStandardFlag;
207 } else if ( eName == "heading" && state == StateBranch)
209 state=StateBranchHeading;
210 if (!atts.value( "textColor").isEmpty() )
212 col.setNamedColor(atts.value("textColor"));
213 lastBranch->setColor(col ,false );
215 } else if ( eName == "note" && state == StateBranch)
217 state=StateBranchNote;
218 if (!readNoteAttr (atts) ) return false;
219 } else if ( eName == "htmlnote" && state == StateBranch)
224 if (!atts.value( "fonthint").isEmpty() )
225 no.setFontHint(atts.value ("fonthint") );
226 } else if ( eName == "floatimage" && state == StateBranch )
228 state=StateBranchFloatImage;
229 lastBranch->addFloatImage();
230 lastFloat=lastBranch->getLastFloatImage();
231 if (!readFloatImageAttr(atts)) return false;
232 } else if ( eName == "xlink" && state == StateBranch )
234 state=StateBranchXLink;
235 if (!readXLinkAttr (atts)) return false;
236 } else if ( eName == "branch" && state == StateBranch )
238 lastBranch->addBranch();
239 lastBranch=lastBranch->getLastBranch();
241 readBranchAttr (atts);
242 } else if ( eName == "html" && state == StateHtmlNote )
248 } else if ( state == StateHtml )
250 // accept all while in html mode,
255 return false; // Error
259 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
261 // cout << "endElement </"<<eName<<"> state="<<state <<" laststate="<<laststate<<endl;
264 case StateMapSelect: state=StateMap; return true;
265 case StateMapSetting: state=StateMap; return true;
266 case StateMapCenter: state=StateMap; return true;
267 case StateMapCenterStandardFlag: state=StateMapCenter; return true;
268 case StateMapCenterHeading: state=StateMapCenter; return true;
269 case StateMapCenterNote: state=StateMapCenter; return true;
270 case StateMapCenterFloatImage: state=StateMapCenter; return true;
282 state=StateMapCenter;
284 lastBranch=(BranchObj*)(lastBranch->getParObj());
286 case StateBranchStandardFlag: state=StateBranch; return true;
287 case StateBranchHeading: state=StateBranch; return true;
288 case StateBranchNote: state=StateBranch; return true;
289 case StateBranchFloatImage: state=StateBranch; return true;
290 case StateBranchXLink: state=StateBranch; return true;
291 case StateHtmlNote: state=laststate; return true;
293 htmldata+="</"+eName+">";
297 htmldata.replace ("<br></br>","<br />");
298 no.setNote (htmldata);
299 lastBranch->setNote (no);
305 case StateMap: state=StateInit; return true;
307 // even for HTML includes, this should never be reached
312 bool mapBuilderHandler::characters ( const QString& ch)
314 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
316 QString ch_org=quotemeta (ch);
317 QString ch_simplified=ch.simplifyWhiteSpace();
318 if ( ch_simplified.isEmpty() ) return true;
322 case StateInit: break;
323 case StateMap: break;
325 me->select(ch_simplified);
327 case StateMapSetting:break;
328 case StateMapCenter: break;
329 case StateMapCenterStandardFlag:
330 lastBranch->activateStandardFlag(ch_simplified);
332 case StateMapCenterHeading:
333 lastBranch->setHeading(ch_simplified);
335 case StateMapCenterNote:
336 lastBranch->setNote(ch_simplified);
338 case StateBranch: break;
339 case StateBranchStandardFlag:
340 lastBranch->activateStandardFlag(ch_simplified);
342 case StateBranchHeading:
343 lastBranch->setHeading(ch_simplified);
345 case StateBranchNote:
346 lastBranch->setNote(ch_simplified);
348 case StateBranchFloatImage: break;
349 case StateHtmlNote: break;
359 QString mapBuilderHandler::errorString()
361 return "the document is not in the VYM file format";
364 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
366 errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
367 .arg( exception.message() )
368 .arg( exception.lineNumber() )
369 .arg( exception.columnNumber() );
371 return QXmlDefaultHandler::fatalError( exception );
374 void mapBuilderHandler::setMapEditor (MapEditor* e)
377 mc=me->getMapCenter();
380 void mapBuilderHandler::setTmpDir (QString tp)
385 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
390 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
394 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
396 if (!a.value( "absPosY").isEmpty() )
398 x=a.value("absPosX").toInt (&okx, 10);
399 y=a.value("absPosY").toInt (&oky, 10);
401 lastBranch->move(x,y);
403 return false; // Couldn't read absPos
406 if (!a.value( "scrolled").isEmpty() )
407 lastBranch->toggleScroll();
408 if (!a.value( "url").isEmpty() )
409 lastBranch->setURL (a.value ("url"));
410 if (!a.value( "vymLink").isEmpty() )
411 lastBranch->setVymLink (a.value ("vymLink"));
412 if (!a.value( "frameType").isEmpty() )
413 lastBranch->setFrameType (a.value("frameType"));
417 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
418 { // only for backward compatibility (<1.4.6). Use htmlnote now.
421 if (!a.value( "href").isEmpty() )
424 fn=parseHREF(a.value ("href") );
426 QString s; // Reading a note
428 if ( !file.open( IO_ReadOnly) )
430 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
433 QTextStream stream( &file );
435 while ( !stream.eof() ) {
436 lines += stream.readLine()+"\n";
439 // Convert to richtext
440 if ( !QStyleSheet::mightBeRichText( lines ) )
442 // Here we are workarounding the QT conversion method:
443 // convertFromPlainText does not generate valid xml, needed
444 // for the parser, but just <p> and <br> without closing tags.
445 // So we have to add those by ourselves
446 //lines=quotemeta (lines);
447 lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
448 lines.replace ("<br>","<br />");
451 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
454 if (!a.value( "fonthint").isEmpty() )
455 no.setFontHint(a.value ("fonthint") );
456 if (state == StateMapCenterNote)
459 lastBranch->setNote(no);
463 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
465 if (!a.value( "useOrientation").isEmpty() )
467 if (a.value ("useOrientation") =="true")
468 lastFloat->setUseOrientation (true);
470 lastFloat->setUseOrientation (false);
472 if (!a.value( "href").isEmpty() )
475 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
477 QMessageBox::warning( 0, "Warning: " ,
478 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
479 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
485 if (!a.value( "floatExport").isEmpty() )
487 if (a.value ("floatExpofrt") =="true")
488 lastFloat->setFloatExport (true);
490 lastFloat->setFloatExport (false);
492 if (!a.value( "zPlane").isEmpty() )
493 lastFloat->setZ (a.value("zPlane").toInt ());
496 if (!a.value( "relPosX").isEmpty() )
498 if (!a.value( "relPosY").isEmpty() )
501 x=a.value("relPosX").toInt (&okx, 10);
502 y=a.value("relPosY").toInt (&oky, 10);
504 lastFloat->setRelPos (QPoint (x,y) );
506 // Couldn't read relPos
513 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
516 XLinkObj *xlo=new XLinkObj (mc->getCanvas());
517 if (!a.value( "xLinkColor").isEmpty() )
519 col.setNamedColor(a.value("xLinkColor"));
523 if (!a.value( "beginBranch").isEmpty() )
525 if (!a.value( "endBranch").isEmpty() )
527 LinkableMapObj *lmo=mc->findObj (a.value( "beginBranch"));
528 if (lmo && typeid (*lmo)==typeid (BranchObj))
530 xlo->setBegin ((BranchObj*)(lmo));
531 lmo=mc->findObj (a.value( "endBranch"));
532 if (lmo && typeid (*lmo)==typeid (BranchObj))
534 xlo->setEnd ((BranchObj*)(lmo));
539 return true; // Not all branches there yet, no error
545 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
547 for (int i=1; i<=a.count(); i++)
548 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
552 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
554 if (!a.value( "key").isEmpty() )
556 if (!a.value( "value").isEmpty() )
557 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));