xml-vym.cpp
author insilmaril
Sat, 08 Aug 2009 21:58:26 +0000
changeset 787 c6bb4fdcc55f
parent 786 6269016c9905
child 788 78ba80b54bc4
permissions -rw-r--r--
Fixed selections with cursor in MapEditor
     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 
    16 static ImageItem *lastImageItem;
    17 static MapItem *lastMI;
    18 
    19 extern Main *mainWindow;
    20 extern Settings settings;
    21 extern QString vymVersion;
    22 
    23 bool parseVYMHandler::startDocument()
    24 {
    25     errorProt = "";
    26     state = StateInit;
    27     laststate = StateInit;
    28 	stateStack.clear();
    29 	stateStack.append(StateInit);
    30 	htmldata="";
    31 	isVymPart=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=branchesCurrent=0;
    52 
    53 		if (loadMode==NewMap )
    54 		{
    55 			// Create mapCenter
    56 			model->clear();
    57 			lastBranch=NULL;
    58 
    59 			if (!atts.value( "author").isEmpty() )
    60 				model->setAuthor(atts.value( "author" ) );
    61 			if (!atts.value( "comment").isEmpty() )
    62 				model->setComment (atts.value( "comment" ) );
    63 			if (!atts.value( "branchCount").isEmpty() )
    64 			{
    65 				branchesTotal=atts.value("branchCount").toInt();
    66 				if (branchesTotal>10)
    67 				{
    68 					mainWindow->setProgressMinimum (0);
    69 					mainWindow->setProgressMaximum (branchesTotal);
    70 					mainWindow->setProgressValue(0);
    71 				}
    72 			} else
    73 			{
    74 				mainWindow->setProgressMinimum (0);
    75 				mainWindow->setProgressMaximum (0);
    76 				mainWindow->setProgressValue(0);
    77 			}
    78 
    79 				
    80 			if (!atts.value( "backgroundColor").isEmpty() )
    81 			{
    82 				col.setNamedColor(atts.value("backgroundColor"));
    83 				model->getScene()->setBackgroundBrush(col);
    84 			}	    
    85 			if (!atts.value( "selectionColor").isEmpty() )
    86 			{
    87 				col.setNamedColor(atts.value("selectionColor"));
    88 				model->setSelectionColor(col);
    89 			}	    
    90 			if (!atts.value( "linkColorHint").isEmpty() ) 
    91 			{
    92 				if (atts.value("linkColorHint")=="HeadingColor")
    93 					model->setMapLinkColorHint(LinkableMapObj::HeadingColor);
    94 				else
    95 					model->setMapLinkColorHint(LinkableMapObj::DefaultColor);
    96 			}
    97 			if (!atts.value( "linkStyle").isEmpty() ) 
    98 				model->setMapLinkStyle(atts.value("linkStyle"));
    99 			if (!atts.value( "linkColor").isEmpty() ) 
   100 			{
   101 				col.setNamedColor(atts.value("linkColor"));
   102 				model->setMapDefLinkColor(col);
   103 			}	
   104 			if (!atts.value( "defXLinkColor").isEmpty() ) 
   105 			{
   106 				col.setNamedColor(atts.value("defXLinkColor"));
   107 				model->setMapDefXLinkColor(col);
   108 			}	
   109 			if (!atts.value( "defXLinkWidth").isEmpty() ) 
   110 				model->setMapDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
   111 		}	
   112 		// Check version
   113 		if (!atts.value( "version").isEmpty() ) 
   114 		{
   115 			if (!checkVersion(atts.value("version")))
   116 				QMessageBox::warning( 0, "Warning: Version Problem" ,
   117 				   "<h3>Map is newer than VYM</h3>"
   118 				   "<p>The map you are just trying to load was "
   119 				   "saved using vym " +atts.value("version")+". "
   120 				   "The version of this vym is " + vymVersion + 
   121 				   ". If you run into problems after pressing "
   122 				   "the ok-button below, updating vym should help.");
   123 			else	   
   124 				model->setVersion(atts.value( "version" ));
   125 
   126 		}
   127 
   128 	} else if ( eName == "select" && state == StateMap ) 
   129 	{
   130 		state=StateMapSelect;
   131 	} else if ( eName == "setting" && state == StateMap ) 
   132 	{
   133 		state=StateMapSetting;
   134 		if (loadMode==NewMap)
   135 			readSettingAttr (atts);
   136 	} else if ( eName == "mapcenter" && state == StateMap ) 
   137 	{
   138 		state=StateMapCenter;
   139 		if (loadMode==NewMap)
   140 		{	
   141 			// Really use the found mapcenter as MCO in a new map
   142 
   143 			lastBranch=model->createMapCenter(); 
   144 		} else
   145 		{
   146 			// Treat the found mapcenter as a branch 
   147 			// in an existing map
   148 			BranchItem *bi=model->getSelectedBranch();	//FIXME-3 selection is no longer used here...
   149 			if (bi)
   150 			{
   151 				lastBranch=bi;
   152 				if (loadMode==ImportAdd)
   153 				{
   154 					lastBranch=model->createBranch(lastBranch);
   155 				} //else
   156 					//FIXME-3 lastBranch->clear();
   157 			} else
   158 				return false;
   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!
   193 		isVymPart=true;
   194 		TreeItem *ti=model->getSelectedItem();	//FIXME-3 selection is no longer used here...
   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 		}	
   205 		if (ti && ti->isBranchLikeType() )
   206 		{
   207 			lastBranch=(BranchItem*)ti;
   208 			if (eName=="branch")
   209 			{
   210 				state=StateBranch;
   211 				if (loadMode==ImportAdd)
   212 				{
   213 					lastBranch=model->createBranch(lastBranch);
   214 					
   215 				} else
   216 					//FIXME-2 lastBranch->clear();
   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++);
   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() && loadMode==NewMap ) 
   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"));		// FIXME-3
   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 	QColor col;
   556 	bool okx;
   557 	bool success=false;
   558 	XLinkObj *xlo=new XLinkObj (model->getScene());
   559 	if (!a.value( "color").isEmpty() ) 
   560 	{
   561 		col.setNamedColor(a.value("color"));
   562 		xlo->setColor (col);
   563 	}
   564 
   565 	if (!a.value( "width").isEmpty() ) 
   566 	{
   567 		xlo->setWidth(a.value ("width").toInt (&okx, 10));
   568 	}
   569 
   570 	// Connecting by select string for compatibility with version < 1.8.76
   571 	if (!a.value( "beginBranch").isEmpty() ) 
   572 	{ 
   573 		if (!a.value( "endBranch").isEmpty() ) 
   574 		{
   575 			TreeItem *ti=model->findBySelectString (a.value( "beginBranch"));
   576 			if (ti && ti->isBranchLikeType())
   577 			{
   578 			/* FIXME-2 xLinks
   579 				xlo->setBegin ((BranchObj*)lmo);
   580 				lmo=model->findBySelectString (a.value( "endBranch"));
   581 				if (lmo && typid (*lmo)==typid (BranchObj))
   582 				{
   583 					xlo->setEnd ((BranchObj*)(lmo));
   584 					xlo->activate();
   585 					success=true;
   586 				}
   587 			*/
   588 			}
   589 		}           
   590 	}	
   591 
   592 	// object ID is used starting in version 1.8.76
   593 	/* FIXME-2 xLinks
   594 	if (!a.value( "beginID").isEmpty() ) 
   595 	{ 
   596 		if (!a.value( "endID").isEmpty() ) 
   597 		{
   598 			LinkableMapObj *lmo=model->findID (a.value( "beginID"));
   599 			if (lmo && typid (*lmo)==typid (BranchObj))
   600 			{
   601 				xlo->setBegin ((BranchObj*)lmo);
   602 				lmo=model->findID (a.value( "endID"));
   603 				if (lmo && typid (*lmo)==typid (BranchObj))
   604 				{
   605 					xlo->setEnd ((BranchObj*)(lmo));
   606 					xlo->activate();
   607 					success=true;
   608 				}
   609 			}
   610 		}           
   611 	}	
   612 	*/
   613 	if (!success) delete (xlo);
   614 	return true;	// xLinks can only be established at the "end branch", return true
   615 }
   616 
   617 bool parseVYMHandler::readHtmlAttr (const QXmlAttributes& a)
   618 {
   619 	for (int i=1; i<=a.count(); i++)
   620 		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
   621 	return true;
   622 }
   623 
   624 bool parseVYMHandler::readSettingAttr (const QXmlAttributes& a)
   625 {
   626 	if (!a.value( "key").isEmpty() ) 
   627 	{
   628 		if (!a.value( "value").isEmpty() ) 
   629 			settings.setLocalEntry (model->getDestPath(), a.value ("key"), a.value ("value"));
   630 		else
   631 			return false;
   632 		
   633 	} else
   634 		return false;
   635 	
   636 	return true;
   637 }