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