xml-vym.cpp
author insilmaril
Tue, 07 Jul 2009 09:34:24 +0000
changeset 779 1fb50e79661c
parent 777 8acac4fade1b
child 780 fe839bdfd10c
permissions -rw-r--r--
Hiding of links works 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 "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();	//FIXME-3 in endElement unscroll again, if branch is still empty 
   363 		// (interesting for import of KDE bookmarks)
   364 /*
   365 	if (!a.value( "frameType").isEmpty() ) 
   366 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   367 
   368 	if (!a.value( "incImgV").isEmpty() ) 
   369 	{	
   370 		if (a.value("incImgV")=="true")
   371 			lastBranch->setIncludeImagesVer(true);
   372 		else	
   373 			lastBranch->setIncludeImagesVer(false);
   374 	}	
   375 	if (!a.value( "incImgH").isEmpty() ) 
   376 	{	
   377 		if (a.value("incImgH")=="true")
   378 			lastBranch->setIncludeImagesHor(true);
   379 		else	
   380 			lastBranch->setIncludeImagesHor(false);
   381 	}	
   382 */	
   383 	return true;	
   384 }
   385 
   386 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)	// FIXME-4 does not work if there is no lmo for treeitem
   387 {
   388 	if (lastMI)
   389 	{
   390 		OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO()); 
   391 		if (oo)
   392 		{
   393 			bool ok;
   394 			int x;
   395 			{
   396 				if (!a.value( "frameType").isEmpty() ) 
   397 					oo->setFrameType (a.value("frameType"));
   398 				if (!a.value( "penColor").isEmpty() ) 
   399 					oo->setFramePenColor (a.value("penColor"));
   400 				if (!a.value( "brushColor").isEmpty() ) 
   401 					oo->setFrameBrushColor (a.value("brushColor"));
   402 				if (!a.value( "padding").isEmpty() ) 
   403 				{
   404 					x=a.value("padding").toInt(&ok);
   405 					if (ok) oo->setFramePadding(x);
   406 				}	
   407 				if (!a.value( "borderWidth").isEmpty() ) 
   408 				{
   409 					x=a.value("borderWidth").toInt(&ok);
   410 					if (ok) oo->setFrameBorderWidth(x);
   411 				}	
   412 			}		
   413 			return true;
   414 		}
   415 	}
   416 	return false;
   417 }
   418 
   419 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   420 {
   421 	if (lastMI)	//FIXME-1  below lastBranch is used, not lastMI. So it might not work for images
   422 	{
   423 		bool okx,oky;
   424 		float x,y;
   425 		if (!a.value( "relPosX").isEmpty() ) 
   426 		{
   427 			if (!a.value( "relPosY").isEmpty() ) 
   428 			{
   429 				x=a.value("relPosX").toFloat (&okx);
   430 				y=a.value("relPosY").toFloat (&oky);
   431 				if (okx && oky  )
   432 					lastBranch->setRelPos (QPointF(x,y));
   433 				else
   434 					return false;   // Couldn't read relPos
   435 			}           
   436 		}           
   437 		if (!a.value( "absPosX").isEmpty() && loadMode==NewMap ) 
   438 		{
   439 			if (!a.value( "absPosY").isEmpty() ) 
   440 			{
   441 				x=a.value("absPosX").toFloat (&okx);
   442 				y=a.value("absPosY").toFloat (&oky);
   443 				if (okx && oky  )
   444 					lastBranch->setAbsPos (QPointF(x,y));
   445 				else
   446 					return false;   // Couldn't read absPos
   447 			}           
   448 		}           
   449 		//if (!a.value( "id").isEmpty() ) 
   450 		//	lastBranch->setID (a.value ("id"));
   451 			
   452 		if (!a.value( "url").isEmpty() ) 
   453 			lastBranch->setURL (a.value ("url"));
   454 		if (!a.value( "vymLink").isEmpty() ) 
   455 			lastBranch->setVymLink (a.value ("vymLink"));
   456 		if (!a.value( "hideInExport").isEmpty() ) 
   457 			if (a.value("hideInExport")=="true")
   458 				lastBranch->setHideInExport(true);
   459 
   460 		if (!a.value( "hideLink").isEmpty()) 
   461 		{
   462 			if (a.value ("hideLink") =="true")
   463 				lastMI->setHideLinkUnselected(true);
   464 			else	
   465 				lastMI->setHideLinkUnselected(false);
   466 		}	
   467 	}
   468 	return true;	
   469 }
   470 
   471 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   472 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   473 	no.clear();
   474 	QString fn;
   475 	if (!a.value( "href").isEmpty() ) 
   476 	{
   477 		// Load note
   478 		fn=parseHREF(a.value ("href") );
   479 		QFile file (fn);
   480 		QString s;						// Reading a note
   481 
   482 		if ( !file.open( QIODevice::ReadOnly) )
   483 		{
   484 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   485 			return false;
   486 		}	
   487 		QTextStream stream( &file );
   488 		QString lines;
   489 		while ( !stream.atEnd() ) {
   490 			lines += stream.readLine()+"\n"; 
   491 		}
   492 		file.close();
   493 
   494 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   495 		no.setNote (lines);
   496 	}		
   497 	if (!a.value( "fonthint").isEmpty() ) 
   498 		no.setFontHint(a.value ("fonthint") );
   499 	lastBranch->setNoteObj(no);
   500 	return true;
   501 }
   502 
   503 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
   504 {
   505 	lastMI=lastImage;
   506 	
   507 	//if (!readOOAttr(a)) return false;
   508 
   509 	/* FIXME-1 if (!a.value( "useOrientation").isEmpty() ) 
   510 	{
   511 		if (a.value ("useOrientation") =="true")
   512 			lastImage->setUseOrientation (true);
   513 		else	
   514 			lastImage->setUseOrientation (false);
   515 	}	
   516 	*/
   517 	if (!a.value( "href").isEmpty() )
   518 	{
   519 		// Load Image
   520 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   521 		{
   522 			QMessageBox::warning( 0, "Warning: " ,
   523 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   524 			lastImage=NULL;
   525 			return true;
   526 		}
   527 		
   528 	}	
   529 	if (!a.value( "zPlane").isEmpty() ) 
   530 		lastImage->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 				lastImage->setRelPos (QPointF (x,y) );
   542 			else
   543 				// Couldn't read relPos
   544 				return false;  
   545 		}           
   546 	}	
   547 	
   548 	//FIXME-3 if (!readOOAttr(a)) return false;
   549 
   550 	if (!a.value ("originalName").isEmpty() )
   551 	{
   552 		lastImage->setOriginalFilename (a.value("originalName"));
   553 	}
   554 	return true;
   555 }
   556 
   557 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)
   558 {
   559 	QColor col;
   560 	bool okx;
   561 	bool success=false;
   562 	XLinkObj *xlo=new XLinkObj (model->getScene());
   563 	if (!a.value( "color").isEmpty() ) 
   564 	{
   565 		col.setNamedColor(a.value("color"));
   566 		xlo->setColor (col);
   567 	}
   568 
   569 	if (!a.value( "width").isEmpty() ) 
   570 	{
   571 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   572 	}
   573 
   574 	// Connecting by select string for compatibility with version < 1.8.76
   575 	if (!a.value( "beginBranch").isEmpty() ) 
   576 	{ 
   577 		if (!a.value( "endBranch").isEmpty() ) 
   578 		{
   579 			TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
   580 			if (ti && ti->isBranchLikeType())
   581 			{
   582 			/* FIXME-2 xLinks
   583 				xlo->setBegin ((BranchObj*)lmo);
   584 				lmo=model->findBySelectString (a.value( "endBranch"));
   585 				if (lmo && typid (*lmo)==typid (BranchObj))
   586 				{
   587 					xlo->setEnd ((BranchObj*)(lmo));
   588 					xlo->activate();
   589 					success=true;
   590 				}
   591 			*/
   592 			}
   593 		}           
   594 	}	
   595 
   596 	// object ID is used starting in version 1.8.76
   597 	/* FIXME-2 xLinks
   598 	if (!a.value( "beginID").isEmpty() ) 
   599 	{ 
   600 		if (!a.value( "endID").isEmpty() ) 
   601 		{
   602 			LinkableMapObj *lmo=model->findID (a.value( "beginID"));
   603 			if (lmo && typid (*lmo)==typid (BranchObj))
   604 			{
   605 				xlo->setBegin ((BranchObj*)lmo);
   606 				lmo=model->findID (a.value( "endID"));
   607 				if (lmo && typid (*lmo)==typid (BranchObj))
   608 				{
   609 					xlo->setEnd ((BranchObj*)(lmo));
   610 					xlo->activate();
   611 					success=true;
   612 				}
   613 			}
   614 		}           
   615 	}	
   616 	*/
   617 	if (!success) delete (xlo);
   618 	return true;	// xLinks can only be established at the "end branch", return true
   619 }
   620 
   621 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   622 {
   623 	for (int i=1; i<=a.count(); i++)
   624 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   625 	return true;
   626 }
   627 
   628 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   629 {
   630 	if (!a.value( "key").isEmpty() ) 
   631 	{
   632 		if (!a.value( "value").isEmpty() ) 
   633 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   634 		else
   635 			return false;
   636 		
   637 	} else
   638 		return false;
   639 	
   640 	return true;
   641 }