9 #include "branchitem.h"
12 #include "linkablemapobj.h"
13 #include "mainwindow.h"
16 static FloatObj *lastFloat;
17 static OrnamentedObj *lastOO;
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 )
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
144 model->createMapCenter();
145 lastBranch=model->getSelectedBranch();
148 // Treat the found mapcenter as a branch
149 // in an existing map
150 LinkableMapObj* lmo=model->getSelectedLMO();
151 if (lmo && ( (typeid(*lmo) == typeid(BranchObj) )
152 || (typeid(*lmo) == typeid(MapCenterObj) ) ) )
154 lastBranch=(BranchObj*)lmo;
155 if (loadMode==ImportAdd)
157 model->createBranch();
158 lastBranch=model->getSelectedBranch();
164 readBranchAttr (atts);
166 (eName == "standardflag" ||eName == "standardFlag") &&
167 (state == StateMapCenter || state==StateBranch))
169 state=StateStandardFlag;
170 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
174 if (!atts.value( "textColor").isEmpty() )
176 col.setNamedColor(atts.value("textColor"));
177 lastBranch->setColor(col );
179 } else if ( eName == "note" &&
180 (state == StateMapCenter ||state==StateBranch))
181 { // only for backward compatibility (<1.4.6). Use htmlnote now.
183 if (!readNoteAttr (atts) ) return false;
184 } else if ( eName == "htmlnote" && state == StateMapCenter)
188 } else if ( eName == "floatimage" &&
189 (state == StateMapCenter ||state==StateBranch))
191 state=StateFloatImage;
192 lastBranch->addFloatImage();
193 lastFloat=lastBranch->getLastFloatImage();
194 if (!readFloatImageAttr(atts)) return false;
195 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
197 // This is used in vymparts, which have no mapcenter!
199 LinkableMapObj* lmo=model->getSelectedLMO();
202 // If a vym part is _loaded_ (not imported),
203 // selection==lmo==NULL
204 // Treat it like ImportAdd then...
206 // FIXME-3 lmo=model->first()->getLMO();
207 // Do we really have no MCO when loading?????
208 cout << "xml-vym aborted\n";
210 if (lmo && ( (typeid(*lmo) == typeid(BranchObj) )
211 || (typeid(*lmo) == typeid(MapCenterObj) ) ) )
213 lastBranch=(BranchObj*)(lmo);
217 if (loadMode==ImportAdd)
219 model->createBranch();
220 lastBranch=model->getSelectedBranch();
224 readBranchAttr (atts);
225 } else if (eName=="floatimage")
227 state=StateFloatImage;
228 lastBranch->addFloatImage();
229 lastFloat=lastBranch->getLastFloatImage();
230 if (!readFloatImageAttr(atts)) return false;
233 } else if ( eName == "branch" && state == StateMapCenter)
236 model->createBranch();
237 lastBranch=model->getSelectedBranch();
238 readBranchAttr (atts);
239 } else if ( eName == "htmlnote" && state == StateBranch)
244 if (!atts.value( "fonthint").isEmpty() )
245 no.setFontHint(atts.value ("fonthint") );
246 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
250 if (!readFrameAttr(atts)) return false;
251 } else if ( eName == "xlink" && state == StateBranch )
253 state=StateBranchXLink;
254 if (!readXLinkAttr (atts)) return false;
255 } else if ( eName == "branch" && state == StateBranch )
257 model->createBranch();
258 lastBranch=model->getSelectedBranch();
259 readBranchAttr (atts);
260 } else if ( eName == "html" && state == StateHtmlNote )
266 } else if ( state == StateHtml )
268 // accept all while in html mode,
273 return false; // Error
277 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
280 cout << "endElement </" <<qPrintable(eName)
282 <<" laststate=" <<laststate
283 <<" stateStack="<<stateStack.last()
289 mainWindow->removeProgressBar();
292 model->selectParent();
293 lastBranch=model->getSelectedBranch();
296 htmldata+="</"+eName+">";
300 htmldata.replace ("<br></br>","<br />");
301 no.setNote (htmldata);
302 lastBranch->getTreeItem()->setNoteObj (no);
308 state=stateStack.takeLast();
312 bool parseVYMHandler::characters ( const QString& ch)
314 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
316 QString ch_org=quotemeta (ch);
317 QString ch_simplified=ch.simplifyWhiteSpace();
318 if ( ch_simplified.isEmpty() ) return true;
322 case StateInit: break;
323 case StateMap: break;
325 model->select(ch_simplified);
327 case StateMapSetting:break;
328 case StateMapCenter: break;
330 lastBranch->getTreeItem()->setNote(ch_simplified);
332 case StateBranch: break;
333 case StateStandardFlag:
334 lastBranch->activateStandardFlag(ch_simplified);
336 case StateFloatImage: break;
337 case StateHtmlNote: break;
342 model->setHeading(ch_simplified);
350 QString parseVYMHandler::errorString()
352 return "the document is not in the VYM file format";
355 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
358 mainWindow->setProgressValue (branchesCurrent);
360 lastBranchItem=(BranchItem*)(lastBranch->getTreeItem());
362 if (!readOOAttr(a)) return false;
364 if (!a.value( "scrolled").isEmpty() )
365 lastBranchItem->toggleScroll();
366 if (!a.value( "frameType").isEmpty() )
367 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
369 if (!a.value( "incImgV").isEmpty() )
371 if (a.value("incImgV")=="true")
372 lastBranch->setIncludeImagesVer(true);
374 lastBranch->setIncludeImagesVer(false);
376 if (!a.value( "incImgH").isEmpty() )
378 if (a.value("incImgH")=="true")
379 lastBranch->setIncludeImagesHor(true);
381 lastBranch->setIncludeImagesHor(false);
386 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
392 if (!a.value( "frameType").isEmpty() )
393 lastOO->setFrameType (a.value("frameType"));
394 if (!a.value( "penColor").isEmpty() )
395 lastOO->setFramePenColor (a.value("penColor"));
396 if (!a.value( "brushColor").isEmpty() )
397 lastOO->setFrameBrushColor (a.value("brushColor"));
398 if (!a.value( "padding").isEmpty() )
400 x=a.value("padding").toInt(&ok);
401 if (ok) lastOO->setFramePadding(x);
403 if (!a.value( "borderWidth").isEmpty() )
405 x=a.value("borderWidth").toInt(&ok);
406 if (ok) lastOO->setFrameBorderWidth(x);
412 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
418 if (!a.value( "relPosX").isEmpty() )
420 if (!a.value( "relPosY").isEmpty() )
422 x=a.value("relPosX").toFloat (&okx);
423 y=a.value("relPosY").toFloat (&oky);
426 lastOO->setUseRelPos (true);
427 lastOO->move2RelPos (x,y);
430 return false; // Couldn't read relPos
433 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
435 if (!a.value( "absPosY").isEmpty() )
437 x=a.value("absPosX").toFloat (&okx);
438 y=a.value("absPosY").toFloat (&oky);
442 return false; // Couldn't read absPos
445 if (!a.value( "id").isEmpty() )
446 lastOO->setID (a.value ("id"));
447 if (!a.value( "url").isEmpty() )
448 lastOO->setURL (a.value ("url"));
449 if (!a.value( "vymLink").isEmpty() )
450 lastOO->setVymLink (a.value ("vymLink"));
451 if (!a.value( "hideInExport").isEmpty() )
452 if (a.value("hideInExport")=="true")
453 lastOO->setHideInExport(true);
455 if (!a.value( "hideLink").isEmpty())
457 if (a.value ("hideLink") =="true")
458 lastOO->setHideLinkUnselected(true);
460 lastOO->setHideLinkUnselected(false);
466 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
467 { // only for backward compatibility (<1.4.6). Use htmlnote now.
470 if (!a.value( "href").isEmpty() )
473 fn=parseHREF(a.value ("href") );
475 QString s; // Reading a note
477 if ( !file.open( QIODevice::ReadOnly) )
479 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
482 QTextStream stream( &file );
484 while ( !stream.atEnd() ) {
485 lines += stream.readLine()+"\n";
489 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
492 if (!a.value( "fonthint").isEmpty() )
493 no.setFontHint(a.value ("fonthint") );
494 lastBranch->getTreeItem()->setNoteObj(no);
498 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
502 //if (!readOOAttr(a)) return false;
504 if (!a.value( "useOrientation").isEmpty() )
506 if (a.value ("useOrientation") =="true")
507 lastFloat->setUseOrientation (true);
509 lastFloat->setUseOrientation (false);
511 if (!a.value( "href").isEmpty() )
514 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
516 QMessageBox::warning( 0, "Warning: " ,
517 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
518 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
524 if (!a.value( "floatExport").isEmpty() )
526 // Only for compatibility. THis is not used since 1.7.11
527 if (a.value ("floatExport") =="true")
528 lastFloat->setFloatExport(true);
530 lastFloat->setFloatExport (false);
532 if (!a.value( "zPlane").isEmpty() )
533 lastFloat->setZValue (a.value("zPlane").toInt ());
536 if (!a.value( "relPosX").isEmpty() )
538 if (!a.value( "relPosY").isEmpty() )
541 x=a.value("relPosX").toFloat (&okx);
542 y=a.value("relPosY").toFloat (&oky);
546 lastFloat->setRelPos (QPointF (x,y) );
547 // make sure floats in mapcenter are repositioned to relative pos
548 if (lastBranch->getDepth()==0) lastBranch->positionContents();
551 // Couldn't read relPos
556 if (!readOOAttr(a)) return false;
558 if (!a.value ("orgName").isEmpty() )
560 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
565 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
570 XLinkObj *xlo=new XLinkObj (model->getScene());
571 if (!a.value( "color").isEmpty() )
573 col.setNamedColor(a.value("color"));
577 if (!a.value( "width").isEmpty() )
579 xlo->setWidth(a.value ("width").toInt (&okx, 10));
582 // Connecting by select string for compatibility with version < 1.8.76
583 if (!a.value( "beginBranch").isEmpty() )
585 if (!a.value( "endBranch").isEmpty() )
587 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
588 if (lmo && typeid (*lmo)==typeid (BranchObj))
590 xlo->setBegin ((BranchObj*)lmo);
591 lmo=model->findObjBySelect (a.value( "endBranch"));
592 if (lmo && typeid (*lmo)==typeid (BranchObj))
594 xlo->setEnd ((BranchObj*)(lmo));
602 // object ID is used starting in version 1.8.76
603 if (!a.value( "beginID").isEmpty() )
605 if (!a.value( "endID").isEmpty() )
607 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
608 if (lmo && typeid (*lmo)==typeid (BranchObj))
610 xlo->setBegin ((BranchObj*)lmo);
611 lmo=model->findID (a.value( "endID"));
612 if (lmo && typeid (*lmo)==typeid (BranchObj))
614 xlo->setEnd ((BranchObj*)(lmo));
621 if (!success) delete (xlo);
622 return true; // xLinks can only be established at the "end branch", return true
625 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
627 for (int i=1; i<=a.count(); i++)
628 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
632 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
634 if (!a.value( "key").isEmpty() )
636 if (!a.value( "value").isEmpty() )
637 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));