xml-vym.cpp
author insilmaril
Wed, 16 Jan 2008 15:45:18 +0000
changeset 653 8be6ac05960b
parent 652 700553af9ca5
child 660 d0e047b8d412
permissions -rw-r--r--
Fixed missing MapCenter
     1 #include "xml-vym.h"
     2 
     3 #include <QMessageBox>
     4 #include <QColor>
     5 #include <QTextStream>
     6 #include <iostream>
     7 #include <typeinfo>
     8 
     9 #include "misc.h"
    10 #include "settings.h"
    11 #include "linkablemapobj.h"
    12 #include "version.h"
    13 
    14 static BranchObj *lastBranch;
    15 static FloatObj *lastFloat;
    16 static OrnamentedObj *lastOO;
    17 
    18 extern Settings settings;
    19 extern QString vymVersion;
    20 
    21 /*
    22 parseVYMHandler::parseVYMHandler() {}
    23 
    24 parseVYMHandler::~parseVYMHandler() {}
    25 
    26 QString parseVYMHandler::errorProtocol() { return errorProt; }
    27 
    28 */
    29 
    30 bool parseVYMHandler::startDocument()
    31 {
    32     errorProt = "";
    33     state = StateInit;
    34     laststate = StateInit;
    35 	stateStack.clear();
    36 	stateStack.append(StateInit);
    37 	htmldata="";
    38 	isVymPart=false;
    39     return true;
    40 }
    41 
    42 
    43 /*
    44 QString parseVYMHandler::parseHREF(QString href)
    45 {
    46 	QString type=href.section(":",0,0);
    47 	QString path=href.section(":",1,1);
    48 	if (!tmpDir.endsWith("/"))
    49 		return tmpDir + "/" + path;
    50 	else	
    51 		return tmpDir + path;
    52 }
    53 */
    54 bool parseVYMHandler::startElement  ( const QString&, const QString&,
    55                     const QString& eName, const QXmlAttributes& atts ) 
    56 {
    57     QColor col;
    58 	/* Testing
    59 	cout << "startElement <"<< eName.ascii()
    60 		<<">  state="<<state 
    61 		<<"  laststate="<<stateStack.last()
    62 		<<"   loadMode="<<loadMode
    63 		<<"       line="<<QXmlDefaultHandler::lineNumber()
    64 		<<endl;
    65 	*/	
    66 	stateStack.append (state);	
    67     if ( state == StateInit && (eName == "vymmap")  ) 
    68 	{
    69         state = StateMap;
    70 
    71 		// Check version
    72 		if (!atts.value( "version").isEmpty() ) 
    73 		{
    74 			if (!checkVersion(atts.value("version")))
    75 				QMessageBox::warning( 0, "Warning: Version Problem" ,
    76 				   "<h3>Map is newer than VYM</h3>"
    77 				   "<p>The map you are just trying to load was "
    78 				   "saved using vym " +atts.value("version")+". "
    79 				   "The version of this vym is " + vymVersion + 
    80 				   ". If you run into problems after pressing "
    81 				   "the ok-button below, updating vym should help.");
    82 			else	   
    83 				model->setVersion(atts.value( "version" ));
    84 
    85 		}
    86 
    87 		if (loadMode==NewMap )
    88 		{
    89 			// Create mapCenter
    90 			model->clear();
    91 			lastBranch=model->first();	// avoid empty pointer
    92 
    93 			if (!atts.value( "author").isEmpty() )
    94 				model->setAuthor(atts.value( "author" ) );
    95 			if (!atts.value( "comment").isEmpty() )
    96 				model->setComment (atts.value( "comment" ) );
    97 			if (!atts.value( "backgroundColor").isEmpty() )
    98 			{
    99 				col.setNamedColor(atts.value("backgroundColor"));
   100 				model->getScene()->setBackgroundBrush(col);
   101 			}	    
   102 			if (!atts.value( "selectionColor").isEmpty() )
   103 			{
   104 				col.setNamedColor(atts.value("selectionColor"));
   105 				model->getMapEditor()->setSelectionColor(col);
   106 			}	    
   107 			if (!atts.value( "linkColorHint").isEmpty() ) 
   108 			{
   109 				if (atts.value("linkColorHint")=="HeadingColor")
   110 					model->getMapEditor()->setMapLinkColorHint(LinkableMapObj::HeadingColor);
   111 				else
   112 					model->getMapEditor()->setMapLinkColorHint(LinkableMapObj::DefaultColor);
   113 			}
   114 			if (!atts.value( "linkStyle").isEmpty() ) 
   115 				model->getMapEditor()->setMapLinkStyle(atts.value("linkStyle"));
   116 			if (!atts.value( "linkColor").isEmpty() ) 
   117 			{
   118 				col.setNamedColor(atts.value("linkColor"));
   119 				model->getMapEditor()->setMapDefLinkColor(col);
   120 			}	
   121 			if (!atts.value( "defXLinkColor").isEmpty() ) 
   122 			{
   123 				col.setNamedColor(atts.value("defXLinkColor"));
   124 				model->getMapEditor()->setMapDefXLinkColor(col);
   125 			}	
   126 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   127 				model->getMapEditor()->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   128 		}	
   129 	} else if ( eName == "select" && state == StateMap ) 
   130 	{
   131 		state=StateMapSelect;
   132 	} else if ( eName == "setting" && state == StateMap ) 
   133 	{
   134 		state=StateMapSetting;
   135 		if (loadMode==NewMap)
   136 			readSettingAttr (atts);
   137 	} else if ( eName == "mapcenter" && state == StateMap ) 
   138 	{
   139 		state=StateMapCenter;
   140 		if (loadMode==NewMap)
   141 		{	
   142 			// Really use the found mapcenter as MCO in a new map
   143 
   144 			// FIXME not working for multiple mapCenters yet:
   145 			lastBranch=model->addMapCenter();
   146 			//lastBranch=model->first();	// avoid empty pointer
   147 		} else
   148 		{
   149 			// Treat the found mapcenter as a branch 
   150 			// in an existing map
   151 			LinkableMapObj* lmo=model->getSelection();
   152 			if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   153 			        || (typeid(*lmo) == typeid(MapCenterObj) ) )
   154 			{
   155 				lastBranch=(BranchObj*)lmo;
   156 				if (loadMode==ImportAdd)
   157 				{
   158 					lastBranch->addBranch();
   159 					lastBranch=lastBranch->getLastBranch();
   160 				} else
   161 					lastBranch->clear();
   162 			} else
   163 				return false;
   164 		}
   165 		readBranchAttr (atts);
   166 	} else if ( 
   167 		(eName == "standardflag" ||eName == "standardFlag") && 
   168 		(state == StateMapCenter || state==StateBranch)) 
   169 	{
   170 		state=StateStandardFlag;
   171 	} else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch)) 
   172 	{
   173 		laststate=state;
   174 		state=StateHeading;
   175 		if (!atts.value( "textColor").isEmpty() ) 
   176 		{
   177 			col.setNamedColor(atts.value("textColor"));
   178 			lastBranch->setColor(col );
   179 		}	    
   180 	} else if ( eName == "note" && 
   181 				(state == StateMapCenter ||state==StateBranch))
   182 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   183 		state=StateNote;
   184 		if (!readNoteAttr (atts) ) return false;
   185 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   186 	{
   187 		laststate=state;
   188 		state=StateHtmlNote;
   189     } else if ( eName == "floatimage" && 
   190 				(state == StateMapCenter ||state==StateBranch)) 
   191 	{
   192 		state=StateFloatImage;
   193         lastBranch->addFloatImage();
   194 		lastFloat=lastBranch->getLastFloatImage();
   195 		if (!readFloatImageAttr(atts)) return false;
   196 	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
   197 	{
   198 		// This is used in vymparts, which have no mapcenter!
   199 		isVymPart=true;
   200 		LinkableMapObj* lmo=model->getSelection();
   201 		if (!lmo)
   202 		{
   203 			// If a vym part is _loaded_ (not imported), 
   204 			// selection==lmo==NULL
   205 			// Treat it like ImportAdd then...
   206 			loadMode=ImportAdd;
   207 			lmo=model->first();		// FIXME this used to be lmo=mc before
   208 		}	
   209 		if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
   210 				|| (typeid(*lmo) == typeid(MapCenterObj) ) )
   211 		{
   212 			lastBranch=(BranchObj*)(lmo);
   213 			if (eName=="branch")
   214 			{
   215 				state=StateBranch;
   216 				if (loadMode==ImportAdd)
   217 				{
   218 					lastBranch->addBranch();
   219 					lastBranch=lastBranch->getLastBranch();
   220 					
   221 				} else
   222 					lastBranch->clear();
   223 				readBranchAttr (atts);
   224 			} else if (eName=="floatimage")
   225 			{
   226 				state=StateFloatImage;
   227 				lastBranch->addFloatImage();
   228 				lastFloat=lastBranch->getLastFloatImage();
   229 				if (!readFloatImageAttr(atts)) return false;
   230 			} else return false;
   231 		} else return false;
   232 	} else if ( eName == "branch" && state == StateMapCenter) 
   233 	{
   234 		state=StateBranch;
   235 		lastBranch->addBranch();
   236 		lastBranch=lastBranch->getLastBranch();
   237 		readBranchAttr (atts);
   238 	} else if ( eName == "htmlnote" && state == StateBranch) 
   239 	{
   240 		laststate=state;
   241 		state=StateHtmlNote;
   242 		no.clear();
   243 		if (!atts.value( "fonthint").isEmpty() ) 
   244 			no.setFontHint(atts.value ("fonthint") );
   245 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   246 	{
   247 		laststate=state;
   248 		state=StateFrame;
   249 		if (!readFrameAttr(atts)) return false;
   250     } else if ( eName == "xlink" && state == StateBranch ) 
   251 	{
   252 		state=StateBranchXLink;
   253 		if (!readXLinkAttr (atts)) return false;
   254     } else if ( eName == "branch" && state == StateBranch ) 
   255 	{
   256         lastBranch->addBranch();
   257 		lastBranch=lastBranch->getLastBranch();		
   258 		readBranchAttr (atts);
   259     } else if ( eName == "html" && state == StateHtmlNote ) 
   260 	{
   261 		state=StateHtml;
   262 		htmldata="<"+eName;
   263 		readHtmlAttr(atts);
   264 		htmldata+=">";
   265     } else if ( state == StateHtml ) 
   266 	{
   267 		// accept all while in html mode,
   268 		htmldata+="<"+eName;
   269 		readHtmlAttr(atts);
   270 		htmldata+=">";
   271     } else
   272         return false;   // Error
   273     return true;
   274 }
   275 
   276 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   277 {
   278 	/* Testing
   279 	cout << "endElement </" <<eName.ascii()
   280 		<<">  state=" <<state 
   281 		<<"  laststate=" <<laststate
   282 		<<"  stateStack="<<stateStack.last() 
   283 		<<endl;
   284 	*/
   285     switch ( state ) 
   286 	{
   287         case StateBranch: 
   288 			lastBranch=(BranchObj*)(lastBranch->getParObj());
   289             break;
   290         case StateHtml: 
   291 			htmldata+="</"+eName+">";
   292 			if (eName=="html")
   293 			{
   294 				state=StateHtmlNote;  
   295 				htmldata.replace ("<br></br>","<br />");
   296 				no.setNote (htmldata);
   297 				lastBranch->setNote (no);
   298 			}	
   299 			break;
   300 		default: 
   301 			break;
   302     }  
   303 	state=stateStack.takeLast();	
   304 	return true;
   305 }
   306 
   307 bool parseVYMHandler::characters   ( const QString& ch)
   308 {
   309 	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   310 
   311 	QString ch_org=quotemeta (ch);
   312     QString ch_simplified=ch.simplifyWhiteSpace();
   313     if ( ch_simplified.isEmpty() ) return true;
   314 
   315     switch ( state ) 
   316     {
   317         case StateInit: break;
   318         case StateMap: break; 
   319 		case StateMapSelect:
   320 			model->select(ch_simplified);
   321 			break;
   322 		case StateMapSetting:break;
   323         case StateMapCenter: break;
   324         case StateNote:
   325 			lastBranch->setNote(ch_simplified);
   326 			break;
   327         case StateBranch: break;
   328         case StateStandardFlag: 
   329             lastBranch->activateStandardFlag(ch_simplified); 
   330             break;
   331         case StateFloatImage: break;
   332         case StateHtmlNote: break;
   333         case StateHtml:
   334 			htmldata+=ch_org;
   335 			break;
   336         case StateHeading: 
   337             lastBranch->setHeading(ch_simplified);
   338             break;
   339         default: 
   340 			return false;
   341     }
   342     return true;
   343 }
   344 
   345 QString parseVYMHandler::errorString() 
   346 {
   347     return "the document is not in the VYM file format";
   348 }
   349 
   350 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
   351 {
   352 	lastOO=lastBranch;
   353 	if (!readOOAttr(a)) return false;
   354 
   355 	if (!a.value( "scrolled").isEmpty() )
   356 		lastBranch->toggleScroll();
   357 	if (!a.value( "frameType").isEmpty() ) 
   358 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   359 
   360 	if (!a.value( "incImgV").isEmpty() ) 
   361 	{	
   362 		if (a.value("incImgV")=="true")
   363 			lastBranch->setIncludeImagesVer(true);
   364 		else	
   365 			lastBranch->setIncludeImagesVer(false);
   366 	}	
   367 	if (!a.value( "incImgH").isEmpty() ) 
   368 	{	
   369 		if (a.value("incImgH")=="true")
   370 			lastBranch->setIncludeImagesHor(true);
   371 		else	
   372 			lastBranch->setIncludeImagesHor(false);
   373 	}	
   374 	return true;	
   375 }
   376 
   377 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
   378 {
   379 	bool ok;
   380 	int x;
   381 	if (lastOO)
   382 	{
   383 		if (!a.value( "frameType").isEmpty() ) 
   384 			lastOO->setFrameType (a.value("frameType"));
   385 		if (!a.value( "penColor").isEmpty() ) 
   386 			lastOO->setFramePenColor (a.value("penColor"));
   387 		if (!a.value( "brushColor").isEmpty() ) 
   388 			lastOO->setFrameBrushColor (a.value("brushColor"));
   389 		if (!a.value( "padding").isEmpty() ) 
   390 		{
   391 			x=a.value("padding").toInt(&ok);
   392 			if (ok) lastOO->setFramePadding(x);
   393 		}	
   394 		if (!a.value( "borderWidth").isEmpty() ) 
   395 		{
   396 			x=a.value("borderWidth").toInt(&ok);
   397 			if (ok) lastOO->setFrameBorderWidth(x);
   398 		}	
   399 	}		
   400 	return true;
   401 }
   402 
   403 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   404 {
   405 	if (lastOO)
   406 	{
   407 		bool okx,oky;
   408 		float x,y;
   409 		if (!a.value( "relPosX").isEmpty() ) 
   410 		{
   411 			if (!a.value( "relPosY").isEmpty() ) 
   412 			{
   413 				x=a.value("relPosX").toFloat (&okx);
   414 				y=a.value("relPosY").toFloat (&oky);
   415 				if (okx && oky  )
   416 				{
   417 					lastOO->setUseRelPos (true);
   418 					lastOO->move2RelPos (x,y);
   419 				}	
   420 				else
   421 					return false;   // Couldn't read relPos
   422 			}           
   423 		}           
   424 		if (!a.value( "absPosX").isEmpty() && loadMode==NewMap ) 
   425 		{
   426 			if (!a.value( "absPosY").isEmpty() ) 
   427 			{
   428 				x=a.value("absPosX").toFloat (&okx);
   429 				y=a.value("absPosY").toFloat (&oky);
   430 				if (okx && oky  )
   431 					lastOO->move(x,y);
   432 				else
   433 					return false;   // Couldn't read absPos
   434 			}           
   435 		}           
   436 		if (!a.value( "id").isEmpty() ) 
   437 			lastOO->setID (a.value ("id"));
   438 		if (!a.value( "url").isEmpty() ) 
   439 			lastOO->setURL (a.value ("url"));
   440 		if (!a.value( "vymLink").isEmpty() ) 
   441 			lastOO->setVymLink (a.value ("vymLink"));
   442 		if (!a.value( "hideInExport").isEmpty() ) 
   443 			if (a.value("hideInExport")=="true")
   444 				lastOO->setHideInExport(true);
   445 
   446 		if (!a.value( "hideLink").isEmpty()) 
   447 		{
   448 			if (a.value ("hideLink") =="true")
   449 				lastOO->setHideLinkUnselected(true);
   450 			else	
   451 				lastOO->setHideLinkUnselected(false);
   452 		}	
   453 	}
   454 	return true;	
   455 }
   456 
   457 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   458 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   459 	no.clear();
   460 	QString fn;
   461 	if (!a.value( "href").isEmpty() ) 
   462 	{
   463 		// Load note
   464 		fn=parseHREF(a.value ("href") );
   465 		QFile file (fn);
   466 		QString s;						// Reading a note
   467 
   468 		if ( !file.open( QIODevice::ReadOnly) )
   469 		{
   470 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   471 			return false;
   472 		}	
   473 		QTextStream stream( &file );
   474 		QString lines;
   475 		while ( !stream.atEnd() ) {
   476 			lines += stream.readLine()+"\n"; 
   477 		}
   478 		file.close();
   479 
   480 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   481 		no.setNote (lines);
   482 	}		
   483 	if (!a.value( "fonthint").isEmpty() ) 
   484 		no.setFontHint(a.value ("fonthint") );
   485 	lastBranch->setNote(no);
   486 	return true;
   487 }
   488 
   489 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
   490 {
   491 	lastOO=lastFloat;
   492 	
   493 	//if (!readOOAttr(a)) return false;
   494 
   495 	if (!a.value( "useOrientation").isEmpty() ) 
   496 	{
   497 		if (a.value ("useOrientation") =="true")
   498 			lastFloat->setUseOrientation (true);
   499 		else	
   500 			lastFloat->setUseOrientation (false);
   501 	}	
   502 	if (!a.value( "href").isEmpty() )
   503 	{
   504 		// Load FloatImage
   505 		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
   506 		{
   507 			QMessageBox::warning( 0, "Warning: " ,
   508 				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
   509 			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
   510 			lastFloat=NULL;
   511 			return true;
   512 		}
   513 		
   514 	}	
   515 	if (!a.value( "floatExport").isEmpty() ) 
   516 	{
   517 		// Only for compatibility. THis is not used since 1.7.11 
   518 		if (a.value ("floatExport") =="true")
   519 			lastFloat->setFloatExport(true);
   520 		else	
   521 			lastFloat->setFloatExport (false);
   522 	}	
   523 	if (!a.value( "zPlane").isEmpty() ) 
   524 		lastFloat->setZValue (a.value("zPlane").toInt ());
   525     float x,y;
   526     bool okx,oky;
   527 	if (!a.value( "relPosX").isEmpty() ) 
   528 	{
   529 		if (!a.value( "relPosY").isEmpty() ) 
   530 		{
   531 			// read relPos
   532 			x=a.value("relPosX").toFloat (&okx);
   533 			y=a.value("relPosY").toFloat (&oky);
   534 			if (okx && oky) 
   535 				
   536 				{
   537 					lastFloat->setRelPos (QPointF (x,y) );
   538 					// make sure floats in mapcenter are repositioned to relative pos
   539 					if (lastBranch->getDepth()==0) lastBranch->positionContents();
   540 				}
   541 			else
   542 				// Couldn't read relPos
   543 				return false;  
   544 		}           
   545 	}	
   546 	
   547 	if (!readOOAttr(a)) return false;
   548 
   549 	if (!a.value ("orgName").isEmpty() )
   550 	{
   551 		((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
   552 	}
   553 	return true;
   554 }
   555 
   556 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
   557 {
   558 	QColor col;
   559 	bool okx;
   560 	bool success=false;
   561 	XLinkObj *xlo=new XLinkObj (model->getScene());
   562 	if (!a.value( "color").isEmpty() ) 
   563 	{
   564 		col.setNamedColor(a.value("color"));
   565 		xlo->setColor (col);
   566 	}
   567 
   568 	if (!a.value( "width").isEmpty() ) 
   569 	{
   570 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   571 	}
   572 
   573 	// Connecting by select string for compatibility with version < 1.8.76
   574 	if (!a.value( "beginBranch").isEmpty() ) 
   575 	{ 
   576 		if (!a.value( "endBranch").isEmpty() ) 
   577 		{
   578 			LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
   579 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   580 			{
   581 				xlo->setBegin ((BranchObj*)lmo);
   582 				lmo=model->findObjBySelect (a.value( "endBranch"));
   583 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   584 				{
   585 					xlo->setEnd ((BranchObj*)(lmo));
   586 					xlo->activate();
   587 					success=true;
   588 				}
   589 			}
   590 		}           
   591 	}	
   592 
   593 	// object ID is used starting in version 1.8.76
   594 	if (!a.value( "beginID").isEmpty() ) 
   595 	{ 
   596 		if (!a.value( "endID").isEmpty() ) 
   597 		{
   598 			LinkableMapObj *lmo=model->findID (a.value( "beginID"));
   599 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   600 			{
   601 				xlo->setBegin ((BranchObj*)lmo);
   602 				lmo=model->findID (a.value( "endID"));
   603 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   604 				{
   605 					xlo->setEnd ((BranchObj*)(lmo));
   606 					xlo->activate();
   607 					success=true;
   608 				}
   609 			}
   610 		}           
   611 	}	
   612 	if (!success) delete (xlo);
   613 	return true;	// xLinks can only be established at the "end branch", return true
   614 }
   615 
   616 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   617 {
   618 	for (int i=1; i<=a.count(); i++)
   619 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   620 	return true;
   621 }
   622 
   623 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   624 {
   625 	if (!a.value( "key").isEmpty() ) 
   626 	{
   627 		if (!a.value( "value").isEmpty() ) 
   628 			settings.setLocalEntry (model->getMapEditor()->getDestPath(), a.value ("key"), a.value ("value"));
   629 		else
   630 			return false;
   631 		
   632 	} else
   633 		return false;
   634 	
   635 	return true;
   636 }