3 #include <qmessagebox.h>
5 #include <qstylesheet.h>
10 #include "linkablemapobj.h"
14 static BranchObj *lastBranch;
15 static FloatObj *lastFloat;
16 static LinkableMapObj *lastLMO;
18 extern Settings settings;
20 mapBuilderHandler::mapBuilderHandler() {}
22 mapBuilderHandler::~mapBuilderHandler() {}
24 QString mapBuilderHandler::errorProtocol() { return errorProt; }
26 bool mapBuilderHandler::startDocument()
30 laststate = StateInit;
38 QString mapBuilderHandler::parseHREF(QString href)
40 QString type=href.section(":",0,0);
41 QString path=href.section(":",1,1);
42 if (!tmpDir.endsWith("/"))
43 return tmpDir + "/" + path;
48 bool mapBuilderHandler::startElement ( const QString&, const QString&,
49 const QString& eName, const QXmlAttributes& atts )
52 //cout << "startElement <"<<eName<<"> state="<<state <<" laststate="<<laststate<<" loadMode="<<loadMode<<endl;
53 if ( state == StateInit && (eName == "vymmap") )
56 if (!atts.value( "version").isEmpty() )
58 mc->setVersion(atts.value( "version" ));
59 if (!mc->checkVersion())
60 QMessageBox::warning( 0, "Warning: Version Problem" ,
61 "<h3>Map is newer than VYM</h3>"
62 "<p>The map you are just trying to load was "
63 "saved using vym " +atts.value("version")+". "
64 "The version of this vym is " __VYM_VERSION__
65 ". If you run into problems after pressing "
66 "the ok-button below, updating vym should help.");
71 if (!atts.value( "author").isEmpty() )
73 mc->setAuthor(atts.value( "author" ) );
75 if (!atts.value( "comment").isEmpty() )
77 mc->setComment (atts.value( "comment" ) );
79 if (!atts.value( "backgroundColor").isEmpty() )
81 col.setNamedColor(atts.value("backgroundColor"));
82 mc->getCanvas()->setBackgroundColor(col);
84 if (!atts.value( "linkColorHint").isEmpty() )
86 if (atts.value("linkColorHint")=="HeadingColor")
87 me->setLinkColorHint(HeadingColor);
89 me->setLinkColorHint(DefaultColor);
91 if (!atts.value( "linkStyle").isEmpty() )
93 QString s=atts.value("linkStyle");
95 me->setLinkStyle(StyleLine);
97 if (s=="StyleParabel")
98 me->setLinkStyle(StyleParabel);
100 if (s=="StylePolyLine")
101 me->setLinkStyle(StylePolyLine);
103 me->setLinkStyle(StylePolyParabel);
105 if (!atts.value( "linkColor").isEmpty() )
107 col.setNamedColor(atts.value("linkColor"));
108 me->setLinkColor(col);
110 if (!atts.value( "defXLinkColor").isEmpty() )
112 col.setNamedColor(atts.value("defXLinkColor"));
113 me->setDefXLinkColor(col);
115 if (!atts.value( "defXLinkWidth").isEmpty() )
117 me->setDefXLinkWidth(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);
154 } else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter)
156 state=StateMapCenterStandardFlag;
157 } else if ( eName == "heading" && state == StateMapCenter)
159 state=StateMapCenterHeading;
160 if (!atts.value( "textColor").isEmpty() )
162 col.setNamedColor(atts.value("textColor"));
163 lastBranch->setColor(col ,false );
165 } else if ( eName == "note" && state == StateMapCenter)
166 { // only for backward compatibility (<1.4.6). Use htmlnote now.
167 state=StateMapCenterNote;
168 if (!readNoteAttr (atts) ) return false;
169 } else if ( eName == "htmlnote" && state == StateMapCenter)
173 } else if ( eName == "floatimage" && state == StateMapCenter )
175 state=StateMapCenterFloatImage;
176 lastBranch->addFloatImage();
177 lastFloat=lastBranch->getLastFloatImage();
178 if (!readFloatImageAttr(atts)) return false;
179 } else if ( eName == "branch" && state == StateMap)
181 // This is used in vymparts, which have no mapcenter!
184 LinkableMapObj* lmo=me->getSelection();
187 // If a vym part is _loaded_ (not imported),
188 // selection==lmo==NULL
189 // Treat it like ImportAdd then...
193 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
194 || (typeid(*lmo) == typeid(MapCenterObj) ) )
196 lastBranch=(BranchObj*)(lmo);
197 if (loadMode==ImportAdd)
199 lastBranch->addBranch();
200 lastBranch=lastBranch->getLastBranch();
207 readBranchAttr (atts);
208 } else if ( eName == "branch" && state == StateMapCenter)
212 lastBranch->addBranch();
213 lastBranch=lastBranch->getLastBranch();
214 readBranchAttr (atts);
215 } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch)
217 state=StateBranchStandardFlag;
218 } else if ( eName == "heading" && state == StateBranch)
220 state=StateBranchHeading;
221 if (!atts.value( "textColor").isEmpty() )
223 col.setNamedColor(atts.value("textColor"));
224 lastBranch->setColor(col ,false );
226 } else if ( eName == "note" && state == StateBranch)
228 state=StateBranchNote;
229 if (!readNoteAttr (atts) ) return false;
230 } else if ( eName == "htmlnote" && state == StateBranch)
235 if (!atts.value( "fonthint").isEmpty() )
236 no.setFontHint(atts.value ("fonthint") );
237 } else if ( eName == "floatimage" && state == StateBranch )
239 state=StateBranchFloatImage;
240 lastBranch->addFloatImage();
241 lastFloat=lastBranch->getLastFloatImage();
242 if (!readFloatImageAttr(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)
272 // cout << "endElement </"<<eName<<"> state="<<state <<" laststate="<<laststate<<endl;
275 case StateMapSelect: state=StateMap; return true;
276 case StateMapSetting: state=StateMap; return true;
277 case StateMapCenter: state=StateMap; return true;
278 case StateMapCenterStandardFlag: state=StateMapCenter; return true;
279 case StateMapCenterHeading: state=StateMapCenter; return true;
280 case StateMapCenterNote: state=StateMapCenter; return true;
281 case StateMapCenterFloatImage: state=StateMapCenter; return true;
293 state=StateMapCenter;
295 lastBranch=(BranchObj*)(lastBranch->getParObj());
297 case StateBranchStandardFlag: state=StateBranch; return true;
298 case StateBranchHeading: state=StateBranch; return true;
299 case StateBranchNote: state=StateBranch; return true;
300 case StateBranchFloatImage: state=StateBranch; return true;
301 case StateBranchXLink: state=StateBranch; return true;
302 case StateHtmlNote: state=laststate; return true;
304 htmldata+="</"+eName+">";
308 htmldata.replace ("<br></br>","<br />");
309 no.setNote (htmldata);
310 lastBranch->setNote (no);
316 case StateMap: state=StateInit; return true;
318 // even for HTML includes, this should never be reached
323 bool mapBuilderHandler::characters ( const QString& ch)
325 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
327 QString ch_org=quotemeta (ch);
328 QString ch_simplified=ch.simplifyWhiteSpace();
329 if ( ch_simplified.isEmpty() ) return true;
333 case StateInit: break;
334 case StateMap: break;
336 me->select(ch_simplified);
338 case StateMapSetting:break;
339 case StateMapCenter: break;
340 case StateMapCenterStandardFlag:
341 lastBranch->activateStandardFlag(ch_simplified);
343 case StateMapCenterHeading:
344 lastBranch->setHeading(ch_simplified);
346 case StateMapCenterNote:
347 lastBranch->setNote(ch_simplified);
349 case StateBranch: break;
350 case StateBranchStandardFlag:
351 lastBranch->activateStandardFlag(ch_simplified);
353 case StateBranchHeading:
354 lastBranch->setHeading(ch_simplified);
356 case StateBranchNote:
357 lastBranch->setNote(ch_simplified);
359 case StateBranchFloatImage: break;
360 case StateHtmlNote: break;
370 QString mapBuilderHandler::errorString()
372 return "the document is not in the VYM file format";
375 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
377 errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
378 .arg( exception.message() )
379 .arg( exception.lineNumber() )
380 .arg( exception.columnNumber() );
382 return QXmlDefaultHandler::fatalError( exception );
385 void mapBuilderHandler::setMapEditor (MapEditor* e)
388 mc=me->getMapCenter();
391 void mapBuilderHandler::setTmpDir (QString tp)
396 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
401 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
404 if (!readLinkAttr(a)) return false;
408 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
410 if (!a.value( "absPosY").isEmpty() )
412 x=a.value("absPosX").toInt (&okx, 10);
413 y=a.value("absPosY").toInt (&oky, 10);
415 lastBranch->move(x,y);
417 return false; // Couldn't read absPos
420 if (!a.value( "scrolled").isEmpty() )
421 lastBranch->toggleScroll();
422 if (!a.value( "url").isEmpty() )
423 lastBranch->setURL (a.value ("url"));
424 if (!a.value( "vymLink").isEmpty() )
425 lastBranch->setVymLink (a.value ("vymLink"));
426 if (!a.value( "frameType").isEmpty() )
427 lastBranch->setFrameType (a.value("frameType"));
431 bool mapBuilderHandler::readLinkAttr (const QXmlAttributes& a)
433 if (!a.value( "hideLinkUnselected").isEmpty())
435 if (a.value ("hideLinkUnselected") =="true")
436 if (lastLMO) lastLMO->setHideLinkUnselected(true);
441 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
442 { // only for backward compatibility (<1.4.6). Use htmlnote now.
445 if (!a.value( "href").isEmpty() )
448 fn=parseHREF(a.value ("href") );
450 QString s; // Reading a note
452 if ( !file.open( IO_ReadOnly) )
454 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
457 QTextStream stream( &file );
459 while ( !stream.eof() ) {
460 lines += stream.readLine()+"\n";
463 // Convert to richtext
464 if ( !QStyleSheet::mightBeRichText( lines ) )
466 // Here we are workarounding the QT conversion method:
467 // convertFromPlainText does not generate valid xml, needed
468 // for the parser, but just <p> and <br> without closing tags.
469 // So we have to add those by ourselves
470 //lines=quotemeta (lines);
471 lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
472 lines.replace ("<br>","<br />");
475 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
478 if (!a.value( "fonthint").isEmpty() )
479 no.setFontHint(a.value ("fonthint") );
480 if (state == StateMapCenterNote)
483 lastBranch->setNote(no);
487 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
490 if (!readLinkAttr(a)) return false;
492 if (!a.value( "useOrientation").isEmpty() )
494 if (a.value ("useOrientation") =="true")
495 lastFloat->setUseOrientation (true);
497 lastFloat->setUseOrientation (false);
499 if (!a.value( "href").isEmpty() )
502 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
504 QMessageBox::warning( 0, "Warning: " ,
505 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
506 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
512 if (!a.value( "floatExport").isEmpty() )
514 if (a.value ("floatExpofrt") =="true")
515 lastFloat->setFloatExport (true);
517 lastFloat->setFloatExport (false);
519 if (!a.value( "zPlane").isEmpty() )
520 lastFloat->setZ (a.value("zPlane").toInt ());
523 if (!a.value( "relPosX").isEmpty() )
525 if (!a.value( "relPosY").isEmpty() )
528 x=a.value("relPosX").toInt (&okx, 10);
529 y=a.value("relPosY").toInt (&oky, 10);
531 lastFloat->setRelPos (QPoint (x,y) );
533 // Couldn't read relPos
540 bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
545 XLinkObj *xlo=new XLinkObj (mc->getCanvas());
546 if (!a.value( "color").isEmpty() )
548 col.setNamedColor(a.value("color"));
552 if (!a.value( "width").isEmpty() )
554 xlo->setWidth(a.value ("width").toInt (&okx, 10));
557 if (!a.value( "beginBranch").isEmpty() )
559 if (!a.value( "endBranch").isEmpty() )
561 LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
562 if (lmo && typeid (*lmo)==typeid (BranchObj))
564 xlo->setBegin ((BranchObj*)(lmo));
565 lmo=mc->findObjBySelect (a.value( "endBranch"));
566 if (lmo && typeid (*lmo)==typeid (BranchObj))
568 xlo->setEnd ((BranchObj*)(lmo));
572 success=true; // Not all branches there yet, no error
575 if (!success) delete (xlo);
579 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
581 for (int i=1; i<=a.count(); i++)
582 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
586 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
588 if (!a.value( "key").isEmpty() )
590 if (!a.value( "value").isEmpty() )
591 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));