xml-vym.cpp
author insilmaril
Wed, 24 Oct 2007 12:21:24 +0000
changeset 613 8fb5b3956b3e
parent 611 a1ae877b438d
child 647 bef71af3f6ab
permissions -rw-r--r--
Improved ascii export
     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     branchDepth=0;
    38 	htmldata="";
    39 	isVymPart=false;
    40     return true;
    41 }
    42 
    43 
    44 /*
    45 QString parseVYMHandler::parseHREF(QString href)
    46 {
    47 	QString type=href.section(":",0,0);
    48 	QString path=href.section(":",1,1);
    49 	if (!tmpDir.endsWith("/"))
    50 		return tmpDir + "/" + path;
    51 	else	
    52 		return tmpDir + path;
    53 }
    54 */
    55 bool parseVYMHandler::startElement  ( const QString&, const QString&,
    56                     const QString& eName, const QXmlAttributes& atts ) 
    57 {
    58     QColor col;
    59 	/* Testing
    60 	cout << "startElement <"<< eName.ascii()
    61 		<<">  state="<<state 
    62 		<<"  laststate="<<stateStack.last()
    63 		<<"   loadMode="<<loadMode
    64 		<<"       line="<<QXmlDefaultHandler::lineNumber()
    65 		<<endl;
    66 	*/	
    67 	stateStack.append (state);	
    68     if ( state == StateInit && (eName == "vymmap")  ) 
    69 	{
    70         state = StateMap;
    71 		if (!atts.value( "version").isEmpty() ) 
    72 		{
    73 			if (!checkVersion(atts.value("version")))
    74 				QMessageBox::warning( 0, "Warning: Version Problem" ,
    75 				   "<h3>Map is newer than VYM</h3>"
    76 				   "<p>The map you are just trying to load was "
    77 				   "saved using vym " +atts.value("version")+". "
    78 				   "The version of this vym is " + vymVersion + 
    79 				   ". If you run into problems after pressing "
    80 				   "the ok-button below, updating vym should help.");
    81 			else	   
    82 				mc->setVersion(atts.value( "version" ));
    83 
    84 		}
    85 		if (loadMode==NewMap || 
    86 			(loadMode==ImportReplace && me->getSelection()==mc))
    87 		{
    88 			if (!atts.value( "author").isEmpty() )
    89 			{
    90 				mc->setAuthor(atts.value( "author" ) );
    91 			}
    92 			if (!atts.value( "comment").isEmpty() )
    93 			{
    94 				mc->setComment (atts.value( "comment" ) );
    95 			}
    96 			if (!atts.value( "backgroundColor").isEmpty() )
    97 			{
    98 				col.setNamedColor(atts.value("backgroundColor"));
    99 				mc->getScene()->setBackgroundBrush(col);
   100 			}	    
   101 			if (!atts.value( "selectionColor").isEmpty() )
   102 			{
   103 				col.setNamedColor(atts.value("selectionColor"));
   104 				me->setSelectionColor(col);
   105 			}	    
   106 			if (!atts.value( "linkColorHint").isEmpty() ) 
   107 			{
   108 				if (atts.value("linkColorHint")=="HeadingColor")
   109 					me->setMapLinkColorHint(LinkableMapObj::HeadingColor);
   110 				else
   111 					me->setMapLinkColorHint(LinkableMapObj::DefaultColor);
   112 			}
   113 			if (!atts.value( "linkStyle").isEmpty() ) 
   114 			{
   115 				me->setMapLinkStyle(atts.value("linkStyle"));
   116 			}	
   117 			if (!atts.value( "linkColor").isEmpty() ) 
   118 			{
   119 				col.setNamedColor(atts.value("linkColor"));
   120 				me->setMapDefLinkColor(col);
   121 			}	
   122 			if (!atts.value( "defXLinkColor").isEmpty() ) 
   123 			{
   124 				col.setNamedColor(atts.value("defXLinkColor"));
   125 				me->setMapDefXLinkColor(col);
   126 			}	
   127 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   128 			{
   129 				me->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   130 			}	
   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 			lastBranch=mc;	// avoid empty pointer
   147 		} else
   148 		{
   149 			// Treat the found mapcenter as a branch 
   150 			// in an existing map
   151 			LinkableMapObj* lmo=me->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=me->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=mc;
   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 				branchDepth=1;
   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 		branchDepth=1;
   237 		lastBranch->addBranch();
   238 		lastBranch=lastBranch->getLastBranch();
   239 		readBranchAttr (atts);
   240 	} else if ( eName == "htmlnote" && state == StateBranch) 
   241 	{
   242 		laststate=state;
   243 		state=StateHtmlNote;
   244 		no.clear();
   245 		if (!atts.value( "fonthint").isEmpty() ) 
   246 			no.setFontHint(atts.value ("fonthint") );
   247 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   248 	{
   249 		laststate=state;
   250 		state=StateFrame;
   251 		if (!readFrameAttr(atts)) return false;
   252     } else if ( eName == "xlink" && state == StateBranch ) 
   253 	{
   254 		state=StateBranchXLink;
   255 		if (!readXLinkAttr (atts)) return false;
   256     } else if ( eName == "branch" && state == StateBranch ) 
   257 	{
   258         lastBranch->addBranch();
   259 		lastBranch=lastBranch->getLastBranch();		
   260         branchDepth++;
   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 </" <<eName.ascii()
   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 			me->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 && branchDepth<2) 
   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 (mc==lastBranch) mc->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 (mc->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=mc->findObjBySelect (a.value( "beginBranch"));
   582 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   583 			{
   584 				xlo->setBegin ((BranchObj*)lmo);
   585 				lmo=mc->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=mc->findID (a.value( "beginID"));
   602 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   603 			{
   604 				xlo->setBegin ((BranchObj*)lmo);
   605 				lmo=mc->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 (me->getDestPath(), a.value ("key"), a.value ("value"));
   632 		else
   633 			return false;
   634 		
   635 	} else
   636 		return false;
   637 	
   638 	return true;
   639 }