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