10 #include "linkablemapobj.h"
13 static BranchObj *lastBranch;
14 static FloatObj *lastFloat;
15 static OrnamentedObj *lastOO;
17 extern Settings settings;
18 extern QString vymVersion;
20 mapBuilderHandler::mapBuilderHandler() {}
22 mapBuilderHandler::~mapBuilderHandler() {}
24 QString mapBuilderHandler::errorProtocol() { return errorProt; }
27 bool mapBuilderHandler::startDocument()
31 laststate = StateInit;
33 stateStack.append(StateInit);
41 QString mapBuilderHandler::parseHREF(QString href)
43 QString type=href.section(":",0,0);
44 QString path=href.section(":",1,1);
45 if (!tmpDir.endsWith("/"))
46 return tmpDir + "/" + path;
51 bool mapBuilderHandler::startElement ( const QString&, const QString&,
52 const QString& eName, const QXmlAttributes& atts )
56 cout << "startElement <"<< eName.ascii()
58 <<" laststate="<<stateStack.last()
59 <<" loadMode="<<loadMode
60 <<" line="<<QXmlDefaultHandler::lineNumber()
63 stateStack.append (state);
64 if ( state == StateInit && (eName == "vymmap") )
67 if (!atts.value( "version").isEmpty() )
69 if (!checkVersion(atts.value("version")))
70 QMessageBox::warning( 0, "Warning: Version Problem" ,
71 "<h3>Map is newer than VYM</h3>"
72 "<p>The map you are just trying to load was "
73 "saved using vym " +atts.value("version")+". "
74 "The version of this vym is " + vymVersion +
75 ". If you run into problems after pressing "
76 "the ok-button below, updating vym should help.");
78 mc->setVersion(atts.value( "version" ));
81 if (loadMode==NewMap ||
82 (loadMode==ImportReplace && me->getSelection()==mc))
84 if (!atts.value( "author").isEmpty() )
86 mc->setAuthor(atts.value( "author" ) );
88 if (!atts.value( "comment").isEmpty() )
90 mc->setComment (atts.value( "comment" ) );
92 if (!atts.value( "backgroundColor").isEmpty() )
94 col.setNamedColor(atts.value("backgroundColor"));
95 mc->getScene()->setBackgroundBrush(col);
97 if (!atts.value( "selectionColor").isEmpty() )
99 col.setNamedColor(atts.value("selectionColor"));
100 me->setSelectionColor(col);
102 if (!atts.value( "linkColorHint").isEmpty() )
104 if (atts.value("linkColorHint")=="HeadingColor")
105 me->setMapLinkColorHint(LinkableMapObj::HeadingColor);
107 me->setMapLinkColorHint(LinkableMapObj::DefaultColor);
109 if (!atts.value( "linkStyle").isEmpty() )
111 me->setMapLinkStyle(atts.value("linkStyle"));
113 if (!atts.value( "linkColor").isEmpty() )
115 col.setNamedColor(atts.value("linkColor"));
116 me->setMapDefLinkColor(col);
118 if (!atts.value( "defXLinkColor").isEmpty() )
120 col.setNamedColor(atts.value("defXLinkColor"));
121 me->setMapDefXLinkColor(col);
123 if (!atts.value( "defXLinkWidth").isEmpty() )
125 me->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
128 } else if ( eName == "select" && state == StateMap )
130 state=StateMapSelect;
131 } else if ( eName == "setting" && state == StateMap )
133 state=StateMapSetting;
134 if (loadMode==NewMap)
135 readSettingAttr (atts);
136 } else if ( eName == "mapcenter" && state == StateMap )
138 state=StateMapCenter;
139 if (loadMode==NewMap)
141 // Really use the found mapcenter as MCO in a new map
142 lastBranch=mc; // avoid empty pointer
145 // Treat the found mapcenter as a branch
146 // in an existing map
147 LinkableMapObj* lmo=me->getSelection();
148 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
149 || (typeid(*lmo) == typeid(MapCenterObj) ) )
151 lastBranch=(BranchObj*)lmo;
152 if (loadMode==ImportAdd)
154 lastBranch->addBranch();
155 lastBranch=lastBranch->getLastBranch();
161 readBranchAttr (atts);
163 (eName == "standardflag" ||eName == "standardFlag") &&
164 (state == StateMapCenter || state==StateBranch))
166 state=StateStandardFlag;
167 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
171 if (!atts.value( "textColor").isEmpty() )
173 col.setNamedColor(atts.value("textColor"));
174 lastBranch->setColor(col );
176 } else if ( eName == "note" &&
177 (state == StateMapCenter ||state==StateBranch))
178 { // only for backward compatibility (<1.4.6). Use htmlnote now.
180 if (!readNoteAttr (atts) ) return false;
181 } else if ( eName == "htmlnote" && state == StateMapCenter)
185 } else if ( eName == "floatimage" &&
186 (state == StateMapCenter ||state==StateBranch))
188 state=StateFloatImage;
189 lastBranch->addFloatImage();
190 lastFloat=lastBranch->getLastFloatImage();
191 if (!readFloatImageAttr(atts)) return false;
192 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
194 // This is used in vymparts, which have no mapcenter!
196 LinkableMapObj* lmo=me->getSelection();
199 // If a vym part is _loaded_ (not imported),
200 // selection==lmo==NULL
201 // Treat it like ImportAdd then...
205 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
206 || (typeid(*lmo) == typeid(MapCenterObj) ) )
208 lastBranch=(BranchObj*)(lmo);
212 if (loadMode==ImportAdd)
214 lastBranch->addBranch();
215 lastBranch=lastBranch->getLastBranch();
220 readBranchAttr (atts);
221 } else if (eName=="floatimage")
223 state=StateFloatImage;
224 lastBranch->addFloatImage();
225 lastFloat=lastBranch->getLastFloatImage();
226 if (!readFloatImageAttr(atts)) return false;
229 } else if ( eName == "branch" && state == StateMapCenter)
233 lastBranch->addBranch();
234 lastBranch=lastBranch->getLastBranch();
235 readBranchAttr (atts);
236 } else if ( eName == "htmlnote" && state == StateBranch)
241 if (!atts.value( "fonthint").isEmpty() )
242 no.setFontHint(atts.value ("fonthint") );
243 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
247 if (!readFrameAttr(atts)) return false;
248 } else if ( eName == "xlink" && state == StateBranch )
250 state=StateBranchXLink;
251 if (!readXLinkAttr (atts)) return false;
252 } else if ( eName == "branch" && state == StateBranch )
254 lastBranch->addBranch();
255 lastBranch=lastBranch->getLastBranch();
257 readBranchAttr (atts);
258 } else if ( eName == "html" && state == StateHtmlNote )
264 } else if ( state == StateHtml )
266 // accept all while in html mode,
271 return false; // Error
275 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
278 cout << "endElement </" <<eName.ascii()
280 <<" laststate=" <<laststate
281 <<" stateStack="<<stateStack.last()
287 lastBranch=(BranchObj*)(lastBranch->getParObj());
290 htmldata+="</"+eName+">";
294 htmldata.replace ("<br></br>","<br />");
295 no.setNote (htmldata);
296 lastBranch->setNote (no);
302 state=stateStack.takeLast();
306 bool mapBuilderHandler::characters ( const QString& ch)
308 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
310 QString ch_org=quotemeta (ch);
311 QString ch_simplified=ch.simplifyWhiteSpace();
312 if ( ch_simplified.isEmpty() ) return true;
316 case StateInit: break;
317 case StateMap: break;
319 me->select(ch_simplified);
321 case StateMapSetting:break;
322 case StateMapCenter: break;
324 lastBranch->setNote(ch_simplified);
326 case StateBranch: break;
327 case StateStandardFlag:
328 lastBranch->activateStandardFlag(ch_simplified);
330 case StateFloatImage: break;
331 case StateHtmlNote: break;
336 lastBranch->setHeading(ch_simplified);
344 QString mapBuilderHandler::errorString()
346 return "the document is not in the VYM file format";
349 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
351 errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
352 .arg( exception.message() )
353 .arg( exception.lineNumber() )
354 .arg( exception.columnNumber() );
355 // Try to read the bogus line
356 errorProt+=QString("File is: %1\n").arg(inputFile);
358 if (loadStringFromDisk (inputFile,s))
360 QStringList sl=QStringList::split ("\n",s);
362 QStringList::Iterator it = sl.begin();
363 while (i<exception.lineNumber()-1)
369 s.insert (exception.columnNumber()-1,"<ERROR>");
372 return QXmlDefaultHandler::fatalError( exception );
375 void mapBuilderHandler::setMapEditor (MapEditor* e)
378 mc=me->getMapCenter();
381 void mapBuilderHandler::setTmpDir (QString tp)
386 void mapBuilderHandler::setInputFile (QString f)
391 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
396 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
399 if (!readOOAttr(a)) return false;
401 if (!a.value( "scrolled").isEmpty() )
402 lastBranch->toggleScroll();
403 if (!a.value( "frameType").isEmpty() )
404 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
406 if (!a.value( "incImgV").isEmpty() )
408 if (a.value("incImgV")=="true")
409 lastBranch->setIncludeImagesVer(true);
411 lastBranch->setIncludeImagesVer(false);
413 if (!a.value( "incImgH").isEmpty() )
415 if (a.value("incImgH")=="true")
416 lastBranch->setIncludeImagesHor(true);
418 lastBranch->setIncludeImagesHor(false);
423 bool mapBuilderHandler::readFrameAttr (const QXmlAttributes& a)
429 if (!a.value( "frameType").isEmpty() )
430 lastOO->setFrameType (a.value("frameType"));
431 if (!a.value( "penColor").isEmpty() )
432 lastOO->setFramePenColor (a.value("penColor"));
433 if (!a.value( "brushColor").isEmpty() )
434 lastOO->setFrameBrushColor (a.value("brushColor"));
435 if (!a.value( "padding").isEmpty() )
437 x=a.value("padding").toInt(&ok);
438 if (ok) lastOO->setFramePadding(x);
440 if (!a.value( "borderWidth").isEmpty() )
442 x=a.value("borderWidth").toInt(&ok);
443 if (ok) lastOO->setFrameBorderWidth(x);
449 bool mapBuilderHandler::readOOAttr (const QXmlAttributes& a)
455 if (!a.value( "relPosX").isEmpty() )
457 if (!a.value( "relPosY").isEmpty() )
459 x=a.value("relPosX").toInt (&okx, 10);
460 y=a.value("relPosY").toInt (&oky, 10);
463 lastOO->setUseRelPos (true);
464 lastOO->move2RelPos (x,y);
467 return false; // Couldn't read relPos
470 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
472 if (!a.value( "absPosY").isEmpty() )
474 x=a.value("absPosX").toInt (&okx, 10);
475 y=a.value("absPosY").toInt (&oky, 10);
479 return false; // Couldn't read absPos
482 if (!a.value( "url").isEmpty() )
483 lastOO->setURL (a.value ("url"));
484 if (!a.value( "vymLink").isEmpty() )
485 lastOO->setVymLink (a.value ("vymLink"));
486 if (!a.value( "hideInExport").isEmpty() )
487 if (a.value("hideInExport")=="true")
488 lastOO->setHideInExport(true);
490 if (!a.value( "hideLink").isEmpty())
492 if (a.value ("hideLink") =="true")
493 lastOO->setHideLinkUnselected(true);
495 lastOO->setHideLinkUnselected(false);
501 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
502 { // only for backward compatibility (<1.4.6). Use htmlnote now.
505 if (!a.value( "href").isEmpty() )
508 fn=parseHREF(a.value ("href") );
510 QString s; // Reading a note
512 if ( !file.open( QIODevice::ReadOnly) )
514 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
517 QTextStream stream( &file );
519 while ( !stream.atEnd() ) {
520 lines += stream.readLine()+"\n";
524 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
527 if (!a.value( "fonthint").isEmpty() )
528 no.setFontHint(a.value ("fonthint") );
529 lastBranch->setNote(no);
533 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
537 //if (!readOOAttr(a)) return false;
539 if (!a.value( "useOrientation").isEmpty() )
541 if (a.value ("useOrientation") =="true")
542 lastFloat->setUseOrientation (true);
544 lastFloat->setUseOrientation (false);
546 if (!a.value( "href").isEmpty() )
549 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
551 QMessageBox::warning( 0, "Warning: " ,
552 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
553 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
559 if (!a.value( "floatExport").isEmpty() )
561 // Only for compatibility. THis is not used since 1.7.11
562 if (a.value ("floatExport") =="true")
563 lastFloat->setFloatExport(true);
565 lastFloat->setFloatExport (false);
567 if (!a.value( "zPlane").isEmpty() )
568 lastFloat->setZValue (a.value("zPlane").toInt ());
571 if (!a.value( "relPosX").isEmpty() )
573 if (!a.value( "relPosY").isEmpty() )
576 x=a.value("relPosX").toInt (&okx, 10);
577 y=a.value("relPosY").toInt (&oky, 10);
581 lastFloat->setRelPos (QPoint (x,y) );
582 // make sure floats in mapcenter are repositioned to relative pos
583 if (mc==lastBranch) mc->positionContents();
586 // Couldn't read relPos
591 if (!readOOAttr(a)) return false;
593 if (!a.value ("orgName").isEmpty() )
595 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
600 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
605 XLinkObj *xlo=new XLinkObj (mc->getScene());
606 if (!a.value( "color").isEmpty() )
608 col.setNamedColor(a.value("color"));
612 if (!a.value( "width").isEmpty() )
614 xlo->setWidth(a.value ("width").toInt (&okx, 10));
617 if (!a.value( "beginBranch").isEmpty() )
619 if (!a.value( "endBranch").isEmpty() )
621 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
622 if (lmo && typeid (*lmo)==typeid (BranchObj))
624 xlo->setBegin ((BranchObj*)lmo);
625 lmo=mc->findObjBySelect (a.value( "endBranch"));
626 if (lmo && typeid (*lmo)==typeid (BranchObj))
628 xlo->setEnd ((BranchObj*)(lmo));
632 success=true; // Not all branches there yet, no error
635 if (!success) delete (xlo);
639 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
641 for (int i=1; i<=a.count(); i++)
642 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
646 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
648 if (!a.value( "key").isEmpty() )
650 if (!a.value( "value").isEmpty() )
651 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));