9 #include "branchitem.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
15 #include "xlinkitem.h"
17 static ImageItem *lastImageItem;
18 static MapItem *lastMI;
20 extern Main *mainWindow;
21 extern Settings settings;
22 extern QString vymVersion;
24 bool parseVYMHandler::startDocument()
28 laststate = StateInit;
30 stateStack.append(StateInit);
36 bool parseVYMHandler::startElement ( const QString&, const QString&,
37 const QString& eName, const QXmlAttributes& atts )
41 cout << "startElement <"<< qPrintable(eName)
43 <<" laststate="<<stateStack.last()
44 <<" loadMode="<<loadMode
45 // <<" line="<<QXmlDefaultHandler::lineNumber()
48 stateStack.append (state);
49 if ( state == StateInit && (eName == "vymmap") )
52 branchesTotal=branchesCurrent=0;
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();
69 mainWindow->setProgressMinimum (0);
70 mainWindow->setProgressMaximum (branchesTotal);
71 mainWindow->setProgressValue(0);
75 mainWindow->setProgressMinimum (0);
76 mainWindow->setProgressMaximum (0);
77 mainWindow->setProgressValue(0);
81 if (!atts.value( "backgroundColor").isEmpty() )
83 col.setNamedColor(atts.value("backgroundColor"));
84 model->getScene()->setBackgroundBrush(col);
86 if (!atts.value( "selectionColor").isEmpty() )
88 col.setNamedColor(atts.value("selectionColor"));
89 model->setSelectionColor(col);
91 if (!atts.value( "linkColorHint").isEmpty() )
93 if (atts.value("linkColorHint")=="HeadingColor")
94 model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
96 model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
98 if (!atts.value( "linkStyle").isEmpty() )
99 model->setMapLinkStyle(atts.value("linkStyle"));
100 if (!atts.value( "linkColor").isEmpty() )
102 col.setNamedColor(atts.value("linkColor"));
103 model->setMapDefLinkColor(col);
105 if (!atts.value( "defXLinkColor").isEmpty() )
107 col.setNamedColor(atts.value("defXLinkColor"));
108 model->setMapDefXLinkColor(col);
110 if (!atts.value( "defXLinkWidth").isEmpty() )
111 model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
114 if (!atts.value( "version").isEmpty() )
116 if (!checkVersion(atts.value("version")))
117 QMessageBox::warning( 0, "Warning: Version Problem" ,
118 "<h3>Map is newer than VYM</h3>"
119 "<p>The map you are just trying to load was "
120 "saved using vym " +atts.value("version")+". "
121 "The version of this vym is " + vymVersion +
122 ". If you run into problems after pressing "
123 "the ok-button below, updating vym should help.");
125 model->setVersion(atts.value( "version" ));
129 } else if ( eName == "select" && state == StateMap )
131 state=StateMapSelect;
132 } else if ( eName == "setting" && state == StateMap )
134 state=StateMapSetting;
135 if (loadMode==NewMap)
136 readSettingAttr (atts);
137 } else if ( eName == "mapcenter" && state == StateMap )
139 state=StateMapCenter;
140 if (loadMode==NewMap)
142 // Really use the found mapcenter as MCO in a new map
143 lastBranch=model->createMapCenter();
146 // Treat the found mapcenter as a branch
147 // in an existing map
148 BranchItem *bi=model->getSelectedBranch(); //FIXME-3 selection is no longer used here... //FIXME-2 really? we are adding to a map...
152 if (loadMode==ImportAdd)
154 lastBranch=model->createBranch(lastBranch);
156 //lastBranch->clear(); //FIXME-1 clear not really defined!
158 // add mapCenter without parent
159 lastBranch=model->createMapCenter();
161 readBranchAttr (atts);
163 (eName == "standardflag" ||eName == "standardFlag") &&
164 (state == StateMapCenter || state==StateBranch))
166 state=StateStandardFlag;
167 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
171 if (!atts.value( "textColor").isEmpty() )
173 col.setNamedColor(atts.value("textColor"));
174 lastBranch->setHeadingColor(col );
176 } else if ( eName == "note" &&
177 (state == StateMapCenter ||state==StateBranch))
178 { // only for backward compatibility (<1.4.6). Use htmlnote now.
180 if (!readNoteAttr (atts) ) return false;
181 } else if ( eName == "htmlnote" && state == StateMapCenter)
185 } else if ( eName == "floatimage" &&
186 (state == StateMapCenter ||state==StateBranch))
189 lastImage=model->createImage(lastBranch);
190 if (!readImageAttr(atts)) return false;
191 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
193 // This is used in vymparts, which have no mapcenter!
195 TreeItem *ti=model->getSelectedItem(); //FIXME-3 selection is no longer used here...
198 // If a vym part is _loaded_ (not imported),
199 // selection==lmo==NULL
200 // Treat it like ImportAdd then...
202 // FIXME-3 lmo=model->first()->getLMO();
203 // Do we really have no MCO when loading?????
204 cout << "xml-vym aborted\n";
206 if (ti && ti->isBranchLikeType() )
208 lastBranch=(BranchItem*)ti;
212 if (loadMode==ImportAdd)
214 lastBranch=model->createBranch(lastBranch);
217 //FIXME-2 lastBranch->clear();
218 readBranchAttr (atts);
219 } else if (eName=="floatimage")
222 lastImage=model->createImage (lastBranch);
223 if (!readImageAttr(atts)) return false;
226 } else if ( eName == "branch" && state == StateMapCenter)
229 lastBranch=model->createBranch(lastBranch);
230 readBranchAttr (atts);
231 } else if ( eName == "htmlnote" && state == StateBranch)
236 if (!atts.value( "fonthint").isEmpty() )
237 no.setFontHint(atts.value ("fonthint") );
238 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
242 if (!readFrameAttr(atts)) return false;
243 } else if ( eName == "xlink" && state == StateBranch )
245 state=StateBranchXLink;
246 if (!readXLinkAttr (atts)) return false;
247 } else if ( eName == "branch" && state == StateBranch )
249 lastBranch=model->createBranch(lastBranch);
250 readBranchAttr (atts);
251 } else if ( eName == "html" && state == StateHtmlNote )
257 } else if ( state == StateHtml )
259 // accept all while in html mode,
264 return false; // Error
268 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
271 cout << "endElement </" <<qPrintable(eName)
273 // <<" laststate=" <<laststate
274 // <<" stateStack="<<stateStack.last()
275 // <<" selString="<<model->getSelectString().toStdString()
281 mainWindow->removeProgressBar();
284 model->emitDataHasChanged (lastBranch);
285 lastBranch=(BranchItem*)(lastBranch->parent());
286 // lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
289 // Empty branches may not be scrolled
290 // (happens if bookmarks are imported)
291 if (lastBranch->isScrolled() && lastBranch->branchCount()==0)
292 lastBranch->unScroll();
293 model->emitDataHasChanged (lastBranch);
295 lastBranch=(BranchItem*)(lastBranch->parent());
296 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
299 htmldata+="</"+eName+">";
303 htmldata.replace ("<br></br>","<br />");
304 no.setNote (htmldata);
305 lastBranch->setNoteObj (no);
311 state=stateStack.takeLast();
315 bool parseVYMHandler::characters ( const QString& ch)
317 //cout << "characters \""<<ch.toStdString()<<"\" state="<<state <<" laststate="<<laststate<<endl;
319 QString ch_org=quotemeta (ch);
320 QString ch_simplified=ch.simplifyWhiteSpace();
321 if ( ch_simplified.isEmpty() ) return true;
325 case StateInit: break;
326 case StateMap: break;
328 model->select(ch_simplified);
330 case StateMapSetting:break;
331 case StateMapCenter: break;
333 lastBranch->setNote(ch_simplified);
335 case StateBranch: break;
336 case StateStandardFlag:
337 lastBranch->activateStandardFlag(ch_simplified);
339 case StateImage: break;
340 case StateHtmlNote: break;
345 lastBranch->setHeading(ch_simplified);
353 QString parseVYMHandler::errorString()
355 return "the document is not in the VYM file format";
358 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
360 mainWindow->setProgressValue (branchesCurrent++);
364 if (!readOOAttr(a)) return false;
366 if (!a.value( "scrolled").isEmpty() )
367 lastBranch->toggleScroll();
368 // (interesting for import of KDE bookmarks)
370 /* if (!a.value( "frameType").isEmpty() ) FIXME-3
371 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
374 if (!a.value( "incImgV").isEmpty() )
376 if (a.value("incImgV")=="true")
377 lastBranch->setIncludeImagesVer(true);
379 lastBranch->setIncludeImagesVer(false);
381 if (!a.value( "incImgH").isEmpty() )
383 if (a.value("incImgH")=="true")
384 lastBranch->setIncludeImagesHor(true);
386 lastBranch->setIncludeImagesHor(false);
391 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
395 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
401 if (!a.value( "frameType").isEmpty() )
402 oo->setFrameType (a.value("frameType"));
403 if (!a.value( "penColor").isEmpty() )
404 oo->setFramePenColor (a.value("penColor"));
405 if (!a.value( "brushColor").isEmpty() )
406 oo->setFrameBrushColor (a.value("brushColor"));
407 if (!a.value( "padding").isEmpty() )
409 x=a.value("padding").toInt(&ok);
410 if (ok) oo->setFramePadding(x);
412 if (!a.value( "borderWidth").isEmpty() )
414 x=a.value("borderWidth").toInt(&ok);
415 if (ok) oo->setFrameBorderWidth(x);
424 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
430 if (!a.value( "relPosX").isEmpty() )
432 if (!a.value( "relPosY").isEmpty() )
434 x=a.value("relPosX").toFloat (&okx);
435 y=a.value("relPosY").toFloat (&oky);
437 lastMI->setRelPos (QPointF(x,y));
439 return false; // Couldn't read relPos
442 if (!a.value( "absPosX").isEmpty() )
444 if (!a.value( "absPosY").isEmpty() )
446 x=a.value("absPosX").toFloat (&okx);
447 y=a.value("absPosY").toFloat (&oky);
449 lastMI->setAbsPos (QPointF(x,y));
451 return false; // Couldn't read absPos
454 if (!a.value( "id").isEmpty() )
455 lastMI->setID (a.value ("id"));
457 if (!a.value( "url").isEmpty() )
458 lastMI->setURL (a.value ("url"));
459 if (!a.value( "vymLink").isEmpty() )
460 lastMI->setVymLink (a.value ("vymLink"));
461 if (!a.value( "hideInExport").isEmpty() )
462 if (a.value("hideInExport")=="true")
463 lastMI->setHideInExport(true);
465 if (!a.value( "hideLink").isEmpty())
467 if (a.value ("hideLink") =="true")
468 lastMI->setHideLinkUnselected(true);
470 lastMI->setHideLinkUnselected(false);
476 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
477 { // only for backward compatibility (<1.4.6). Use htmlnote now.
480 if (!a.value( "href").isEmpty() )
483 fn=parseHREF(a.value ("href") );
485 QString s; // Reading a note
487 if ( !file.open( QIODevice::ReadOnly) )
489 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
492 QTextStream stream( &file );
494 while ( !stream.atEnd() ) {
495 lines += stream.readLine()+"\n";
499 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
502 if (!a.value( "fonthint").isEmpty() )
503 no.setFontHint(a.value ("fonthint") );
504 lastBranch->setNoteObj(no);
508 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
512 //if (!readOOAttr(a)) return false; FIXME-3
514 if (!a.value( "href").isEmpty() )
517 if (!lastImage->load (parseHREF(a.value ("href") ) ))
519 QMessageBox::warning( 0, "Warning: " ,
520 "Couldn't load image\n"+parseHREF(a.value ("href") ));
526 if (!a.value( "zPlane").isEmpty() )
527 lastImage->setZValue (a.value("zPlane").toInt ());
530 if (!a.value( "relPosX").isEmpty() )
532 if (!a.value( "relPosY").isEmpty() )
535 x=a.value("relPosX").toFloat (&okx);
536 y=a.value("relPosY").toFloat (&oky);
538 lastImage->setRelPos (QPointF (x,y) );
540 // Couldn't read relPos
545 //FIXME-3 if (!readOOAttr(a)) return false;
547 if (!a.value ("originalName").isEmpty() )
549 lastImage->setOriginalFilename (a.value("originalName"));
554 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
556 // object ID is used starting in version 1.8.76
557 // (before there was beginBranch and endBranch)
558 if (!a.value( "beginID").isEmpty() )
560 if (!a.value( "endID").isEmpty() )
562 TreeItem *beginBI=model->findID (a.value( "beginID"));
563 TreeItem *endBI=model->findID (a.value( "endID"));
564 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
566 XLinkItem *xli=model->createXLink (lastBranch,true);
567 xli->setBegin ( (BranchItem*)beginBI );
568 xli->setEnd ( (BranchItem*)endBI);
571 if (!a.value( "color").isEmpty() )
574 col.setNamedColor(a.value("color"));
578 if (!a.value( "width").isEmpty() )
581 xli->setWidth(a.value ("width").toInt (&okx, 10));
587 return true; // xLinks can only be established at the "end branch", return true
590 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
592 for (int i=1; i<=a.count(); i++)
593 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
597 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
599 if (!a.value( "key").isEmpty() )
601 if (!a.value( "value").isEmpty() )
602 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));