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