GPL V2. Added settings to documentation
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( "id").isEmpty() )
483 lastOO->setID (a.value ("id"));
484 if (!a.value( "url").isEmpty() )
485 lastOO->setURL (a.value ("url"));
486 if (!a.value( "vymLink").isEmpty() )
487 lastOO->setVymLink (a.value ("vymLink"));
488 if (!a.value( "hideInExport").isEmpty() )
489 if (a.value("hideInExport")=="true")
490 lastOO->setHideInExport(true);
492 if (!a.value( "hideLink").isEmpty())
494 if (a.value ("hideLink") =="true")
495 lastOO->setHideLinkUnselected(true);
497 lastOO->setHideLinkUnselected(false);
503 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
504 { // only for backward compatibility (<1.4.6). Use htmlnote now.
507 if (!a.value( "href").isEmpty() )
510 fn=parseHREF(a.value ("href") );
512 QString s; // Reading a note
514 if ( !file.open( QIODevice::ReadOnly) )
516 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
519 QTextStream stream( &file );
521 while ( !stream.atEnd() ) {
522 lines += stream.readLine()+"\n";
526 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
529 if (!a.value( "fonthint").isEmpty() )
530 no.setFontHint(a.value ("fonthint") );
531 lastBranch->setNote(no);
535 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
539 //if (!readOOAttr(a)) return false;
541 if (!a.value( "useOrientation").isEmpty() )
543 if (a.value ("useOrientation") =="true")
544 lastFloat->setUseOrientation (true);
546 lastFloat->setUseOrientation (false);
548 if (!a.value( "href").isEmpty() )
551 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
553 QMessageBox::warning( 0, "Warning: " ,
554 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
555 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
561 if (!a.value( "floatExport").isEmpty() )
563 // Only for compatibility. THis is not used since 1.7.11
564 if (a.value ("floatExport") =="true")
565 lastFloat->setFloatExport(true);
567 lastFloat->setFloatExport (false);
569 if (!a.value( "zPlane").isEmpty() )
570 lastFloat->setZValue (a.value("zPlane").toInt ());
573 if (!a.value( "relPosX").isEmpty() )
575 if (!a.value( "relPosY").isEmpty() )
578 x=a.value("relPosX").toInt (&okx, 10);
579 y=a.value("relPosY").toInt (&oky, 10);
583 lastFloat->setRelPos (QPoint (x,y) );
584 // make sure floats in mapcenter are repositioned to relative pos
585 if (mc==lastBranch) mc->positionContents();
588 // Couldn't read relPos
593 if (!readOOAttr(a)) return false;
595 if (!a.value ("orgName").isEmpty() )
597 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
602 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
607 XLinkObj *xlo=new XLinkObj (mc->getScene());
608 if (!a.value( "color").isEmpty() )
610 col.setNamedColor(a.value("color"));
614 if (!a.value( "width").isEmpty() )
616 xlo->setWidth(a.value ("width").toInt (&okx, 10));
619 // Connecting by select string for compatibility with version < 1.8.76
620 if (!a.value( "beginBranch").isEmpty() )
622 if (!a.value( "endBranch").isEmpty() )
624 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
625 if (lmo && typeid (*lmo)==typeid (BranchObj))
627 xlo->setBegin ((BranchObj*)lmo);
628 lmo=mc->findObjBySelect (a.value( "endBranch"));
629 if (lmo && typeid (*lmo)==typeid (BranchObj))
631 xlo->setEnd ((BranchObj*)(lmo));
635 success=true; // Not all branches there yet, no error
639 // object ID is used starting in version 1.8.76
640 if (!a.value( "beginID").isEmpty() )
642 if (!a.value( "endID").isEmpty() )
644 LinkableMapObj *lmo=mc->findID (a.value( "beginBranch"));
645 if (lmo && typeid (*lmo)==typeid (BranchObj))
647 xlo->setBegin ((BranchObj*)lmo);
648 lmo=mc->findID (a.value( "endID"));
649 if (lmo && typeid (*lmo)==typeid (BranchObj))
651 xlo->setEnd ((BranchObj*)(lmo));
655 success=true; // Not all branches there yet, no error
658 if (!success) delete (xlo);
662 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
664 for (int i=1; i<=a.count(); i++)
665 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
669 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
671 if (!a.value( "key").isEmpty() )
673 if (!a.value( "value").isEmpty() )
674 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));