3 #include <qmessagebox.h>
5 #include <qstylesheet.h>
13 static BranchObj *lastBranch;
14 static FloatObj *lastFloat;
16 extern Settings settings;
18 mapBuilderHandler::mapBuilderHandler() {}
20 mapBuilderHandler::~mapBuilderHandler() {}
22 QString mapBuilderHandler::errorProtocol() { return errorProt; }
24 bool mapBuilderHandler::startDocument()
28 laststate = StateInit;
36 QString mapBuilderHandler::parseHREF(QString href)
38 QString type=href.section(":",0,0);
39 QString path=href.section(":",1,1);
40 if (!tmpDir.endsWith("/"))
41 return tmpDir + "/" + path;
46 bool mapBuilderHandler::startElement ( const QString&, const QString&,
47 const QString& eName, const QXmlAttributes& atts )
50 //cout << "startElement <"<<eName<<"> state="<<state <<" laststate="<<laststate<<" loadMode="<<loadMode<<endl;
51 if ( state == StateInit && (eName == "vymmap") )
54 if (!atts.value( "version").isEmpty() )
56 mc->setVersion(atts.value( "version" ));
57 if (!mc->checkVersion())
58 QMessageBox::warning( 0, "Warning: Version Problem" ,
59 "<h3>Map is newer than VYM</h3>"
60 "<p>The map you are just trying to load was "
61 "saved using vym " +atts.value("version")+". "
62 "The version of this vym is " __VYM_VERSION__
63 ". If you run into problems after pressing "
64 "the ok-button below, updating vym should help.");
69 if (!atts.value( "author").isEmpty() )
71 mc->setAuthor(atts.value( "author" ) );
73 if (!atts.value( "comment").isEmpty() )
75 mc->setComment (atts.value( "comment" ) );
77 if (!atts.value( "backgroundColor").isEmpty() )
79 col.setNamedColor(atts.value("backgroundColor"));
80 mc->getCanvas()->setBackgroundColor(col);
82 if (!atts.value( "linkColorHint").isEmpty() )
84 if (atts.value("linkColorHint")=="HeadingColor")
85 me->setLinkColorHint(HeadingColor);
87 me->setLinkColorHint(DefaultColor);
89 if (!atts.value( "linkStyle").isEmpty() )
91 QString s=atts.value("linkStyle");
93 me->setLinkStyle(StyleLine);
95 if (s=="StyleParabel")
96 me->setLinkStyle(StyleParabel);
98 if (s=="StylePolyLine")
99 me->setLinkStyle(StylePolyLine);
101 me->setLinkStyle(StylePolyParabel);
103 if (!atts.value( "linkColor").isEmpty() )
105 col.setNamedColor(atts.value("linkColor"));
106 me->setLinkColor(col);
109 } else if ( eName == "select" && state == StateMap )
111 state=StateMapSelect;
112 } else if ( eName == "setting" && state == StateMap )
114 state=StateMapSetting;
115 if (loadMode==NewMap)
116 readSettingAttr (atts);
117 } else if ( eName == "mapcenter" && state == StateMap )
119 state=StateMapCenter;
120 if (loadMode==NewMap)
122 // Really use the found mapcenter as MCO in a new map
123 lastBranch=mc; // avoid empty pointer
126 // Treat the found mapcenter as a branch
127 // in an existing map
128 LinkableMapObj* lmo=me->getSelection();
129 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
130 || (typeid(*lmo) == typeid(MapCenterObj) ) )
132 lastBranch=(BranchObj*)(lmo);
133 if (loadMode==ImportAdd)
135 lastBranch->addBranch();
136 lastBranch=lastBranch->getLastBranch();
142 readBranchAttr (atts);
143 } else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter)
145 state=StateMapCenterStandardFlag;
146 } else if ( eName == "heading" && state == StateMapCenter)
148 state=StateMapCenterHeading;
149 if (!atts.value( "textColor").isEmpty() )
151 col.setNamedColor(atts.value("textColor"));
152 lastBranch->setColor(col ,false );
154 } else if ( eName == "note" && state == StateMapCenter)
155 { // only for backward compatibility (<1.4.6). Use htmlnote now.
156 state=StateMapCenterNote;
157 if (!readNoteAttr (atts) ) return false;
158 } else if ( eName == "htmlnote" && state == StateMapCenter)
162 } else if ( eName == "floatimage" && state == StateMapCenter )
164 state=StateMapCenterFloatImage;
165 lastBranch->addFloatImage();
166 lastFloat=lastBranch->getLastFloatImage();
167 if (!readFloatImageAttr(atts)) return false;
168 } else if ( eName == "branch" && state == StateMap)
170 // This is used in vymparts, which have no mapcenter!
173 LinkableMapObj* lmo=me->getSelection();
176 // If a vym part is _loaded_ (not imported),
177 // selection==lmo==NULL
178 // Treat it like ImportAdd then...
182 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
183 || (typeid(*lmo) == typeid(MapCenterObj) ) )
185 lastBranch=(BranchObj*)(lmo);
186 if (loadMode==ImportAdd)
188 lastBranch->addBranch();
189 lastBranch=lastBranch->getLastBranch();
195 readBranchAttr (atts);
196 } else if ( eName == "branch" && state == StateMapCenter)
200 lastBranch->addBranch();
201 lastBranch=lastBranch->getLastBranch();
202 readBranchAttr (atts);
203 } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch)
205 state=StateBranchStandardFlag;
206 } else if ( eName == "heading" && state == StateBranch)
208 state=StateBranchHeading;
209 if (!atts.value( "textColor").isEmpty() )
211 col.setNamedColor(atts.value("textColor"));
212 lastBranch->setColor(col ,false );
214 } else if ( eName == "note" && state == StateBranch)
216 state=StateBranchNote;
217 if (!readNoteAttr (atts) ) return false;
218 } else if ( eName == "htmlnote" && state == StateBranch)
223 if (!atts.value( "fonthint").isEmpty() )
224 no.setFontHint(atts.value ("fonthint") );
225 } else if ( eName == "floatimage" && state == StateBranch )
227 state=StateBranchFloatImage;
228 lastBranch->addFloatImage();
229 lastFloat=lastBranch->getLastFloatImage();
230 if (!readFloatImageAttr(atts)) return false;
231 } else if ( eName == "branch" && state == StateBranch )
233 lastBranch->addBranch();
234 lastBranch=lastBranch->getLastBranch();
236 readBranchAttr (atts);
237 } else if ( eName == "html" && state == StateHtmlNote )
243 } else if ( state == StateHtml )
245 // accept all while in html mode,
250 return false; // Error
254 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
256 // cout << "endElement </"<<eName<<"> state="<<state <<" laststate="<<laststate<<endl;
259 case StateMapSelect: state=StateMap; return true;
260 case StateMapSetting: state=StateMap; return true;
261 case StateMapCenter: state=StateMap; return true;
262 case StateMapCenterStandardFlag: state=StateMapCenter; return true;
263 case StateMapCenterHeading: state=StateMapCenter; return true;
264 case StateMapCenterNote: state=StateMapCenter; return true;
265 case StateMapCenterFloatImage: state=StateMapCenter; return true;
277 state=StateMapCenter;
279 lastBranch=(BranchObj*)(lastBranch->getParObj());
281 case StateBranchStandardFlag: state=StateBranch; return true;
282 case StateBranchHeading: state=StateBranch; return true;
283 case StateBranchNote: state=StateBranch; return true;
284 case StateBranchFloatImage: state=StateBranch; return true;
285 case StateHtmlNote: state=laststate; return true;
287 htmldata+="</"+eName+">";
291 htmldata.replace ("<br></br>","<br />");
292 no.setNote (htmldata);
293 lastBranch->setNote (no);
299 case StateMap: state=StateInit; return true;
301 // even for HTML includes, this should never be reached
306 bool mapBuilderHandler::characters ( const QString& ch)
308 //cout << "characters \""<<ch<<"\" state="<<state <<" laststate="<<laststate<<endl;
310 QString ch_org=quotemeta (ch);
311 QString ch_simplified=ch.simplifyWhiteSpace();
312 if ( ch_simplified.isEmpty() ) return true;
316 case StateInit: break;
317 case StateMap: break;
319 me->select(ch_simplified);
321 case StateMapSetting:break;
322 case StateMapCenter: break;
323 case StateMapCenterStandardFlag:
324 lastBranch->activateStandardFlag(ch_simplified);
326 case StateMapCenterHeading:
327 lastBranch->setHeading(ch_simplified);
329 case StateMapCenterNote:
330 lastBranch->setNote(ch_simplified);
332 case StateBranch: break;
333 case StateBranchStandardFlag:
334 lastBranch->activateStandardFlag(ch_simplified);
336 case StateBranchHeading:
337 lastBranch->setHeading(ch_simplified);
339 case StateBranchNote:
340 lastBranch->setNote(ch_simplified);
342 case StateBranchFloatImage: break;
343 case StateHtmlNote: break;
353 QString mapBuilderHandler::errorString()
355 return "the document is not in the VYM file format";
358 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
360 errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
361 .arg( exception.message() )
362 .arg( exception.lineNumber() )
363 .arg( exception.columnNumber() );
365 return QXmlDefaultHandler::fatalError( exception );
368 void mapBuilderHandler::setMapEditor (MapEditor* e)
371 mc=me->getMapCenter();
374 void mapBuilderHandler::setTmpDir (QString tp)
379 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
384 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
388 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
390 if (!a.value( "absPosY").isEmpty() )
392 x=a.value("absPosX").toInt (&okx, 10);
393 y=a.value("absPosY").toInt (&oky, 10);
395 lastBranch->move(x,y);
397 return false; // Couldn't read absPos
400 if (!a.value( "scrolled").isEmpty() )
401 lastBranch->toggleScroll();
402 if (!a.value( "url").isEmpty() )
403 lastBranch->setURL (a.value ("url"));
404 if (!a.value( "vymLink").isEmpty() )
405 lastBranch->setVymLink (a.value ("vymLink"));
406 if (!a.value( "frameType").isEmpty() )
407 lastBranch->setFrameType (a.value("frameType"));
411 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
412 { // only for backward compatibility (<1.4.6). Use htmlnote now.
415 if (!a.value( "href").isEmpty() )
418 fn=parseHREF(a.value ("href") );
420 QString s; // Reading a note
422 if ( !file.open( IO_ReadOnly) )
424 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
427 QTextStream stream( &file );
429 while ( !stream.eof() ) {
430 lines += stream.readLine()+"\n";
433 // Convert to richtext
434 if ( !QStyleSheet::mightBeRichText( lines ) )
436 // Here we are workarounding the QT conversion method:
437 // convertFromPlainText does not generate valid xml, needed
438 // for the parser, but just <p> and <br> without closing tags.
439 // So we have to add those by ourselves
440 //lines=quotemeta (lines);
441 lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
442 lines.replace ("<br>","<br />");
445 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
448 if (!a.value( "fonthint").isEmpty() )
449 no.setFontHint(a.value ("fonthint") );
450 if (state == StateMapCenterNote)
453 lastBranch->setNote(no);
457 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
459 if (!a.value( "useOrientation").isEmpty() )
461 if (a.value ("useOrientation") =="true")
462 lastFloat->setUseOrientation (true);
464 lastFloat->setUseOrientation (false);
466 if (!a.value( "href").isEmpty() )
469 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
471 QMessageBox::warning( 0, "Warning: " ,
472 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
473 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
479 if (!a.value( "floatExport").isEmpty() )
481 if (a.value ("floatExpofrt") =="true")
482 lastFloat->setFloatExport (true);
484 lastFloat->setFloatExport (false);
486 if (!a.value( "zPlane").isEmpty() )
487 lastFloat->setZ (a.value("zPlane").toInt ());
490 if (!a.value( "relPosX").isEmpty() )
492 if (!a.value( "relPosY").isEmpty() )
495 x=a.value("relPosX").toInt (&okx, 10);
496 y=a.value("relPosY").toInt (&oky, 10);
498 lastFloat->setRelPos (QPoint (x,y) );
500 // Couldn't read relPos
507 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
509 for (int i=1; i<=a.count(); i++)
510 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
514 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
516 if (!a.value( "key").isEmpty() )
518 if (!a.value( "value").isEmpty() )
519 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));