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