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