Initial import.
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<<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();
174 if (lmo && (typeid(*lmo) == typeid(BranchObj) )
175 || (typeid(*lmo) == typeid(MapCenterObj) ) )
177 lastBranch=(BranchObj*)(lmo);
178 if (loadMode==ImportAdd)
180 lastBranch->addBranch();
181 lastBranch=lastBranch->getLastBranch();
187 readBranchAttr (atts);
188 } else if ( eName == "branch" && state == StateMapCenter)
192 lastBranch->addBranch();
193 lastBranch=lastBranch->getLastBranch();
194 readBranchAttr (atts);
195 } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch)
197 state=StateBranchStandardFlag;
198 } else if ( eName == "heading" && state == StateBranch)
200 state=StateBranchHeading;
201 if (!atts.value( "textColor").isEmpty() )
203 col.setNamedColor(atts.value("textColor"));
204 lastBranch->setColor(col ,false );
206 } else if ( eName == "note" && state == StateBranch)
208 state=StateBranchNote;
209 if (!readNoteAttr (atts) ) return false;
210 } else if ( eName == "htmlnote" && state == StateBranch)
215 if (!atts.value( "fonthint").isEmpty() )
216 no.setFontHint(atts.value ("fonthint") );
217 } else if ( eName == "floatimage" && state == StateBranch )
219 state=StateBranchFloatImage;
220 lastBranch->addFloatImage();
221 lastFloat=lastBranch->getLastFloatImage();
222 if (!readFloatImageAttr(atts)) return false;
223 } else if ( eName == "branch" && state == StateBranch )
225 lastBranch->addBranch();
226 lastBranch=lastBranch->getLastBranch();
228 readBranchAttr (atts);
229 } else if ( eName == "html" && state == StateHtmlNote )
235 } else if ( state == StateHtml )
237 // accept all while in html mode,
242 return false; // Error
246 bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName)
248 // cout << "endElement </"<<eName<<"> state="<<state <<" laststate="<<laststate<<endl;
251 case StateMapSelect: state=StateMap; return true;
252 case StateMapSetting: state=StateMap; return true;
253 case StateMapCenter: state=StateMap; return true;
254 case StateMapCenterStandardFlag: state=StateMapCenter; return true;
255 case StateMapCenterHeading: state=StateMapCenter; return true;
256 case StateMapCenterNote: state=StateMapCenter; return true;
257 case StateMapCenterFloatImage: state=StateMapCenter; return true;
269 state=StateMapCenter;
271 lastBranch=(BranchObj*)(lastBranch->getParObj());
273 case StateBranchStandardFlag: state=StateBranch; return true;
274 case StateBranchHeading: state=StateBranch; return true;
275 case StateBranchNote: state=StateBranch; return true;
276 case StateBranchFloatImage: state=StateBranch; return true;
277 case StateHtmlNote: state=laststate; return true;
279 htmldata+="</"+eName+">";
283 htmldata.replace ("<br></br>","<br />");
284 no.setNote (htmldata);
285 lastBranch->setNote (no);
291 case StateMap: state=StateInit; return true;
293 // even for HTML includes, this should never be reached
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;
315 case StateMapCenterStandardFlag:
316 lastBranch->activateStandardFlag(ch_simplified);
318 case StateMapCenterHeading:
319 lastBranch->setHeading(ch_simplified);
321 case StateMapCenterNote:
322 lastBranch->setNote(ch_simplified);
324 case StateBranch: break;
325 case StateBranchStandardFlag:
326 lastBranch->activateStandardFlag(ch_simplified);
328 case StateBranchHeading:
329 lastBranch->setHeading(ch_simplified);
331 case StateBranchNote:
332 lastBranch->setNote(ch_simplified);
334 case StateBranchFloatImage: break;
335 case StateHtmlNote: break;
345 QString mapBuilderHandler::errorString()
347 return "the document is not in the VYM file format";
350 bool mapBuilderHandler::fatalError( const QXmlParseException& exception )
352 errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
353 .arg( exception.message() )
354 .arg( exception.lineNumber() )
355 .arg( exception.columnNumber() );
357 return QXmlDefaultHandler::fatalError( exception );
360 void mapBuilderHandler::setMapEditor (MapEditor* e)
363 mc=me->getMapCenter();
366 void mapBuilderHandler::setTmpDir (QString tp)
371 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
376 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
380 if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2)
382 if (!a.value( "absPosY").isEmpty() )
384 x=a.value("absPosX").toInt (&okx, 10);
385 y=a.value("absPosY").toInt (&oky, 10);
387 lastBranch->move(x,y);
389 return false; // Couldn't read absPos
392 if (!a.value( "scrolled").isEmpty() )
393 lastBranch->toggleScroll();
394 if (!a.value( "url").isEmpty() )
395 lastBranch->setURL (a.value ("url"));
396 if (!a.value( "vymLink").isEmpty() )
397 lastBranch->setVymLink (a.value ("vymLink"));
398 if (!a.value( "frameType").isEmpty() )
399 lastBranch->setFrameType (a.value("frameType"));
403 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
404 { // only for backward compatibility (<1.4.6). Use htmlnote now.
407 if (!a.value( "href").isEmpty() )
410 fn=parseHREF(a.value ("href") );
412 QString s; // Reading a note
414 if ( !file.open( IO_ReadOnly) )
416 qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn);
419 QTextStream stream( &file );
421 while ( !stream.eof() ) {
422 lines += stream.readLine()+"\n";
425 // Convert to richtext
426 if ( !QStyleSheet::mightBeRichText( lines ) )
428 // Here we are workarounding the QT conversion method:
429 // convertFromPlainText does not generate valid xml, needed
430 // for the parser, but just <p> and <br> without closing tags.
431 // So we have to add those by ourselves
432 //lines=quotemeta (lines);
433 lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
434 lines.replace ("<br>","<br />");
437 lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
440 if (!a.value( "fonthint").isEmpty() )
441 no.setFontHint(a.value ("fonthint") );
442 if (state == StateMapCenterNote)
445 lastBranch->setNote(no);
449 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
451 if (!a.value( "useOrientation").isEmpty() )
453 if (a.value ("useOrientation") =="true")
454 lastFloat->setUseOrientation (true);
456 lastFloat->setUseOrientation (false);
458 if (!a.value( "href").isEmpty() )
461 if (!lastFloat->load (parseHREF(a.value ("href") ) ))
463 QMessageBox::warning( 0, "Warning: " ,
464 "Couldn't load float image\n"+parseHREF(a.value ("href") ));
465 lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
471 if (!a.value( "floatExport").isEmpty() )
473 if (a.value ("floatExpofrt") =="true")
474 lastFloat->setFloatExport (true);
476 lastFloat->setFloatExport (false);
478 if (!a.value( "zPlane").isEmpty() )
479 lastFloat->setZ (a.value("zPlane").toInt ());
482 if (!a.value( "relPosX").isEmpty() )
484 if (!a.value( "relPosY").isEmpty() )
487 x=a.value("relPosX").toInt (&okx, 10);
488 y=a.value("relPosY").toInt (&oky, 10);
490 lastFloat->setRelPos (QPoint (x,y) );
492 // Couldn't read relPos
499 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
501 for (int i=1; i<=a.count(); i++)
502 htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
506 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
508 if (!a.value( "key").isEmpty() )
510 if (!a.value( "value").isEmpty() )
511 settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));