xml-freemind.cpp
author insilmaril
Thu, 26 Mar 2009 07:49:17 +0000
changeset 746 ee6b0f3a4c2f
parent 741 1b4d1ea6ea8c
child 749 9ff332964015
permissions -rw-r--r--
Notes work again (to some degree)
     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 		//FIXME-3 TreeItem *ti=model->first();	//  this will be NULL !!!
    71 		TreeItem *ti=NULL;
    72 
    73 		BranchObj *bo;
    74 		if (ti->getType()==TreeItem::MapCenter)
    75 			bo=(BranchObj*)(ti->getLMO());
    76 		else
    77 			qWarning ("parseFreeMindHandler::startElement  no mapCenter!!");
    78 
    79 		//cout <<"model="<<model<<"   first="<<model->first()<<endl;
    80 
    81 		lastBranch=bo->addBranch();
    82 
    83 		lastBranch->move2RelPos (200,0);
    84 		lastBranch->setHeading ("  ");
    85 		lastBranch=bo->addBranch();
    86 		lastBranch->move2RelPos (-200,0);
    87 		lastBranch->setHeading ("  ");
    88 		lastBranch=bo;
    89 
    90 	} else if ( eName == "node" &&  (state == StateMap || state == StateNode )) 
    91 	{
    92 		if (!atts.value( "POSITION").isEmpty() )
    93 		{
    94 			if (atts.value ("POSITION")=="left")
    95 			{
    96 				model->select ("bo:1");
    97 				lastBranch=model->getSelectedBranch();
    98 				if (lastBranch)
    99 				{
   100 					lastBranch->addBranch();
   101 					lastBranch=lastBranch->getLastBranch();
   102 					readNodeAttr (atts);
   103 				}	
   104 			} else if (atts.value ("POSITION")=="right")
   105 			{
   106 				model->select ("bo:0");
   107 				lastBranch=model->getSelectedBranch();
   108 				if (lastBranch)
   109 				{
   110 					lastBranch->addBranch();
   111 					lastBranch=lastBranch->getLastBranch();
   112 					readNodeAttr (atts);
   113 				}	
   114 			}
   115 		} else
   116 		{
   117 			if (state!=StateMap)
   118 			{
   119 				lastBranch->addBranch();
   120 				lastBranch=lastBranch->getLastBranch();
   121 			}
   122 			readNodeAttr (atts);
   123 		}
   124 		state=StateNode;
   125 	} else if ( eName == "font" && state == StateNode) 
   126 	{
   127 		state=StateFont;
   128 	} else if ( eName == "edge" && state == StateNode) 
   129 	{
   130 		state=StateEdge;
   131 	} else if ( eName == "hook" && state == StateNode) 
   132 	{
   133 		state=StateHook;
   134 	} else if ( eName == "icon" && state == StateNode) 
   135 	{
   136 		state=StateIcon;
   137 		if (!atts.value("BUILTIN").isEmpty() )
   138 		{
   139 			QString f=atts.value("BUILTIN");
   140 			QString v;
   141 			if (f=="help")
   142 				v="questionmark";
   143 			else if (f=="messagebox_warning")
   144 				v="freemind-warning"; 
   145 			else if (f=="idea")
   146 				v="lamp"; 
   147 			else if (f=="button_ok")
   148 				v="hook-green"; 
   149 			else if (f=="button_cancel")
   150 				v="cross-red"; 
   151 			else if (f.contains("full-"))
   152 				v=f.replace("full-","freemind-priority-"); 
   153 			else if (f=="back")
   154 				v="freemind-back"; 
   155 			else if (f=="forward")
   156 				v="freemind-forward"; 
   157 			else if (f=="attach")
   158 				v="freemind-attach"; 
   159 			else if (f=="ksmiletris")
   160 				v="smiley-good"; // 
   161 			else if (f=="clanbomber")
   162 				v="freemind-clanbomber"; 
   163 			else if (f=="desktop_new")
   164 				v="freemind-desktopnew"; 
   165 			else if (f=="flag")
   166 				v="freemind-flag"; 
   167 			else if (f=="gohome")
   168 				v="freemind-gohome"; 
   169 			else if (f=="kaddressbook")
   170 				v="freemind-kaddressbook"; 
   171 			else if (f=="knotify")
   172 				v="freemind-knotify"; 
   173 			else if (f=="korn")
   174 				v="freemind-korn";
   175 			else if (f=="Mail")
   176 				v="freemind-mail"; 
   177 			else if (f=="password")
   178 				v="freemind-password"; 
   179 			else if (f=="pencil")
   180 				v="freemind-pencil";
   181 			else if (f=="stop")
   182 				v="freemind-stop"; 
   183 			else if (f=="wizard")
   184 				v="freemind-wizard";
   185 			else if (f=="xmag")
   186 				v="freemind-xmag";
   187 			else if (f=="bell")
   188 				v="freemind-bell";
   189 			else if (f=="bookmark")
   190 				v="freemind-bookmark"; 
   191 			else if (f=="penguin")
   192 				v="freemind-penguin"; 
   193 			else if (f=="licq")
   194 				v="freemind-licq"; 
   195 
   196 			lastBranch->activateStandardFlag( v);
   197 		}
   198 	} else if ( eName == "arrowlink" && state == StateNode) 
   199 	{
   200 		state=StateArrowLink;
   201 	} else if ( eName == "cloud" && state == StateNode) 
   202 	{
   203 		state=StateCloud;
   204 	} else if ( eName == "text" && state == StateHook) 
   205 	{
   206 		state=StateText;
   207 	} else 
   208         return false;   // Error
   209     return true;
   210 }
   211 
   212 bool parseFreemindHandler::endElement  ( const QString&, const QString&, const QString&)
   213 {
   214 	/* Testing
   215 	cout << "endElement </" <<qPrintable(eName)
   216 		<<">  state=" <<state 
   217 		<<"  laststate=" <<laststate
   218 		<<"  stateStack="<<stateStack.last() 
   219 		<<endl;
   220 	*/
   221     switch ( state ) 
   222 	{
   223         case StateNode: 
   224 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   225             break;
   226 		default: 
   227 			break;
   228     }  
   229 	state=stateStack.takeLast();	
   230 	return true;
   231 }
   232 
   233 bool parseFreemindHandler::characters   ( const QString& ch)
   234 {
   235 	//cout << "characters \""<<qPrintable(ch)<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   236 
   237 	QString ch_org=quotemeta (ch);
   238     QString ch_simplified=ch.simplifyWhiteSpace();
   239     if ( ch_simplified.isEmpty() ) return true;
   240 
   241     switch ( state ) 
   242     {
   243         case StateInit: break;
   244         case StateMap: break; 
   245         case StateNode: break; 
   246         case StateCloud: break; 
   247         case StateEdge: break; 
   248         case StateIcon: break; 
   249         case StateArrowLink: break; 
   250         case StateFont: break; 
   251         case StateHook: break; 
   252         case StateText: 
   253 			lastBranch->getTreeItem()->setNote (ch_simplified);
   254 			break; 
   255         default: 
   256 			return false;
   257     }
   258     return true;
   259 }
   260 
   261 QString parseFreemindHandler::errorString() 
   262 {
   263     return "the document is not in the Freemind file format";
   264 }
   265 
   266 bool parseFreemindHandler::readNodeAttr (const QXmlAttributes& a)
   267 {
   268 	lastOO=lastBranch;
   269 
   270 	if (a.value( "FOLDED")=="true" )
   271 		lastBranch->toggleScroll();
   272 	if (!a.value( "TEXT").isEmpty() )
   273 		lastBranch->setHeading (a.value ("TEXT"));
   274 
   275 	if (!a.value( "COLOR").isEmpty() )
   276 		lastBranch->setColor (QColor (a.value ("COLOR")));
   277 
   278 	if (!a.value( "LINK").isEmpty() )
   279 		lastBranch->setURL (a.value ("LINK"));
   280 
   281 	return true;	
   282 }
   283 
   284