9 #include "branchitem.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
16 static ImageItem *lastImageItem;
17 static MapItem *lastMI;
19 extern Main *mainWindow;
20 extern Settings settings;
21 extern QString vymVersion;
23 bool parseVYMHandler::startDocument()
27 laststate = StateInit;
29 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=branchesCurrent=0;
53 if (loadMode==NewMap )
59 if (!atts.value( "author").isEmpty() )
60 model->setAuthor(atts.value( "author" ) );
61 if (!atts.value( "comment").isEmpty() )
62 model->setComment (atts.value( "comment" ) );
63 if (!atts.value( "branchCount").isEmpty() )
65 branchesTotal=atts.value("branchCount").toInt();
68 mainWindow->setProgressMinimum (0);
69 mainWindow->setProgressMaximum (branchesTotal);
70 mainWindow->setProgressValue(0);
74 mainWindow->setProgressMinimum (0);
75 mainWindow->setProgressMaximum (0);
76 mainWindow->setProgressValue(0);
80 if (!atts.value( "backgroundColor").isEmpty() )
82 col.setNamedColor(atts.value("backgroundColor"));
83 model->getScene()->setBackgroundBrush(col);
85 if (!atts.value( "selectionColor").isEmpty() )
87 col.setNamedColor(atts.value("selectionColor"));
88 model->setSelectionColor(col);
90 if (!atts.value( "linkColorHint").isEmpty() )
92 if (atts.value("linkColorHint")=="HeadingColor")
93 model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
95 model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
97 if (!atts.value( "linkStyle").isEmpty() )
98 model->setMapLinkStyle(atts.value("linkStyle"));
99 if (!atts.value( "linkColor").isEmpty() )
101 col.setNamedColor(atts.value("linkColor"));
102 model->setMapDefLinkColor(col);
104 if (!atts.value( "defXLinkColor").isEmpty() )
106 col.setNamedColor(atts.value("defXLinkColor"));
107 model->setMapDefXLinkColor(col);
109 if (!atts.value( "defXLinkWidth").isEmpty() )
110 model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
113 if (!atts.value( "version").isEmpty() )
115 if (!checkVersion(atts.value("version")))
116 QMessageBox::warning( 0, "Warning: Version Problem" ,
117 "<h3>Map is newer than VYM</h3>"
118 "<p>The map you are just trying to load was "
119 "saved using vym " +atts.value("version")+". "
120 "The version of this vym is " + vymVersion +
121 ". If you run into problems after pressing "
122 "the ok-button below, updating vym should help.");
124 model->setVersion(atts.value( "version" ));
128 } else if ( eName == "select" && state == StateMap )
130 state=StateMapSelect;
131 } else if ( eName == "setting" && state == StateMap )
133 state=StateMapSetting;
134 if (loadMode==NewMap)
135 readSettingAttr (atts);
136 } else if ( eName == "mapcenter" && state == StateMap )
138 state=StateMapCenter;
139 if (loadMode==NewMap)
141 // 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->getSelectedBranchItem();
152 if (loadMode==ImportAdd)
154 lastBranch=model->createBranch(lastBranch);
156 //FIXME-3 lastBranch->clear();
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!
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";
205 if (ti && ti->isBranchLikeType() )
207 lastBranch=(BranchItem*)ti;
211 if (loadMode==ImportAdd)
213 lastBranch=model->createBranch(lastBranch);
216 //FIXME-2 lastBranch->clear();
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()
279 mainWindow->removeProgressBar();
282 model->selectParent(); // FIXME-3 really needed to "select"? Maybe optimize...
283 model->emitDataHasChanged (lastBranch);
284 lastBranch=(BranchItem*)(lastBranch->parent());
285 lastBranch->setLastSelectedBranch (0); // Reset last selected to first child branch
288 model->selectParent();// FIXME-3 really needed to "select"? Maybe optimize...
289 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) //FIXME-2
355 mainWindow->setProgressValue (branchesCurrent++);
359 if (!readOOAttr(a)) return false;
361 if (!a.value( "scrolled").isEmpty() )
362 lastBranch->toggleScroll(); //FIXME-3 in endElement unscroll again, if branch is still empty
363 // (interesting for import of KDE bookmarks)
365 if (!a.value( "frameType").isEmpty() )
366 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
368 if (!a.value( "incImgV").isEmpty() )
370 if (a.value("incImgV")=="true")
371 lastBranch->setIncludeImagesVer(true);
373 lastBranch->setIncludeImagesVer(false);
375 if (!a.value( "incImgH").isEmpty() )
377 if (a.value("incImgH")=="true")
378 lastBranch->setIncludeImagesHor(true);
380 lastBranch->setIncludeImagesHor(false);
386 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a) // FIXME-4 does not work if there is no lmo for treeitem
390 OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO());
396 if (!a.value( "frameType").isEmpty() )
397 oo->setFrameType (a.value("frameType"));
398 if (!a.value( "penColor").isEmpty() )
399 oo->setFramePenColor (a.value("penColor"));
400 if (!a.value( "brushColor").isEmpty() )
401 oo->setFrameBrushColor (a.value("brushColor"));
402 if (!a.value( "padding").isEmpty() )
404 x=a.value("padding").toInt(&ok);
405 if (ok) oo->setFramePadding(x);
407 if (!a.value( "borderWidth").isEmpty() )
409 x=a.value("borderWidth").toInt(&ok);
410 if (ok) oo->setFrameBorderWidth(x);
419 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
425 if (!a.value( "relPosX").isEmpty() )
427 if (!a.value( "relPosY").isEmpty() )
429 x=a.value("relPosX").toFloat (&okx);
430 y=a.value("relPosY").toFloat (&oky);
432 lastMI->setRelPos (QPointF(x,y));
434 return false; // Couldn't read relPos
437 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
439 if (!a.value( "absPosY").isEmpty() )
441 x=a.value("absPosX").toFloat (&okx);
442 y=a.value("absPosY").toFloat (&oky);
444 lastMI->setAbsPos (QPointF(x,y));
446 return false; // Couldn't read absPos
449 //if (!a.value( "id").isEmpty() )
450 // lastMI->setID (a.value ("id")); // FIXME-3
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; FIXME-3
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)
554 XLinkObj *xlo=new XLinkObj (model->getScene());
555 if (!a.value( "color").isEmpty() )
557 col.setNamedColor(a.value("color"));
561 if (!a.value( "width").isEmpty() )
563 xlo->setWidth(a.value ("width").toInt (&okx, 10));
566 // Connecting by select string for compatibility with version < 1.8.76
567 if (!a.value( "beginBranch").isEmpty() )
569 if (!a.value( "endBranch").isEmpty() )
571 TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
572 if (ti && ti->isBranchLikeType())
575 xlo->setBegin ((BranchObj*)lmo);
576 lmo=model->findBySelectString (a.value( "endBranch"));
577 if (lmo && typid (*lmo)==typid (BranchObj))
579 xlo->setEnd ((BranchObj*)(lmo));
588 // object ID is used starting in version 1.8.76
590 if (!a.value( "beginID").isEmpty() )
592 if (!a.value( "endID").isEmpty() )
594 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
595 if (lmo && typid (*lmo)==typid (BranchObj))
597 xlo->setBegin ((BranchObj*)lmo);
598 lmo=model->findID (a.value( "endID"));
599 if (lmo && typid (*lmo)==typid (BranchObj))
601 xlo->setEnd ((BranchObj*)(lmo));
609 if (!success) delete (xlo);
610 return true; // xLinks can only be established at the "end branch", return true
613 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
615 for (int i=1; i<=a.count(); i++)
616 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
620 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
622 if (!a.value( "key").isEmpty() )
624 if (!a.value( "value").isEmpty() )
625 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));