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