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";
352 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
355 if (!readOOAttr(a)) return false;
357 if (!a.value( "scrolled").isEmpty() )
358 lastBranch->toggleScroll();
359 if (!a.value( "frameType").isEmpty() )
360 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
362 if (!a.value( "incImgV").isEmpty() )
364 if (a.value("incImgV")=="true")
365 lastBranch->setIncludeImagesVer(true);
367 lastBranch->setIncludeImagesVer(false);
369 if (!a.value( "incImgH").isEmpty() )
371 if (a.value("incImgH")=="true")
372 lastBranch->setIncludeImagesHor(true);
374 lastBranch->setIncludeImagesHor(false);
379 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
385 if (!a.value( "frameType").isEmpty() )
386 lastOO->setFrameType (a.value("frameType"));
387 if (!a.value( "penColor").isEmpty() )
388 lastOO->setFramePenColor (a.value("penColor"));
389 if (!a.value( "brushColor").isEmpty() )
390 lastOO->setFrameBrushColor (a.value("brushColor"));
391 if (!a.value( "padding").isEmpty() )
393 x=a.value("padding").toInt(&ok);
394 if (ok) lastOO->setFramePadding(x);
396 if (!a.value( "borderWidth").isEmpty() )
398 x=a.value("borderWidth").toInt(&ok);
399 if (ok) lastOO->setFrameBorderWidth(x);
405 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
411 if (!a.value( "relPosX").isEmpty() )
413 if (!a.value( "relPosY").isEmpty() )
415 x=a.value("relPosX").toFloat (&okx);
416 y=a.value("relPosY").toFloat (&oky);
419 lastOO->setUseRelPos (true);
420 lastOO->move2RelPos (x,y);
423 return false; // Couldn't read relPos
426 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
428 if (!a.value( "absPosY").isEmpty() )
430 x=a.value("absPosX").toFloat (&okx);
431 y=a.value("absPosY").toFloat (&oky);
435 return false; // Couldn't read absPos
438 if (!a.value( "id").isEmpty() )
439 lastOO->setID (a.value ("id"));
440 if (!a.value( "url").isEmpty() )
441 lastOO->setURL (a.value ("url"));
442 if (!a.value( "vymLink").isEmpty() )
443 lastOO->setVymLink (a.value ("vymLink"));
444 if (!a.value( "hideInExport").isEmpty() )
445 if (a.value("hideInExport")=="true")
446 lastOO->setHideInExport(true);
448 if (!a.value( "hideLink").isEmpty())
450 if (a.value ("hideLink") =="true")
451 lastOO->setHideLinkUnselected(true);
453 lastOO->setHideLinkUnselected(false);
459 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
460 { // only for backward compatibility (<1.4.6). Use htmlnote now.
463 if (!a.value( "href").isEmpty() )
466 fn=parseHREF(a.value ("href") );
468 QString s; // Reading a note
470 if ( !file.open( QIODevice::ReadOnly) )
472 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
475 QTextStream stream( &file );
477 while ( !stream.atEnd() ) {
478 lines += stream.readLine()+"\n";
482 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
485 if (!a.value( "fonthint").isEmpty() )
486 no.setFontHint(a.value ("fonthint") );
487 lastBranch->setNote(no);
491 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
495 //if (!readOOAttr(a)) return false;
497 if (!a.value( "useOrientation").isEmpty() )
499 if (a.value ("useOrientation") =="true")
500 lastFloat->setUseOrientation (true);
502 lastFloat->setUseOrientation (false);
504 if (!a.value( "href").isEmpty() )
507 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
509 QMessageBox::warning( 0, "Warning: " ,
510 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
511 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
517 if (!a.value( "floatExport").isEmpty() )
519 // Only for compatibility. THis is not used since 1.7.11
520 if (a.value ("floatExport") =="true")
521 lastFloat->setFloatExport(true);
523 lastFloat->setFloatExport (false);
525 if (!a.value( "zPlane").isEmpty() )
526 lastFloat->setZValue (a.value("zPlane").toInt ());
529 if (!a.value( "relPosX").isEmpty() )
531 if (!a.value( "relPosY").isEmpty() )
534 x=a.value("relPosX").toFloat (&okx);
535 y=a.value("relPosY").toFloat (&oky);
539 lastFloat->setRelPos (QPointF (x,y) );
540 // make sure floats in mapcenter are repositioned to relative pos
541 if (mc==lastBranch) mc->positionContents();
544 // Couldn't read relPos
549 if (!readOOAttr(a)) return false;
551 if (!a.value ("orgName").isEmpty() )
553 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
558 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
563 XLinkObj *xlo=new XLinkObj (mc->getScene());
564 if (!a.value( "color").isEmpty() )
566 col.setNamedColor(a.value("color"));
570 if (!a.value( "width").isEmpty() )
572 xlo->setWidth(a.value ("width").toInt (&okx, 10));
575 // Connecting by select string for compatibility with version < 1.8.76
576 if (!a.value( "beginBranch").isEmpty() )
578 if (!a.value( "endBranch").isEmpty() )
580 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
581 if (lmo && typeid (*lmo)==typeid (BranchObj))
583 xlo->setBegin ((BranchObj*)lmo);
584 lmo=mc->findObjBySelect (a.value( "endBranch"));
585 if (lmo && typeid (*lmo)==typeid (BranchObj))
587 xlo->setEnd ((BranchObj*)(lmo));
595 // object ID is used starting in version 1.8.76
596 if (!a.value( "beginID").isEmpty() )
598 if (!a.value( "endID").isEmpty() )
600 LinkableMapObj *lmo=mc->findID (a.value( "beginID"));
601 if (lmo && typeid (*lmo)==typeid (BranchObj))
603 xlo->setBegin ((BranchObj*)lmo);
604 lmo=mc->findID (a.value( "endID"));
605 if (lmo && typeid (*lmo)==typeid (BranchObj))
607 xlo->setEnd ((BranchObj*)(lmo));
614 if (!success) delete (xlo);
615 return true; // xLinks can only be established at the "end branch", return true
618 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
620 for (int i=1; i<=a.count(); i++)
621 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
625 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
627 if (!a.value( "key").isEmpty() )
629 if (!a.value( "value").isEmpty() )
630 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));