xml-freemind.cpp
author insilmaril
Tue, 04 Sep 2007 13:53:33 +0000
changeset 595 0bd62e09d061
parent 574 56fa27b2be3e
child 647 bef71af3f6ab
permissions -rw-r--r--
Fixed broken xLink handling
     1 #include "xml-freemind.h"
     2 
     3 #include <QMessageBox>
     4 #include <QColor>
     5 #include <QTextStream>
     6 #include <iostream>
     7 
     8 #include "misc.h"
     9 #include "settings.h"
    10 #include "linkablemapobj.h"
    11 #include "version.h"
    12 
    13 static BranchObj *lastBranch;
    14 static FloatObj *lastFloat;
    15 static OrnamentedObj *lastOO;
    16 
    17 extern Settings settings;
    18 extern QString vymVersion;
    19 
    20 extern QString flagsPath;
    21 
    22 bool parseFreemindHandler::startDocument()
    23 {
    24     errorProt = "";
    25     state = StateInit;
    26     laststate = StateInit;
    27 	stateStack.clear();
    28 	stateStack.append(StateInit);
    29     branchDepth=0;
    30 	isVymPart=false;
    31     return true;
    32 }
    33 
    34 
    35 QString parseFreemindHandler::parseHREF(QString href)
    36 {
    37 	QString type=href.section(":",0,0);
    38 	QString path=href.section(":",1,1);
    39 	if (!tmpDir.endsWith("/"))
    40 		return tmpDir + "/" + path;
    41 	else	
    42 		return tmpDir + path;
    43 }
    44 
    45 bool parseFreemindHandler::startElement  ( const QString&, const QString&,
    46                     const QString& eName, const QXmlAttributes& atts ) 
    47 {
    48     QColor col;
    49 	/* Testing
    50 	cout << "startElement <"<< eName.ascii()
    51 		<<">  state="<<state 
    52 		<<"  laststate="<<stateStack.last()
    53 		<<"   loadMode="<<loadMode
    54 	//	<<"       line="<<QXmlDefaultHandler::lineNumber().ascii()
    55 		<<endl;
    56 	*/	
    57 	stateStack.append (state);	
    58     if ( state == StateInit && (eName == "map")  ) 
    59 	{
    60         state = StateMap;
    61 		if (!atts.value( "version").isEmpty() ) 
    62 		{
    63 			QString v="0.8.0";
    64 			if (!checkVersion(atts.value("version"),v))
    65 				QMessageBox::warning( 0, "Warning: Version Problem" ,
    66 				   "<h3>Freemind map is newer than version " +v +" </h3>"
    67 				   "<p>The map you are just trying to load was "
    68 				   "saved using freemind " +atts.value("version")+". "
    69 				   "The version of this vym can parse freemind " + v +"."); 
    70 		}
    71 		mc->addBranch();
    72 		lastBranch=mc->getLastBranch();
    73 		lastBranch->move2RelPos (200,0);
    74 		lastBranch->setHeading ("  ");
    75 		mc->addBranch();
    76 		lastBranch=mc->getLastBranch();
    77 		lastBranch->move2RelPos (-200,0);
    78 		lastBranch->setHeading ("  ");
    79 		lastBranch=mc;
    80 
    81 	} else if ( eName == "node" &&  (state == StateMap || state == StateNode )) 
    82 	{
    83 		if (!atts.value( "POSITION").isEmpty() )
    84 		{
    85 			if (atts.value ("POSITION")=="left")
    86 			{
    87 				me->select ("bo:1");
    88 				lastBranch=me->getSelectedBranch();
    89 				if (lastBranch)
    90 				{
    91 					lastBranch->addBranch();
    92 					lastBranch=lastBranch->getLastBranch();
    93 					readNodeAttr (atts);
    94 				}	
    95 			} else if (atts.value ("POSITION")=="right")
    96 			{
    97 				me->select ("bo:0");
    98 				lastBranch=me->getSelectedBranch();
    99 				if (lastBranch)
   100 				{
   101 					lastBranch->addBranch();
   102 					lastBranch=lastBranch->getLastBranch();
   103 					readNodeAttr (atts);
   104 				}	
   105 			}
   106 		} else
   107 		{
   108 			if (state!=StateMap)
   109 			{
   110 				lastBranch->addBranch();
   111 				lastBranch=lastBranch->getLastBranch();
   112 			}
   113 			readNodeAttr (atts);
   114 		}
   115 		state=StateNode;
   116 	} else if ( eName == "font" && state == StateNode) 
   117 	{
   118 		state=StateFont;
   119 	} else if ( eName == "edge" && state == StateNode) 
   120 	{
   121 		state=StateEdge;
   122 	} else if ( eName == "hook" && state == StateNode) 
   123 	{
   124 		state=StateHook;
   125 	} else if ( eName == "icon" && state == StateNode) 
   126 	{
   127 		state=StateIcon;
   128 		if (!atts.value("BUILTIN").isEmpty() )
   129 		{
   130 			QString f=atts.value("BUILTIN");
   131 			QString v;
   132 			if (f=="help")
   133 				v="questionmark";
   134 			else if (f=="messagebox_warning")
   135 				v="freemind-warning"; 
   136 			else if (f=="idea")
   137 				v="lamp"; 
   138 			else if (f=="button_ok")
   139 				v="hook-green"; 
   140 			else if (f=="button_cancel")
   141 				v="cross-red"; 
   142 			else if (f.contains("full-"))
   143 				v=f.replace("full-","freemind-priority-"); 
   144 			else if (f=="back")
   145 				v="freemind-back"; 
   146 			else if (f=="forward")
   147 				v="freemind-forward"; 
   148 			else if (f=="attach")
   149 				v="freemind-attach"; 
   150 			else if (f=="ksmiletris")
   151 				v="smiley-good"; // 
   152 			else if (f=="clanbomber")
   153 				v="freemind-clanbomber"; 
   154 			else if (f=="desktop_new")
   155 				v="freemind-desktopnew"; 
   156 			else if (f=="flag")
   157 				v="freemind-flag"; 
   158 			else if (f=="gohome")
   159 				v="freemind-gohome"; 
   160 			else if (f=="kaddressbook")
   161 				v="freemind-kaddressbook"; 
   162 			else if (f=="knotify")
   163 				v="freemind-knotify"; 
   164 			else if (f=="korn")
   165 				v="freemind-korn";
   166 			else if (f=="Mail")
   167 				v="freemind-mail"; 
   168 			else if (f=="password")
   169 				v="freemind-password"; 
   170 			else if (f=="pencil")
   171 				v="freemind-pencil";
   172 			else if (f=="stop")
   173 				v="freemind-stop"; 
   174 			else if (f=="wizard")
   175 				v="freemind-wizard";
   176 			else if (f=="xmag")
   177 				v="freemind-xmag";
   178 			else if (f=="bell")
   179 				v="freemind-bell";
   180 			else if (f=="bookmark")
   181 				v="freemind-bookmark"; 
   182 			else if (f=="penguin")
   183 				v="freemind-penguin"; 
   184 			else if (f=="licq")
   185 				v="freemind-licq"; 
   186 
   187 			lastBranch->activateStandardFlag( v);
   188 		}
   189 	} else if ( eName == "arrowlink" && state == StateNode) 
   190 	{
   191 		state=StateArrowLink;
   192 	} else if ( eName == "cloud" && state == StateNode) 
   193 	{
   194 		state=StateCloud;
   195 	} else if ( eName == "text" && state == StateHook) 
   196 	{
   197 		state=StateText;
   198 	} else 
   199         return false;   // Error
   200     return true;
   201 }
   202 
   203 bool parseFreemindHandler::endElement  ( const QString&, const QString&, const QString &eName)
   204 {
   205 	/* Testing
   206 	cout << "endElement </" <<eName.ascii()
   207 		<<">  state=" <<state 
   208 		<<"  laststate=" <<laststate
   209 		<<"  stateStack="<<stateStack.last() 
   210 		<<endl;
   211 	*/
   212     switch ( state ) 
   213 	{
   214         case StateNode: 
   215 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   216             break;
   217 		default: 
   218 			break;
   219     }  
   220 	state=stateStack.takeLast();	
   221 	return true;
   222 }
   223 
   224 bool parseFreemindHandler::characters   ( const QString& ch)
   225 {
   226 	//cout << "characters \""<<ch.ascii()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   227 
   228 	QString ch_org=quotemeta (ch);
   229     QString ch_simplified=ch.simplifyWhiteSpace();
   230     if ( ch_simplified.isEmpty() ) return true;
   231 
   232     switch ( state ) 
   233     {
   234         case StateInit: break;
   235         case StateMap: break; 
   236         case StateNode: break; 
   237         case StateCloud: break; 
   238         case StateEdge: break; 
   239         case StateIcon: break; 
   240         case StateArrowLink: break; 
   241         case StateFont: break; 
   242         case StateHook: break; 
   243         case StateText: 
   244 			lastBranch->setNote (ch_simplified);
   245 			break; 
   246         default: 
   247 			return false;
   248     }
   249     return true;
   250 }
   251 
   252 QString parseFreemindHandler::errorString() 
   253 {
   254     return "the document is not in the Freemind file format";
   255 }
   256 
   257 bool parseFreemindHandler::readNodeAttr (const QXmlAttributes& a)
   258 {
   259 	lastOO=lastBranch;
   260 
   261 	if (a.value( "FOLDED")=="true" )
   262 		lastBranch->toggleScroll();
   263 	if (!a.value( "TEXT").isEmpty() )
   264 		lastBranch->setHeading (a.value ("TEXT"));
   265 
   266 	if (!a.value( "COLOR").isEmpty() )
   267 		lastBranch->setColor (QColor (a.value ("COLOR")));
   268 
   269 	if (!a.value( "LINK").isEmpty() )
   270 		lastBranch->setURL (a.value ("LINK"));
   271 
   272 	return true;	
   273 }
   274 
   275