10 #include "linkablemapobj.h"
13 static BranchObj *lastBranch;
14 static FloatObj *lastFloat;
15 static OrnamentedObj *lastOO;
17 extern Settings settings;
18 extern QString vymVersion;
21 parseVYMHandler::parseVYMHandler() {}
23 parseVYMHandler::~parseVYMHandler() {}
25 QString parseVYMHandler::errorProtocol() { return errorProt; }
29 bool parseVYMHandler::startDocument()
33 laststate = StateInit;
35 stateStack.append(StateInit);
44 QString parseVYMHandler::parseHREF(QString href)
46 QString type=href.section(":",0,0);
47 QString path=href.section(":",1,1);
48 if (!tmpDir.endsWith("/"))
49 return tmpDir + "/" + path;
54 bool parseVYMHandler::startElement ( const QString&, const QString&,
55 const QString& eName, const QXmlAttributes& atts )
59 cout << "startElement <"<< eName.ascii()
61 <<" laststate="<<stateStack.last()
62 <<" loadMode="<<loadMode
63 <<" line="<<QXmlDefaultHandler::lineNumber()
66 stateStack.append (state);
67 if ( state == StateInit && (eName == "vymmap") )
70 if (!atts.value( "version").isEmpty() )
72 if (!checkVersion(atts.value("version")))
73 QMessageBox::warning( 0, "Warning: Version Problem" ,
74 "<h3>Map is newer than VYM</h3>"
75 "<p>The map you are just trying to load was "
76 "saved using vym " +atts.value("version")+". "
77 "The version of this vym is " + vymVersion +
78 ". If you run into problems after pressing "
79 "the ok-button below, updating vym should help.");
81 mc->setVersion(atts.value( "version" ));
84 if (loadMode==NewMap ||
85 (loadMode==ImportReplace && me->getSelection()==mc))
87 if (!atts.value( "author").isEmpty() )
89 mc->setAuthor(atts.value( "author" ) );
91 if (!atts.value( "comment").isEmpty() )
93 mc->setComment (atts.value( "comment" ) );
95 if (!atts.value( "backgroundColor").isEmpty() )
97 col.setNamedColor(atts.value("backgroundColor"));
98 mc->getScene()->setBackgroundBrush(col);
100 if (!atts.value( "selectionColor").isEmpty() )
102 col.setNamedColor(atts.value("selectionColor"));
103 me->setSelectionColor(col);
105 if (!atts.value( "linkColorHint").isEmpty() )
107 if (atts.value("linkColorHint")=="HeadingColor")
108 me->setMapLinkColorHint(LinkableMapObj::HeadingColor);
110 me->setMapLinkColorHint(LinkableMapObj::DefaultColor);
112 if (!atts.value( "linkStyle").isEmpty() )
114 me->setMapLinkStyle(atts.value("linkStyle"));
116 if (!atts.value( "linkColor").isEmpty() )
118 col.setNamedColor(atts.value("linkColor"));
119 me->setMapDefLinkColor(col);
121 if (!atts.value( "defXLinkColor").isEmpty() )
123 col.setNamedColor(atts.value("defXLinkColor"));
124 me->setMapDefXLinkColor(col);
126 if (!atts.value( "defXLinkWidth").isEmpty() )
128 me->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
131 } else if ( eName == "select" && state == StateMap )
133 state=StateMapSelect;
134 } else if ( eName == "setting" && state == StateMap )
136 state=StateMapSetting;
137 if (loadMode==NewMap)
138 readSettingAttr (atts);
139 } else if ( eName == "mapcenter" && state == StateMap )
141 state=StateMapCenter;
142 if (loadMode==NewMap)
144 // Really use the found mapcenter as MCO in a new map
145 lastBranch=mc; // avoid empty pointer
148 // Treat the found mapcenter as a branch
149 // in an existing map
150 LinkableMapObj* lmo=me->getSelection();
151 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
152 || (typeid(*lmo) == typeid(MapCenterObj) ) )
154 lastBranch=(BranchObj*)lmo;
155 if (loadMode==ImportAdd)
157 lastBranch->addBranch();
158 lastBranch=lastBranch->getLastBranch();
164 readBranchAttr (atts);
166 (eName == "standardflag" ||eName == "standardFlag") &&
167 (state == StateMapCenter || state==StateBranch))
169 state=StateStandardFlag;
170 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
174 if (!atts.value( "textColor").isEmpty() )
176 col.setNamedColor(atts.value("textColor"));
177 lastBranch->setColor(col );
179 } else if ( eName == "note" &&
180 (state == StateMapCenter ||state==StateBranch))
181 { // only for backward compatibility (<1.4.6). Use htmlnote now.
183 if (!readNoteAttr (atts) ) return false;
184 } else if ( eName == "htmlnote" && state == StateMapCenter)
188 } else if ( eName == "floatimage" &&
189 (state == StateMapCenter ||state==StateBranch))
191 state=StateFloatImage;
192 lastBranch->addFloatImage();
193 lastFloat=lastBranch->getLastFloatImage();
194 if (!readFloatImageAttr(atts)) return false;
195 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
197 // This is used in vymparts, which have no mapcenter!
199 LinkableMapObj* lmo=me->getSelection();
202 // If a vym part is _loaded_ (not imported),
203 // selection==lmo==NULL
204 // Treat it like ImportAdd then...
208 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
209 || (typeid(*lmo) == typeid(MapCenterObj) ) )
211 lastBranch=(BranchObj*)(lmo);
215 if (loadMode==ImportAdd)
217 lastBranch->addBranch();
218 lastBranch=lastBranch->getLastBranch();
223 readBranchAttr (atts);
224 } else if (eName=="floatimage")
226 state=StateFloatImage;
227 lastBranch->addFloatImage();
228 lastFloat=lastBranch->getLastFloatImage();
229 if (!readFloatImageAttr(atts)) return false;
232 } else if ( eName == "branch" && state == StateMapCenter)
236 lastBranch->addBranch();
237 lastBranch=lastBranch->getLastBranch();
238 readBranchAttr (atts);
239 } else if ( eName == "htmlnote" && state == StateBranch)
244 if (!atts.value( "fonthint").isEmpty() )
245 no.setFontHint(atts.value ("fonthint") );
246 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
250 if (!readFrameAttr(atts)) return false;
251 } else if ( eName == "xlink" && state == StateBranch )
253 state=StateBranchXLink;
254 if (!readXLinkAttr (atts)) return false;
255 } else if ( eName == "branch" && state == StateBranch )
257 lastBranch->addBranch();
258 lastBranch=lastBranch->getLastBranch();
260 readBranchAttr (atts);
261 } else if ( eName == "html" && state == StateHtmlNote )
267 } else if ( state == StateHtml )
269 // accept all while in html mode,
274 return false; // Error
278 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
281 cout << "endElement </" <<eName.ascii()
283 <<" laststate=" <<laststate
284 <<" stateStack="<<stateStack.last()
290 lastBranch=(BranchObj*)(lastBranch->getParObj());
293 htmldata+="</"+eName+">";
297 htmldata.replace ("<br></br>","<br />");
298 no.setNote (htmldata);
299 lastBranch->setNote (no);
305 state=stateStack.takeLast();
309 bool parseVYMHandler::characters ( const QString& ch)
311 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
313 QString ch_org=quotemeta (ch);
314 QString ch_simplified=ch.simplifyWhiteSpace();
315 if ( ch_simplified.isEmpty() ) return true;
319 case StateInit: break;
320 case StateMap: break;
322 me->select(ch_simplified);
324 case StateMapSetting:break;
325 case StateMapCenter: break;
327 lastBranch->setNote(ch_simplified);
329 case StateBranch: break;
330 case StateStandardFlag:
331 lastBranch->activateStandardFlag(ch_simplified);
333 case StateFloatImage: break;
334 case StateHtmlNote: break;
339 lastBranch->setHeading(ch_simplified);
347 QString parseVYMHandler::errorString()
349 return "the document is not in the VYM file format";
353 bool parseVYMHandler::fatalError( const QXmlParseException& exception )
355 errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
356 .arg( exception.message() )
357 .arg( exception.lineNumber() )
358 .arg( exception.columnNumber() );
359 // Try to read the bogus line
360 errorProt+=QString("File is: %1\n").arg(inputFile);
362 if (loadStringFromDisk (inputFile,s))
364 QStringList sl=QStringList::split ("\n",s);
366 QStringList::Iterator it = sl.begin();
367 while (i<exception.lineNumber()-1)
373 s.insert (exception.columnNumber()-1,"<ERROR>");
376 return QXmlDefaultHandler::fatalError( exception );
379 void parseVYMHandler::setMapEditor (MapEditor* e)
382 mc=me->getMapCenter();
385 void parseVYMHandler::setTmpDir (QString tp)
390 void parseVYMHandler::setInputFile (QString f)
395 void parseVYMHandler::setLoadMode (const LoadMode &lm)
400 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
403 if (!readOOAttr(a)) return false;
405 if (!a.value( "scrolled").isEmpty() )
406 lastBranch->toggleScroll();
407 if (!a.value( "frameType").isEmpty() )
408 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
410 if (!a.value( "incImgV").isEmpty() )
412 if (a.value("incImgV")=="true")
413 lastBranch->setIncludeImagesVer(true);
415 lastBranch->setIncludeImagesVer(false);
417 if (!a.value( "incImgH").isEmpty() )
419 if (a.value("incImgH")=="true")
420 lastBranch->setIncludeImagesHor(true);
422 lastBranch->setIncludeImagesHor(false);
427 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
433 if (!a.value( "frameType").isEmpty() )
434 lastOO->setFrameType (a.value("frameType"));
435 if (!a.value( "penColor").isEmpty() )
436 lastOO->setFramePenColor (a.value("penColor"));
437 if (!a.value( "brushColor").isEmpty() )
438 lastOO->setFrameBrushColor (a.value("brushColor"));
439 if (!a.value( "padding").isEmpty() )
441 x=a.value("padding").toInt(&ok);
442 if (ok) lastOO->setFramePadding(x);
444 if (!a.value( "borderWidth").isEmpty() )
446 x=a.value("borderWidth").toInt(&ok);
447 if (ok) lastOO->setFrameBorderWidth(x);
453 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
459 if (!a.value( "relPosX").isEmpty() )
461 if (!a.value( "relPosY").isEmpty() )
463 x=a.value("relPosX").toFloat (&okx);
464 y=a.value("relPosY").toFloat (&oky);
467 lastOO->setUseRelPos (true);
468 lastOO->move2RelPos (x,y);
471 return false; // Couldn't read relPos
474 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
476 if (!a.value( "absPosY").isEmpty() )
478 x=a.value("absPosX").toFloat (&okx);
479 y=a.value("absPosY").toFloat (&oky);
483 return false; // Couldn't read absPos
486 if (!a.value( "id").isEmpty() )
487 lastOO->setID (a.value ("id"));
488 if (!a.value( "url").isEmpty() )
489 lastOO->setURL (a.value ("url"));
490 if (!a.value( "vymLink").isEmpty() )
491 lastOO->setVymLink (a.value ("vymLink"));
492 if (!a.value( "hideInExport").isEmpty() )
493 if (a.value("hideInExport")=="true")
494 lastOO->setHideInExport(true);
496 if (!a.value( "hideLink").isEmpty())
498 if (a.value ("hideLink") =="true")
499 lastOO->setHideLinkUnselected(true);
501 lastOO->setHideLinkUnselected(false);
507 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
508 { // only for backward compatibility (<1.4.6). Use htmlnote now.
511 if (!a.value( "href").isEmpty() )
514 fn=parseHREF(a.value ("href") );
516 QString s; // Reading a note
518 if ( !file.open( QIODevice::ReadOnly) )
520 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
523 QTextStream stream( &file );
525 while ( !stream.atEnd() ) {
526 lines += stream.readLine()+"\n";
530 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
533 if (!a.value( "fonthint").isEmpty() )
534 no.setFontHint(a.value ("fonthint") );
535 lastBranch->setNote(no);
539 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
543 //if (!readOOAttr(a)) return false;
545 if (!a.value( "useOrientation").isEmpty() )
547 if (a.value ("useOrientation") =="true")
548 lastFloat->setUseOrientation (true);
550 lastFloat->setUseOrientation (false);
552 if (!a.value( "href").isEmpty() )
555 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
557 QMessageBox::warning( 0, "Warning: " ,
558 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
559 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
565 if (!a.value( "floatExport").isEmpty() )
567 // Only for compatibility. THis is not used since 1.7.11
568 if (a.value ("floatExport") =="true")
569 lastFloat->setFloatExport(true);
571 lastFloat->setFloatExport (false);
573 if (!a.value( "zPlane").isEmpty() )
574 lastFloat->setZValue (a.value("zPlane").toInt ());
577 if (!a.value( "relPosX").isEmpty() )
579 if (!a.value( "relPosY").isEmpty() )
582 x=a.value("relPosX").toFloat (&okx);
583 y=a.value("relPosY").toFloat (&oky);
587 lastFloat->setRelPos (QPoint (x,y) );
588 // make sure floats in mapcenter are repositioned to relative pos
589 if (mc==lastBranch) mc->positionContents();
592 // Couldn't read relPos
597 if (!readOOAttr(a)) return false;
599 if (!a.value ("orgName").isEmpty() )
601 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
606 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
611 XLinkObj *xlo=new XLinkObj (mc->getScene());
612 if (!a.value( "color").isEmpty() )
614 col.setNamedColor(a.value("color"));
618 if (!a.value( "width").isEmpty() )
620 xlo->setWidth(a.value ("width").toInt (&okx, 10));
623 // Connecting by select string for compatibility with version < 1.8.76
624 if (!a.value( "beginBranch").isEmpty() )
626 if (!a.value( "endBranch").isEmpty() )
628 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
629 if (lmo && typeid (*lmo)==typeid (BranchObj))
631 xlo->setBegin ((BranchObj*)lmo);
632 lmo=mc->findObjBySelect (a.value( "endBranch"));
633 if (lmo && typeid (*lmo)==typeid (BranchObj))
635 xlo->setEnd ((BranchObj*)(lmo));
639 success=true; // Not all branches there yet, no error
643 // object ID is used starting in version 1.8.76
644 if (!a.value( "beginID").isEmpty() )
646 if (!a.value( "endID").isEmpty() )
648 LinkableMapObj *lmo=mc->findID (a.value( "beginBranch"));
649 if (lmo && typeid (*lmo)==typeid (BranchObj))
651 xlo->setBegin ((BranchObj*)lmo);
652 lmo=mc->findID (a.value( "endID"));
653 if (lmo && typeid (*lmo)==typeid (BranchObj))
655 xlo->setEnd ((BranchObj*)(lmo));
659 success=true; // Not all branches there yet, no error
662 if (!success) delete (xlo);
666 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
668 for (int i=1; i<=a.count(); i++)
669 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
673 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
675 if (!a.value( "key").isEmpty() )
677 if (!a.value( "value").isEmpty() )
678 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));