11 #include "linkablemapobj.h"
14 static BranchObj *lastBranch;
15 static FloatObj *lastFloat;
16 static OrnamentedObj *lastOO;
18 extern Settings settings;
19 extern QString vymVersion;
21 bool parseVYMHandler::startDocument()
25 laststate = StateInit;
27 stateStack.append(StateInit);
33 bool parseVYMHandler::startElement ( const QString&, const QString&,
34 const QString& eName, const QXmlAttributes& atts )
38 cout << "startElement <"<< qPrintable(eName)
40 <<" laststate="<<stateStack.last()
41 <<" loadMode="<<loadMode
42 <<" line="<<QXmlDefaultHandler::lineNumber()
45 stateStack.append (state);
46 if ( state == StateInit && (eName == "vymmap") )
50 if (loadMode==NewMap )
54 lastBranch=model->first(); // avoid empty pointer
56 if (!atts.value( "author").isEmpty() )
57 model->setAuthor(atts.value( "author" ) );
58 if (!atts.value( "comment").isEmpty() )
59 model->setComment (atts.value( "comment" ) );
60 if (!atts.value( "backgroundColor").isEmpty() )
62 col.setNamedColor(atts.value("backgroundColor"));
63 model->getScene()->setBackgroundBrush(col);
65 if (!atts.value( "selectionColor").isEmpty() )
67 col.setNamedColor(atts.value("selectionColor"));
68 model->setSelectionColor(col);
70 if (!atts.value( "linkColorHint").isEmpty() )
72 if (atts.value("linkColorHint")=="HeadingColor")
73 model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
75 model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
77 if (!atts.value( "linkStyle").isEmpty() )
78 model->setMapLinkStyle(atts.value("linkStyle"));
79 if (!atts.value( "linkColor").isEmpty() )
81 col.setNamedColor(atts.value("linkColor"));
82 model->setMapDefLinkColor(col);
84 if (!atts.value( "defXLinkColor").isEmpty() )
86 col.setNamedColor(atts.value("defXLinkColor"));
87 model->setMapDefXLinkColor(col);
89 if (!atts.value( "defXLinkWidth").isEmpty() )
90 model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
93 if (!atts.value( "version").isEmpty() )
95 if (!checkVersion(atts.value("version")))
96 QMessageBox::warning( 0, "Warning: Version Problem" ,
97 "<h3>Map is newer than VYM</h3>"
98 "<p>The map you are just trying to load was "
99 "saved using vym " +atts.value("version")+". "
100 "The version of this vym is " + vymVersion +
101 ". If you run into problems after pressing "
102 "the ok-button below, updating vym should help.");
104 model->setVersion(atts.value( "version" ));
108 } else if ( eName == "select" && state == StateMap )
110 state=StateMapSelect;
111 } else if ( eName == "setting" && state == StateMap )
113 state=StateMapSetting;
114 if (loadMode==NewMap)
115 readSettingAttr (atts);
116 } else if ( eName == "mapcenter" && state == StateMap )
118 state=StateMapCenter;
119 if (loadMode==NewMap)
121 // Really use the found mapcenter as MCO in a new map
123 model->createMapCenter();
124 lastBranch=model->getSelectedBranch();
127 // Treat the found mapcenter as a branch
128 // in an existing map
129 LinkableMapObj* lmo=model->getSelection();
130 if (lmo && ( (typeid(*lmo) == typeid(BranchObj) )
131 || (typeid(*lmo) == typeid(MapCenterObj) ) ) )
133 lastBranch=(BranchObj*)lmo;
134 if (loadMode==ImportAdd)
136 model->createBranch();
137 lastBranch=model->getSelectedBranch();
143 readBranchAttr (atts);
145 (eName == "standardflag" ||eName == "standardFlag") &&
146 (state == StateMapCenter || state==StateBranch))
148 state=StateStandardFlag;
149 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
153 if (!atts.value( "textColor").isEmpty() )
155 col.setNamedColor(atts.value("textColor"));
156 lastBranch->setColor(col );
158 } else if ( eName == "note" &&
159 (state == StateMapCenter ||state==StateBranch))
160 { // only for backward compatibility (<1.4.6). Use htmlnote now.
162 if (!readNoteAttr (atts) ) return false;
163 } else if ( eName == "htmlnote" && state == StateMapCenter)
167 } else if ( eName == "floatimage" &&
168 (state == StateMapCenter ||state==StateBranch))
170 state=StateFloatImage;
171 lastBranch->addFloatImage();
172 lastFloat=lastBranch->getLastFloatImage();
173 if (!readFloatImageAttr(atts)) return false;
174 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
176 // This is used in vymparts, which have no mapcenter!
178 LinkableMapObj* lmo=model->getSelection();
181 // If a vym part is _loaded_ (not imported),
182 // selection==lmo==NULL
183 // Treat it like ImportAdd then...
187 if (lmo && ( (typeid(*lmo) == typeid(BranchObj) )
188 || (typeid(*lmo) == typeid(MapCenterObj) ) ) )
190 lastBranch=(BranchObj*)(lmo);
194 if (loadMode==ImportAdd)
196 model->createBranch();
197 lastBranch=model->getSelectedBranch();
201 readBranchAttr (atts);
202 } else if (eName=="floatimage")
204 state=StateFloatImage;
205 lastBranch->addFloatImage();
206 lastFloat=lastBranch->getLastFloatImage();
207 if (!readFloatImageAttr(atts)) return false;
210 } else if ( eName == "branch" && state == StateMapCenter)
213 model->createBranch();
214 lastBranch=model->getSelectedBranch();
215 readBranchAttr (atts);
216 } else if ( eName == "htmlnote" && state == StateBranch)
221 if (!atts.value( "fonthint").isEmpty() )
222 no.setFontHint(atts.value ("fonthint") );
223 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
227 if (!readFrameAttr(atts)) return false;
228 } else if ( eName == "xlink" && state == StateBranch )
230 state=StateBranchXLink;
231 if (!readXLinkAttr (atts)) return false;
232 } else if ( eName == "branch" && state == StateBranch )
234 model->createBranch();
235 lastBranch=model->getSelectedBranch();
236 readBranchAttr (atts);
237 } else if ( eName == "html" && state == StateHtmlNote )
243 } else if ( state == StateHtml )
245 // accept all while in html mode,
250 return false; // Error
254 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
257 cout << "endElement </" <<qPrintable(eName)
259 <<" laststate=" <<laststate
260 <<" stateStack="<<stateStack.last()
266 model->selectParent();
267 lastBranch=model->getSelectedBranch();
270 htmldata+="</"+eName+">";
274 htmldata.replace ("<br></br>","<br />");
275 no.setNote (htmldata);
276 lastBranch->setNote (no);
282 state=stateStack.takeLast();
286 bool parseVYMHandler::characters ( const QString& ch)
288 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
290 QString ch_org=quotemeta (ch);
291 QString ch_simplified=ch.simplifyWhiteSpace();
292 if ( ch_simplified.isEmpty() ) return true;
296 case StateInit: break;
297 case StateMap: break;
299 model->select(ch_simplified);
301 case StateMapSetting:break;
302 case StateMapCenter: break;
304 lastBranch->setNote(ch_simplified);
306 case StateBranch: break;
307 case StateStandardFlag:
308 lastBranch->activateStandardFlag(ch_simplified);
310 case StateFloatImage: break;
311 case StateHtmlNote: break;
316 model->setHeading(ch_simplified);
324 QString parseVYMHandler::errorString()
326 return "the document is not in the VYM file format";
329 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
332 if (!readOOAttr(a)) return false;
334 if (!a.value( "scrolled").isEmpty() )
335 lastBranch->toggleScroll();
336 if (!a.value( "frameType").isEmpty() )
337 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
339 if (!a.value( "incImgV").isEmpty() )
341 if (a.value("incImgV")=="true")
342 lastBranch->setIncludeImagesVer(true);
344 lastBranch->setIncludeImagesVer(false);
346 if (!a.value( "incImgH").isEmpty() )
348 if (a.value("incImgH")=="true")
349 lastBranch->setIncludeImagesHor(true);
351 lastBranch->setIncludeImagesHor(false);
356 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
362 if (!a.value( "frameType").isEmpty() )
363 lastOO->setFrameType (a.value("frameType"));
364 if (!a.value( "penColor").isEmpty() )
365 lastOO->setFramePenColor (a.value("penColor"));
366 if (!a.value( "brushColor").isEmpty() )
367 lastOO->setFrameBrushColor (a.value("brushColor"));
368 if (!a.value( "padding").isEmpty() )
370 x=a.value("padding").toInt(&ok);
371 if (ok) lastOO->setFramePadding(x);
373 if (!a.value( "borderWidth").isEmpty() )
375 x=a.value("borderWidth").toInt(&ok);
376 if (ok) lastOO->setFrameBorderWidth(x);
382 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
388 if (!a.value( "relPosX").isEmpty() )
390 if (!a.value( "relPosY").isEmpty() )
392 x=a.value("relPosX").toFloat (&okx);
393 y=a.value("relPosY").toFloat (&oky);
396 lastOO->setUseRelPos (true);
397 lastOO->move2RelPos (x,y);
400 return false; // Couldn't read relPos
403 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
405 if (!a.value( "absPosY").isEmpty() )
407 x=a.value("absPosX").toFloat (&okx);
408 y=a.value("absPosY").toFloat (&oky);
412 return false; // Couldn't read absPos
415 if (!a.value( "id").isEmpty() )
416 lastOO->setID (a.value ("id"));
417 if (!a.value( "url").isEmpty() )
418 lastOO->setURL (a.value ("url"));
419 if (!a.value( "vymLink").isEmpty() )
420 lastOO->setVymLink (a.value ("vymLink"));
421 if (!a.value( "hideInExport").isEmpty() )
422 if (a.value("hideInExport")=="true")
423 lastOO->setHideInExport(true);
425 if (!a.value( "hideLink").isEmpty())
427 if (a.value ("hideLink") =="true")
428 lastOO->setHideLinkUnselected(true);
430 lastOO->setHideLinkUnselected(false);
436 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
437 { // only for backward compatibility (<1.4.6). Use htmlnote now.
440 if (!a.value( "href").isEmpty() )
443 fn=parseHREF(a.value ("href") );
445 QString s; // Reading a note
447 if ( !file.open( QIODevice::ReadOnly) )
449 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
452 QTextStream stream( &file );
454 while ( !stream.atEnd() ) {
455 lines += stream.readLine()+"\n";
459 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
462 if (!a.value( "fonthint").isEmpty() )
463 no.setFontHint(a.value ("fonthint") );
464 lastBranch->setNote(no);
468 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
472 //if (!readOOAttr(a)) return false;
474 if (!a.value( "useOrientation").isEmpty() )
476 if (a.value ("useOrientation") =="true")
477 lastFloat->setUseOrientation (true);
479 lastFloat->setUseOrientation (false);
481 if (!a.value( "href").isEmpty() )
484 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
486 QMessageBox::warning( 0, "Warning: " ,
487 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
488 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
494 if (!a.value( "floatExport").isEmpty() )
496 // Only for compatibility. THis is not used since 1.7.11
497 if (a.value ("floatExport") =="true")
498 lastFloat->setFloatExport(true);
500 lastFloat->setFloatExport (false);
502 if (!a.value( "zPlane").isEmpty() )
503 lastFloat->setZValue (a.value("zPlane").toInt ());
506 if (!a.value( "relPosX").isEmpty() )
508 if (!a.value( "relPosY").isEmpty() )
511 x=a.value("relPosX").toFloat (&okx);
512 y=a.value("relPosY").toFloat (&oky);
516 lastFloat->setRelPos (QPointF (x,y) );
517 // make sure floats in mapcenter are repositioned to relative pos
518 if (lastBranch->getDepth()==0) lastBranch->positionContents();
521 // Couldn't read relPos
526 if (!readOOAttr(a)) return false;
528 if (!a.value ("orgName").isEmpty() )
530 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
535 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
540 XLinkObj *xlo=new XLinkObj (model->getScene());
541 if (!a.value( "color").isEmpty() )
543 col.setNamedColor(a.value("color"));
547 if (!a.value( "width").isEmpty() )
549 xlo->setWidth(a.value ("width").toInt (&okx, 10));
552 // Connecting by select string for compatibility with version < 1.8.76
553 if (!a.value( "beginBranch").isEmpty() )
555 if (!a.value( "endBranch").isEmpty() )
557 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
558 if (lmo && typeid (*lmo)==typeid (BranchObj))
560 xlo->setBegin ((BranchObj*)lmo);
561 lmo=model->findObjBySelect (a.value( "endBranch"));
562 if (lmo && typeid (*lmo)==typeid (BranchObj))
564 xlo->setEnd ((BranchObj*)(lmo));
572 // object ID is used starting in version 1.8.76
573 if (!a.value( "beginID").isEmpty() )
575 if (!a.value( "endID").isEmpty() )
577 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
578 if (lmo && typeid (*lmo)==typeid (BranchObj))
580 xlo->setBegin ((BranchObj*)lmo);
581 lmo=model->findID (a.value( "endID"));
582 if (lmo && typeid (*lmo)==typeid (BranchObj))
584 xlo->setEnd ((BranchObj*)(lmo));
591 if (!success) delete (xlo);
592 return true; // xLinks can only be established at the "end branch", return true
595 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
597 for (int i=1; i<=a.count(); i++)
598 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
602 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
604 if (!a.value( "key").isEmpty() )
606 if (!a.value( "value").isEmpty() )
607 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));