9 #include "branchitem.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
15 #include "xlinkitem.h"
18 extern Main *mainWindow;
19 extern Settings settings;
20 extern QString vymVersion;
22 bool parseVYMHandler::startDocument()
26 laststate = StateInit;
28 stateStack.append(StateInit);
35 bool parseVYMHandler::startElement ( const QString&, const QString&,
36 const QString& eName, const QXmlAttributes& atts )
40 cout << "startElement <"<< qPrintable(eName)
42 <<" laststate="<<stateStack.last()
43 <<" loadMode="<<loadMode
44 // <<" line="<<QXmlDefaultHandler::lineNumber()
47 stateStack.append (state);
48 if ( state == StateInit && (eName == "vymmap") )
51 branchesTotal=0; //FIXME-3 what if we load a .vyp ?
54 if (loadMode==NewMap )
60 if (!atts.value( "author").isEmpty() )
61 model->setAuthor(atts.value( "author" ) );
62 if (!atts.value( "comment").isEmpty() )
63 model->setComment (atts.value( "comment" ) );
64 if (!atts.value( "branchCount").isEmpty() )
66 branchesTotal=atts.value("branchCount").toInt();
70 mainWindow->setProgressMaximum (branchesTotal);
74 if (!atts.value( "backgroundColor").isEmpty() )
76 col.setNamedColor(atts.value("backgroundColor"));
77 model->getScene()->setBackgroundBrush(col);
79 if (!atts.value( "selectionColor").isEmpty() )
81 col.setNamedColor(atts.value("selectionColor"));
82 model->setSelectionColor(col);
84 if (!atts.value( "linkColorHint").isEmpty() )
86 if (atts.value("linkColorHint")=="HeadingColor")
87 model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
89 model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
91 if (!atts.value( "linkStyle").isEmpty() )
92 model->setMapLinkStyle(atts.value("linkStyle"));
93 if (!atts.value( "linkColor").isEmpty() )
95 col.setNamedColor(atts.value("linkColor"));
96 model->setMapDefLinkColor(col);
98 if (!atts.value( "defXLinkColor").isEmpty() )
100 col.setNamedColor(atts.value("defXLinkColor"));
101 model->setMapDefXLinkColor(col);
103 if (!atts.value( "defXLinkWidth").isEmpty() )
104 model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
105 if (!atts.value( "mapZoomFactor").isEmpty() )
106 model->setMapZoomFactor(atts.value("mapZoomFactor").toDouble());
109 if (!atts.value( "version").isEmpty() )
111 if (!checkVersion(atts.value("version")))
112 QMessageBox::warning( 0, "Warning: Version Problem" ,
113 "<h3>Map is newer than VYM</h3>"
114 "<p>The map you are just trying to load was "
115 "saved using vym " +atts.value("version")+". "
116 "The version of this vym is " + vymVersion +
117 ". If you run into problems after pressing "
118 "the ok-button below, updating vym should help.");
120 model->setVersion(atts.value( "version" ));
124 } else if ( eName == "select" && state == StateMap )
126 state=StateMapSelect;
127 } else if ( eName == "setting" && state == StateMap )
129 state=StateMapSetting;
130 if (loadMode==NewMap)
131 readSettingAttr (atts);
132 } else if ( eName == "mapcenter" && state == StateMap )
134 state=StateMapCenter;
135 if (loadMode==NewMap)
137 // Really use the found mapcenter as MCO in a new map
138 lastBranch=model->createMapCenter();
141 // Treat the found mapcenter as a branch
142 // in an existing map
143 BranchItem *bi=model->getSelectedBranch();
147 if (loadMode==ImportAdd)
150 lastBranch=model->createBranch(lastBranch);
154 // Parser should not be called with ImportReplace any longer,
155 // that's done in VymModel now.
156 qDebug()<<"xml-vym: ImportReplace ?!";
159 // add mapCenter without parent
160 lastBranch=model->createMapCenter();
162 readBranchAttr (atts);
164 (eName == "standardflag" ||eName == "standardFlag") &&
165 (state == StateMapCenter || state==StateBranch))
167 state=StateStandardFlag;
168 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
172 if (!atts.value( "textColor").isEmpty() )
174 col.setNamedColor(atts.value("textColor"));
175 lastBranch->setHeadingColor(col );
177 } else if ( eName == "note" &&
178 (state == StateMapCenter ||state==StateBranch))
179 { // only for backward compatibility (<1.4.6). Use htmlnote now.
181 if (!readNoteAttr (atts) ) return false;
182 } else if ( eName == "htmlnote" && state == StateMapCenter)
186 } else if ( eName == "floatimage" &&
187 (state == StateMapCenter ||state==StateBranch))
190 lastImage=model->createImage(lastBranch);
191 if (!readImageAttr(atts)) return false;
192 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
194 // This is used in vymparts, which have no mapcenter or for undo
196 TreeItem *ti=model->getSelectedItem();
199 // If a vym part is _loaded_ (not imported),
200 // selection==lmo==NULL
201 // Treat it like ImportAdd then...
203 // FIXME-3 lmo=model->first()->getLMO();
204 // Do we really have no MCO when loading?????
205 cout << "xml-vym aborted\n";
208 if (ti && ti->isBranchLikeType() )
210 lastBranch=(BranchItem*)ti;
214 if (loadMode==ImportAdd)
216 lastBranch=model->createBranch(lastBranch);
218 model->relinkBranch (lastBranch,(BranchItem*)ti,insertPos);
220 model->clearItem (lastBranch);
221 readBranchAttr (atts);
222 } else if (eName=="floatimage")
225 lastImage=model->createImage (lastBranch);
226 if (!readImageAttr(atts)) return false;
229 } else if ( eName == "branch" && state == StateMapCenter)
232 lastBranch=model->createBranch(lastBranch);
233 readBranchAttr (atts);
234 } else if ( eName == "htmlnote" && state == StateBranch)
239 if (!atts.value( "fonthint").isEmpty() )
240 no.setFontHint(atts.value ("fonthint") );
241 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
245 if (!readFrameAttr(atts)) return false;
246 } else if ( eName == "xlink" && state == StateBranch )
248 state=StateBranchXLink;
249 if (!readXLinkAttr (atts)) return false;
250 } else if ( eName == "xlink" && state == StateMap)
253 if (!readLinkNewAttr (atts)) return false;
254 } else if ( eName == "branch" && state == StateBranch )
256 lastBranch=model->createBranch(lastBranch);
257 readBranchAttr (atts);
258 } else if ( eName == "html" && state == StateHtmlNote )
264 } else if ( state == StateHtml )
266 // accept all while in html mode,
271 return false; // Error
275 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
278 cout << "endElement </" <<qPrintable(eName)
280 // <<" laststate=" <<laststate
281 // <<" stateStack="<<stateStack.last()
282 // <<" selString="<<model->getSelectString().toStdString()
288 mainWindow->removeProgressCounter();
291 model->emitDataHasChanged (lastBranch);
292 lastBranch=(BranchItem*)(lastBranch->parent());
293 // lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
296 // Empty branches may not be scrolled
297 // (happens if bookmarks are imported)
298 if (lastBranch->isScrolled() && lastBranch->branchCount()==0)
299 lastBranch->unScroll();
300 model->emitDataHasChanged (lastBranch);
302 lastBranch=(BranchItem*)(lastBranch->parent());
303 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
306 htmldata+="</"+eName+">";
310 htmldata.replace ("<br></br>","<br />");
311 no.setNote (htmldata);
312 lastBranch->setNoteObj (no);
318 state=stateStack.takeLast();
322 bool parseVYMHandler::characters ( const QString& ch)
324 //cout << "characters \""<<ch.toStdString()<<"\" state="<<state <<" laststate="<<laststate<<endl;
326 QString ch_org=quotemeta (ch);
327 QString ch_simplified=ch.simplifyWhiteSpace();
328 if ( ch_simplified.isEmpty() ) return true;
332 case StateInit: break;
333 case StateMap: break;
335 model->select(ch_simplified);
337 case StateMapSetting:break;
338 case StateMapCenter: break;
340 lastBranch->setNote(ch_simplified);
342 case StateBranch: break;
343 case StateStandardFlag:
344 lastBranch->activateStandardFlag(ch_simplified);
346 case StateImage: break;
347 case StateHtmlNote: break;
352 lastBranch->setHeading(ch_simplified);
360 QString parseVYMHandler::errorString()
362 return "the document is not in the VYM file format";
365 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
369 mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);
373 if (!readOOAttr(a)) return false;
375 if (!a.value( "scrolled").isEmpty() )
376 lastBranch->toggleScroll();
377 // (interesting for import of KDE bookmarks)
379 /* if (!a.value( "frameType").isEmpty() ) FIXME-3
380 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
383 if (!a.value( "incImgV").isEmpty() )
385 if (a.value("incImgV")=="true")
386 lastBranch->setIncludeImagesVer(true);
388 lastBranch->setIncludeImagesVer(false);
390 if (!a.value( "incImgH").isEmpty() )
392 if (a.value("incImgH")=="true")
393 lastBranch->setIncludeImagesHor(true);
395 lastBranch->setIncludeImagesHor(false);
400 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
404 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
410 if (!a.value( "frameType").isEmpty() )
411 oo->setFrameType (a.value("frameType"));
412 if (!a.value( "penColor").isEmpty() )
413 oo->setFramePenColor (a.value("penColor"));
414 if (!a.value( "brushColor").isEmpty() )
415 oo->setFrameBrushColor (a.value("brushColor"));
416 if (!a.value( "padding").isEmpty() )
418 x=a.value("padding").toInt(&ok);
419 if (ok) oo->setFramePadding(x);
421 if (!a.value( "borderWidth").isEmpty() )
423 x=a.value("borderWidth").toInt(&ok);
424 if (ok) oo->setFrameBorderWidth(x);
433 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
439 if (!a.value( "relPosX").isEmpty() )
441 if (!a.value( "relPosY").isEmpty() )
443 x=a.value("relPosX").toFloat (&okx);
444 y=a.value("relPosY").toFloat (&oky);
446 lastMI->setRelPos (QPointF(x,y));
448 return false; // Couldn't read relPos
451 if (!a.value( "absPosX").isEmpty() )
453 if (!a.value( "absPosY").isEmpty() )
455 x=a.value("absPosX").toFloat (&okx);
456 y=a.value("absPosY").toFloat (&oky);
458 lastMI->setAbsPos (QPointF(x,y));
460 return false; // Couldn't read absPos
463 if (!a.value( "url").isEmpty() )
464 lastMI->setURL (a.value ("url"));
465 if (!a.value( "vymLink").isEmpty() )
466 lastMI->setVymLink (a.value ("vymLink"));
467 if (!a.value( "hideInExport").isEmpty() )
468 if (a.value("hideInExport")=="true")
469 lastMI->setHideInExport(true);
471 if (!a.value( "hideLink").isEmpty())
473 if (a.value ("hideLink") =="true")
474 lastMI->setHideLinkUnselected(true);
476 lastMI->setHideLinkUnselected(false);
482 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
483 { // only for backward compatibility (<1.4.6). Use htmlnote now.
486 if (!a.value( "href").isEmpty() )
489 fn=parseHREF(a.value ("href") );
491 QString s; // Reading a note
493 if ( !file.open( QIODevice::ReadOnly) )
495 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
498 QTextStream stream( &file );
500 while ( !stream.atEnd() ) {
501 lines += stream.readLine()+"\n";
505 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
508 if (!a.value( "fonthint").isEmpty() )
509 no.setFontHint(a.value ("fonthint") );
510 lastBranch->setNoteObj(no);
514 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
518 if (!readOOAttr(a)) return false;
520 if (!a.value( "href").isEmpty() )
523 if (!lastImage->load (parseHREF(a.value ("href") ) ))
525 QMessageBox::warning( 0, "Warning: " ,
526 "Couldn't load image\n"+parseHREF(a.value ("href") ));
532 if (!a.value( "zPlane").isEmpty() )
533 lastImage->setZValue (a.value("zPlane").toInt ());
536 if (!a.value( "relPosX").isEmpty() )
538 if (!a.value( "relPosY").isEmpty() )
541 x=a.value("relPosX").toFloat (&okx);
542 y=a.value("relPosY").toFloat (&oky);
544 lastImage->setRelPos (QPointF (x,y) );
546 // Couldn't read relPos
551 //FIXME-3 if (!readOOAttr(a)) return false;
553 if (!a.value ("originalName").isEmpty() )
555 lastImage->setOriginalFilename (a.value("originalName"));
560 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
562 // Format of links was changed several times:
564 // object ID is used starting in version 1.8.76
565 // (before there was beginBranch and endBranch)
567 // Starting in 1.13.2 xlinks are no longer subitems of branches,
568 // but listed at the end of the data in a map. This make handling
569 // of links much safer and easier
571 if (!a.value( "beginID").isEmpty() )
573 if (!a.value( "endID").isEmpty() )
575 TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
576 TreeItem *endBI=model->findBySelectString (a.value( "endID"));
577 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
579 Link *li=new Link (model);
580 li->setBeginBranch ( (BranchItem*)beginBI );
581 li->setEndBranch ( (BranchItem*)endBI);
583 if (!a.value( "color").isEmpty() )
586 col.setNamedColor(a.value("color"));
590 if (!a.value( "width").isEmpty() )
593 li->setWidth(a.value ("width").toInt (&okx, 10));
595 model->createLink (li,true); // create MO by default
602 bool parseVYMHandler::readLinkNewAttr (const QXmlAttributes& a)
604 // object ID is used starting in version 1.8.76
605 // (before there was beginBranch and endBranch)
607 // Beginning in 1.13.2 xLinks are no longer parts of branches, but
608 // a separate list after all the mapCenters within <vymmap> ... </vymmap>
610 if (!a.value( "beginID").isEmpty() )
612 if (!a.value( "endID").isEmpty() )
614 TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
615 TreeItem *endBI=model->findBySelectString (a.value( "endID"));
616 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
618 Link *li=new Link (model);
619 li->setBeginBranch ( (BranchItem*)beginBI );
620 li->setEndBranch ( (BranchItem*)endBI);
622 if (!a.value( "color").isEmpty() )
625 col.setNamedColor(a.value("color"));
629 if (!a.value( "width").isEmpty() )
632 li->setWidth(a.value ("width").toInt (&okx, 10));
634 model->createLink (li,true); // create MO by default
641 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
643 for (int i=1; i<=a.count(); i++)
644 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
648 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
650 if (!a.value( "key").isEmpty() )
652 if (!a.value( "value").isEmpty() )
653 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));