xml-vym.cpp
author insilmaril
Mon, 08 Dec 2008 16:57:33 +0000
changeset 729 7ddbe3fa34a1
parent 727 96402b172173
child 735 84ae10f6e3a3
permissions -rw-r--r--
More fixes for selections
     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=model->first();	// avoid empty pointer
    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->getSelection();
   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->getSelection();
   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 			lmo=model->first();		
   186 		}	
   187 		if (lmo && ( (typeid(*lmo) == typeid(BranchObj) ) 
   188 				|| (typeid(*lmo) == typeid(MapCenterObj) ) ) )
   189 		{
   190 			lastBranch=(BranchObj*)(lmo);
   191 			if (eName=="branch")
   192 			{
   193 				state=StateBranch;
   194 				if (loadMode==ImportAdd)
   195 				{
   196 					model->createBranch();
   197 					lastBranch=model->getSelectedBranch();
   198 					
   199 				} else
   200 					lastBranch->clear();
   201 				readBranchAttr (atts);
   202 			} else if (eName=="floatimage")
   203 			{
   204 				state=StateFloatImage;
   205 				lastBranch->addFloatImage();
   206 				lastFloat=lastBranch->getLastFloatImage();
   207 				if (!readFloatImageAttr(atts)) return false;
   208 			} else return false;
   209 		} else return false;
   210 	} else if ( eName == "branch" && state == StateMapCenter) 
   211 	{
   212 		state=StateBranch;
   213 		model->createBranch();
   214 		lastBranch=model->getSelectedBranch();
   215 		readBranchAttr (atts);
   216 	} else if ( eName == "htmlnote" && state == StateBranch) 
   217 	{
   218 		laststate=state;
   219 		state=StateHtmlNote;
   220 		no.clear();
   221 		if (!atts.value( "fonthint").isEmpty() ) 
   222 			no.setFontHint(atts.value ("fonthint") );
   223 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   224 	{
   225 		laststate=state;
   226 		state=StateFrame;
   227 		if (!readFrameAttr(atts)) return false;
   228     } else if ( eName == "xlink" && state == StateBranch ) 
   229 	{
   230 		state=StateBranchXLink;
   231 		if (!readXLinkAttr (atts)) return false;
   232     } else if ( eName == "branch" && state == StateBranch ) 
   233 	{
   234 		model->createBranch();
   235 		lastBranch=model->getSelectedBranch();
   236 		readBranchAttr (atts);
   237     } else if ( eName == "html" && state == StateHtmlNote ) 
   238 	{
   239 		state=StateHtml;
   240 		htmldata="<"+eName;
   241 		readHtmlAttr(atts);
   242 		htmldata+=">";
   243     } else if ( state == StateHtml ) 
   244 	{
   245 		// accept all while in html mode,
   246 		htmldata+="<"+eName;
   247 		readHtmlAttr(atts);
   248 		htmldata+=">";
   249     } else
   250         return false;   // Error
   251     return true;
   252 }
   253 
   254 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   255 {
   256 	/* Testing
   257 	cout << "endElement </" <<qPrintable(eName)
   258 		<<">  state=" <<state 
   259 		<<"  laststate=" <<laststate
   260 		<<"  stateStack="<<stateStack.last() 
   261 		<<endl;
   262 	*/
   263     switch ( state ) 
   264 	{
   265         case StateBranch: 
   266 			model->selectParent();
   267 			lastBranch=model->getSelectedBranch();
   268             break;
   269         case StateHtml: 
   270 			htmldata+="</"+eName+">";
   271 			if (eName=="html")
   272 			{
   273 				state=StateHtmlNote;  
   274 				htmldata.replace ("<br></br>","<br />");
   275 				no.setNote (htmldata);
   276 				lastBranch->setNote (no);
   277 			}	
   278 			break;
   279 		default: 
   280 			break;
   281     }  
   282 	state=stateStack.takeLast();	
   283 	return true;
   284 }
   285 
   286 bool parseVYMHandler::characters   ( const QString& ch)
   287 {
   288 	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   289 
   290 	QString ch_org=quotemeta (ch);
   291     QString ch_simplified=ch.simplifyWhiteSpace();
   292     if ( ch_simplified.isEmpty() ) return true;
   293 
   294     switch ( state ) 
   295     {
   296         case StateInit: break;
   297         case StateMap: break; 
   298 		case StateMapSelect:
   299 			model->select(ch_simplified);
   300 			break;
   301 		case StateMapSetting:break;
   302         case StateMapCenter: break;
   303         case StateNote:
   304 			lastBranch->setNote(ch_simplified);
   305 			break;
   306         case StateBranch: break;
   307         case StateStandardFlag: 
   308             lastBranch->activateStandardFlag(ch_simplified); 
   309             break;
   310         case StateFloatImage: break;
   311         case StateHtmlNote: break;
   312         case StateHtml:
   313 			htmldata+=ch_org;
   314 			break;
   315         case StateHeading: 
   316             model->setHeading(ch_simplified);
   317             break;
   318         default: 
   319 			return false;
   320     }
   321     return true;
   322 }
   323 
   324 QString parseVYMHandler::errorString() 
   325 {
   326     return "the document is not in the VYM file format";
   327 }
   328 
   329 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)
   330 {
   331 	lastOO=lastBranch;
   332 	if (!readOOAttr(a)) return false;
   333 
   334 	if (!a.value( "scrolled").isEmpty() )
   335 		lastBranch->toggleScroll();
   336 	if (!a.value( "frameType").isEmpty() ) 
   337 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   338 
   339 	if (!a.value( "incImgV").isEmpty() ) 
   340 	{	
   341 		if (a.value("incImgV")=="true")
   342 			lastBranch->setIncludeImagesVer(true);
   343 		else	
   344 			lastBranch->setIncludeImagesVer(false);
   345 	}	
   346 	if (!a.value( "incImgH").isEmpty() ) 
   347 	{	
   348 		if (a.value("incImgH")=="true")
   349 			lastBranch->setIncludeImagesHor(true);
   350 		else	
   351 			lastBranch->setIncludeImagesHor(false);
   352 	}	
   353 	return true;	
   354 }
   355 
   356 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)
   357 {
   358 	bool ok;
   359 	int x;
   360 	if (lastOO)
   361 	{
   362 		if (!a.value( "frameType").isEmpty() ) 
   363 			lastOO->setFrameType (a.value("frameType"));
   364 		if (!a.value( "penColor").isEmpty() ) 
   365 			lastOO->setFramePenColor (a.value("penColor"));
   366 		if (!a.value( "brushColor").isEmpty() ) 
   367 			lastOO->setFrameBrushColor (a.value("brushColor"));
   368 		if (!a.value( "padding").isEmpty() ) 
   369 		{
   370 			x=a.value("padding").toInt(&ok);
   371 			if (ok) lastOO->setFramePadding(x);
   372 		}	
   373 		if (!a.value( "borderWidth").isEmpty() ) 
   374 		{
   375 			x=a.value("borderWidth").toInt(&ok);
   376 			if (ok) lastOO->setFrameBorderWidth(x);
   377 		}	
   378 	}		
   379 	return true;
   380 }
   381 
   382 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   383 {
   384 	if (lastOO)
   385 	{
   386 		bool okx,oky;
   387 		float x,y;
   388 		if (!a.value( "relPosX").isEmpty() ) 
   389 		{
   390 			if (!a.value( "relPosY").isEmpty() ) 
   391 			{
   392 				x=a.value("relPosX").toFloat (&okx);
   393 				y=a.value("relPosY").toFloat (&oky);
   394 				if (okx && oky  )
   395 				{
   396 					lastOO->setUseRelPos (true);
   397 					lastOO->move2RelPos (x,y);
   398 				}	
   399 				else
   400 					return false;   // Couldn't read relPos
   401 			}           
   402 		}           
   403 		if (!a.value( "absPosX").isEmpty() && loadMode==NewMap ) 
   404 		{
   405 			if (!a.value( "absPosY").isEmpty() ) 
   406 			{
   407 				x=a.value("absPosX").toFloat (&okx);
   408 				y=a.value("absPosY").toFloat (&oky);
   409 				if (okx && oky  )
   410 					lastOO->move(x,y);
   411 				else
   412 					return false;   // Couldn't read absPos
   413 			}           
   414 		}           
   415 		if (!a.value( "id").isEmpty() ) 
   416 			lastOO->setID (a.value ("id"));
   417 		if (!a.value( "url").isEmpty() ) 
   418 			lastOO->setURL (a.value ("url"));
   419 		if (!a.value( "vymLink").isEmpty() ) 
   420 			lastOO->setVymLink (a.value ("vymLink"));
   421 		if (!a.value( "hideInExport").isEmpty() ) 
   422 			if (a.value("hideInExport")=="true")
   423 				lastOO->setHideInExport(true);
   424 
   425 		if (!a.value( "hideLink").isEmpty()) 
   426 		{
   427 			if (a.value ("hideLink") =="true")
   428 				lastOO->setHideLinkUnselected(true);
   429 			else	
   430 				lastOO->setHideLinkUnselected(false);
   431 		}	
   432 	}
   433 	return true;	
   434 }
   435 
   436 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   437 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   438 	no.clear();
   439 	QString fn;
   440 	if (!a.value( "href").isEmpty() ) 
   441 	{
   442 		// Load note
   443 		fn=parseHREF(a.value ("href") );
   444 		QFile file (fn);
   445 		QString s;						// Reading a note
   446 
   447 		if ( !file.open( QIODevice::ReadOnly) )
   448 		{
   449 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   450 			return false;
   451 		}	
   452 		QTextStream stream( &file );
   453 		QString lines;
   454 		while ( !stream.atEnd() ) {
   455 			lines += stream.readLine()+"\n"; 
   456 		}
   457 		file.close();
   458 
   459 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   460 		no.setNote (lines);
   461 	}		
   462 	if (!a.value( "fonthint").isEmpty() ) 
   463 		no.setFontHint(a.value ("fonthint") );
   464 	lastBranch->setNote(no);
   465 	return true;
   466 }
   467 
   468 bool parseVYMHandler::readFloatImageAttr (const QXmlAttributes& a)
   469 {
   470 	lastOO=lastFloat;
   471 	
   472 	//if (!readOOAttr(a)) return false;
   473 
   474 	if (!a.value( "useOrientation").isEmpty() ) 
   475 	{
   476 		if (a.value ("useOrientation") =="true")
   477 			lastFloat->setUseOrientation (true);
   478 		else	
   479 			lastFloat->setUseOrientation (false);
   480 	}	
   481 	if (!a.value( "href").isEmpty() )
   482 	{
   483 		// Load FloatImage
   484 		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
   485 		{
   486 			QMessageBox::warning( 0, "Warning: " ,
   487 				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
   488 			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
   489 			lastFloat=NULL;
   490 			return true;
   491 		}
   492 		
   493 	}	
   494 	if (!a.value( "floatExport").isEmpty() ) 
   495 	{
   496 		// Only for compatibility. THis is not used since 1.7.11 
   497 		if (a.value ("floatExport") =="true")
   498 			lastFloat->setFloatExport(true);
   499 		else	
   500 			lastFloat->setFloatExport (false);
   501 	}	
   502 	if (!a.value( "zPlane").isEmpty() ) 
   503 		lastFloat->setZValue (a.value("zPlane").toInt ());
   504     float x,y;
   505     bool okx,oky;
   506 	if (!a.value( "relPosX").isEmpty() ) 
   507 	{
   508 		if (!a.value( "relPosY").isEmpty() ) 
   509 		{
   510 			// read relPos
   511 			x=a.value("relPosX").toFloat (&okx);
   512 			y=a.value("relPosY").toFloat (&oky);
   513 			if (okx && oky) 
   514 				
   515 				{
   516 					lastFloat->setRelPos (QPointF (x,y) );
   517 					// make sure floats in mapcenter are repositioned to relative pos
   518 					if (lastBranch->getDepth()==0) lastBranch->positionContents();
   519 				}
   520 			else
   521 				// Couldn't read relPos
   522 				return false;  
   523 		}           
   524 	}	
   525 	
   526 	if (!readOOAttr(a)) return false;
   527 
   528 	if (!a.value ("orgName").isEmpty() )
   529 	{
   530 		((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
   531 	}
   532 	return true;
   533 }
   534 
   535 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
   536 {
   537 	QColor col;
   538 	bool okx;
   539 	bool success=false;
   540 	XLinkObj *xlo=new XLinkObj (model->getScene());
   541 	if (!a.value( "color").isEmpty() ) 
   542 	{
   543 		col.setNamedColor(a.value("color"));
   544 		xlo->setColor (col);
   545 	}
   546 
   547 	if (!a.value( "width").isEmpty() ) 
   548 	{
   549 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   550 	}
   551 
   552 	// Connecting by select string for compatibility with version < 1.8.76
   553 	if (!a.value( "beginBranch").isEmpty() ) 
   554 	{ 
   555 		if (!a.value( "endBranch").isEmpty() ) 
   556 		{
   557 			LinkableMapObj *lmo=model->findObjBySelect (a.value( "beginBranch"));
   558 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   559 			{
   560 				xlo->setBegin ((BranchObj*)lmo);
   561 				lmo=model->findObjBySelect (a.value( "endBranch"));
   562 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   563 				{
   564 					xlo->setEnd ((BranchObj*)(lmo));
   565 					xlo->activate();
   566 					success=true;
   567 				}
   568 			}
   569 		}           
   570 	}	
   571 
   572 	// object ID is used starting in version 1.8.76
   573 	if (!a.value( "beginID").isEmpty() ) 
   574 	{ 
   575 		if (!a.value( "endID").isEmpty() ) 
   576 		{
   577 			LinkableMapObj *lmo=model->findID (a.value( "beginID"));
   578 			if (lmo && typeid (*lmo)==typeid (BranchObj))
   579 			{
   580 				xlo->setBegin ((BranchObj*)lmo);
   581 				lmo=model->findID (a.value( "endID"));
   582 				if (lmo && typeid (*lmo)==typeid (BranchObj))
   583 				{
   584 					xlo->setEnd ((BranchObj*)(lmo));
   585 					xlo->activate();
   586 					success=true;
   587 				}
   588 			}
   589 		}           
   590 	}	
   591 	if (!success) delete (xlo);
   592 	return true;	// xLinks can only be established at the "end branch", return true
   593 }
   594 
   595 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   596 {
   597 	for (int i=1; i<=a.count(); i++)
   598 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   599 	return true;
   600 }
   601 
   602 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   603 {
   604 	if (!a.value( "key").isEmpty() ) 
   605 	{
   606 		if (!a.value( "value").isEmpty() ) 
   607 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   608 		else
   609 			return false;
   610 		
   611 	} else
   612 		return false;
   613 	
   614 	return true;
   615 }