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();
144 cout << "xml-vym bi="<<bi->getHeadingStd()<<" loadMode="<<loadMode<<endl;
148 if (loadMode==ImportAdd)
150 lastBranch=model->createBranch(lastBranch);
152 model->clearItem(lastBranch);
154 // add mapCenter without parent
155 lastBranch=model->createMapCenter();
157 readBranchAttr (atts);
159 (eName == "standardflag" ||eName == "standardFlag") &&
160 (state == StateMapCenter || state==StateBranch))
162 state=StateStandardFlag;
163 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
167 if (!atts.value( "textColor").isEmpty() )
169 col.setNamedColor(atts.value("textColor"));
170 lastBranch->setHeadingColor(col );
172 } else if ( eName == "note" &&
173 (state == StateMapCenter ||state==StateBranch))
174 { // only for backward compatibility (<1.4.6). Use htmlnote now.
176 if (!readNoteAttr (atts) ) return false;
177 } else if ( eName == "htmlnote" && state == StateMapCenter)
181 } else if ( eName == "floatimage" &&
182 (state == StateMapCenter ||state==StateBranch))
185 lastImage=model->createImage(lastBranch);
186 if (!readImageAttr(atts)) return false;
187 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
189 // This is used in vymparts, which have no mapcenter or for undo
191 TreeItem *ti=model->getSelectedItem();
194 // If a vym part is _loaded_ (not imported),
195 // selection==lmo==NULL
196 // Treat it like ImportAdd then...
198 // FIXME-3 lmo=model->first()->getLMO();
199 // Do we really have no MCO when loading?????
200 cout << "xml-vym aborted\n";
203 if (ti && ti->isBranchLikeType() )
205 lastBranch=(BranchItem*)ti;
209 if (loadMode==ImportAdd)
211 lastBranch=model->createBranch(lastBranch);
213 model->clearItem (lastBranch);
214 readBranchAttr (atts);
215 } else if (eName=="floatimage")
218 lastImage=model->createImage (lastBranch);
219 if (!readImageAttr(atts)) return false;
222 } else if ( eName == "branch" && state == StateMapCenter)
225 lastBranch=model->createBranch(lastBranch);
226 readBranchAttr (atts);
227 } else if ( eName == "htmlnote" && state == StateBranch)
232 if (!atts.value( "fonthint").isEmpty() )
233 no.setFontHint(atts.value ("fonthint") );
234 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
238 if (!readFrameAttr(atts)) return false;
239 } else if ( eName == "xlink" && state == StateBranch )
241 state=StateBranchXLink;
242 if (!readXLinkAttr (atts)) return false;
243 } else if ( eName == "branch" && state == StateBranch )
245 lastBranch=model->createBranch(lastBranch);
246 readBranchAttr (atts);
247 } else if ( eName == "html" && state == StateHtmlNote )
253 } else if ( state == StateHtml )
255 // accept all while in html mode,
260 return false; // Error
264 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
267 cout << "endElement </" <<qPrintable(eName)
269 // <<" laststate=" <<laststate
270 // <<" stateStack="<<stateStack.last()
271 // <<" selString="<<model->getSelectString().toStdString()
277 mainWindow->removeProgressCounter();
280 model->emitDataHasChanged (lastBranch);
281 lastBranch=(BranchItem*)(lastBranch->parent());
282 // lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
285 // Empty branches may not be scrolled
286 // (happens if bookmarks are imported)
287 if (lastBranch->isScrolled() && lastBranch->branchCount()==0)
288 lastBranch->unScroll();
289 model->emitDataHasChanged (lastBranch);
291 lastBranch=(BranchItem*)(lastBranch->parent());
292 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
295 htmldata+="</"+eName+">";
299 htmldata.replace ("<br></br>","<br />");
300 no.setNote (htmldata);
301 lastBranch->setNoteObj (no);
307 state=stateStack.takeLast();
311 bool parseVYMHandler::characters ( const QString& ch)
313 //cout << "characters \""<<ch.toStdString()<<"\" state="<<state <<" laststate="<<laststate<<endl;
315 QString ch_org=quotemeta (ch);
316 QString ch_simplified=ch.simplifyWhiteSpace();
317 if ( ch_simplified.isEmpty() ) return true;
321 case StateInit: break;
322 case StateMap: break;
324 model->select(ch_simplified);
326 case StateMapSetting:break;
327 case StateMapCenter: break;
329 lastBranch->setNote(ch_simplified);
331 case StateBranch: break;
332 case StateStandardFlag:
333 lastBranch->activateStandardFlag(ch_simplified);
335 case StateImage: break;
336 case StateHtmlNote: break;
341 lastBranch->setHeading(ch_simplified);
349 QString parseVYMHandler::errorString()
351 return "the document is not in the VYM file format";
354 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
358 mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);
362 if (!readOOAttr(a)) return false;
364 if (!a.value( "scrolled").isEmpty() )
365 lastBranch->toggleScroll();
366 // (interesting for import of KDE bookmarks)
368 /* if (!a.value( "frameType").isEmpty() ) FIXME-3
369 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
372 if (!a.value( "incImgV").isEmpty() )
374 if (a.value("incImgV")=="true")
375 lastBranch->setIncludeImagesVer(true);
377 lastBranch->setIncludeImagesVer(false);
379 if (!a.value( "incImgH").isEmpty() )
381 if (a.value("incImgH")=="true")
382 lastBranch->setIncludeImagesHor(true);
384 lastBranch->setIncludeImagesHor(false);
389 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
393 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
399 if (!a.value( "frameType").isEmpty() )
400 oo->setFrameType (a.value("frameType"));
401 if (!a.value( "penColor").isEmpty() )
402 oo->setFramePenColor (a.value("penColor"));
403 if (!a.value( "brushColor").isEmpty() )
404 oo->setFrameBrushColor (a.value("brushColor"));
405 if (!a.value( "padding").isEmpty() )
407 x=a.value("padding").toInt(&ok);
408 if (ok) oo->setFramePadding(x);
410 if (!a.value( "borderWidth").isEmpty() )
412 x=a.value("borderWidth").toInt(&ok);
413 if (ok) oo->setFrameBorderWidth(x);
422 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
428 if (!a.value( "relPosX").isEmpty() )
430 if (!a.value( "relPosY").isEmpty() )
432 x=a.value("relPosX").toFloat (&okx);
433 y=a.value("relPosY").toFloat (&oky);
435 lastMI->setRelPos (QPointF(x,y));
437 return false; // Couldn't read relPos
440 if (!a.value( "absPosX").isEmpty() )
442 if (!a.value( "absPosY").isEmpty() )
444 x=a.value("absPosX").toFloat (&okx);
445 y=a.value("absPosY").toFloat (&oky);
447 lastMI->setAbsPos (QPointF(x,y));
449 return false; // Couldn't read absPos
452 if (!a.value( "url").isEmpty() )
453 lastMI->setURL (a.value ("url"));
454 if (!a.value( "vymLink").isEmpty() )
455 lastMI->setVymLink (a.value ("vymLink"));
456 if (!a.value( "hideInExport").isEmpty() )
457 if (a.value("hideInExport")=="true")
458 lastMI->setHideInExport(true);
460 if (!a.value( "hideLink").isEmpty())
462 if (a.value ("hideLink") =="true")
463 lastMI->setHideLinkUnselected(true);
465 lastMI->setHideLinkUnselected(false);
471 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
472 { // only for backward compatibility (<1.4.6). Use htmlnote now.
475 if (!a.value( "href").isEmpty() )
478 fn=parseHREF(a.value ("href") );
480 QString s; // Reading a note
482 if ( !file.open( QIODevice::ReadOnly) )
484 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
487 QTextStream stream( &file );
489 while ( !stream.atEnd() ) {
490 lines += stream.readLine()+"\n";
494 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
497 if (!a.value( "fonthint").isEmpty() )
498 no.setFontHint(a.value ("fonthint") );
499 lastBranch->setNoteObj(no);
503 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
507 if (!readOOAttr(a)) return false;
509 if (!a.value( "href").isEmpty() )
512 if (!lastImage->load (parseHREF(a.value ("href") ) ))
514 QMessageBox::warning( 0, "Warning: " ,
515 "Couldn't load image\n"+parseHREF(a.value ("href") ));
521 if (!a.value( "zPlane").isEmpty() )
522 lastImage->setZValue (a.value("zPlane").toInt ());
525 if (!a.value( "relPosX").isEmpty() )
527 if (!a.value( "relPosY").isEmpty() )
530 x=a.value("relPosX").toFloat (&okx);
531 y=a.value("relPosY").toFloat (&oky);
533 lastImage->setRelPos (QPointF (x,y) );
535 // Couldn't read relPos
540 //FIXME-3 if (!readOOAttr(a)) return false;
542 if (!a.value ("originalName").isEmpty() )
544 lastImage->setOriginalFilename (a.value("originalName"));
549 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
551 // object ID is used starting in version 1.8.76
552 // (before there was beginBranch and endBranch)
553 if (!a.value( "beginID").isEmpty() )
555 if (!a.value( "endID").isEmpty() )
557 TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
558 TreeItem *endBI=model->findBySelectString (a.value( "endID"));
559 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
561 XLinkItem *xli=model->createXLink (lastBranch,true);
562 xli->setBegin ( (BranchItem*)beginBI );
563 xli->setEnd ( (BranchItem*)endBI);
566 if (!a.value( "color").isEmpty() )
569 col.setNamedColor(a.value("color"));
573 if (!a.value( "width").isEmpty() )
576 xli->setWidth(a.value ("width").toInt (&okx, 10));
582 return true; // xLinks can only be established at the "end branch", return true
585 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
587 for (int i=1; i<=a.count(); i++)
588 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
592 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
594 if (!a.value( "key").isEmpty() )
596 if (!a.value( "value").isEmpty() )
597 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));