10 #include "linkablemapobj.h"
13 static BranchObj *lastBranch;
14 static FloatObj *lastFloat;
15 static OrnamentedObj *lastOO;
17 extern Settings settings;
18 extern QString vymVersion;
20 mapBuilderHandler::mapBuilderHandler() {}
22 mapBuilderHandler::~mapBuilderHandler() {}
24 QString mapBuilderHandler::errorProtocol() { return errorProt; }
27 bool mapBuilderHandler::startDocument()
31 laststate = StateInit;
33 stateStack.append(StateInit);
41 QString mapBuilderHandler::parseHREF(QString href)
43 QString type=href.section(":",0,0);
44 QString path=href.section(":",1,1);
45 if (!tmpDir.endsWith("/"))
46 return tmpDir + "/" + path;
51 bool mapBuilderHandler::startElement ( const QString&, const QString&,
52 const QString& eName, const QXmlAttributes& atts )
56 cout << "startElement <"<< eName.ascii()
58 <<" laststate="<<stateStack.last()
59 <<" loadMode="<<loadMode
60 <<" line="<<QXmlDefaultHandler::lineNumber()
63 stateStack.append (state);
64 if ( state == StateInit && (eName == "vymmap") )
67 if (!atts.value( "version").isEmpty() )
69 if (!checkVersion(atts.value("version")))
70 QMessageBox::warning( 0, "Warning: Version Problem" ,
71 "<h3>Map is newer than VYM</h3>"
72 "<p>The map you are just trying to load was "
73 "saved using vym " +atts.value("version")+". "
74 "The version of this vym is " + vymVersion +
75 ". If you run into problems after pressing "
76 "the ok-button below, updating vym should help.");
78 mc->setVersion(atts.value( "version" ));
81 if (loadMode==NewMap ||
82 (loadMode==ImportReplace && me->getSelection()==mc))
84 if (!atts.value( "author").isEmpty() )
86 mc->setAuthor(atts.value( "author" ) );
88 if (!atts.value( "comment").isEmpty() )
90 mc->setComment (atts.value( "comment" ) );
92 if (!atts.value( "backgroundColor").isEmpty() )
94 col.setNamedColor(atts.value("backgroundColor"));
95 mc->getScene()->setBackgroundBrush(col);
97 if (!atts.value( "linkColorHint").isEmpty() )
99 if (atts.value("linkColorHint")=="HeadingColor")
100 me->setMapLinkColorHint(HeadingColor);
102 me->setMapLinkColorHint(DefaultColor);
104 if (!atts.value( "linkStyle").isEmpty() )
106 me->setMapLinkStyle(atts.value("linkStyle"));
108 if (!atts.value( "linkColor").isEmpty() )
110 col.setNamedColor(atts.value("linkColor"));
111 me->setMapDefLinkColor(col);
113 if (!atts.value( "defXLinkColor").isEmpty() )
115 col.setNamedColor(atts.value("defXLinkColor"));
116 me->setMapDefXLinkColor(col);
118 if (!atts.value( "defXLinkWidth").isEmpty() )
120 me->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
123 } else if ( eName == "select" && state == StateMap )
125 state=StateMapSelect;
126 } else if ( eName == "setting" && state == StateMap )
128 state=StateMapSetting;
129 if (loadMode==NewMap)
130 readSettingAttr (atts);
131 } else if ( eName == "mapcenter" && state == StateMap )
133 state=StateMapCenter;
134 if (loadMode==NewMap)
136 // Really use the found mapcenter as MCO in a new map
137 lastBranch=mc; // avoid empty pointer
140 // Treat the found mapcenter as a branch
141 // in an existing map
142 LinkableMapObj* lmo=me->getSelection();
143 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
144 || (typeid(*lmo) == typeid(MapCenterObj) ) )
146 lastBranch=(BranchObj*)lmo;
147 if (loadMode==ImportAdd)
149 lastBranch->addBranch();
150 lastBranch=lastBranch->getLastBranch();
156 readBranchAttr (atts);
158 (eName == "standardflag" ||eName == "standardFlag") &&
159 (state == StateMapCenter || state==StateBranch))
161 state=StateStandardFlag;
162 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
166 if (!atts.value( "textColor").isEmpty() )
168 col.setNamedColor(atts.value("textColor"));
169 lastBranch->setColor(col );
171 } else if ( eName == "note" &&
172 (state == StateMapCenter ||state==StateBranch))
173 { // only for backward compatibility (<1.4.6). Use htmlnote now.
175 if (!readNoteAttr (atts) ) return false;
176 } else if ( eName == "htmlnote" && state == StateMapCenter)
180 } else if ( eName == "floatimage" &&
181 (state == StateMapCenter ||state==StateBranch))
183 state=StateFloatImage;
184 lastBranch->addFloatImage();
185 lastFloat=lastBranch->getLastFloatImage();
186 if (!readFloatImageAttr(atts)) return false;
187 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
189 // This is used in vymparts, which have no mapcenter!
191 LinkableMapObj* lmo=me->getSelection();
194 // If a vym part is _loaded_ (not imported),
195 // selection==lmo==NULL
196 // Treat it like ImportAdd then...
200 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
201 || (typeid(*lmo) == typeid(MapCenterObj) ) )
203 lastBranch=(BranchObj*)(lmo);
207 if (loadMode==ImportAdd)
209 lastBranch->addBranch();
210 lastBranch=lastBranch->getLastBranch();
215 readBranchAttr (atts);
216 } else if (eName=="floatimage")
218 state=StateFloatImage;
219 lastBranch->addFloatImage();
220 lastFloat=lastBranch->getLastFloatImage();
221 if (!readFloatImageAttr(atts)) return false;
224 } else if ( eName == "branch" && state == StateMapCenter)
228 lastBranch->addBranch();
229 lastBranch=lastBranch->getLastBranch();
230 readBranchAttr (atts);
231 } else if ( eName == "htmlnote" && state == StateBranch)
236 if (!atts.value( "fonthint").isEmpty() )
237 no.setFontHint(atts.value ("fonthint") );
238 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
242 if (!readFrameAttr(atts)) return false;
243 } else if ( eName == "xlink" && state == StateBranch )
245 state=StateBranchXLink;
246 if (!readXLinkAttr (atts)) return false;
247 } else if ( eName == "branch" && state == StateBranch )
249 lastBranch->addBranch();
250 lastBranch=lastBranch->getLastBranch();
252 readBranchAttr (atts);
253 } else if ( eName == "html" && state == StateHtmlNote )
259 } else if ( state == StateHtml )
261 // accept all while in html mode,
266 return false; // Error
270 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
273 cout << "endElement </" <<eName.ascii()
275 <<" laststate=" <<laststate
276 <<" stateStack="<<stateStack.last()
282 lastBranch=(BranchObj*)(lastBranch->getParObj());
285 htmldata+="</"+eName+">";
289 htmldata.replace ("<br></br>","<br />");
290 no.setNote (htmldata);
291 lastBranch->setNote (no);
297 state=stateStack.takeLast();
301 bool mapBuilderHandler::characters ( const QString& ch)
303 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
305 QString ch_org=quotemeta (ch);
306 QString ch_simplified=ch.simplifyWhiteSpace();
307 if ( ch_simplified.isEmpty() ) return true;
311 case StateInit: break;
312 case StateMap: break;
314 me->select(ch_simplified);
316 case StateMapSetting:break;
317 case StateMapCenter: break;
319 lastBranch->setNote(ch_simplified);
321 case StateBranch: break;
322 case StateStandardFlag:
323 lastBranch->activateStandardFlag(ch_simplified);
325 case StateFloatImage: break;
326 case StateHtmlNote: break;
331 lastBranch->setHeading(ch_simplified);
339 QString mapBuilderHandler::errorString()
341 return "the document is not in the VYM file format";
344 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
346 errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
347 .arg( exception.message() )
348 .arg( exception.lineNumber() )
349 .arg( exception.columnNumber() );
350 // Try to read the bogus line
351 errorProt+=QString("File is: %1\n").arg(inputFile);
353 if (loadStringFromDisk (inputFile,s))
355 QStringList sl=QStringList::split ("\n",s);
357 QStringList::Iterator it = sl.begin();
358 while (i<exception.lineNumber()-1)
364 s.insert (exception.columnNumber()-1,"<ERROR>");
367 return QXmlDefaultHandler::fatalError( exception );
370 void mapBuilderHandler::setMapEditor (MapEditor* e)
373 mc=me->getMapCenter();
376 void mapBuilderHandler::setTmpDir (QString tp)
381 void mapBuilderHandler::setInputFile (QString f)
386 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
391 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
394 if (!readOOAttr(a)) return false;
396 if (!a.value( "scrolled").isEmpty() )
397 lastBranch->toggleScroll();
398 if (!a.value( "frameType").isEmpty() )
399 lastBranch->setFrameType (a.value("frameType"));
401 if (!a.value( "incImgV").isEmpty() )
403 if (a.value("incImgV")=="true")
404 lastBranch->setIncludeImagesVer(true);
406 lastBranch->setIncludeImagesVer(false);
408 if (!a.value( "incImgH").isEmpty() )
410 if (a.value("incImgH")=="true")
411 lastBranch->setIncludeImagesHor(true);
413 lastBranch->setIncludeImagesHor(false);
418 bool mapBuilderHandler::readFrameAttr (const QXmlAttributes& a)
422 if (!a.value( "frameType").isEmpty() )
423 lastOO->setFrameType (a.value("frameType"));
428 bool mapBuilderHandler::readOOAttr (const QXmlAttributes& a)
434 if (!a.value( "relPosX").isEmpty() )
436 if (!a.value( "relPosY").isEmpty() )
438 x=a.value("relPosX").toInt (&okx, 10);
439 y=a.value("relPosY").toInt (&oky, 10);
442 lastOO->setUseRelPos (true);
443 lastOO->move2RelPos (x,y);
446 return false; // Couldn't read relPos
449 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
451 if (!a.value( "absPosY").isEmpty() )
453 x=a.value("absPosX").toInt (&okx, 10);
454 y=a.value("absPosY").toInt (&oky, 10);
458 return false; // Couldn't read absPos
461 if (!a.value( "url").isEmpty() )
462 lastOO->setURL (a.value ("url"));
463 if (!a.value( "vymLink").isEmpty() )
464 lastOO->setVymLink (a.value ("vymLink"));
465 if (!a.value( "hideInExport").isEmpty() )
466 if (a.value("hideInExport")=="true")
467 lastOO->setHideInExport(true);
469 if (!a.value( "hideLink").isEmpty())
471 if (a.value ("hideLink") =="true")
472 lastOO->setHideLinkUnselected(true);
474 lastOO->setHideLinkUnselected(false);
480 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
481 { // only for backward compatibility (<1.4.6). Use htmlnote now.
484 if (!a.value( "href").isEmpty() )
487 fn=parseHREF(a.value ("href") );
489 QString s; // Reading a note
491 if ( !file.open( QIODevice::ReadOnly) )
493 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
496 QTextStream stream( &file );
498 while ( !stream.atEnd() ) {
499 lines += stream.readLine()+"\n";
503 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
506 if (!a.value( "fonthint").isEmpty() )
507 no.setFontHint(a.value ("fonthint") );
508 lastBranch->setNote(no);
512 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
516 //if (!readOOAttr(a)) return false;
518 if (!a.value( "useOrientation").isEmpty() )
520 if (a.value ("useOrientation") =="true")
521 lastFloat->setUseOrientation (true);
523 lastFloat->setUseOrientation (false);
525 if (!a.value( "href").isEmpty() )
528 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
530 QMessageBox::warning( 0, "Warning: " ,
531 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
532 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
538 if (!a.value( "floatExport").isEmpty() )
540 // Only for compatibility. THis is not used since 1.7.11
541 if (a.value ("floatExport") =="true")
542 lastFloat->setFloatExport(true);
544 lastFloat->setFloatExport (false);
546 if (!a.value( "zPlane").isEmpty() )
547 lastFloat->setZValue (a.value("zPlane").toInt ());
550 if (!a.value( "relPosX").isEmpty() )
552 if (!a.value( "relPosY").isEmpty() )
555 x=a.value("relPosX").toInt (&okx, 10);
556 y=a.value("relPosY").toInt (&oky, 10);
560 lastFloat->setRelPos (QPoint (x,y) );
561 // make sure floats in mapcenter are repositioned to relative pos
562 if (mc==lastBranch) mc->positionContents();
565 // Couldn't read relPos
570 if (!readOOAttr(a)) return false;
572 if (!a.value ("orgName").isEmpty() )
574 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
579 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
584 XLinkObj *xlo=new XLinkObj (mc->getScene());
585 if (!a.value( "color").isEmpty() )
587 col.setNamedColor(a.value("color"));
591 if (!a.value( "width").isEmpty() )
593 xlo->setWidth(a.value ("width").toInt (&okx, 10));
596 if (!a.value( "beginBranch").isEmpty() )
598 if (!a.value( "endBranch").isEmpty() )
600 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
601 if (lmo && typeid (*lmo)==typeid (BranchObj))
603 xlo->setBegin ((BranchObj*)lmo);
604 lmo=mc->findObjBySelect (a.value( "endBranch"));
605 if (lmo && typeid (*lmo)==typeid (BranchObj))
607 xlo->setEnd ((BranchObj*)(lmo));
611 success=true; // Not all branches there yet, no error
614 if (!success) delete (xlo);
618 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
620 for (int i=1; i<=a.count(); i++)
621 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
625 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
627 if (!a.value( "key").isEmpty() )
629 if (!a.value( "value").isEmpty() )
630 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));