diff -r 000000000000 -r 4b65c117aab7 xml.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xml.cpp Mon Oct 06 09:18:11 2008 +0000 @@ -0,0 +1,519 @@ +#include "xml.h" + +#include +#include +#include +#include + +#include "misc.h" +#include "settings.h" + +#include "version.h" + +static BranchObj *lastBranch; +static FloatObj *lastFloat; + +extern Settings settings; + +mapBuilderHandler::mapBuilderHandler() {} + +mapBuilderHandler::~mapBuilderHandler() {} + +QString mapBuilderHandler::errorProtocol() { return errorProt; } + +bool mapBuilderHandler::startDocument() +{ + errorProt = ""; + state = StateInit; + laststate = StateInit; + branchDepth=0; + htmldata=""; + isVymPart=false; + return true; +} + + +QString mapBuilderHandler::parseHREF(QString href) +{ + QString type=href.section(":",0,0); + QString path=href.section(":",1,1); + if (!tmpDir.endsWith("/")) + return tmpDir + "/" + path; + else + return tmpDir + path; +} + +bool mapBuilderHandler::startElement ( const QString&, const QString&, + const QString& eName, const QXmlAttributes& atts ) +{ + QColor col; +// cout << "startElement <"< state="<setVersion(atts.value( "version" )); + if (!mc->checkVersion()) + QMessageBox::warning( 0, "Warning: Version Problem" , + "

Map is newer than VYM

" + "

