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 //FIXME-2 lastBranch->setColor(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 model->createBranch();
233 lastBranchItem=model->getSelectedBranchItem();
234 readBranchAttr (atts);
235 } else if ( eName == "htmlnote" && state == StateBranch)
240 if (!atts.value( "fonthint").isEmpty() )
241 no.setFontHint(atts.value ("fonthint") );
242 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
246 if (!readFrameAttr(atts)) return false;
247 } else if ( eName == "xlink" && state == StateBranch )
249 state=StateBranchXLink;
250 if (!readXLinkAttr (atts)) return false;
251 } else if ( eName == "branch" && state == StateBranch )
253 model->createBranch();
254 lastBranchItem=model->getSelectedBranchItem();
255 readBranchAttr (atts);
256 } else if ( eName == "html" && state == StateHtmlNote )
262 } else if ( state == StateHtml )
264 // accept all while in html mode,
269 return false; // Error
273 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
276 cout << "endElement </" <<qPrintable(eName)
278 <<" laststate=" <<laststate
279 <<" stateStack="<<stateStack.last()
285 mainWindow->removeProgressBar();
288 model->selectParent();
289 lastBranchItem=model->getSelectedBranchItem();
292 htmldata+="</"+eName+">";
296 htmldata.replace ("<br></br>","<br />");
297 no.setNote (htmldata);
298 lastBranchItem->setNoteObj (no);
304 state=stateStack.takeLast();
308 bool parseVYMHandler::characters ( const QString& ch)
310 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
312 QString ch_org=quotemeta (ch);
313 QString ch_simplified=ch.simplifyWhiteSpace();
314 if ( ch_simplified.isEmpty() ) return true;
318 case StateInit: break;
319 case StateMap: break;
321 model->select(ch_simplified);
323 case StateMapSetting:break;
324 case StateMapCenter: break;
326 lastBranchItem->setNote(ch_simplified);
328 case StateBranch: break;
329 case StateStandardFlag:
330 //FIXME-2 lastBranchItem->activateStandardFlag(ch_simplified);
332 case StateFloatImage: break;
333 case StateHtmlNote: break;
338 model->setHeading(ch_simplified);
346 QString parseVYMHandler::errorString()
348 return "the document is not in the VYM file format";
351 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a) //FIXME-2
354 mainWindow->setProgressValue (branchesCurrent);
356 //lastBranchItem=(BranchItem*)(lastBranch->getTreeItem());
359 if (!readOOAttr(a)) return false;
361 if (!a.value( "scrolled").isEmpty() )
362 lastBranchItem->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);
384 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
390 if (!a.value( "frameType").isEmpty() )
391 lastOO->setFrameType (a.value("frameType"));
392 if (!a.value( "penColor").isEmpty() )
393 lastOO->setFramePenColor (a.value("penColor"));
394 if (!a.value( "brushColor").isEmpty() )
395 lastOO->setFrameBrushColor (a.value("brushColor"));
396 if (!a.value( "padding").isEmpty() )
398 x=a.value("padding").toInt(&ok);
399 if (ok) lastOO->setFramePadding(x);
401 if (!a.value( "borderWidth").isEmpty() )
403 x=a.value("borderWidth").toInt(&ok);
404 if (ok) lastOO->setFrameBorderWidth(x);
410 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
416 if (!a.value( "relPosX").isEmpty() )
418 if (!a.value( "relPosY").isEmpty() )
420 x=a.value("relPosX").toFloat (&okx);
421 y=a.value("relPosY").toFloat (&oky);
424 lastOO->setUseRelPos (true);
425 lastOO->move2RelPos (x,y);
428 return false; // Couldn't read relPos
431 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap )
433 if (!a.value( "absPosY").isEmpty() )
435 x=a.value("absPosX").toFloat (&okx);
436 y=a.value("absPosY").toFloat (&oky);
440 return false; // Couldn't read absPos
443 if (!a.value( "id").isEmpty() )
444 lastOO->setID (a.value ("id"));
445 if (!a.value( "url").isEmpty() )
446 lastOO->setURL (a.value ("url"));
447 if (!a.value( "vymLink").isEmpty() )
448 lastOO->setVymLink (a.value ("vymLink"));
449 if (!a.value( "hideInExport").isEmpty() )
450 if (a.value("hideInExport")=="true")
451 lastOO->getTreeItem()->setHideInExport(true);
453 if (!a.value( "hideLink").isEmpty())
455 if (a.value ("hideLink") =="true")
456 lastOO->setHideLinkUnselected(true);
458 lastOO->setHideLinkUnselected(false);
464 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
465 { // only for backward compatibility (<1.4.6). Use htmlnote now.
468 if (!a.value( "href").isEmpty() )
471 fn=parseHREF(a.value ("href") );
473 QString s; // Reading a note
475 if ( !file.open( QIODevice::ReadOnly) )
477 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
480 QTextStream stream( &file );
482 while ( !stream.atEnd() ) {
483 lines += stream.readLine()+"\n";
487 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
490 if (!a.value( "fonthint").isEmpty() )
491 no.setFontHint(a.value ("fonthint") );
492 lastBranchItem->setNoteObj(no);
496 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
500 //if (!readOOAttr(a)) return false;
502 if (!a.value( "useOrientation").isEmpty() )
504 if (a.value ("useOrientation") =="true")
505 lastFloat->setUseOrientation (true);
507 lastFloat->setUseOrientation (false);
509 if (!a.value( "href").isEmpty() )
512 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
514 QMessageBox::warning( 0, "Warning: " ,
515 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
516 //FIXME-2 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
522 if (!a.value( "floatExport").isEmpty() )
524 // Only for compatibility. THis is not used since 1.7.11
525 if (a.value ("floatExport") =="true")
526 lastFloat->setFloatExport(true);
528 lastFloat->setFloatExport (false);
530 if (!a.value( "zPlane").isEmpty() )
531 lastFloat->setZValue (a.value("zPlane").toInt ());
534 if (!a.value( "relPosX").isEmpty() )
536 if (!a.value( "relPosY").isEmpty() )
539 x=a.value("relPosX").toFloat (&okx);
540 y=a.value("relPosY").toFloat (&oky);
544 lastFloat->setRelPos (QPointF (x,y) );
545 // make sure floats in mapcenter are repositioned to relative pos
546 //FIXME-2 if (lastBranchItem->depth()==0) lastBranch->positionContents();
549 // Couldn't read relPos
554 if (!readOOAttr(a)) return false;
556 if (!a.value ("orgName").isEmpty() )
558 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
563 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
568 XLinkObj *xlo=new XLinkObj (model->getScene());
569 if (!a.value( "color").isEmpty() )
571 col.setNamedColor(a.value("color"));
575 if (!a.value( "width").isEmpty() )
577 xlo->setWidth(a.value ("width").toInt (&okx, 10));
580 // Connecting by select string for compatibility with version < 1.8.76
581 if (!a.value( "beginBranch").isEmpty() )
583 if (!a.value( "endBranch").isEmpty() )
585 LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
586 if (lmo && typeid (*lmo)==typeid (BranchObj))
588 xlo->setBegin ((BranchObj*)lmo);
589 lmo=model->findObjBySelect (a.value( "endBranch"));
590 if (lmo && typeid (*lmo)==typeid (BranchObj))
592 xlo->setEnd ((BranchObj*)(lmo));
600 // object ID is used starting in version 1.8.76
601 if (!a.value( "beginID").isEmpty() )
603 if (!a.value( "endID").isEmpty() )
605 LinkableMapObj *lmo=model->findID (a.value( "beginID"));
606 if (lmo && typeid (*lmo)==typeid (BranchObj))
608 xlo->setBegin ((BranchObj*)lmo);
609 lmo=model->findID (a.value( "endID"));
610 if (lmo && typeid (*lmo)==typeid (BranchObj))
612 xlo->setEnd ((BranchObj*)(lmo));
619 if (!success) delete (xlo);
620 return true; // xLinks can only be established at the "end branch", return true
623 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
625 for (int i=1; i<=a.count(); i++)
626 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
630 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
632 if (!a.value( "key").isEmpty() )
634 if (!a.value( "value").isEmpty() )
635 settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));