xml-vym.cpp
author insilmaril
Thu, 21 Jan 2010 11:56:57 +0000
changeset 821 4a84d7e444d8
parent 805 fc7a93ff97c3
child 822 c2ce9944148c
permissions -rw-r--r--
Various fixes, including combined progress bar for loading maps
     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->removeProgressValue (branchesTotal); //FIXME-2 let mainWindow handle this 
   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( "id").isEmpty() ) 
   451 			lastMI->setID (a.value ("id"));		
   452 			
   453 		if (!a.value( "url").isEmpty() ) 
   454 			lastMI->setURL (a.value ("url"));
   455 		if (!a.value( "vymLink").isEmpty() ) 
   456 			lastMI->setVymLink (a.value ("vymLink"));
   457 		if (!a.value( "hideInExport").isEmpty() ) 
   458 			if (a.value("hideInExport")=="true")
   459 				lastMI->setHideInExport(true);
   460 
   461 		if (!a.value( "hideLink").isEmpty()) 
   462 		{
   463 			if (a.value ("hideLink") =="true")
   464 				lastMI->setHideLinkUnselected(true);
   465 			else	
   466 				lastMI->setHideLinkUnselected(false);
   467 		}	
   468 	}
   469 	return true;	
   470 }
   471 
   472 bool parseVYMHandler::readNoteAttr (const QXmlAttributes& a)
   473 {	// only for backward compatibility (<1.4.6). Use htmlnote now.
   474 	no.clear();
   475 	QString fn;
   476 	if (!a.value( "href").isEmpty() ) 
   477 	{
   478 		// Load note
   479 		fn=parseHREF(a.value ("href") );
   480 		QFile file (fn);
   481 		QString s;						// Reading a note
   482 
   483 		if ( !file.open( QIODevice::ReadOnly) )
   484 		{
   485 			qWarning ("parseVYMHandler::readNoteAttr:  Couldn't load "+fn);
   486 			return false;
   487 		}	
   488 		QTextStream stream( &file );
   489 		QString lines;
   490 		while ( !stream.atEnd() ) {
   491 			lines += stream.readLine()+"\n"; 
   492 		}
   493 		file.close();
   494 
   495 		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
   496 		no.setNote (lines);
   497 	}		
   498 	if (!a.value( "fonthint").isEmpty() ) 
   499 		no.setFontHint(a.value ("fonthint") );
   500 	lastBranch->setNoteObj(no);
   501 	return true;
   502 }
   503 
   504 bool parseVYMHandler::readImageAttr (const QXmlAttributes& a)
   505 {
   506 	lastMI=lastImage;
   507 	
   508 	//if (!readOOAttr(a)) return false;   FIXME-3
   509 
   510 	if (!a.value( "href").isEmpty() )
   511 	{
   512 		// Load Image
   513 		if (!lastImage->load (parseHREF(a.value ("href") ) ))
   514 		{
   515 			QMessageBox::warning( 0, "Warning: " ,
   516 				"Couldn't load image\n"+parseHREF(a.value ("href") ));
   517 			lastImage=NULL;
   518 			return true;
   519 		}
   520 		
   521 	}	
   522 	if (!a.value( "zPlane").isEmpty() ) 
   523 		lastImage->setZValue (a.value("zPlane").toInt ());
   524     float x,y;
   525     bool okx,oky;
   526 	if (!a.value( "relPosX").isEmpty() ) 
   527 	{
   528 		if (!a.value( "relPosY").isEmpty() ) 
   529 		{
   530 			// read relPos
   531 			x=a.value("relPosX").toFloat (&okx);
   532 			y=a.value("relPosY").toFloat (&oky);
   533 			if (okx && oky) 
   534 				lastImage->setRelPos (QPointF (x,y) );
   535 			else
   536 				// Couldn't read relPos
   537 				return false;  
   538 		}           
   539 	}	
   540 	
   541 	//FIXME-3 if (!readOOAttr(a)) return false;
   542 
   543 	if (!a.value ("originalName").isEmpty() )
   544 	{
   545 		lastImage->setOriginalFilename (a.value("originalName"));
   546 	}
   547 	return true;
   548 }
   549 
   550 bool parseVYMHandler::readXLinkAttr (const QXmlAttributes& a)	
   551 {
   552 	// object ID is used starting in version 1.8.76
   553 	// (before there was beginBranch and endBranch)
   554 	if (!a.value( "beginID").isEmpty() ) 
   555 	{ 
   556 		if (!a.value( "endID").isEmpty() ) 
   557 		{
   558 			TreeItem *beginBI=model->findID (a.value( "beginID"));
   559 			TreeItem   *endBI=model->findID (a.value( "endID"));
   560 			if (beginBI && endBI && beginBI->isBranchLikeType() && endBI->isBranchLikeType() )
   561 			{
   562 				XLinkItem *xli=model->createXLink (lastBranch,true);
   563 				xli->setBegin ( (BranchItem*)beginBI );
   564 				xli->setEnd ( (BranchItem*)endBI);
   565 				xli->activate();
   566 
   567 				if (!a.value( "color").isEmpty() ) 
   568 				{
   569 					QColor col;
   570 					col.setNamedColor(a.value("color"));
   571 					xli->setColor (col);
   572 				}
   573 
   574 				if (!a.value( "width").isEmpty() ) 
   575 				{
   576 					bool okx;
   577 					xli->setWidth(a.value ("width").toInt (&okx, 10));
   578 				}
   579 				xli->updateXLink();
   580 			}
   581 		}           
   582 	}	
   583 	return true;	// xLinks can only be established at the "end branch", return true
   584 }
   585 
   586 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   587 {
   588 	for (int i=1; i<=a.count(); i++)
   589 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   590 	return true;
   591 }
   592 
   593 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   594 {
   595 	if (!a.value( "key").isEmpty() ) 
   596 	{
   597 		if (!a.value( "value").isEmpty() ) 
   598 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   599 		else
   600 			return false;
   601 		
   602 	} else
   603 		return false;
   604 	
   605 	return true;
   606 }