1.11.2 split up of xml helper functions. started to work on attributes
11 #include "linkablemapobj.h"
14 static BranchObj *lastBranch;
15 static FloatObj *lastFloat;
16 static OrnamentedObj *lastOO;
18 extern Settings settings;
19 extern QString vymVersion;
22 parseVYMHandler::parseVYMHandler() {}
24 parseVYMHandler::~parseVYMHandler() {}
26 QString parseVYMHandler::errorProtocol() { return errorProt; }
30 bool parseVYMHandler::startDocument()
34 laststate = StateInit;
36 stateStack.append(StateInit);
45 QString parseVYMHandler::parseHREF(QString href)
47 QString type=href.section(":",0,0);
48 QString path=href.section(":",1,1);
49 if (!tmpDir.endsWith("/"))
50 return tmpDir + "/" + path;
55 bool parseVYMHandler::startElement ( const QString&, const QString&,
56 const QString& eName, const QXmlAttributes& atts )
60 cout << "startElement <"<< eName.ascii()
62 <<" laststate="<<stateStack.last()
63 <<" loadMode="<<loadMode
64 <<" line="<<QXmlDefaultHandler::lineNumber()
67 stateStack.append (state);
68 if ( state == StateInit && (eName == "vymmap") )
71 if (!atts.value( "version").isEmpty() )
73 if (!checkVersion(atts.value("version")))
74 QMessageBox::warning( 0, "Warning: Version Problem" ,
75 "<h3>Map is newer than VYM</h3>"
76 "<p>The map you are just trying to load was "
77 "saved using vym " +atts.value("version")+". "
78 "The version of this vym is " + vymVersion +
79 ". If you run into problems after pressing "
80 "the ok-button below, updating vym should help.");
82 mc->setVersion(atts.value( "version" ));
85 if (loadMode==NewMap ||
86 (loadMode==ImportReplace && me->getSelection()==mc))
88 if (!atts.value( "author").isEmpty() )
90 mc->setAuthor(atts.value( "author" ) );
92 if (!atts.value( "comment").isEmpty() )
94 mc->setComment (atts.value( "comment" ) );
96 if (!atts.value( "backgroundColor").isEmpty() )
98 col.setNamedColor(atts.value("backgroundColor"));
99 mc->getScene()->setBackgroundBrush(col);
101 if (!atts.value( "selectionColor").isEmpty() )
103 col.setNamedColor(atts.value("selectionColor"));
104 me->setSelectionColor(col);
106 if (!atts.value( "linkColorHint").isEmpty() )
108 if (atts.value("linkColorHint")=="HeadingColor")
109 me->setMapLinkColorHint(LinkableMapObj::HeadingColor);
111 me->setMapLinkColorHint(LinkableMapObj::DefaultColor);
113 if (!atts.value( "linkStyle").isEmpty() )
115 me->setMapLinkStyle(atts.value("linkStyle"));
117 if (!atts.value( "linkColor").isEmpty() )
119 col.setNamedColor(atts.value("linkColor"));
120 me->setMapDefLinkColor(col);
122 if (!atts.value( "defXLinkColor").isEmpty() )
124 col.setNamedColor(atts.value("defXLinkColor"));
125 me->setMapDefXLinkColor(col);
127 if (!atts.value( "defXLinkWidth").isEmpty() )
129 me->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
132 } else if ( eName == "select" && state == StateMap )
134 state=StateMapSelect;
135 } else if ( eName == "setting" && state == StateMap )
137 state=StateMapSetting;
138 if (loadMode==NewMap)
139 readSettingAttr (atts);
140 } else if ( eName == "mapcenter" && state == StateMap )
142 state=StateMapCenter;
143 if (loadMode==NewMap)
145 // Really use the found mapcenter as MCO in a new map
146 lastBranch=mc; // avoid empty pointer
149 // Treat the found mapcenter as a branch
150 // in an existing map
151 LinkableMapObj* lmo=me->getSelection();
152 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
153 || (typeid(*lmo) == typeid(MapCenterObj) ) )
155 lastBranch=(BranchObj*)lmo;
156 if (loadMode==ImportAdd)
158 lastBranch->addBranch();
159 lastBranch=lastBranch->getLastBranch();
165 readBranchAttr (atts);
167 (eName == "standardflag" ||eName == "standardFlag") &&
168 (state == StateMapCenter || state==StateBranch))
170 state=StateStandardFlag;
171 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
175 if (!atts.value( "textColor").isEmpty() )
177 col.setNamedColor(atts.value("textColor"));
178 lastBranch->setColor(col );
180 } else if ( eName == "note" &&
181 (state == StateMapCenter ||state==StateBranch))
182 { // only for backward compatibility (<1.4.6). Use htmlnote now.
184 if (!readNoteAttr (atts) ) return false;
185 } else if ( eName == "htmlnote" && state == StateMapCenter)
189 } else if ( eName == "floatimage" &&
190 (state == StateMapCenter ||state==StateBranch))
192 state=StateFloatImage;
193 lastBranch->addFloatImage();
194 lastFloat=lastBranch->getLastFloatImage();
195 if (!readFloatImageAttr(atts)) return false;
196 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
198 // This is used in vymparts, which have no mapcenter!
200 LinkableMapObj* lmo=me->getSelection();
203 // If a vym part is _loaded_ (not imported),
204 // selection==lmo==NULL
205 // Treat it like ImportAdd then...
209 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
210 || (typeid(*lmo) == typeid(MapCenterObj) ) )
212 lastBranch=(BranchObj*)(lmo);
216 if (loadMode==ImportAdd)
218 lastBranch->addBranch();
219 lastBranch=lastBranch->getLastBranch();
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)
237 lastBranch->addBranch();
238 lastBranch=lastBranch->getLastBranch();
239 readBranchAttr (atts);
240 } else if ( eName == "htmlnote" && state == StateBranch)
245 if (!atts.value( "fonthint").isEmpty() )
246 no.setFontHint(atts.value ("fonthint") );
247 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
251 if (!readFrameAttr(atts)) return false;
252 } else if ( eName == "xlink" && state == StateBranch )
254 state=StateBranchXLink;
255 if (!readXLinkAttr (atts)) return false;
256 } else if ( eName == "branch" && state == StateBranch )
258 lastBranch->addBranch();
259 lastBranch=lastBranch->getLastBranch();
261 readBranchAttr (atts);
262 } else if ( eName == "html" && state == StateHtmlNote )
268 } else if ( state == StateHtml )
270 // accept all while in html mode,
275 return false; // Error
279 bool parseVYMHandler::endElement ( const QString&, const QString&, const QString &eName)
282 cout << "endElement </" <<eName.ascii()
284 <<" laststate=" <<laststate
285 <<" stateStack="<<stateStack.last()
291 lastBranch=(BranchObj*)(lastBranch->getParObj());
294 htmldata+="</"+eName+">";
298 htmldata.replace ("<br></br>","<br />");
299 no.setNote (htmldata);
300 lastBranch->setNote (no);
306 state=stateStack.takeLast();
310 bool parseVYMHandler::characters ( const QString& ch)
312 //cout << "characters \""<<ch<<"\" 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 me->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 StateFloatImage: 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)
356 if (!readOOAttr(a)) return false;
358 if (!a.value( "scrolled").isEmpty() )
359 lastBranch->toggleScroll();
360 if (!a.value( "frameType").isEmpty() )
361 lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
363 if (!a.value( "incImgV").isEmpty() )
365 if (a.value("incImgV")=="true")
366 lastBranch->setIncludeImagesVer(true);
368 lastBranch->setIncludeImagesVer(false);
370 if (!a.value( "incImgH").isEmpty() )
372 if (a.value("incImgH")=="true")
373 lastBranch->setIncludeImagesHor(true);
375 lastBranch->setIncludeImagesHor(false);
380 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
386 if (!a.value( "frameType").isEmpty() )
387 lastOO->setFrameType (a.value("frameType"));
388 if (!a.value( "penColor").isEmpty() )
389 lastOO->setFramePenColor (a.value("penColor"));
390 if (!a.value( "brushColor").isEmpty() )
391 lastOO->setFrameBrushColor (a.value("brushColor"));
392 if (!a.value( "padding").isEmpty() )
394 x=a.value("padding").toInt(&ok);
395 if (ok) lastOO->setFramePadding(x);
397 if (!a.value( "borderWidth").isEmpty() )
399 x=a.value("borderWidth").toInt(&ok);
400 if (ok) lastOO->setFrameBorderWidth(x);
406 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
412 if (!a.value( "relPosX").isEmpty() )
414 if (!a.value( "relPosY").isEmpty() )
416 x=a.value("relPosX").toFloat (&okx);
417 y=a.value("relPosY").toFloat (&oky);
420 lastOO->setUseRelPos (true);
421 lastOO->move2RelPos (x,y);
424 return false; // Couldn't read relPos
427 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
429 if (!a.value( "absPosY").isEmpty() )
431 x=a.value("absPosX").toFloat (&okx);
432 y=a.value("absPosY").toFloat (&oky);
436 return false; // Couldn't read absPos
439 if (!a.value( "id").isEmpty() )
440 lastOO->setID (a.value ("id"));
441 if (!a.value( "url").isEmpty() )
442 lastOO->setURL (a.value ("url"));
443 if (!a.value( "vymLink").isEmpty() )
444 lastOO->setVymLink (a.value ("vymLink"));
445 if (!a.value( "hideInExport").isEmpty() )
446 if (a.value("hideInExport")=="true")
447 lastOO->setHideInExport(true);
449 if (!a.value( "hideLink").isEmpty())
451 if (a.value ("hideLink") =="true")
452 lastOO->setHideLinkUnselected(true);
454 lastOO->setHideLinkUnselected(false);
460 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
461 { // only for backward compatibility (<1.4.6). Use htmlnote now.
464 if (!a.value( "href").isEmpty() )
467 fn=parseHREF(a.value ("href") );
469 QString s; // Reading a note
471 if ( !file.open( QIODevice::ReadOnly) )
473 qWarning ("parseVYMHandler::readNoteAttr: Couldn't load "+fn);
476 QTextStream stream( &file );
478 while ( !stream.atEnd() ) {
479 lines += stream.readLine()+"\n";
483 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
486 if (!a.value( "fonthint").isEmpty() )
487 no.setFontHint(a.value ("fonthint") );
488 lastBranch->setNote(no);
492 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
496 //if (!readOOAttr(a)) return false;
498 if (!a.value( "useOrientation").isEmpty() )
500 if (a.value ("useOrientation") =="true")
501 lastFloat->setUseOrientation (true);
503 lastFloat->setUseOrientation (false);
505 if (!a.value( "href").isEmpty() )
508 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
510 QMessageBox::warning( 0, "Warning: " ,
511 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
512 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
518 if (!a.value( "floatExport").isEmpty() )
520 // Only for compatibility. THis is not used since 1.7.11
521 if (a.value ("floatExport") =="true")
522 lastFloat->setFloatExport(true);
524 lastFloat->setFloatExport (false);
526 if (!a.value( "zPlane").isEmpty() )
527 lastFloat->setZValue (a.value("zPlane").toInt ());
530 if (!a.value( "relPosX").isEmpty() )
532 if (!a.value( "relPosY").isEmpty() )
535 x=a.value("relPosX").toFloat (&okx);
536 y=a.value("relPosY").toFloat (&oky);
540 lastFloat->setRelPos (QPointF (x,y) );
541 // make sure floats in mapcenter are repositioned to relative pos
542 if (mc==lastBranch) mc->positionContents();
545 // Couldn't read relPos
550 if (!readOOAttr(a)) return false;
552 if (!a.value ("orgName").isEmpty() )
554 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
559 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
564 XLinkObj *xlo=new XLinkObj (mc->getScene());
565 if (!a.value( "color").isEmpty() )
567 col.setNamedColor(a.value("color"));
571 if (!a.value( "width").isEmpty() )
573 xlo->setWidth(a.value ("width").toInt (&okx, 10));
576 // Connecting by select string for compatibility with version < 1.8.76
577 if (!a.value( "beginBranch").isEmpty() )
579 if (!a.value( "endBranch").isEmpty() )
581 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
582 if (lmo && typeid (*lmo)==typeid (BranchObj))
584 xlo->setBegin ((BranchObj*)lmo);
585 lmo=mc->findObjBySelect (a.value( "endBranch"));
586 if (lmo && typeid (*lmo)==typeid (BranchObj))
588 xlo->setEnd ((BranchObj*)(lmo));
596 // object ID is used starting in version 1.8.76
597 if (!a.value( "beginID").isEmpty() )
599 if (!a.value( "endID").isEmpty() )
601 LinkableMapObj *lmo=mc->findID (a.value( "beginID"));
602 if (lmo && typeid (*lmo)==typeid (BranchObj))
604 xlo->setBegin ((BranchObj*)lmo);
605 lmo=mc->findID (a.value( "endID"));
606 if (lmo && typeid (*lmo)==typeid (BranchObj))
608 xlo->setEnd ((BranchObj*)(lmo));
615 if (!success) delete (xlo);
616 return true; // xLinks can only be established at the "end branch", return true
619 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
621 for (int i=1; i<=a.count(); i++)
622 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
626 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
628 if (!a.value( "key").isEmpty() )
630 if (!a.value( "value").isEmpty() )
631 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));