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 // FIXME not working for multiple mapCenters yet:
148 lastBranch=model->addMapCenter();
149 //lastBranch=model->first(); // avoid empty pointer
152 // Treat the found mapcenter as a branch
153 // in an existing map
154 LinkableMapObj* lmo=model->getSelection();
155 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
156 || (typeid(*lmo) == typeid(MapCenterObj) ) )
158 lastBranch=(BranchObj*)lmo;
159 if (loadMode==ImportAdd)
161 lastBranch->addBranch();
162 lastBranch=lastBranch->getLastBranch();
168 readBranchAttr (atts);
170 (eName == "standardflag" ||eName == "standardFlag") &&
171 (state == StateMapCenter || state==StateBranch))
173 state=StateStandardFlag;
174 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
178 if (!atts.value( "textColor").isEmpty() )
180 col.setNamedColor(atts.value("textColor"));
181 lastBranch->setColor(col );
183 } else if ( eName == "note" &&
184 (state == StateMapCenter ||state==StateBranch))
185 { // only for backward compatibility (<1.4.6). Use htmlnote now.
187 if (!readNoteAttr (atts) ) return false;
188 } else if ( eName == "htmlnote" && state == StateMapCenter)
192 } else if ( eName == "floatimage" &&
193 (state == StateMapCenter ||state==StateBranch))
195 state=StateFloatImage;
196 lastBranch->addFloatImage();
197 lastFloat=lastBranch->getLastFloatImage();
198 if (!readFloatImageAttr(atts)) return false;
199 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
201 // This is used in vymparts, which have no mapcenter!
203 LinkableMapObj* lmo=model->getSelection();
206 // If a vym part is _loaded_ (not imported),
207 // selection==lmo==NULL
208 // Treat it like ImportAdd then...
210 lmo=model->first(); // FIXME this used to be lmo=mc before
212 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
213 || (typeid(*lmo) == typeid(MapCenterObj) ) )
215 lastBranch=(BranchObj*)(lmo);
219 if (loadMode==ImportAdd)
221 lastBranch->addBranch();
222 lastBranch=lastBranch->getLastBranch();
226 readBranchAttr (atts);
227 } else if (eName=="floatimage")
229 state=StateFloatImage;
230 lastBranch->addFloatImage();
231 lastFloat=lastBranch->getLastFloatImage();
232 if (!readFloatImageAttr(atts)) return false;
235 } else if ( eName == "branch" && state == StateMapCenter)
238 lastBranch->addBranch();
239 lastBranch=lastBranch->getLastBranch();
240 readBranchAttr (atts);
241 } else if ( eName == "htmlnote" && state == StateBranch)
246 if (!atts.value( "fonthint").isEmpty() )
247 no.setFontHint(atts.value ("fonthint") );
248 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
252 if (!readFrameAttr(atts)) return false;
253 } else if ( eName == "xlink" && state == StateBranch )
255 state=StateBranchXLink;
256 if (!readXLinkAttr (atts)) return false;
257 } else if ( eName == "branch" && state == StateBranch )
259 lastBranch->addBranch();
260 lastBranch=lastBranch->getLastBranch();
261 readBranchAttr (atts);
262 } else if ( eName == "html" && state == StateHtmlNote )
268 } else if ( state == StateHtml )
270 // accept all while in html mode,
275 return false; // Error
279 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
282 cout << "endElement </" <<qPrintable(eName)
284 <<" laststate=" <<laststate
285 <<" stateStack="<<stateStack.last()
291 lastBranch=(BranchObj*)(lastBranch->getParObj());
294 htmldata+="</"+eName+">";
298 htmldata.replace ("<br></br>","<br />");
299 no.setNote (htmldata);
300 lastBranch->setNote (no);
306 state=stateStack.takeLast();
310 bool parseVYMHandler::characters ( const QString& ch)
312 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
314 QString ch_org=quotemeta (ch);
315 QString ch_simplified=ch.simplifyWhiteSpace();
316 if ( ch_simplified.isEmpty() ) return true;
320 case StateInit: break;
321 case StateMap: break;
323 model->select(ch_simplified);
325 case StateMapSetting:break;
326 case StateMapCenter: break;
328 lastBranch->setNote(ch_simplified);
330 case StateBranch: break;
331 case StateStandardFlag:
332 lastBranch->activateStandardFlag(ch_simplified);
334 case StateFloatImage: break;
335 case StateHtmlNote: break;
340 lastBranch->setHeading(ch_simplified);
348 QString parseVYMHandler::errorString()
350 return "the document is not in the VYM file format";
353 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
356 if (!readOOAttr(a)) return false;
358 if (!a.value( "scrolled").isEmpty() )
359 lastBranch->toggleScroll();
360 if (!a.value( "frameType").isEmpty() )
361 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
363 if (!a.value( "incImgV").isEmpty() )
365 if (a.value("incImgV")=="true")
366 lastBranch->setIncludeImagesVer(true);
368 lastBranch->setIncludeImagesVer(false);
370 if (!a.value( "incImgH").isEmpty() )
372 if (a.value("incImgH")=="true")
373 lastBranch->setIncludeImagesHor(true);
375 lastBranch->setIncludeImagesHor(false);
380 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
386 if (!a.value( "frameType").isEmpty() )
387 lastOO->setFrameType (a.value("frameType"));
388 if (!a.value( "penColor").isEmpty() )
389 lastOO->setFramePenColor (a.value("penColor"));
390 if (!a.value( "brushColor").isEmpty() )
391 lastOO->setFrameBrushColor (a.value("brushColor"));
392 if (!a.value( "padding").isEmpty() )
394 x=a.value("padding").toInt(&ok);
395 if (ok) lastOO->setFramePadding(x);
397 if (!a.value( "borderWidth").isEmpty() )
399 x=a.value("borderWidth").toInt(&ok);
400 if (ok) lastOO->setFrameBorderWidth(x);
406 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
412 if (!a.value( "relPosX").isEmpty() )
414 if (!a.value( "relPosY").isEmpty() )
416 x=a.value("relPosX").toFloat (&okx);
417 y=a.value("relPosY").toFloat (&oky);
420 lastOO->setUseRelPos (true);
421 lastOO->move2RelPos (x,y);
424 return false; // Couldn't read relPos
427 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
429 if (!a.value( "absPosY").isEmpty() )
431 x=a.value("absPosX").toFloat (&okx);
432 y=a.value("absPosY").toFloat (&oky);
436 return false; // Couldn't read absPos
439 if (!a.value( "id").isEmpty() )
440 lastOO->setID (a.value ("id"));
441 if (!a.value( "url").isEmpty() )
442 lastOO->setURL (a.value ("url"));
443 if (!a.value( "vymLink").isEmpty() )
444 lastOO->setVymLink (a.value ("vymLink"));
445 if (!a.value( "hideInExport").isEmpty() )
446 if (a.value("hideInExport")=="true")
447 lastOO->setHideInExport(true);
449 if (!a.value( "hideLink").isEmpty())
451 if (a.value ("hideLink") =="true")
452 lastOO->setHideLinkUnselected(true);
454 lastOO->setHideLinkUnselected(false);
460 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
461 { // only for backward compatibility (<1.4.6). Use htmlnote now.
464 if (!a.value( "href").isEmpty() )
467 fn=parseHREF(a.value ("href") );
469 QString s; // Reading a note
471 if ( !file.open( QIODevice::ReadOnly) )
473 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
476 QTextStream stream( &file );
478 while ( !stream.atEnd() ) {
479 lines += stream.readLine()+"\n";
483 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
486 if (!a.value( "fonthint").isEmpty() )
487 no.setFontHint(a.value ("fonthint") );
488 lastBranch->setNote(no);
492 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
496 //if (!readOOAttr(a)) return false;
498 if (!a.value( "useOrientation").isEmpty() )
500 if (a.value ("useOrientation") =="true")
501 lastFloat->setUseOrientation (true);
503 lastFloat->setUseOrientation (false);
505 if (!a.value( "href").isEmpty() )
508 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
510 QMessageBox::warning( 0, "Warning: " ,
511 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
512 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
518 if (!a.value( "floatExport").isEmpty() )
520 // Only for compatibility. THis is not used since 1.7.11
521 if (a.value ("floatExport") =="true")
522 lastFloat->setFloatExport(true);
524 lastFloat->setFloatExport (false);
526 if (!a.value( "zPlane").isEmpty() )
527 lastFloat->setZValue (a.value("zPlane").toInt ());
530 if (!a.value( "relPosX").isEmpty() )
532 if (!a.value( "relPosY").isEmpty() )
535 x=a.value("relPosX").toFloat (&okx);
536 y=a.value("relPosY").toFloat (&oky);
540 lastFloat->setRelPos (QPointF (x,y) );
541 // make sure floats in mapcenter are repositioned to relative pos
542 if (lastBranch->getDepth()==0) lastBranch->positionContents();
545 // Couldn't read relPos
550 if (!readOOAttr(a)) return false;
552 if (!a.value ("orgName").isEmpty() )
554 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
559 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
564 XLinkObj *xlo=new XLinkObj (model->getScene());
565 if (!a.value( "color").isEmpty() )
567 col.setNamedColor(a.value("color"));
571 if (!a.value( "width").isEmpty() )
573 xlo->setWidth(a.value ("width").toInt (&okx, 10));
576 // Connecting by select string for compatibility with version < 1.8.76
577 if (!a.value( "beginBranch").isEmpty() )
579 if (!a.value( "endBranch").isEmpty() )
581 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
582 if (lmo && typeid (*lmo)==typeid (BranchObj))
584 xlo->setBegin ((BranchObj*)lmo);
585 lmo=model->findObjBySelect (a.value( "endBranch"));
586 if (lmo && typeid (*lmo)==typeid (BranchObj))
588 xlo->setEnd ((BranchObj*)(lmo));
596 // object ID is used starting in version 1.8.76
597 if (!a.value( "beginID").isEmpty() )
599 if (!a.value( "endID").isEmpty() )
601 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
602 if (lmo && typeid (*lmo)==typeid (BranchObj))
604 xlo->setBegin ((BranchObj*)lmo);
605 lmo=model->findID (a.value( "endID"));
606 if (lmo && typeid (*lmo)==typeid (BranchObj))
608 xlo->setEnd ((BranchObj*)(lmo));
615 if (!success) delete (xlo);
616 return true; // xLinks can only be established at the "end branch", return true
619 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
621 for (int i=1; i<=a.count(); i++)
622 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
626 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
628 if (!a.value( "key").isEmpty() )
630 if (!a.value( "value").isEmpty() )
631 settings.setLocalEntry (model->getMapEditor()->getDestPath(), a.value ("key"), a.value ("value"));