xml-vym.cpp
author insilmaril
Fri, 09 Apr 2010 14:24:04 +0000
changeset 846 f0fe7c36ec5c
parent 841 46553c106c52
child 847 43268373032d
permissions -rw-r--r--
Version 1.13.3 and added treeeditor.png
     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 #include "xlinkitem.h"
    16 
    17 
    18 extern Main *mainWindow;
    19 extern Settings settings;
    20 extern QString vymVersion;
    21 
    22 bool parseVYMHandler::startDocument()
    23 {
    24     errorProt = "";
    25     state = StateInit;
    26     laststate = StateInit;
    27 	stateStack.clear();
    28 	stateStack.append(StateInit);
    29 	htmldata="";
    30 	isVymPart=false;
    31 	useProgress=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=0;			//FIXME-3 what if we load a .vyp ?
    52 		branchesCounter=0;
    53 
    54 		if (loadMode==NewMap )
    55 		{
    56 			// Create mapCenter
    57 			model->clear();
    58 			lastBranch=NULL;
    59 
    60 			if (!atts.value( "author").isEmpty() )
    61 				model->setAuthor(atts.value( "author" ) );
    62 			if (!atts.value( "comment").isEmpty() )
    63 				model->setComment (atts.value( "comment" ) );
    64 			if (!atts.value( "branchCount").isEmpty() )
    65 			{
    66 				branchesTotal=atts.value("branchCount").toInt();
    67 				if (branchesTotal>10)
    68 				{
    69 					useProgress=true;
    70 					mainWindow->setProgressMaximum (branchesTotal);
    71 				}
    72 			} 
    73 				
    74 			if (!atts.value( "backgroundColor").isEmpty() )
    75 			{
    76 				col.setNamedColor(atts.value("backgroundColor"));
    77 				model->getScene()->setBackgroundBrush(col);
    78 			}	    
    79 			if (!atts.value( "selectionColor").isEmpty() )
    80 			{
    81 				col.setNamedColor(atts.value("selectionColor"));
    82 				model->setSelectionColor(col);
    83 			}	    
    84 			if (!atts.value( "linkColorHint").isEmpty() ) 
    85 			{
    86 				if (atts.value("linkColorHint")=="HeadingColor")
    87 					model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
    88 				else
    89 					model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
    90 			}
    91 			if (!atts.value( "linkStyle").isEmpty() ) 
    92 				model->setMapLinkStyle(atts.value("linkStyle"));
    93 			if (!atts.value( "linkColor").isEmpty() ) 
    94 			{
    95 				col.setNamedColor(atts.value("linkColor"));
    96 				model->setMapDefLinkColor(col);
    97 			}	
    98 			if (!atts.value( "defXLinkColor").isEmpty() ) 
    99 			{
   100 				col.setNamedColor(atts.value("defXLinkColor"));
   101 				model->setMapDefXLinkColor(col);
   102 			}	
   103 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   104 				model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   105 			if (!atts.value( "mapZoomFactor").isEmpty() ) 
   106 				model->setMapZoomFactor(atts.value("mapZoomFactor").toDouble());
   107 		}	
   108 		// Check version
   109 		if (!atts.value( "version").isEmpty() ) 
   110 		{
   111 			if (!checkVersion(atts.value("version")))
   112 				QMessageBox::warning( 0, "Warning: Version Problem" ,
   113 				   "<h3>Map is newer than VYM</h3>"
   114 				   "<p>The map you are just trying to load was "
   115 				   "saved using vym " +atts.value("version")+". "
   116 				   "The version of this vym is " + vymVersion + 
   117 				   ". If you run into problems after pressing "
   118 				   "the ok-button below, updating vym should help.");
   119 			else	   
   120 				model->setVersion(atts.value( "version" ));
   121 
   122 		}
   123 
   124 	} else if ( eName == "select" && state == StateMap ) 
   125 	{
   126 		state=StateMapSelect;
   127 	} else if ( eName == "setting" && state == StateMap ) 
   128 	{
   129 		state=StateMapSetting;
   130 		if (loadMode==NewMap)
   131 			readSettingAttr (atts);
   132 	} else if ( eName == "mapcenter" && state == StateMap ) 
   133 	{
   134 		state=StateMapCenter;
   135 		if (loadMode==NewMap)
   136 		{	
   137 			// Really use the found mapcenter as MCO in a new map
   138 			lastBranch=model->createMapCenter(); 
   139 		} else
   140 		{
   141 			// Treat the found mapcenter as a branch 
   142 			// in an existing map
   143 			BranchItem *bi=model->getSelectedBranch();	
   144 			if (bi)
   145 			{
   146 				lastBranch=bi;
   147 				if (loadMode==ImportAdd)
   148 				{
   149 					lastBranch=model->createBranch(lastBranch);
   150 				} //else
   151 					model->clearItem(lastBranch); 
   152 			} else
   153 				// add mapCenter without parent
   154 				lastBranch=model->createMapCenter(); 
   155 		}		
   156 		readBranchAttr (atts);
   157 	} else if ( 
   158 		(eName == "standardflag" ||eName == "standardFlag") && 
   159 		(state == StateMapCenter || state==StateBranch)) 
   160 	{
   161 		state=StateStandardFlag;
   162 	} else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch)) 
   163 	{
   164 		laststate=state;
   165 		state=StateHeading;
   166 		if (!atts.value( "textColor").isEmpty() ) 
   167 		{
   168 			col.setNamedColor(atts.value("textColor"));
   169 			lastBranch->setHeadingColor(col );
   170 		}	    
   171 	} else if ( eName == "note" && 
   172 				(state == StateMapCenter ||state==StateBranch))
   173 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   174 		state=StateNote;
   175 		if (!readNoteAttr (atts) ) return false;
   176 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   177 	{
   178 		laststate=state;
   179 		state=StateHtmlNote;
   180     } else if ( eName == "floatimage" && 
   181 				(state == StateMapCenter ||state==StateBranch)) 
   182 	{
   183 		state=StateImage;
   184 		lastImage=model->createImage(lastBranch);
   185 		if (!readImageAttr(atts)) return false;
   186 	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
   187 	{
   188 		// This is used in vymparts, which have no mapcenter or for undo
   189 		isVymPart=true;
   190 		TreeItem *ti=model->getSelectedItem();
   191 		if (!ti)
   192 		{
   193 			// If a vym part is _loaded_ (not imported), 
   194 			// selection==lmo==NULL
   195 			// Treat it like ImportAdd then...
   196 			loadMode=ImportAdd;
   197 			// FIXME-3 lmo=model->first()->getLMO();		
   198 			// Do we really have no MCO when loading?????
   199 			cout << "xml-vym aborted\n";
   200 			return false;
   201 		}	
   202 		if (ti && ti->isBranchLikeType() )
   203 		{
   204 			lastBranch=(BranchItem*)ti;
   205 			if (eName=="branch")
   206 			{
   207 				state=StateBranch;
   208 				if (loadMode==ImportAdd)
   209 				{
   210 					lastBranch=model->createBranch(lastBranch);
   211 				} else
   212 					model->clearItem (lastBranch);
   213 				readBranchAttr (atts);
   214 			} else if (eName=="floatimage")
   215 			{
   216 				state=StateImage;
   217 				lastImage=model->createImage (lastBranch);
   218 				if (!readImageAttr(atts)) return false;
   219 			} else return false;
   220 		} else return false;
   221 	} else if ( eName == "branch" && state == StateMapCenter) 
   222 	{
   223 		state=StateBranch;
   224 		lastBranch=model->createBranch(lastBranch);
   225 		readBranchAttr (atts);
   226 	} else if ( eName == "htmlnote" && state == StateBranch) 
   227 	{
   228 		laststate=state;
   229 		state=StateHtmlNote;
   230 		no.clear();
   231 		if (!atts.value( "fonthint").isEmpty() ) 
   232 			no.setFontHint(atts.value ("fonthint") );
   233 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   234 	{
   235 		laststate=state;
   236 		state=StateFrame;
   237 		if (!readFrameAttr(atts)) return false;
   238     } else if ( eName == "xlink" && state == StateBranch ) 
   239 	{
   240 		state=StateBranchXLink;
   241 		if (!readXLinkAttr (atts)) return false;
   242     } else if ( eName == "branch" && state == StateBranch ) 
   243 	{
   244 		lastBranch=model->createBranch(lastBranch);
   245 		readBranchAttr (atts);
   246     } else if ( eName == "html" && state == StateHtmlNote ) 
   247 	{
   248 		state=StateHtml;
   249 		htmldata="<"+eName;
   250 		readHtmlAttr(atts);
   251 		htmldata+=">";
   252     } else if ( state == StateHtml ) 
   253 	{
   254 		// accept all while in html mode,
   255 		htmldata+="<"+eName;
   256 		readHtmlAttr(atts);
   257 		htmldata+=">";
   258     } else
   259         return false;   // Error
   260     return true;
   261 }
   262 
   263 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   264 {
   265 	/* Testing
   266 	cout << "endElement </" <<qPrintable(eName)
   267 		<<">  state=" <<state 
   268 	//	<<"  laststate=" <<laststate
   269 	//	<<"  stateStack="<<stateStack.last() 
   270 	//	<<"  selString="<<model->getSelectString().toStdString()
   271 		<<endl;
   272 	*/
   273     switch ( state ) 
   274 	{
   275 		case StateMap:
   276 			mainWindow->removeProgressCounter(); 
   277 			break;
   278         case StateMapCenter: 
   279 			model->emitDataHasChanged (lastBranch);
   280 			lastBranch=(BranchItem*)(lastBranch->parent());
   281 		//	lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   282             break;
   283         case StateBranch: 
   284 			// Empty branches may not be scrolled 
   285 			// (happens if bookmarks are imported)
   286 			if (lastBranch->isScrolled() && lastBranch->branchCount()==0) 
   287 				lastBranch->unScroll();
   288 			model->emitDataHasChanged (lastBranch);
   289 
   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)	
   354 {
   355 	branchesCounter++;
   356 	if (useProgress) 
   357 		mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);	
   358 
   359 	lastMI=lastBranch;
   360 
   361 	if (!readOOAttr(a)) return false;
   362 
   363 	if (!a.value( "scrolled").isEmpty() )
   364 		lastBranch->toggleScroll();	
   365 		// (interesting for import of KDE bookmarks)
   366 
   367 /*	if (!a.value( "frameType").isEmpty() )  FIXME-3
   368 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   369 
   370 */	
   371 	if (!a.value( "incImgV").isEmpty() ) 
   372 	{	
   373 		if (a.value("incImgV")=="true")
   374 			lastBranch->setIncludeImagesVer(true);
   375 		else	
   376 			lastBranch->setIncludeImagesVer(false);
   377 	}	
   378 	if (!a.value( "incImgH").isEmpty() ) 
   379 	{	
   380 		if (a.value("incImgH")=="true")
   381 			lastBranch->setIncludeImagesHor(true);
   382 		else	
   383 			lastBranch->setIncludeImagesHor(false);
   384 	}	
   385 	return true;	
   386 }
   387 
   388 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)	// FIXME-4 does not work if there is no lmo for treeitem
   389 {
   390 	if (lastMI)
   391 	{
   392 		OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO()); 
   393 		if (oo)
   394 		{
   395 			bool ok;
   396 			int x;
   397 			{
   398 				if (!a.value( "frameType").isEmpty() ) 
   399 					oo->setFrameType (a.value("frameType"));
   400 				if (!a.value( "penColor").isEmpty() ) 
   401 					oo->setFramePenColor (a.value("penColor"));
   402 				if (!a.value( "brushColor").isEmpty() ) 
   403 					oo->setFrameBrushColor (a.value("brushColor"));
   404 				if (!a.value( "padding").isEmpty() ) 
   405 				{
   406 					x=a.value("padding").toInt(&ok);
   407 					if (ok) oo->setFramePadding(x);
   408 				}	
   409 				if (!a.value( "borderWidth").isEmpty() ) 
   410 				{
   411 					x=a.value("borderWidth").toInt(&ok);
   412 					if (ok) oo->setFrameBorderWidth(x);
   413 				}	
   414 			}		
   415 			return true;
   416 		}
   417 	}
   418 	return false;
   419 }
   420 
   421 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   422 {
   423 	if (lastMI)
   424 	{
   425 		bool okx,oky;
   426 		float x,y;
   427 		if (!a.value( "relPosX").isEmpty() ) 
   428 		{
   429 			if (!a.value( "relPosY").isEmpty() ) 
   430 			{
   431 				x=a.value("relPosX").toFloat (&okx);
   432 				y=a.value("relPosY").toFloat (&oky);
   433 				if (okx && oky  )
   434 					lastMI->setRelPos (QPointF(x,y));
   435 				else
   436 					return false;   // Couldn't read relPos
   437 			}           
   438 		}           
   439 		if (!a.value( "absPosX").isEmpty() ) 
   440 		{
   441 			if (!a.value( "absPosY").isEmpty() ) 
   442 			{
   443 				x=a.value("absPosX").toFloat (&okx);
   444 				y=a.value("absPosY").toFloat (&oky);
   445 				if (okx && oky  )
   446 					lastMI->setAbsPos (QPointF(x,y));
   447 				else
   448 					return false;   // Couldn't read absPos
   449 			}           
   450 		}           
   451 		if (!a.value( "url").isEmpty() ) 
   452 			lastMI->setURL (a.value ("url"));
   453 		if (!a.value( "vymLink").isEmpty() ) 
   454 			lastMI->setVymLink (a.value ("vymLink"));
   455 		if (!a.value( "hideInExport").isEmpty() ) 
   456 			if (a.value("hideInExport")=="true")
   457 				lastMI->setHideInExport(true);
   458 
   459 		if (!a.value( "hideLink").isEmpty()) 
   460 		{
   461 			if (a.value ("hideLink") =="true")
   462 				lastMI->setHideLinkUnselected(true);
   463 			else	
   464 				lastMI->setHideLinkUnselected(false);
   465 		}	
   466 	}
   467 	return true;	
   468 }
   469 
   470 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   471 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   472 	no.clear();
   473 	QString fn;
   474 	if (!a.value( "href").isEmpty() ) 
   475 	{
   476 		// Load note
   477 		fn=parseHREF(a.value ("href") );
   478 		QFile file (fn);
   479 		QString s;						// Reading a note
   480 
   481 		if ( !file.open( QIODevice::ReadOnly) )
   482 		{
   483 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   484 			return false;
   485 		}	
   486 		QTextStream stream( &file );
   487 		QString lines;
   488 		while ( !stream.atEnd() ) {
   489 			lines += stream.readLine()+"\n"; 
   490 		}
   491 		file.close();
   492 
   493 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   494 		no.setNote (lines);
   495 	}		
   496 	if (!a.value( "fonthint").isEmpty() ) 
   497 		no.setFontHint(a.value ("fonthint") );
   498 	lastBranch->setNoteObj(no);
   499 	return true;
   500 }
   501 
   502 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
   503 {
   504 	lastMI=lastImage;
   505 	
   506 	if (!readOOAttr(a)) return false;  
   507 
   508 	if (!a.value( "href").isEmpty() )
   509 	{
   510 		// Load Image
   511 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   512 		{
   513 			QMessageBox::warning( 0, "Warning: " ,
   514 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   515 			lastImage=NULL;
   516 			return true;
   517 		}
   518 		
   519 	}	
   520 	if (!a.value( "zPlane").isEmpty() ) 
   521 		lastImage->setZValue (a.value("zPlane").toInt ());
   522     float x,y;
   523     bool okx,oky;
   524 	if (!a.value( "relPosX").isEmpty() ) 
   525 	{
   526 		if (!a.value( "relPosY").isEmpty() ) 
   527 		{
   528 			// read relPos
   529 			x=a.value("relPosX").toFloat (&okx);
   530 			y=a.value("relPosY").toFloat (&oky);
   531 			if (okx && oky) 
   532 				lastImage->setRelPos (QPointF (x,y) );
   533 			else
   534 				// Couldn't read relPos
   535 				return false;  
   536 		}           
   537 	}	
   538 	
   539 	//FIXME-3 if (!readOOAttr(a)) return false;
   540 
   541 	if (!a.value ("originalName").isEmpty() )
   542 	{
   543 		lastImage->setOriginalFilename (a.value("originalName"));
   544 	}
   545 	return true;
   546 }
   547 
   548 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)	
   549 {
   550 	// object ID is used starting in version 1.8.76
   551 	// (before there was beginBranch and endBranch)
   552 	if (!a.value( "beginID").isEmpty() ) 
   553 	{ 
   554 		if (!a.value( "endID").isEmpty() ) 
   555 		{
   556 			TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
   557 			TreeItem   *endBI=model->findBySelectString (a.value( "endID"));
   558 			if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
   559 			{
   560 				XLinkItem *xli=model->createXLink (lastBranch,true);
   561 				xli->setBegin ( (BranchItem*)beginBI );
   562 				xli->setEnd ( (BranchItem*)endBI);
   563 				xli->activate();
   564 
   565 				if (!a.value( "color").isEmpty() ) 
   566 				{
   567 					QColor col;
   568 					col.setNamedColor(a.value("color"));
   569 					xli->setColor (col);
   570 				}
   571 
   572 				if (!a.value( "width").isEmpty() ) 
   573 				{
   574 					bool okx;
   575 					xli->setWidth(a.value ("width").toInt (&okx, 10));
   576 				}
   577 				xli->updateXLink();
   578 			}
   579 		}           
   580 	}	
   581 	return true;	// xLinks can only be established at the "end branch", return true
   582 }
   583 
   584 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   585 {
   586 	for (int i=1; i<=a.count(); i++)
   587 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   588 	return true;
   589 }
   590 
   591 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   592 {
   593 	if (!a.value( "key").isEmpty() ) 
   594 	{
   595 		if (!a.value( "value").isEmpty() ) 
   596 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   597 		else
   598 			return false;
   599 		
   600 	} else
   601 		return false;
   602 	
   603 	return true;
   604 }