xml.cpp
author insilmaril
Thu Mar 24 20:48:58 2005 +0000 (2005-03-24)
changeset 92 ec8c73975c1a
parent 0 7a96bd401351
child 95 f688a9913724
permissions -rw-r--r--
added modifier modes (color,link,copy). Not fully functional yet
     1 #include "xml.h"
     2 
     3 #include <qmessagebox.h>
     4 #include <qcolor.h>
     5 #include <qstylesheet.h>
     6 #include <iostream>
     7 
     8 #include "misc.h"
     9 #include "settings.h"
    10 
    11 #include "version.h"
    12 
    13 static BranchObj *lastBranch;
    14 static FloatObj *lastFloat;
    15 
    16 extern Settings settings;
    17 
    18 mapBuilderHandler::mapBuilderHandler() {}
    19 
    20 mapBuilderHandler::~mapBuilderHandler() {}
    21 
    22 QString mapBuilderHandler::errorProtocol() { return errorProt; }
    23 
    24 bool mapBuilderHandler::startDocument()
    25 {
    26     errorProt = "";
    27     state = StateInit;
    28     laststate = StateInit;
    29     branchDepth=0;
    30 	htmldata="";
    31 	isVymPart=false;
    32     return true;
    33 }
    34 
    35 
    36 QString mapBuilderHandler::parseHREF(QString href)
    37 {
    38 	QString type=href.section(":",0,0);
    39 	QString path=href.section(":",1,1);
    40 	if (!tmpDir.endsWith("/"))
    41 		return tmpDir + "/" + path;
    42 	else	
    43 		return tmpDir + path;
    44 }
    45 
    46 bool mapBuilderHandler::startElement  ( const QString&, const QString&,
    47                     const QString& eName, const QXmlAttributes& atts ) 
    48 {
    49     QColor col;
    50 	//cout << "startElement <"<<eName<<">  state="<<state <<"  laststate="<<laststate<<"   loadMode="<<loadMode<<endl;
    51     if ( state == StateInit && (eName == "vymmap")  ) 
    52 	{
    53         state = StateMap;
    54 		if (!atts.value( "version").isEmpty() ) 
    55 		{
    56 			mc->setVersion(atts.value( "version" ));
    57 			if (!mc->checkVersion())
    58 				QMessageBox::warning( 0, "Warning: Version Problem" ,
    59 				   "<h3>Map is newer than VYM</h3>"
    60 				   "<p>The map you are just trying to load was "
    61 				   "saved using vym " +atts.value("version")+". "
    62 				   "The version of this vym is " __VYM_VERSION__
    63 				   ". If you run into problems after pressing "
    64 				   "the ok-button below, updating vym should help.");
    65 
    66 		}
    67 		if (loadMode==NewMap)
    68 		{
    69 			if (!atts.value( "author").isEmpty() )
    70 			{
    71 				mc->setAuthor(atts.value( "author" ) );
    72 			}
    73 			if (!atts.value( "comment").isEmpty() )
    74 			{
    75 				mc->setComment (atts.value( "comment" ) );
    76 			}
    77 			if (!atts.value( "backgroundColor").isEmpty() )
    78 			{
    79 				col.setNamedColor(atts.value("backgroundColor"));
    80 				mc->getCanvas()->setBackgroundColor(col);
    81 			}	    
    82 			if (!atts.value( "linkColorHint").isEmpty() ) 
    83 			{
    84 				if (atts.value("linkColorHint")=="HeadingColor")
    85 					me->setLinkColorHint(HeadingColor);
    86 				else
    87 					me->setLinkColorHint(DefaultColor);
    88 			}
    89 			if (!atts.value( "linkStyle").isEmpty() ) 
    90 			{
    91 				QString s=atts.value("linkStyle");
    92 				if (s=="StyleLine")
    93 					me->setLinkStyle(StyleLine);
    94 				else	
    95 					if (s=="StyleParabel")
    96 						me->setLinkStyle(StyleParabel);
    97 					else	
    98 						if (s=="StylePolyLine")
    99 							me->setLinkStyle(StylePolyLine);
   100 						else	
   101 							me->setLinkStyle(StylePolyParabel);
   102 			}	
   103 			if (!atts.value( "linkColor").isEmpty() ) 
   104 			{
   105 				col.setNamedColor(atts.value("linkColor"));
   106 				me->setLinkColor(col);
   107 			}	
   108 		}	
   109 	} else if ( eName == "select" && state == StateMap ) 
   110 	{
   111 		state=StateMapSelect;
   112 	} else if ( eName == "setting" && state == StateMap ) 
   113 	{
   114 		state=StateMapSetting;
   115 		if (loadMode==NewMap)
   116 			readSettingAttr (atts);
   117 	} else if ( eName == "mapcenter" && state == StateMap ) 
   118 	{
   119 		state=StateMapCenter;
   120 		if (loadMode==NewMap)
   121 		{	
   122 			// Really use the found mapcenter as MCO in a new map
   123 			lastBranch=mc;	// avoid empty pointer
   124 		} else
   125 		{
   126 			// Treat the found mapcenter as a branch 
   127 			// in an existing map
   128 			LinkableMapObj* lmo=me->getSelection();
   129 			if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   130 			        || (typeid(*lmo) == typeid(MapCenterObj) ) )
   131 			{
   132 				lastBranch=(BranchObj*)(lmo);
   133 				if (loadMode==ImportAdd)
   134 				{
   135 					lastBranch->addBranch();
   136 					lastBranch=lastBranch->getLastBranch();
   137 				} else
   138 					lastBranch->clear();
   139 			} else
   140 				return false;
   141 		}
   142 		readBranchAttr (atts);
   143 	} else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter) 
   144 	{
   145 		state=StateMapCenterStandardFlag;
   146 	} else if ( eName == "heading" && state == StateMapCenter) 
   147 	{
   148 		state=StateMapCenterHeading;
   149 		if (!atts.value( "textColor").isEmpty() ) 
   150 		{
   151 			col.setNamedColor(atts.value("textColor"));
   152 			lastBranch->setColor(col ,false );
   153 		}	    
   154 	} else if ( eName == "note" && state == StateMapCenter) 
   155 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   156 		state=StateMapCenterNote;
   157 		if (!readNoteAttr (atts) ) return false;
   158 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   159 	{
   160 		laststate=state;
   161 		state=StateHtmlNote;
   162     } else if ( eName == "floatimage" && state == StateMapCenter ) 
   163 	{
   164 		state=StateMapCenterFloatImage;
   165         lastBranch->addFloatImage();
   166 		lastFloat=lastBranch->getLastFloatImage();
   167 		if (!readFloatImageAttr(atts)) return false;
   168 	} else if ( eName == "branch" && state == StateMap) 
   169 	{
   170 		// This is used in vymparts, which have no mapcenter!
   171 		isVymPart=true;
   172 		state=StateBranch;
   173 		LinkableMapObj* lmo=me->getSelection();
   174 		if (!lmo)
   175 		{
   176 			// If a vym part is _loaded_ (not imported), 
   177 			// selection==lmo==NULL
   178 			// Treat it like ImportAdd then...
   179 			loadMode=ImportAdd;
   180 			lmo=mc;
   181 		}	
   182 		if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   183 				|| (typeid(*lmo) == typeid(MapCenterObj) ) )
   184 		{
   185 			lastBranch=(BranchObj*)(lmo);
   186 			if (loadMode==ImportAdd)
   187 			{
   188 				lastBranch->addBranch();
   189 				lastBranch=lastBranch->getLastBranch();
   190 			} else
   191 				lastBranch->clear();
   192 		} else
   193 			return false;
   194 		branchDepth=1;
   195 		readBranchAttr (atts);
   196 	} else if ( eName == "branch" && state == StateMapCenter) 
   197 	{
   198 		state=StateBranch;
   199 		branchDepth=1;
   200 		lastBranch->addBranch();
   201 		lastBranch=lastBranch->getLastBranch();
   202 		readBranchAttr (atts);
   203 	} else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch) 
   204 	{
   205 		state=StateBranchStandardFlag;
   206 	} else if ( eName == "heading" && state == StateBranch) 
   207 	{
   208 		state=StateBranchHeading;
   209 		if (!atts.value( "textColor").isEmpty() ) 
   210 		{
   211 			col.setNamedColor(atts.value("textColor"));
   212 			lastBranch->setColor(col ,false );
   213 		}	    
   214     } else if ( eName == "note" && state == StateBranch) 
   215 	{
   216         state=StateBranchNote;
   217 		if (!readNoteAttr (atts) ) return false;
   218 	} else if ( eName == "htmlnote" && state == StateBranch) 
   219 	{
   220 		laststate=state;
   221 		state=StateHtmlNote;
   222 		no.clear();
   223 		if (!atts.value( "fonthint").isEmpty() ) 
   224 			no.setFontHint(atts.value ("fonthint") );
   225     } else if ( eName == "floatimage" && state == StateBranch ) 
   226 	{
   227 		state=StateBranchFloatImage;
   228         lastBranch->addFloatImage();
   229 		lastFloat=lastBranch->getLastFloatImage();
   230 		if (!readFloatImageAttr(atts)) return false;
   231     } else if ( eName == "branch" && state == StateBranch ) 
   232 	{
   233         lastBranch->addBranch();
   234 		lastBranch=lastBranch->getLastBranch();		
   235         branchDepth++;
   236 		readBranchAttr (atts);
   237     } else if ( eName == "html" && state == StateHtmlNote ) 
   238 	{
   239 		state=StateHtml;
   240 		htmldata="<"+eName;
   241 		readHtmlAttr(atts);
   242 		htmldata+=">";
   243     } else if ( state == StateHtml ) 
   244 	{
   245 		// accept all while in html mode,
   246 		htmldata+="<"+eName;
   247 		readHtmlAttr(atts);
   248 		htmldata+=">";
   249     } else
   250         return false;   // Error
   251     return true;
   252 }
   253 
   254 bool mapBuilderHandler::endElement  ( const QString&, const QString&, const QString &eName)
   255 {
   256 //	cout << "endElement </"<<eName<<">  state="<<state <<"  laststate="<<laststate<<endl;
   257     switch ( state ) 
   258 	{
   259         case StateMapSelect: state=StateMap;  return true;
   260         case StateMapSetting: state=StateMap;  return true;
   261         case StateMapCenter: state=StateMap;  return true;
   262         case StateMapCenterStandardFlag: state=StateMapCenter;  return true;
   263         case StateMapCenterHeading: state=StateMapCenter;  return true;
   264         case StateMapCenterNote: state=StateMapCenter;  return true;
   265         case StateMapCenterFloatImage: state=StateMapCenter;  return true;
   266         case StateBranch: 
   267             if (branchDepth>1) 
   268 			{
   269                 branchDepth--;
   270                 state=StateBranch;
   271             } else  
   272 			{
   273                 branchDepth=0;
   274 				if (isVymPart)
   275 					state=StateMap;
   276 				else
   277 					state=StateMapCenter;
   278             }   
   279 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   280              return true;
   281         case StateBranchStandardFlag: state=StateBranch;  return true;
   282         case StateBranchHeading: state=StateBranch;  return true;
   283         case StateBranchNote: state=StateBranch; return true;
   284         case StateBranchFloatImage: state=StateBranch;  return true;
   285         case StateHtmlNote: state=laststate; return true;
   286         case StateHtml: 
   287 			htmldata+="</"+eName+">";
   288 			if (eName=="html")
   289 			{
   290 				state=StateHtmlNote;  
   291 				htmldata.replace ("<br></br>","<br />");
   292 				no.setNote (htmldata);
   293 				lastBranch->setNote (no);
   294 				return true;
   295 			}	else
   296 			{
   297 				return true;
   298 			}	
   299         case StateMap: state=StateInit;  return true;
   300         default : 
   301 			// even for HTML includes, this should never be reached
   302 			return false;
   303     }   
   304 }
   305 
   306 bool mapBuilderHandler::characters   ( const QString& ch)
   307 {
   308 	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   309 
   310 	QString ch_org=quotemeta (ch);
   311     QString ch_simplified=ch.simplifyWhiteSpace();
   312     if ( ch_simplified.isEmpty() ) return true;
   313 
   314     switch ( state ) 
   315     {
   316         case StateInit: break;
   317         case StateMap: break; 
   318 		case StateMapSelect:
   319 			me->select(ch_simplified);
   320 			break;
   321 		case StateMapSetting:break;
   322         case StateMapCenter: break;
   323         case StateMapCenterStandardFlag: 
   324             lastBranch->activateStandardFlag(ch_simplified); 
   325             break;
   326         case StateMapCenterHeading: 
   327             lastBranch->setHeading(ch_simplified); 
   328             break;
   329         case StateMapCenterNote:
   330 			lastBranch->setNote(ch_simplified);
   331 			break;
   332         case StateBranch: break;
   333         case StateBranchStandardFlag: 
   334             lastBranch->activateStandardFlag(ch_simplified); 
   335             break;
   336         case StateBranchHeading: 
   337             lastBranch->setHeading(ch_simplified);
   338             break;
   339         case StateBranchNote: 
   340 			lastBranch->setNote(ch_simplified);
   341 			break;
   342         case StateBranchFloatImage: break;
   343         case StateHtmlNote: break;
   344         case StateHtml:
   345 			htmldata+=ch_org;
   346 			break;
   347         default: 
   348 			return false;
   349     }
   350     return true;
   351 }
   352 
   353 QString mapBuilderHandler::errorString() 
   354 {
   355     return "the document is not in the VYM file format";
   356 }
   357 
   358 bool mapBuilderHandler::fatalError( const QXmlParseException& exception ) 
   359 {
   360     errorProt += QString( "fatal parsing error: %1 in line %2, column %3\n")
   361     .arg( exception.message() )
   362     .arg( exception.lineNumber() )
   363     .arg( exception.columnNumber() );
   364 
   365     return QXmlDefaultHandler::fatalError( exception );
   366 }
   367 
   368 void mapBuilderHandler::setMapEditor (MapEditor* e)
   369 {
   370     me=e;
   371 	mc=me->getMapCenter();
   372 }
   373 
   374 void mapBuilderHandler::setTmpDir (QString tp)
   375 {
   376 	tmpDir=tp;
   377 }
   378 
   379 void mapBuilderHandler::setLoadMode (const LoadMode &lm)
   380 {
   381 	loadMode=lm;
   382 }
   383 
   384 bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
   385 {
   386 	bool okx,oky;
   387 	int x,y;
   388 	if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2) 
   389 	{
   390 		if (!a.value( "absPosY").isEmpty() ) 
   391 		{
   392 			x=a.value("absPosX").toInt (&okx, 10);
   393 			y=a.value("absPosY").toInt (&oky, 10);
   394 			if (okx && oky) 
   395 				lastBranch->move(x,y);
   396 			else
   397 				return false;   // Couldn't read absPos
   398 		}           
   399 	}           
   400 	if (!a.value( "scrolled").isEmpty() )
   401 		lastBranch->toggleScroll();
   402 	if (!a.value( "url").isEmpty() ) 
   403 		lastBranch->setURL (a.value ("url"));
   404 	if (!a.value( "vymLink").isEmpty() ) 
   405 		lastBranch->setVymLink (a.value ("vymLink"));
   406 	if (!a.value( "frameType").isEmpty() ) 
   407 		lastBranch->setFrameType (a.value("frameType"));
   408 	return true;	
   409 }
   410 
   411 bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
   412 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   413 	no.clear();
   414 	QString fn;
   415 	if (!a.value( "href").isEmpty() ) 
   416 	{
   417 		// Load note
   418 		fn=parseHREF(a.value ("href") );
   419 		QFile file (fn);
   420 		QString s;						// Reading a note
   421 
   422 		if ( !file.open( IO_ReadOnly) )
   423 		{
   424 			qWarning ("mapBuilderHandler::readNoteAttr:  Couldn't load "+fn);
   425 			return false;
   426 		}	
   427 		QTextStream stream( &file );
   428 		QString lines;
   429 		while ( !stream.eof() ) {
   430 			lines += stream.readLine()+"\n"; 
   431 		}
   432 		file.close();
   433 		// Convert to richtext
   434 		if ( !QStyleSheet::mightBeRichText( lines ) )
   435 		{
   436 			// Here we are workarounding the QT conversion method:
   437 			// convertFromPlainText does not generate valid xml, needed
   438 			// for the parser, but just <p> and <br> without closing tags.
   439 			// So we have to add those by ourselves
   440 			//lines=quotemeta (lines);
   441 			lines = QStyleSheet::convertFromPlainText( lines, QStyleSheetItem::WhiteSpaceNormal );
   442 			lines.replace ("<br>","<br />");
   443 		}	
   444 
   445 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   446 		no.setNote (lines);
   447 	}		
   448 	if (!a.value( "fonthint").isEmpty() ) 
   449 		no.setFontHint(a.value ("fonthint") );
   450 	if (state == StateMapCenterNote) 	
   451 		mc->setNote(no);
   452 	else
   453 		lastBranch->setNote(no);
   454 	return true;
   455 }
   456 
   457 bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
   458 {
   459 	if (!a.value( "useOrientation").isEmpty() ) 
   460 	{
   461 		if (a.value ("useOrientation") =="true")
   462 			lastFloat->setUseOrientation (true);
   463 		else	
   464 			lastFloat->setUseOrientation (false);
   465 	}	
   466 	if (!a.value( "href").isEmpty() )
   467 	{
   468 		// Load FloatImage
   469 		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
   470 		{
   471 			QMessageBox::warning( 0, "Warning: " ,
   472 				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
   473 			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
   474 			lastFloat=NULL;
   475 			return true;
   476 		}
   477 		
   478 	}	
   479 	if (!a.value( "floatExport").isEmpty() ) 
   480 	{
   481 		if (a.value ("floatExpofrt") =="true")
   482 			lastFloat->setFloatExport (true);
   483 		else	
   484 			lastFloat->setFloatExport (false);
   485 	}	
   486 	if (!a.value( "zPlane").isEmpty() ) 
   487 		lastFloat->setZ (a.value("zPlane").toInt ());
   488     int x,y;
   489     bool okx,oky;
   490 	if (!a.value( "relPosX").isEmpty() ) 
   491 	{
   492 		if (!a.value( "relPosY").isEmpty() ) 
   493 		{
   494 			// read relPos
   495 			x=a.value("relPosX").toInt (&okx, 10);
   496 			y=a.value("relPosY").toInt (&oky, 10);
   497 			if (okx && oky) 
   498 				lastFloat->setRelPos (QPoint (x,y) );
   499 			else
   500 				// Couldn't read relPos
   501 				return false;  
   502 		}           
   503 	}	
   504 	return true;
   505 }
   506 
   507 bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
   508 {
   509 	for (int i=1; i<=a.count(); i++)
   510 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   511 	return true;
   512 }
   513 
   514 bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
   515 {
   516 	if (!a.value( "key").isEmpty() ) 
   517 	{
   518 		if (!a.value( "value").isEmpty() ) 
   519 			settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));
   520 		else
   521 			return false;
   522 		
   523 	} else
   524 		return false;
   525 	
   526 	return true;
   527 }