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; }
26 bool mapBuilderHandler::startDocument()
30 laststate = StateInit;
32 stateStack.append(StateInit);
40 QString mapBuilderHandler::parseHREF(QString href)
42 QString type=href.section(":",0,0);
43 QString path=href.section(":",1,1);
44 if (!tmpDir.endsWith("/"))
45 return tmpDir + "/" + path;
50 bool mapBuilderHandler::startElement ( const QString&, const QString&,
51 const QString& eName, const QXmlAttributes& atts )
55 cout << "startElement <"<< eName.ascii()<<
57 " laststate="<<stateStack.last()<<
58 " loadMode="<<loadMode<<endl;
60 stateStack.append (state);
61 if ( state == StateInit && (eName == "vymmap") )
64 if (!atts.value( "version").isEmpty() )
66 if (!checkVersion(atts.value("version")))
67 QMessageBox::warning( 0, "Warning: Version Problem" ,
68 "<h3>Map is newer than VYM</h3>"
69 "<p>The map you are just trying to load was "
70 "saved using vym " +atts.value("version")+". "
71 "The version of this vym is " + vymVersion +
72 ". If you run into problems after pressing "
73 "the ok-button below, updating vym should help.");
75 mc->setVersion(atts.value( "version" ));
78 if (loadMode==NewMap ||
79 (loadMode==ImportReplace && me->getSelection()==mc))
81 if (!atts.value( "author").isEmpty() )
83 mc->setAuthor(atts.value( "author" ) );
85 if (!atts.value( "comment").isEmpty() )
87 mc->setComment (atts.value( "comment" ) );
89 if (!atts.value( "backgroundColor").isEmpty() )
91 col.setNamedColor(atts.value("backgroundColor"));
92 mc->getScene()->setBackgroundBrush(col);
94 if (!atts.value( "linkColorHint").isEmpty() )
96 if (atts.value("linkColorHint")=="HeadingColor")
97 me->setMapLinkColorHint(HeadingColor);
99 me->setMapLinkColorHint(DefaultColor);
101 if (!atts.value( "linkStyle").isEmpty() )
103 me->setMapLinkStyle(atts.value("linkStyle"));
105 if (!atts.value( "linkColor").isEmpty() )
107 col.setNamedColor(atts.value("linkColor"));
108 me->setMapDefLinkColor(col);
110 if (!atts.value( "defXLinkColor").isEmpty() )
112 col.setNamedColor(atts.value("defXLinkColor"));
113 me->setMapDefXLinkColor(col);
115 if (!atts.value( "defXLinkWidth").isEmpty() )
117 me->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
120 } else if ( eName == "select" && state == StateMap )
122 state=StateMapSelect;
123 } else if ( eName == "setting" && state == StateMap )
125 state=StateMapSetting;
126 if (loadMode==NewMap)
127 readSettingAttr (atts);
128 } else if ( eName == "mapcenter" && state == StateMap )
130 state=StateMapCenter;
131 if (loadMode==NewMap)
133 // Really use the found mapcenter as MCO in a new map
134 lastBranch=mc; // avoid empty pointer
137 // Treat the found mapcenter as a branch
138 // in an existing map
139 LinkableMapObj* lmo=me->getSelection();
140 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
141 || (typeid(*lmo) == typeid(MapCenterObj) ) )
143 lastBranch=(BranchObj*)lmo;
144 if (loadMode==ImportAdd)
146 lastBranch->addBranch();
147 lastBranch=lastBranch->getLastBranch();
153 readBranchAttr (atts);
155 (eName == "standardflag" ||eName == "standardFlag") &&
156 (state == StateMapCenter || state==StateBranch))
158 state=StateStandardFlag;
159 } else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch))
163 if (!atts.value( "textColor").isEmpty() )
165 col.setNamedColor(atts.value("textColor"));
166 lastBranch->setColor(col );
168 } else if ( eName == "note" &&
169 (state == StateMapCenter ||state==StateBranch))
170 { // only for backward compatibility (<1.4.6). Use htmlnote now.
172 if (!readNoteAttr (atts) ) return false;
173 } else if ( eName == "htmlnote" && state == StateMapCenter)
177 } else if ( eName == "floatimage" &&
178 (state == StateMapCenter ||state==StateBranch))
180 state=StateFloatImage;
181 lastBranch->addFloatImage();
182 lastFloat=lastBranch->getLastFloatImage();
183 if (!readFloatImageAttr(atts)) return false;
184 } else if ( (eName == "branch"||eName=="floatimage") && state == StateMap)
186 // This is used in vymparts, which have no mapcenter!
188 LinkableMapObj* lmo=me->getSelection();
191 // If a vym part is _loaded_ (not imported),
192 // selection==lmo==NULL
193 // Treat it like ImportAdd then...
197 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
198 || (typeid(*lmo) == typeid(MapCenterObj) ) )
200 lastBranch=(BranchObj*)(lmo);
204 if (loadMode==ImportAdd)
206 lastBranch->addBranch();
207 lastBranch=lastBranch->getLastBranch();
212 readBranchAttr (atts);
213 } else if (eName=="floatimage")
215 state=StateFloatImage;
216 lastBranch->addFloatImage();
217 lastFloat=lastBranch->getLastFloatImage();
218 if (!readFloatImageAttr(atts)) return false;
221 } else if ( eName == "branch" && state == StateMapCenter)
225 lastBranch->addBranch();
226 lastBranch=lastBranch->getLastBranch();
227 readBranchAttr (atts);
228 } else if ( eName == "htmlnote" && state == StateBranch)
233 if (!atts.value( "fonthint").isEmpty() )
234 no.setFontHint(atts.value ("fonthint") );
235 } else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter))
239 if (!readFrameAttr(atts)) return false;
240 } else if ( eName == "xlink" && state == StateBranch )
242 state=StateBranchXLink;
243 if (!readXLinkAttr (atts)) return false;
244 } else if ( eName == "branch" && state == StateBranch )
246 lastBranch->addBranch();
247 lastBranch=lastBranch->getLastBranch();
249 readBranchAttr (atts);
250 } else if ( eName == "html" && state == StateHtmlNote )
256 } else if ( state == StateHtml )
258 // accept all while in html mode,
263 return false; // Error
267 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
270 cout << "endElement </" <<eName.ascii()
272 <<" laststate=" <<laststate
273 <<" stateStack="<<stateStack.last()
279 lastBranch=(BranchObj*)(lastBranch->getParObj());
282 htmldata+="</"+eName+">";
286 htmldata.replace ("<br></br>","<br />");
287 no.setNote (htmldata);
288 lastBranch->setNote (no);
294 state=stateStack.takeLast();
298 bool mapBuilderHandler::characters ( const QString& ch)
300 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
302 QString ch_org=quotemeta (ch);
303 QString ch_simplified=ch.simplifyWhiteSpace();
304 if ( ch_simplified.isEmpty() ) return true;
308 case StateInit: break;
309 case StateMap: break;
311 me->select(ch_simplified);
313 case StateMapSetting:break;
314 case StateMapCenter: break;
316 lastBranch->setNote(ch_simplified);
318 case StateBranch: break;
319 case StateStandardFlag:
320 lastBranch->activateStandardFlag(ch_simplified);
322 case StateFloatImage: break;
323 case StateHtmlNote: break;
328 lastBranch->setHeading(ch_simplified);
336 QString mapBuilderHandler::errorString()
338 return "the document is not in the VYM file format";
341 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
343 errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
344 .arg( exception.message() )
345 .arg( exception.lineNumber() )
346 .arg( exception.columnNumber() );
347 // Try to read the bogus line
348 errorProt+=QString("File is: %1\n").arg(inputFile);
350 if (loadStringFromDisk (inputFile,s))
352 QStringList sl=QStringList::split ("\n",s);
354 QStringList::Iterator it = sl.begin();
355 while (i<exception.lineNumber()-1)
361 s.insert (exception.columnNumber()-1,"<ERROR>");
364 return QXmlDefaultHandler::fatalError( exception );
367 void mapBuilderHandler::setMapEditor (MapEditor* e)
370 mc=me->getMapCenter();
373 void mapBuilderHandler::setTmpDir (QString tp)
378 void mapBuilderHandler::setInputFile (QString f)
383 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
388 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
391 if (!readOOAttr(a)) return false;
393 if (!a.value( "scrolled").isEmpty() )
394 lastBranch->toggleScroll();
395 if (!a.value( "frameType").isEmpty() )
396 lastBranch->setFrameType (a.value("frameType"));
398 if (!a.value( "incImgV").isEmpty() )
400 if (a.value("incImgV")=="true")
401 lastBranch->setIncludeImagesVer(true);
403 lastBranch->setIncludeImagesVer(false);
405 if (!a.value( "incImgH").isEmpty() )
407 if (a.value("incImgH")=="true")
408 lastBranch->setIncludeImagesHor(true);
410 lastBranch->setIncludeImagesHor(false);
415 bool mapBuilderHandler::readFrameAttr (const QXmlAttributes& a)
419 if (!a.value( "frameType").isEmpty() )
420 lastOO->setFrameType (a.value("frameType"));
425 bool mapBuilderHandler::readOOAttr (const QXmlAttributes& a)
431 if (!a.value( "relPosX").isEmpty() )
433 if (!a.value( "relPosY").isEmpty() )
435 x=a.value("relPosX").toInt (&okx, 10);
436 y=a.value("relPosY").toInt (&oky, 10);
439 lastOO->setUseRelPos (true);
440 lastOO->move2RelPos (x,y);
443 return false; // Couldn't read relPos
446 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
448 if (!a.value( "absPosY").isEmpty() )
450 x=a.value("absPosX").toInt (&okx, 10);
451 y=a.value("absPosY").toInt (&oky, 10);
455 return false; // Couldn't read absPos
458 if (!a.value( "url").isEmpty() )
459 lastOO->setURL (a.value ("url"));
460 if (!a.value( "vymLink").isEmpty() )
461 lastOO->setVymLink (a.value ("vymLink"));
462 if (!a.value( "hideInExport").isEmpty() )
463 if (a.value("hideInExport")=="true")
464 lastOO->setHideInExport(true);
466 if (!a.value( "hideLink").isEmpty())
468 if (a.value ("hideLink") =="true")
469 lastOO->setHideLinkUnselected(true);
471 lastOO->setHideLinkUnselected(false);
477 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
478 { // only for backward compatibility (<1.4.6). Use htmlnote now.
481 if (!a.value( "href").isEmpty() )
484 fn=parseHREF(a.value ("href") );
486 QString s; // Reading a note
488 if ( !file.open( QIODevice::ReadOnly) )
490 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
493 QTextStream stream( &file );
495 while ( !stream.atEnd() ) {
496 lines += stream.readLine()+"\n";
500 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
503 if (!a.value( "fonthint").isEmpty() )
504 no.setFontHint(a.value ("fonthint") );
505 lastBranch->setNote(no);
509 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
513 //if (!readOOAttr(a)) return false;
515 if (!a.value( "useOrientation").isEmpty() )
517 if (a.value ("useOrientation") =="true")
518 lastFloat->setUseOrientation (true);
520 lastFloat->setUseOrientation (false);
522 if (!a.value( "href").isEmpty() )
525 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
527 QMessageBox::warning( 0, "Warning: " ,
528 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
529 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
535 if (!a.value( "floatExport").isEmpty() )
537 // Only for compatibility. THis is not used since 1.7.11
538 if (a.value ("floatExport") =="true")
539 lastFloat->setFloatExport(true);
541 lastFloat->setFloatExport (false);
543 if (!a.value( "zPlane").isEmpty() )
544 lastFloat->setZValue (a.value("zPlane").toInt ());
547 if (!a.value( "relPosX").isEmpty() )
549 if (!a.value( "relPosY").isEmpty() )
552 x=a.value("relPosX").toInt (&okx, 10);
553 y=a.value("relPosY").toInt (&oky, 10);
557 lastFloat->setRelPos (QPoint (x,y) );
558 // make sure floats in mapcenter are repositioned to relative pos
559 if (mc==lastBranch) mc->positionContents();
562 // Couldn't read relPos
567 if (!readOOAttr(a)) return false;
569 if (!a.value ("orgName").isEmpty() )
571 ((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
576 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
581 XLinkObj *xlo=new XLinkObj (mc->getScene());
582 if (!a.value( "color").isEmpty() )
584 col.setNamedColor(a.value("color"));
588 if (!a.value( "width").isEmpty() )
590 xlo->setWidth(a.value ("width").toInt (&okx, 10));
593 if (!a.value( "beginBranch").isEmpty() )
595 if (!a.value( "endBranch").isEmpty() )
597 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
598 if (lmo && typeid (*lmo)==typeid (BranchObj))
600 xlo->setBegin ((BranchObj*)lmo);
601 lmo=mc->findObjBySelect (a.value( "endBranch"));
602 if (lmo && typeid (*lmo)==typeid (BranchObj))
604 xlo->setEnd ((BranchObj*)(lmo));
608 success=true; // Not all branches there yet, no error
611 if (!success) delete (xlo);
615 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
617 for (int i=1; i<=a.count(); i++)
618 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
622 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
624 if (!a.value( "key").isEmpty() )
626 if (!a.value( "value").isEmpty() )
627 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));