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 // Empty branches may not be scrolled
290 // (happens if bookmarks are imported)
291 if (lastBranch->isScrolled() && lastBranch->countBranches()==0)
292 lastBranch->unScroll();
293 lastBranch=(BranchObj*)(lastBranch->getParObj());
296 htmldata+="</"+eName+">";
300 htmldata.replace ("<br></br>","<br />");
301 no.setNote (htmldata);
302 lastBranch->setNote (no);
308 state=stateStack.takeLast();
312 bool parseVYMHandler::characters ( const QString& ch)
314 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
316 QString ch_org=quotemeta (ch);
317 QString ch_simplified=ch.simplifyWhiteSpace();
318 if ( ch_simplified.isEmpty() ) return true;
322 case StateInit: break;
323 case StateMap: break;
325 model->select(ch_simplified);
327 case StateMapSetting:break;
328 case StateMapCenter: break;
330 lastBranch->setNote(ch_simplified);
332 case StateBranch: break;
333 case StateStandardFlag:
334 lastBranch->activateStandardFlag(ch_simplified);
336 case StateFloatImage: break;
337 case StateHtmlNote: break;
342 lastBranch->setHeading(ch_simplified);
350 QString parseVYMHandler::errorString()
352 return "the document is not in the VYM file format";
355 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
358 if (!readOOAttr(a)) return false;
360 if (!a.value( "scrolled").isEmpty() )
361 lastBranch->toggleScroll();
362 if (!a.value( "frameType").isEmpty() )
363 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
365 if (!a.value( "incImgV").isEmpty() )
367 if (a.value("incImgV")=="true")
368 lastBranch->setIncludeImagesVer(true);
370 lastBranch->setIncludeImagesVer(false);
372 if (!a.value( "incImgH").isEmpty() )
374 if (a.value("incImgH")=="true")
375 lastBranch->setIncludeImagesHor(true);
377 lastBranch->setIncludeImagesHor(false);
382 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
388 if (!a.value( "frameType").isEmpty() )
389 lastOO->setFrameType (a.value("frameType"));
390 if (!a.value( "penColor").isEmpty() )
391 lastOO->setFramePenColor (a.value("penColor"));
392 if (!a.value( "brushColor").isEmpty() )
393 lastOO->setFrameBrushColor (a.value("brushColor"));
394 if (!a.value( "padding").isEmpty() )
396 x=a.value("padding").toInt(&ok);
397 if (ok) lastOO->setFramePadding(x);
399 if (!a.value( "borderWidth").isEmpty() )
401 x=a.value("borderWidth").toInt(&ok);
402 if (ok) lastOO->setFrameBorderWidth(x);
408 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
414 if (!a.value( "relPosX").isEmpty() )
416 if (!a.value( "relPosY").isEmpty() )
418 x=a.value("relPosX").toFloat (&okx);
419 y=a.value("relPosY").toFloat (&oky);
422 lastOO->setUseRelPos (true);
423 lastOO->move2RelPos (x,y);
426 return false; // Couldn't read relPos
429 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
431 if (!a.value( "absPosY").isEmpty() )
433 x=a.value("absPosX").toFloat (&okx);
434 y=a.value("absPosY").toFloat (&oky);
438 return false; // Couldn't read absPos
441 if (!a.value( "id").isEmpty() )
442 lastOO->setID (a.value ("id"));
443 if (!a.value( "url").isEmpty() )
444 lastOO->setURL (a.value ("url"));
445 if (!a.value( "vymLink").isEmpty() )
446 lastOO->setVymLink (a.value ("vymLink"));
447 if (!a.value( "hideInExport").isEmpty() )
448 if (a.value("hideInExport")=="true")
449 lastOO->setHideInExport(true);
451 if (!a.value( "hideLink").isEmpty())
453 if (a.value ("hideLink") =="true")
454 lastOO->setHideLinkUnselected(true);
456 lastOO->setHideLinkUnselected(false);
462 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
463 { // only for backward compatibility (<1.4.6). Use htmlnote now.
466 if (!a.value( "href").isEmpty() )
469 fn=parseHREF(a.value ("href") );
471 QString s; // Reading a note
473 if ( !file.open( QIODevice::ReadOnly) )
475 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
478 QTextStream stream( &file );
480 while ( !stream.atEnd() ) {
481 lines += stream.readLine()+"\n";
485 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
488 if (!a.value( "fonthint").isEmpty() )
489 no.setFontHint(a.value ("fonthint") );
490 lastBranch->setNote(no);
494 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
498 //if (!readOOAttr(a)) return false;
500 if (!a.value( "useOrientation").isEmpty() )
502 if (a.value ("useOrientation") =="true")
503 lastFloat->setUseOrientation (true);
505 lastFloat->setUseOrientation (false);
507 if (!a.value( "href").isEmpty() )
510 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
512 QMessageBox::warning( 0, "Warning: " ,
513 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
514 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
520 if (!a.value( "floatExport").isEmpty() )
522 // Only for compatibility. THis is not used since 1.7.11
523 if (a.value ("floatExport") =="true")
524 lastFloat->setFloatExport(true);
526 lastFloat->setFloatExport (false);
528 if (!a.value( "zPlane").isEmpty() )
529 lastFloat->setZValue (a.value("zPlane").toInt ());
532 if (!a.value( "relPosX").isEmpty() )
534 if (!a.value( "relPosY").isEmpty() )
537 x=a.value("relPosX").toFloat (&okx);
538 y=a.value("relPosY").toFloat (&oky);
542 lastFloat->setRelPos (QPointF (x,y) );
543 // make sure floats in mapcenter are repositioned to relative pos
544 if (lastBranch->getDepth()==0) lastBranch->positionContents();
547 // Couldn't read relPos
552 if (!readOOAttr(a)) return false;
554 if (!a.value ("orgName").isEmpty() )
556 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
561 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
566 XLinkObj *xlo=new XLinkObj (model->getScene());
567 if (!a.value( "color").isEmpty() )
569 col.setNamedColor(a.value("color"));
573 if (!a.value( "width").isEmpty() )
575 xlo->setWidth(a.value ("width").toInt (&okx, 10));
578 // Connecting by select string for compatibility with version < 1.8.76
579 if (!a.value( "beginBranch").isEmpty() )
581 if (!a.value( "endBranch").isEmpty() )
583 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
584 if (lmo && typeid (*lmo)==typeid (BranchObj))
586 xlo->setBegin ((BranchObj*)lmo);
587 lmo=model->findObjBySelect (a.value( "endBranch"));
588 if (lmo && typeid (*lmo)==typeid (BranchObj))
590 xlo->setEnd ((BranchObj*)(lmo));
598 // object ID is used starting in version 1.8.76
599 if (!a.value( "beginID").isEmpty() )
601 if (!a.value( "endID").isEmpty() )
603 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
604 if (lmo && typeid (*lmo)==typeid (BranchObj))
606 xlo->setBegin ((BranchObj*)lmo);
607 lmo=model->findID (a.value( "endID"));
608 if (lmo && typeid (*lmo)==typeid (BranchObj))
610 xlo->setEnd ((BranchObj*)(lmo));
617 if (!success) delete (xlo);
618 return true; // xLinks can only be established at the "end branch", return true
621 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
623 for (int i=1; i<=a.count(); i++)
624 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
628 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
630 if (!a.value( "key").isEmpty() )
632 if (!a.value( "value").isEmpty() )
633 settings.setLocalEntry (model->getMapEditor()->getDestPath(), a.value ("key"), a.value ("value"));