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