xml-vym.cpp
author insilmaril
Tue, 02 Mar 2010 13:59:19 +0000
changeset 825 1ad892c1a709
parent 823 0bba81dde1bc
child 837 5ecd0462f76b
permissions -rw-r--r--
Added file for later dockwidget showing all search results at once
     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 		}	
   106 		// Check version
   107 		if (!atts.value( "version").isEmpty() ) 
   108 		{
   109 			if (!checkVersion(atts.value("version")))
   110 				QMessageBox::warning( 0, "Warning: Version Problem" ,
   111 				   "<h3>Map is newer than VYM</h3>"
   112 				   "<p>The map you are just trying to load was "
   113 				   "saved using vym " +atts.value("version")+". "
   114 				   "The version of this vym is " + vymVersion + 
   115 				   ". If you run into problems after pressing "
   116 				   "the ok-button below, updating vym should help.");
   117 			else	   
   118 				model->setVersion(atts.value( "version" ));
   119 
   120 		}
   121 
   122 	} else if ( eName == "select" && state == StateMap ) 
   123 	{
   124 		state=StateMapSelect;
   125 	} else if ( eName == "setting" && state == StateMap ) 
   126 	{
   127 		state=StateMapSetting;
   128 		if (loadMode==NewMap)
   129 			readSettingAttr (atts);
   130 	} else if ( eName == "mapcenter" && state == StateMap ) 
   131 	{
   132 		state=StateMapCenter;
   133 		if (loadMode==NewMap)
   134 		{	
   135 			// Really use the found mapcenter as MCO in a new map
   136 			lastBranch=model->createMapCenter(); 
   137 		} else
   138 		{
   139 			// Treat the found mapcenter as a branch 
   140 			// in an existing map
   141 			BranchItem *bi=model->getSelectedBranch();	
   142 			cout << "xml-vym  bi="<<bi->getHeadingStd()<<"  loadMode="<<loadMode<<endl;
   143 			if (bi)
   144 			{
   145 				lastBranch=bi;
   146 				if (loadMode==ImportAdd)
   147 				{
   148 					lastBranch=model->createBranch(lastBranch);
   149 				} //else
   150 					model->clearItem(lastBranch); 
   151 			} else
   152 				// add mapCenter without parent
   153 				lastBranch=model->createMapCenter(); 
   154 		}		
   155 		readBranchAttr (atts);
   156 	} else if ( 
   157 		(eName == "standardflag" ||eName == "standardFlag") && 
   158 		(state == StateMapCenter || state==StateBranch)) 
   159 	{
   160 		state=StateStandardFlag;
   161 	} else if ( eName == "heading" && (state == StateMapCenter||state==StateBranch)) 
   162 	{
   163 		laststate=state;
   164 		state=StateHeading;
   165 		if (!atts.value( "textColor").isEmpty() ) 
   166 		{
   167 			col.setNamedColor(atts.value("textColor"));
   168 			lastBranch->setHeadingColor(col );
   169 		}	    
   170 	} else if ( eName == "note" && 
   171 				(state == StateMapCenter ||state==StateBranch))
   172 	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
   173 		state=StateNote;
   174 		if (!readNoteAttr (atts) ) return false;
   175 	} else if ( eName == "htmlnote" && state == StateMapCenter) 
   176 	{
   177 		laststate=state;
   178 		state=StateHtmlNote;
   179     } else if ( eName == "floatimage" && 
   180 				(state == StateMapCenter ||state==StateBranch)) 
   181 	{
   182 		state=StateImage;
   183 		lastImage=model->createImage(lastBranch);
   184 		if (!readImageAttr(atts)) return false;
   185 	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
   186 	{
   187 		// This is used in vymparts, which have no mapcenter or for undo
   188 		isVymPart=true;
   189 		TreeItem *ti=model->getSelectedItem();
   190 		if (!ti)
   191 		{
   192 			// If a vym part is _loaded_ (not imported), 
   193 			// selection==lmo==NULL
   194 			// Treat it like ImportAdd then...
   195 			loadMode=ImportAdd;
   196 			// FIXME-3 lmo=model->first()->getLMO();		
   197 			// Do we really have no MCO when loading?????
   198 			cout << "xml-vym aborted\n";
   199 			return false;
   200 		}	
   201 		if (ti && ti->isBranchLikeType() )
   202 		{
   203 			lastBranch=(BranchItem*)ti;
   204 			if (eName=="branch")
   205 			{
   206 				state=StateBranch;
   207 				if (loadMode==ImportAdd)
   208 				{
   209 					lastBranch=model->createBranch(lastBranch);
   210 				} else
   211 					model->clearItem (lastBranch);
   212 				readBranchAttr (atts);
   213 			} else if (eName=="floatimage")
   214 			{
   215 				state=StateImage;
   216 				lastImage=model->createImage (lastBranch);
   217 				if (!readImageAttr(atts)) return false;
   218 			} else return false;
   219 		} else return false;
   220 	} else if ( eName == "branch" && state == StateMapCenter) 
   221 	{
   222 		state=StateBranch;
   223 		lastBranch=model->createBranch(lastBranch);
   224 		readBranchAttr (atts);
   225 	} else if ( eName == "htmlnote" && state == StateBranch) 
   226 	{
   227 		laststate=state;
   228 		state=StateHtmlNote;
   229 		no.clear();
   230 		if (!atts.value( "fonthint").isEmpty() ) 
   231 			no.setFontHint(atts.value ("fonthint") );
   232 	} else if ( eName == "frame" && (state == StateBranch||state==StateMapCenter)) 
   233 	{
   234 		laststate=state;
   235 		state=StateFrame;
   236 		if (!readFrameAttr(atts)) return false;
   237     } else if ( eName == "xlink" && state == StateBranch ) 
   238 	{
   239 		state=StateBranchXLink;
   240 		if (!readXLinkAttr (atts)) return false;
   241     } else if ( eName == "branch" && state == StateBranch ) 
   242 	{
   243 		lastBranch=model->createBranch(lastBranch);
   244 		readBranchAttr (atts);
   245     } else if ( eName == "html" && state == StateHtmlNote ) 
   246 	{
   247 		state=StateHtml;
   248 		htmldata="<"+eName;
   249 		readHtmlAttr(atts);
   250 		htmldata+=">";
   251     } else if ( state == StateHtml ) 
   252 	{
   253 		// accept all while in html mode,
   254 		htmldata+="<"+eName;
   255 		readHtmlAttr(atts);
   256 		htmldata+=">";
   257     } else
   258         return false;   // Error
   259     return true;
   260 }
   261 
   262 bool parseVYMHandler::endElement  ( const QString&, const QString&, const QString &eName)
   263 {
   264 	/* Testing
   265 	cout << "endElement </" <<qPrintable(eName)
   266 		<<">  state=" <<state 
   267 	//	<<"  laststate=" <<laststate
   268 	//	<<"  stateStack="<<stateStack.last() 
   269 	//	<<"  selString="<<model->getSelectString().toStdString()
   270 		<<endl;
   271 	*/
   272     switch ( state ) 
   273 	{
   274 		case StateMap:
   275 			mainWindow->removeProgressCounter(); 
   276 			break;
   277         case StateMapCenter: 
   278 			model->emitDataHasChanged (lastBranch);
   279 			lastBranch=(BranchItem*)(lastBranch->parent());
   280 		//	lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   281             break;
   282         case StateBranch: 
   283 			// Empty branches may not be scrolled 
   284 			// (happens if bookmarks are imported)
   285 			if (lastBranch->isScrolled() && lastBranch->branchCount()==0) 
   286 				lastBranch->unScroll();
   287 			model->emitDataHasChanged (lastBranch);
   288 
   289 			lastBranch=(BranchItem*)(lastBranch->parent());
   290 			lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   291             break;
   292         case StateHtml: 
   293 			htmldata+="</"+eName+">";
   294 			if (eName=="html")
   295 			{
   296 				state=StateHtmlNote;  
   297 				htmldata.replace ("<br></br>","<br />");
   298 				no.setNote (htmldata);
   299 				lastBranch->setNoteObj (no);
   300 			}	
   301 			break;
   302 		default: 
   303 			break;
   304     }  
   305 	state=stateStack.takeLast();	
   306 	return true;
   307 }
   308 
   309 bool parseVYMHandler::characters   ( const QString& ch)
   310 {
   311 	//cout << "characters \""<<ch.toStdString()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   312 
   313 	QString ch_org=quotemeta (ch);
   314     QString ch_simplified=ch.simplifyWhiteSpace();
   315     if ( ch_simplified.isEmpty() ) return true;
   316 
   317     switch ( state ) 
   318     {
   319         case StateInit: break;
   320         case StateMap: break; 
   321 		case StateMapSelect:
   322 			model->select(ch_simplified);
   323 			break;
   324 		case StateMapSetting:break;
   325         case StateMapCenter: break;
   326         case StateNote:
   327 			lastBranch->setNote(ch_simplified);
   328 			break;
   329         case StateBranch: break;
   330         case StateStandardFlag: 
   331             lastBranch->activateStandardFlag(ch_simplified); 
   332             break;
   333         case StateImage: break;
   334         case StateHtmlNote: break;
   335         case StateHtml:
   336 			htmldata+=ch_org;
   337 			break;
   338         case StateHeading: 
   339             lastBranch->setHeading(ch_simplified);
   340             break;
   341         default: 
   342 			return false;
   343     }
   344     return true;
   345 }
   346 
   347 QString parseVYMHandler::errorString() 
   348 {
   349     return "the document is not in the VYM file format";
   350 }
   351 
   352 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)	
   353 {
   354 	branchesCounter++;
   355 	if (useProgress) 
   356 		mainWindow->addProgressValue ((float)branchesCounter/branchesTotal);	
   357 
   358 	lastMI=lastBranch;
   359 
   360 	if (!readOOAttr(a)) return false;
   361 
   362 	if (!a.value( "scrolled").isEmpty() )
   363 		lastBranch->toggleScroll();	
   364 		// (interesting for import of KDE bookmarks)
   365 
   366 /*	if (!a.value( "frameType").isEmpty() )  FIXME-3
   367 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   368 
   369 */	
   370 	if (!a.value( "incImgV").isEmpty() ) 
   371 	{	
   372 		if (a.value("incImgV")=="true")
   373 			lastBranch->setIncludeImagesVer(true);
   374 		else	
   375 			lastBranch->setIncludeImagesVer(false);
   376 	}	
   377 	if (!a.value( "incImgH").isEmpty() ) 
   378 	{	
   379 		if (a.value("incImgH")=="true")
   380 			lastBranch->setIncludeImagesHor(true);
   381 		else	
   382 			lastBranch->setIncludeImagesHor(false);
   383 	}	
   384 	return true;	
   385 }
   386 
   387 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)	// FIXME-4 does not work if there is no lmo for treeitem
   388 {
   389 	if (lastMI)
   390 	{
   391 		OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO()); 
   392 		if (oo)
   393 		{
   394 			bool ok;
   395 			int x;
   396 			{
   397 				if (!a.value( "frameType").isEmpty() ) 
   398 					oo->setFrameType (a.value("frameType"));
   399 				if (!a.value( "penColor").isEmpty() ) 
   400 					oo->setFramePenColor (a.value("penColor"));
   401 				if (!a.value( "brushColor").isEmpty() ) 
   402 					oo->setFrameBrushColor (a.value("brushColor"));
   403 				if (!a.value( "padding").isEmpty() ) 
   404 				{
   405 					x=a.value("padding").toInt(&ok);
   406 					if (ok) oo->setFramePadding(x);
   407 				}	
   408 				if (!a.value( "borderWidth").isEmpty() ) 
   409 				{
   410 					x=a.value("borderWidth").toInt(&ok);
   411 					if (ok) oo->setFrameBorderWidth(x);
   412 				}	
   413 			}		
   414 			return true;
   415 		}
   416 	}
   417 	return false;
   418 }
   419 
   420 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   421 {
   422 	if (lastMI)
   423 	{
   424 		bool okx,oky;
   425 		float x,y;
   426 		if (!a.value( "relPosX").isEmpty() ) 
   427 		{
   428 			if (!a.value( "relPosY").isEmpty() ) 
   429 			{
   430 				x=a.value("relPosX").toFloat (&okx);
   431 				y=a.value("relPosY").toFloat (&oky);
   432 				if (okx && oky  )
   433 					lastMI->setRelPos (QPointF(x,y));
   434 				else
   435 					return false;   // Couldn't read relPos
   436 			}           
   437 		}           
   438 		if (!a.value( "absPosX").isEmpty() ) 
   439 		{
   440 			if (!a.value( "absPosY").isEmpty() ) 
   441 			{
   442 				x=a.value("absPosX").toFloat (&okx);
   443 				y=a.value("absPosY").toFloat (&oky);
   444 				if (okx && oky  )
   445 					lastMI->setAbsPos (QPointF(x,y));
   446 				else
   447 					return false;   // Couldn't read absPos
   448 			}           
   449 		}           
   450 		if (!a.value( "url").isEmpty() ) 
   451 			lastMI->setURL (a.value ("url"));
   452 		if (!a.value( "vymLink").isEmpty() ) 
   453 			lastMI->setVymLink (a.value ("vymLink"));
   454 		if (!a.value( "hideInExport").isEmpty() ) 
   455 			if (a.value("hideInExport")=="true")
   456 				lastMI->setHideInExport(true);
   457 
   458 		if (!a.value( "hideLink").isEmpty()) 
   459 		{
   460 			if (a.value ("hideLink") =="true")
   461 				lastMI->setHideLinkUnselected(true);
   462 			else	
   463 				lastMI->setHideLinkUnselected(false);
   464 		}	
   465 	}
   466 	return true;	
   467 }
   468 
   469 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   470 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   471 	no.clear();
   472 	QString fn;
   473 	if (!a.value( "href").isEmpty() ) 
   474 	{
   475 		// Load note
   476 		fn=parseHREF(a.value ("href") );
   477 		QFile file (fn);
   478 		QString s;						// Reading a note
   479 
   480 		if ( !file.open( QIODevice::ReadOnly) )
   481 		{
   482 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   483 			return false;
   484 		}	
   485 		QTextStream stream( &file );
   486 		QString lines;
   487 		while ( !stream.atEnd() ) {
   488 			lines += stream.readLine()+"\n"; 
   489 		}
   490 		file.close();
   491 
   492 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   493 		no.setNote (lines);
   494 	}		
   495 	if (!a.value( "fonthint").isEmpty() ) 
   496 		no.setFontHint(a.value ("fonthint") );
   497 	lastBranch->setNoteObj(no);
   498 	return true;
   499 }
   500 
   501 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
   502 {
   503 	lastMI=lastImage;
   504 	
   505 	if (!readOOAttr(a)) return false;  
   506 
   507 	if (!a.value( "href").isEmpty() )
   508 	{
   509 		// Load Image
   510 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   511 		{
   512 			QMessageBox::warning( 0, "Warning: " ,
   513 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   514 			lastImage=NULL;
   515 			return true;
   516 		}
   517 		
   518 	}	
   519 	if (!a.value( "zPlane").isEmpty() ) 
   520 		lastImage->setZValue (a.value("zPlane").toInt ());
   521     float x,y;
   522     bool okx,oky;
   523 	if (!a.value( "relPosX").isEmpty() ) 
   524 	{
   525 		if (!a.value( "relPosY").isEmpty() ) 
   526 		{
   527 			// read relPos
   528 			x=a.value("relPosX").toFloat (&okx);
   529 			y=a.value("relPosY").toFloat (&oky);
   530 			if (okx && oky) 
   531 				lastImage->setRelPos (QPointF (x,y) );
   532 			else
   533 				// Couldn't read relPos
   534 				return false;  
   535 		}           
   536 	}	
   537 	
   538 	//FIXME-3 if (!readOOAttr(a)) return false;
   539 
   540 	if (!a.value ("originalName").isEmpty() )
   541 	{
   542 		lastImage->setOriginalFilename (a.value("originalName"));
   543 	}
   544 	return true;
   545 }
   546 
   547 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)	
   548 {
   549 	// object ID is used starting in version 1.8.76
   550 	// (before there was beginBranch and endBranch)
   551 	if (!a.value( "beginID").isEmpty() ) 
   552 	{ 
   553 		if (!a.value( "endID").isEmpty() ) 
   554 		{
   555 			TreeItem *beginBI=model->findBySelectString (a.value( "beginID"));
   556 			TreeItem   *endBI=model->findBySelectString (a.value( "endID"));
   557 			if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
   558 			{
   559 				XLinkItem *xli=model->createXLink (lastBranch,true);
   560 				xli->setBegin ( (BranchItem*)beginBI );
   561 				xli->setEnd ( (BranchItem*)endBI);
   562 				xli->activate();
   563 
   564 				if (!a.value( "color").isEmpty() ) 
   565 				{
   566 					QColor col;
   567 					col.setNamedColor(a.value("color"));
   568 					xli->setColor (col);
   569 				}
   570 
   571 				if (!a.value( "width").isEmpty() ) 
   572 				{
   573 					bool okx;
   574 					xli->setWidth(a.value ("width").toInt (&okx, 10));
   575 				}
   576 				xli->updateXLink();
   577 			}
   578 		}           
   579 	}	
   580 	return true;	// xLinks can only be established at the "end branch", return true
   581 }
   582 
   583 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   584 {
   585 	for (int i=1; i<=a.count(); i++)
   586 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   587 	return true;
   588 }
   589 
   590 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   591 {
   592 	if (!a.value( "key").isEmpty() ) 
   593 	{
   594 		if (!a.value( "value").isEmpty() ) 
   595 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   596 		else
   597 			return false;
   598 		
   599 	} else
   600 		return false;
   601 	
   602 	return true;
   603 }