xml-vym.cpp
author insilmaril
Wed, 25 Nov 2009 10:58:21 +0000
changeset 807 f9f7922989d8
parent 805 fc7a93ff97c3
child 821 4a84d7e444d8
permissions -rw-r--r--
Added demos/vym-contribute.vym, fixes for selecting items
     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     return true;
    32 }
    33 
    34 bool parseVYMHandler::startElement  ( const QString&, const QString&,
    35                     const QString& eName, const QXmlAttributes& atts ) 
    36 {
    37     QColor col;
    38 	/* Testing
    39 	cout << "startElement <"<< qPrintable(eName)
    40 		<<">  state="<<state 
    41 		<<"  laststate="<<stateStack.last()
    42 		<<"   loadMode="<<loadMode
    43 	//	<<"       line="<<QXmlDefaultHandler::lineNumber()
    44 		<<endl;
    45 	*/	
    46 	stateStack.append (state);	
    47     if ( state == StateInit && (eName == "vymmap")  ) 
    48 	{
    49         state = StateMap;
    50 		branchesTotal=branchesCurrent=0;
    51 
    52 		if (loadMode==NewMap )
    53 		{
    54 			// Create mapCenter
    55 			model->clear();
    56 			lastBranch=NULL;
    57 
    58 			if (!atts.value( "author").isEmpty() )
    59 				model->setAuthor(atts.value( "author" ) );
    60 			if (!atts.value( "comment").isEmpty() )
    61 				model->setComment (atts.value( "comment" ) );
    62 			if (!atts.value( "branchCount").isEmpty() )
    63 			{
    64 				branchesTotal=atts.value("branchCount").toInt();
    65 				if (branchesTotal>10)
    66 				{
    67 					mainWindow->setProgressMinimum (0);
    68 					mainWindow->setProgressMaximum (branchesTotal);
    69 					mainWindow->setProgressValue(0);
    70 				}
    71 			} else
    72 			{
    73 				mainWindow->setProgressMinimum (0);
    74 				mainWindow->setProgressMaximum (0);
    75 				mainWindow->setProgressValue(0);
    76 			}
    77 
    78 				
    79 			if (!atts.value( "backgroundColor").isEmpty() )
    80 			{
    81 				col.setNamedColor(atts.value("backgroundColor"));
    82 				model->getScene()->setBackgroundBrush(col);
    83 			}	    
    84 			if (!atts.value( "selectionColor").isEmpty() )
    85 			{
    86 				col.setNamedColor(atts.value("selectionColor"));
    87 				model->setSelectionColor(col);
    88 			}	    
    89 			if (!atts.value( "linkColorHint").isEmpty() ) 
    90 			{
    91 				if (atts.value("linkColorHint")=="HeadingColor")
    92 					model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
    93 				else
    94 					model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
    95 			}
    96 			if (!atts.value( "linkStyle").isEmpty() ) 
    97 				model->setMapLinkStyle(atts.value("linkStyle"));
    98 			if (!atts.value( "linkColor").isEmpty() ) 
    99 			{
   100 				col.setNamedColor(atts.value("linkColor"));
   101 				model->setMapDefLinkColor(col);
   102 			}	
   103 			if (!atts.value( "defXLinkColor").isEmpty() ) 
   104 			{
   105 				col.setNamedColor(atts.value("defXLinkColor"));
   106 				model->setMapDefXLinkColor(col);
   107 			}	
   108 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   109 				model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   110 		}	
   111 		// Check version
   112 		if (!atts.value( "version").isEmpty() ) 
   113 		{
   114 			if (!checkVersion(atts.value("version")))
   115 				QMessageBox::warning( 0, "Warning: Version Problem" ,
   116 				   "<h3>Map is newer than VYM</h3>"
   117 				   "<p>The map you are just trying to load was "
   118 				   "saved using vym " +atts.value("version")+". "
   119 				   "The version of this vym is " + vymVersion + 
   120 				   ". If you run into problems after pressing "
   121 				   "the ok-button below, updating vym should help.");
   122 			else	   
   123 				model->setVersion(atts.value( "version" ));
   124 
   125 		}
   126 
   127 	} else if ( eName == "select" && state == StateMap ) 
   128 	{
   129 		state=StateMapSelect;
   130 	} else if ( eName == "setting" && state == StateMap ) 
   131 	{
   132 		state=StateMapSetting;
   133 		if (loadMode==NewMap)
   134 			readSettingAttr (atts);
   135 	} else if ( eName == "mapcenter" && state == StateMap ) 
   136 	{
   137 		state=StateMapCenter;
   138 		if (loadMode==NewMap)
   139 		{	
   140 			// Really use the found mapcenter as MCO in a new map
   141 			lastBranch=model->createMapCenter(); 
   142 		} else
   143 		{
   144 			// Treat the found mapcenter as a branch 
   145 			// in an existing map
   146 			BranchItem *bi=model->getSelectedBranch();	
   147 			cout << "xml-vym  bi="<<bi->getHeadingStd()<<"  loadMode="<<loadMode<<endl;
   148 			if (bi)
   149 			{
   150 				lastBranch=bi;
   151 				if (loadMode==ImportAdd)
   152 				{
   153 					lastBranch=model->createBranch(lastBranch);
   154 				} //else
   155 					model->clearItem(lastBranch); 
   156 			} else
   157 				// add mapCenter without parent
   158 				lastBranch=model->createMapCenter(); 
   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 or for undo
   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 			return false;
   205 		}	
   206 		if (ti && ti->isBranchLikeType() )
   207 		{
   208 			lastBranch=(BranchItem*)ti;
   209 			if (eName=="branch")
   210 			{
   211 				state=StateBranch;
   212 				if (loadMode==ImportAdd)
   213 				{
   214 					lastBranch=model->createBranch(lastBranch);
   215 				} else
   216 					model->clearItem (lastBranch);
   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 	//	<<"  selString="<<model->getSelectString().toStdString()
   275 		<<endl;
   276 	*/
   277     switch ( state ) 
   278 	{
   279 		case StateMap:
   280 			mainWindow->removeProgressBar();
   281 			break;
   282         case StateMapCenter: 
   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 			// Empty branches may not be scrolled 
   289 			// (happens if bookmarks are imported)
   290 			if (lastBranch->isScrolled() && lastBranch->branchCount()==0) 
   291 				lastBranch->unScroll();
   292 			model->emitDataHasChanged (lastBranch);
   293 
   294 			lastBranch=(BranchItem*)(lastBranch->parent());
   295 			lastBranch->setLastSelectedBranch (0);	// Reset last selected to first child branch
   296             break;
   297         case StateHtml: 
   298 			htmldata+="</"+eName+">";
   299 			if (eName=="html")
   300 			{
   301 				state=StateHtmlNote;  
   302 				htmldata.replace ("<br></br>","<br />");
   303 				no.setNote (htmldata);
   304 				lastBranch->setNoteObj (no);
   305 			}	
   306 			break;
   307 		default: 
   308 			break;
   309     }  
   310 	state=stateStack.takeLast();	
   311 	return true;
   312 }
   313 
   314 bool parseVYMHandler::characters   ( const QString& ch)
   315 {
   316 	//cout << "characters \""<<ch.toStdString()<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
   317 
   318 	QString ch_org=quotemeta (ch);
   319     QString ch_simplified=ch.simplifyWhiteSpace();
   320     if ( ch_simplified.isEmpty() ) return true;
   321 
   322     switch ( state ) 
   323     {
   324         case StateInit: break;
   325         case StateMap: break; 
   326 		case StateMapSelect:
   327 			model->select(ch_simplified);
   328 			break;
   329 		case StateMapSetting:break;
   330         case StateMapCenter: break;
   331         case StateNote:
   332 			lastBranch->setNote(ch_simplified);
   333 			break;
   334         case StateBranch: break;
   335         case StateStandardFlag: 
   336             lastBranch->activateStandardFlag(ch_simplified); 
   337             break;
   338         case StateImage: break;
   339         case StateHtmlNote: break;
   340         case StateHtml:
   341 			htmldata+=ch_org;
   342 			break;
   343         case StateHeading: 
   344             lastBranch->setHeading(ch_simplified);
   345             break;
   346         default: 
   347 			return false;
   348     }
   349     return true;
   350 }
   351 
   352 QString parseVYMHandler::errorString() 
   353 {
   354     return "the document is not in the VYM file format";
   355 }
   356 
   357 bool parseVYMHandler::readBranchAttr (const QXmlAttributes& a)	
   358 {
   359 	//mainWindow->setProgressValue (branchesCurrent++);	// FIXME-2 Makes load incredibily slow
   360 
   361 	lastMI=lastBranch;
   362 
   363 	if (!readOOAttr(a)) return false;
   364 
   365 	if (!a.value( "scrolled").isEmpty() )
   366 		lastBranch->toggleScroll();	
   367 		// (interesting for import of KDE bookmarks)
   368 
   369 /*	if (!a.value( "frameType").isEmpty() )  FIXME-3
   370 		lastOO->setFrameType (a.value("frameType")); //Compatibility 1.8.1
   371 
   372 */	
   373 	if (!a.value( "incImgV").isEmpty() ) 
   374 	{	
   375 		if (a.value("incImgV")=="true")
   376 			lastBranch->setIncludeImagesVer(true);
   377 		else	
   378 			lastBranch->setIncludeImagesVer(false);
   379 	}	
   380 	if (!a.value( "incImgH").isEmpty() ) 
   381 	{	
   382 		if (a.value("incImgH")=="true")
   383 			lastBranch->setIncludeImagesHor(true);
   384 		else	
   385 			lastBranch->setIncludeImagesHor(false);
   386 	}	
   387 	return true;	
   388 }
   389 
   390 bool parseVYMHandler::readFrameAttr (const QXmlAttributes& a)	// FIXME-4 does not work if there is no lmo for treeitem
   391 {
   392 	if (lastMI)
   393 	{
   394 		OrnamentedObj* oo=(OrnamentedObj*)(lastMI->getLMO()); 
   395 		if (oo)
   396 		{
   397 			bool ok;
   398 			int x;
   399 			{
   400 				if (!a.value( "frameType").isEmpty() ) 
   401 					oo->setFrameType (a.value("frameType"));
   402 				if (!a.value( "penColor").isEmpty() ) 
   403 					oo->setFramePenColor (a.value("penColor"));
   404 				if (!a.value( "brushColor").isEmpty() ) 
   405 					oo->setFrameBrushColor (a.value("brushColor"));
   406 				if (!a.value( "padding").isEmpty() ) 
   407 				{
   408 					x=a.value("padding").toInt(&ok);
   409 					if (ok) oo->setFramePadding(x);
   410 				}	
   411 				if (!a.value( "borderWidth").isEmpty() ) 
   412 				{
   413 					x=a.value("borderWidth").toInt(&ok);
   414 					if (ok) oo->setFrameBorderWidth(x);
   415 				}	
   416 			}		
   417 			return true;
   418 		}
   419 	}
   420 	return false;
   421 }
   422 
   423 bool parseVYMHandler::readOOAttr (const QXmlAttributes& a)
   424 {
   425 	if (lastMI)
   426 	{
   427 		bool okx,oky;
   428 		float x,y;
   429 		if (!a.value( "relPosX").isEmpty() ) 
   430 		{
   431 			if (!a.value( "relPosY").isEmpty() ) 
   432 			{
   433 				x=a.value("relPosX").toFloat (&okx);
   434 				y=a.value("relPosY").toFloat (&oky);
   435 				if (okx && oky  )
   436 					lastMI->setRelPos (QPointF(x,y));
   437 				else
   438 					return false;   // Couldn't read relPos
   439 			}           
   440 		}           
   441 		if (!a.value( "absPosX").isEmpty() ) 
   442 		{
   443 			if (!a.value( "absPosY").isEmpty() ) 
   444 			{
   445 				x=a.value("absPosX").toFloat (&okx);
   446 				y=a.value("absPosY").toFloat (&oky);
   447 				if (okx && oky  )
   448 					lastMI->setAbsPos (QPointF(x,y));
   449 				else
   450 					return false;   // Couldn't read absPos
   451 			}           
   452 		}           
   453 		if (!a.value( "id").isEmpty() ) 
   454 			lastMI->setID (a.value ("id"));		
   455 			
   456 		if (!a.value( "url").isEmpty() ) 
   457 			lastMI->setURL (a.value ("url"));
   458 		if (!a.value( "vymLink").isEmpty() ) 
   459 			lastMI->setVymLink (a.value ("vymLink"));
   460 		if (!a.value( "hideInExport").isEmpty() ) 
   461 			if (a.value("hideInExport")=="true")
   462 				lastMI->setHideInExport(true);
   463 
   464 		if (!a.value( "hideLink").isEmpty()) 
   465 		{
   466 			if (a.value ("hideLink") =="true")
   467 				lastMI->setHideLinkUnselected(true);
   468 			else	
   469 				lastMI->setHideLinkUnselected(false);
   470 		}	
   471 	}
   472 	return true;	
   473 }
   474 
   475 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   476 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   477 	no.clear();
   478 	QString fn;
   479 	if (!a.value( "href").isEmpty() ) 
   480 	{
   481 		// Load note
   482 		fn=parseHREF(a.value ("href") );
   483 		QFile file (fn);
   484 		QString s;						// Reading a note
   485 
   486 		if ( !file.open( QIODevice::ReadOnly) )
   487 		{
   488 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   489 			return false;
   490 		}	
   491 		QTextStream stream( &file );
   492 		QString lines;
   493 		while ( !stream.atEnd() ) {
   494 			lines += stream.readLine()+"\n"; 
   495 		}
   496 		file.close();
   497 
   498 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   499 		no.setNote (lines);
   500 	}		
   501 	if (!a.value( "fonthint").isEmpty() ) 
   502 		no.setFontHint(a.value ("fonthint") );
   503 	lastBranch->setNoteObj(no);
   504 	return true;
   505 }
   506 
   507 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
   508 {
   509 	lastMI=lastImage;
   510 	
   511 	//if (!readOOAttr(a)) return false;   FIXME-3
   512 
   513 	if (!a.value( "href").isEmpty() )
   514 	{
   515 		// Load Image
   516 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   517 		{
   518 			QMessageBox::warning( 0, "Warning: " ,
   519 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   520 			lastImage=NULL;
   521 			return true;
   522 		}
   523 		
   524 	}	
   525 	if (!a.value( "zPlane").isEmpty() ) 
   526 		lastImage->setZValue (a.value("zPlane").toInt ());
   527     float x,y;
   528     bool okx,oky;
   529 	if (!a.value( "relPosX").isEmpty() ) 
   530 	{
   531 		if (!a.value( "relPosY").isEmpty() ) 
   532 		{
   533 			// read relPos
   534 			x=a.value("relPosX").toFloat (&okx);
   535 			y=a.value("relPosY").toFloat (&oky);
   536 			if (okx && oky) 
   537 				lastImage->setRelPos (QPointF (x,y) );
   538 			else
   539 				// Couldn't read relPos
   540 				return false;  
   541 		}           
   542 	}	
   543 	
   544 	//FIXME-3 if (!readOOAttr(a)) return false;
   545 
   546 	if (!a.value ("originalName").isEmpty() )
   547 	{
   548 		lastImage->setOriginalFilename (a.value("originalName"));
   549 	}
   550 	return true;
   551 }
   552 
   553 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)	
   554 {
   555 	// object ID is used starting in version 1.8.76
   556 	// (before there was beginBranch and endBranch)
   557 	if (!a.value( "beginID").isEmpty() ) 
   558 	{ 
   559 		if (!a.value( "endID").isEmpty() ) 
   560 		{
   561 			TreeItem *beginBI=model->findID (a.value( "beginID"));
   562 			TreeItem   *endBI=model->findID (a.value( "endID"));
   563 			if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
   564 			{
   565 				XLinkItem *xli=model->createXLink (lastBranch,true);
   566 				xli->setBegin ( (BranchItem*)beginBI );
   567 				xli->setEnd ( (BranchItem*)endBI);
   568 				xli->activate();
   569 
   570 				if (!a.value( "color").isEmpty() ) 
   571 				{
   572 					QColor col;
   573 					col.setNamedColor(a.value("color"));
   574 					xli->setColor (col);
   575 				}
   576 
   577 				if (!a.value( "width").isEmpty() ) 
   578 				{
   579 					bool okx;
   580 					xli->setWidth(a.value ("width").toInt (&okx, 10));
   581 				}
   582 				xli->updateXLink();
   583 			}
   584 		}           
   585 	}	
   586 	return true;	// xLinks can only be established at the "end branch", return true
   587 }
   588 
   589 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   590 {
   591 	for (int i=1; i<=a.count(); i++)
   592 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   593 	return true;
   594 }
   595 
   596 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   597 {
   598 	if (!a.value( "key").isEmpty() ) 
   599 	{
   600 		if (!a.value( "value").isEmpty() ) 
   601 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   602 		else
   603 			return false;
   604 		
   605 	} else
   606 		return false;
   607 	
   608 	return true;
   609 }