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();
153 if (lmo && ((typeid(*lmo) == typeid(BranchObj) )
154 || typeid(*lmo) == typeid(MapCenterObj) ) )
156 lastBranch=(BranchObj*)lmo;
157 if (loadMode==ImportAdd)
159 lastBranch->addBranch();
160 lastBranch=lastBranch->getLastBranch();
166 readBranchAttr (atts);
168 (eName == "standardflag" ||eName == "standardFlag") &&
169 (state == StateMapCenter || state==StateBranch))
171 state=StateStandardFlag;
172 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
176 if (!atts.value( "textColor").isEmpty() )
178 col.setNamedColor(atts.value("textColor"));
179 lastBranch->setColor(col );
181 } else if ( eName == "note" &&
182 (state == StateMapCenter ||state==StateBranch))
183 { // only for backward compatibility (<1.4.6). Use htmlnote now.
185 if (!readNoteAttr (atts) ) return false;
186 } else if ( eName == "htmlnote" && state == StateMapCenter)
190 } else if ( eName == "floatimage" &&
191 (state == StateMapCenter ||state==StateBranch))
193 state=StateFloatImage;
194 lastBranch->addFloatImage();
195 lastFloat=lastBranch->getLastFloatImage();
196 if (!readFloatImageAttr(atts)) return false;
197 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
199 // This is used in vymparts, which have no mapcenter!
201 LinkableMapObj* lmo=model->getSelection();
204 // If a vym part is _loaded_ (not imported),
205 // selection==lmo==NULL
206 // Treat it like ImportAdd then...
208 lmo=model->first(); // FIXME this used to be lmo=mc before
210 if (lmo && ((typeid(*lmo) == typeid(BranchObj) )
211 || typeid(*lmo) == typeid(MapCenterObj) ) )
213 lastBranch=(BranchObj*)(lmo);
217 if (loadMode==ImportAdd)
219 lastBranch->addBranch();
220 lastBranch=lastBranch->getLastBranch();
224 readBranchAttr (atts);
225 } else if (eName=="floatimage")
227 state=StateFloatImage;
228 lastBranch->addFloatImage();
229 lastFloat=lastBranch->getLastFloatImage();
230 if (!readFloatImageAttr(atts)) return false;
233 } 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();
259 readBranchAttr (atts);
260 } else if ( eName == "html" && state == StateHtmlNote )
266 } else if ( state == StateHtml )
268 // accept all while in html mode,
273 return false; // Error
277 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
280 cout << "endElement </" <<qPrintable(eName)
282 <<" laststate=" <<laststate
283 <<" stateStack="<<stateStack.last()
289 lastBranch=(BranchObj*)(lastBranch->getParObj());
292 htmldata+="</"+eName+">";
296 htmldata.replace ("<br></br>","<br />");
297 no.setNote (htmldata);
298 lastBranch->setNote (no);
304 state=stateStack.takeLast();
308 bool parseVYMHandler::characters ( const QString& ch)
310 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
312 QString ch_org=quotemeta (ch);
313 QString ch_simplified=ch.simplifyWhiteSpace();
314 if ( ch_simplified.isEmpty() ) return true;
318 case StateInit: break;
319 case StateMap: break;
321 model->select(ch_simplified);
323 case StateMapSetting:break;
324 case StateMapCenter: break;
326 lastBranch->setNote(ch_simplified);
328 case StateBranch: break;
329 case StateStandardFlag:
330 lastBranch->activateStandardFlag(ch_simplified);
332 case StateFloatImage: break;
333 case StateHtmlNote: break;
338 lastBranch->setHeading(ch_simplified);
346 QString parseVYMHandler::errorString()
348 return "the document is not in the VYM file format";
351 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
354 if (!readOOAttr(a)) return false;
356 if (!a.value( "scrolled").isEmpty() )
357 lastBranch->toggleScroll();
358 if (!a.value( "frameType").isEmpty() )
359 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
361 if (!a.value( "incImgV").isEmpty() )
363 if (a.value("incImgV")=="true")
364 lastBranch->setIncludeImagesVer(true);
366 lastBranch->setIncludeImagesVer(false);
368 if (!a.value( "incImgH").isEmpty() )
370 if (a.value("incImgH")=="true")
371 lastBranch->setIncludeImagesHor(true);
373 lastBranch->setIncludeImagesHor(false);
378 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
384 if (!a.value( "frameType").isEmpty() )
385 lastOO->setFrameType (a.value("frameType"));
386 if (!a.value( "penColor").isEmpty() )
387 lastOO->setFramePenColor (a.value("penColor"));
388 if (!a.value( "brushColor").isEmpty() )
389 lastOO->setFrameBrushColor (a.value("brushColor"));
390 if (!a.value( "padding").isEmpty() )
392 x=a.value("padding").toInt(&ok);
393 if (ok) lastOO->setFramePadding(x);
395 if (!a.value( "borderWidth").isEmpty() )
397 x=a.value("borderWidth").toInt(&ok);
398 if (ok) lastOO->setFrameBorderWidth(x);
404 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
410 if (!a.value( "relPosX").isEmpty() )
412 if (!a.value( "relPosY").isEmpty() )
414 x=a.value("relPosX").toFloat (&okx);
415 y=a.value("relPosY").toFloat (&oky);
418 lastOO->setUseRelPos (true);
419 lastOO->move2RelPos (x,y);
422 return false; // Couldn't read relPos
425 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
427 if (!a.value( "absPosY").isEmpty() )
429 x=a.value("absPosX").toFloat (&okx);
430 y=a.value("absPosY").toFloat (&oky);
434 return false; // Couldn't read absPos
437 if (!a.value( "id").isEmpty() )
438 lastOO->setID (a.value ("id"));
439 if (!a.value( "url").isEmpty() )
440 lastOO->setURL (a.value ("url"));
441 if (!a.value( "vymLink").isEmpty() )
442 lastOO->setVymLink (a.value ("vymLink"));
443 if (!a.value( "hideInExport").isEmpty() )
444 if (a.value("hideInExport")=="true")
445 lastOO->setHideInExport(true);
447 if (!a.value( "hideLink").isEmpty())
449 if (a.value ("hideLink") =="true")
450 lastOO->setHideLinkUnselected(true);
452 lastOO->setHideLinkUnselected(false);
458 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
459 { // only for backward compatibility (<1.4.6). Use htmlnote now.
462 if (!a.value( "href").isEmpty() )
465 fn=parseHREF(a.value ("href") );
467 QString s; // Reading a note
469 if ( !file.open( QIODevice::ReadOnly) )
471 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
474 QTextStream stream( &file );
476 while ( !stream.atEnd() ) {
477 lines += stream.readLine()+"\n";
481 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
484 if (!a.value( "fonthint").isEmpty() )
485 no.setFontHint(a.value ("fonthint") );
486 lastBranch->setNote(no);
490 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
494 //if (!readOOAttr(a)) return false;
496 if (!a.value( "useOrientation").isEmpty() )
498 if (a.value ("useOrientation") =="true")
499 lastFloat->setUseOrientation (true);
501 lastFloat->setUseOrientation (false);
503 if (!a.value( "href").isEmpty() )
506 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
508 QMessageBox::warning( 0, "Warning: " ,
509 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
510 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
516 if (!a.value( "floatExport").isEmpty() )
518 // Only for compatibility. THis is not used since 1.7.11
519 if (a.value ("floatExport") =="true")
520 lastFloat->setFloatExport(true);
522 lastFloat->setFloatExport (false);
524 if (!a.value( "zPlane").isEmpty() )
525 lastFloat->setZValue (a.value("zPlane").toInt ());
528 if (!a.value( "relPosX").isEmpty() )
530 if (!a.value( "relPosY").isEmpty() )
533 x=a.value("relPosX").toFloat (&okx);
534 y=a.value("relPosY").toFloat (&oky);
538 lastFloat->setRelPos (QPointF (x,y) );
539 // make sure floats in mapcenter are repositioned to relative pos
540 if (lastBranch->getDepth()==0) lastBranch->positionContents();
543 // Couldn't read relPos
548 if (!readOOAttr(a)) return false;
550 if (!a.value ("orgName").isEmpty() )
552 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
557 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
562 XLinkObj *xlo=new XLinkObj (model->getScene());
563 if (!a.value( "color").isEmpty() )
565 col.setNamedColor(a.value("color"));
569 if (!a.value( "width").isEmpty() )
571 xlo->setWidth(a.value ("width").toInt (&okx, 10));
574 // Connecting by select string for compatibility with version < 1.8.76
575 if (!a.value( "beginBranch").isEmpty() )
577 if (!a.value( "endBranch").isEmpty() )
579 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
580 if (lmo && typeid (*lmo)==typeid (BranchObj))
582 xlo->setBegin ((BranchObj*)lmo);
583 lmo=model->findObjBySelect (a.value( "endBranch"));
584 if (lmo && typeid (*lmo)==typeid (BranchObj))
586 xlo->setEnd ((BranchObj*)(lmo));
594 // object ID is used starting in version 1.8.76
595 if (!a.value( "beginID").isEmpty() )
597 if (!a.value( "endID").isEmpty() )
599 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
600 if (lmo && typeid (*lmo)==typeid (BranchObj))
602 xlo->setBegin ((BranchObj*)lmo);
603 lmo=model->findID (a.value( "endID"));
604 if (lmo && typeid (*lmo)==typeid (BranchObj))
606 xlo->setEnd ((BranchObj*)(lmo));
613 if (!success) delete (xlo);
614 return true; // xLinks can only be established at the "end branch", return true
617 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
619 for (int i=1; i<=a.count(); i++)
620 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
624 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
626 if (!a.value( "key").isEmpty() )
628 if (!a.value( "value").isEmpty() )
629 settings.setLocalEntry (model->getMapEditor()->getDestPath(), a.value ("key"), a.value ("value"));