xml-vym.cpp
author insilmaril
Mon, 29 Jun 2009 10:27:42 +0000
changeset 777 8acac4fade1b
parent 776 25e634a7e1dc
child 779 1fb50e79661c
permissions -rw-r--r--
more fixes
     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 ImageItem *lastImageItem;
    17 static MapItem *lastMI;
    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( "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 			lastBranch=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 				lastBranch=bi;
   152 				if (loadMode==ImportAdd)
   153 				{
   154 					lastBranch=model->createBranch(lastBranch);
   155 				} //else
   156 					//FIXME-3 lastBranch->clear();
   157 			} else
   158 				return false;
   159 		}
   160 		readBranchAttr (atts);
   161 	} else if ( 
   162 		(eName == "standardflag" ||eName == "standardFlag") && 
   163 		(state == StateMapCenter || state==StateBranch)) 
   164 	{
   165 		state=StateStandardFlag;
   166 	} else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch)) 
   167 	{
   168 		laststate=state;
   169 		state=StateHeading;
   170 		if (!atts.value( "textColor").isEmpty() ) 
   171 		{
   172 			col.setNamedColor(atts.value("textColor"));
   173 			lastBranch->setHeadingColor(col );
   174 		}	    
   175 	} else if ( eName == "note" && 
   176 				(state == StateMapCenter ||state==StateBranch))
   177 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   178 		state=StateNote;
   179 		if (!readNoteAttr (atts) ) return false;
   180 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   181 	{
   182 		laststate=state;
   183 		state=StateHtmlNote;
   184     } else if ( eName == "floatimage" && 
   185 				(state == StateMapCenter ||state==StateBranch)) 
   186 	{
   187 		state=StateImage;
   188 		lastImage=model->createImage(lastBranch);
   189 		if (!readImageAttr(atts)) return false;
   190 	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
   191 	{
   192 		// This is used in vymparts, which have no mapcenter!
   193 		isVymPart=true;
   194 		TreeItem *ti=model->getSelectedItem();
   195 		if (!ti)
   196 		{
   197 			// If a vym part is _loaded_ (not imported), 
   198 			// selection==lmo==NULL
   199 			// Treat it like ImportAdd then...
   200 			loadMode=ImportAdd;
   201 			// FIXME-3 lmo=model->first()->getLMO();		
   202 			// Do we really have no MCO when loading?????
   203 			cout << "xml-vym aborted\n";
   204 		}	
   205 		if (ti && ti->isBranchLikeType() )
   206 		{
   207 			lastBranch=(BranchItem*)ti;
   208 			if (eName=="branch")
   209 			{
   210 				state=StateBranch;
   211 				if (loadMode==ImportAdd)
   212 				{
   213 					lastBranch=model->createBranch(lastBranch);
   214 					
   215 				} else
   216 					//FIXME-2 lastBranch->clear();
   217 				readBranchAttr (atts);
   218 			} else if (eName=="floatimage")
   219 			{
   220 				state=StateImage;
   221 				lastImage=model->createImage (lastBranch);
   222 				if (!readImageAttr(atts)) return false;
   223 			} else return false;
   224 		} else return false;
   225 	} else if ( eName == "branch" && state == StateMapCenter) 
   226 	{
   227 		state=StateBranch;
   228 		lastBranch=model->createBranch(lastBranch);
   229 		readBranchAttr (atts);
   230 	} else if ( eName == "htmlnote" && state == StateBranch) 
   231 	{
   232 		laststate=state;
   233 		state=StateHtmlNote;
   234 		no.clear();
   235 		if (!atts.value( "fonthint").isEmpty() ) 
   236 			no.setFontHint(atts.value ("fonthint") );
   237 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   238 	{
   239 		laststate=state;
   240 		state=StateFrame;
   241 		if (!readFrameAttr(atts)) return false;
   242     } else if ( eName == "xlink" && state == StateBranch ) 
   243 	{
   244 		state=StateBranchXLink;
   245 		if (!readXLinkAttr (atts)) return false;
   246     } else if ( eName == "branch" && state == StateBranch ) 
   247 	{
   248 		lastBranch=model->createBranch(lastBranch);
   249 		readBranchAttr (atts);
   250     } else if ( eName == "html" && state == StateHtmlNote ) 
   251 	{
   252 		state=StateHtml;
   253 		htmldata="<"+eName;
   254 		readHtmlAttr(atts);
   255 		htmldata+=">";
   256     } else if ( state == StateHtml ) 
   257 	{
   258 		// accept all while in html mode,
   259 		htmldata+="<"+eName;
   260 		readHtmlAttr(atts);
   261 		htmldata+=">";
   262     } else
   263         return false;   // Error
   264     return true;
   265 }
   266 
   267 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   268 {
   269 	/* Testing
   270 	cout << "endElement </" <<qPrintable(eName)
   271 		<<">  state=" <<state 
   272 		<<"  laststate=" <<laststate
   273 		<<"  stateStack="<<stateStack.last() 
   274 		<<endl;
   275 	*/
   276     switch ( state ) 
   277 	{
   278 		case StateMap:
   279 			mainWindow->removeProgressBar();
   280 			break;
   281         case StateMapCenter: 
   282 			model->selectParent();	// FIXME-3 really needed to "select"? Maybe optimize...
   283 			model->emitDataHasChanged (lastBranch);
   284 			lastBranch=(BranchItem*)(lastBranch->parent());
   285 			lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   286             break;
   287         case StateBranch: 
   288 			model->selectParent();// FIXME-3 really needed to "select"? Maybe optimize...
   289 			model->emitDataHasChanged (lastBranch);
   290 			lastBranch=(BranchItem*)(lastBranch->parent());
   291 			lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   292             break;
   293         case StateHtml: 
   294 			htmldata+="</"+eName+">";
   295 			if (eName=="html")
   296 			{
   297 				state=StateHtmlNote;  
   298 				htmldata.replace ("<br></br>","<br />");
   299 				no.setNote (htmldata);
   300 				lastBranch->setNoteObj (no);
   301 			}	
   302 			break;
   303 		default: 
   304 			break;
   305     }  
   306 	state=stateStack.takeLast();	
   307 	return true;
   308 }
   309 
   310 bool parseVYMHandler::characters   ( const QString& ch)
   311 {
   312 	//cout << "characters \""<<ch.toStdString()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   313 
   314 	QString ch_org=quotemeta (ch);
   315     QString ch_simplified=ch.simplifyWhiteSpace();
   316     if ( ch_simplified.isEmpty() ) return true;
   317 
   318     switch ( state ) 
   319     {
   320         case StateInit: break;
   321         case StateMap: break; 
   322 		case StateMapSelect:
   323 			model->select(ch_simplified);
   324 			break;
   325 		case StateMapSetting:break;
   326         case StateMapCenter: break;
   327         case StateNote:
   328 			lastBranch->setNote(ch_simplified);
   329 			break;
   330         case StateBranch: break;
   331         case StateStandardFlag: 
   332             lastBranch->activateStandardFlag(ch_simplified); 
   333             break;
   334         case StateImage: break;
   335         case StateHtmlNote: break;
   336         case StateHtml:
   337 			htmldata+=ch_org;
   338 			break;
   339         case StateHeading: 
   340             lastBranch->setHeading(ch_simplified);
   341             break;
   342         default: 
   343 			return false;
   344     }
   345     return true;
   346 }
   347 
   348 QString parseVYMHandler::errorString() 
   349 {
   350     return "the document is not in the VYM file format";
   351 }
   352 
   353 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)	//FIXME-2
   354 {
   355 	mainWindow->setProgressValue (branchesCurrent++);
   356 
   357 	lastMI=lastBranch;
   358 
   359 	if (!readOOAttr(a)) return false;
   360 
   361 	if (!a.value( "scrolled").isEmpty() )
   362 		lastBranch->toggleScroll();
   363 /*
   364 	if (!a.value( "frameType").isEmpty() ) 
   365 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   366 
   367 	if (!a.value( "incImgV").isEmpty() ) 
   368 	{	
   369 		if (a.value("incImgV")=="true")
   370 			lastBranch->setIncludeImagesVer(true);
   371 		else	
   372 			lastBranch->setIncludeImagesVer(false);
   373 	}	
   374 	if (!a.value( "incImgH").isEmpty() ) 
   375 	{	
   376 		if (a.value("incImgH")=="true")
   377 			lastBranch->setIncludeImagesHor(true);
   378 		else	
   379 			lastBranch->setIncludeImagesHor(false);
   380 	}	
   381 */	
   382 	return true;	
   383 }
   384 
   385 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)	// FIXME-4 does not work if there is no lmo for treeitem
   386 {
   387 	if (lastMI)
   388 	{
   389 		OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO()); 
   390 		if (oo)
   391 		{
   392 			bool ok;
   393 			int x;
   394 			{
   395 				if (!a.value( "frameType").isEmpty() ) 
   396 					oo->setFrameType (a.value("frameType"));
   397 				if (!a.value( "penColor").isEmpty() ) 
   398 					oo->setFramePenColor (a.value("penColor"));
   399 				if (!a.value( "brushColor").isEmpty() ) 
   400 					oo->setFrameBrushColor (a.value("brushColor"));
   401 				if (!a.value( "padding").isEmpty() ) 
   402 				{
   403 					x=a.value("padding").toInt(&ok);
   404 					if (ok) oo->setFramePadding(x);
   405 				}	
   406 				if (!a.value( "borderWidth").isEmpty() ) 
   407 				{
   408 					x=a.value("borderWidth").toInt(&ok);
   409 					if (ok) oo->setFrameBorderWidth(x);
   410 				}	
   411 			}		
   412 			return true;
   413 		}
   414 	}
   415 	return false;
   416 }
   417 
   418 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   419 {
   420 	if (lastMI)	//FIXME-1  below lastBranch is used, not lastMI. So it might not work for images
   421 	{
   422 		bool okx,oky;
   423 		float x,y;
   424 		if (!a.value( "relPosX").isEmpty() ) 
   425 		{
   426 			if (!a.value( "relPosY").isEmpty() ) 
   427 			{
   428 				x=a.value("relPosX").toFloat (&okx);
   429 				y=a.value("relPosY").toFloat (&oky);
   430 				if (okx && oky  )
   431 					lastBranch->setRelPos (QPointF(x,y));
   432 				else
   433 					return false;   // Couldn't read relPos
   434 			}           
   435 		}           
   436 		if (!a.value( "absPosX").isEmpty() && loadMode==NewMap ) 
   437 		{
   438 			if (!a.value( "absPosY").isEmpty() ) 
   439 			{
   440 				x=a.value("absPosX").toFloat (&okx);
   441 				y=a.value("absPosY").toFloat (&oky);
   442 				if (okx && oky  )
   443 					lastBranch->setAbsPos (QPointF(x,y));
   444 				else
   445 					return false;   // Couldn't read absPos
   446 			}           
   447 		}           
   448 		//if (!a.value( "id").isEmpty() ) 
   449 		//	lastBranch->setID (a.value ("id"));
   450 			
   451 		if (!a.value( "url").isEmpty() ) 
   452 			lastBranch->setURL (a.value ("url"));
   453 		if (!a.value( "vymLink").isEmpty() ) 
   454 			lastBranch->setVymLink (a.value ("vymLink"));
   455 		if (!a.value( "hideInExport").isEmpty() ) 
   456 			if (a.value("hideInExport")=="true")
   457 				lastBranch->setHideInExport(true);
   458 
   459 		/* FIXME-2
   460 		if (!a.value( "hideLink").isEmpty()) 
   461 		{
   462 			if (a.value ("hideLink") =="true")
   463 				lastOO->setHideLinkUnselected(true);
   464 			else	
   465 				lastOO->setHideLinkUnselected(false);
   466 		}	
   467 		*/
   468 	}
   469 	return true;	
   470 }
   471 
   472 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   473 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   474 	no.clear();
   475 	QString fn;
   476 	if (!a.value( "href").isEmpty() ) 
   477 	{
   478 		// Load note
   479 		fn=parseHREF(a.value ("href") );
   480 		QFile file (fn);
   481 		QString s;						// Reading a note
   482 
   483 		if ( !file.open( QIODevice::ReadOnly) )
   484 		{
   485 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   486 			return false;
   487 		}	
   488 		QTextStream stream( &file );
   489 		QString lines;
   490 		while ( !stream.atEnd() ) {
   491 			lines += stream.readLine()+"\n"; 
   492 		}
   493 		file.close();
   494 
   495 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   496 		no.setNote (lines);
   497 	}		
   498 	if (!a.value( "fonthint").isEmpty() ) 
   499 		no.setFontHint(a.value ("fonthint") );
   500 	lastBranch->setNoteObj(no);
   501 	return true;
   502 }
   503 
   504 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
   505 {
   506 	lastMI=lastImage;
   507 	
   508 	//if (!readOOAttr(a)) return false;
   509 
   510 	/* FIXME-1 if (!a.value( "useOrientation").isEmpty() ) 
   511 	{
   512 		if (a.value ("useOrientation") =="true")
   513 			lastImage->setUseOrientation (true);
   514 		else	
   515 			lastImage->setUseOrientation (false);
   516 	}	
   517 	*/
   518 	if (!a.value( "href").isEmpty() )
   519 	{
   520 		// Load Image
   521 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   522 		{
   523 			QMessageBox::warning( 0, "Warning: " ,
   524 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   525 			lastImage=NULL;
   526 			return true;
   527 		}
   528 		
   529 	}	
   530 	if (!a.value( "zPlane").isEmpty() ) 
   531 		lastImage->setZValue (a.value("zPlane").toInt ());
   532     float x,y;
   533     bool okx,oky;
   534 	if (!a.value( "relPosX").isEmpty() ) 
   535 	{
   536 		if (!a.value( "relPosY").isEmpty() ) 
   537 		{
   538 			// read relPos
   539 			x=a.value("relPosX").toFloat (&okx);
   540 			y=a.value("relPosY").toFloat (&oky);
   541 			if (okx && oky) 
   542 				lastImage->setRelPos (QPointF (x,y) );
   543 			else
   544 				// Couldn't read relPos
   545 				return false;  
   546 		}           
   547 	}	
   548 	
   549 	//FIXME-3 if (!readOOAttr(a)) return false;
   550 
   551 	if (!a.value ("originalName").isEmpty() )
   552 	{
   553 		lastImage->setOriginalFilename (a.value("originalName"));
   554 	}
   555 	return true;
   556 }
   557 
   558 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
   559 {
   560 	QColor col;
   561 	bool okx;
   562 	bool success=false;
   563 	XLinkObj *xlo=new XLinkObj (model->getScene());
   564 	if (!a.value( "color").isEmpty() ) 
   565 	{
   566 		col.setNamedColor(a.value("color"));
   567 		xlo->setColor (col);
   568 	}
   569 
   570 	if (!a.value( "width").isEmpty() ) 
   571 	{
   572 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   573 	}
   574 
   575 	// Connecting by select string for compatibility with version < 1.8.76
   576 	if (!a.value( "beginBranch").isEmpty() ) 
   577 	{ 
   578 		if (!a.value( "endBranch").isEmpty() ) 
   579 		{
   580 			TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
   581 			if (ti && ti->isBranchLikeType())
   582 			{
   583 			/* FIXME-2 xLinks
   584 				xlo->setBegin ((BranchObj*)lmo);
   585 				lmo=model->findBySelectString (a.value( "endBranch"));
   586 				if (lmo && typid (*lmo)==typid (BranchObj))
   587 				{
   588 					xlo->setEnd ((BranchObj*)(lmo));
   589 					xlo->activate();
   590 					success=true;
   591 				}
   592 			*/
   593 			}
   594 		}           
   595 	}	
   596 
   597 	// object ID is used starting in version 1.8.76
   598 	/* FIXME-2 xLinks
   599 	if (!a.value( "beginID").isEmpty() ) 
   600 	{ 
   601 		if (!a.value( "endID").isEmpty() ) 
   602 		{
   603 			LinkableMapObj *lmo=model->findID (a.value( "beginID"));
   604 			if (lmo && typid (*lmo)==typid (BranchObj))
   605 			{
   606 				xlo->setBegin ((BranchObj*)lmo);
   607 				lmo=model->findID (a.value( "endID"));
   608 				if (lmo && typid (*lmo)==typid (BranchObj))
   609 				{
   610 					xlo->setEnd ((BranchObj*)(lmo));
   611 					xlo->activate();
   612 					success=true;
   613 				}
   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 }