11 #include "linkablemapobj.h"
14 static BranchObj *lastBranch;
15 static FloatObj *lastFloat;
16 static OrnamentedObj *lastOO;
18 extern Settings settings;
19 extern QString vymVersion;
22 parseVYMHandler::parseVYMHandler() {}
24 parseVYMHandler::~parseVYMHandler() {}
26 QString parseVYMHandler::errorProtocol() { return errorProt; }
30 bool parseVYMHandler::startDocument()
34 laststate = StateInit;
36 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 <"<< qPrintable(eName)
61 <<" laststate="<<stateStack.last()
62 <<" loadMode="<<loadMode
63 //<<" line="<<QXmlDefaultHandler::lineNumber()
66 stateStack.append (state);
67 if ( state == StateInit && (eName == "vymmap") )
72 model->clear(); // remove existing mapCenter
75 if (!atts.value( "version").isEmpty() )
77 if (!checkVersion(atts.value("version")))
78 QMessageBox::warning( 0, "Warning: Version Problem" ,
79 "<h3>Map is newer than VYM</h3>"
80 "<p>The map you are just trying to load was "
81 "saved using vym " +atts.value("version")+". "
82 "The version of this vym is " + vymVersion +
83 ". If you run into problems after pressing "
84 "the ok-button below, updating vym should help.");
86 model->setVersion(atts.value( "version" ));
90 if (loadMode==NewMap )
94 lastBranch=model->first(); // avoid empty pointer
96 if (!atts.value( "author").isEmpty() )
97 model->setAuthor(atts.value( "author" ) );
98 if (!atts.value( "comment").isEmpty() )
99 model->setComment (atts.value( "comment" ) );
100 if (!atts.value( "backgroundColor").isEmpty() )
102 col.setNamedColor(atts.value("backgroundColor"));
103 model->getScene()->setBackgroundBrush(col);
105 if (!atts.value( "selectionColor").isEmpty() )
107 col.setNamedColor(atts.value("selectionColor"));
108 model->getMapEditor()->setSelectionColor(col);
110 if (!atts.value( "linkColorHint").isEmpty() )
112 if (atts.value("linkColorHint")=="HeadingColor")
113 model->getMapEditor()->setMapLinkColorHint(LinkableMapObj::HeadingColor);
115 model->getMapEditor()->setMapLinkColorHint(LinkableMapObj::DefaultColor);
117 if (!atts.value( "linkStyle").isEmpty() )
118 model->getMapEditor()->setMapLinkStyle(atts.value("linkStyle"));
119 if (!atts.value( "linkColor").isEmpty() )
121 col.setNamedColor(atts.value("linkColor"));
122 model->getMapEditor()->setMapDefLinkColor(col);
124 if (!atts.value( "defXLinkColor").isEmpty() )
126 col.setNamedColor(atts.value("defXLinkColor"));
127 model->getMapEditor()->setMapDefXLinkColor(col);
129 if (!atts.value( "defXLinkWidth").isEmpty() )
130 model->getMapEditor()->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
132 } else if ( eName == "select" && state == StateMap )
134 state=StateMapSelect;
135 } else if ( eName == "setting" && state == StateMap )
137 state=StateMapSetting;
138 if (loadMode==NewMap)
139 readSettingAttr (atts);
140 } else if ( eName == "mapcenter" && state == StateMap )
142 state=StateMapCenter;
143 if (loadMode==NewMap)
145 // Really use the found mapcenter as MCO in a new map
147 lastBranch=model->addMapCenter();
150 // Treat the found mapcenter as a branch
151 // in an existing map
152 LinkableMapObj* lmo=model->getSelection();
155 if ( (typeid(*lmo) == typeid(BranchObj) )
156 || typeid(*lmo) == typeid(MapCenterObj) )
158 lastBranch=(BranchObj*)lmo;
159 if (loadMode==ImportAdd)
161 lastBranch->addBranch();
162 lastBranch=lastBranch->getLastBranch();
167 // Add mapCenter without parent
168 lastBranch=model->addMapCenter();
171 readBranchAttr (atts);
173 (eName == "standardflag" ||eName == "standardFlag") &&
174 (state == StateMapCenter || state==StateBranch))
176 state=StateStandardFlag;
177 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
181 if (!atts.value( "textColor").isEmpty() )
183 col.setNamedColor(atts.value("textColor"));
184 lastBranch->setColor(col );
186 } else if ( eName == "note" &&
187 (state == StateMapCenter ||state==StateBranch))
188 { // only for backward compatibility (<1.4.6). Use htmlnote now.
190 if (!readNoteAttr (atts) ) return false;
191 } else if ( eName == "htmlnote" && state == StateMapCenter)
195 } else if ( eName == "floatimage" &&
196 (state == StateMapCenter ||state==StateBranch))
198 state=StateFloatImage;
199 lastBranch->addFloatImage();
200 lastFloat=lastBranch->getLastFloatImage();
201 if (!readFloatImageAttr(atts)) return false;
202 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
204 // This is used in vymparts, which have no mapcenter!
206 LinkableMapObj* lmo=model->getSelection();
209 // If a vym part is _loaded_ (not imported),
210 // selection==lmo==NULL
211 // Treat it like ImportAdd then...
213 lmo=model->first(); // FIXME this used to be lmo=mc before
215 if (lmo && ((typeid(*lmo) == typeid(BranchObj) )
216 || typeid(*lmo) == typeid(MapCenterObj) ) )
218 lastBranch=(BranchObj*)(lmo);
222 if (loadMode==ImportAdd)
224 lastBranch->addBranch();
225 lastBranch=lastBranch->getLastBranch();
229 readBranchAttr (atts);
230 } else if (eName=="floatimage")
232 state=StateFloatImage;
233 lastBranch->addFloatImage();
234 lastFloat=lastBranch->getLastFloatImage();
235 if (!readFloatImageAttr(atts)) return false;
238 } else if ( eName == "branch" && state == StateMapCenter)
241 lastBranch->addBranch();
242 lastBranch=lastBranch->getLastBranch();
243 readBranchAttr (atts);
244 } else if ( eName == "htmlnote" && state == StateBranch)
249 if (!atts.value( "fonthint").isEmpty() )
250 no.setFontHint(atts.value ("fonthint") );
251 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
255 if (!readFrameAttr(atts)) return false;
256 } else if ( eName == "xlink" && state == StateBranch )
258 state=StateBranchXLink;
259 if (!readXLinkAttr (atts)) return false;
260 } else if ( eName == "branch" && state == StateBranch )
262 lastBranch->addBranch();
263 lastBranch=lastBranch->getLastBranch();
264 readBranchAttr (atts);
265 } else if ( eName == "html" && state == StateHtmlNote )
271 } else if ( state == StateHtml )
273 // accept all while in html mode,
278 return false; // Error
282 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
285 cout << "endElement </" <<qPrintable(eName)
287 <<" laststate=" <<laststate
288 <<" stateStack="<<stateStack.last()
294 // Empty branches may not be scrolled
295 // (happens if bookmarks are imported)
296 if (lastBranch->isScrolled() && lastBranch->countBranches()==0)
297 lastBranch->unScroll();
298 lastBranch=(BranchObj*)(lastBranch->getParObj());
301 htmldata+="</"+eName+">";
305 htmldata.replace ("<br></br>","<br />");
306 no.setNote (htmldata);
307 lastBranch->setNote (no);
313 state=stateStack.takeLast();
317 bool parseVYMHandler::characters ( const QString& ch)
319 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
321 QString ch_org=quotemeta (ch);
322 QString ch_simplified=ch.simplifyWhiteSpace();
323 if ( ch_simplified.isEmpty() ) return true;
327 case StateInit: break;
328 case StateMap: break;
330 model->select(ch_simplified);
332 case StateMapSetting:break;
333 case StateMapCenter: break;
335 lastBranch->setNote(ch_simplified);
337 case StateBranch: break;
338 case StateStandardFlag:
339 lastBranch->activateStandardFlag(ch_simplified);
341 case StateFloatImage: break;
342 case StateHtmlNote: break;
347 lastBranch->setHeading(ch_simplified);
355 QString parseVYMHandler::errorString()
357 return "the document is not in the VYM file format";
360 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
363 if (!readOOAttr(a)) return false;
365 if (!a.value( "scrolled").isEmpty() )
366 lastBranch->toggleScroll();
367 if (!a.value( "frameType").isEmpty() )
368 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
370 if (!a.value( "incImgV").isEmpty() )
372 if (a.value("incImgV")=="true")
373 lastBranch->setIncludeImagesVer(true);
375 lastBranch->setIncludeImagesVer(false);
377 if (!a.value( "incImgH").isEmpty() )
379 if (a.value("incImgH")=="true")
380 lastBranch->setIncludeImagesHor(true);
382 lastBranch->setIncludeImagesHor(false);
387 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
393 if (!a.value( "frameType").isEmpty() )
394 lastOO->setFrameType (a.value("frameType"));
395 if (!a.value( "penColor").isEmpty() )
396 lastOO->setFramePenColor (a.value("penColor"));
397 if (!a.value( "brushColor").isEmpty() )
398 lastOO->setFrameBrushColor (a.value("brushColor"));
399 if (!a.value( "padding").isEmpty() )
401 x=a.value("padding").toInt(&ok);
402 if (ok) lastOO->setFramePadding(x);
404 if (!a.value( "borderWidth").isEmpty() )
406 x=a.value("borderWidth").toInt(&ok);
407 if (ok) lastOO->setFrameBorderWidth(x);
413 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
419 if (!a.value( "relPosX").isEmpty() )
421 if (!a.value( "relPosY").isEmpty() )
423 x=a.value("relPosX").toFloat (&okx);
424 y=a.value("relPosY").toFloat (&oky);
427 lastOO->setUseRelPos (true);
428 lastOO->move2RelPos (x,y);
431 return false; // Couldn't read relPos
434 if (!a.value( "absPosX").isEmpty() )
436 if (!a.value( "absPosY").isEmpty() )
438 x=a.value("absPosX").toFloat (&okx);
439 y=a.value("absPosY").toFloat (&oky);
443 return false; // Couldn't read absPos
446 if (!a.value( "id").isEmpty() )
447 lastOO->setID (a.value ("id"));
448 if (!a.value( "url").isEmpty() )
449 lastOO->setURL (a.value ("url"));
450 if (!a.value( "vymLink").isEmpty() )
451 lastOO->setVymLink (a.value ("vymLink"));
452 if (!a.value( "hideInExport").isEmpty() )
453 if (a.value("hideInExport")=="true")
454 lastOO->setHideInExport(true);
456 if (!a.value( "hideLink").isEmpty())
458 if (a.value ("hideLink") =="true")
459 lastOO->setHideLinkUnselected(true);
461 lastOO->setHideLinkUnselected(false);
467 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
468 { // only for backward compatibility (<1.4.6). Use htmlnote now.
471 if (!a.value( "href").isEmpty() )
474 fn=parseHREF(a.value ("href") );
476 QString s; // Reading a note
478 if ( !file.open( QIODevice::ReadOnly) )
480 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
483 QTextStream stream( &file );
485 while ( !stream.atEnd() ) {
486 lines += stream.readLine()+"\n";
490 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
493 if (!a.value( "fonthint").isEmpty() )
494 no.setFontHint(a.value ("fonthint") );
495 lastBranch->setNote(no);
499 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
503 //if (!readOOAttr(a)) return false;
505 if (!a.value( "useOrientation").isEmpty() )
507 if (a.value ("useOrientation") =="true")
508 lastFloat->setUseOrientation (true);
510 lastFloat->setUseOrientation (false);
512 if (!a.value( "href").isEmpty() )
515 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
517 QMessageBox::warning( 0, "Warning: " ,
518 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
519 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
525 if (!a.value( "floatExport").isEmpty() )
527 // Only for compatibility. THis is not used since 1.7.11
528 if (a.value ("floatExport") =="true")
529 lastFloat->setFloatExport(true);
531 lastFloat->setFloatExport (false);
533 if (!a.value( "zPlane").isEmpty() )
534 lastFloat->setZValue (a.value("zPlane").toInt ());
537 if (!a.value( "relPosX").isEmpty() )
539 if (!a.value( "relPosY").isEmpty() )
542 x=a.value("relPosX").toFloat (&okx);
543 y=a.value("relPosY").toFloat (&oky);
547 lastFloat->setRelPos (QPointF (x,y) );
548 // make sure floats in mapcenter are repositioned to relative pos
549 if (lastBranch->getDepth()==0) lastBranch->positionContents();
552 // Couldn't read relPos
557 if (!readOOAttr(a)) return false;
559 if (!a.value ("orgName").isEmpty() )
561 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
566 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
571 XLinkObj *xlo=new XLinkObj (model->getScene());
572 if (!a.value( "color").isEmpty() )
574 col.setNamedColor(a.value("color"));
578 if (!a.value( "width").isEmpty() )
580 xlo->setWidth(a.value ("width").toInt (&okx, 10));
583 // Connecting by select string for compatibility with version < 1.8.76
584 if (!a.value( "beginBranch").isEmpty() )
586 if (!a.value( "endBranch").isEmpty() )
588 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
589 if (lmo && typeid (*lmo)==typeid (BranchObj))
591 xlo->setBegin ((BranchObj*)lmo);
592 lmo=model->findObjBySelect (a.value( "endBranch"));
593 if (lmo && typeid (*lmo)==typeid (BranchObj))
595 xlo->setEnd ((BranchObj*)(lmo));
603 // object ID is used starting in version 1.8.76
604 if (!a.value( "beginID").isEmpty() )
606 if (!a.value( "endID").isEmpty() )
608 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
609 if (lmo && typeid (*lmo)==typeid (BranchObj))
611 xlo->setBegin ((BranchObj*)lmo);
612 lmo=model->findID (a.value( "endID"));
613 if (lmo && typeid (*lmo)==typeid (BranchObj))
615 xlo->setEnd ((BranchObj*)(lmo));
622 if (!success) delete (xlo);
623 return true; // xLinks can only be established at the "end branch", return true
626 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
628 for (int i=1; i<=a.count(); i++)
629 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
633 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
635 if (!a.value( "key").isEmpty() )
637 if (!a.value( "value").isEmpty() )
638 settings.setLocalEntry (model->getMapEditor()->getDestPath(), a.value ("key"), a.value ("value"));