xml-vym.cpp
author insilmaril
Thu, 07 May 2009 08:48:53 +0000
changeset 766 7a71a914afdb
parent 763 8c028a5d9083
child 768 382a444f5b0c
permissions -rw-r--r--
Started to reanimate flags
     1 #include "xml-vym.h"
     2 
     3 #include <QMessageBox>
     4 #include <QColor>
     5 #include <QTextStream>
     6 #include <iostream>
     7 #include <typeinfo>
     8 
     9 #include "branchitem.h"
    10 #include "misc.h"
    11 #include "settings.h"
    12 #include "linkablemapobj.h"
    13 #include "mainwindow.h"
    14 #include "version.h"
    15 
    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 			lastBranchItem=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( "branchCount").isEmpty() )
    64 			{
    65 				branchesTotal=atts.value("branchCount").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 			lastBranchItem=(BranchItem*)model->createMapCenter(); 
   144 		} else
   145 		{
   146 			// Treat the found mapcenter as a branch 
   147 			// in an existing map
   148 			BranchItem *bi=model->getSelectedBranchItem();
   149 			if (bi)
   150 			{
   151 				lastBranchItem=bi;
   152 				if (loadMode==ImportAdd)
   153 				{
   154 					model->createBranch();
   155 					lastBranchItem=model->getSelectedBranchItem();
   156 				} //else
   157 					//FIXME-3 lastBranch->clear();
   158 			} else
   159 				return false;
   160 		}
   161 		readBranchAttr (atts);
   162 	} else if ( 
   163 		(eName == "standardflag" ||eName == "standardFlag") && 
   164 		(state == StateMapCenter || state==StateBranch)) 
   165 	{
   166 		state=StateStandardFlag;
   167 	} else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch)) 
   168 	{
   169 		laststate=state;
   170 		state=StateHeading;
   171 		if (!atts.value( "textColor").isEmpty() ) 
   172 		{
   173 			col.setNamedColor(atts.value("textColor"));
   174 			lastBranchItem->setHeadingColor(col );
   175 		}	    
   176 	} else if ( eName == "note" && 
   177 				(state == StateMapCenter ||state==StateBranch))
   178 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   179 		state=StateNote;
   180 		if (!readNoteAttr (atts) ) return false;
   181 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   182 	{
   183 		laststate=state;
   184 		state=StateHtmlNote;
   185     } else if ( eName == "floatimage" && 
   186 				(state == StateMapCenter ||state==StateBranch)) 
   187 	{
   188 		state=StateFloatImage;
   189         //FIXME-2lastBranch->addFloatImage();
   190 		//FIXME-2lastFloat=lastBranch->getLastFloatImage();
   191 		if (!readFloatImageAttr(atts)) return false;
   192 	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
   193 	{
   194 		// This is used in vymparts, which have no mapcenter!
   195 		isVymPart=true;
   196 		TreeItem *ti=model->getSelectedItem();
   197 		if (!ti)
   198 		{
   199 			// If a vym part is _loaded_ (not imported), 
   200 			// selection==lmo==NULL
   201 			// Treat it like ImportAdd then...
   202 			loadMode=ImportAdd;
   203 			// FIXME-3 lmo=model->first()->getLMO();		
   204 			// Do we really have no MCO when loading?????
   205 			cout << "xml-vym aborted\n";
   206 		}	
   207 		if (ti && ti->isBranchLikeType() )
   208 		{
   209 			lastBranchItem=(BranchItem*)ti;
   210 			if (eName=="branch")
   211 			{
   212 				state=StateBranch;
   213 				if (loadMode==ImportAdd)
   214 				{
   215 					model->createBranch();
   216 					lastBranchItem=model->getSelectedBranchItem();
   217 					
   218 				} else
   219 					//FIXME-2 lastBranch->clear();
   220 				readBranchAttr (atts);
   221 			} else if (eName=="floatimage")
   222 			{
   223 				state=StateFloatImage;
   224 				//FIXME-2 lastBranch->addFloatImage();
   225 				//FIXME-2 lastFloat=lastBranch->getLastFloatImage();
   226 				if (!readFloatImageAttr(atts)) return false;
   227 			} else return false;
   228 		} else return false;
   229 	} else if ( eName == "branch" && state == StateMapCenter) 
   230 	{
   231 		state=StateBranch;
   232 		lastBranchItem=model->createBranch();
   233 		readBranchAttr (atts);
   234 	} else if ( eName == "htmlnote" && state == StateBranch) 
   235 	{
   236 		laststate=state;
   237 		state=StateHtmlNote;
   238 		no.clear();
   239 		if (!atts.value( "fonthint").isEmpty() ) 
   240 			no.setFontHint(atts.value ("fonthint") );
   241 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   242 	{
   243 		laststate=state;
   244 		state=StateFrame;
   245 		if (!readFrameAttr(atts)) return false;
   246     } else if ( eName == "xlink" && state == StateBranch ) 
   247 	{
   248 		state=StateBranchXLink;
   249 		if (!readXLinkAttr (atts)) return false;
   250     } else if ( eName == "branch" && state == StateBranch ) 
   251 	{
   252 		lastBranchItem=model->createBranch();
   253 		readBranchAttr (atts);
   254     } else if ( eName == "html" && state == StateHtmlNote ) 
   255 	{
   256 		state=StateHtml;
   257 		htmldata="<"+eName;
   258 		readHtmlAttr(atts);
   259 		htmldata+=">";
   260     } else if ( state == StateHtml ) 
   261 	{
   262 		// accept all while in html mode,
   263 		htmldata+="<"+eName;
   264 		readHtmlAttr(atts);
   265 		htmldata+=">";
   266     } else
   267         return false;   // Error
   268     return true;
   269 }
   270 
   271 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   272 {
   273 	/* Testing
   274 	cout << "endElement </" <<qPrintable(eName)
   275 		<<">  state=" <<state 
   276 		<<"  laststate=" <<laststate
   277 		<<"  stateStack="<<stateStack.last() 
   278 		<<endl;
   279 	*/
   280     switch ( state ) 
   281 	{
   282 		case StateMap:
   283 			mainWindow->removeProgressBar();
   284 			break;
   285         case StateBranch: 
   286 			model->selectParent();
   287 			lastBranchItem=model->getSelectedBranchItem();
   288 			lastBranchItem->setLastSelectedBranch (0);	// Reset last selected to first child branch
   289             break;
   290         case StateHtml: 
   291 			htmldata+="</"+eName+">";
   292 			if (eName=="html")
   293 			{
   294 				state=StateHtmlNote;  
   295 				htmldata.replace ("<br></br>","<br />");
   296 				no.setNote (htmldata);
   297 				lastBranchItem->setNoteObj (no);
   298 			}	
   299 			break;
   300 		default: 
   301 			break;
   302     }  
   303 	state=stateStack.takeLast();	
   304 	return true;
   305 }
   306 
   307 bool parseVYMHandler::characters   ( const QString& ch)
   308 {
   309 	//cout << "characters \""<<ch.toStdString()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   310 
   311 	QString ch_org=quotemeta (ch);
   312     QString ch_simplified=ch.simplifyWhiteSpace();
   313     if ( ch_simplified.isEmpty() ) return true;
   314 
   315     switch ( state ) 
   316     {
   317         case StateInit: break;
   318         case StateMap: break; 
   319 		case StateMapSelect:
   320 			model->select(ch_simplified);
   321 			break;
   322 		case StateMapSetting:break;
   323         case StateMapCenter: break;
   324         case StateNote:
   325 			lastBranchItem->setNote(ch_simplified);
   326 			break;
   327         case StateBranch: break;
   328         case StateStandardFlag: 
   329             lastBranchItem->activateStandardFlag(ch_simplified); 
   330             break;
   331         case StateFloatImage: break;
   332         case StateHtmlNote: break;
   333         case StateHtml:
   334 			htmldata+=ch_org;
   335 			break;
   336         case StateHeading: 
   337             model->setHeading(ch_simplified);
   338             break;
   339         default: 
   340 			return false;
   341     }
   342     return true;
   343 }
   344 
   345 QString parseVYMHandler::errorString() 
   346 {
   347     return "the document is not in the VYM file format";
   348 }
   349 
   350 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)	//FIXME-2
   351 {
   352 	branchesCurrent++;
   353 	mainWindow->setProgressValue (branchesCurrent);
   354 
   355 	if (!readOOAttr(a)) return false;
   356 
   357 	if (!a.value( "scrolled").isEmpty() )
   358 		lastBranchItem->toggleScroll();
   359 /*
   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 */	
   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 (lastBranchItem)
   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 					lastBranchItem->setRelPos (QPointF(x,y));
   421 				else
   422 					return false;   // Couldn't read relPos
   423 			}           
   424 		}           
   425 		if (!a.value( "absPosX").isEmpty() && loadMode==NewMap ) 
   426 		{
   427 			if (!a.value( "absPosY").isEmpty() ) 
   428 			{
   429 				x=a.value("absPosX").toFloat (&okx);
   430 				y=a.value("absPosY").toFloat (&oky);
   431 				if (okx && oky  )
   432 					lastBranchItem->setAbsPos (QPointF(x,y));
   433 				else
   434 					return false;   // Couldn't read absPos
   435 			}           
   436 		}           
   437 		/* FIXME-2
   438 		if (!a.value( "id").isEmpty() ) 
   439 			lastBranchItem->setID (a.value ("id"));
   440 		if (!a.value( "url").isEmpty() ) 
   441 			lastOO->setURL (a.value ("url"));
   442 		if (!a.value( "vymLink").isEmpty() ) 
   443 			lastOO->setVymLink (a.value ("vymLink"));
   444 		if (!a.value( "hideInExport").isEmpty() ) 
   445 			if (a.value("hideInExport")=="true")
   446 				lastOO->getTreeItem()->setHideInExport(true);
   447 
   448 		if (!a.value( "hideLink").isEmpty()) 
   449 		{
   450 			if (a.value ("hideLink") =="true")
   451 				lastOO->setHideLinkUnselected(true);
   452 			else	
   453 				lastOO->setHideLinkUnselected(false);
   454 		}	
   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 	lastBranchItem->setNoteObj(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 			//FIXME-2 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 					//FIXME-2 if (lastBranchItem->depth()==0) lastBranch->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 (model->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 			TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
   582 			if (ti && ti->isBranchLikeType())
   583 			{
   584 			/* FIXME-2 xLinks
   585 				xlo->setBegin ((BranchObj*)lmo);
   586 				lmo=model->findBySelectString (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 
   598 	// object ID is used starting in version 1.8.76
   599 	/* FIXME-2 xLinks
   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 	*/
   619 	if (!success) delete (xlo);
   620 	return true;	// xLinks can only be established at the "end branch", return true
   621 }
   622 
   623 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   624 {
   625 	for (int i=1; i<=a.count(); i++)
   626 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   627 	return true;
   628 }
   629 
   630 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   631 {
   632 	if (!a.value( "key").isEmpty() ) 
   633 	{
   634 		if (!a.value( "value").isEmpty() ) 
   635 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   636 		else
   637 			return false;
   638 		
   639 	} else
   640 		return false;
   641 	
   642 	return true;
   643 }