xml-vym.cpp
author insilmaril
Thu, 18 Mar 2010 11:46:52 +0000
changeset 839 fbb927bbdda3
parent 837 5ecd0462f76b
child 841 46553c106c52
permissions -rw-r--r--
Version bump to 1.13.1 due to first release of 1.13.0 to openSUSE buildservice
     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 			cout << "xml-vym  bi="<<bi->getHeadingStd()<<"  loadMode="<<loadMode<<endl;
   145 			if (bi)
   146 			{
   147 				lastBranch=bi;
   148 				if (loadMode==ImportAdd)
   149 				{
   150 					lastBranch=model->createBranch(lastBranch);
   151 				} //else
   152 					model->clearItem(lastBranch); 
   153 			} else
   154 				// add mapCenter without parent
   155 				lastBranch=model->createMapCenter(); 
   156 		}		
   157 		readBranchAttr (atts);
   158 	} else if ( 
   159 		(eName == "standardflag" ||eName == "standardFlag") && 
   160 		(state == StateMapCenter || state==StateBranch)) 
   161 	{
   162 		state=StateStandardFlag;
   163 	} else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch)) 
   164 	{
   165 		laststate=state;
   166 		state=StateHeading;
   167 		if (!atts.value( "textColor").isEmpty() ) 
   168 		{
   169 			col.setNamedColor(atts.value("textColor"));
   170 			lastBranch->setHeadingColor(col );
   171 		}	    
   172 	} else if ( eName == "note" && 
   173 				(state == StateMapCenter ||state==StateBranch))
   174 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   175 		state=StateNote;
   176 		if (!readNoteAttr (atts) ) return false;
   177 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   178 	{
   179 		laststate=state;
   180 		state=StateHtmlNote;
   181     } else if ( eName == "floatimage" && 
   182 				(state == StateMapCenter ||state==StateBranch)) 
   183 	{
   184 		state=StateImage;
   185 		lastImage=model->createImage(lastBranch);
   186 		if (!readImageAttr(atts)) return false;
   187 	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
   188 	{
   189 		// This is used in vymparts, which have no mapcenter or for undo
   190 		isVymPart=true;
   191 		TreeItem *ti=model->getSelectedItem();
   192 		if (!ti)
   193 		{
   194 			// If a vym part is _loaded_ (not imported), 
   195 			// selection==lmo==NULL
   196 			// Treat it like ImportAdd then...
   197 			loadMode=ImportAdd;
   198 			// FIXME-3 lmo=model->first()->getLMO();		
   199 			// Do we really have no MCO when loading?????
   200 			cout << "xml-vym aborted\n";
   201 			return false;
   202 		}	
   203 		if (ti && ti->isBranchLikeType() )
   204 		{
   205 			lastBranch=(BranchItem*)ti;
   206 			if (eName=="branch")
   207 			{
   208 				state=StateBranch;
   209 				if (loadMode==ImportAdd)
   210 				{
   211 					lastBranch=model->createBranch(lastBranch);
   212 				} else
   213 					model->clearItem (lastBranch);
   214 				readBranchAttr (atts);
   215 			} else if (eName=="floatimage")
   216 			{
   217 				state=StateImage;
   218 				lastImage=model->createImage (lastBranch);
   219 				if (!readImageAttr(atts)) return false;
   220 			} else return false;
   221 		} else return false;
   222 	} else if ( eName == "branch" && state == StateMapCenter) 
   223 	{
   224 		state=StateBranch;
   225 		lastBranch=model->createBranch(lastBranch);
   226 		readBranchAttr (atts);
   227 	} else if ( eName == "htmlnote" && state == StateBranch) 
   228 	{
   229 		laststate=state;
   230 		state=StateHtmlNote;
   231 		no.clear();
   232 		if (!atts.value( "fonthint").isEmpty() ) 
   233 			no.setFontHint(atts.value ("fonthint") );
   234 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   235 	{
   236 		laststate=state;
   237 		state=StateFrame;
   238 		if (!readFrameAttr(atts)) return false;
   239     } else if ( eName == "xlink" && state == StateBranch ) 
   240 	{
   241 		state=StateBranchXLink;
   242 		if (!readXLinkAttr (atts)) return false;
   243     } else if ( eName == "branch" && state == StateBranch ) 
   244 	{
   245 		lastBranch=model->createBranch(lastBranch);
   246 		readBranchAttr (atts);
   247     } else if ( eName == "html" && state == StateHtmlNote ) 
   248 	{
   249 		state=StateHtml;
   250 		htmldata="<"+eName;
   251 		readHtmlAttr(atts);
   252 		htmldata+=">";
   253     } else if ( state == StateHtml ) 
   254 	{
   255 		// accept all while in html mode,
   256 		htmldata+="<"+eName;
   257 		readHtmlAttr(atts);
   258 		htmldata+=">";
   259     } else
   260         return false;   // Error
   261     return true;
   262 }
   263 
   264 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   265 {
   266 	/* Testing
   267 	cout << "endElement </" <<qPrintable(eName)
   268 		<<">  state=" <<state 
   269 	//	<<"  laststate=" <<laststate
   270 	//	<<"  stateStack="<<stateStack.last() 
   271 	//	<<"  selString="<<model->getSelectString().toStdString()
   272 		<<endl;
   273 	*/
   274     switch ( state ) 
   275 	{
   276 		case StateMap:
   277 			mainWindow->removeProgressCounter(); 
   278 			break;
   279         case StateMapCenter: 
   280 			model->emitDataHasChanged (lastBranch);
   281 			lastBranch=(BranchItem*)(lastBranch->parent());
   282 		//	lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   283             break;
   284         case StateBranch: 
   285 			// Empty branches may not be scrolled 
   286 			// (happens if bookmarks are imported)
   287 			if (lastBranch->isScrolled() && lastBranch->branchCount()==0) 
   288 				lastBranch->unScroll();
   289 			model->emitDataHasChanged (lastBranch);
   290 
   291 			lastBranch=(BranchItem*)(lastBranch->parent());
   292 			lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   293             break;
   294         case StateHtml: 
   295 			htmldata+="</"+eName+">";
   296 			if (eName=="html")
   297 			{
   298 				state=StateHtmlNote;  
   299 				htmldata.replace ("<br></br>","<br />");
   300 				no.setNote (htmldata);
   301 				lastBranch->setNoteObj (no);
   302 			}	
   303 			break;
   304 		default: 
   305 			break;
   306     }  
   307 	state=stateStack.takeLast();	
   308 	return true;
   309 }
   310 
   311 bool parseVYMHandler::characters   ( const QString& ch)
   312 {
   313 	//cout << "characters \""<<ch.toStdString()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   314 
   315 	QString ch_org=quotemeta (ch);
   316     QString ch_simplified=ch.simplifyWhiteSpace();
   317     if ( ch_simplified.isEmpty() ) return true;
   318 
   319     switch ( state ) 
   320     {
   321         case StateInit: break;
   322         case StateMap: break; 
   323 		case StateMapSelect:
   324 			model->select(ch_simplified);
   325 			break;
   326 		case StateMapSetting:break;
   327         case StateMapCenter: break;
   328         case StateNote:
   329 			lastBranch->setNote(ch_simplified);
   330 			break;
   331         case StateBranch: break;
   332         case StateStandardFlag: 
   333             lastBranch->activateStandardFlag(ch_simplified); 
   334             break;
   335         case StateImage: break;
   336         case StateHtmlNote: break;
   337         case StateHtml:
   338 			htmldata+=ch_org;
   339 			break;
   340         case StateHeading: 
   341             lastBranch->setHeading(ch_simplified);
   342             break;
   343         default: 
   344 			return false;
   345     }
   346     return true;
   347 }
   348 
   349 QString parseVYMHandler::errorString() 
   350 {
   351     return "the document is not in the VYM file format";
   352 }
   353 
   354 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)	
   355 {
   356 	branchesCounter++;
   357 	if (useProgress) 
   358 		mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);	
   359 
   360 	lastMI=lastBranch;
   361 
   362 	if (!readOOAttr(a)) return false;
   363 
   364 	if (!a.value( "scrolled").isEmpty() )
   365 		lastBranch->toggleScroll();	
   366 		// (interesting for import of KDE bookmarks)
   367 
   368 /*	if (!a.value( "frameType").isEmpty() )  FIXME-3
   369 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   370 
   371 */	
   372 	if (!a.value( "incImgV").isEmpty() ) 
   373 	{	
   374 		if (a.value("incImgV")=="true")
   375 			lastBranch->setIncludeImagesVer(true);
   376 		else	
   377 			lastBranch->setIncludeImagesVer(false);
   378 	}	
   379 	if (!a.value( "incImgH").isEmpty() ) 
   380 	{	
   381 		if (a.value("incImgH")=="true")
   382 			lastBranch->setIncludeImagesHor(true);
   383 		else	
   384 			lastBranch->setIncludeImagesHor(false);
   385 	}	
   386 	return true;	
   387 }
   388 
   389 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)	// FIXME-4 does not work if there is no lmo for treeitem
   390 {
   391 	if (lastMI)
   392 	{
   393 		OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO()); 
   394 		if (oo)
   395 		{
   396 			bool ok;
   397 			int x;
   398 			{
   399 				if (!a.value( "frameType").isEmpty() ) 
   400 					oo->setFrameType (a.value("frameType"));
   401 				if (!a.value( "penColor").isEmpty() ) 
   402 					oo->setFramePenColor (a.value("penColor"));
   403 				if (!a.value( "brushColor").isEmpty() ) 
   404 					oo->setFrameBrushColor (a.value("brushColor"));
   405 				if (!a.value( "padding").isEmpty() ) 
   406 				{
   407 					x=a.value("padding").toInt(&ok);
   408 					if (ok) oo->setFramePadding(x);
   409 				}	
   410 				if (!a.value( "borderWidth").isEmpty() ) 
   411 				{
   412 					x=a.value("borderWidth").toInt(&ok);
   413 					if (ok) oo->setFrameBorderWidth(x);
   414 				}	
   415 			}		
   416 			return true;
   417 		}
   418 	}
   419 	return false;
   420 }
   421 
   422 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   423 {
   424 	if (lastMI)
   425 	{
   426 		bool okx,oky;
   427 		float x,y;
   428 		if (!a.value( "relPosX").isEmpty() ) 
   429 		{
   430 			if (!a.value( "relPosY").isEmpty() ) 
   431 			{
   432 				x=a.value("relPosX").toFloat (&okx);
   433 				y=a.value("relPosY").toFloat (&oky);
   434 				if (okx && oky  )
   435 					lastMI->setRelPos (QPointF(x,y));
   436 				else
   437 					return false;   // Couldn't read relPos
   438 			}           
   439 		}           
   440 		if (!a.value( "absPosX").isEmpty() ) 
   441 		{
   442 			if (!a.value( "absPosY").isEmpty() ) 
   443 			{
   444 				x=a.value("absPosX").toFloat (&okx);
   445 				y=a.value("absPosY").toFloat (&oky);
   446 				if (okx && oky  )
   447 					lastMI->setAbsPos (QPointF(x,y));
   448 				else
   449 					return false;   // Couldn't read absPos
   450 			}           
   451 		}           
   452 		if (!a.value( "url").isEmpty() ) 
   453 			lastMI->setURL (a.value ("url"));
   454 		if (!a.value( "vymLink").isEmpty() ) 
   455 			lastMI->setVymLink (a.value ("vymLink"));
   456 		if (!a.value( "hideInExport").isEmpty() ) 
   457 			if (a.value("hideInExport")=="true")
   458 				lastMI->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 	if (!a.value( "href").isEmpty() )
   510 	{
   511 		// Load Image
   512 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   513 		{
   514 			QMessageBox::warning( 0, "Warning: " ,
   515 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   516 			lastImage=NULL;
   517 			return true;
   518 		}
   519 		
   520 	}	
   521 	if (!a.value( "zPlane").isEmpty() ) 
   522 		lastImage->setZValue (a.value("zPlane").toInt ());
   523     float x,y;
   524     bool okx,oky;
   525 	if (!a.value( "relPosX").isEmpty() ) 
   526 	{
   527 		if (!a.value( "relPosY").isEmpty() ) 
   528 		{
   529 			// read relPos
   530 			x=a.value("relPosX").toFloat (&okx);
   531 			y=a.value("relPosY").toFloat (&oky);
   532 			if (okx && oky) 
   533 				lastImage->setRelPos (QPointF (x,y) );
   534 			else
   535 				// Couldn't read relPos
   536 				return false;  
   537 		}           
   538 	}	
   539 	
   540 	//FIXME-3 if (!readOOAttr(a)) return false;
   541 
   542 	if (!a.value ("originalName").isEmpty() )
   543 	{
   544 		lastImage->setOriginalFilename (a.value("originalName"));
   545 	}
   546 	return true;
   547 }
   548 
   549 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)	
   550 {
   551 	// object ID is used starting in version 1.8.76
   552 	// (before there was beginBranch and endBranch)
   553 	if (!a.value( "beginID").isEmpty() ) 
   554 	{ 
   555 		if (!a.value( "endID").isEmpty() ) 
   556 		{
   557 			TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
   558 			TreeItem   *endBI=model->findBySelectString (a.value( "endID"));
   559 			if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
   560 			{
   561 				XLinkItem *xli=model->createXLink (lastBranch,true);
   562 				xli->setBegin ( (BranchItem*)beginBI );
   563 				xli->setEnd ( (BranchItem*)endBI);
   564 				xli->activate();
   565 
   566 				if (!a.value( "color").isEmpty() ) 
   567 				{
   568 					QColor col;
   569 					col.setNamedColor(a.value("color"));
   570 					xli->setColor (col);
   571 				}
   572 
   573 				if (!a.value( "width").isEmpty() ) 
   574 				{
   575 					bool okx;
   576 					xli->setWidth(a.value ("width").toInt (&okx, 10));
   577 				}
   578 				xli->updateXLink();
   579 			}
   580 		}           
   581 	}	
   582 	return true;	// xLinks can only be established at the "end branch", return true
   583 }
   584 
   585 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   586 {
   587 	for (int i=1; i<=a.count(); i++)
   588 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   589 	return true;
   590 }
   591 
   592 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   593 {
   594 	if (!a.value( "key").isEmpty() ) 
   595 	{
   596 		if (!a.value( "value").isEmpty() ) 
   597 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   598 		else
   599 			return false;
   600 		
   601 	} else
   602 		return false;
   603 	
   604 	return true;
   605 }