11 #include "linkablemapobj.h"
12 #include "mainwindow.h"
15 static BranchObj *lastBranch;
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 )
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( "countBranches").isEmpty() )
65 branchesTotal=atts.value("countBranches").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 model->createMapCenter();
144 lastBranch=model->getSelectedBranch();
147 // Treat the found mapcenter as a branch
148 // in an existing map
149 LinkableMapObj* lmo=model->getSelectedLMO();
150 if (lmo && ( (typeid(*lmo) == typeid(BranchObj) )
151 || (typeid(*lmo) == typeid(MapCenterObj) ) ) )
153 lastBranch=(BranchObj*)lmo;
154 if (loadMode==ImportAdd)
156 model->createBranch();
157 lastBranch=model->getSelectedBranch();
163 readBranchAttr (atts);
165 (eName == "standardflag" ||eName == "standardFlag") &&
166 (state == StateMapCenter || state==StateBranch))
168 state=StateStandardFlag;
169 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
173 if (!atts.value( "textColor").isEmpty() )
175 col.setNamedColor(atts.value("textColor"));
176 lastBranch->setColor(col );
178 } else if ( eName == "note" &&
179 (state == StateMapCenter ||state==StateBranch))
180 { // only for backward compatibility (<1.4.6). Use htmlnote now.
182 if (!readNoteAttr (atts) ) return false;
183 } else if ( eName == "htmlnote" && state == StateMapCenter)
187 } else if ( eName == "floatimage" &&
188 (state == StateMapCenter ||state==StateBranch))
190 state=StateFloatImage;
191 lastBranch->addFloatImage();
192 lastFloat=lastBranch->getLastFloatImage();
193 if (!readFloatImageAttr(atts)) return false;
194 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
196 // This is used in vymparts, which have no mapcenter!
198 LinkableMapObj* lmo=model->getSelectedLMO();
201 // If a vym part is _loaded_ (not imported),
202 // selection==lmo==NULL
203 // Treat it like ImportAdd then...
205 // FIXME-3 lmo=model->first()->getLMO();
206 // Do we really have no MCO when loading?????
207 cout << "xml-vym aborted\n";
209 if (lmo && ( (typeid(*lmo) == typeid(BranchObj) )
210 || (typeid(*lmo) == typeid(MapCenterObj) ) ) )
212 lastBranch=(BranchObj*)(lmo);
216 if (loadMode==ImportAdd)
218 model->createBranch();
219 lastBranch=model->getSelectedBranch();
223 readBranchAttr (atts);
224 } else if (eName=="floatimage")
226 state=StateFloatImage;
227 lastBranch->addFloatImage();
228 lastFloat=lastBranch->getLastFloatImage();
229 if (!readFloatImageAttr(atts)) return false;
232 } else if ( eName == "branch" && state == StateMapCenter)
235 model->createBranch();
236 lastBranch=model->getSelectedBranch();
237 readBranchAttr (atts);
238 } else if ( eName == "htmlnote" && state == StateBranch)
243 if (!atts.value( "fonthint").isEmpty() )
244 no.setFontHint(atts.value ("fonthint") );
245 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
249 if (!readFrameAttr(atts)) return false;
250 } else if ( eName == "xlink" && state == StateBranch )
252 state=StateBranchXLink;
253 if (!readXLinkAttr (atts)) return false;
254 } else if ( eName == "branch" && state == StateBranch )
256 model->createBranch();
257 lastBranch=model->getSelectedBranch();
258 readBranchAttr (atts);
259 } else if ( eName == "html" && state == StateHtmlNote )
265 } else if ( state == StateHtml )
267 // accept all while in html mode,
272 return false; // Error
276 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
279 cout << "endElement </" <<qPrintable(eName)
281 <<" laststate=" <<laststate
282 <<" stateStack="<<stateStack.last()
288 mainWindow->removeProgressBar();
291 model->selectParent();
292 lastBranch=model->getSelectedBranch();
295 htmldata+="</"+eName+">";
299 htmldata.replace ("<br></br>","<br />");
300 no.setNote (htmldata);
301 lastBranch->getTreeItem()->setNoteObj (no);
307 state=stateStack.takeLast();
311 bool parseVYMHandler::characters ( const QString& ch)
313 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
315 QString ch_org=quotemeta (ch);
316 QString ch_simplified=ch.simplifyWhiteSpace();
317 if ( ch_simplified.isEmpty() ) return true;
321 case StateInit: break;
322 case StateMap: break;
324 model->select(ch_simplified);
326 case StateMapSetting:break;
327 case StateMapCenter: break;
329 lastBranch->getTreeItem()->setNote(ch_simplified);
331 case StateBranch: break;
332 case StateStandardFlag:
333 lastBranch->activateStandardFlag(ch_simplified);
335 case StateFloatImage: break;
336 case StateHtmlNote: break;
341 model->setHeading(ch_simplified);
349 QString parseVYMHandler::errorString()
351 return "the document is not in the VYM file format";
354 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
357 mainWindow->setProgressValue (branchesCurrent);
359 if (!readOOAttr(a)) return false;
361 if (!a.value( "scrolled").isEmpty() )
362 lastBranch->toggleScroll();
363 if (!a.value( "frameType").isEmpty() )
364 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
366 if (!a.value( "incImgV").isEmpty() )
368 if (a.value("incImgV")=="true")
369 lastBranch->setIncludeImagesVer(true);
371 lastBranch->setIncludeImagesVer(false);
373 if (!a.value( "incImgH").isEmpty() )
375 if (a.value("incImgH")=="true")
376 lastBranch->setIncludeImagesHor(true);
378 lastBranch->setIncludeImagesHor(false);
383 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
389 if (!a.value( "frameType").isEmpty() )
390 lastOO->setFrameType (a.value("frameType"));
391 if (!a.value( "penColor").isEmpty() )
392 lastOO->setFramePenColor (a.value("penColor"));
393 if (!a.value( "brushColor").isEmpty() )
394 lastOO->setFrameBrushColor (a.value("brushColor"));
395 if (!a.value( "padding").isEmpty() )
397 x=a.value("padding").toInt(&ok);
398 if (ok) lastOO->setFramePadding(x);
400 if (!a.value( "borderWidth").isEmpty() )
402 x=a.value("borderWidth").toInt(&ok);
403 if (ok) lastOO->setFrameBorderWidth(x);
409 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
415 if (!a.value( "relPosX").isEmpty() )
417 if (!a.value( "relPosY").isEmpty() )
419 x=a.value("relPosX").toFloat (&okx);
420 y=a.value("relPosY").toFloat (&oky);
423 lastOO->setUseRelPos (true);
424 lastOO->move2RelPos (x,y);
427 return false; // Couldn't read relPos
430 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
432 if (!a.value( "absPosY").isEmpty() )
434 x=a.value("absPosX").toFloat (&okx);
435 y=a.value("absPosY").toFloat (&oky);
439 return false; // Couldn't read absPos
442 if (!a.value( "id").isEmpty() )
443 lastOO->setID (a.value ("id"));
444 if (!a.value( "url").isEmpty() )
445 lastOO->setURL (a.value ("url"));
446 if (!a.value( "vymLink").isEmpty() )
447 lastOO->setVymLink (a.value ("vymLink"));
448 if (!a.value( "hideInExport").isEmpty() )
449 if (a.value("hideInExport")=="true")
450 lastOO->setHideInExport(true);
452 if (!a.value( "hideLink").isEmpty())
454 if (a.value ("hideLink") =="true")
455 lastOO->setHideLinkUnselected(true);
457 lastOO->setHideLinkUnselected(false);
463 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
464 { // only for backward compatibility (<1.4.6). Use htmlnote now.
467 if (!a.value( "href").isEmpty() )
470 fn=parseHREF(a.value ("href") );
472 QString s; // Reading a note
474 if ( !file.open( QIODevice::ReadOnly) )
476 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
479 QTextStream stream( &file );
481 while ( !stream.atEnd() ) {
482 lines += stream.readLine()+"\n";
486 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
489 if (!a.value( "fonthint").isEmpty() )
490 no.setFontHint(a.value ("fonthint") );
491 lastBranch->getTreeItem()->setNoteObj(no);
495 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
499 //if (!readOOAttr(a)) return false;
501 if (!a.value( "useOrientation").isEmpty() )
503 if (a.value ("useOrientation") =="true")
504 lastFloat->setUseOrientation (true);
506 lastFloat->setUseOrientation (false);
508 if (!a.value( "href").isEmpty() )
511 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
513 QMessageBox::warning( 0, "Warning: " ,
514 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
515 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
521 if (!a.value( "floatExport").isEmpty() )
523 // Only for compatibility. THis is not used since 1.7.11
524 if (a.value ("floatExport") =="true")
525 lastFloat->setFloatExport(true);
527 lastFloat->setFloatExport (false);
529 if (!a.value( "zPlane").isEmpty() )
530 lastFloat->setZValue (a.value("zPlane").toInt ());
533 if (!a.value( "relPosX").isEmpty() )
535 if (!a.value( "relPosY").isEmpty() )
538 x=a.value("relPosX").toFloat (&okx);
539 y=a.value("relPosY").toFloat (&oky);
543 lastFloat->setRelPos (QPointF (x,y) );
544 // make sure floats in mapcenter are repositioned to relative pos
545 if (lastBranch->getDepth()==0) lastBranch->positionContents();
548 // Couldn't read relPos
553 if (!readOOAttr(a)) return false;
555 if (!a.value ("orgName").isEmpty() )
557 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
562 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
567 XLinkObj *xlo=new XLinkObj (model->getScene());
568 if (!a.value( "color").isEmpty() )
570 col.setNamedColor(a.value("color"));
574 if (!a.value( "width").isEmpty() )
576 xlo->setWidth(a.value ("width").toInt (&okx, 10));
579 // Connecting by select string for compatibility with version < 1.8.76
580 if (!a.value( "beginBranch").isEmpty() )
582 if (!a.value( "endBranch").isEmpty() )
584 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
585 if (lmo && typeid (*lmo)==typeid (BranchObj))
587 xlo->setBegin ((BranchObj*)lmo);
588 lmo=model->findObjBySelect (a.value( "endBranch"));
589 if (lmo && typeid (*lmo)==typeid (BranchObj))
591 xlo->setEnd ((BranchObj*)(lmo));
599 // object ID is used starting in version 1.8.76
600 if (!a.value( "beginID").isEmpty() )
602 if (!a.value( "endID").isEmpty() )
604 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
605 if (lmo && typeid (*lmo)==typeid (BranchObj))
607 xlo->setBegin ((BranchObj*)lmo);
608 lmo=model->findID (a.value( "endID"));
609 if (lmo && typeid (*lmo)==typeid (BranchObj))
611 xlo->setEnd ((BranchObj*)(lmo));
618 if (!success) delete (xlo);
619 return true; // xLinks can only be established at the "end branch", return true
622 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
624 for (int i=1; i<=a.count(); i++)
625 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
629 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
631 if (!a.value( "key").isEmpty() )
633 if (!a.value( "value").isEmpty() )
634 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));