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 ());
107 if (!atts.value( "version").isEmpty() )
109 if (!checkVersion(atts.value("version")))
110 QMessageBox::warning( 0, "Warning: Version Problem" ,
111 "<h3>Map is newer than VYM</h3>"
112 "<p>The map you are just trying to load was "
113 "saved using vym " +atts.value("version")+". "
114 "The version of this vym is " + vymVersion +
115 ". If you run into problems after pressing "
116 "the ok-button below, updating vym should help.");
118 model->setVersion(atts.value( "version" ));
122 } else if ( eName == "select" && state == StateMap )
124 state=StateMapSelect;
125 } else if ( eName == "setting" && state == StateMap )
127 state=StateMapSetting;
128 if (loadMode==NewMap)
129 readSettingAttr (atts);
130 } else if ( eName == "mapcenter" && state == StateMap )
132 state=StateMapCenter;
133 if (loadMode==NewMap)
135 // Really use the found mapcenter as MCO in a new map
136 lastBranch=model->createMapCenter();
139 // Treat the found mapcenter as a branch
140 // in an existing map
141 BranchItem *bi=model->getSelectedBranch();
142 cout << "xml-vym bi="<<bi->getHeadingStd()<<" loadMode="<<loadMode<<endl;
146 if (loadMode==ImportAdd)
148 lastBranch=model->createBranch(lastBranch);
150 model->clearItem(lastBranch);
152 // add mapCenter without parent
153 lastBranch=model->createMapCenter();
155 readBranchAttr (atts);
157 (eName == "standardflag" ||eName == "standardFlag") &&
158 (state == StateMapCenter || state==StateBranch))
160 state=StateStandardFlag;
161 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
165 if (!atts.value( "textColor").isEmpty() )
167 col.setNamedColor(atts.value("textColor"));
168 lastBranch->setHeadingColor(col );
170 } else if ( eName == "note" &&
171 (state == StateMapCenter ||state==StateBranch))
172 { // only for backward compatibility (<1.4.6). Use htmlnote now.
174 if (!readNoteAttr (atts) ) return false;
175 } else if ( eName == "htmlnote" && state == StateMapCenter)
179 } else if ( eName == "floatimage" &&
180 (state == StateMapCenter ||state==StateBranch))
183 lastImage=model->createImage(lastBranch);
184 if (!readImageAttr(atts)) return false;
185 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
187 // This is used in vymparts, which have no mapcenter or for undo
189 TreeItem *ti=model->getSelectedItem();
192 // If a vym part is _loaded_ (not imported),
193 // selection==lmo==NULL
194 // Treat it like ImportAdd then...
196 // FIXME-3 lmo=model->first()->getLMO();
197 // Do we really have no MCO when loading?????
198 cout << "xml-vym aborted\n";
201 if (ti && ti->isBranchLikeType() )
203 lastBranch=(BranchItem*)ti;
207 if (loadMode==ImportAdd)
209 lastBranch=model->createBranch(lastBranch);
211 model->clearItem (lastBranch);
212 readBranchAttr (atts);
213 } else if (eName=="floatimage")
216 lastImage=model->createImage (lastBranch);
217 if (!readImageAttr(atts)) return false;
220 } else if ( eName == "branch" && state == StateMapCenter)
223 lastBranch=model->createBranch(lastBranch);
224 readBranchAttr (atts);
225 } else if ( eName == "htmlnote" && state == StateBranch)
230 if (!atts.value( "fonthint").isEmpty() )
231 no.setFontHint(atts.value ("fonthint") );
232 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
236 if (!readFrameAttr(atts)) return false;
237 } else if ( eName == "xlink" && state == StateBranch )
239 state=StateBranchXLink;
240 if (!readXLinkAttr (atts)) return false;
241 } else if ( eName == "branch" && state == StateBranch )
243 lastBranch=model->createBranch(lastBranch);
244 readBranchAttr (atts);
245 } else if ( eName == "html" && state == StateHtmlNote )
251 } else if ( state == StateHtml )
253 // accept all while in html mode,
258 return false; // Error
262 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
265 cout << "endElement </" <<qPrintable(eName)
267 // <<" laststate=" <<laststate
268 // <<" stateStack="<<stateStack.last()
269 // <<" selString="<<model->getSelectString().toStdString()
275 mainWindow->removeProgressCounter();
278 model->emitDataHasChanged (lastBranch);
279 lastBranch=(BranchItem*)(lastBranch->parent());
280 // lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
283 // Empty branches may not be scrolled
284 // (happens if bookmarks are imported)
285 if (lastBranch->isScrolled() && lastBranch->branchCount()==0)
286 lastBranch->unScroll();
287 model->emitDataHasChanged (lastBranch);
289 lastBranch=(BranchItem*)(lastBranch->parent());
290 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
293 htmldata+="</"+eName+">";
297 htmldata.replace ("<br></br>","<br />");
298 no.setNote (htmldata);
299 lastBranch->setNoteObj (no);
305 state=stateStack.takeLast();
309 bool parseVYMHandler::characters ( const QString& ch)
311 //cout << "characters \""<<ch.toStdString()<<"\" state="<<state <<" laststate="<<laststate<<endl;
313 QString ch_org=quotemeta (ch);
314 QString ch_simplified=ch.simplifyWhiteSpace();
315 if ( ch_simplified.isEmpty() ) return true;
319 case StateInit: break;
320 case StateMap: break;
322 model->select(ch_simplified);
324 case StateMapSetting:break;
325 case StateMapCenter: break;
327 lastBranch->setNote(ch_simplified);
329 case StateBranch: break;
330 case StateStandardFlag:
331 lastBranch->activateStandardFlag(ch_simplified);
333 case StateImage: break;
334 case StateHtmlNote: break;
339 lastBranch->setHeading(ch_simplified);
347 QString parseVYMHandler::errorString()
349 return "the document is not in the VYM file format";
352 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
356 mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);
360 if (!readOOAttr(a)) return false;
362 if (!a.value( "scrolled").isEmpty() )
363 lastBranch->toggleScroll();
364 // (interesting for import of KDE bookmarks)
366 /* if (!a.value( "frameType").isEmpty() ) FIXME-3
367 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
370 if (!a.value( "incImgV").isEmpty() )
372 if (a.value("incImgV")=="true")
373 lastBranch->setIncludeImagesVer(true);
375 lastBranch->setIncludeImagesVer(false);
377 if (!a.value( "incImgH").isEmpty() )
379 if (a.value("incImgH")=="true")
380 lastBranch->setIncludeImagesHor(true);
382 lastBranch->setIncludeImagesHor(false);
387 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
391 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
397 if (!a.value( "frameType").isEmpty() )
398 oo->setFrameType (a.value("frameType"));
399 if (!a.value( "penColor").isEmpty() )
400 oo->setFramePenColor (a.value("penColor"));
401 if (!a.value( "brushColor").isEmpty() )
402 oo->setFrameBrushColor (a.value("brushColor"));
403 if (!a.value( "padding").isEmpty() )
405 x=a.value("padding").toInt(&ok);
406 if (ok) oo->setFramePadding(x);
408 if (!a.value( "borderWidth").isEmpty() )
410 x=a.value("borderWidth").toInt(&ok);
411 if (ok) oo->setFrameBorderWidth(x);
420 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
426 if (!a.value( "relPosX").isEmpty() )
428 if (!a.value( "relPosY").isEmpty() )
430 x=a.value("relPosX").toFloat (&okx);
431 y=a.value("relPosY").toFloat (&oky);
433 lastMI->setRelPos (QPointF(x,y));
435 return false; // Couldn't read relPos
438 if (!a.value( "absPosX").isEmpty() )
440 if (!a.value( "absPosY").isEmpty() )
442 x=a.value("absPosX").toFloat (&okx);
443 y=a.value("absPosY").toFloat (&oky);
445 lastMI->setAbsPos (QPointF(x,y));
447 return false; // Couldn't read absPos
450 if (!a.value( "url").isEmpty() )
451 lastMI->setURL (a.value ("url"));
452 if (!a.value( "vymLink").isEmpty() )
453 lastMI->setVymLink (a.value ("vymLink"));
454 if (!a.value( "hideInExport").isEmpty() )
455 if (a.value("hideInExport")=="true")
456 lastMI->setHideInExport(true);
458 if (!a.value( "hideLink").isEmpty())
460 if (a.value ("hideLink") =="true")
461 lastMI->setHideLinkUnselected(true);
463 lastMI->setHideLinkUnselected(false);
469 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
470 { // only for backward compatibility (<1.4.6). Use htmlnote now.
473 if (!a.value( "href").isEmpty() )
476 fn=parseHREF(a.value ("href") );
478 QString s; // Reading a note
480 if ( !file.open( QIODevice::ReadOnly) )
482 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
485 QTextStream stream( &file );
487 while ( !stream.atEnd() ) {
488 lines += stream.readLine()+"\n";
492 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
495 if (!a.value( "fonthint").isEmpty() )
496 no.setFontHint(a.value ("fonthint") );
497 lastBranch->setNoteObj(no);
501 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
505 if (!readOOAttr(a)) return false;
507 if (!a.value( "href").isEmpty() )
510 if (!lastImage->load (parseHREF(a.value ("href") ) ))
512 QMessageBox::warning( 0, "Warning: " ,
513 "Couldn't load image\n"+parseHREF(a.value ("href") ));
519 if (!a.value( "zPlane").isEmpty() )
520 lastImage->setZValue (a.value("zPlane").toInt ());
523 if (!a.value( "relPosX").isEmpty() )
525 if (!a.value( "relPosY").isEmpty() )
528 x=a.value("relPosX").toFloat (&okx);
529 y=a.value("relPosY").toFloat (&oky);
531 lastImage->setRelPos (QPointF (x,y) );
533 // Couldn't read relPos
538 //FIXME-3 if (!readOOAttr(a)) return false;
540 if (!a.value ("originalName").isEmpty() )
542 lastImage->setOriginalFilename (a.value("originalName"));
547 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
549 // object ID is used starting in version 1.8.76
550 // (before there was beginBranch and endBranch)
551 if (!a.value( "beginID").isEmpty() )
553 if (!a.value( "endID").isEmpty() )
555 TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
556 TreeItem *endBI=model->findBySelectString (a.value( "endID"));
557 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
559 XLinkItem *xli=model->createXLink (lastBranch,true);
560 xli->setBegin ( (BranchItem*)beginBI );
561 xli->setEnd ( (BranchItem*)endBI);
564 if (!a.value( "color").isEmpty() )
567 col.setNamedColor(a.value("color"));
571 if (!a.value( "width").isEmpty() )
574 xli->setWidth(a.value ("width").toInt (&okx, 10));
580 return true; // xLinks can only be established at the "end branch", return true
583 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
585 for (int i=1; i<=a.count(); i++)
586 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
590 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
592 if (!a.value( "key").isEmpty() )
594 if (!a.value( "value").isEmpty() )
595 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));