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);
34 bool parseVYMHandler::startElement ( const QString&, const QString&,
35 const QString& eName, const QXmlAttributes& atts )
39 cout << "startElement <"<< qPrintable(eName)
41 <<" laststate="<<stateStack.last()
42 <<" loadMode="<<loadMode
43 // <<" line="<<QXmlDefaultHandler::lineNumber()
46 stateStack.append (state);
47 if ( state == StateInit && (eName == "vymmap") )
50 branchesTotal=branchesCurrent=0;
52 if (loadMode==NewMap )
58 if (!atts.value( "author").isEmpty() )
59 model->setAuthor(atts.value( "author" ) );
60 if (!atts.value( "comment").isEmpty() )
61 model->setComment (atts.value( "comment" ) );
62 if (!atts.value( "branchCount").isEmpty() )
64 branchesTotal=atts.value("branchCount").toInt();
67 mainWindow->setProgressMinimum (0);
68 mainWindow->setProgressMaximum (branchesTotal);
69 mainWindow->setProgressValue(0);
73 mainWindow->setProgressMinimum (0);
74 mainWindow->setProgressMaximum (0);
75 mainWindow->setProgressValue(0);
79 if (!atts.value( "backgroundColor").isEmpty() )
81 col.setNamedColor(atts.value("backgroundColor"));
82 model->getScene()->setBackgroundBrush(col);
84 if (!atts.value( "selectionColor").isEmpty() )
86 col.setNamedColor(atts.value("selectionColor"));
87 model->setSelectionColor(col);
89 if (!atts.value( "linkColorHint").isEmpty() )
91 if (atts.value("linkColorHint")=="HeadingColor")
92 model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
94 model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
96 if (!atts.value( "linkStyle").isEmpty() )
97 model->setMapLinkStyle(atts.value("linkStyle"));
98 if (!atts.value( "linkColor").isEmpty() )
100 col.setNamedColor(atts.value("linkColor"));
101 model->setMapDefLinkColor(col);
103 if (!atts.value( "defXLinkColor").isEmpty() )
105 col.setNamedColor(atts.value("defXLinkColor"));
106 model->setMapDefXLinkColor(col);
108 if (!atts.value( "defXLinkWidth").isEmpty() )
109 model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
112 if (!atts.value( "version").isEmpty() )
114 if (!checkVersion(atts.value("version")))
115 QMessageBox::warning( 0, "Warning: Version Problem" ,
116 "<h3>Map is newer than VYM</h3>"
117 "<p>The map you are just trying to load was "
118 "saved using vym " +atts.value("version")+". "
119 "The version of this vym is " + vymVersion +
120 ". If you run into problems after pressing "
121 "the ok-button below, updating vym should help.");
123 model->setVersion(atts.value( "version" ));
127 } else if ( eName == "select" && state == StateMap )
129 state=StateMapSelect;
130 } else if ( eName == "setting" && state == StateMap )
132 state=StateMapSetting;
133 if (loadMode==NewMap)
134 readSettingAttr (atts);
135 } else if ( eName == "mapcenter" && state == StateMap )
137 state=StateMapCenter;
138 if (loadMode==NewMap)
140 // Really use the found mapcenter as MCO in a new map
141 lastBranch=model->createMapCenter();
144 // Treat the found mapcenter as a branch
145 // in an existing map
146 BranchItem *bi=model->getSelectedBranch();
147 cout << "xml-vym bi="<<bi->getHeadingStd()<<" loadMode="<<loadMode<<endl;
151 if (loadMode==ImportAdd)
153 lastBranch=model->createBranch(lastBranch);
155 model->clearItem(lastBranch);
157 // add mapCenter without parent
158 lastBranch=model->createMapCenter();
160 readBranchAttr (atts);
162 (eName == "standardflag" ||eName == "standardFlag") &&
163 (state == StateMapCenter || state==StateBranch))
165 state=StateStandardFlag;
166 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
170 if (!atts.value( "textColor").isEmpty() )
172 col.setNamedColor(atts.value("textColor"));
173 lastBranch->setHeadingColor(col );
175 } else if ( eName == "note" &&
176 (state == StateMapCenter ||state==StateBranch))
177 { // only for backward compatibility (<1.4.6). Use htmlnote now.
179 if (!readNoteAttr (atts) ) return false;
180 } else if ( eName == "htmlnote" && state == StateMapCenter)
184 } else if ( eName == "floatimage" &&
185 (state == StateMapCenter ||state==StateBranch))
188 lastImage=model->createImage(lastBranch);
189 if (!readImageAttr(atts)) return false;
190 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
192 // This is used in vymparts, which have no mapcenter or for undo
194 TreeItem *ti=model->getSelectedItem();
197 // If a vym part is _loaded_ (not imported),
198 // selection==lmo==NULL
199 // Treat it like ImportAdd then...
201 // FIXME-3 lmo=model->first()->getLMO();
202 // Do we really have no MCO when loading?????
203 cout << "xml-vym aborted\n";
206 if (ti && ti->isBranchLikeType() )
208 lastBranch=(BranchItem*)ti;
212 if (loadMode==ImportAdd)
214 lastBranch=model->createBranch(lastBranch);
216 model->clearItem (lastBranch);
217 readBranchAttr (atts);
218 } else if (eName=="floatimage")
221 lastImage=model->createImage (lastBranch);
222 if (!readImageAttr(atts)) return false;
225 } else if ( eName == "branch" && state == StateMapCenter)
228 lastBranch=model->createBranch(lastBranch);
229 readBranchAttr (atts);
230 } else if ( eName == "htmlnote" && state == StateBranch)
235 if (!atts.value( "fonthint").isEmpty() )
236 no.setFontHint(atts.value ("fonthint") );
237 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
241 if (!readFrameAttr(atts)) return false;
242 } else if ( eName == "xlink" && state == StateBranch )
244 state=StateBranchXLink;
245 if (!readXLinkAttr (atts)) return false;
246 } else if ( eName == "branch" && state == StateBranch )
248 lastBranch=model->createBranch(lastBranch);
249 readBranchAttr (atts);
250 } else if ( eName == "html" && state == StateHtmlNote )
256 } else if ( state == StateHtml )
258 // accept all while in html mode,
263 return false; // Error
267 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
270 cout << "endElement </" <<qPrintable(eName)
272 // <<" laststate=" <<laststate
273 // <<" stateStack="<<stateStack.last()
274 // <<" selString="<<model->getSelectString().toStdString()
280 mainWindow->removeProgressBar();
283 model->emitDataHasChanged (lastBranch);
284 lastBranch=(BranchItem*)(lastBranch->parent());
285 // lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
288 // Empty branches may not be scrolled
289 // (happens if bookmarks are imported)
290 if (lastBranch->isScrolled() && lastBranch->branchCount()==0)
291 lastBranch->unScroll();
292 model->emitDataHasChanged (lastBranch);
294 lastBranch=(BranchItem*)(lastBranch->parent());
295 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
298 htmldata+="</"+eName+">";
302 htmldata.replace ("<br></br>","<br />");
303 no.setNote (htmldata);
304 lastBranch->setNoteObj (no);
310 state=stateStack.takeLast();
314 bool parseVYMHandler::characters ( const QString& ch)
316 //cout << "characters \""<<ch.toStdString()<<"\" state="<<state <<" laststate="<<laststate<<endl;
318 QString ch_org=quotemeta (ch);
319 QString ch_simplified=ch.simplifyWhiteSpace();
320 if ( ch_simplified.isEmpty() ) return true;
324 case StateInit: break;
325 case StateMap: break;
327 model->select(ch_simplified);
329 case StateMapSetting:break;
330 case StateMapCenter: break;
332 lastBranch->setNote(ch_simplified);
334 case StateBranch: break;
335 case StateStandardFlag:
336 lastBranch->activateStandardFlag(ch_simplified);
338 case StateImage: break;
339 case StateHtmlNote: break;
344 lastBranch->setHeading(ch_simplified);
352 QString parseVYMHandler::errorString()
354 return "the document is not in the VYM file format";
357 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
359 mainWindow->setProgressValue (branchesCurrent++);
363 if (!readOOAttr(a)) return false;
365 if (!a.value( "scrolled").isEmpty() )
366 lastBranch->toggleScroll();
367 // (interesting for import of KDE bookmarks)
369 /* if (!a.value( "frameType").isEmpty() ) FIXME-3
370 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
373 if (!a.value( "incImgV").isEmpty() )
375 if (a.value("incImgV")=="true")
376 lastBranch->setIncludeImagesVer(true);
378 lastBranch->setIncludeImagesVer(false);
380 if (!a.value( "incImgH").isEmpty() )
382 if (a.value("incImgH")=="true")
383 lastBranch->setIncludeImagesHor(true);
385 lastBranch->setIncludeImagesHor(false);
390 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
394 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
400 if (!a.value( "frameType").isEmpty() )
401 oo->setFrameType (a.value("frameType"));
402 if (!a.value( "penColor").isEmpty() )
403 oo->setFramePenColor (a.value("penColor"));
404 if (!a.value( "brushColor").isEmpty() )
405 oo->setFrameBrushColor (a.value("brushColor"));
406 if (!a.value( "padding").isEmpty() )
408 x=a.value("padding").toInt(&ok);
409 if (ok) oo->setFramePadding(x);
411 if (!a.value( "borderWidth").isEmpty() )
413 x=a.value("borderWidth").toInt(&ok);
414 if (ok) oo->setFrameBorderWidth(x);
423 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
429 if (!a.value( "relPosX").isEmpty() )
431 if (!a.value( "relPosY").isEmpty() )
433 x=a.value("relPosX").toFloat (&okx);
434 y=a.value("relPosY").toFloat (&oky);
436 lastMI->setRelPos (QPointF(x,y));
438 return false; // Couldn't read relPos
441 if (!a.value( "absPosX").isEmpty() )
443 if (!a.value( "absPosY").isEmpty() )
445 x=a.value("absPosX").toFloat (&okx);
446 y=a.value("absPosY").toFloat (&oky);
448 lastMI->setAbsPos (QPointF(x,y));
450 return false; // Couldn't read absPos
453 if (!a.value( "id").isEmpty() )
454 lastMI->setID (a.value ("id"));
456 if (!a.value( "url").isEmpty() )
457 lastMI->setURL (a.value ("url"));
458 if (!a.value( "vymLink").isEmpty() )
459 lastMI->setVymLink (a.value ("vymLink"));
460 if (!a.value( "hideInExport").isEmpty() )
461 if (a.value("hideInExport")=="true")
462 lastMI->setHideInExport(true);
464 if (!a.value( "hideLink").isEmpty())
466 if (a.value ("hideLink") =="true")
467 lastMI->setHideLinkUnselected(true);
469 lastMI->setHideLinkUnselected(false);
475 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
476 { // only for backward compatibility (<1.4.6). Use htmlnote now.
479 if (!a.value( "href").isEmpty() )
482 fn=parseHREF(a.value ("href") );
484 QString s; // Reading a note
486 if ( !file.open( QIODevice::ReadOnly) )
488 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
491 QTextStream stream( &file );
493 while ( !stream.atEnd() ) {
494 lines += stream.readLine()+"\n";
498 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
501 if (!a.value( "fonthint").isEmpty() )
502 no.setFontHint(a.value ("fonthint") );
503 lastBranch->setNoteObj(no);
507 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
511 //if (!readOOAttr(a)) return false; FIXME-3
513 if (!a.value( "href").isEmpty() )
516 if (!lastImage->load (parseHREF(a.value ("href") ) ))
518 QMessageBox::warning( 0, "Warning: " ,
519 "Couldn't load image\n"+parseHREF(a.value ("href") ));
525 if (!a.value( "zPlane").isEmpty() )
526 lastImage->setZValue (a.value("zPlane").toInt ());
529 if (!a.value( "relPosX").isEmpty() )
531 if (!a.value( "relPosY").isEmpty() )
534 x=a.value("relPosX").toFloat (&okx);
535 y=a.value("relPosY").toFloat (&oky);
537 lastImage->setRelPos (QPointF (x,y) );
539 // Couldn't read relPos
544 //FIXME-3 if (!readOOAttr(a)) return false;
546 if (!a.value ("originalName").isEmpty() )
548 lastImage->setOriginalFilename (a.value("originalName"));
553 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
555 // object ID is used starting in version 1.8.76
556 // (before there was beginBranch and endBranch)
557 if (!a.value( "beginID").isEmpty() )
559 if (!a.value( "endID").isEmpty() )
561 TreeItem *beginBI=model->findID (a.value( "beginID"));
562 TreeItem *endBI=model->findID (a.value( "endID"));
563 if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
565 XLinkItem *xli=model->createXLink (lastBranch,true);
566 xli->setBegin ( (BranchItem*)beginBI );
567 xli->setEnd ( (BranchItem*)endBI);
570 if (!a.value( "color").isEmpty() )
573 col.setNamedColor(a.value("color"));
577 if (!a.value( "width").isEmpty() )
580 xli->setWidth(a.value ("width").toInt (&okx, 10));
586 return true; // xLinks can only be established at the "end branch", return true
589 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
591 for (int i=1; i<=a.count(); i++)
592 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
596 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
598 if (!a.value( "key").isEmpty() )
600 if (!a.value( "value").isEmpty() )
601 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));