xml-freemind.cpp
changeset 566 ebebedffba50
child 574 56fa27b2be3e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xml-freemind.cpp	Mon Jul 23 12:42:02 2007 +0000
     1.3 @@ -0,0 +1,330 @@
     1.4 +#include "xml-freemind.h"
     1.5 +
     1.6 +#include <QMessageBox>
     1.7 +#include <QColor>
     1.8 +#include <QTextStream>
     1.9 +#include <iostream>
    1.10 +
    1.11 +#include "misc.h"
    1.12 +#include "settings.h"
    1.13 +#include "linkablemapobj.h"
    1.14 +#include "version.h"
    1.15 +
    1.16 +static BranchObj *lastBranch;
    1.17 +static FloatObj *lastFloat;
    1.18 +static OrnamentedObj *lastOO;
    1.19 +
    1.20 +extern Settings settings;
    1.21 +extern QString vymVersion;
    1.22 +
    1.23 +extern QString flagsPath;
    1.24 +
    1.25 +parseFMHandler::parseFMHandler() {}
    1.26 +
    1.27 +parseFMHandler::~parseFMHandler() {}
    1.28 +
    1.29 +QString parseFMHandler::errorProtocol() { return errorProt; }
    1.30 +
    1.31 +
    1.32 +bool parseFMHandler::startDocument()
    1.33 +{
    1.34 +    errorProt = "";
    1.35 +    state = StateInit;
    1.36 +    laststate = StateInit;
    1.37 +	stateStack.clear();
    1.38 +	stateStack.append(StateInit);
    1.39 +    branchDepth=0;
    1.40 +	htmldata="";
    1.41 +	isVymPart=false;
    1.42 +    return true;
    1.43 +}
    1.44 +
    1.45 +
    1.46 +QString parseFMHandler::parseHREF(QString href)
    1.47 +{
    1.48 +	QString type=href.section(":",0,0);
    1.49 +	QString path=href.section(":",1,1);
    1.50 +	if (!tmpDir.endsWith("/"))
    1.51 +		return tmpDir + "/" + path;
    1.52 +	else	
    1.53 +		return tmpDir + path;
    1.54 +}
    1.55 +
    1.56 +bool parseFMHandler::startElement  ( const QString&, const QString&,
    1.57 +                    const QString& eName, const QXmlAttributes& atts ) 
    1.58 +{
    1.59 +    QColor col;
    1.60 +	/* Testing
    1.61 +	cout << "startElement <"<< eName.ascii()
    1.62 +		<<">  state="<<state 
    1.63 +		<<"  laststate="<<stateStack.last()
    1.64 +		<<"   loadMode="<<loadMode
    1.65 +	//	<<"       line="<<QXmlDefaultHandler::lineNumber().ascii()
    1.66 +		<<endl;
    1.67 +	*/	
    1.68 +	stateStack.append (state);	
    1.69 +    if ( state == StateInit && (eName == "map")  ) 
    1.70 +	{
    1.71 +        state = StateMap;
    1.72 +		if (!atts.value( "version").isEmpty() ) 
    1.73 +		{
    1.74 +			QString v="0.8.0";
    1.75 +			if (!checkVersion(atts.value("version"),v))
    1.76 +				QMessageBox::warning( 0, "Warning: Version Problem" ,
    1.77 +				   "<h3>Freemind map is newer than version " +v +" </h3>"
    1.78 +				   "<p>The map you are just trying to load was "
    1.79 +				   "saved using freemind " +atts.value("version")+". "
    1.80 +				   "The version of this vym can parse freemind " + v +"."); 
    1.81 +		}
    1.82 +		mc->addBranch();
    1.83 +		lastBranch=mc->getLastBranch();
    1.84 +		lastBranch->move2RelPos (200,0);
    1.85 +		lastBranch->setHeading ("  ");
    1.86 +		mc->addBranch();
    1.87 +		lastBranch=mc->getLastBranch();
    1.88 +		lastBranch->move2RelPos (-200,0);
    1.89 +		lastBranch->setHeading ("  ");
    1.90 +		lastBranch=mc;
    1.91 +
    1.92 +	} else if ( eName == "node" &&  (state == StateMap || state == StateNode )) 
    1.93 +	{
    1.94 +		if (!atts.value( "POSITION").isEmpty() )
    1.95 +		{
    1.96 +			if (atts.value ("POSITION")=="left")
    1.97 +			{
    1.98 +				me->select ("bo:1");
    1.99 +				lastBranch=me->getSelectedBranch();
   1.100 +				if (lastBranch)
   1.101 +				{
   1.102 +					lastBranch->addBranch();
   1.103 +					lastBranch=lastBranch->getLastBranch();
   1.104 +					readNodeAttr (atts);
   1.105 +				}	
   1.106 +			} else if (atts.value ("POSITION")=="right")
   1.107 +			{
   1.108 +				me->select ("bo:0");
   1.109 +				lastBranch=me->getSelectedBranch();
   1.110 +				if (lastBranch)
   1.111 +				{
   1.112 +					lastBranch->addBranch();
   1.113 +					lastBranch=lastBranch->getLastBranch();
   1.114 +					readNodeAttr (atts);
   1.115 +				}	
   1.116 +			}
   1.117 +		} else
   1.118 +		{
   1.119 +			if (state!=StateMap)
   1.120 +			{
   1.121 +				lastBranch->addBranch();
   1.122 +				lastBranch=lastBranch->getLastBranch();
   1.123 +			}
   1.124 +			readNodeAttr (atts);
   1.125 +		}
   1.126 +		state=StateNode;
   1.127 +	} else if ( eName == "font" && state == StateNode) 
   1.128 +	{
   1.129 +		state=StateFont;
   1.130 +	} else if ( eName == "edge" && state == StateNode) 
   1.131 +	{
   1.132 +		state=StateEdge;
   1.133 +	} else if ( eName == "hook" && state == StateNode) 
   1.134 +	{
   1.135 +		state=StateHook;
   1.136 +	} else if ( eName == "icon" && state == StateNode) 
   1.137 +	{
   1.138 +		state=StateIcon;
   1.139 +		if (!atts.value("BUILTIN").isEmpty() )
   1.140 +		{
   1.141 +			QString f=atts.value("BUILTIN");
   1.142 +			QString v;
   1.143 +			if (f=="help")
   1.144 +				v="questionmark";
   1.145 +			else if (f=="messagebox_warning")
   1.146 +				v="freemind-warning"; 
   1.147 +			else if (f=="idea")
   1.148 +				v="lamp"; 
   1.149 +			else if (f=="button_ok")
   1.150 +				v="hook-green"; 
   1.151 +			else if (f=="button_cancel")
   1.152 +				v="cross-red"; 
   1.153 +			else if (f.contains("full-"))
   1.154 +				v=f.replace("full-","freemind-priority-"); //FIXME
   1.155 +			else if (f=="back")
   1.156 +				v="freemind-back"; 
   1.157 +			else if (f=="forward")
   1.158 +				v="freemind-forward"; 
   1.159 +			else if (f=="attach")
   1.160 +				v="freemind-attach"; 
   1.161 +			else if (f=="ksmiletris")
   1.162 +				v="smiley-good"; // 
   1.163 +			else if (f=="clanbomber")
   1.164 +				v="freemind-clanbomber"; 
   1.165 +			else if (f=="desktop_new")
   1.166 +				v="freemind-desktopnew"; 
   1.167 +			else if (f=="flag")
   1.168 +				v="freemind-flag"; 
   1.169 +			else if (f=="gohome")
   1.170 +				v="freemind-gohome"; 
   1.171 +			else if (f=="kaddressbook")
   1.172 +				v="freemind-kaddressbook"; 
   1.173 +			else if (f=="knotify")
   1.174 +				v="freemind-knotify"; 
   1.175 +			else if (f=="korn")
   1.176 +				v="freemind-korn";
   1.177 +			else if (f=="Mail")
   1.178 +				v="freemind-mail"; 
   1.179 +			else if (f=="password")
   1.180 +				v="freemind-password"; 
   1.181 +			else if (f=="pencil")
   1.182 +				v="freemind-pencil";
   1.183 +			else if (f=="stop")
   1.184 +				v="freemind-stop"; 
   1.185 +			else if (f=="wizard")
   1.186 +				v="freemind-wizard";
   1.187 +			else if (f=="xmag")
   1.188 +				v="freemind-xmag";
   1.189 +			else if (f=="bell")
   1.190 +				v="freemind-bell";
   1.191 +			else if (f=="bookmark")
   1.192 +				v="freemind-bookmark"; 
   1.193 +			else if (f=="penguin")
   1.194 +				v="freemind-penguin"; 
   1.195 +			else if (f=="licq")
   1.196 +				v="freemind-licq"; 
   1.197 +
   1.198 +			lastBranch->activateStandardFlag( v);
   1.199 +		}
   1.200 +	} else if ( eName == "arrowlink" && state == StateNode) 
   1.201 +	{
   1.202 +		state=StateArrowLink;
   1.203 +	} else if ( eName == "cloud" && state == StateNode) 
   1.204 +	{
   1.205 +		state=StateCloud;
   1.206 +	} else if ( eName == "text" && state == StateHook) 
   1.207 +	{
   1.208 +		state=StateText;
   1.209 +	} else 
   1.210 +        return false;   // Error
   1.211 +    return true;
   1.212 +}
   1.213 +
   1.214 +bool parseFMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   1.215 +{
   1.216 +	/* Testing
   1.217 +	cout << "endElement </" <<eName.ascii()
   1.218 +		<<">  state=" <<state 
   1.219 +		<<"  laststate=" <<laststate
   1.220 +		<<"  stateStack="<<stateStack.last() 
   1.221 +		<<endl;
   1.222 +	*/
   1.223 +    switch ( state ) 
   1.224 +	{
   1.225 +        case StateNode: 
   1.226 +			lastBranch=(BranchObj*)(lastBranch->getParObj());
   1.227 +            break;
   1.228 +		default: 
   1.229 +			break;
   1.230 +    }  
   1.231 +	state=stateStack.takeLast();	
   1.232 +	return true;
   1.233 +}
   1.234 +
   1.235 +bool parseFMHandler::characters   ( const QString& ch)
   1.236 +{
   1.237 +	//cout << "characters \""<<ch.ascii()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   1.238 +
   1.239 +	QString ch_org=quotemeta (ch);
   1.240 +    QString ch_simplified=ch.simplifyWhiteSpace();
   1.241 +    if ( ch_simplified.isEmpty() ) return true;
   1.242 +
   1.243 +    switch ( state ) 
   1.244 +    {
   1.245 +        case StateInit: break;
   1.246 +        case StateMap: break; 
   1.247 +        case StateNode: break; 
   1.248 +        case StateCloud: break; 
   1.249 +        case StateEdge: break; 
   1.250 +        case StateIcon: break; 
   1.251 +        case StateArrowLink: break; 
   1.252 +        case StateFont: break; 
   1.253 +        case StateHook: break; 
   1.254 +        case StateText: 
   1.255 +			lastBranch->setNote (ch_simplified);
   1.256 +			break; 
   1.257 +        default: 
   1.258 +			return false;
   1.259 +    }
   1.260 +    return true;
   1.261 +}
   1.262 +
   1.263 +QString parseFMHandler::errorString() 
   1.264 +{
   1.265 +    return "the document is not in the VYM file format";
   1.266 +}
   1.267 +
   1.268 +bool parseFMHandler::fatalError( const QXmlParseException& exception ) 
   1.269 +{
   1.270 +    errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
   1.271 +    .arg( exception.message() )
   1.272 +    .arg( exception.lineNumber() )
   1.273 +    .arg( exception.columnNumber() );
   1.274 +	// Try to read the bogus line
   1.275 +	errorProt+=QString("File is: %1\n").arg(inputFile);
   1.276 +	QString s;
   1.277 +	if (loadStringFromDisk (inputFile,s))
   1.278 +	{
   1.279 +		QStringList sl=QStringList::split ("\n",s);
   1.280 +		int i=1;
   1.281 +		QStringList::Iterator it = sl.begin();
   1.282 +		while (i<exception.lineNumber())
   1.283 +		{
   1.284 +			it++;
   1.285 +			i++;
   1.286 +		}
   1.287 +		s=*it;
   1.288 +		s.insert (exception.columnNumber()-1,"<ERROR>");
   1.289 +		errorProt+=s;
   1.290 +    }
   1.291 +    return QXmlDefaultHandler::fatalError( exception );
   1.292 +}
   1.293 +
   1.294 +void parseFMHandler::setMapEditor (MapEditor* e)
   1.295 +{
   1.296 +    me=e;
   1.297 +	mc=me->getMapCenter();
   1.298 +}
   1.299 +
   1.300 +void parseFMHandler::setTmpDir (QString tp)
   1.301 +{
   1.302 +	tmpDir=tp;
   1.303 +}
   1.304 +
   1.305 +void parseFMHandler::setInputFile (QString f)
   1.306 +{
   1.307 +	inputFile=f;
   1.308 +}
   1.309 +
   1.310 +void parseFMHandler::setLoadMode (const LoadMode &lm)
   1.311 +{
   1.312 +	loadMode=lm;
   1.313 +}
   1.314 +
   1.315 +bool parseFMHandler::readNodeAttr (const QXmlAttributes& a)
   1.316 +{
   1.317 +	lastOO=lastBranch;
   1.318 +
   1.319 +	if (a.value( "FOLDED")=="true" )
   1.320 +		lastBranch->toggleScroll();
   1.321 +	if (!a.value( "TEXT").isEmpty() )
   1.322 +		lastBranch->setHeading (a.value ("TEXT"));
   1.323 +
   1.324 +	if (!a.value( "COLOR").isEmpty() )
   1.325 +		lastBranch->setColor (QColor (a.value ("COLOR")));
   1.326 +
   1.327 +	if (!a.value( "LINK").isEmpty() )
   1.328 +		lastBranch->setURL (a.value ("LINK"));
   1.329 +
   1.330 +	return true;	
   1.331 +}
   1.332 +
   1.333 +