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