The map you are just trying to load was " + "saved using vym " +atts.value("version")+". " + "The version of this vym is " __VYM_VERSION__ + ". If you run into problems after pressing " + "the ok-button below, updating vym should help."); + + } + if (loadMode==NewMap) + { + if (!atts.value( "author").isEmpty() ) + { + mc->setAuthor(atts.value( "author" ) ); + } + if (!atts.value( "comment").isEmpty() ) + { + mc->setComment (atts.value( "comment" ) ); + } + if (!atts.value( "backgroundColor").isEmpty() ) + { + col.setNamedColor(atts.value("backgroundColor")); + mc->getCanvas()->setBackgroundColor(col); + } + if (!atts.value( "linkColorHint").isEmpty() ) + { + if (atts.value("linkColorHint")=="HeadingColor") + me->setLinkColorHint(HeadingColor); + else + me->setLinkColorHint(DefaultColor); + } + if (!atts.value( "linkStyle").isEmpty() ) + { + QString s=atts.value("linkStyle"); + if (s=="StyleLine") + me->setLinkStyle(StyleLine); + else + if (s=="StyleParabel") + me->setLinkStyle(StyleParabel); + else + if (s=="StylePolyLine") + me->setLinkStyle(StylePolyLine); + else + me->setLinkStyle(StylePolyParabel); + } + if (!atts.value( "linkColor").isEmpty() ) + { + col.setNamedColor(atts.value("linkColor")); + me->setLinkColor(col); + } + } + } else if ( eName == "select" && state == StateMap ) + { + state=StateMapSelect; + } else if ( eName == "setting" && state == StateMap ) + { + state=StateMapSetting; + if (loadMode==NewMap) + readSettingAttr (atts); + } else if ( eName == "mapcenter" && state == StateMap ) + { + state=StateMapCenter; + if (loadMode==NewMap) + { + // Really use the found mapcenter as MCO in a new map + lastBranch=mc; // avoid empty pointer + } else + { + // Treat the found mapcenter as a branch + // in an existing map + LinkableMapObj* lmo=me->getSelection(); + if (lmo && (typeid(*lmo) == typeid(BranchObj) ) + || (typeid(*lmo) == typeid(MapCenterObj) ) ) + { + lastBranch=(BranchObj*)(lmo); + if (loadMode==ImportAdd) + { + lastBranch->addBranch(); + lastBranch=lastBranch->getLastBranch(); + } else + lastBranch->clear(); + } else + return false; + } + readBranchAttr (atts); + } else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter) + { + state=StateMapCenterStandardFlag; + } else if ( eName == "heading" && state == StateMapCenter) + { + state=StateMapCenterHeading; + if (!atts.value( "textColor").isEmpty() ) + { + col.setNamedColor(atts.value("textColor")); + lastBranch->setColor(col ,false ); + } + } else if ( eName == "note" && state == StateMapCenter) + { // only for backward compatibility (<1.4.6). Use htmlnote now. + state=StateMapCenterNote; + if (!readNoteAttr (atts) ) return false; + } else if ( eName == "htmlnote" && state == StateMapCenter) + { + laststate=state; + state=StateHtmlNote; + } else if ( eName == "floatimage" && state == StateMapCenter ) + { + state=StateMapCenterFloatImage; + lastBranch->addFloatImage(); + lastFloat=lastBranch->getLastFloatImage(); + if (!readFloatImageAttr(atts)) return false; + } else if ( eName == "branch" && state == StateMap) + { + // This is used in vymparts, which have no mapcenter! + isVymPart=true; + state=StateBranch; + LinkableMapObj* lmo=me->getSelection(); + if (lmo && (typeid(*lmo) == typeid(BranchObj) ) + || (typeid(*lmo) == typeid(MapCenterObj) ) ) + { + lastBranch=(BranchObj*)(lmo); + if (loadMode==ImportAdd) + { + lastBranch->addBranch(); + lastBranch=lastBranch->getLastBranch(); + } else + lastBranch->clear(); + } else + return false; + branchDepth=1; + readBranchAttr (atts); + } else if ( eName == "branch" && state == StateMapCenter) + { + state=StateBranch; + branchDepth=1; + lastBranch->addBranch(); + lastBranch=lastBranch->getLastBranch(); + readBranchAttr (atts); + } else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch) + { + state=StateBranchStandardFlag; + } else if ( eName == "heading" && state == StateBranch) + { + state=StateBranchHeading; + if (!atts.value( "textColor").isEmpty() ) + { + col.setNamedColor(atts.value("textColor")); + lastBranch->setColor(col ,false ); + } + } else if ( eName == "note" && state == StateBranch) + { + state=StateBranchNote; + if (!readNoteAttr (atts) ) return false; + } else if ( eName == "htmlnote" && state == StateBranch) + { + laststate=state; + state=StateHtmlNote; + no.clear(); + if (!atts.value( "fonthint").isEmpty() ) + no.setFontHint(atts.value ("fonthint") ); + } else if ( eName == "floatimage" && state == StateBranch ) + { + state=StateBranchFloatImage; + lastBranch->addFloatImage(); + lastFloat=lastBranch->getLastFloatImage(); + if (!readFloatImageAttr(atts)) return false; + } else if ( eName == "branch" && state == StateBranch ) + { + lastBranch->addBranch(); + lastBranch=lastBranch->getLastBranch(); + branchDepth++; + readBranchAttr (atts); + } else if ( eName == "html" && state == StateHtmlNote ) + { + state=StateHtml; + htmldata="<"+eName; + readHtmlAttr(atts); + htmldata+=">"; + } else if ( state == StateHtml ) + { + // accept all while in html mode, + htmldata+="<"+eName; + readHtmlAttr(atts); + htmldata+=">"; + } else + return false; // Error + return true; +} + +bool mapBuilderHandler::endElement ( const QString&, const QString&, const QString &eName) +{ +// cout << "endElement state="<"; + if (eName=="html") + { + state=StateHtmlNote; + htmldata.replace ("

","
"); + no.setNote (htmldata); + lastBranch->setNote (no); + return true; + } else + { + return true; + } + case StateMap: state=StateInit; return true; + default : + // even for HTML includes, this should never be reached + return false; + } +} + +bool mapBuilderHandler::characters ( const QString& ch) +{ + //cout << "characters \""<getMapCenter(); +} + +void mapBuilderHandler::setTmpDir (QString tp) +{ + tmpDir=tp; +} + +void mapBuilderHandler::setLoadMode (const LoadMode &lm) +{ + loadMode=lm; +} + +bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a) +{ + bool okx,oky; + int x,y; + if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2) + { + if (!a.value( "absPosY").isEmpty() ) + { + x=a.value("absPosX").toInt (&okx, 10); + y=a.value("absPosY").toInt (&oky, 10); + if (okx && oky) + lastBranch->move(x,y); + else + return false; // Couldn't read absPos + } + } + if (!a.value( "scrolled").isEmpty() ) + lastBranch->toggleScroll(); + if (!a.value( "url").isEmpty() ) + lastBranch->setURL (a.value ("url")); + if (!a.value( "vymLink").isEmpty() ) + lastBranch->setVymLink (a.value ("vymLink")); + if (!a.value( "frameType").isEmpty() ) + lastBranch->setFrameType (a.value("frameType")); + return true; +} + +bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a) +{ // only for backward compatibility (<1.4.6). Use htmlnote now. + no.clear(); + QString fn; + if (!a.value( "href").isEmpty() ) + { + // Load note + fn=parseHREF(a.value ("href") ); + QFile file (fn); + QString s; // Reading a note + + if ( !file.open( IO_ReadOnly) ) + { + qWarning ("mapBuilderHandler::readNoteAttr: Couldn't load "+fn); + return false; + } + QTextStream stream( &file ); + QString lines; + while ( !stream.eof() ) { + lines += stream.readLine()+"\n"; + } + file.close(); + // Convert to richtext + if ( !QStyleSheet::mightBeRichText( lines ) ) + { + // Here we are workarounding the QT conversion method: + // convertFromPlainText does not generate valid xml, needed + // for the parser, but just

and
without closing tags. + // So we have to add those by ourselves + //lines=quotemeta (lines); + lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal ); + lines.replace ("
","
"); + } + + lines =""+lines + "

"; + no.setNote (lines); + } + if (!a.value( "fonthint").isEmpty() ) + no.setFontHint(a.value ("fonthint") ); + if (state == StateMapCenterNote) + mc->setNote(no); + else + lastBranch->setNote(no); + return true; +} + +bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a) +{ + if (!a.value( "useOrientation").isEmpty() ) + { + if (a.value ("useOrientation") =="true") + lastFloat->setUseOrientation (true); + else + lastFloat->setUseOrientation (false); + } + if (!a.value( "href").isEmpty() ) + { + // Load FloatImage + if (!lastFloat->load (parseHREF(a.value ("href") ) )) + { + QMessageBox::warning( 0, "Warning: " , + "Couldn't load float image\n"+parseHREF(a.value ("href") )); + lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat))); + lastFloat=NULL; + return true; + } + + } + if (!a.value( "floatExport").isEmpty() ) + { + if (a.value ("floatExpofrt") =="true") + lastFloat->setFloatExport (true); + else + lastFloat->setFloatExport (false); + } + if (!a.value( "zPlane").isEmpty() ) + lastFloat->setZ (a.value("zPlane").toInt ()); + int x,y; + bool okx,oky; + if (!a.value( "relPosX").isEmpty() ) + { + if (!a.value( "relPosY").isEmpty() ) + { + // read relPos + x=a.value("relPosX").toInt (&okx, 10); + y=a.value("relPosY").toInt (&oky, 10); + if (okx && oky) + lastFloat->setRelPos (QPoint (x,y) ); + else + // Couldn't read relPos + return false; + } + } + return true; +} + +bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a) +{ + for (int i=1; i<=a.count(); i++) + htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\""; + return true; +} + +bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a) +{ + if (!a.value( "key").isEmpty() ) + { + if (!a.value( "value").isEmpty() ) + settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value")); + else + return false; + + } else + return false; + + return true; +}