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 )
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 lastBranchItem=(BranchItem*)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 model->createBranch();
155 lastBranchItem=model->getSelectedBranchItem();
157 //FIXME-3 lastBranch->clear();
161 readBranchAttr (atts);
163 (eName == "standardflag" ||eName == "standardFlag") &&
164 (state == StateMapCenter || state==StateBranch))
166 state=StateStandardFlag;
167 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
171 if (!atts.value( "textColor").isEmpty() )
173 col.setNamedColor(atts.value("textColor"));
174 lastBranchItem->setHeadingColor(col );
176 } else if ( eName == "note" &&
177 (state == StateMapCenter ||state==StateBranch))
178 { // only for backward compatibility (<1.4.6). Use htmlnote now.
180 if (!readNoteAttr (atts) ) return false;
181 } else if ( eName == "htmlnote" && state == StateMapCenter)
185 } else if ( eName == "floatimage" &&
186 (state == StateMapCenter ||state==StateBranch))
188 state=StateFloatImage;
189 //FIXME-2lastBranch->addFloatImage();
190 //FIXME-2lastFloat=lastBranch->getLastFloatImage();
191 if (!readFloatImageAttr(atts)) return false;
192 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
194 // This is used in vymparts, which have no mapcenter!
196 TreeItem *ti=model->getSelectedItem();
199 // If a vym part is _loaded_ (not imported),
200 // selection==lmo==NULL
201 // Treat it like ImportAdd then...
203 // FIXME-3 lmo=model->first()->getLMO();
204 // Do we really have no MCO when loading?????
205 cout << "xml-vym aborted\n";
207 if (ti && ti->isBranchLikeType() )
209 lastBranchItem=(BranchItem*)ti;
213 if (loadMode==ImportAdd)
215 model->createBranch();
216 lastBranchItem=model->getSelectedBranchItem();
219 //FIXME-2 lastBranch->clear();
220 readBranchAttr (atts);
221 } else if (eName=="floatimage")
223 state=StateFloatImage;
224 //FIXME-2 lastBranch->addFloatImage();
225 //FIXME-2 lastFloat=lastBranch->getLastFloatImage();
226 if (!readFloatImageAttr(atts)) return false;
229 } else if ( eName == "branch" && state == StateMapCenter)
232 lastBranchItem=model->createBranch();
233 readBranchAttr (atts);
234 } else if ( eName == "htmlnote" && state == StateBranch)
239 if (!atts.value( "fonthint").isEmpty() )
240 no.setFontHint(atts.value ("fonthint") );
241 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
245 if (!readFrameAttr(atts)) return false;
246 } else if ( eName == "xlink" && state == StateBranch )
248 state=StateBranchXLink;
249 if (!readXLinkAttr (atts)) return false;
250 } else if ( eName == "branch" && state == StateBranch )
252 lastBranchItem=model->createBranch();
253 readBranchAttr (atts);
254 } else if ( eName == "html" && state == StateHtmlNote )
260 } else if ( state == StateHtml )
262 // accept all while in html mode,
267 return false; // Error
271 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
274 cout << "endElement </" <<qPrintable(eName)
276 <<" laststate=" <<laststate
277 <<" stateStack="<<stateStack.last()
283 mainWindow->removeProgressBar();
286 model->selectParent();
287 model->emitDataHasChanged (lastBranchItem);
288 lastBranchItem=model->getSelectedBranchItem();
289 lastBranchItem->setLastSelectedBranch (0); // Reset last selected to first child branch
292 model->selectParent();
293 model->emitDataHasChanged (lastBranchItem);
294 lastBranchItem=model->getSelectedBranchItem();
295 lastBranchItem->setLastSelectedBranch (0); // Reset last selected to first child branch
298 htmldata+="</"+eName+">";
302 htmldata.replace ("<br></br>","<br />");
303 no.setNote (htmldata);
304 lastBranchItem->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 lastBranchItem->setNote(ch_simplified);
334 case StateBranch: break;
335 case StateStandardFlag:
336 lastBranchItem->activateStandardFlag(ch_simplified);
338 case StateFloatImage: break;
339 case StateHtmlNote: break;
344 model->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) //FIXME-2
360 mainWindow->setProgressValue (branchesCurrent);
362 if (!readOOAttr(a)) return false;
364 if (!a.value( "scrolled").isEmpty() )
365 lastBranchItem->toggleScroll();
367 if (!a.value( "frameType").isEmpty() )
368 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);
388 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
394 if (!a.value( "frameType").isEmpty() )
395 lastOO->setFrameType (a.value("frameType"));
396 if (!a.value( "penColor").isEmpty() )
397 lastOO->setFramePenColor (a.value("penColor"));
398 if (!a.value( "brushColor").isEmpty() )
399 lastOO->setFrameBrushColor (a.value("brushColor"));
400 if (!a.value( "padding").isEmpty() )
402 x=a.value("padding").toInt(&ok);
403 if (ok) lastOO->setFramePadding(x);
405 if (!a.value( "borderWidth").isEmpty() )
407 x=a.value("borderWidth").toInt(&ok);
408 if (ok) lastOO->setFrameBorderWidth(x);
414 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
420 if (!a.value( "relPosX").isEmpty() )
422 if (!a.value( "relPosY").isEmpty() )
424 x=a.value("relPosX").toFloat (&okx);
425 y=a.value("relPosY").toFloat (&oky);
427 lastBranchItem->setRelPos (QPointF(x,y));
429 return false; // Couldn't read relPos
432 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
434 if (!a.value( "absPosY").isEmpty() )
436 x=a.value("absPosX").toFloat (&okx);
437 y=a.value("absPosY").toFloat (&oky);
439 lastBranchItem->setAbsPos (QPointF(x,y));
441 return false; // Couldn't read absPos
444 //if (!a.value( "id").isEmpty() )
445 // lastBranchItem->setID (a.value ("id"));
447 if (!a.value( "url").isEmpty() )
448 lastBranchItem->setURL (a.value ("url"));
449 if (!a.value( "vymLink").isEmpty() )
450 lastBranchItem->setVymLink (a.value ("vymLink"));
451 if (!a.value( "hideInExport").isEmpty() )
452 if (a.value("hideInExport")=="true")
453 lastBranchItem->setHideInExport(true);
456 if (!a.value( "hideLink").isEmpty())
458 if (a.value ("hideLink") =="true")
459 lastOO->setHideLinkUnselected(true);
461 lastOO->setHideLinkUnselected(false);
468 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
469 { // only for backward compatibility (<1.4.6). Use htmlnote now.
472 if (!a.value( "href").isEmpty() )
475 fn=parseHREF(a.value ("href") );
477 QString s; // Reading a note
479 if ( !file.open( QIODevice::ReadOnly) )
481 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
484 QTextStream stream( &file );
486 while ( !stream.atEnd() ) {
487 lines += stream.readLine()+"\n";
491 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
494 if (!a.value( "fonthint").isEmpty() )
495 no.setFontHint(a.value ("fonthint") );
496 lastBranchItem->setNoteObj(no);
500 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
504 //if (!readOOAttr(a)) return false;
506 if (!a.value( "useOrientation").isEmpty() )
508 if (a.value ("useOrientation") =="true")
509 lastFloat->setUseOrientation (true);
511 lastFloat->setUseOrientation (false);
513 if (!a.value( "href").isEmpty() )
516 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
518 QMessageBox::warning( 0, "Warning: " ,
519 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
520 //FIXME-2 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
526 if (!a.value( "floatExport").isEmpty() )
528 // Only for compatibility. THis is not used since 1.7.11
529 if (a.value ("floatExport") =="true")
530 lastFloat->setFloatExport(true);
532 lastFloat->setFloatExport (false);
534 if (!a.value( "zPlane").isEmpty() )
535 lastFloat->setZValue (a.value("zPlane").toInt ());
538 if (!a.value( "relPosX").isEmpty() )
540 if (!a.value( "relPosY").isEmpty() )
543 x=a.value("relPosX").toFloat (&okx);
544 y=a.value("relPosY").toFloat (&oky);
548 lastFloat->setRelPos (QPointF (x,y) );
549 // make sure floats in mapcenter are repositioned to relative pos
550 //FIXME-2 if (lastBranchItem->depth()==0) lastBranch->positionContents();
553 // Couldn't read relPos
558 if (!readOOAttr(a)) return false;
560 if (!a.value ("orgName").isEmpty() )
562 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
567 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
572 XLinkObj *xlo=new XLinkObj (model->getScene());
573 if (!a.value( "color").isEmpty() )
575 col.setNamedColor(a.value("color"));
579 if (!a.value( "width").isEmpty() )
581 xlo->setWidth(a.value ("width").toInt (&okx, 10));
584 // Connecting by select string for compatibility with version < 1.8.76
585 if (!a.value( "beginBranch").isEmpty() )
587 if (!a.value( "endBranch").isEmpty() )
589 TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
590 if (ti && ti->isBranchLikeType())
593 xlo->setBegin ((BranchObj*)lmo);
594 lmo=model->findBySelectString (a.value( "endBranch"));
595 if (lmo && typeid (*lmo)==typeid (BranchObj))
597 xlo->setEnd ((BranchObj*)(lmo));
606 // object ID is used starting in version 1.8.76
608 if (!a.value( "beginID").isEmpty() )
610 if (!a.value( "endID").isEmpty() )
612 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
613 if (lmo && typeid (*lmo)==typeid (BranchObj))
615 xlo->setBegin ((BranchObj*)lmo);
616 lmo=model->findID (a.value( "endID"));
617 if (lmo && typeid (*lmo)==typeid (BranchObj))
619 xlo->setEnd ((BranchObj*)(lmo));
627 if (!success) delete (xlo);
628 return true; // xLinks can only be established at the "end branch", return true
631 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
633 for (int i=1; i<=a.count(); i++)
634 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
638 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
640 if (!a.value( "key").isEmpty() )
642 if (!a.value( "value").isEmpty() )
643 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));