xml-freemind.cpp
author insilmaril
Wed, 27 Feb 2008 16:09:06 +0000
changeset 676 3dabc6424d73
parent 666 51c69652a9d0
child 740 6dc0a20031f7
permissions -rw-r--r--
autosave fix
     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 <"<< qPrintable(eName)
    50 		<<">  state="<<state 
    51 		<<"  laststate="<<stateStack.last()
    52 		<<"   loadMode="<<loadMode
    53 	//	<<"       line="<<qPrintable (QXmlDefaultHandler::lineNumber())
    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 		cout <<"model="<<model<<"   first="<<model->first()<<endl;
    71 		model->first()->addBranch();
    72 		lastBranch=model->first()->getLastBranch();
    73 		lastBranch->move2RelPos (200,0);
    74 		lastBranch->setHeading ("  ");
    75 		model->first()->addBranch();
    76 		lastBranch=model->first()->getLastBranch();
    77 		lastBranch->move2RelPos (-200,0);
    78 		lastBranch->setHeading ("  ");
    79 		lastBranch=model->first();
    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 				model->select ("bo:1");
    88 				lastBranch=model->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 				model->select ("bo:0");
    98 				lastBranch=model->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&)
   204 {
   205 	/* Testing
   206 	cout << "endElement </" <<qPrintable(eName)
   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 \""<<qPrintable(ch)<<"\"  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