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 <"<< eName.ascii()
61 <<" laststate="<<stateStack.last()
62 <<" loadMode="<<loadMode
63 <<" line="<<QXmlDefaultHandler::lineNumber()
66 stateStack.append (state);
67 if ( state == StateInit && (eName == "vymmap") )
72 if (!atts.value( "version").isEmpty() )
74 if (!checkVersion(atts.value("version")))
75 QMessageBox::warning( 0, "Warning: Version Problem" ,
76 "<h3>Map is newer than VYM</h3>"
77 "<p>The map you are just trying to load was "
78 "saved using vym " +atts.value("version")+". "
79 "The version of this vym is " + vymVersion +
80 ". If you run into problems after pressing "
81 "the ok-button below, updating vym should help.");
83 model->setVersion(atts.value( "version" ));
87 if (loadMode==NewMap )
91 lastBranch=model->first(); // avoid empty pointer
93 if (!atts.value( "author").isEmpty() )
94 model->setAuthor(atts.value( "author" ) );
95 if (!atts.value( "comment").isEmpty() )
96 model->setComment (atts.value( "comment" ) );
97 if (!atts.value( "backgroundColor").isEmpty() )
99 col.setNamedColor(atts.value("backgroundColor"));
100 model->getScene()->setBackgroundBrush(col);
102 if (!atts.value( "selectionColor").isEmpty() )
104 col.setNamedColor(atts.value("selectionColor"));
105 model->getMapEditor()->setSelectionColor(col);
107 if (!atts.value( "linkColorHint").isEmpty() )
109 if (atts.value("linkColorHint")=="HeadingColor")
110 model->getMapEditor()->setMapLinkColorHint(LinkableMapObj::HeadingColor);
112 model->getMapEditor()->setMapLinkColorHint(LinkableMapObj::DefaultColor);
114 if (!atts.value( "linkStyle").isEmpty() )
115 model->getMapEditor()->setMapLinkStyle(atts.value("linkStyle"));
116 if (!atts.value( "linkColor").isEmpty() )
118 col.setNamedColor(atts.value("linkColor"));
119 model->getMapEditor()->setMapDefLinkColor(col);
121 if (!atts.value( "defXLinkColor").isEmpty() )
123 col.setNamedColor(atts.value("defXLinkColor"));
124 model->getMapEditor()->setMapDefXLinkColor(col);
126 if (!atts.value( "defXLinkWidth").isEmpty() )
127 model->getMapEditor()->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
129 } else if ( eName == "select" && state == StateMap )
131 state=StateMapSelect;
132 } else if ( eName == "setting" && state == StateMap )
134 state=StateMapSetting;
135 if (loadMode==NewMap)
136 readSettingAttr (atts);
137 } else if ( eName == "mapcenter" && state == StateMap )
139 state=StateMapCenter;
140 if (loadMode==NewMap)
142 // Really use the found mapcenter as MCO in a new map
144 // FIXME not working for multiple mapCenters yet:
145 lastBranch=model->addMapCenter();
146 //lastBranch=model->first(); // avoid empty pointer
149 // Treat the found mapcenter as a branch
150 // in an existing map
151 LinkableMapObj* lmo=model->getSelection();
152 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
153 || (typeid(*lmo) == typeid(MapCenterObj) ) )
155 lastBranch=(BranchObj*)lmo;
156 if (loadMode==ImportAdd)
158 lastBranch->addBranch();
159 lastBranch=lastBranch->getLastBranch();
165 readBranchAttr (atts);
167 (eName == "standardflag" ||eName == "standardFlag") &&
168 (state == StateMapCenter || state==StateBranch))
170 state=StateStandardFlag;
171 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
175 if (!atts.value( "textColor").isEmpty() )
177 col.setNamedColor(atts.value("textColor"));
178 lastBranch->setColor(col );
180 } else if ( eName == "note" &&
181 (state == StateMapCenter ||state==StateBranch))
182 { // only for backward compatibility (<1.4.6). Use htmlnote now.
184 if (!readNoteAttr (atts) ) return false;
185 } else if ( eName == "htmlnote" && state == StateMapCenter)
189 } else if ( eName == "floatimage" &&
190 (state == StateMapCenter ||state==StateBranch))
192 state=StateFloatImage;
193 lastBranch->addFloatImage();
194 lastFloat=lastBranch->getLastFloatImage();
195 if (!readFloatImageAttr(atts)) return false;
196 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
198 // This is used in vymparts, which have no mapcenter!
200 LinkableMapObj* lmo=model->getSelection();
203 // If a vym part is _loaded_ (not imported),
204 // selection==lmo==NULL
205 // Treat it like ImportAdd then...
207 lmo=model->first(); // FIXME this used to be lmo=mc before
209 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
210 || (typeid(*lmo) == typeid(MapCenterObj) ) )
212 lastBranch=(BranchObj*)(lmo);
216 if (loadMode==ImportAdd)
218 lastBranch->addBranch();
219 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)
235 lastBranch->addBranch();
236 lastBranch=lastBranch->getLastBranch();
237 readBranchAttr (atts);
238 } else if ( eName == "htmlnote" && state == StateBranch)
243 if (!atts.value( "fonthint").isEmpty() )
244 no.setFontHint(atts.value ("fonthint") );
245 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
249 if (!readFrameAttr(atts)) return false;
250 } else if ( eName == "xlink" && state == StateBranch )
252 state=StateBranchXLink;
253 if (!readXLinkAttr (atts)) return false;
254 } else if ( eName == "branch" && state == StateBranch )
256 lastBranch->addBranch();
257 lastBranch=lastBranch->getLastBranch();
258 readBranchAttr (atts);
259 } else if ( eName == "html" && state == StateHtmlNote )
265 } else if ( state == StateHtml )
267 // accept all while in html mode,
272 return false; // Error
276 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
279 cout << "endElement </" <<eName.ascii()
281 <<" laststate=" <<laststate
282 <<" stateStack="<<stateStack.last()
288 lastBranch=(BranchObj*)(lastBranch->getParObj());
291 htmldata+="</"+eName+">";
295 htmldata.replace ("<br></br>","<br />");
296 no.setNote (htmldata);
297 lastBranch->setNote (no);
303 state=stateStack.takeLast();
307 bool parseVYMHandler::characters ( const QString& ch)
309 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
311 QString ch_org=quotemeta (ch);
312 QString ch_simplified=ch.simplifyWhiteSpace();
313 if ( ch_simplified.isEmpty() ) return true;
317 case StateInit: break;
318 case StateMap: break;
320 model->select(ch_simplified);
322 case StateMapSetting:break;
323 case StateMapCenter: break;
325 lastBranch->setNote(ch_simplified);
327 case StateBranch: break;
328 case StateStandardFlag:
329 lastBranch->activateStandardFlag(ch_simplified);
331 case StateFloatImage: break;
332 case StateHtmlNote: break;
337 lastBranch->setHeading(ch_simplified);
345 QString parseVYMHandler::errorString()
347 return "the document is not in the VYM file format";
350 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
353 if (!readOOAttr(a)) return false;
355 if (!a.value( "scrolled").isEmpty() )
356 lastBranch->toggleScroll();
357 if (!a.value( "frameType").isEmpty() )
358 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
360 if (!a.value( "incImgV").isEmpty() )
362 if (a.value("incImgV")=="true")
363 lastBranch->setIncludeImagesVer(true);
365 lastBranch->setIncludeImagesVer(false);
367 if (!a.value( "incImgH").isEmpty() )
369 if (a.value("incImgH")=="true")
370 lastBranch->setIncludeImagesHor(true);
372 lastBranch->setIncludeImagesHor(false);
377 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
383 if (!a.value( "frameType").isEmpty() )
384 lastOO->setFrameType (a.value("frameType"));
385 if (!a.value( "penColor").isEmpty() )
386 lastOO->setFramePenColor (a.value("penColor"));
387 if (!a.value( "brushColor").isEmpty() )
388 lastOO->setFrameBrushColor (a.value("brushColor"));
389 if (!a.value( "padding").isEmpty() )
391 x=a.value("padding").toInt(&ok);
392 if (ok) lastOO->setFramePadding(x);
394 if (!a.value( "borderWidth").isEmpty() )
396 x=a.value("borderWidth").toInt(&ok);
397 if (ok) lastOO->setFrameBorderWidth(x);
403 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
409 if (!a.value( "relPosX").isEmpty() )
411 if (!a.value( "relPosY").isEmpty() )
413 x=a.value("relPosX").toFloat (&okx);
414 y=a.value("relPosY").toFloat (&oky);
417 lastOO->setUseRelPos (true);
418 lastOO->move2RelPos (x,y);
421 return false; // Couldn't read relPos
424 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
426 if (!a.value( "absPosY").isEmpty() )
428 x=a.value("absPosX").toFloat (&okx);
429 y=a.value("absPosY").toFloat (&oky);
433 return false; // Couldn't read absPos
436 if (!a.value( "id").isEmpty() )
437 lastOO->setID (a.value ("id"));
438 if (!a.value( "url").isEmpty() )
439 lastOO->setURL (a.value ("url"));
440 if (!a.value( "vymLink").isEmpty() )
441 lastOO->setVymLink (a.value ("vymLink"));
442 if (!a.value( "hideInExport").isEmpty() )
443 if (a.value("hideInExport")=="true")
444 lastOO->setHideInExport(true);
446 if (!a.value( "hideLink").isEmpty())
448 if (a.value ("hideLink") =="true")
449 lastOO->setHideLinkUnselected(true);
451 lastOO->setHideLinkUnselected(false);
457 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
458 { // only for backward compatibility (<1.4.6). Use htmlnote now.
461 if (!a.value( "href").isEmpty() )
464 fn=parseHREF(a.value ("href") );
466 QString s; // Reading a note
468 if ( !file.open( QIODevice::ReadOnly) )
470 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
473 QTextStream stream( &file );
475 while ( !stream.atEnd() ) {
476 lines += stream.readLine()+"\n";
480 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
483 if (!a.value( "fonthint").isEmpty() )
484 no.setFontHint(a.value ("fonthint") );
485 lastBranch->setNote(no);
489 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
493 //if (!readOOAttr(a)) return false;
495 if (!a.value( "useOrientation").isEmpty() )
497 if (a.value ("useOrientation") =="true")
498 lastFloat->setUseOrientation (true);
500 lastFloat->setUseOrientation (false);
502 if (!a.value( "href").isEmpty() )
505 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
507 QMessageBox::warning( 0, "Warning: " ,
508 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
509 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
515 if (!a.value( "floatExport").isEmpty() )
517 // Only for compatibility. THis is not used since 1.7.11
518 if (a.value ("floatExport") =="true")
519 lastFloat->setFloatExport(true);
521 lastFloat->setFloatExport (false);
523 if (!a.value( "zPlane").isEmpty() )
524 lastFloat->setZValue (a.value("zPlane").toInt ());
527 if (!a.value( "relPosX").isEmpty() )
529 if (!a.value( "relPosY").isEmpty() )
532 x=a.value("relPosX").toFloat (&okx);
533 y=a.value("relPosY").toFloat (&oky);
537 lastFloat->setRelPos (QPointF (x,y) );
538 // make sure floats in mapcenter are repositioned to relative pos
539 if (lastBranch->getDepth()==0) lastBranch->positionContents();
542 // Couldn't read relPos
547 if (!readOOAttr(a)) return false;
549 if (!a.value ("orgName").isEmpty() )
551 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
556 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
561 XLinkObj *xlo=new XLinkObj (model->getScene());
562 if (!a.value( "color").isEmpty() )
564 col.setNamedColor(a.value("color"));
568 if (!a.value( "width").isEmpty() )
570 xlo->setWidth(a.value ("width").toInt (&okx, 10));
573 // Connecting by select string for compatibility with version < 1.8.76
574 if (!a.value( "beginBranch").isEmpty() )
576 if (!a.value( "endBranch").isEmpty() )
578 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
579 if (lmo && typeid (*lmo)==typeid (BranchObj))
581 xlo->setBegin ((BranchObj*)lmo);
582 lmo=model->findObjBySelect (a.value( "endBranch"));
583 if (lmo && typeid (*lmo)==typeid (BranchObj))
585 xlo->setEnd ((BranchObj*)(lmo));
593 // object ID is used starting in version 1.8.76
594 if (!a.value( "beginID").isEmpty() )
596 if (!a.value( "endID").isEmpty() )
598 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
599 if (lmo && typeid (*lmo)==typeid (BranchObj))
601 xlo->setBegin ((BranchObj*)lmo);
602 lmo=model->findID (a.value( "endID"));
603 if (lmo && typeid (*lmo)==typeid (BranchObj))
605 xlo->setEnd ((BranchObj*)(lmo));
612 if (!success) delete (xlo);
613 return true; // xLinks can only be established at the "end branch", return true
616 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
618 for (int i=1; i<=a.count(); i++)
619 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
623 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
625 if (!a.value( "key").isEmpty() )
627 if (!a.value( "value").isEmpty() )
628 settings.setLocalEntry (model->getMapEditor()->getDestPath(), a.value ("key"), a.value ("value"));