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)
149 lastBranch=model->createBranch(lastBranch);
151 model->clearItem(lastBranch);
153 // add mapCenter without parent
154 lastBranch=model->createMapCenter();
156 readBranchAttr (atts);
158 (eName == "standardflag" ||eName == "standardFlag") &&
159 (state == StateMapCenter || state==StateBranch))
161 state=StateStandardFlag;
162 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
166 if (!atts.value( "textColor").isEmpty() )
168 col.setNamedColor(atts.value("textColor"));
169 lastBranch->setHeadingColor(col );
171 } else if ( eName == "note" &&
172 (state == StateMapCenter ||state==StateBranch))
173 { // only for backward compatibility (<1.4.6). Use htmlnote now.
175 if (!readNoteAttr (atts) ) return false;
176 } else if ( eName == "htmlnote" && state == StateMapCenter)
180 } else if ( eName == "floatimage" &&
181 (state == StateMapCenter ||state==StateBranch))
184 lastImage=model->createImage(lastBranch);
185 if (!readImageAttr(atts)) return false;
186 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
188 // This is used in vymparts, which have no mapcenter or for undo
190 TreeItem *ti=model->getSelectedItem();
193 // If a vym part is _loaded_ (not imported),
194 // selection==lmo==NULL
195 // Treat it like ImportAdd then...
197 // FIXME-3 lmo=model->first()->getLMO();
198 // Do we really have no MCO when loading?????
199 cout << "xml-vym aborted\n";
202 if (ti && ti->isBranchLikeType() )
204 lastBranch=(BranchItem*)ti;
208 if (loadMode==ImportAdd)
210 lastBranch=model->createBranch(lastBranch);
212 model->clearItem (lastBranch);
213 readBranchAttr (atts);
214 } else if (eName=="floatimage")
217 lastImage=model->createImage (lastBranch);
218 if (!readImageAttr(atts)) return false;
221 } else if ( eName == "branch" && state == StateMapCenter)
224 lastBranch=model->createBranch(lastBranch);
225 readBranchAttr (atts);
226 } else if ( eName == "htmlnote" && state == StateBranch)
231 if (!atts.value( "fonthint").isEmpty() )
232 no.setFontHint(atts.value ("fonthint") );
233 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
237 if (!readFrameAttr(atts)) return false;
238 } else if ( eName == "xlink" && state == StateBranch )
240 state=StateBranchXLink;
241 if (!readXLinkAttr (atts)) return false;
242 } else if ( eName == "branch" && state == StateBranch )
244 lastBranch=model->createBranch(lastBranch);
245 readBranchAttr (atts);
246 } else if ( eName == "html" && state == StateHtmlNote )
252 } else if ( state == StateHtml )
254 // accept all while in html mode,
259 return false; // Error
263 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
266 cout << "endElement </" <<qPrintable(eName)
268 // <<" laststate=" <<laststate
269 // <<" stateStack="<<stateStack.last()
270 // <<" selString="<<model->getSelectString().toStdString()
276 mainWindow->removeProgressCounter();
279 model->emitDataHasChanged (lastBranch);
280 lastBranch=(BranchItem*)(lastBranch->parent());
281 // lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
284 // Empty branches may not be scrolled
285 // (happens if bookmarks are imported)
286 if (lastBranch->isScrolled() && lastBranch->branchCount()==0)
287 lastBranch->unScroll();
288 model->emitDataHasChanged (lastBranch);
290 lastBranch=(BranchItem*)(lastBranch->parent());
291 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
294 htmldata+="</"+eName+">";
298 htmldata.replace ("<br></br>","<br />");
299 no.setNote (htmldata);
300 lastBranch->setNoteObj (no);
306 state=stateStack.takeLast();
310 bool parseVYMHandler::characters ( const QString& ch)
312 //cout << "characters \""<<ch.toStdString()<<"\" 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 StateImage: 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)
357 mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);
361 if (!readOOAttr(a)) return false;
363 if (!a.value( "scrolled").isEmpty() )
364 lastBranch->toggleScroll();
365 // (interesting for import of KDE bookmarks)
367 /* if (!a.value( "frameType").isEmpty() ) FIXME-3
368 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
371 if (!a.value( "incImgV").isEmpty() )
373 if (a.value("incImgV")=="true")
374 lastBranch->setIncludeImagesVer(true);
376 lastBranch->setIncludeImagesVer(false);
378 if (!a.value( "incImgH").isEmpty() )
380 if (a.value("incImgH")=="true")
381 lastBranch->setIncludeImagesHor(true);
383 lastBranch->setIncludeImagesHor(false);
388 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
392 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
398 if (!a.value( "frameType").isEmpty() )
399 oo->setFrameType (a.value("frameType"));
400 if (!a.value( "penColor").isEmpty() )
401 oo->setFramePenColor (a.value("penColor"));
402 if (!a.value( "brushColor").isEmpty() )
403 oo->setFrameBrushColor (a.value("brushColor"));
404 if (!a.value( "padding").isEmpty() )
406 x=a.value("padding").toInt(&ok);
407 if (ok) oo->setFramePadding(x);
409 if (!a.value( "borderWidth").isEmpty() )
411 x=a.value("borderWidth").toInt(&ok);
412 if (ok) oo->setFrameBorderWidth(x);
421 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
427 if (!a.value( "relPosX").isEmpty() )
429 if (!a.value( "relPosY").isEmpty() )
431 x=a.value("relPosX").toFloat (&okx);
432 y=a.value("relPosY").toFloat (&oky);
434 lastMI->setRelPos (QPointF(x,y));
436 return false; // Couldn't read relPos
439 if (!a.value( "absPosX").isEmpty() )
441 if (!a.value( "absPosY").isEmpty() )
443 x=a.value("absPosX").toFloat (&okx);
444 y=a.value("absPosY").toFloat (&oky);
446 lastMI->setAbsPos (QPointF(x,y));
448 return false; // Couldn't read absPos
451 if (!a.value( "url").isEmpty() )
452 lastMI->setURL (a.value ("url"));
453 if (!a.value( "vymLink").isEmpty() )
454 lastMI->setVymLink (a.value ("vymLink"));
455 if (!a.value( "hideInExport").isEmpty() )
456 if (a.value("hideInExport")=="true")
457 lastMI->setHideInExport(true);
459 if (!a.value( "hideLink").isEmpty())
461 if (a.value ("hideLink") =="true")
462 lastMI->setHideLinkUnselected(true);
464 lastMI->setHideLinkUnselected(false);
470 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
471 { // only for backward compatibility (<1.4.6). Use htmlnote now.
474 if (!a.value( "href").isEmpty() )
477 fn=parseHREF(a.value ("href") );
479 QString s; // Reading a note
481 if ( !file.open( QIODevice::ReadOnly) )
483 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
486 QTextStream stream( &file );
488 while ( !stream.atEnd() ) {
489 lines += stream.readLine()+"\n";
493 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
496 if (!a.value( "fonthint").isEmpty() )
497 no.setFontHint(a.value ("fonthint") );
498 lastBranch->setNoteObj(no);
502 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
506 if (!readOOAttr(a)) return false;
508 if (!a.value( "href").isEmpty() )
511 if (!lastImage->load (parseHREF(a.value ("href") ) ))
513 QMessageBox::warning( 0, "Warning: " ,
514 "Couldn't load image\n"+parseHREF(a.value ("href") ));
520 if (!a.value( "zPlane").isEmpty() )
521 lastImage->setZValue (a.value("zPlane").toInt ());
524 if (!a.value( "relPosX").isEmpty() )
526 if (!a.value( "relPosY").isEmpty() )
529 x=a.value("relPosX").toFloat (&okx);
530 y=a.value("relPosY").toFloat (&oky);
532 lastImage->setRelPos (QPointF (x,y) );
534 // Couldn't read relPos
539 //FIXME-3 if (!readOOAttr(a)) return false;
541 if (!a.value ("originalName").isEmpty() )
543 lastImage->setOriginalFilename (a.value("originalName"));
548 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
550 // object ID is used starting in version 1.8.76
551 // (before there was beginBranch and endBranch)
552 if (!a.value( "beginID").isEmpty() )
554 if (!a.value( "endID").isEmpty() )
556 TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
557 TreeItem *endBI=model->findBySelectString (a.value( "endID"));
558 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
560 XLinkItem *xli=model->createXLink (lastBranch,true);
561 xli->setBegin ( (BranchItem*)beginBI );
562 xli->setEnd ( (BranchItem*)endBI);
565 if (!a.value( "color").isEmpty() )
568 col.setNamedColor(a.value("color"));
572 if (!a.value( "width").isEmpty() )
575 xli->setWidth(a.value ("width").toInt (&okx, 10));
581 return true; // xLinks can only be established at the "end branch", return true
584 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
586 for (int i=1; i<=a.count(); i++)
587 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
591 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
593 if (!a.value( "key").isEmpty() )
595 if (!a.value( "value").isEmpty() )
596 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));