vymmodel.cpp
author insilmaril
Mon, 20 Apr 2009 10:42:05 +0000
changeset 754 db0ec4bcf416
parent 753 25a77484ec72
child 755 ed5b407975b3
permissions -rw-r--r--
minor fixes
     1 #include <QApplication>
     2 #include <typeinfo>
     3 
     4 #include "vymmodel.h"
     5 
     6 #include "treeitem.h"
     7 #include "branchitem.h"
     8 #include "mapcenteritem.h"
     9 #include "editxlinkdialog.h"
    10 #include "exports.h"
    11 #include "exportxhtmldialog.h"
    12 #include "file.h"
    13 #include "geometry.h"		// for addBBox
    14 #include "mainwindow.h"
    15 #include "mapcenterobj.h"
    16 #include "misc.h"
    17 #include "parser.h"
    18 #include "selection.h"
    19 
    20 
    21 #include "warningdialog.h"
    22 #include "xml-freemind.h"
    23 #include "xmlobj.h"
    24 #include "xml-vym.h"
    25 
    26 
    27 extern bool debug;
    28 extern Main *mainWindow;
    29 extern Settings settings;
    30 extern QString tmpVymDir;
    31 
    32 extern TextEditor *textEditor;
    33 
    34 extern QString clipboardDir;
    35 extern QString clipboardFile;
    36 extern bool clipboardEmpty;
    37 
    38 extern ImageIO imageIO;
    39 
    40 extern QString vymName;
    41 extern QString vymVersion;
    42 extern QDir vymBaseDir;
    43 
    44 extern QDir lastImageDir;
    45 extern QDir lastFileDir;
    46 
    47 extern FlagRowObj *standardFlagsDefault;
    48 
    49 extern Settings settings;
    50 
    51 
    52 
    53 int VymModel::mapNum=0;	// make instance
    54 
    55 VymModel::VymModel() 
    56 {
    57 //    cout << "Const VymModel\n";
    58 	init();
    59 	rootItem->setModel (this);
    60 }
    61 
    62 
    63 VymModel::~VymModel() 
    64 {
    65 //    cout << "Destr VymModel\n";
    66 	autosaveTimer->stop();
    67 	fileChangedTimer->stop();
    68 	clear();
    69 }	
    70 
    71 void VymModel::clear() 
    72 {
    73 	selModel->clearSelection();
    74 
    75 	//QModelIndex ri=index(rootItem);
    76 	//removeRows (0, rowCount(ri),ri);		// FIXME-2 here should be at least a beginRemoveRows...
    77 }
    78 
    79 void VymModel::init () 
    80 {
    81 	// We should have at least one map center to start with
    82 	// addMapCenter();  FIXME-2 VM create this in MapEditor as long as model is part of that
    83 
    84 	// No MapEditor yet
    85 	mapEditor=NULL;
    86 
    87 	// Also no scene yet (should not be needed anyway)  FIXME-3 VM
    88 	mapScene=NULL;
    89 
    90 	// History 
    91 	mapNum++;
    92     mapChanged=false;
    93 	mapDefault=true;
    94 	mapUnsaved=false;
    95 
    96 	curStep=0;
    97 	redosAvail=0;
    98 	undosAvail=0;
    99 
   100  	stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
   101 	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
   102 	mainWindow->updateHistory (undoSet);
   103 
   104 	// Create tmp dirs
   105 	makeTmpDirectories();
   106 	
   107 	// Files
   108 	zipped=true;
   109 	filePath="";
   110 	fileName=tr("unnamed");
   111 	mapName="";
   112 	blockReposition=false;
   113 	blockSaveState=false;
   114 
   115 	autosaveTimer=new QTimer (this);
   116 	connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
   117 
   118 	fileChangedTimer=new QTimer (this);
   119 	fileChangedTimer->start(3000);
   120 	connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
   121 
   122 
   123 	// selections
   124 	selModel=NULL;
   125 
   126 	// find routine
   127 	findCurrent=NULL;				
   128 	findPrevious=NULL;				
   129 	EOFind=false;
   130 
   131 	// animations
   132 	animationUse=settings.readBoolEntry("/animation/use",false);
   133 	animationTicks=settings.readNumEntry("/animation/ticks",10);
   134 	animationInterval=settings.readNumEntry("/animation/interval",50);
   135 	animObjList.clear();
   136 	animationTimer=new QTimer (this);
   137 	connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
   138 
   139 	// View - map
   140 	defLinkColor=QColor (0,0,255);
   141 	defXLinkColor=QColor (180,180,180);
   142 	linkcolorhint=LinkableMapObj::DefaultColor;
   143 	linkstyle=LinkableMapObj::PolyParabel;
   144 	defXLinkWidth=1;
   145 	defXLinkColor=QColor (230,230,230);
   146 
   147 	hidemode=TreeItem::HideNone;
   148 
   149 	// Network
   150 	netstate=Offline;
   151 
   152 	// Create MapCenter
   153 	//  addMapCenter();  FIXME-2 VM create this in MapEditor until BO and MCO are independent of scene
   154 
   155 }
   156 
   157 void VymModel::makeTmpDirectories()
   158 {
   159 	// Create unique temporary directories
   160 	tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
   161 	histPath = tmpMapDir+"/history";
   162 	QDir d;
   163 	d.mkdir (tmpMapDir);
   164 }
   165 
   166 
   167 MapEditor* VymModel::getMapEditor()	// FIXME-2 VM better return favourite editor here
   168 {
   169 	return mapEditor;
   170 }
   171 
   172 bool VymModel::isRepositionBlocked()
   173 {
   174 	return blockReposition;
   175 }
   176 
   177 void VymModel::updateActions()	// FIXME-2  maybe don't update if blockReposition is set
   178 {
   179 	cout << "VymModel::updateActions \n";
   180 	// Tell mainwindow to update states of actions
   181 	mainWindow->updateActions();
   182 }
   183 
   184 
   185 
   186 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, TreeItem *saveSel)
   187 {
   188 	// tmpdir		temporary directory to which data will be written
   189 	// prefix		mapname, which will be appended to images etc.
   190 	// writeflags	Only write flags for "real" save of map, not undo
   191 	// offset		offset of bbox of whole map in scene. 
   192 	//				Needed for XML export
   193 	
   194 
   195 	XMLObj xml;
   196 
   197 	// Save Header
   198 	QString ls;
   199 	switch (linkstyle)
   200 	{
   201 		case LinkableMapObj::Line: 
   202 			ls="StyleLine";
   203 			break;
   204 		case LinkableMapObj::Parabel:
   205 			ls="StyleParabel";
   206 			break;
   207 		case LinkableMapObj::PolyLine:	
   208 			ls="StylePolyLine";
   209 			break;
   210 		default:
   211 			ls="StylePolyParabel";
   212 			break;
   213 	}	
   214 
   215 	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
   216 	QString colhint="";
   217 	if (linkcolorhint==LinkableMapObj::HeadingColor) 
   218 		colhint=xml.attribut("linkColorHint","HeadingColor");
   219 
   220 	QString mapAttr=xml.attribut("version",vymVersion);
   221 	if (!saveSel)
   222 		mapAttr+= xml.attribut("author",author) +
   223 				  xml.attribut("comment",comment) +
   224 			      xml.attribut("date",getDate()) +
   225 				  xml.attribut("branchCount", QString().number(branchCount())) +
   226 		          xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
   227 		          xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
   228 		          xml.attribut("linkStyle", ls ) +
   229 		          xml.attribut("linkColor", defLinkColor.name() ) +
   230 		          xml.attribut("defXLinkColor", defXLinkColor.name() ) +
   231 		          xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
   232 		          colhint; 
   233 	s+=xml.beginElement("vymmap",mapAttr);
   234 	xml.incIndent();
   235 
   236 	// Find the used flags while traversing the tree	// FIXME-2 this can be done local to vymmodel maybe...
   237 	standardFlagsDefault->resetUsedCounter();
   238 	
   239 	// Reset the counters before saving
   240 	// TODO constr. of FIO creates lots of objects, better do this in some other way...
   241 	FloatImageObj (mapScene).resetSaveCounter();// FIXME-2 this can be done local to vymmodel maybe...
   242 
   243 	// Build xml recursivly
   244 	if (!saveSel || typeid (*saveSel) == typeid (MapCenterObj))
   245 		// Save all mapcenters as complete map, if saveSel not set
   246 		s+=saveTreeToDir(tmpdir,prefix,writeflags,offset);
   247 	else
   248 	{
   249 		if ( typeid(*saveSel) == typeid(BranchObj) )
   250 			// Save Subtree
   251 			s+=((BranchObj*)(saveSel))->saveToDir(tmpdir,prefix,offset);
   252 		else if ( typeid(*saveSel) == typeid(FloatImageObj) )
   253 			// Save image
   254 			s+=((FloatImageObj*)(saveSel))->saveToDir(tmpdir,prefix);
   255 	}
   256 
   257 	// Save local settings
   258 	s+=settings.getDataXML (destPath);
   259 
   260 	// Save selection
   261 	if (!selection.isEmpty() && !saveSel ) 
   262 		s+=xml.valueElement("select",selection.getSelectString());
   263 
   264 	xml.decIndent();
   265 	s+=xml.endElement("vymmap");
   266 
   267 	if (writeflags)
   268 		standardFlagsDefault->saveToDir (tmpdir+"/flags/","",writeflags);
   269 	return s;
   270 }
   271 
   272 QString VymModel::saveTreeToDir (const QString &tmpdir,const QString &prefix, int verbose, const QPointF &offset)	// FIXME-4 verbose not needed (used to write icons)
   273 {
   274     QString s;
   275 
   276 	for (int i=0; i<rootItem->branchCount(); i++)	// FIXME-0 remove mapCenters
   277 		s+=((MapCenterItem*)rootItem->getBranchNum(i))->saveToDir (tmpdir,prefix,offset);
   278     return s;
   279 }
   280 
   281 void VymModel::setFilePath(QString fpath, QString destname)
   282 {
   283 	if (fpath.isEmpty() || fpath=="")
   284 	{
   285 		filePath="";
   286 		fileName="";
   287 		destPath="";
   288 	} else
   289 	{
   290 		filePath=fpath;		// becomes absolute path
   291 		fileName=fpath;		// gets stripped of path
   292 		destPath=destname;	// needed for vymlinks and during load to reset fileChangedTime
   293 
   294 		// If fpath is not an absolute path, complete it
   295 		filePath=QDir(fpath).absPath();
   296 		fileDir=filePath.left (1+filePath.findRev ("/"));
   297 
   298 		// Set short name, too. Search from behind:
   299 		int i=fileName.findRev("/");
   300 		if (i>=0) fileName=fileName.remove (0,i+1);
   301 
   302 		// Forget the .vym (or .xml) for name of map
   303 		mapName=fileName.left(fileName.findRev(".",-1,true) );
   304 	}
   305 }
   306 
   307 void VymModel::setFilePath(QString fpath)
   308 {
   309 	setFilePath (fpath,fpath);
   310 }
   311 
   312 QString VymModel::getFilePath()
   313 {
   314 	return filePath;
   315 }
   316 
   317 QString VymModel::getFileName()
   318 {
   319 	return fileName;
   320 }
   321 
   322 QString VymModel::getMapName()
   323 {
   324 	return mapName;
   325 }
   326 
   327 QString VymModel::getDestPath()
   328 {
   329 	return destPath;
   330 }
   331 
   332 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
   333 {
   334 	ErrorCode err=success;
   335 
   336 	parseBaseHandler *handler;
   337 	fileType=ftype;
   338 	switch (fileType)
   339 	{
   340 		case VymMap: handler=new parseVYMHandler; break;
   341 		case FreemindMap : handler=new parseFreemindHandler; break;
   342 		default: 
   343 			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   344 				   "Unknown FileType in VymModel::load()");
   345 		return aborted;	
   346 	}
   347 
   348 	bool zipped_org=zipped;
   349 
   350 	if (lmode==NewMap)
   351 	{
   352 		selModel->clearSelection();
   353 		// FIXME-2 VM not needed??? model->setMapEditor(this);
   354 		// (map state is set later at end of load...)
   355 	} else
   356 	{
   357 		BranchItem *bi=getSelectedBranchItem();
   358 		if (!bi) return aborted;
   359 		if (lmode==ImportAdd)
   360 			saveStateChangingPart(
   361 				bi,
   362 				bi,
   363 				QString("addMapInsert (%1)").arg(fname),
   364 				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bi)));
   365 		else	
   366 			saveStateChangingPart(
   367 				bi,
   368 				bi,
   369 				QString("addMapReplace(%1)").arg(fname),
   370 				QString("Add map %1 to %2").arg(fname).arg(getObjectName(bi)));
   371 	}	
   372     
   373 
   374 	// Create temporary directory for packing
   375 	bool ok;
   376 	QString tmpZipDir=makeTmpDir (ok,"vym-pack");
   377 	if (!ok)
   378 	{
   379 		QMessageBox::critical( 0, tr( "Critical Load Error" ),
   380 		   tr("Couldn't create temporary directory before load\n"));
   381 		return aborted; 
   382 	}
   383 
   384 	// Try to unzip file
   385 	err=unzipDir (tmpZipDir,fname);
   386 	QString xmlfile;
   387 	if (err==nozip)
   388 	{
   389 		xmlfile=fname;
   390 		zipped=false;
   391 	} else
   392 	{
   393 		zipped=true;
   394 		
   395 		// Look for mapname.xml
   396 		xmlfile= fname.left(fname.findRev(".",-1,true));
   397 		xmlfile=xmlfile.section( '/', -1 );
   398 		QFile mfile( tmpZipDir + "/" + xmlfile + ".xml");
   399 		if (!mfile.exists() )
   400 		{
   401 			// mapname.xml does not exist, well, 
   402 			// maybe someone renamed the mapname.vym file...
   403 			// Try to find any .xml in the toplevel 
   404 			// directory of the .vym file
   405 			QStringList flist=QDir (tmpZipDir).entryList("*.xml");
   406 			if (flist.count()==1) 
   407 			{
   408 				// Only one entry, take this one
   409 				xmlfile=tmpZipDir + "/"+flist.first();
   410 			} else
   411 			{
   412 				for ( QStringList::Iterator it = flist.begin(); it != flist.end(); ++it ) 
   413 					*it=tmpZipDir + "/" + *it;
   414 				// TODO Multiple entries, load all (but only the first one into this ME)
   415 				//mainWindow->fileLoadFromTmp (flist);
   416 				//returnCode=1;	// Silently forget this attempt to load
   417 				qWarning ("MainWindow::load (fn)  multimap found...");
   418 			}	
   419 				
   420 			if (flist.isEmpty() )
   421 			{
   422 				QMessageBox::critical( 0, tr( "Critical Load Error" ),
   423 						   tr("Couldn't find a map (*.xml) in .vym archive.\n"));
   424 				err=aborted;				   
   425 			}	
   426 		} //file doesn't exist	
   427 		else
   428 			xmlfile=mfile.name();
   429 	}
   430 
   431 	QFile file( xmlfile);
   432 
   433 	// I am paranoid: file should exist anyway
   434 	// according to check in mainwindow.
   435 	if (!file.exists() )
   436 	{
   437 		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   438 				   tr(QString("Couldn't open map %1").arg(file.name())));
   439 		err=aborted;	
   440 	} else
   441 	{
   442 		bool blockSaveStateOrg=blockSaveState;
   443 		blockReposition=true;
   444 		blockSaveState=true;
   445 		QXmlInputSource source( file);
   446 		QXmlSimpleReader reader;
   447 		reader.setContentHandler( handler );
   448 		reader.setErrorHandler( handler );
   449 		handler->setModel ( this);
   450 
   451 
   452 		// We need to set the tmpDir in order  to load files with rel. path
   453 		QString tmpdir;
   454 		if (zipped)
   455 			tmpdir=tmpZipDir;
   456 		else
   457 			tmpdir=fname.left(fname.findRev("/",-1));	
   458 		handler->setTmpDir (tmpdir);
   459 		handler->setInputFile (file.name());
   460 		handler->setLoadMode (lmode);
   461 		bool ok = reader.parse( source );
   462 		blockReposition=false;
   463 		blockSaveState=blockSaveStateOrg;
   464 		file.close();
   465 		if ( ok ) 
   466 		{
   467 			reposition();	// FIXME-2 VM reposition the view instead...
   468 			selection.update();
   469 			if (lmode==NewMap)
   470 			{
   471 				mapDefault=false;
   472 				mapChanged=false;
   473 				mapUnsaved=false;
   474 				autosaveTimer->stop();
   475 			}
   476 
   477 			// Reset timestamp to check for later updates of file
   478 			fileChangedTime=QFileInfo (destPath).lastModified();
   479 		} else 
   480 		{
   481 			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   482 					   tr( handler->errorProtocol() ) );
   483 			// returnCode=1;	
   484 			// Still return "success": the map maybe at least
   485 			// partially read by the parser
   486 		}	
   487 	}	
   488 
   489 	// Delete tmpZipDir
   490 	removeDir (QDir(tmpZipDir));
   491 
   492 	// Restore original zip state
   493 	zipped=zipped_org;
   494 
   495 	updateActions();
   496 	return err;
   497 }
   498 
   499 ErrorCode VymModel::save (const SaveMode &savemode)
   500 {
   501 	QString tmpZipDir;
   502 	QString mapFileName;
   503 	QString safeFilePath;
   504 
   505 	ErrorCode err=success;
   506 
   507 	if (zipped)
   508 		// save as .xml
   509 		mapFileName=mapName+".xml";
   510 	else
   511 		// use name given by user, even if he chooses .doc
   512 		mapFileName=fileName;
   513 
   514 	// Look, if we should zip the data:
   515 	if (!zipped)
   516 	{
   517 		QMessageBox mb( vymName,
   518 			tr("The map %1\ndid not use the compressed "
   519 			"vym file format.\nWriting it uncompressed will also write images \n"
   520 			"and flags and thus may overwrite files in the "
   521 			"given directory\n\nDo you want to write the map").arg(filePath),
   522 			QMessageBox::Warning,
   523 			QMessageBox::Yes | QMessageBox::Default,
   524 			QMessageBox::No ,
   525 			QMessageBox::Cancel | QMessageBox::Escape);
   526 		mb.setButtonText( QMessageBox::Yes, tr("compressed (vym default)") );
   527 		mb.setButtonText( QMessageBox::No, tr("uncompressed") );
   528 		mb.setButtonText( QMessageBox::Cancel, tr("Cancel"));
   529 		switch( mb.exec() ) 
   530 		{
   531 			case QMessageBox::Yes:
   532 				// save compressed (default file format)
   533 				zipped=true;
   534 				break;
   535 			case QMessageBox::No:
   536 				// save uncompressed
   537 				zipped=false;
   538 				break;
   539 			case QMessageBox::Cancel:
   540 				// do nothing
   541 				return aborted;
   542 				break;
   543 		}
   544 	}
   545 
   546 	// First backup existing file, we 
   547 	// don't want to add to old zip archives
   548 	QFile f(destPath);
   549 	if (f.exists())
   550 	{
   551 		if ( settings.value ("/mapeditor/writeBackupFile").toBool())
   552 		{
   553 			QString backupFileName(destPath + "~");
   554 			QFile backupFile(backupFileName);
   555 			if (backupFile.exists() && !backupFile.remove())
   556 			{
   557 				QMessageBox::warning(0, tr("Save Error"),
   558 									 tr("%1\ncould not be removed before saving").arg(backupFileName));
   559 			}
   560 			else if (!f.rename(backupFileName))
   561 			{
   562 				QMessageBox::warning(0, tr("Save Error"),
   563 									 tr("%1\ncould not be renamed before saving").arg(destPath));
   564 			}
   565 		}
   566 	}
   567 
   568 	if (zipped)
   569 	{
   570 		// Create temporary directory for packing
   571 		bool ok;
   572 		tmpZipDir=makeTmpDir (ok,"vym-zip");
   573 		if (!ok)
   574 		{
   575 			QMessageBox::critical( 0, tr( "Critical Load Error" ),
   576 			   tr("Couldn't create temporary directory before save\n"));
   577 			return aborted; 
   578 		}
   579 
   580 		safeFilePath=filePath;
   581 		setFilePath (tmpZipDir+"/"+ mapName+ ".xml", safeFilePath);
   582 	} // zipped
   583 
   584 	// Create mapName and fileDir
   585 	makeSubDirs (fileDir);
   586 
   587 	QString saveFile;
   588 	if (savemode==CompleteMap || selection.isEmpty())
   589 	{
   590 		// Save complete map
   591 		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
   592 		mapChanged=false;
   593 		mapUnsaved=false;
   594 		autosaveTimer->stop();
   595 	}
   596 	else	
   597 	{
   598 		// Save part of map
   599 		if (selectionType()==TreeItem::Image)
   600 			saveFloatImage();
   601 		else	
   602 			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),getSelectedBranchItem());	
   603 		// TODO take care of multiselections
   604 	}	
   605 
   606 	if (!saveStringToDisk(fileDir+mapFileName,saveFile))
   607 	{
   608 		err=aborted;
   609 		qWarning ("ME::saveStringToDisk failed!");
   610 	}
   611 
   612 	if (zipped)
   613 	{
   614 		// zip
   615 		if (err==success) err=zipDir (tmpZipDir,destPath);
   616 
   617 		// Delete tmpDir
   618 		removeDir (QDir(tmpZipDir));
   619 
   620 		// Restore original filepath outside of tmp zip dir
   621 		setFilePath (safeFilePath);
   622 	}
   623 
   624 	updateActions();
   625 	fileChangedTime=QFileInfo (destPath).lastModified();
   626 	return err;
   627 }
   628 
   629 void VymModel::addMapReplaceInt(const QString &undoSel, const QString &path)
   630 {
   631 	QString pathDir=path.left(path.findRev("/"));
   632 	QDir d(pathDir);
   633 	QFile file (path);
   634 
   635 	if (d.exists() )
   636 	{
   637 		// We need to parse saved XML data
   638 		parseVYMHandler handler;
   639 		QXmlInputSource source( file);
   640 		QXmlSimpleReader reader;
   641 		reader.setContentHandler( &handler );
   642 		reader.setErrorHandler( &handler );
   643 		handler.setModel ( this);
   644 		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
   645 		if (undoSel.isEmpty())
   646 		{
   647 			unselect();
   648 			clear();
   649 			handler.setLoadMode (NewMap);
   650 		} else	
   651 		{
   652 			select (undoSel);
   653 			handler.setLoadMode (ImportReplace);
   654 		}	
   655 		blockReposition=true;
   656 		bool ok = reader.parse( source );
   657 		blockReposition=false;
   658 		if (! ok ) 
   659 		{	
   660 			// This should never ever happen
   661 			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
   662 								    handler.errorProtocol());
   663 		}
   664 	} else	
   665 		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   666 }
   667 
   668 void VymModel::addMapInsertInt (const QString &path, int pos)
   669 {
   670 /* FIXME-2  addMapInsertInt not ported yet
   671 	BranchObj *sel=getSelectedBranch();
   672 	if (sel)
   673 	{
   674 		QString pathDir=path.left(path.findRev("/"));
   675 		QDir d(pathDir);
   676 		QFile file (path);
   677 
   678 		if (d.exists() )
   679 		{
   680 			// We need to parse saved XML data
   681 			parseVYMHandler handler;
   682 			QXmlInputSource source( file);
   683 			QXmlSimpleReader reader;
   684 			reader.setContentHandler( &handler );
   685 			reader.setErrorHandler( &handler );
   686 			handler.setModel (this);
   687 			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
   688 			handler.setLoadMode (ImportAdd);
   689 			blockReposition=true;
   690 			bool ok = reader.parse( source );
   691 			blockReposition=false;
   692 			if (! ok ) 
   693 			{	
   694 				// This should never ever happen
   695 				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
   696 										handler.errorProtocol());
   697 			}
   698 			if (sel->getDepth()>0)
   699 				sel->getLastBranch()->linkTo (sel,pos);
   700 		} else	
   701 			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
   702 	}		
   703 */
   704 }
   705 
   706 FloatImageObj* VymModel::loadFloatImageInt (QString fn)
   707 {
   708 	TreeItem *fi=createImage();
   709 	if (fi)
   710 	{
   711 		FloatImageObj *fio= ((FloatImageObj*)fi->getLMO());
   712 		fio->load (fn);
   713 		reposition();
   714 		return fio;
   715 	}
   716 	return NULL;
   717 }	
   718 
   719 void VymModel::loadFloatImage ()	// FIXME-2
   720 {
   721 /*
   722 	BranchObj *bo=getSelectedBranch();
   723 	if (bo)
   724 	{
   725 
   726 		Q3FileDialog *fd=new Q3FileDialog( NULL);
   727 		fd->setMode (Q3FileDialog::ExistingFiles);
   728 		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
   729 		ImagePreview *p =new ImagePreview (fd);
   730 		fd->setContentsPreviewEnabled( TRUE );
   731 		fd->setContentsPreview( p, p );
   732 		fd->setPreviewMode( Q3FileDialog::Contents );
   733 		fd->setCaption(vymName+" - " +tr("Load image"));
   734 		fd->setDir (lastImageDir);
   735 		fd->show();
   736 
   737 		if ( fd->exec() == QDialog::Accepted )
   738 		{
   739 			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
   740 			lastImageDir=QDir (fd->dirPath());
   741 			QString s;
   742 			FloatImageObj *fio;
   743 			for (int j=0; j<fd->selectedFiles().count(); j++)
   744 			{
   745 				s=fd->selectedFiles().at(j);
   746 				fio=loadFloatImageInt (s);
   747 				if (fio)
   748 					saveState(
   749 						(LinkableMapObj*)fio,
   750 						"delete ()",
   751 						bo, 
   752 						QString ("loadImage (%1)").arg(s ),
   753 						QString("Add image %1 to %2").arg(s).arg(getObjectName(bo))
   754 					);
   755 				else
   756 					// TODO loadFIO error handling
   757 					qWarning ("Failed to load "+s);
   758 			}
   759 		}
   760 		delete (p);
   761 		delete (fd);
   762 	}
   763 */	
   764 }
   765 
   766 void VymModel::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
   767 {
   768 	fio->save (fn,type);
   769 }
   770 
   771 void VymModel::saveFloatImage ()
   772 {
   773 	FloatImageObj *fio=selection.getFloatImage();
   774 	if (fio)
   775 	{
   776 		QFileDialog *fd=new QFileDialog( NULL);
   777 		fd->setFilters (imageIO.getFilters());
   778 		fd->setCaption(vymName+" - " +tr("Save image"));
   779 		fd->setFileMode( QFileDialog::AnyFile );
   780 		fd->setDirectory (lastImageDir);
   781 //		fd->setSelection (fio->getOriginalFilename());
   782 		fd->show();
   783 
   784 		QString fn;
   785 		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
   786 		{
   787 			fn=fd->selectedFiles().at(0);
   788 			if (QFile (fn).exists() )
   789 			{
   790 				QMessageBox mb( vymName,
   791 					tr("The file %1 exists already.\n"
   792 					"Do you want to overwrite it?").arg(fn),
   793 				QMessageBox::Warning,
   794 				QMessageBox::Yes | QMessageBox::Default,
   795 				QMessageBox::Cancel | QMessageBox::Escape,
   796 				QMessageBox::NoButton );
   797 
   798 				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
   799 				mb.setButtonText( QMessageBox::No, tr("Cancel"));
   800 				switch( mb.exec() ) 
   801 				{
   802 					case QMessageBox::Yes:
   803 						// save 
   804 						break;
   805 					case QMessageBox::Cancel:
   806 						// do nothing
   807 						delete (fd);
   808 						return;
   809 						break;
   810 				}
   811 			}
   812 			saveFloatImageInt (fio,fd->selectedFilter(),fn );
   813 		}
   814 		delete (fd);
   815 	}
   816 }
   817 
   818 
   819 void VymModel::importDirInt(BranchObj *dst, QDir d)
   820 {
   821 /* FIXME-2 importDirInt not ported yet
   822 	BranchObj *bo=getSelectedBranch();
   823 	if (bo)
   824 	{
   825 		// Traverse directories
   826 		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
   827 		QFileInfoList list = d.entryInfoList();
   828 		QFileInfo fi;
   829 
   830 		for (int i = 0; i < list.size(); ++i) 
   831 		{
   832 			fi=list.at(i);
   833 			if (fi.fileName() != "." && fi.fileName() != ".." )
   834 			{
   835 				dst->addBranch();
   836 				bo=dst->getLastBranch();
   837 				BranchItem *bi=(BranchItem*)(bo->getTreeItem());
   838 				bi->setHeading (fi.fileName() );	// FIXME-3 check this
   839 				bo->setColor (QColor("blue"));
   840 				bi->toggleScroll();
   841 				if ( !d.cd(fi.fileName()) ) 
   842 					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
   843 				else 
   844 				{
   845 					// Recursively add subdirs
   846 					importDirInt (bo,d);
   847 					d.cdUp();
   848 				}
   849 			}	
   850 		}		
   851 		// Traverse files
   852 		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
   853 		list = d.entryInfoList();
   854 
   855 		for (int i = 0; i < list.size(); ++i) 
   856 		{
   857 			fi=list.at(i);
   858 			dst->addBranch();
   859 			bo=dst->getLastBranch();
   860 			bo->setHeading (fi.fileName() );
   861 			bo->setColor (QColor("black"));
   862 			if (fi.fileName().right(4) == ".vym" )
   863 				bo->setVymLink (fi.filePath());
   864 		}	
   865 	}		
   866 */
   867 }
   868 
   869 void VymModel::importDirInt (const QString &s)	// FIXME-2
   870 {
   871 /*
   872 	BranchObj *bo=getSelectedBranch();
   873 	if (bo)
   874 	{
   875 		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
   876 
   877 		QDir d(s);
   878 		importDirInt (bo,d);
   879 	}
   880 */	
   881 }	
   882 
   883 void VymModel::importDir()	//FIXME-2
   884 {
   885 /*
   886 	BranchObj *bo=getSelectedBranch();
   887 	if (bo)
   888 	{
   889 		QStringList filters;
   890 		filters <<"VYM map (*.vym)";
   891 		QFileDialog *fd=new QFileDialog( NULL,vymName+ " - " +tr("Choose directory structure to import"));
   892 		fd->setMode (QFileDialog::DirectoryOnly);
   893 		fd->setFilters (filters);
   894 		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
   895 		fd->show();
   896 
   897 		QString fn;
   898 		if ( fd->exec() == QDialog::Accepted )
   899 		{
   900 			importDirInt (fd->selectedFile() );
   901 			reposition();
   902 			//FIXME-3 VM needed? scene()->update();
   903 		}
   904 	}	
   905 */
   906 }
   907 
   908 
   909 void VymModel::autosave()
   910 {
   911 	if (filePath=="") 
   912 	{
   913 		if (debug)
   914 			cout << "VymModel::autosave rejected due to missing filePath\n";
   915 	}
   916 
   917 	QDateTime now=QDateTime().currentDateTime();
   918 
   919 	// Disable autosave, while we have gone back in history
   920 	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
   921 	if (redosAvail>0) return;
   922 
   923 	// Also disable autosave for new map without filename
   924 	if (filePath.isEmpty()) return;
   925 
   926 
   927 	if (mapUnsaved &&mapChanged && settings.value ("/mainwindow/autosave/use",true).toBool() )
   928 	{
   929 		if (QFileInfo(filePath).lastModified()<=fileChangedTime) 
   930 			mainWindow->fileSave (this);
   931 		else
   932 			if (debug)
   933 				cout <<"  ME::autosave  rejected, file on disk is newer than last save.\n"; 
   934 
   935 	}	
   936 }
   937 
   938 void VymModel::fileChanged()
   939 {
   940 	// Check if file on disk has changed meanwhile
   941 	if (!filePath.isEmpty())
   942 	{
   943 		QDateTime tmod=QFileInfo (filePath).lastModified();
   944 		if (tmod>fileChangedTime)
   945 		{
   946 			// FIXME-2 VM switch to current mapeditor and finish lineedits...
   947 			QMessageBox mb( vymName,
   948 				tr("The file of the map  on disk has changed:\n\n"  
   949 				   "   %1\n\nDo you want to reload that map with the new file?").arg(filePath),
   950 				QMessageBox::Question,
   951 				QMessageBox::Yes ,
   952 				QMessageBox::Cancel | QMessageBox::Default,
   953 				QMessageBox::NoButton );
   954 
   955 			mb.setButtonText( QMessageBox::Yes, tr("Reload"));
   956 			mb.setButtonText( QMessageBox::No, tr("Ignore"));
   957 			switch( mb.exec() ) 
   958 			{
   959 				case QMessageBox::Yes:
   960 					// Reload map
   961 					load (filePath,NewMap,fileType);
   962 		        case QMessageBox::Cancel:
   963 					fileChangedTime=tmod; // allow autosave to overwrite newer file!
   964 			}
   965 		}
   966 	}	
   967 }
   968 
   969 bool VymModel::isDefault()
   970 {
   971     return mapDefault;
   972 }
   973 
   974 void VymModel::makeDefault()
   975 {
   976 	mapChanged=false;
   977 	mapDefault=true;
   978 }
   979 
   980 bool VymModel::hasChanged()
   981 {
   982     return mapChanged;
   983 }
   984 
   985 void VymModel::setChanged()
   986 {
   987 	cout << "VM::setChanged()\n";
   988 	if (!mapChanged)
   989 	{
   990 		autosaveTimer->start(settings.value("/mainwindow/autosave/ms/",300000).toInt());
   991 		cout <<"   timer started with "<<settings.value("/mainwindow/autosave/ms/",300000).toInt()<<endl;
   992 	}	
   993 	mapChanged=true;
   994 	mapDefault=false;
   995 	mapUnsaved=true;
   996 	findReset();
   997 }
   998 
   999 QString VymModel::getObjectName (const LinkableMapObj *lmo)	// FIXME-3 should be obsolete
  1000 {
  1001 	QString s;
  1002 	if (!lmo) return QString("Error: NULL has no name!");
  1003 
  1004 	if ((typeid(*lmo) == typeid(BranchObj) ||
  1005 				      typeid(*lmo) == typeid(MapCenterObj))) 
  1006 	{
  1007 		
  1008 		s=lmo->getTreeItem()->getHeading();
  1009 		if (s=="") s="unnamed";
  1010 		return QString("branch (%1)").arg(s);
  1011 	}	
  1012 	if ((typeid(*lmo) == typeid(FloatImageObj) ))
  1013 		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
  1014 	return QString("Unknown type has no name!");
  1015 }
  1016 
  1017 
  1018 QString VymModel::getObjectName (const TreeItem *ti)
  1019 {
  1020 	QString s;
  1021 	if (!ti) return QString("Error: NULL has no name!");
  1022 
  1023 	if (ti->isBranchLikeType() )
  1024 	{
  1025 		s=ti->getHeading();
  1026 		if (s=="") s="unnamed";
  1027 		return QString("branch (%1)").arg(s);
  1028 	}	
  1029 	/* FIXME-2 move floatimage to TreeModel first
  1030 	if (type==TreeItem::Image)
  1031 		return QString ("floatimage [%1]").arg(((FloatImageObj*)lmo)->getOriginalFilename());
  1032 	*/	
  1033 	return QString("Unknown type has no name!");
  1034 }
  1035 
  1036 void VymModel::redo()
  1037 {
  1038 	// Can we undo at all?
  1039 	if (redosAvail<1) return;
  1040 
  1041 	bool blockSaveStateOrg=blockSaveState;
  1042 	blockSaveState=true;
  1043 	
  1044 	redosAvail--;
  1045 
  1046 	if (undosAvail<stepsTotal) undosAvail++;
  1047 	curStep++;
  1048 	if (curStep>stepsTotal) curStep=1;
  1049 	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
  1050 	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
  1051 	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
  1052 	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
  1053 	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
  1054 	QString version=undoSet.readEntry ("/history/version");
  1055 
  1056 	/* TODO Maybe check for version, if we save the history
  1057 	if (!checkVersion(version))
  1058 		QMessageBox::warning(0,tr("Warning"),
  1059 			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
  1060 	*/ 
  1061 
  1062 	// Find out current undo directory
  1063 	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
  1064 
  1065 	if (debug)
  1066 	{
  1067 		cout << "VymModel::redo() begin\n";
  1068 		cout << "    undosAvail="<<undosAvail<<endl;
  1069 		cout << "    redosAvail="<<redosAvail<<endl;
  1070 		cout << "       curStep="<<curStep<<endl;
  1071 		cout << "    ---------------------------"<<endl;
  1072 		cout << "    comment="<<comment.toStdString()<<endl;
  1073 		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
  1074 		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
  1075 		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
  1076 		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
  1077 		cout << "    ---------------------------"<<endl<<endl;
  1078 	}
  1079 
  1080 	// select  object before redo
  1081 	if (!redoSelection.isEmpty())
  1082 		select (redoSelection);
  1083 
  1084 
  1085 	parseAtom (redoCommand);
  1086 	reposition();
  1087 
  1088 	blockSaveState=blockSaveStateOrg;
  1089 
  1090 	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
  1091 	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
  1092 	undoSet.setEntry ("/history/curStep",QString::number(curStep));
  1093 	undoSet.writeSettings(histPath);
  1094 
  1095 	mainWindow->updateHistory (undoSet);
  1096 	updateActions();
  1097 
  1098 	/* TODO remove testing
  1099 	cout << "ME::redo() end\n";
  1100 	cout << "    undosAvail="<<undosAvail<<endl;
  1101 	cout << "    redosAvail="<<redosAvail<<endl;
  1102 	cout << "       curStep="<<curStep<<endl;
  1103 	cout << "    ---------------------------"<<endl<<endl;
  1104 	*/
  1105 
  1106 
  1107 }
  1108 
  1109 bool VymModel::isRedoAvailable()
  1110 {
  1111 	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
  1112 		return true;
  1113 	else	
  1114 		return false;
  1115 }
  1116 
  1117 void VymModel::undo()
  1118 {
  1119 	// Can we undo at all?
  1120 	if (undosAvail<1) return;
  1121 
  1122 	mainWindow->statusMessage (tr("Autosave disabled during undo."));
  1123 
  1124 	bool blockSaveStateOrg=blockSaveState;
  1125 	blockSaveState=true;
  1126 	
  1127 	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
  1128 	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
  1129 	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
  1130 	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
  1131 	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
  1132 	QString version=undoSet.readEntry ("/history/version");
  1133 
  1134 	/* TODO Maybe check for version, if we save the history
  1135 	if (!checkVersion(version))
  1136 		QMessageBox::warning(0,tr("Warning"),
  1137 			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
  1138 	*/
  1139 
  1140 	// Find out current undo directory
  1141 	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
  1142 
  1143 	// select  object before undo
  1144 	if (!undoSelection.isEmpty())
  1145 		select (undoSelection);
  1146 
  1147 	if (debug)
  1148 	{
  1149 		cout << "VymModel::undo() begin\n";
  1150 		cout << "    undosAvail="<<undosAvail<<endl;
  1151 		cout << "    redosAvail="<<redosAvail<<endl;
  1152 		cout << "       curStep="<<curStep<<endl;
  1153 		cout << "    ---------------------------"<<endl;
  1154 		cout << "    comment="<<comment.toStdString()<<endl;
  1155 		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
  1156 		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
  1157 		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
  1158 		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
  1159 		cout << "    ---------------------------"<<endl<<endl;
  1160 	}	
  1161 	parseAtom (undoCommand);
  1162 	reposition();
  1163 
  1164 	undosAvail--;
  1165 	curStep--; 
  1166 	if (curStep<1) curStep=stepsTotal;
  1167 
  1168 	redosAvail++;
  1169 
  1170 	blockSaveState=blockSaveStateOrg;
  1171 /* TODO remove testing
  1172 	cout << "VymModel::undo() end\n";
  1173 	cout << "    undosAvail="<<undosAvail<<endl;
  1174 	cout << "    redosAvail="<<redosAvail<<endl;
  1175 	cout << "       curStep="<<curStep<<endl;
  1176 	cout << "    ---------------------------"<<endl<<endl;
  1177 */
  1178 
  1179 	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
  1180 	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
  1181 	undoSet.setEntry ("/history/curStep",QString::number(curStep));
  1182 	undoSet.writeSettings(histPath);
  1183 
  1184 	mainWindow->updateHistory (undoSet);
  1185 	updateActions();
  1186 	selection.update();
  1187 	ensureSelectionVisible();
  1188 }
  1189 
  1190 bool VymModel::isUndoAvailable()
  1191 {
  1192 	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
  1193 		return true;
  1194 	else	
  1195 		return false;
  1196 }
  1197 
  1198 void VymModel::gotoHistoryStep (int i)
  1199 {
  1200 	// Restore variables
  1201 	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
  1202 	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
  1203 
  1204 	if (i<0) i=undosAvail+redosAvail;
  1205 
  1206 	// Clicking above current step makes us undo things
  1207 	if (i<undosAvail) 
  1208 	{	
  1209 		for (int j=0; j<undosAvail-i; j++) undo();
  1210 		return;
  1211 	}	
  1212 	// Clicking below current step makes us redo things
  1213 	if (i>undosAvail) 
  1214 		for (int j=undosAvail; j<i; j++) 
  1215 		{
  1216 			if (debug) cout << "VymModel::gotoHistoryStep redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
  1217 			redo();
  1218 		}
  1219 
  1220 	// And ignore clicking the current row ;-)	
  1221 }
  1222 
  1223 
  1224 QString VymModel::getHistoryPath()
  1225 {
  1226 	QString histName(QString("history-%1").arg(curStep));
  1227 	return (tmpMapDir+"/"+histName);
  1228 }
  1229 
  1230 void VymModel::saveState(const SaveMode &savemode, const QString &undoSelection, const QString &undoCom, const QString &redoSelection, const QString &redoCom, const QString &comment, TreeItem *saveSel)
  1231 {
  1232 	sendData(redoCom);	//FIXME-3 testing
  1233 
  1234 	// Main saveState
  1235 
  1236 
  1237 	if (blockSaveState) return;
  1238 
  1239 	if (debug) cout << "ME::saveState() for  "<<qPrintable (mapName)<<endl;
  1240 	
  1241 	// Find out current undo directory
  1242 	if (undosAvail<stepsTotal) undosAvail++;
  1243 	curStep++;
  1244 	if (curStep>stepsTotal) curStep=1;
  1245 	
  1246 	QString backupXML="";
  1247 	QString histDir=getHistoryPath();
  1248 	QString bakMapPath=histDir+"/map.xml";
  1249 
  1250 	// Create histDir if not available
  1251 	QDir d(histDir);
  1252 	if (!d.exists()) 
  1253 		makeSubDirs (histDir);
  1254 
  1255 	// Save depending on how much needs to be saved	
  1256 	if (saveSel)
  1257 		backupXML=saveToDir (histDir,mapName+"-",false, QPointF (),saveSel);
  1258 		
  1259 	QString undoCommand="";
  1260 	if (savemode==UndoCommand)
  1261 	{
  1262 		undoCommand=undoCom;
  1263 	}	
  1264 	else if (savemode==PartOfMap )
  1265 	{
  1266 		undoCommand=undoCom;
  1267 		undoCommand.replace ("PATH",bakMapPath);
  1268 	}
  1269 
  1270 	if (!backupXML.isEmpty())
  1271 		// Write XML Data to disk
  1272 		saveStringToDisk (bakMapPath,backupXML);
  1273 
  1274 	// We would have to save all actions in a tree, to keep track of 
  1275 	// possible redos after a action. Possible, but we are too lazy: forget about redos.
  1276 	redosAvail=0;
  1277 
  1278 	// Write the current state to disk
  1279 	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
  1280 	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
  1281 	undoSet.setEntry ("/history/curStep",QString::number(curStep));
  1282 	undoSet.setEntry (QString("/history/step-%1/undoCommand").arg(curStep),undoCommand);
  1283 	undoSet.setEntry (QString("/history/step-%1/undoSelection").arg(curStep),undoSelection);
  1284 	undoSet.setEntry (QString("/history/step-%1/redoCommand").arg(curStep),redoCom);
  1285 	undoSet.setEntry (QString("/history/step-%1/redoSelection").arg(curStep),redoSelection);
  1286 	undoSet.setEntry (QString("/history/step-%1/comment").arg(curStep),comment);
  1287 	undoSet.setEntry (QString("/history/version"),vymVersion);
  1288 	undoSet.writeSettings(histPath);
  1289 
  1290 	if (debug)
  1291 	{
  1292 		// TODO remove after testing
  1293 		//cout << "          into="<< histPath.toStdString()<<endl;
  1294 		cout << "    stepsTotal="<<stepsTotal<<
  1295 		", undosAvail="<<undosAvail<<
  1296 		", redosAvail="<<redosAvail<<
  1297 		", curStep="<<curStep<<endl;
  1298 		cout << "    ---------------------------"<<endl;
  1299 		cout << "    comment="<<comment.toStdString()<<endl;
  1300 		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
  1301 		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
  1302 		cout << "    redoCom="<<redoCom.toStdString()<<endl;
  1303 		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
  1304 		if (saveSel) cout << "    saveSel="<<qPrintable (getSelectString(saveSel))<<endl;
  1305 		cout << "    ---------------------------"<<endl;
  1306 	}
  1307 
  1308 	mainWindow->updateHistory (undoSet);
  1309 	setChanged();
  1310 	updateActions();
  1311 }
  1312 
  1313 
  1314 void VymModel::saveStateChangingPart(TreeItem *undoSel, TreeItem* redoSel, const QString &rc, const QString &comment)
  1315 {
  1316 	// save the selected part of the map, Undo will replace part of map 
  1317 	QString undoSelection="";
  1318 	if (undoSel)
  1319 		undoSelection=getSelectString(undoSel);
  1320 	else
  1321 		qWarning ("VymModel::saveStateChangingPart  no undoSel given!");
  1322 	QString redoSelection="";
  1323 	if (redoSel)
  1324 		redoSelection=getSelectString(undoSel);
  1325 	else
  1326 		qWarning ("VymModel::saveStateChangingPart  no redoSel given!");
  1327 		
  1328 
  1329 	saveState (PartOfMap,
  1330 		undoSelection, "addMapReplace (\"PATH\")",
  1331 		redoSelection, rc, 
  1332 		comment, 
  1333 		undoSel);
  1334 }
  1335 
  1336 void VymModel::saveStateRemovingPart(TreeItem*redoSel, const QString &comment)
  1337 {
  1338 	if (!redoSel)
  1339 	{
  1340 		qWarning ("VymModel::saveStateRemovingPart  no redoSel given!");
  1341 		return;
  1342 	}
  1343 	QString undoSelection=getSelectString (redoSel->parent() );
  1344 	QString redoSelection=getSelectString(redoSel);
  1345 	if (redoSel->getType()==TreeItem::Branch) 
  1346 	{
  1347 		// save the selected branch of the map, Undo will insert part of map 
  1348 		saveState (PartOfMap,
  1349 			undoSelection, QString("addMapInsert (\"PATH\",%1)").arg(redoSel->num()),
  1350 			redoSelection, "delete ()", 
  1351 			comment, 
  1352 			redoSel);
  1353 	}
  1354 }
  1355 
  1356 
  1357 void VymModel::saveState(TreeItem *undoSel, const QString &uc, TreeItem *redoSel, const QString &rc, const QString &comment) 
  1358 {
  1359 	// "Normal" savestate: save commands, selections and comment
  1360 	// so just save commands for undo and redo
  1361 	// and use current selection
  1362 
  1363 	QString redoSelection="";
  1364 	if (redoSel) redoSelection=getSelectString(redoSel);
  1365 	QString undoSelection="";
  1366 	if (undoSel) undoSelection=getSelectString(undoSel);
  1367 
  1368 	saveState (UndoCommand,
  1369 		undoSelection, uc,
  1370 		redoSelection, rc, 
  1371 		comment, 
  1372 		NULL);
  1373 }
  1374 
  1375 void VymModel::saveState(const QString &undoSel, const QString &uc, const QString &redoSel, const QString &rc, const QString &comment) 
  1376 {
  1377 	// "Normal" savestate: save commands, selections and comment
  1378 	// so just save commands for undo and redo
  1379 	// and use current selection
  1380 	saveState (UndoCommand,
  1381 		undoSel, uc,
  1382 		redoSel, rc, 
  1383 		comment, 
  1384 		NULL);
  1385 }
  1386 
  1387 void VymModel::saveState(const QString &uc, const QString &rc, const QString &comment) 
  1388 {
  1389 	// "Normal" savestate applied to model (no selection needed): 
  1390 	// save commands  and comment
  1391 	saveState (UndoCommand,
  1392 		NULL, uc,
  1393 		NULL, rc, 
  1394 		comment, 
  1395 		NULL);
  1396 }
  1397 
  1398 
  1399 QGraphicsScene* VymModel::getScene ()
  1400 {
  1401 	return mapScene;
  1402 }
  1403 
  1404 TreeItem* VymModel::findBySelectString(const QString &s)
  1405 {
  1406 	if (!s.isEmpty() )
  1407 	{
  1408 		QString part;
  1409 		QString typ;
  1410 		QString num;
  1411 		part=s.section(",",0,0);
  1412 		typ=part.left (2);
  1413 		num=part.right(part.length() - 3);
  1414 		if (typ=="mc" && num.toInt()>=0 && num.toInt() <rootItem->branchCount() )
  1415 			return rootItem->getBranchNum (num.toInt() );
  1416 	}		
  1417 
  1418 	TreeItem *ti;
  1419 	for (int i=0; i<rootItem->branchCount(); i++)
  1420 	{
  1421 		ti=rootItem->getBranchNum(i)->findBySelectString(s);
  1422 		if (ti) return ti;
  1423 	}	
  1424 	return NULL;
  1425 }
  1426 
  1427 TreeItem* VymModel::findID (const QString &s)
  1428 {
  1429 	TreeItem *ti;
  1430 	for (int i=0; i<rootItem->branchCount(); i++)
  1431 	{
  1432 		ti=rootItem->getBranchNum(i)->findID (s);
  1433 		if (ti) return ti;
  1434 	}	
  1435 	return NULL;
  1436 }
  1437 
  1438 //////////////////////////////////////////////
  1439 // Interface 
  1440 //////////////////////////////////////////////
  1441 void VymModel::setVersion (const QString &s)
  1442 {
  1443 	version=s;
  1444 }
  1445 
  1446 void VymModel::setAuthor (const QString &s)
  1447 {
  1448 	saveState (
  1449 		QString ("setMapAuthor (\"%1\")").arg(author),
  1450 		QString ("setMapAuthor (\"%1\")").arg(s),
  1451 		QString ("Set author of map to \"%1\"").arg(s)
  1452 	);
  1453 	author=s;
  1454 }
  1455 
  1456 QString VymModel::getAuthor()
  1457 {
  1458 	return author;
  1459 }
  1460 
  1461 void VymModel::setComment (const QString &s)
  1462 {
  1463 	saveState (
  1464 		QString ("setMapComment (\"%1\")").arg(comment),
  1465 		QString ("setMapComment (\"%1\")").arg(s),
  1466 		QString ("Set comment of map")
  1467 	);
  1468 	comment=s;
  1469 }
  1470 
  1471 QString VymModel::getComment ()
  1472 {
  1473 	return comment;
  1474 }
  1475 
  1476 QString VymModel::getDate ()
  1477 {
  1478 	return QDate::currentDate().toString ("yyyy-MM-dd");
  1479 }
  1480 
  1481 int VymModel::branchCount()	// FIXME-2 Optimize this: use internal counter instead of going through whole map each time...
  1482 {
  1483 	int c=0;
  1484 	BranchItem *cur=NULL;
  1485 	BranchItem *prev=NULL;
  1486 	int d;
  1487 	next(cur,prev,d);
  1488 	while (cur) 
  1489 	{
  1490 		c++;
  1491 		next(cur,prev,d);
  1492 	}
  1493 	return c;
  1494 }
  1495 
  1496 void VymModel::setHeading(const QString &s)
  1497 {
  1498 	BranchItem *selbi=getSelectedBranchItem();
  1499 	if (selbi)
  1500 	{
  1501 		saveState(
  1502 			selbi,
  1503 			"setHeading (\""+selbi->getHeading()+"\")", 
  1504 			selbi,
  1505 			"setHeading (\""+s+"\")", 
  1506 			QString("Set heading of %1 to \"%2\"").arg(getObjectName(selbi)).arg(s) );
  1507 		selbi->setHeading(s );
  1508 		QModelIndex ix2=index (selbi);
  1509 		emit (dataChanged ( ix2,ix2));
  1510 		/* FIXME-3 testing only
  1511 		*/
  1512 
  1513 		reposition();
  1514 //		selection.update();	//FIXME-4
  1515 		ensureSelectionVisible();
  1516 	}
  1517 }
  1518 
  1519 BranchItem* VymModel::findText (QString s, bool cs)   
  1520 {
  1521 	int d=0;
  1522 	QTextDocument::FindFlags flags=0;
  1523 	if (cs) flags=QTextDocument::FindCaseSensitively;
  1524 
  1525 	if (!findCurrent) 
  1526 	{	// Nothing found or new find process
  1527 		if (EOFind)
  1528 			// nothing found, start again
  1529 			EOFind=false;
  1530 		findCurrent=NULL;	
  1531 		findPrevious=NULL;	
  1532 		next (findCurrent,findPrevious,d);
  1533 	}	
  1534 	bool searching=true;
  1535 	bool foundNote=false;
  1536 	while (searching && !EOFind)
  1537 	{
  1538 		if (findCurrent)
  1539 		{
  1540 			// Searching in Note
  1541 			if (findCurrent->getNote().contains(s,cs))
  1542 			{
  1543 				select (findCurrent);
  1544 				/*
  1545 				if (getSelectedBranch()!=itFind) 
  1546 				{
  1547 					select(itFind);
  1548 					ensureSelectionVisible();
  1549 				}
  1550 				*/
  1551 				if (textEditor->findText(s,flags)) 
  1552 				{
  1553 					searching=false;
  1554 					foundNote=true;
  1555 				}	
  1556 			}
  1557 			// Searching in Heading
  1558 			if (searching && findCurrent->getHeading().contains (s,cs) ) 
  1559 			{
  1560 				select(findCurrent);
  1561 				searching=false;
  1562 			}
  1563 		}	
  1564 		if (!foundNote)
  1565 		{
  1566 			if (!next(findCurrent,findPrevious,d) )
  1567 				EOFind=true;
  1568 		}
  1569 	//cout <<"still searching...  "<<qPrintable( itFind->getHeading())<<endl;
  1570 	}	
  1571 	if (!searching)
  1572 		return getSelectedBranchItem();
  1573 	else
  1574 		return NULL;
  1575 }
  1576 
  1577 void VymModel::findReset()
  1578 {	// Necessary if text to find changes during a find process
  1579 	findCurrent=NULL;
  1580 	findPrevious=NULL;
  1581 	EOFind=false;
  1582 }
  1583 
  1584 void VymModel::setScene (QGraphicsScene *s)
  1585 {
  1586 	mapScene=s;	// FIXME-2 VM should not be necessary anymore, move all occurences to MapEditor
  1587     //init();	// Here we have a mapScene set, 
  1588 			// which is (still) needed to create MapCenters
  1589 }
  1590 
  1591 void VymModel::setURL(const QString &url) //FIXME-2
  1592 {
  1593 /*
  1594 	BranchObj *bo=getSelectedBranch();
  1595 	if (bo)
  1596 	{
  1597 		QString oldurl=bo->getURL();
  1598 		bo->setURL (url);
  1599 		saveState (
  1600 			bo,
  1601 			QString ("setURL (\"%1\")").arg(oldurl),
  1602 			bo,
  1603 			QString ("setURL (\"%1\")").arg(url),
  1604 			QString ("set URL of %1 to %2").arg(getObjectName(bo)).arg(url)
  1605 		);
  1606 		updateActions();
  1607 		reposition();
  1608 		selection.update();
  1609 		ensureSelectionVisible();
  1610 	}
  1611 */
  1612 }	
  1613 
  1614 QString VymModel::getURL()	//FIXME-2
  1615 {
  1616 /*
  1617 	BranchObj *bo=getSelectedBranch();
  1618 	if (bo)
  1619 		return bo->getURL();
  1620 	else
  1621 		return "";
  1622 */
  1623 }
  1624 
  1625 QStringList VymModel::getURLs()	// FIXME-1	first, next moved to vymmodel
  1626 {
  1627 	return QStringList();
  1628 	/*
  1629 	QStringList urls;
  1630 	BranchObj *bo=getSelectedBranch();
  1631 	if (bo)
  1632 	{		
  1633 		bo=bo->first();	
  1634 		while (bo) 
  1635 		{
  1636 			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
  1637 			bo=bo->next();
  1638 		}	
  1639 	}	
  1640 	return urls;
  1641 	*/
  1642 }
  1643 
  1644 void VymModel::linkFloatImageTo(const QString &dstString)	// FIXME-2
  1645 {
  1646 	FloatImageObj *fio=selection.getFloatImage();
  1647 	if (fio)
  1648 	{
  1649 		TreeItem *dst=findBySelectString (dstString);
  1650 		if (dst && dst->isBranchLikeType() )
  1651 		{			
  1652 			TreeItem *dstPar=dst->parent();
  1653 			QString parString=getSelectString(dstPar);
  1654 			QString fioPreSelectString=getSelectString(fio);
  1655 			QString fioPreParentSelectString=getSelectString (fio->getParObj());
  1656 			// FIXME-2 ((BranchObj*)dst)->addFloatImage (fio);
  1657 			unselect();
  1658 			// ((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
  1659 			fio=((BranchObj*)dst)->getLastFloatImage();
  1660 			fio->setRelPos();
  1661 			fio->reposition();
  1662 			// select (fio);
  1663 			saveState(
  1664 				getSelectString(fio),
  1665 				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
  1666 				fioPreSelectString, 
  1667 				QString ("linkTo (\"%1\")").arg(dstString),
  1668 				QString ("Link floatimage to %1").arg(getObjectName(dst)));
  1669 		}
  1670 	}
  1671 }
  1672 
  1673 
  1674 void VymModel::setFrameType(const FrameObj::FrameType &t)	//FIXME-2
  1675 {
  1676 /*
  1677 	BranchObj *bo=getSelectedBranch();
  1678 	if (bo)
  1679 	{
  1680 		QString s=bo->getFrameTypeName();
  1681 		bo->setFrameType (t);
  1682 		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
  1683 			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
  1684 		reposition();
  1685 		bo->updateLink();
  1686 	}
  1687 */	
  1688 }
  1689 
  1690 void VymModel::setFrameType(const QString &s)	//FIXME-2
  1691 {
  1692 /*
  1693 	BranchObj *bo=getSelectedBranch();
  1694 	if (bo)
  1695 	{
  1696 		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
  1697 			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
  1698 		bo->setFrameType (s);
  1699 		reposition();
  1700 		bo->updateLink();
  1701 	}
  1702 */
  1703 }
  1704 
  1705 void VymModel::setFramePenColor(const QColor &c)	//FIXME-2
  1706 {
  1707 /*
  1708 	BranchObj *bo=getSelectedBranch();
  1709 	if (bo)
  1710 	{
  1711 		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
  1712 			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
  1713 		bo->setFramePenColor (c);
  1714 	}	
  1715 */	
  1716 }
  1717 
  1718 void VymModel::setFrameBrushColor(const QColor &c)	//FIXME-2
  1719 {
  1720 /*
  1721 	BranchObj *bo=getSelectedBranch();
  1722 	if (bo)
  1723 	{
  1724 		saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
  1725 			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
  1726 		bo->setFrameBrushColor (c);
  1727 	}	
  1728 */}
  1729 
  1730 void VymModel::setFramePadding (const int &i) //FIXME-2
  1731 {
  1732 /*
  1733 	BranchObj *bo=getSelectedBranch();
  1734 	if (bo)
  1735 	{
  1736 		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
  1737 			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
  1738 		bo->setFramePadding (i);
  1739 		reposition();
  1740 		bo->updateLink();
  1741 	}	
  1742 	*/
  1743 }
  1744 
  1745 void VymModel::setFrameBorderWidth(const int &i) //FIXME-2
  1746 {
  1747 /*
  1748 	BranchObj *bo=getSelectedBranch();
  1749 	if (bo)
  1750 	{
  1751 		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
  1752 			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
  1753 		bo->setFrameBorderWidth (i);
  1754 		reposition();
  1755 		bo->updateLink();
  1756 	}	
  1757 */	
  1758 }
  1759 
  1760 void VymModel::setIncludeImagesVer(bool b)	//FIXME-2
  1761 {
  1762 /*
  1763 	BranchObj *bo=getSelectedBranch();
  1764 	if (bo)
  1765 	{
  1766 		QString u= b ? "false" : "true";
  1767 		QString r=!b ? "false" : "true";
  1768 		
  1769 		saveState(
  1770 			bo,
  1771 			QString("setIncludeImagesVertically (%1)").arg(u),
  1772 			bo, 
  1773 			QString("setIncludeImagesVertically (%1)").arg(r),
  1774 			QString("Include images vertically in %1").arg(getObjectName(bo))
  1775 		);	
  1776 		bo->setIncludeImagesVer(b);
  1777 		reposition();
  1778 	}	
  1779 */}
  1780 
  1781 void VymModel::setIncludeImagesHor(bool b)	//FIXME-2
  1782 {
  1783 /*
  1784 	BranchObj *bo=getSelectedBranch();
  1785 	if (bo)
  1786 	{
  1787 		QString u= b ? "false" : "true";
  1788 		QString r=!b ? "false" : "true";
  1789 		
  1790 		saveState(
  1791 			bo,
  1792 			QString("setIncludeImagesHorizontally (%1)").arg(u),
  1793 			bo, 
  1794 			QString("setIncludeImagesHorizontally (%1)").arg(r),
  1795 			QString("Include images horizontally in %1").arg(getObjectName(bo))
  1796 		);	
  1797 		bo->setIncludeImagesHor(b);
  1798 		reposition();
  1799 	}	
  1800 	*/
  1801 }
  1802 
  1803 void VymModel::setHideLinkUnselected (bool b)//FIXME-2
  1804 {
  1805 /*
  1806 	LinkableMapObj *sel=getSelectedLMO();
  1807 	if (sel &&
  1808 		(selectionType() == TreeItem::Branch || 
  1809 		selectionType() == TreeItem::MapCenter  ||
  1810 		selectionType() == TreeItem::Image ))
  1811 	{
  1812 		QString u= b ? "false" : "true";
  1813 		QString r=!b ? "false" : "true";
  1814 		
  1815 		saveState(
  1816 			sel,
  1817 			QString("setHideLinkUnselected (%1)").arg(u),
  1818 			sel, 
  1819 			QString("setHideLinkUnselected (%1)").arg(r),
  1820 			QString("Hide link of %1 if unselected").arg(getObjectName(sel))
  1821 		);	
  1822 		sel->setHideLinkUnselected(b);
  1823 	}
  1824 */
  1825 }
  1826 
  1827 void VymModel::setHideExport(bool b)
  1828 {
  1829 	BranchItem *bi=getSelectedBranchItem();
  1830 	if (bi)
  1831 	{
  1832 		bi->setHideInExport (b);
  1833 		QString u= b ? "false" : "true";
  1834 		QString r=!b ? "false" : "true";
  1835 		
  1836 		saveState(
  1837 			bi,
  1838 			QString ("setHideExport (%1)").arg(u),
  1839 			bi,
  1840 			QString ("setHideExport (%1)").arg(r),
  1841 			QString ("Set HideExport flag of %1 to %2").arg(getObjectName(bi)).arg (r)
  1842 		);	
  1843 		updateActions();
  1844 		reposition();
  1845 		// selection.update();
  1846 		// FIXME-3 VM needed? scene()->update();
  1847 	}
  1848 }
  1849 
  1850 void VymModel::toggleHideExport()
  1851 {
  1852 	BranchItem *selbi=getSelectedBranchItem();
  1853 	if (selbi)
  1854 		setHideExport ( !selbi->hideInExport() );
  1855 }
  1856 
  1857 
  1858 void VymModel::copy()	//FIXME-2
  1859 {
  1860 /*
  1861 	LinkableMapObj *sel=getSelectedLMO();
  1862 	if (sel &&
  1863 		(selectionType() == TreeItem::Branch || 
  1864 		selectionType() == TreeItem::MapCenter  ||
  1865 		selectionType() == TreeItem::Image ))
  1866 	{
  1867 		if (redosAvail == 0)
  1868 		{
  1869 			// Copy to history
  1870 			QString s=getSelectString(sel);
  1871 			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel  );
  1872 			curClipboard=curStep;
  1873 		}
  1874 
  1875 		// Copy also to global clipboard, because we are at last step in history
  1876 		QString bakMapName(QString("history-%1").arg(curStep));
  1877 		QString bakMapDir(tmpMapDir +"/"+bakMapName);
  1878 		copyDir (bakMapDir,clipboardDir );
  1879 
  1880 		clipboardEmpty=false;
  1881 		updateActions();
  1882 	}	    
  1883 */	
  1884 }
  1885 
  1886 
  1887 void VymModel::pasteNoSave(const int &n)	//FIXME-2
  1888 {
  1889 /*
  1890 	bool old=blockSaveState;
  1891 	blockSaveState=true;
  1892 	bool zippedOrg=zipped;
  1893 	if (redosAvail > 0 || n!=0)
  1894 	{
  1895 		// Use the "historical" buffer
  1896 		QString bakMapName(QString("history-%1").arg(n));
  1897 		QString bakMapDir(tmpMapDir +"/"+bakMapName);
  1898 		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
  1899 	} else
  1900 		// Use the global buffer
  1901 		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
  1902 	zipped=zippedOrg;
  1903 	blockSaveState=old;
  1904 */
  1905 }
  1906 
  1907 void VymModel::paste()		//FIXME-2
  1908 {   
  1909 /*
  1910 	BranchObj *sel=getSelectedBranch();
  1911 	if (sel)
  1912 	{
  1913 		saveStateChangingPart(
  1914 			sel,
  1915 			sel,
  1916 			QString ("paste (%1)").arg(curClipboard),
  1917 			QString("Paste to %1").arg( getObjectName(sel))
  1918 		);
  1919 		pasteNoSave(0);
  1920 		reposition();
  1921 	}
  1922 */	
  1923 }
  1924 
  1925 void VymModel::cut()	//FIXME-2
  1926 {
  1927 /*
  1928 	LinkableMapObj *sel=getSelectedLMO();
  1929 	if ( sel && (selectionType() == TreeItem::Branch ||
  1930 		selectionType()==TreeItem::MapCenter ||
  1931 		selectionType()==TreeItem::Image))
  1932 	{
  1933 */
  1934 	/* No savestate! savestate is called in cutNoSave
  1935 		saveStateChangingPart(
  1936 			sel->getParObj(),
  1937 			sel,
  1938 			"cut ()",
  1939 			QString("Cut %1").arg(getObjectName(sel ))
  1940 		);
  1941 	*/	
  1942 /*	
  1943 		copy();
  1944 		deleteSelection();
  1945 		reposition();
  1946 	}
  1947 */}
  1948 
  1949 void VymModel::moveBranchUp()	//FIXME-2
  1950 {
  1951 /*
  1952 	BranchObj* bo=getSelectedBranch();
  1953 	BranchObj* par;
  1954 	if (bo)
  1955 	{
  1956 		if (!bo->canMoveBranchUp()) return;
  1957 		par=(BranchObj*)(bo->getParObj());
  1958 		BranchObj *obo=par->moveBranchUp (bo);	// bo will be the one below selection
  1959 		saveState (getSelectString(bo),"moveBranchDown ()",getSelectString(obo),"moveBranchUp ()",QString("Move up %1").arg(getObjectName(bo)));
  1960 		reposition();
  1961 		//FIXME-3 VM needed? scene()->update();
  1962 		selection.update();
  1963 		ensureSelectionVisible();
  1964 	}
  1965 	*/
  1966 }
  1967 
  1968 void VymModel::moveBranchDown()	//FIXME-2
  1969 {
  1970 /*
  1971 	BranchObj* bo=getSelectedBranch();
  1972 	BranchObj* par;
  1973 	if (bo)
  1974 	{
  1975 		if (!bo->canMoveBranchDown()) return;
  1976 		par=(BranchObj*)(bo->getParObj());
  1977 		BranchObj *obo=par->moveBranchDown(bo);	// bo will be the one above selection
  1978 		saveState(getSelectString(bo),"moveBranchUp ()",getSelectString(obo),"moveBranchDown ()",QString("Move down %1").arg(getObjectName(bo)));
  1979 		reposition();
  1980 		//FIXME-3 VM needed? scene()->update();
  1981 		selection.update();
  1982 		ensureSelectionVisible();
  1983 	}	
  1984 */	
  1985 }
  1986 
  1987 void VymModel::sortChildren()	// FIXME-2 not implemented yet
  1988 {
  1989 /*
  1990 	BranchObj* bo=getSelectedBranch();
  1991 	if (bo)
  1992 	{
  1993 		if(treeItem->branchCount()>1)
  1994 		{
  1995 			saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getObjectName(bo)));
  1996 			bo->sortChildren();
  1997 			reposition();
  1998 			ensureSelectionVisible();
  1999 		}
  2000 	}
  2001 */
  2002 }
  2003 
  2004 MapCenterItem* VymModel::createMapCenter()
  2005 {
  2006 	MapCenterItem *mci=addMapCenter (QPointF (0,0) );
  2007 	select (mci);
  2008 	return mci;
  2009 }
  2010 
  2011 BranchItem* VymModel::createBranch()	// FIXME-2 switcht addNewBrancInt to BranchItem...
  2012 {
  2013 	BranchObj* bo=addNewBranchInt (-2);
  2014 	return (BranchItem*)bo->getTreeItem();
  2015 }
  2016 
  2017 TreeItem* VymModel::createImage()	//FIXME-2
  2018 {
  2019 /*
  2020 	BranchObj *bo=getSelectedBranch();
  2021 	if (bo)
  2022 	{
  2023 		FloatImageObj *newfio=bo->addFloatImage(); // FIXME-1 VM Old model, merge with below
  2024 
  2025 		// Create TreeItem
  2026 		QList<QVariant> cData;
  2027 		cData << "VM:createImage" << "undef"<<"undef";
  2028 		TreeItem *parti=bo->getTreeItem();
  2029 		TreeItem *ti=new TreeItem (cData,parti);
  2030 		ti->setLMO (newfio);
  2031 		ti->setType (TreeItem::Image);
  2032 		parti->appendChild (ti);
  2033 
  2034 		if (newfio)
  2035 		{
  2036 			newfio->setTreeItem (ti);
  2037 			select (newfio); // FIXME-2 VM really needed here?
  2038 			return ti;
  2039 		}
  2040 	}
  2041 	return NULL;
  2042 */
  2043 }
  2044 
  2045 MapCenterItem* VymModel::addMapCenter ()
  2046 {
  2047 	MapCenterItem *mci=addMapCenter (contextPos);	//FIXME-2 get rid of MCO here
  2048 	//FIXME-3 selection.select (mco);
  2049 	updateActions();
  2050 	ensureSelectionVisible();
  2051 	saveState (
  2052 		mci,
  2053 		"delete()",
  2054 		NULL,
  2055 		QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
  2056 		QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
  2057 	);	
  2058 	return mci;	
  2059 }
  2060 
  2061 MapCenterItem* VymModel::addMapCenter(QPointF absPos)
  2062 {
  2063 
  2064 	// Create TreeItem
  2065 	QModelIndex parix=index(rootItem);
  2066 
  2067 	int n=rootItem->branchCount();
  2068 
  2069 	emit (layoutAboutToBeChanged() );
  2070 	beginInsertRows (parix,n,n+1);
  2071 
  2072 	QList<QVariant> cData;
  2073 	cData << "VM:addMapCenter" << "undef"<<"undef";
  2074 	MapCenterItem *mci=new MapCenterItem (cData,rootItem);
  2075 	cout << "VM::addMapCenter  mci="<<mci<<endl;
  2076 	mci->setType (TreeItem::MapCenter);
  2077 	mci->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
  2078 	rootItem->appendChild (mci);
  2079 
  2080 	endInsertRows();
  2081 	emit (newChildObject (parix));
  2082 	emit (layoutChanged() );
  2083 
  2084 	// Create MapObj
  2085 	MapCenterObj *mapCenter = new MapCenterObj(mapScene,this);
  2086 	//mapCenter->setMapEditor(mapEditor);		//FIXME-3 VM needed to get defLinkStyle, mapLinkColorHint ... for later added objects
  2087 	mapCenter->setTreeItem (mci);	// TreeItem needs to exist before setVisibility
  2088 	mci->setLMO (mapCenter);
  2089 	mapCenter->updateHeading();
  2090 	mapCenter->move (absPos);
  2091     mapCenter->setVisibility (true);
  2092 	//mapCenter->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
  2093 	// Testing
  2094 /*
  2095 	qWarning ("MW::insertRow a");
  2096     if (!insertRow(0, parix))
  2097 	{
  2098 		std::cout << "  war nix...\n";
  2099 	}	
  2100 	qWarning ("MW::insertRow b");
  2101 */
  2102 
  2103 	return mci;
  2104 }
  2105 
  2106 MapCenterItem* VymModel::removeMapCenter(MapCenterItem* mci) //FIXME-2
  2107 {
  2108 /*
  2109 	int i=mapCenters.indexOf (mco);
  2110 	if (i>=0)
  2111 	{
  2112 		mapCenters.removeAt (i);
  2113 		delete (mco);
  2114 		if (i>0) return rootItem->getBranchNum(i-1);	// Return previous MCO
  2115 	}
  2116 */	
  2117 	return NULL;
  2118 }
  2119 
  2120 MapCenterObj* VymModel::getLastMapCenter()		// FIXME-3 still needed?
  2121 {
  2122 	return (MapCenterObj*)rootItem->getLastBranch();
  2123 }
  2124 
  2125 
  2126 BranchObj* VymModel::addNewBranchInt(int num)
  2127 {
  2128 	// Depending on pos:
  2129 	// -3		insert in children of parent  above selection 
  2130 	// -2		add branch to selection 
  2131 	// -1		insert in children of parent below selection 
  2132 	// 0..n		insert in children of parent at pos
  2133 	BranchObj *newbo=NULL;
  2134 	BranchObj *bo=getSelectedBranchObj();	//FIXME-2
  2135 	BranchItem *bi=getSelectedBranchItem();
  2136 	if (bo)
  2137 	{
  2138 		if (num==-2)
  2139 		{
  2140 			// Create TreeItem
  2141 			QList<QVariant> cData;
  2142 			cData << "new" << "undef"<<"undef";
  2143 
  2144 			BranchItem *parbi=bi;
  2145 			QModelIndex parix=index(parbi);
  2146 			int n=parbi->branchCount();
  2147 
  2148 			emit (layoutAboutToBeChanged() );
  2149 			beginInsertRows (parix,n,n+1);
  2150 			bi=new BranchItem (cData,parbi);
  2151 			bi->setType (TreeItem::Branch);
  2152 			bi->setHeading (QApplication::translate("Heading of new branch in map", "new"));
  2153 
  2154 			parbi->appendChild (bi);
  2155 			endInsertRows ();
  2156 			emit (newChildObject (parix));
  2157 			emit (layoutChanged() );
  2158 
  2159 			// save scroll state. If scrolled, automatically select
  2160 			// new branch in order to tmp unscroll parent...
  2161 			newbo=bo->addBranch();
  2162 
  2163 			if (newbo)
  2164 			{
  2165 				bi->setLMO (newbo);
  2166 				newbo->setTreeItem (bi);
  2167 				select (bi); 
  2168 			}
  2169 			
  2170 		}else if (num==-1)
  2171 		{
  2172 			num=bi->num()+1;
  2173 			bo=(BranchObj*)bo->getParObj();
  2174 			if (bo) newbo=bo->insertBranch(num);	//FIXME-1 VM still missing 
  2175 		}else if (num==-3)
  2176 		{
  2177 			num=bi->num();
  2178 			bo=(BranchObj*)bo->getParObj();
  2179 			if (bo) newbo=bo->insertBranch(num);	//FIXME-1 VM still missing
  2180 		}
  2181 	}	
  2182 	return newbo;
  2183 }	
  2184 
  2185 BranchObj* VymModel::addNewBranch(int pos)
  2186 {
  2187 	// Different meaning than num in addNewBranchInt!
  2188 	// -1	add above
  2189 	//  0	add as child
  2190 	// +1	add below
  2191 	BranchObj *bo = getSelectedBranchObj();	//FIXME-2
  2192 	BranchObj *newbo=NULL;
  2193 
  2194 	if (bo)
  2195 	{
  2196 		// FIXME-2 VM  do we still need this in model? setCursor (Qt::ArrowCursor);
  2197 
  2198 		newbo=addNewBranchInt (pos-2);
  2199 
  2200 		if (newbo)
  2201 		{
  2202 			saveState(
  2203 				newbo->getTreeItem(),		
  2204 				"delete ()",
  2205 				bo->getTreeItem(),
  2206 				QString ("addBranch (%1)").arg(pos),
  2207 				QString ("Add new branch to %1").arg(getObjectName(bo)));	
  2208 
  2209 			reposition();
  2210 			// selection.update(); FIXME-3
  2211 			latestSelectionString=getSelectString(newbo);
  2212 			// In Network mode, the client needs to know where the new branch is,
  2213 			// so we have to pass on this information via saveState.
  2214 			// TODO: Get rid of this positioning workaround
  2215 			QString ps=qpointfToString (newbo->getAbsPos());
  2216 			sendData ("selectLatestAdded ()");
  2217 			sendData (QString("move %1").arg(ps));
  2218 			sendSelection();
  2219 		}
  2220 	}	
  2221 	return newbo;
  2222 }
  2223 
  2224 
  2225 BranchObj* VymModel::addNewBranchBefore()	//FIXME-2
  2226 {
  2227 /*
  2228 	BranchObj *newbo=NULL;
  2229 	BranchObj *bo = getSelectedBranch();
  2230 	if (bo && selectionType()==TreeItem::Branch)
  2231 		 // We accept no MapCenterObj here, so we _have_ a parent
  2232 	{
  2233 		QPointF p=bo->getRelPos();
  2234 
  2235 
  2236 		BranchObj *parbo=(BranchObj*)(bo->getParObj());
  2237 
  2238 		// add below selection
  2239 		newbo=parbo->insertBranch(bo->getTreeItem()->num()+1);		//FIXME-1 VM still missing
  2240 
  2241 		if (newbo)
  2242 		{
  2243 			newbo->move2RelPos (p);
  2244 
  2245 			// Move selection to new branch
  2246 			bo->linkTo (newbo,-1);
  2247 
  2248 			saveState (newbo, "deleteKeepChildren ()", newbo, "addBranchBefore ()", 
  2249 				QString ("Add branch before %1").arg(getObjectName(bo)));
  2250 
  2251 			reposition();
  2252 			// selection.update(); FIXME-3 
  2253 		}
  2254 	}	
  2255 	latestSelectionString=selection.getSelectString();
  2256 	return newbo;
  2257 	*/
  2258 	return NULL;
  2259 }
  2260 
  2261 void VymModel::deleteSelection()
  2262 {
  2263 	BranchObj *bo = getSelectedBranchObj();	// FIXME-2 VM should not be necessary
  2264 	BranchItem *selbi=getSelectedBranchItem();
  2265 
  2266 	if (!selbi) return;
  2267 
  2268 	QModelIndex ix=index (selbi);
  2269 	if (!ix.isValid() ) return;
  2270 
  2271 	QModelIndex parentIndex=parent(ix);
  2272 /*
  2273 	if (!parentIndex.isValid()) 
  2274 	{
  2275 	cout << "VM::delete no valid parent ix\n";
  2276 	return;
  2277 	}
  2278 */	
  2279 	
  2280 	/*
  2281 	if (selbi->getType()==TreeItem::MapCenter)	
  2282 	{
  2283 	//	BranchObj* par=(BranchObj*)(bo->getParObj());
  2284 		//selection.unselect();
  2285 		// FIXME-1 VM Note:  does saveStateRemovingPart work for MCO? (No parent!)
  2286 		//saveStateRemovingPart (bo, QString ("Delete %1").arg(getObjectName(bo)));
  2287 		emit (layoutAboutToBeChanged() );
  2288 
  2289 		parentIndex=parent(index(selbi));
  2290 		cout << "VM::delete  selbi="<<selbi<<"  row="<<ix.row()<<"  parIndex.valid="<<parentIndex.isValid()<<endl;
  2291 		int n=ix.row();
  2292 		beginRemoveRows (parentIndex,n,n);
  2293 		removeRows (n,1,parentIndex);
  2294 		MapCenterItem *mci=removeMapCenter (selbi);
  2295 		endRemoveRows();
  2296 		if (mci) 
  2297 		{
  2298 			select (mci);
  2299 			ensureSelectionVisible();
  2300 			reposition();
  2301 		}	
  2302 
  2303 		emit (layoutChanged() );
  2304 	}
  2305 	if (selbi->getType()==TreeItem::Branch)
  2306 	*/
  2307 	if (selbi->isBranchLikeType() )
  2308 	{
  2309 
  2310 		BranchObj* par=(BranchObj*)bo->getParObj();
  2311 		unselect();
  2312 		saveStateRemovingPart (selbi, QString ("Delete %1").arg(getObjectName(bo)));
  2313 
  2314 		emit (layoutAboutToBeChanged() );
  2315 
  2316 		cout << "VM::delete  selbi="<<selbi<<"  row="<<ix.row()<<endl;
  2317 		parentIndex=parent(index(selbi));
  2318 		int n=ix.row();
  2319 		beginRemoveRows (parentIndex,n,n);
  2320 		removeRows (n,1,parentIndex);
  2321 		endRemoveRows();
  2322 		par->removeBranch(bo);	// remove from BranchObj lists...
  2323 		select (par);
  2324 		ensureSelectionVisible();
  2325 		reposition();
  2326 
  2327 		emit (layoutChanged() );
  2328 		return;
  2329 	}
  2330 	//FloatImageObj *fio=selection.getFloatImage(); 	//FIXME-1 VM still missing
  2331 
  2332 /*
  2333 	if (fio)
  2334 	{
  2335 		BranchObj* par=(BranchObj*)fio->getParObj();
  2336 		saveStateChangingPart(
  2337 			par, 
  2338 			fio,
  2339 			"delete ()",
  2340 			QString("Delete %1").arg(getObjectName(fio))
  2341 		);
  2342 		unselect();
  2343 		par->removeFloatImage(fio);
  2344 		select (par);
  2345 		reposition();
  2346 		ensureSelectionVisible();
  2347 		return;
  2348 	}
  2349 	*/
  2350 }
  2351 
  2352 void VymModel::deleteKeepChildren()		//FIXME-2 VM still missing
  2353 
  2354 {
  2355 /*
  2356 	BranchObj *bo=getSelectedBranch();
  2357 	BranchObj *par;
  2358 	if (bo)
  2359 	{
  2360 		par=(BranchObj*)(bo->getParObj());
  2361 
  2362 		// Don't use this on mapcenter
  2363 		if (!par) return;
  2364 
  2365 		// Check if we have childs at all to keep
  2366 		if (bo->getTreeItem()->branchCount()==0) 
  2367 		{
  2368 			deleteSelection();
  2369 			return;
  2370 		}
  2371 
  2372 		QPointF p=bo->getRelPos();
  2373 		saveStateChangingPart(
  2374 			bo->getParObj(),
  2375 			bo,
  2376 			"deleteKeepChildren ()",
  2377 			QString("Remove %1 and keep its children").arg(getObjectName(bo))
  2378 		);
  2379 
  2380 		QString sel=getSelectString(bo);
  2381 		unselect();
  2382 		par->removeBranchHere(bo);
  2383 		reposition();
  2384 		select (sel);
  2385 		getSelectedBranch()->move2RelPos (p);
  2386 		reposition();
  2387 	}	
  2388 */}
  2389 
  2390 void VymModel::deleteChildren()		//FIXME-2 VM still missing
  2391 
  2392 {
  2393 /*
  2394 	BranchObj *bo=getSelectedBranch();
  2395 	if (bo)
  2396 	{		
  2397 		saveStateChangingPart(
  2398 			bo, 
  2399 			bo,
  2400 			"deleteChildren ()",
  2401 			QString( "Remove children of branch %1").arg(getObjectName(bo))
  2402 		);
  2403 		bo->removeChildren();
  2404 		reposition();
  2405 	}	
  2406 */}
  2407 
  2408 
  2409 bool VymModel::scrollBranch(BranchItem *bi)
  2410 {
  2411 	if (bi)	
  2412 	{
  2413 		if (bi->isScrolled()) return false;
  2414 		if (bi->branchCount()==0) return false;
  2415 		if (bi->depth()==0) return false;
  2416 		QString u,r;
  2417 		r="scroll";
  2418 		u="unscroll";
  2419 		/* FIXME-3 no savestate yet
  2420 		saveState(
  2421 			bo,
  2422 			QString ("%1 ()").arg(u),
  2423 			bo,
  2424 			QString ("%1 ()").arg(r),
  2425 			QString ("%1 %2").arg(r).arg(getObjectName(bo))
  2426 		);
  2427 		*/
  2428 		bi->toggleScroll();
  2429 		//selection.update();
  2430 		// FIXME-3 VM needed? scene()->update();
  2431 		return true;
  2432 	}	
  2433 	return false;
  2434 }
  2435 
  2436 bool VymModel::unscrollBranch(BranchItem *bi)
  2437 {
  2438 	if (bi)
  2439 	{
  2440 		if (bi->isScrolled()) return false;
  2441 		if (bi->branchCount()==0) return false;
  2442 		if (bi->depth()==0) return false;
  2443 
  2444 		QString u,r;
  2445 		u="scroll";
  2446 		r="unscroll";
  2447 		/* FIXME-3 no savestate yet
  2448 		saveState(
  2449 			bo,
  2450 			QString ("%1 ()").arg(u),
  2451 			bo,
  2452 			QString ("%1 ()").arg(r),
  2453 			QString ("%1 %2").arg(r).arg(getObjectName(bo))
  2454 		);
  2455 		*/
  2456 		bi->toggleScroll();
  2457 		// selection.update();
  2458 		// FIXME-3 VM needed? scene()->update();
  2459 		return true;
  2460 	}	
  2461 	return false;
  2462 }
  2463 
  2464 void VymModel::toggleScroll()
  2465 {
  2466 	BranchItem *bi=(BranchItem*)getSelectedBranchItem();
  2467 	if (bi && bi->getType()==TreeItem::Branch )
  2468 	{
  2469 		if (bi->isScrolled())
  2470 			unscrollBranch (bi);
  2471 		else
  2472 			scrollBranch (bi);
  2473 	}
  2474 }
  2475 
  2476 void VymModel::unscrollChildren() 	// FIXME-1	first, next moved to vymmodel
  2477 
  2478 {
  2479 /*
  2480 	BranchObj *bo=getSelectedBranch();
  2481 	if (bo)
  2482 	{
  2483 		bo->first();
  2484 		while (bo) 
  2485 		{
  2486 			if (bo->isScrolled()) unscrollBranch (bo);
  2487 			bo=bo->next();
  2488 		}
  2489 	}	
  2490 */	
  2491 }
  2492 
  2493 void VymModel::addFloatImage (const QPixmap &img) //FIXME-2
  2494 {
  2495 /*
  2496 	BranchObj *bo=getSelectedBranch();
  2497 	if (bo)
  2498   {
  2499 	FloatImageObj *fio=bo->addFloatImage();
  2500     fio->load(img);
  2501     fio->setOriginalFilename("No original filename (image added by dropevent)");	
  2502 	QString s=getSelectString(bo);
  2503 	saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio  );
  2504 	saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
  2505     reposition();
  2506     // FIXME-3 VM needed? scene()->update();
  2507   }
  2508 */
  2509 }
  2510 
  2511 
  2512 void VymModel::colorBranch (QColor c)	//FIXME-2
  2513 {
  2514 /*
  2515 	BranchObj *bo=getSelectedBranch();
  2516 	if (bo)
  2517 	{
  2518 		saveState(
  2519 			bo, 
  2520 			QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
  2521 			bo,
  2522 			QString ("colorBranch (\"%1\")").arg(c.name()),
  2523 			QString("Set color of %1 to %2").arg(getObjectName(bo)).arg(c.name())
  2524 		);	
  2525 		bo->setColor(c); // color branch
  2526 	}
  2527 */
  2528 }
  2529 
  2530 void VymModel::colorSubtree (QColor c) //FIXME-2
  2531 {
  2532 /*
  2533 	BranchObj *bo=getSelectedBranch();
  2534 	if (bo) 
  2535 	{
  2536 		saveStateChangingPart(
  2537 			bo, 
  2538 			bo,
  2539 			QString ("colorSubtree (\"%1\")").arg(c.name()),
  2540 			QString ("Set color of %1 and children to %2").arg(getObjectName(bo)).arg(c.name())
  2541 		);	
  2542 		bo->setColorSubtree (c); // color links, color children
  2543 	}
  2544 */	
  2545 }
  2546 
  2547 QColor VymModel::getCurrentHeadingColor()	//FIXME-2
  2548 {
  2549 /*
  2550 	BranchObj *bo=getSelectedBranch();
  2551 	if (bo) return bo->getColor(); 
  2552 	
  2553 	QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
  2554 	*/
  2555 	return Qt::black;
  2556 }
  2557 
  2558 
  2559 
  2560 void VymModel::editURL()	//FIXME-2
  2561 {
  2562 /*
  2563 	BranchObj *bo=getSelectedBranch();
  2564 	if (bo)
  2565 	{		
  2566 		bool ok;
  2567 		QString text = QInputDialog::getText(
  2568 				"VYM", tr("Enter URL:"), QLineEdit::Normal,
  2569 				bo->getURL(), &ok, NULL);
  2570 		if ( ok) 
  2571 			// user entered something and pressed OK
  2572 			setURL(text);
  2573 	}
  2574 */
  2575 }
  2576 
  2577 void VymModel::editLocalURL()	//FIXME-2
  2578 {
  2579 /*
  2580 	BranchObj *bo=getSelectedBranch();
  2581 	if (bo)
  2582 	{		
  2583 		QStringList filters;
  2584 		filters <<"All files (*)";
  2585 		filters << tr("Text","Filedialog") + " (*.txt)";
  2586 		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
  2587 		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
  2588 		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
  2589 		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
  2590 		fd->setFilters (filters);
  2591 		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
  2592 		fd->setDirectory (lastFileDir);
  2593 		if (! bo->getVymLink().isEmpty() )
  2594 			fd->selectFile( bo->getURL() );
  2595 		fd->show();
  2596 
  2597 		if ( fd->exec() == QDialog::Accepted )
  2598 		{
  2599 			lastFileDir=QDir (fd->directory().path());
  2600 			setURL (fd->selectedFile() );
  2601 		}
  2602 	}
  2603 	*/
  2604 }
  2605 
  2606 
  2607 void VymModel::editHeading2URL() //FIXME-2
  2608 {
  2609 /*
  2610 	BranchObj *bo=getSelectedBranch();
  2611 	if (bo)
  2612 		setURL (bo->getHeading());
  2613 */		
  2614 }	
  2615 
  2616 void VymModel::editBugzilla2URL()	//FIXME-2
  2617 {
  2618 /*
  2619 	BranchObj *bo=getSelectedBranch();
  2620 	if (bo)
  2621 	{		
  2622 		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
  2623 		setURL (url);
  2624 	}
  2625 */	
  2626 }	
  2627 
  2628 void VymModel::editFATE2URL()	//FIXME-2
  2629 {
  2630 /*
  2631 	BranchObj *bo=getSelectedBranch();
  2632 	if (bo)
  2633 	{		
  2634 		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
  2635 		saveState(
  2636 			bo,
  2637 			"setURL (\""+bo->getURL()+"\")",
  2638 			bo,
  2639 			"setURL (\""+url+"\")",
  2640 			QString("Use heading of %1 as link to FATE").arg(getObjectName(bo))
  2641 		);	
  2642 		bo->setURL (url);
  2643 		updateActions();
  2644 	}
  2645 */	
  2646 }	
  2647 
  2648 void VymModel::editVymLink()
  2649 {
  2650 	BranchItem *bi=getSelectedBranchItem();
  2651 	if (bi)
  2652 	{		
  2653 		QStringList filters;
  2654 		filters <<"VYM map (*.vym)";
  2655 		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
  2656 		fd->setFilters (filters);
  2657 		fd->setCaption(vymName+" - " +tr("Link to another map"));
  2658 		fd->setDirectory (lastFileDir);
  2659 		if (! bi->getVymLink().isEmpty() )
  2660 			fd->selectFile( bi->getVymLink() );
  2661 		fd->show();
  2662 
  2663 		QString fn;
  2664 		if ( fd->exec() == QDialog::Accepted )
  2665 		{
  2666 			lastFileDir=QDir (fd->directory().path());
  2667 			saveState(
  2668 				bi,
  2669 				"setVymLink (\""+bi->getVymLink()+"\")",
  2670 				bi,
  2671 				"setVymLink (\""+fd->selectedFile()+"\")",
  2672 				QString("Set vymlink of %1 to %2").arg(getObjectName(bi)).arg(fd->selectedFile())
  2673 			);	
  2674 			setVymLink (fd->selectedFile() );	// FIXME-2 ok?
  2675 		}
  2676 	}
  2677 }
  2678 
  2679 void VymModel::setVymLink (const QString &s)	// FIXME-3 no savestate?
  2680 {
  2681 	// Internal function, no saveState needed
  2682 	BranchItem *bi=getSelectedBranchItem();
  2683 	if (bi)
  2684 	{
  2685 		bi->getBranchObj()->setVymLink(s);	//FIXME-3 check getBO
  2686 		reposition();
  2687 		updateActions();
  2688 		//selection.update();
  2689 		ensureSelectionVisible();
  2690 	}
  2691 }
  2692 
  2693 void VymModel::deleteVymLink()
  2694 {
  2695 	BranchItem *bi=getSelectedBranchItem();
  2696 	if (bi)
  2697 	{		
  2698 		saveState(
  2699 			bi,
  2700 			"setVymLink (\""+bi->getBranchObj()->getVymLink()+"\")", //FIXME-3 check getBO
  2701 
  2702 			bi,
  2703 			"setVymLink (\"\")",
  2704 			QString("Unset vymlink of %1").arg(getObjectName(bi))
  2705 		);	
  2706 		bi->getBranchObj()->setVymLink ("" );	//FIXME-3 check getBO
  2707 		updateActions();
  2708 		reposition();
  2709 		// FIXME-3 VM needed? scene()->update();
  2710 	}
  2711 }
  2712 
  2713 QString VymModel::getVymLink()
  2714 {
  2715 	BranchItem *bi=getSelectedBranchItem();
  2716 	if (bi)
  2717 		return bi->getBranchObj()->getVymLink();	//FIXME-3 check getBO here...
  2718 	else	
  2719 		return "";
  2720 	
  2721 }
  2722 
  2723 QStringList VymModel::getVymLinks()	// FIXME-1	first, next moved to vymmodel
  2724 {
  2725 	return QStringList();
  2726 /*
  2727 	QStringList links;
  2728 	BranchObj *bo=getSelectedBranch();
  2729 	if (bo)
  2730 	{		
  2731 		bo=bo->first();	
  2732 		while (bo) 
  2733 		{
  2734 			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
  2735 			bo=bo->next();
  2736 		}	
  2737 	}	
  2738 	return links;
  2739 */	
  2740 }
  2741 
  2742 
  2743 void VymModel::followXLink(int i)	// FIXME-2
  2744 {
  2745 	i=0;
  2746 	/*
  2747 	BranchObj *bo=getSelectedBranch();
  2748 	if (bo)
  2749 	{
  2750 		bo=bo->XLinkTargetAt(i);
  2751 		if (bo) 
  2752 		{
  2753 			selection.select(bo);
  2754 			ensureSelectionVisible();
  2755 		}
  2756 	}
  2757 	*/
  2758 }
  2759 
  2760 void VymModel::editXLink(int i)	// FIXME-2 VM missing saveState
  2761 {
  2762 	i=0;
  2763 	/*
  2764 	BranchObj *bo=getSelectedBranch();
  2765 	if (bo)
  2766 	{
  2767 		XLinkObj *xlo=bo->XLinkAt(i);
  2768 		if (xlo) 
  2769 		{
  2770 			EditXLinkDialog dia;
  2771 			dia.setXLink (xlo);
  2772 			dia.setSelection(bo);
  2773 			if (dia.exec() == QDialog::Accepted)
  2774 			{
  2775 				if (dia.useSettingsGlobal() )
  2776 				{
  2777 					setMapDefXLinkColor (xlo->getColor() );
  2778 					setMapDefXLinkWidth (xlo->getWidth() );
  2779 				}
  2780 				if (dia.deleteXLink())
  2781 					bo->deleteXLinkAt(i);
  2782 			}
  2783 		}	
  2784 	}
  2785 */	
  2786 }
  2787 
  2788 
  2789 
  2790 
  2791 
  2792 //////////////////////////////////////////////
  2793 // Scripting
  2794 //////////////////////////////////////////////
  2795 
  2796 void VymModel::parseAtom(const QString &atom)
  2797 {
  2798 	//BranchObj *selb=getSelectedBranch();	// FIXME-4
  2799 	TreeItem* selti=getSelectedItem();
  2800 	BranchItem *selbi=getSelectedBranchItem();
  2801 	QString s,t;
  2802 	double x,y;
  2803 	int n;
  2804 	bool b,ok;
  2805 
  2806 	// Split string s into command and parameters
  2807 	parser.parseAtom (atom);
  2808 	QString com=parser.getCommand();
  2809 	
  2810 	// External commands
  2811 	/////////////////////////////////////////////////////////////////////
  2812 	if (com=="addBranch")
  2813 	{
  2814 		if (!selti)
  2815 		{
  2816 			parser.setError (Aborted,"Nothing selected");
  2817 		} else if (! selbi )
  2818 		{				  
  2819 			parser.setError (Aborted,"Type of selection is not a branch");
  2820 		} else 
  2821 		{	
  2822 			QList <int> pl;
  2823 			pl << 0 <<1;
  2824 			if (parser.checkParCount(pl))
  2825 			{
  2826 				if (parser.parCount()==0)
  2827 					addNewBranch (0);
  2828 				else
  2829 				{
  2830 					n=parser.parInt (ok,0);
  2831 					if (ok ) addNewBranch (n);
  2832 				}
  2833 			}
  2834 		}
  2835 	/////////////////////////////////////////////////////////////////////
  2836 	} else if (com=="addBranchBefore")
  2837 	{
  2838 		if (!selti)
  2839 		{
  2840 			parser.setError (Aborted,"Nothing selected");
  2841 		} else if (! selbi )
  2842 		{				  
  2843 			parser.setError (Aborted,"Type of selection is not a branch");
  2844 		} else 
  2845 		{	
  2846 			if (parser.parCount()==0)
  2847 			{
  2848 				addNewBranchBefore ();
  2849 			}	
  2850 		}
  2851 	/////////////////////////////////////////////////////////////////////
  2852 	} else if (com==QString("addMapCenter"))
  2853 	{
  2854 		if (parser.checkParCount(2))
  2855 		{
  2856 			x=parser.parDouble (ok,0);
  2857 			if (ok)
  2858 			{
  2859 				y=parser.parDouble (ok,1);
  2860 				if (ok) addMapCenter (QPointF(x,y));
  2861 			}
  2862 		}	
  2863 	/////////////////////////////////////////////////////////////////////
  2864 	} else if (com==QString("addMapReplace"))
  2865 	{
  2866 		if (!selti)
  2867 		{
  2868 			parser.setError (Aborted,"Nothing selected");
  2869 		} else if (! selbi )
  2870 		{				  
  2871 			parser.setError (Aborted,"Type of selection is not a branch");
  2872 		} else if (parser.checkParCount(1))
  2873 		{
  2874 			//s=parser.parString (ok,0);	// selection
  2875 			t=parser.parString (ok,0);	// path to map
  2876 			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  2877 			addMapReplaceInt(getSelectString(selbi),t);	
  2878 		}
  2879 	/////////////////////////////////////////////////////////////////////
  2880 	} else if (com==QString("addMapInsert"))
  2881 	{
  2882 		if (!selti)
  2883 		{
  2884 			parser.setError (Aborted,"Nothing selected");
  2885 		} else if (! selbi )
  2886 		{				  
  2887 			parser.setError (Aborted,"Type of selection is not a branch");
  2888 		} else 
  2889 		{	
  2890 			if (parser.checkParCount(2))
  2891 			{
  2892 				t=parser.parString (ok,0);	// path to map
  2893 				n=parser.parInt(ok,1);		// position
  2894 				if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  2895 				addMapInsertInt(t,n);	
  2896 			}
  2897 		}
  2898 	/////////////////////////////////////////////////////////////////////
  2899 	} else /*if (com=="clearFlags")	// FIXME-2
  2900 	{
  2901 		if (!selti )
  2902 		{
  2903 			parser.setError (Aborted,"Nothing selected");
  2904 		} else if (! selbi )
  2905 		{				  
  2906 			parser.setError (Aborted,"Type of selection is not a branch");
  2907 		} else if (parser.checkParCount(0))
  2908 		{
  2909 			selb->clearStandardFlags();	
  2910 			selb->updateFlagsToolbar();
  2911 		}
  2912 	/////////////////////////////////////////////////////////////////////
  2913 	} else */ if (com=="colorBranch")
  2914 	{
  2915 		if (!selti)
  2916 		{
  2917 			parser.setError (Aborted,"Nothing selected");
  2918 		} else if (! selbi )
  2919 		{				  
  2920 			parser.setError (Aborted,"Type of selection is not a branch");
  2921 		} else if (parser.checkParCount(1))
  2922 		{	
  2923 			QColor c=parser.parColor (ok,0);
  2924 			if (ok) colorBranch (c);
  2925 		}	
  2926 	/////////////////////////////////////////////////////////////////////
  2927 	} else if (com=="colorSubtree")
  2928 	{
  2929 		if (!selti)
  2930 		{
  2931 			parser.setError (Aborted,"Nothing selected");
  2932 		} else if (! selbi )
  2933 		{				  
  2934 			parser.setError (Aborted,"Type of selection is not a branch");
  2935 		} else if (parser.checkParCount(1))
  2936 		{	
  2937 			QColor c=parser.parColor (ok,0);
  2938 			if (ok) colorSubtree (c);
  2939 		}	
  2940 	/////////////////////////////////////////////////////////////////////
  2941 	} else if (com=="copy")
  2942 	{
  2943 		if (!selti)
  2944 		{
  2945 			parser.setError (Aborted,"Nothing selected");
  2946 		} else if (! selbi )
  2947 		{				  
  2948 			parser.setError (Aborted,"Type of selection is not a branch");
  2949 		} else if (parser.checkParCount(0))
  2950 		{	
  2951 			//FIXME-1 missing action for copy
  2952 		}	
  2953 	/////////////////////////////////////////////////////////////////////
  2954 	} else if (com=="cut")
  2955 	{
  2956 		if (!selti)
  2957 		{
  2958 			parser.setError (Aborted,"Nothing selected");
  2959 		} else if ( selectionType()!=TreeItem::Branch  && 
  2960 					selectionType()!=TreeItem::MapCenter  &&
  2961 					selectionType()!=TreeItem::Image )
  2962 		{				  
  2963 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  2964 		} else if (parser.checkParCount(0))
  2965 		{	
  2966 			cut();
  2967 		}	
  2968 	/////////////////////////////////////////////////////////////////////
  2969 	} else if (com=="delete")
  2970 	{
  2971 		if (!selti)
  2972 		{
  2973 			parser.setError (Aborted,"Nothing selected");
  2974 		} 
  2975 		/*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
  2976 		{
  2977 			parser.setError (Aborted,"Type of selection is wrong.");
  2978 		} 
  2979 		*/
  2980 		else if (parser.checkParCount(0))
  2981 		{	
  2982 			deleteSelection();
  2983 		}	
  2984 	/////////////////////////////////////////////////////////////////////
  2985 	} else if (com=="deleteKeepChildren")
  2986 	{
  2987 		if (!selti)
  2988 		{
  2989 			parser.setError (Aborted,"Nothing selected");
  2990 		} else if (! selbi )
  2991 		{
  2992 			parser.setError (Aborted,"Type of selection is not a branch");
  2993 		} else if (parser.checkParCount(0))
  2994 		{	
  2995 			deleteKeepChildren();
  2996 		}	
  2997 	/////////////////////////////////////////////////////////////////////
  2998 	} else if (com=="deleteChildren")
  2999 	{
  3000 		if (!selti)
  3001 		{
  3002 			parser.setError (Aborted,"Nothing selected");
  3003 		} else if (! selbi)
  3004 		{
  3005 			parser.setError (Aborted,"Type of selection is not a branch");
  3006 		} else if (parser.checkParCount(0))
  3007 		{	
  3008 			deleteChildren();
  3009 		}	
  3010 	/////////////////////////////////////////////////////////////////////
  3011 	} else if (com=="exportASCII")
  3012 	{
  3013 		QString fname="";
  3014 		ok=true;
  3015 		if (parser.parCount()>=1)
  3016 			// Hey, we even have a filename
  3017 			fname=parser.parString(ok,0); 
  3018 		if (!ok)
  3019 		{
  3020 			parser.setError (Aborted,"Could not read filename");
  3021 		} else
  3022 		{
  3023 				exportASCII (fname,false);
  3024 		}
  3025 	/////////////////////////////////////////////////////////////////////
  3026 	} else if (com=="exportImage")
  3027 	{
  3028 		QString fname="";
  3029 		ok=true;
  3030 		if (parser.parCount()>=2)
  3031 			// Hey, we even have a filename
  3032 			fname=parser.parString(ok,0); 
  3033 		if (!ok)
  3034 		{
  3035 			parser.setError (Aborted,"Could not read filename");
  3036 		} else
  3037 		{
  3038 			QString format="PNG";
  3039 			if (parser.parCount()>=2)
  3040 			{
  3041 				format=parser.parString(ok,1);
  3042 			}
  3043 			exportImage (fname,false,format);
  3044 		}
  3045 	/////////////////////////////////////////////////////////////////////
  3046 	} else if (com=="exportXHTML")
  3047 	{
  3048 		QString fname="";
  3049 		ok=true;
  3050 		if (parser.parCount()>=2)
  3051 			// Hey, we even have a filename
  3052 			fname=parser.parString(ok,1); 
  3053 		if (!ok)
  3054 		{
  3055 			parser.setError (Aborted,"Could not read filename");
  3056 		} else
  3057 		{
  3058 			exportXHTML (fname,false);
  3059 		}
  3060 	/////////////////////////////////////////////////////////////////////
  3061 	} else if (com=="exportXML")
  3062 	{
  3063 		QString fname="";
  3064 		ok=true;
  3065 		if (parser.parCount()>=2)
  3066 			// Hey, we even have a filename
  3067 			fname=parser.parString(ok,1); 
  3068 		if (!ok)
  3069 		{
  3070 			parser.setError (Aborted,"Could not read filename");
  3071 		} else
  3072 		{
  3073 			exportXML (fname,false);
  3074 		}
  3075 	/////////////////////////////////////////////////////////////////////
  3076 	} else if (com=="importDir")
  3077 	{
  3078 		if (!selti)
  3079 		{
  3080 			parser.setError (Aborted,"Nothing selected");
  3081 		} else if (! selbi )
  3082 		{				  
  3083 			parser.setError (Aborted,"Type of selection is not a branch");
  3084 		} else if (parser.checkParCount(1))
  3085 		{
  3086 			s=parser.parString(ok,0);
  3087 			if (ok) importDirInt(s);
  3088 		}	
  3089 	/////////////////////////////////////////////////////////////////////
  3090 	} else /* FIXME-2 if (com=="linkTo")
  3091 	{
  3092 		if (!selti)
  3093 		{
  3094 			parser.setError (Aborted,"Nothing selected");
  3095 		} else if ( selbi)
  3096 		{
  3097 			if (parser.checkParCount(4))
  3098 			{
  3099 				// 0	selectstring of parent
  3100 				// 1	num in parent (for branches)
  3101 				// 2,3	x,y of mainbranch or mapcenter
  3102 				s=parser.parString(ok,0);
  3103 				LinkableMapObj *dst=findObjBySelect (s);
  3104 				if (dst)
  3105 				{	
  3106 					if (typeid(*dst) == typeid(BranchObj) ) 
  3107 					{
  3108 						// Get number in parent
  3109 						n=parser.parInt (ok,1);
  3110 						if (ok)
  3111 						{
  3112 							selb->linkTo ((BranchObj*)(dst),n);
  3113 							selection.update();
  3114 						}	
  3115 					} else if (typeid(*dst) == typeid(MapCenterObj) ) 
  3116 					{
  3117 						selb->linkTo ((BranchObj*)(dst),-1);
  3118 						// Get coordinates of mainbranch
  3119 						x=parser.parDouble(ok,2);
  3120 						if (ok)
  3121 						{
  3122 							y=parser.parDouble(ok,3);
  3123 							if (ok) 
  3124 							{
  3125 								selbi->move (x,y);
  3126 								selection.update();
  3127 							}
  3128 						}
  3129 					}	
  3130 				}	
  3131 			}	
  3132 		} else if ( selectionType() == TreeItem::Image) 
  3133 		{
  3134 			if (parser.checkParCount(1))
  3135 			{
  3136 				// 0	selectstring of parent
  3137 				s=parser.parString(ok,0);
  3138 				LinkableMapObj *dst=findObjBySelect (s);
  3139 				if (dst)
  3140 				{	
  3141 					if (typeid(*dst) == typeid(BranchObj) ||
  3142 						typeid(*dst) == typeid(MapCenterObj)) 
  3143 						linkFloatImageTo (getSelectString(dst));
  3144 				} else	
  3145 					parser.setError (Aborted,"Destination is not a branch");
  3146 			}		
  3147 		} else
  3148 			parser.setError (Aborted,"Type of selection is not a floatimage or branch");
  3149 	/////////////////////////////////////////////////////////////////////
  3150 	} else */ if (com=="loadImage")
  3151 	{
  3152 		if (!selti)
  3153 		{
  3154 			parser.setError (Aborted,"Nothing selected");
  3155 		} else if (! selbi )
  3156 		{				  
  3157 			parser.setError (Aborted,"Type of selection is not a branch");
  3158 		} else if (parser.checkParCount(1))
  3159 		{
  3160 			s=parser.parString(ok,0);
  3161 			if (ok) loadFloatImageInt (s);
  3162 		}	
  3163 	/////////////////////////////////////////////////////////////////////
  3164 	} else if (com=="moveBranchUp")
  3165 	{
  3166 		if (!selti )
  3167 		{
  3168 			parser.setError (Aborted,"Nothing selected");
  3169 		} else if (! selbi )
  3170 		{				  
  3171 			parser.setError (Aborted,"Type of selection is not a branch");
  3172 		} else if (parser.checkParCount(0))
  3173 		{
  3174 			moveBranchUp();
  3175 		}	
  3176 	/////////////////////////////////////////////////////////////////////
  3177 	} else if (com=="moveBranchDown")
  3178 	{
  3179 		if (!selti )
  3180 		{
  3181 			parser.setError (Aborted,"Nothing selected");
  3182 		} else if (! selbi )
  3183 		{				  
  3184 			parser.setError (Aborted,"Type of selection is not a branch");
  3185 		} else if (parser.checkParCount(0))
  3186 		{
  3187 			moveBranchDown();
  3188 		}	
  3189 	/////////////////////////////////////////////////////////////////////
  3190 	} else if (com=="move")
  3191 	{
  3192 		if (!selti )
  3193 		{
  3194 			parser.setError (Aborted,"Nothing selected");
  3195 		} else if ( selectionType()!=TreeItem::Branch  && 
  3196 					selectionType()!=TreeItem::MapCenter  &&
  3197 					selectionType()!=TreeItem::Image )
  3198 		{				  
  3199 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3200 		} else if (parser.checkParCount(2))
  3201 		{	
  3202 			x=parser.parDouble (ok,0);
  3203 			if (ok)
  3204 			{
  3205 				y=parser.parDouble (ok,1);
  3206 				if (ok) move (x,y);
  3207 			}
  3208 		}	
  3209 	/////////////////////////////////////////////////////////////////////
  3210 	} else if (com=="moveRel")
  3211 	{
  3212 		if (!selti )
  3213 		{
  3214 			parser.setError (Aborted,"Nothing selected");
  3215 		} else if ( selectionType()!=TreeItem::Branch  && 
  3216 					selectionType()!=TreeItem::MapCenter  &&
  3217 					selectionType()!=TreeItem::Image )
  3218 		{				  
  3219 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3220 		} else if (parser.checkParCount(2))
  3221 		{	
  3222 			x=parser.parDouble (ok,0);
  3223 			if (ok)
  3224 			{
  3225 				y=parser.parDouble (ok,1);
  3226 				if (ok) moveRel (x,y);
  3227 			}
  3228 		}	
  3229 	/////////////////////////////////////////////////////////////////////
  3230 	} else if (com=="nop")
  3231 	{
  3232 	/////////////////////////////////////////////////////////////////////
  3233 	} else if (com=="paste")
  3234 	{
  3235 		if (!selti )
  3236 		{
  3237 			parser.setError (Aborted,"Nothing selected");
  3238 		} else if (! selbi )
  3239 		{				  
  3240 			parser.setError (Aborted,"Type of selection is not a branch");
  3241 		} else if (parser.checkParCount(1))
  3242 		{	
  3243 			n=parser.parInt (ok,0);
  3244 			if (ok) pasteNoSave(n);
  3245 		}	
  3246 	/////////////////////////////////////////////////////////////////////
  3247 	} else if (com=="qa")
  3248 	{
  3249 		if (!selti )
  3250 		{
  3251 			parser.setError (Aborted,"Nothing selected");
  3252 		} else if (! selbi )
  3253 		{				  
  3254 			parser.setError (Aborted,"Type of selection is not a branch");
  3255 		} else if (parser.checkParCount(4))
  3256 		{	
  3257 			QString c,u;
  3258 			c=parser.parString (ok,0);
  3259 			if (!ok)
  3260 			{
  3261 				parser.setError (Aborted,"No comment given");
  3262 			} else
  3263 			{
  3264 				s=parser.parString (ok,1);
  3265 				if (!ok)
  3266 				{
  3267 					parser.setError (Aborted,"First parameter is not a string");
  3268 				} else
  3269 				{
  3270 					t=parser.parString (ok,2);
  3271 					if (!ok)
  3272 					{
  3273 						parser.setError (Aborted,"Condition is not a string");
  3274 					} else
  3275 					{
  3276 						u=parser.parString (ok,3);
  3277 						if (!ok)
  3278 						{
  3279 							parser.setError (Aborted,"Third parameter is not a string");
  3280 						} else
  3281 						{
  3282 							if (s!="heading")
  3283 							{
  3284 								parser.setError (Aborted,"Unknown type: "+s);
  3285 							} else
  3286 							{
  3287 								if (! (t=="eq") ) 
  3288 								{
  3289 									parser.setError (Aborted,"Unknown operator: "+t);
  3290 								} else
  3291 								{
  3292 									if (! selbi    )
  3293 									{
  3294 										parser.setError (Aborted,"Type of selection is not a branch");
  3295 									} else
  3296 									{
  3297 										if (selbi->getHeading() == u)
  3298 										{
  3299 											cout << "PASSED: " << qPrintable (c)  << endl;
  3300 										} else
  3301 										{
  3302 											cout << "FAILED: " << qPrintable (c)  << endl;
  3303 										}
  3304 									}
  3305 								}
  3306 							}
  3307 						} 
  3308 					} 
  3309 				} 
  3310 			}
  3311 		}	
  3312 	/////////////////////////////////////////////////////////////////////
  3313 	} else if (com=="saveImage")
  3314 	{
  3315 		FloatImageObj *fio=selection.getFloatImage();
  3316 		if (!fio)
  3317 		{
  3318 			parser.setError (Aborted,"Type of selection is not an image");
  3319 		} else if (parser.checkParCount(2))
  3320 		{
  3321 			s=parser.parString(ok,0);
  3322 			if (ok)
  3323 			{
  3324 				t=parser.parString(ok,1);
  3325 				if (ok) saveFloatImageInt (fio,t,s);
  3326 			}
  3327 		}	
  3328 	/////////////////////////////////////////////////////////////////////
  3329 	} else if (com=="scroll")
  3330 	{
  3331 		if (!selti)
  3332 		{
  3333 			parser.setError (Aborted,"Nothing selected");
  3334 		} else if (! selbi )
  3335 		{				  
  3336 			parser.setError (Aborted,"Type of selection is not a branch");
  3337 		} else if (parser.checkParCount(0))
  3338 		{	
  3339 			if (!scrollBranch (selbi))	
  3340 				parser.setError (Aborted,"Could not scroll branch");
  3341 		}	
  3342 	/////////////////////////////////////////////////////////////////////
  3343 	} else if (com=="select")
  3344 	{
  3345 		if (parser.checkParCount(1))
  3346 		{
  3347 			s=parser.parString(ok,0);
  3348 			if (ok) select (s);
  3349 		}	
  3350 	/////////////////////////////////////////////////////////////////////
  3351 	} else if (com=="selectLastBranch")
  3352 	{
  3353 		if (!selti )
  3354 		{
  3355 			parser.setError (Aborted,"Nothing selected");
  3356 		} else if (! selbi )
  3357 		{				  
  3358 			parser.setError (Aborted,"Type of selection is not a branch");
  3359 		} else if (parser.checkParCount(0))
  3360 		{	
  3361 			BranchItem *bi=selbi->getLastBranch();
  3362 			if (!bi)
  3363 				parser.setError (Aborted,"Could not select last branch");
  3364 			select (bi);		// FIXME-3 was selectInt
  3365 				
  3366 		}	
  3367 	/////////////////////////////////////////////////////////////////////
  3368 	} else /* FIXME-2 if (com=="selectLastImage")
  3369 	{
  3370 		if (!selti )
  3371 		{
  3372 			parser.setError (Aborted,"Nothing selected");
  3373 		} else if (! selbi )
  3374 		{				  
  3375 			parser.setError (Aborted,"Type of selection is not a branch");
  3376 		} else if (parser.checkParCount(0))
  3377 		{	
  3378 			FloatImageObj *fio=selb->getLastFloatImage();
  3379 			if (!fio)
  3380 				parser.setError (Aborted,"Could not select last image");
  3381 			select (fio);		// FIXME-3 was selectInt
  3382 				
  3383 		}	
  3384 	/////////////////////////////////////////////////////////////////////
  3385 	} else */ if (com=="selectLatestAdded")
  3386 	{
  3387 		if (latestSelectionString.isEmpty() )
  3388 		{
  3389 			parser.setError (Aborted,"No latest added object");
  3390 		} else
  3391 		{	
  3392 			if (!select (latestSelectionString))
  3393 				parser.setError (Aborted,"Could not select latest added object "+latestSelectionString);
  3394 		}	
  3395 	/////////////////////////////////////////////////////////////////////
  3396 	} else if (com=="setFrameType")
  3397 	{
  3398 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3399 		{
  3400 			parser.setError (Aborted,"Type of selection does not allow setting frame type");
  3401 		}
  3402 		else if (parser.checkParCount(1))
  3403 		{
  3404 			s=parser.parString(ok,0);
  3405 			if (ok) setFrameType (s);
  3406 		}	
  3407 	/////////////////////////////////////////////////////////////////////
  3408 	} else if (com=="setFramePenColor")
  3409 	{
  3410 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3411 		{
  3412 			parser.setError (Aborted,"Type of selection does not allow setting of pen color");
  3413 		}
  3414 		else if (parser.checkParCount(1))
  3415 		{
  3416 			QColor c=parser.parColor(ok,0);
  3417 			if (ok) setFramePenColor (c);
  3418 		}	
  3419 	/////////////////////////////////////////////////////////////////////
  3420 	} else if (com=="setFrameBrushColor")
  3421 	{
  3422 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3423 		{
  3424 			parser.setError (Aborted,"Type of selection does not allow setting brush color");
  3425 		}
  3426 		else if (parser.checkParCount(1))
  3427 		{
  3428 			QColor c=parser.parColor(ok,0);
  3429 			if (ok) setFrameBrushColor (c);
  3430 		}	
  3431 	/////////////////////////////////////////////////////////////////////
  3432 	} else if (com=="setFramePadding")
  3433 	{
  3434 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3435 		{
  3436 			parser.setError (Aborted,"Type of selection does not allow setting frame padding");
  3437 		}
  3438 		else if (parser.checkParCount(1))
  3439 		{
  3440 			n=parser.parInt(ok,0);
  3441 			if (ok) setFramePadding(n);
  3442 		}	
  3443 	/////////////////////////////////////////////////////////////////////
  3444 	} else if (com=="setFrameBorderWidth")
  3445 	{
  3446 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3447 		{
  3448 			parser.setError (Aborted,"Type of selection does not allow setting frame border width");
  3449 		}
  3450 		else if (parser.checkParCount(1))
  3451 		{
  3452 			n=parser.parInt(ok,0);
  3453 			if (ok) setFrameBorderWidth (n);
  3454 		}	
  3455 	/////////////////////////////////////////////////////////////////////
  3456 	} else if (com=="setMapAuthor")
  3457 	{
  3458 		if (parser.checkParCount(1))
  3459 		{
  3460 			s=parser.parString(ok,0);
  3461 			if (ok) setAuthor (s);
  3462 		}	
  3463 	/////////////////////////////////////////////////////////////////////
  3464 	} else if (com=="setMapComment")
  3465 	{
  3466 		if (parser.checkParCount(1))
  3467 		{
  3468 			s=parser.parString(ok,0);
  3469 			if (ok) setComment(s);
  3470 		}	
  3471 	/////////////////////////////////////////////////////////////////////
  3472 	} else if (com=="setMapBackgroundColor")
  3473 	{
  3474 		if (!selti )
  3475 		{
  3476 			parser.setError (Aborted,"Nothing selected");
  3477 		} else if (! selbi )
  3478 		{				  
  3479 			parser.setError (Aborted,"Type of selection is not a branch");
  3480 		} else if (parser.checkParCount(1))
  3481 		{
  3482 			QColor c=parser.parColor (ok,0);
  3483 			if (ok) setMapBackgroundColor (c);
  3484 		}	
  3485 	/////////////////////////////////////////////////////////////////////
  3486 	} else if (com=="setMapDefLinkColor")
  3487 	{
  3488 		if (!selti )
  3489 		{
  3490 			parser.setError (Aborted,"Nothing selected");
  3491 		} else if (! selbi )
  3492 		{				  
  3493 			parser.setError (Aborted,"Type of selection is not a branch");
  3494 		} else if (parser.checkParCount(1))
  3495 		{
  3496 			QColor c=parser.parColor (ok,0);
  3497 			if (ok) setMapDefLinkColor (c);
  3498 		}	
  3499 	/////////////////////////////////////////////////////////////////////
  3500 	} else if (com=="setMapLinkStyle")
  3501 	{
  3502 		if (parser.checkParCount(1))
  3503 		{
  3504 			s=parser.parString (ok,0);
  3505 			if (ok) setMapLinkStyle(s);
  3506 		}	
  3507 	/////////////////////////////////////////////////////////////////////
  3508 	} else if (com=="setHeading")
  3509 	{
  3510 		if (!selti )
  3511 		{
  3512 			parser.setError (Aborted,"Nothing selected");
  3513 		} else if (! selbi )
  3514 		{				  
  3515 			parser.setError (Aborted,"Type of selection is not a branch");
  3516 		} else if (parser.checkParCount(1))
  3517 		{
  3518 			s=parser.parString (ok,0);
  3519 			if (ok) 
  3520 				setHeading (s);
  3521 		}	
  3522 	/////////////////////////////////////////////////////////////////////
  3523 	} else if (com=="setHideExport")
  3524 	{
  3525 		if (!selti )
  3526 		{
  3527 			parser.setError (Aborted,"Nothing selected");
  3528 		} else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
  3529 		{				  
  3530 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3531 		} else if (parser.checkParCount(1))
  3532 		{
  3533 			b=parser.parBool(ok,0);
  3534 			if (ok) setHideExport (b);
  3535 		}
  3536 	/////////////////////////////////////////////////////////////////////
  3537 	} else if (com=="setIncludeImagesHorizontally")
  3538 	{ 
  3539 		if (!selti )
  3540 		{
  3541 			parser.setError (Aborted,"Nothing selected");
  3542 		} else if (! selbi)
  3543 		{				  
  3544 			parser.setError (Aborted,"Type of selection is not a branch");
  3545 		} else if (parser.checkParCount(1))
  3546 		{
  3547 			b=parser.parBool(ok,0);
  3548 			if (ok) setIncludeImagesHor(b);
  3549 		}
  3550 	/////////////////////////////////////////////////////////////////////
  3551 	} else if (com=="setIncludeImagesVertically")
  3552 	{
  3553 		if (!selti )
  3554 		{
  3555 			parser.setError (Aborted,"Nothing selected");
  3556 		} else if (! selbi)
  3557 		{				  
  3558 			parser.setError (Aborted,"Type of selection is not a branch");
  3559 		} else if (parser.checkParCount(1))
  3560 		{
  3561 			b=parser.parBool(ok,0);
  3562 			if (ok) setIncludeImagesVer(b);
  3563 		}
  3564 	/////////////////////////////////////////////////////////////////////
  3565 	} else if (com=="setHideLinkUnselected")
  3566 	{
  3567 		if (!selti )
  3568 		{
  3569 			parser.setError (Aborted,"Nothing selected");
  3570 		} else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3571 		{				  
  3572 			parser.setError (Aborted,"Type of selection does not allow hiding the link");
  3573 		} else if (parser.checkParCount(1))
  3574 		{
  3575 			b=parser.parBool(ok,0);
  3576 			if (ok) setHideLinkUnselected(b);
  3577 		}
  3578 	/////////////////////////////////////////////////////////////////////
  3579 	} else if (com=="setSelectionColor")
  3580 	{
  3581 		if (parser.checkParCount(1))
  3582 		{
  3583 			QColor c=parser.parColor (ok,0);
  3584 			if (ok) setSelectionColorInt (c);
  3585 		}	
  3586 	/////////////////////////////////////////////////////////////////////
  3587 	} else if (com=="setURL")
  3588 	{
  3589 		if (!selti )
  3590 		{
  3591 			parser.setError (Aborted,"Nothing selected");
  3592 		} else if (! selbi )
  3593 		{				  
  3594 			parser.setError (Aborted,"Type of selection is not a branch");
  3595 		} else if (parser.checkParCount(1))
  3596 		{
  3597 			s=parser.parString (ok,0);
  3598 			if (ok) setURL(s);
  3599 		}	
  3600 	/////////////////////////////////////////////////////////////////////
  3601 	} else if (com=="setVymLink")
  3602 	{
  3603 		if (!selti )
  3604 		{
  3605 			parser.setError (Aborted,"Nothing selected");
  3606 		} else if (! selbi )
  3607 		{				  
  3608 			parser.setError (Aborted,"Type of selection is not a branch");
  3609 		} else if (parser.checkParCount(1))
  3610 		{
  3611 			s=parser.parString (ok,0);
  3612 			if (ok) setVymLink(s);
  3613 		}	
  3614 	}
  3615 	/////////////////////////////////////////////////////////////////////
  3616 	else /* FIXME-2 if (com=="setFlag")
  3617 	{
  3618 		if (!selti )
  3619 		{
  3620 			parser.setError (Aborted,"Nothing selected");
  3621 		} else if (! selbi )
  3622 		{				  
  3623 			parser.setError (Aborted,"Type of selection is not a branch");
  3624 		} else if (parser.checkParCount(1))
  3625 		{
  3626 			s=parser.parString(ok,0);
  3627 			if (ok) 
  3628 			{
  3629 				selb->activateStandardFlag(s);
  3630 				selb->updateFlagsToolbar();
  3631 			}	
  3632 		}
  3633 	/////////////////////////////////////////////////////////////////////
  3634 	} else */ /* FIXME-2 if (com=="setFrameType")
  3635 	{
  3636 		if (!selti )
  3637 		{
  3638 			parser.setError (Aborted,"Nothing selected");
  3639 		} else if (! selb )
  3640 		{				  
  3641 			parser.setError (Aborted,"Type of selection is not a branch");
  3642 		} else if (parser.checkParCount(1))
  3643 		{
  3644 			s=parser.parString(ok,0);
  3645 			if (ok) 
  3646 				setFrameType (s);
  3647 		}
  3648 	/////////////////////////////////////////////////////////////////////
  3649 	} else*/ if (com=="sortChildren")
  3650 	{
  3651 		if (!selti )
  3652 		{
  3653 			parser.setError (Aborted,"Nothing selected");
  3654 		} else if (! selbi )
  3655 		{				  
  3656 			parser.setError (Aborted,"Type of selection is not a branch");
  3657 		} else if (parser.checkParCount(0))
  3658 		{
  3659 			sortChildren();
  3660 		}
  3661 	/////////////////////////////////////////////////////////////////////
  3662 	} else /* FIXME-2 if (com=="toggleFlag")
  3663 	{
  3664 		if (!selti )
  3665 		{
  3666 			parser.setError (Aborted,"Nothing selected");
  3667 		} else if (! selbi )
  3668 		{				  
  3669 			parser.setError (Aborted,"Type of selection is not a branch");
  3670 		} else if (parser.checkParCount(1))
  3671 		{
  3672 			s=parser.parString(ok,0);
  3673 			if (ok) 
  3674 			{
  3675 				selbi->toggleStandardFlag(s);	
  3676 				selb->updateFlagsToolbar();
  3677 			}	
  3678 		}
  3679 	/////////////////////////////////////////////////////////////////////
  3680 	} else */ if (com=="unscroll")
  3681 	{
  3682 		if (!selti)
  3683 		{
  3684 			parser.setError (Aborted,"Nothing selected");
  3685 		} else if (! selbi )
  3686 		{				  
  3687 			parser.setError (Aborted,"Type of selection is not a branch");
  3688 		} else if (parser.checkParCount(0))
  3689 		{	
  3690 			if (!unscrollBranch (selbi))	
  3691 				parser.setError (Aborted,"Could not unscroll branch");
  3692 		}	
  3693 	/////////////////////////////////////////////////////////////////////
  3694 	} else if (com=="unscrollChildren")
  3695 	{
  3696 		if (!selti)
  3697 		{
  3698 			parser.setError (Aborted,"Nothing selected");
  3699 		} else if (! selbi )
  3700 		{				  
  3701 			parser.setError (Aborted,"Type of selection is not a branch");
  3702 		} else if (parser.checkParCount(0))
  3703 		{	
  3704 			unscrollChildren ();
  3705 		}	
  3706 	/////////////////////////////////////////////////////////////////////
  3707 	} else /* FIXME-2 if (com=="unsetFlag")
  3708 	{
  3709 		if (selection.isEmpty() )
  3710 		{
  3711 			parser.setError (Aborted,"Nothing selected");
  3712 		} else if (! selbi )
  3713 		{				  
  3714 			parser.setError (Aborted,"Type of selection is not a branch");
  3715 		} else if (parser.checkParCount(1))
  3716 		{
  3717 			s=parser.parString(ok,0);
  3718 			if (ok) 
  3719 			{
  3720 				selb->deactivateStandardFlag(s);
  3721 				selb->updateFlagsToolbar();
  3722 			}	
  3723 		}
  3724 	} else */
  3725 		parser.setError (Aborted,"Unknown command");
  3726 
  3727 	// Any errors?
  3728 	if (parser.errorLevel()==NoError)
  3729 	{
  3730 		// setChanged();  FIXME-2 should not be called e.g. for export?!
  3731 		reposition();
  3732 	}	
  3733 	else	
  3734 	{
  3735 		// TODO Error handling
  3736 		qWarning("VymModel::parseAtom: Error!");
  3737 		qWarning(parser.errorMessage());
  3738 	} 
  3739 }
  3740 
  3741 void VymModel::runScript (QString script)
  3742 {
  3743 	parser.setScript (script);
  3744 	parser.runScript();
  3745 	while (parser.next() ) 
  3746 		parseAtom(parser.getAtom());
  3747 }
  3748 
  3749 void VymModel::setExportMode (bool b)
  3750 {
  3751 	// should be called before and after exports
  3752 	// depending on the settings
  3753 	if (b && settings.value("/export/useHideExport","true")=="true")
  3754 		setHideTmpMode (TreeItem::HideExport);
  3755 	else	
  3756 		setHideTmpMode (TreeItem::HideNone);
  3757 }
  3758 
  3759 void VymModel::exportImage(QString fname, bool askName, QString format)
  3760 {
  3761 	if (fname=="")
  3762 	{
  3763 		fname=getMapName()+".png";
  3764 		format="PNG";
  3765 	} 	
  3766 
  3767 	if (askName)
  3768 	{
  3769 		QStringList fl;
  3770 		QFileDialog *fd=new QFileDialog (NULL);
  3771 		fd->setCaption (tr("Export map as image"));
  3772 		fd->setDirectory (lastImageDir);
  3773 		fd->setFileMode(QFileDialog::AnyFile);
  3774 		fd->setFilters  (imageIO.getFilters() );
  3775 		if (fd->exec())
  3776 		{
  3777 			fl=fd->selectedFiles();
  3778 			fname=fl.first();
  3779 			format=imageIO.getType(fd->selectedFilter());
  3780 		} 
  3781 	}
  3782 
  3783 	setExportMode (true);
  3784 	QPixmap pix (getPixmap());
  3785 	pix.save(fname, format);
  3786 	setExportMode (false);
  3787 }
  3788 
  3789 
  3790 void VymModel::exportXML(QString dir, bool askForName)
  3791 {
  3792 	if (askForName)
  3793 	{
  3794 		dir=browseDirectory(NULL,tr("Export XML to directory"));
  3795 		if (dir =="" && !reallyWriteDirectory(dir) )
  3796 		return;
  3797 	}
  3798 
  3799 	// Hide stuff during export, if settings want this
  3800 	setExportMode (true);
  3801 
  3802 	// Create subdirectories
  3803 	makeSubDirs (dir);
  3804 
  3805 	// write to directory
  3806 	QString saveFile=saveToDir (dir,mapName+"-",true,getTotalBBox().topLeft() ,NULL);
  3807 	QFile file;
  3808 
  3809 	file.setName ( dir + "/"+mapName+".xml");
  3810 	if ( !file.open( QIODevice::WriteOnly ) )
  3811 	{
  3812 		// This should neverever happen
  3813 		QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
  3814 		return;
  3815 	}	
  3816 
  3817 	// Write it finally, and write in UTF8, no matter what 
  3818 	QTextStream ts( &file );
  3819 	ts.setEncoding (QTextStream::UnicodeUTF8);
  3820 	ts << saveFile;
  3821 	file.close();
  3822 
  3823 	// Now write image, too
  3824 	exportImage (dir+"/images/"+mapName+".png",false,"PNG");
  3825 
  3826 	setExportMode (false);
  3827 }
  3828 
  3829 void VymModel::exportASCII(QString fname,bool askName)
  3830 {
  3831 	ExportASCII ex;
  3832 	ex.setModel (this);
  3833 	if (fname=="") 
  3834 		ex.setFile (mapName+".txt");	
  3835 	else
  3836 		ex.setFile (fname);
  3837 
  3838 	if (askName)
  3839 	{
  3840 		//ex.addFilter ("TXT (*.txt)");
  3841 		ex.setDir(lastImageDir);
  3842 		//ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
  3843 		ex.execDialog() ; 
  3844 	} 
  3845 	if (!ex.canceled())
  3846 	{
  3847 		setExportMode(true);
  3848 		ex.doExport();
  3849 		setExportMode(false);
  3850 	}
  3851 }
  3852 
  3853 void VymModel::exportXHTML (const QString &dir, bool askForName)
  3854 {
  3855 			ExportXHTMLDialog dia(NULL);
  3856 			dia.setFilePath (filePath );
  3857 			dia.setMapName (mapName );
  3858 			dia.readSettings();
  3859 			if (dir!="") dia.setDir (dir);
  3860 
  3861 			bool ok=true;
  3862 			
  3863 			if (askForName)
  3864 			{
  3865 				if (dia.exec()!=QDialog::Accepted) 
  3866 					ok=false;
  3867 				else	
  3868 				{
  3869 					QDir d (dia.getDir());
  3870 					// Check, if warnings should be used before overwriting
  3871 					// the output directory
  3872 					if (d.exists() && d.count()>0)
  3873 					{
  3874 						WarningDialog warn;
  3875 						warn.showCancelButton (true);
  3876 						warn.setText(QString(
  3877 							"The directory %1 is not empty.\n"
  3878 							"Do you risk to overwrite some of its contents?").arg(d.path() ));
  3879 						warn.setCaption("Warning: Directory not empty");
  3880 						warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
  3881 
  3882 						if (warn.exec()!=QDialog::Accepted) ok=false;
  3883 					}
  3884 				}	
  3885 			}
  3886 
  3887 			if (ok)
  3888 			{
  3889 				exportXML (dia.getDir(),false );
  3890 				dia.doExport(mapName );
  3891 				//if (dia.hasChanged()) setChanged();
  3892 			}
  3893 }
  3894 
  3895 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
  3896 {
  3897 	ExportOO ex;
  3898 	ex.setFile (fn);
  3899 	ex.setModel (this);
  3900 	if (ex.setConfigFile(cf)) 
  3901 	{
  3902 		setExportMode (true);
  3903 		ex.exportPresentation();
  3904 		setExportMode (false);
  3905 	}
  3906 }
  3907 
  3908 
  3909 
  3910 
  3911 //////////////////////////////////////////////
  3912 // View related
  3913 //////////////////////////////////////////////
  3914 
  3915 void VymModel::registerEditor(QWidget *me)
  3916 {
  3917 	mapEditor=(MapEditor*)me;
  3918 	/* FIXME-3
  3919 	for (int i=0; i<rootItem->branchCount(); i++)
  3920 		rootItem->getBranchNum(i)->setMapEditor(mapEditor);
  3921 	*/
  3922 }
  3923 
  3924 void VymModel::unregisterEditor(QWidget *)
  3925 {
  3926 	mapEditor=NULL;
  3927 }
  3928 
  3929 void VymModel::setContextPos(QPointF p)
  3930 {
  3931 	contextPos=p;
  3932 }
  3933 
  3934 void VymModel::unsetContextPos()
  3935 {
  3936 	contextPos=QPointF();
  3937 }
  3938 
  3939 void VymModel::updateNoteFlag()
  3940 {
  3941 	setChanged();
  3942 	cout << "VM::updateNoteFlag()\n";
  3943 	/* FIXME-1 modify note flag
  3944 	BranchObj *bo=getSelectedBranch();
  3945 	if (bo) 
  3946 	{
  3947 		bo->updateNoteFlag();
  3948 		mainWindow->updateActions();
  3949 	}	
  3950 	*/
  3951 }
  3952 
  3953 void VymModel::updateRelPositions()		//FIXME-2 VM should have no need to updateRelPos
  3954 {
  3955 	cout << "VM::updateRelPos...\n";
  3956 	for (int i=0; i<rootItem->branchCount(); i++)
  3957 		((MapCenterObj*)rootItem->getBranchObjNum(i))->updateRelPositions();
  3958 }
  3959 
  3960 void VymModel::reposition()	//FIXME-2 VM should have no need to reposition, this is done in views???
  3961 {
  3962 	cout << "VM::reposition ...\n";
  3963 	for (int i=0;i<rootItem->branchCount(); i++)
  3964 		rootItem->getBranchObjNum(i)->reposition();	//	for positioning heading
  3965 }
  3966 
  3967 QPolygonF VymModel::shape(BranchObj *bo)
  3968 {
  3969 	// Creating (arbitrary) shapes
  3970 
  3971 	QPolygonF p;
  3972 	QRectF rb=bo->getBBox();
  3973 	if (bo->getDepth()==0)
  3974 	{
  3975 		// Just take BBox of this mapCenter
  3976 		p<<rb.topLeft()<<rb.topRight()<<rb.bottomRight()<<rb.bottomLeft();
  3977 		return p;
  3978 	}
  3979 
  3980 	// Take union of BBox and TotalBBox 
  3981 
  3982 	QRectF ra=bo->getTotalBBox();
  3983 	if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
  3984 		p   <<ra.bottomLeft()
  3985 			<<ra.topLeft()
  3986 			<<QPointF (rb.topLeft().x(), ra.topLeft().y() )
  3987 			<<rb.topRight()
  3988 			<<rb.bottomRight()
  3989 			<<QPointF (rb.bottomLeft().x(), ra.bottomLeft().y() ) ;
  3990 	else		
  3991 		p   <<ra.bottomRight()
  3992 			<<ra.topRight()
  3993 			<<QPointF (rb.topRight().x(), ra.topRight().y() )
  3994 			<<rb.topLeft()
  3995 			<<rb.bottomLeft()
  3996 			<<QPointF (rb.bottomRight().x(), ra.bottomRight().y() ) ;
  3997 	return p;		
  3998 
  3999 }
  4000 
  4001 void VymModel::moveAway(LinkableMapObj *lmo)	//FIXME-5
  4002 {
  4003 	// Autolayout:
  4004 	//
  4005 	// Move all branches and MapCenters away from lmo 
  4006 	// to avoid collisions 
  4007 
  4008 		/*
  4009 	QPolygonF pA;
  4010 	QPolygonF pB;
  4011 
  4012 	BranchObj *boA=(BranchObj*)lmo;
  4013 	BranchObj *boB;
  4014 	for (int i=0; i<rootItem->branchCount(); i++)
  4015 	{
  4016 		boB=rootItem->getBranchNum(i);
  4017 		pA=shape (boA);
  4018 		pB=shape (boB);
  4019 		PolygonCollisionResult r = PolygonCollision(pA, pB, QPoint(0,0));
  4020 		cout <<"------->"
  4021 			<<"="<<r.intersect
  4022 			<<"  ("<<qPrintable(boA->getHeading() )<<")"
  4023 			<<"  with ("<< qPrintable (boB->getHeading() )
  4024 			<<")  willIntersect"
  4025 			<<r.willIntersect 
  4026 			<<"  minT="<<r.minTranslation<<endl<<endl;
  4027 	}
  4028 			*/
  4029 }
  4030 
  4031 QPixmap VymModel::getPixmap()
  4032 {
  4033 	QRectF mapRect=getTotalBBox();
  4034 	QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
  4035 	QPainter pp (&pix);
  4036 	
  4037 	pp.setRenderHints(mapEditor->renderHints());
  4038 
  4039 	// Don't print the visualisation of selection
  4040 	selection.unselect();
  4041 
  4042 	mapScene->render (	&pp, 
  4043 		QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
  4044 		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
  4045 
  4046 	// Restore selection
  4047 	selection.reselect();
  4048 	
  4049 	return pix;
  4050 }
  4051 
  4052 
  4053 void VymModel::setMapLinkStyle (const QString & s)
  4054 {
  4055 	QString snow;
  4056 	switch (linkstyle)
  4057 	{
  4058 		case LinkableMapObj::Line :
  4059 			snow="StyleLine";
  4060 			break;
  4061 		case LinkableMapObj::Parabel:
  4062 			snow="StyleParabel";
  4063 			break;
  4064 		case LinkableMapObj::PolyLine:
  4065 			snow="StylePolyLine";
  4066 			break;
  4067 		case LinkableMapObj::PolyParabel:
  4068 			snow="StylePolyParabel";
  4069 			break;
  4070 		default:	
  4071 			snow="UndefinedStyle";
  4072 			break;
  4073 	}
  4074 
  4075 	saveState (
  4076 		QString("setMapLinkStyle (\"%1\")").arg(s),
  4077 		QString("setMapLinkStyle (\"%1\")").arg(snow),
  4078 		QString("Set map link style (\"%1\")").arg(s)
  4079 	);	
  4080 
  4081 	if (s=="StyleLine")
  4082 		linkstyle=LinkableMapObj::Line;
  4083 	else if (s=="StyleParabel")
  4084 		linkstyle=LinkableMapObj::Parabel;
  4085 	else if (s=="StylePolyLine")
  4086 		linkstyle=LinkableMapObj::PolyLine;
  4087 	else if (s=="StylePolyParabel")	
  4088 		linkstyle=LinkableMapObj::PolyParabel;
  4089 	else
  4090 		linkstyle=LinkableMapObj::UndefinedStyle;
  4091 
  4092 	BranchItem *cur=NULL;
  4093 	BranchItem *prev=NULL;
  4094 	int d=0;
  4095 	BranchObj *bo;
  4096 	next (cur,prev,d);
  4097 	while (cur) 
  4098 	{
  4099 		bo=(BranchObj*)(cur->getLMO() );
  4100 		bo->setLinkStyle(bo->getDefLinkStyle());
  4101 		cur=next(cur,prev,d);
  4102 	}
  4103 	reposition();
  4104 }
  4105 
  4106 LinkableMapObj::Style VymModel::getMapLinkStyle ()
  4107 {
  4108 	return linkstyle;
  4109 }	
  4110 
  4111 void VymModel::setMapDefLinkColor(QColor col)
  4112 {
  4113 	if ( !col.isValid() ) return;
  4114 	saveState (
  4115 		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
  4116 		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
  4117 		QString("Set map link color to %1").arg(col.name())
  4118 	);
  4119 
  4120 	defLinkColor=col;
  4121 	BranchItem *cur=NULL;
  4122 	BranchItem *prev=NULL;
  4123 	int d=0;
  4124 	BranchObj *bo;
  4125 	cur=next(cur,prev,d);
  4126 	while (cur) 
  4127 	{
  4128 		bo=(BranchObj*)(cur->getLMO() );
  4129 		bo->setLinkColor();
  4130 		next(cur,prev,d);
  4131 	}
  4132 	updateActions();
  4133 }
  4134 
  4135 void VymModel::setMapLinkColorHintInt()
  4136 {
  4137 	// called from setMapLinkColorHint(lch) or at end of parse
  4138 	BranchItem *cur=NULL;
  4139 	BranchItem *prev=NULL;
  4140 	int d=0;
  4141 	BranchObj *bo;
  4142 	cur=next(cur,prev,d);
  4143 	while (cur) 
  4144 	{
  4145 		bo=(BranchObj*)(cur->getLMO() );
  4146 		bo->setLinkColor();
  4147 		cur=next(cur,prev,d);
  4148 	}
  4149 }
  4150 
  4151 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
  4152 {
  4153 	linkcolorhint=lch;
  4154 	setMapLinkColorHintInt();
  4155 }
  4156 
  4157 void VymModel::toggleMapLinkColorHint()
  4158 {
  4159 	if (linkcolorhint==LinkableMapObj::HeadingColor)
  4160 		linkcolorhint=LinkableMapObj::DefaultColor;
  4161 	else	
  4162 		linkcolorhint=LinkableMapObj::HeadingColor;
  4163 	BranchItem *cur=NULL;
  4164 	BranchItem *prev=NULL;
  4165 	int d=0;
  4166 	BranchObj *bo;
  4167 	cur=next(cur,prev,d);
  4168 	while (cur) 
  4169 	{
  4170 		bo=(BranchObj*)(cur->getLMO() );
  4171 		bo->setLinkColor();
  4172 		next(cur,prev,d);
  4173 	}
  4174 }
  4175 
  4176 void VymModel::selectMapBackgroundImage ()	// FIXME-2 move to ME
  4177 {
  4178 	Q3FileDialog *fd=new Q3FileDialog( NULL);
  4179 	fd->setMode (Q3FileDialog::ExistingFile);
  4180 	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  4181 	ImagePreview *p =new ImagePreview (fd);
  4182 	fd->setContentsPreviewEnabled( TRUE );
  4183 	fd->setContentsPreview( p, p );
  4184 	fd->setPreviewMode( Q3FileDialog::Contents );
  4185 	fd->setCaption(vymName+" - " +tr("Load background image"));
  4186 	fd->setDir (lastImageDir);
  4187 	fd->show();
  4188 
  4189 	if ( fd->exec() == QDialog::Accepted )
  4190 	{
  4191 		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
  4192 		lastImageDir=QDir (fd->dirPath());
  4193 		setMapBackgroundImage (fd->selectedFile());
  4194 	}
  4195 }	
  4196 
  4197 void VymModel::setMapBackgroundImage (const QString &fn)	//FIXME-2 missing savestate
  4198 {
  4199 	QColor oldcol=mapScene->backgroundBrush().color();
  4200 	/*
  4201 	saveState(
  4202 		selection,
  4203 		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
  4204 		selection,
  4205 		QString ("setMapBackgroundImage (%1)").arg(col.name()),
  4206 		QString("Set background color of map to %1").arg(col.name()));
  4207 	*/	
  4208 	QBrush brush;
  4209 	brush.setTextureImage (QPixmap (fn));
  4210 	mapScene->setBackgroundBrush(brush);
  4211 }
  4212 
  4213 void VymModel::selectMapBackgroundColor()	// FIXME-3 move to ME
  4214 {
  4215 	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
  4216 	if ( !col.isValid() ) return;
  4217 	setMapBackgroundColor( col );
  4218 }
  4219 
  4220 
  4221 void VymModel::setMapBackgroundColor(QColor col)	// FIXME-3 move to ME
  4222 {
  4223 	QColor oldcol=mapScene->backgroundBrush().color();
  4224 	saveState(
  4225 		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
  4226 		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
  4227 		QString("Set background color of map to %1").arg(col.name()));
  4228 	mapScene->setBackgroundBrush(col);
  4229 }
  4230 
  4231 QColor VymModel::getMapBackgroundColor()	// FIXME-3 move to ME
  4232 {
  4233     return mapScene->backgroundBrush().color();
  4234 }
  4235 
  4236 
  4237 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()	// FIXME-3 move to ME
  4238 {
  4239 	return linkcolorhint;
  4240 }
  4241 
  4242 QColor VymModel::getMapDefLinkColor()	// FIXME-3 move to ME
  4243 {
  4244 	return defLinkColor;
  4245 }
  4246 
  4247 void VymModel::setMapDefXLinkColor(QColor col)	// FIXME-3 move to ME
  4248 {
  4249 	defXLinkColor=col;
  4250 }
  4251 
  4252 QColor VymModel::getMapDefXLinkColor()	// FIXME-3 move to ME
  4253 {
  4254 	return defXLinkColor;
  4255 }
  4256 
  4257 void VymModel::setMapDefXLinkWidth (int w)	// FIXME-3 move to ME
  4258 {
  4259 	defXLinkWidth=w;
  4260 }
  4261 
  4262 int VymModel::getMapDefXLinkWidth()	// FIXME-3 move to ME
  4263 {
  4264 	return defXLinkWidth;
  4265 }
  4266 
  4267 void VymModel::move(const double &x, const double &y)	// FIXME-3
  4268 {
  4269 	int i=x; i=y;
  4270 /*
  4271 	BranchObj *bo = getSelectedBranch();
  4272 	if (bo && 
  4273 		(selectionType()==TreeItem::Branch ||
  4274 		 selectionType()==TreeItem::MapCenter ||
  4275 		 selectionType()==TreeItem::Image
  4276 		))
  4277 	{
  4278         QPointF ap(bo->getAbsPos());
  4279         QPointF to(x, y);
  4280         if (ap != to)
  4281         {
  4282             QString ps=qpointfToString(ap);
  4283             QString s=getSelectString();
  4284             saveState(
  4285                 s, "move "+ps, 
  4286                 s, "move "+qpointfToString(to), 
  4287                 QString("Move %1 to %2").arg(getObjectName(bo)).arg(ps));
  4288             bo->move(x,y);
  4289             reposition();
  4290             selection.update();
  4291         }
  4292 	}
  4293 */	
  4294 }
  4295 
  4296 void VymModel::moveRel (const double &x, const double &y)	// FIXME-3
  4297 {
  4298 	int i=x; i=y;
  4299 /*
  4300 	BranchObj *bo = getSelectedBranch();
  4301 	if (bo && 
  4302 		(selectionType()==TreeItem::Branch ||
  4303 		 selectionType()==TreeItem::MapCenter ||
  4304 		 selectionType()==TreeItem::Image
  4305 		))
  4306 	if (bo)
  4307 	{
  4308         QPointF rp(bo->getRelPos());
  4309         QPointF to(x, y);
  4310         if (rp != to)
  4311         {
  4312             QString ps=qpointfToString (bo->getRelPos());
  4313             QString s=getSelectString(bo);
  4314             saveState(
  4315                 s, "moveRel "+ps, 
  4316                 s, "moveRel "+qpointfToString(to), 
  4317                 QString("Move %1 to relative position %2").arg(getObjectName(bo)).arg(ps));
  4318             ((OrnamentedObj*)bo)->move2RelPos (x,y);
  4319             reposition();
  4320             bo->updateLink();
  4321             selection.update();
  4322         }
  4323 	}
  4324 */	
  4325 }
  4326 
  4327 
  4328 void VymModel::animate()
  4329 {
  4330 	animationTimer->stop();
  4331 	BranchObj *bo;
  4332 	int i=0;
  4333 	while (i<animObjList.size() )
  4334 	{
  4335 		bo=(BranchObj*)animObjList.at(i);
  4336 		if (!bo->animate())
  4337 		{
  4338 			if (i>=0) 
  4339 			{	
  4340 				animObjList.removeAt(i);
  4341 				i--;
  4342 			}
  4343 		}
  4344 		bo->reposition();
  4345 		i++;
  4346 	} 
  4347 	QItemSelection sel=selModel->selection();
  4348 	emit (selectionChanged(sel,sel));
  4349 
  4350 	mapScene->update();
  4351 	if (!animObjList.isEmpty()) animationTimer->start();
  4352 }
  4353 
  4354 
  4355 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
  4356 {
  4357 	if (bo && bo->getDepth()>0) 
  4358 	{
  4359 		AnimPoint ap;
  4360 		ap.setStart (start);
  4361 		ap.setDest  (dest);
  4362 		ap.setTicks (animationTicks);
  4363 		ap.setAnimated (true);
  4364 		bo->setAnimation (ap);
  4365 		animObjList.append( bo );
  4366 		animationTimer->setSingleShot (true);
  4367 		animationTimer->start(animationInterval);
  4368 	}
  4369 }
  4370 
  4371 void VymModel::stopAnimation (MapObj *mo)
  4372 {
  4373 	int i=animObjList.indexOf(mo);
  4374     if (i>=0)
  4375 		animObjList.removeAt (i);
  4376 }
  4377 
  4378 void VymModel::sendSelection()
  4379 {
  4380 	if (netstate!=Server) return;
  4381 	sendData (QString("select (\"%1\")").arg(selection.getSelectString()) );
  4382 }
  4383 
  4384 void VymModel::newServer()
  4385 {
  4386 	port=54321;
  4387 	sendCounter=0;
  4388     tcpServer = new QTcpServer(this);
  4389     if (!tcpServer->listen(QHostAddress::Any,port)) {
  4390         QMessageBox::critical(NULL, "vym server",
  4391                               QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
  4392         //FIXME-3 needed? we are no widget any longer... close();
  4393         return;
  4394     }
  4395 	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
  4396 	netstate=Server;
  4397 	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
  4398 }
  4399 
  4400 void VymModel::connectToServer()
  4401 {
  4402 	port=54321;
  4403 	server="salam.suse.de";
  4404 	server="localhost";
  4405 	clientSocket = new QTcpSocket (this);
  4406 	clientSocket->abort();
  4407     clientSocket->connectToHost(server ,port);
  4408 	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
  4409     connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
  4410             this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
  4411 	netstate=Client;		
  4412 	cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
  4413 
  4414 	
  4415 }
  4416 
  4417 void VymModel::newClient()
  4418 {
  4419     QTcpSocket *newClient = tcpServer->nextPendingConnection();
  4420     connect(newClient, SIGNAL(disconnected()),
  4421             newClient, SLOT(deleteLater()));
  4422 
  4423 	cout <<"ME::newClient  at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
  4424 
  4425 	clientList.append (newClient);
  4426 }
  4427 
  4428 
  4429 void VymModel::sendData(const QString &s)
  4430 {
  4431 	if (clientList.size()==0) return;
  4432 
  4433 	// Create bytearray to send
  4434 	QByteArray block;
  4435     QDataStream out(&block, QIODevice::WriteOnly);
  4436     out.setVersion(QDataStream::Qt_4_0);
  4437 
  4438 	// Reserve some space for blocksize
  4439     out << (quint16)0;
  4440 
  4441 	// Write sendCounter
  4442     out << sendCounter++;
  4443 
  4444 	// Write data
  4445     out << s;
  4446 
  4447 	// Go back and write blocksize so far
  4448     out.device()->seek(0);
  4449     quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
  4450 	out << bs;
  4451 
  4452 	if (debug)
  4453 		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<qPrintable(s)<<endl;
  4454 
  4455 	for (int i=0; i<clientList.size(); ++i)
  4456 	{
  4457 		//cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
  4458 		clientList.at(i)->write (block);
  4459 	}
  4460 }
  4461 
  4462 void VymModel::readData ()
  4463 {
  4464 	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
  4465 	{
  4466 		if (debug)
  4467 			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
  4468 		quint16 recCounter;
  4469 		quint16 blockSize;
  4470 
  4471 		QDataStream in(clientSocket);
  4472 		in.setVersion(QDataStream::Qt_4_0);
  4473 
  4474 		in >> blockSize;
  4475 		in >> recCounter;
  4476 		
  4477 		QString t;
  4478 		in >>t;
  4479 		if (debug)
  4480 			cout << "  t="<<qPrintable (t)<<endl;
  4481 		parseAtom (t);
  4482 	}
  4483 	return;
  4484 }
  4485 
  4486 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
  4487 {
  4488     switch (socketError) {
  4489     case QAbstractSocket::RemoteHostClosedError:
  4490         break;
  4491     case QAbstractSocket::HostNotFoundError:
  4492         QMessageBox::information(NULL, vymName +" Network client",
  4493                                  "The host was not found. Please check the "
  4494                                     "host name and port settings.");
  4495         break;
  4496     case QAbstractSocket::ConnectionRefusedError:
  4497         QMessageBox::information(NULL, vymName + " Network client",
  4498                                  "The connection was refused by the peer. "
  4499                                     "Make sure the fortune server is running, "
  4500                                     "and check that the host name and port "
  4501                                     "settings are correct.");
  4502         break;
  4503     default:
  4504         QMessageBox::information(NULL, vymName + " Network client",
  4505                                  QString("The following error occurred: %1.")
  4506                                  .arg(clientSocket->errorString()));
  4507     }
  4508 }
  4509 
  4510 
  4511 
  4512 
  4513 void VymModel::selectMapSelectionColor()
  4514 {
  4515 	QColor col = QColorDialog::getColor( defLinkColor, NULL);
  4516 	setSelectionColor (col);
  4517 }
  4518 
  4519 void VymModel::setSelectionColorInt (QColor col)
  4520 {
  4521 	if ( !col.isValid() ) return;
  4522 	saveState (
  4523 		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
  4524 		QString("setSelectionColor (%1)").arg(col.name()),
  4525 		QString("Set color of selection box to %1").arg(col.name())
  4526 	);
  4527 
  4528 	mapEditor->setSelectionColor (col);
  4529 }
  4530 
  4531 /*
  4532 void VymModel::changeSelection (const QItemSelection &newsel,const QItemSelection &oldsel)
  4533 {
  4534 	cout << "VymModel::changeSelection (";
  4535 	if (!newsel.indexes().isEmpty() )
  4536 		cout << getItem(newsel.indexes().first() )->getHeading().toStdString();
  4537 	cout << ",";
  4538 	if (!oldsel.indexes().isEmpty() )
  4539 		cout << getItem(oldsel.indexes().first() )->getHeading().toStdString();
  4540 	cout << ")\n";
  4541 }
  4542 */
  4543 
  4544 void VymModel::updateSelection(const QItemSelection &newsel)
  4545 {
  4546 	emit (selectionChanged(newsel,newsel));	// needed e.g. to update geometry in editor
  4547 	ensureSelectionVisible();
  4548 	sendSelection();
  4549 }
  4550 
  4551 void VymModel::updateSelection()
  4552 {
  4553 	QItemSelection newsel=selModel->selection();
  4554 	updateSelection (newsel);
  4555 }
  4556 
  4557 void VymModel::setSelectionColor(QColor col)
  4558 {
  4559 	if ( !col.isValid() ) return;
  4560 	saveState (
  4561 		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
  4562 		QString("setSelectionColor (%1)").arg(col.name()),
  4563 		QString("Set color of selection box to %1").arg(col.name())
  4564 	);
  4565 	setSelectionColorInt (col);
  4566 }
  4567 
  4568 QColor VymModel::getSelectionColor()
  4569 {
  4570 	return mapEditor->getSelectionColor();
  4571 }
  4572 
  4573 void VymModel::setHideTmpMode (TreeItem::HideTmpMode mode)
  4574 {
  4575 	hidemode=mode;
  4576 	for (int i=0;i<rootItem->childCount();i++)
  4577 		rootItem->child(i)->setHideTmp (mode);	
  4578 	reposition();
  4579 	// FIXME-3 needed? scene()->update();
  4580 }
  4581 
  4582 
  4583 QRectF VymModel::getTotalBBox()	//FIXME-2
  4584 {
  4585 	QRectF r;
  4586 /*
  4587 	for (int i=0;i<rootItem->branchCount(); i++)
  4588 		r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
  4589 */ 
  4590 	return r;	
  4591 }
  4592 
  4593 //////////////////////////////////////////////
  4594 // Selection related
  4595 //////////////////////////////////////////////
  4596 
  4597 void VymModel::setSelectionModel (QItemSelectionModel *sm)
  4598 {
  4599 	selModel=sm;
  4600 }
  4601 
  4602 QItemSelectionModel* VymModel::getSelectionModel()
  4603 {
  4604 	return selModel;
  4605 }
  4606 
  4607 void VymModel::setSelectionBlocked (bool b)
  4608 {
  4609 	if (b)
  4610 		selection.block();
  4611 	else	
  4612 		selection.unblock();
  4613 }
  4614 
  4615 bool VymModel::isSelectionBlocked()
  4616 {
  4617 	return selection.isBlocked();
  4618 }
  4619 
  4620 bool VymModel::select ()
  4621 {
  4622 	QModelIndex index=selModel->selectedIndexes().first();	// TODO no multiselections yet
  4623 
  4624 	TreeItem *item = getItem (index);
  4625 	return select (item->getLMO() );
  4626 }
  4627 
  4628 bool VymModel::select (const QString &s)
  4629 {
  4630 	TreeItem *ti=findBySelectString(s);
  4631 	if (ti)
  4632 	{
  4633 		unselect();
  4634 		select (ti);
  4635 		return true;
  4636 	} 
  4637 	return false;
  4638 }
  4639 
  4640 bool VymModel::select (LinkableMapObj *lmo)
  4641 {
  4642 	QItemSelection oldsel=selModel->selection();
  4643 
  4644 	if (lmo)
  4645 		return select (lmo->getTreeItem() );
  4646 	else	
  4647 		return false;
  4648 }
  4649 
  4650 bool VymModel::select (TreeItem *ti)
  4651 {
  4652 	if (ti)
  4653 	{
  4654 		QModelIndex ix=index(ti);
  4655 		selModel->select (ix,QItemSelectionModel::ClearAndSelect  );
  4656 		ti->setLastSelectedBranch();
  4657 		return true;
  4658 	}
  4659 	return false;
  4660 }
  4661 
  4662 void VymModel::unselect()
  4663 {
  4664 	selModel->clearSelection();
  4665 }	
  4666 
  4667 void VymModel::reselect()
  4668 {
  4669 	selection.reselect();
  4670 }	
  4671 
  4672 void VymModel::ensureSelectionVisible()		//FIXME-3 needed? in vymmodel.h commented...
  4673 {
  4674 	LinkableMapObj *lmo=getSelectedLMO();
  4675 	if (lmo &&mapEditor) mapEditor->ensureVisible (lmo->getBBox() );
  4676 }
  4677 
  4678 void VymModel::emitNoteHasChanged (TreeItem *ti)
  4679 {
  4680 	QModelIndex ix=index(ti);
  4681 	emit (noteHasChanged (ix) );
  4682 }
  4683 
  4684 void VymModel::emitDataHasChanged (TreeItem *ti)
  4685 {
  4686 	QModelIndex ix=index(ti);
  4687 	emit (dataHasChanged (ix) );
  4688 }
  4689 
  4690 
  4691 //void VymModel::selectInt (LinkableMapObj *lmo)	// FIXME-3 still needed?
  4692 /*
  4693 {
  4694 	if (selection.select(lmo))
  4695 	{
  4696 		//selection.update();
  4697 		sendSelection ();	// FIXME-4 VM use signal
  4698 	}
  4699 }
  4700 
  4701 void VymModel::selectInt (TreeItem *ti)	
  4702 {
  4703 	if (selection.select(lmo))
  4704 	{
  4705 		//selection.update();
  4706 		sendSelection ();	// FIXME-4 VM use signal
  4707 	}
  4708 }
  4709 */
  4710 
  4711 void VymModel::selectNextBranchInt()
  4712 {
  4713 	// Increase number of branch
  4714 	BranchItem *selbi=getSelectedBranchItem();
  4715 	if (selbi)
  4716 	{
  4717 		QString s=getSelectString();
  4718 		QString part;
  4719 		QString typ;
  4720 		QString num;
  4721 
  4722 		// Where am I? 
  4723 		part=s.section(",",-1);
  4724 		typ=part.left (3);
  4725 		num=part.right(part.length() - 3);
  4726 
  4727 		s=s.left (s.length() -num.length());
  4728 
  4729 		// Go to next lmo
  4730 		num=QString ("%1").arg(num.toUInt()+1);
  4731 
  4732 		s=s+num;
  4733 		
  4734 		// Try to select this one
  4735 		if (select (s)) return;
  4736 
  4737 		// We have no direct successor, 
  4738 		// try to increase the parental number in order to
  4739 		// find a successor with same depth
  4740 
  4741 		int d=getSelectedBranchItem()->depth();
  4742 		int oldDepth=d;
  4743 		int i;
  4744 		bool found=false;
  4745 		bool b;
  4746 		while (!found && d>0)
  4747 		{
  4748 			s=s.section (",",0,d-1);
  4749 			// replace substring of current depth in s with "1"
  4750 			part=s.section(",",-1);
  4751 			typ=part.left (3);
  4752 			num=part.right(part.length() - 3);
  4753 
  4754 			if (d>1)
  4755 			{	
  4756 				// increase number of parent
  4757 				num=QString ("%1").arg(num.toUInt()+1);
  4758 				s=s.section (",",0,d-2) + ","+ typ+num;
  4759 			} else
  4760 			{
  4761 				// Special case, look at orientation
  4762 				if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)	// FIXME-3 check access to LMO
  4763 					num=QString ("%1").arg(num.toUInt()+1);
  4764 				else	
  4765 					num=QString ("%1").arg(num.toUInt()-1);
  4766 				s=typ+num;
  4767 			}	
  4768 
  4769 			if (select (s))
  4770 				// pad to oldDepth, select the first branch for each depth
  4771 				for (i=d;i<oldDepth;i++)
  4772 				{
  4773 					b=select (s);
  4774 					if (b)
  4775 					{	
  4776 						if ( getSelectedItem()->branchCount()>0)
  4777 							s+=",bo:0";
  4778 						else	
  4779 							break;
  4780 					} else
  4781 						break;
  4782 				}	
  4783 
  4784 			// try to select the freshly built string
  4785 			found=select(s);
  4786 			d--;
  4787 		}
  4788 		return;
  4789 	}	
  4790 }
  4791 
  4792 void VymModel::selectPrevBranchInt()
  4793 {
  4794 	// Decrease number of branch
  4795 	if (selectionType()==TreeItem::Branch)
  4796 	{
  4797 		QString s=getSelectString();
  4798 		QString part;
  4799 		QString typ;
  4800 		QString num;
  4801 
  4802 		// Where am I? 
  4803 		part=s.section(",",-1);
  4804 		typ=part.left (3);
  4805 		num=part.right(part.length() - 3);
  4806 
  4807 		s=s.left (s.length() -num.length());
  4808 
  4809 		int n=num.toInt()-1;
  4810 		
  4811 		// Go to next lmo
  4812 		num=QString ("%1").arg(n);
  4813 		s=s+num;
  4814 		
  4815 		// Try to select this one
  4816 		if (n>=0 && select (s)) return;
  4817 
  4818 		// We have no direct precessor, 
  4819 		// try to decrease the parental number in order to
  4820 		// find a precessor with same depth
  4821 
  4822 		int d=getSelectedBranchItem()->depth();
  4823 		int oldDepth=d;
  4824 		int i;
  4825 		bool found=false;
  4826 		bool b;
  4827 		while (!found && d>0)
  4828 		{
  4829 			s=s.section (",",0,d-1);
  4830 			// replace substring of current depth in s with "1"
  4831 			part=s.section(",",-1);
  4832 			typ=part.left (3);
  4833 			num=part.right(part.length() - 3);
  4834 
  4835 			if (d>1)
  4836 			{
  4837 				// decrease number of parent
  4838 				num=QString ("%1").arg(num.toInt()-1);
  4839 				s=s.section (",",0,d-2) + ","+ typ+num;
  4840 			} else
  4841 			{
  4842 				// Special case, look at orientation
  4843 				if (getSelectedLMO()->getOrientation()==LinkableMapObj::RightOfCenter)	// FIXME-3 check access to LMO
  4844 					num=QString ("%1").arg(num.toInt()-1);
  4845 				else	
  4846 					num=QString ("%1").arg(num.toInt()+1);
  4847 				s=typ+num;
  4848 			}	
  4849 
  4850 			if (select(s))
  4851 				// pad to oldDepth, select the last branch for each depth
  4852 				for (i=d;i<oldDepth;i++)
  4853 				{
  4854 					b=select (s);
  4855 					if (b)
  4856 						if ( getSelectedItem()->branchCount()>0)
  4857 							s+=",bo:"+ QString ("%1").arg( getSelectedItem()->branchCount()-1 );
  4858 						else	
  4859 							break;
  4860 					else
  4861 						break;
  4862 				}	
  4863 			
  4864 			// try to select the freshly built string
  4865 			found=select(s);
  4866 			d--;
  4867 		}
  4868 		return;
  4869 	}	
  4870 }
  4871 
  4872 void VymModel::selectUpperBranch()
  4873 {
  4874 	if (selection.isBlocked() ) return;
  4875 
  4876 	BranchItem *bi=getSelectedBranchItem();
  4877 	if (bi && bi->getType()==TreeItem::Branch)
  4878 	{
  4879 		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	//FIXME-3 check getBO
  4880 			selectPrevBranchInt();
  4881 		else
  4882 			if (bi->depth()==1)
  4883 				selectNextBranchInt();
  4884 			else
  4885 				selectPrevBranchInt();
  4886 	}
  4887 }
  4888 
  4889 void VymModel::selectLowerBranch()
  4890 {
  4891 	if (selection.isBlocked() ) return;
  4892 
  4893 	BranchItem *bi=getSelectedBranchItem();
  4894 	if (bi && bi->getType()==TreeItem::Branch)
  4895 	{
  4896 		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	//FIXME-3 check getBO
  4897 			selectNextBranchInt();
  4898 		else
  4899 			if (bi->depth()==1)
  4900 				selectPrevBranchInt();
  4901 			else
  4902 				selectNextBranchInt();
  4903 	}			
  4904 }
  4905 
  4906 
  4907 void VymModel::selectLeftBranch()
  4908 {
  4909 	if (selection.isBlocked() ) return;
  4910 
  4911 	QItemSelection oldsel=selModel->selection();
  4912 
  4913 	BranchItem* par;
  4914 	BranchItem *selbi=getSelectedBranchItem();
  4915 	TreeItem::Type type=selbi->getType();
  4916 	if (selbi)
  4917 	{
  4918 		if (type == TreeItem::MapCenter)
  4919 		{
  4920 			QModelIndex ix=index(selbi);
  4921 			selModel->select (index (rowCount(ix)-1,0,ix),QItemSelectionModel::ClearAndSelect  );
  4922 		} else
  4923 		{
  4924 			par=(BranchItem*)selbi->parent();
  4925 			if (selbi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	//FIXME-3 check getBO...
  4926 			{
  4927 				// right of center
  4928 				if (type == TreeItem::Branch ||
  4929 					type == TreeItem::Image)
  4930 				{
  4931 					QModelIndex ix=index (selbi->parent());
  4932 					selModel->select (ix,QItemSelectionModel::ClearAndSelect  );
  4933 				}
  4934 			} else
  4935 			{
  4936 				// left of center
  4937 				if (type == TreeItem::Branch )
  4938 				{
  4939 					cout << "VM::selLeft\n";
  4940 					selectLastSelectedBranch();
  4941 					return;
  4942 				}
  4943 			}
  4944 		}	
  4945 	}
  4946 }
  4947 
  4948 void VymModel::selectRightBranch()
  4949 {
  4950 	if (selection.isBlocked() ) return;
  4951 
  4952 	QItemSelection oldsel=selModel->selection();
  4953 
  4954 	BranchItem* par;
  4955 	BranchItem *selbi=getSelectedBranchItem();
  4956 	TreeItem::Type type=selbi->getType();
  4957 	if (selbi)
  4958 	{
  4959 		if (type==TreeItem::MapCenter)
  4960 		{
  4961 			QModelIndex ix=index(selbi);
  4962 			selModel->select (index (0,0,ix),QItemSelectionModel::ClearAndSelect  );
  4963 		} else
  4964 		{
  4965 			par=(BranchItem*)selbi->parent();
  4966 			if (selbi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	//FIXME-3 check getBO
  4967 			{
  4968 				// right of center
  4969 				if ( type== TreeItem::Branch )
  4970 				{
  4971 					selectLastSelectedBranch();
  4972 					return;
  4973 				}
  4974 			} else
  4975 			{
  4976 				// left of center
  4977 				if (type == TreeItem::Branch ||
  4978 					type == TreeItem::Image)
  4979 				{
  4980 					cout << "VM::selRight\n";
  4981 					QModelIndex ix=index(selbi->parent());
  4982 					selModel->select (ix,QItemSelectionModel::ClearAndSelect  );
  4983 				}
  4984 			}
  4985 		}	
  4986 	}
  4987 }
  4988 
  4989 void VymModel::selectFirstBranch()
  4990 {
  4991 	TreeItem *ti=getSelectedBranchItem();
  4992 	if (ti)
  4993 	{
  4994 		TreeItem *par=ti->parent();
  4995 		if (!par) return;
  4996 		TreeItem *ti2=par->getFirstBranch();
  4997 		if (ti2) {
  4998 			select(ti2);
  4999 			selection.update();
  5000 			ensureSelectionVisible();
  5001 			sendSelection();
  5002 		}
  5003 	}		
  5004 }
  5005 
  5006 void VymModel::selectLastBranch()
  5007 {
  5008 	TreeItem *ti=getSelectedBranchItem();
  5009 	if (ti)
  5010 	{
  5011 		TreeItem *par=ti->parent();
  5012 		if (!par) return;
  5013 		TreeItem *ti2=par->getLastBranch();
  5014 		if (ti2) {
  5015 			select(ti2);
  5016 			selection.update();
  5017 			ensureSelectionVisible();
  5018 			sendSelection();
  5019 		}
  5020 	}		
  5021 }
  5022 
  5023 void VymModel::selectLastSelectedBranch()
  5024 {
  5025 	TreeItem *ti=getSelectedBranchItem();
  5026 	if (ti)
  5027 	{
  5028 		ti=ti->getLastSelectedBranch();
  5029 		if (ti) select (ti);
  5030 	}		
  5031 }
  5032 
  5033 void VymModel::selectParent()
  5034 {
  5035 	TreeItem *ti=getSelectedItem();
  5036 	TreeItem *par;
  5037 	if (ti)
  5038 	{
  5039 		par=ti->parent();
  5040 		if (!par) return;
  5041 		select(par);
  5042 		selection.update();
  5043 		ensureSelectionVisible();
  5044 		sendSelection();
  5045 	}		
  5046 }
  5047 
  5048 TreeItem::Type VymModel::selectionType()
  5049 {
  5050 	QModelIndexList list=selModel->selectedIndexes();
  5051 	if (list.isEmpty()) return TreeItem::Undefined;	
  5052 	TreeItem *ti = getItem (list.first() );
  5053 	return ti->getType();
  5054 
  5055 }
  5056 
  5057 LinkableMapObj* VymModel::getSelectedLMO()
  5058 {
  5059 	QModelIndexList list=selModel->selectedIndexes();
  5060 	if (!list.isEmpty() )
  5061 	{
  5062 		TreeItem *ti = getItem (list.first() );
  5063 		TreeItem::Type type=ti->getType();
  5064 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
  5065 		{
  5066 			return ti->getLMO();
  5067 		}	
  5068 	}
  5069 	return NULL;
  5070 }
  5071 
  5072 BranchObj* VymModel::getSelectedBranchObj()	// FIXME-3 this should not be needed in the end!!!
  5073 {
  5074 	TreeItem *ti = getSelectedBranchItem();
  5075 	if (ti)
  5076 		return (BranchObj*)ti->getLMO();
  5077 	else	
  5078 		return NULL;
  5079 }
  5080 
  5081 BranchItem* VymModel::getSelectedBranchItem()
  5082 {
  5083 	QModelIndexList list=selModel->selectedIndexes();
  5084 	if (!list.isEmpty() )
  5085 	{
  5086 		TreeItem *ti = getItem (list.first() );
  5087 		TreeItem::Type type=ti->getType();
  5088 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
  5089 			return (BranchItem*)ti;
  5090 	}
  5091 	return NULL;
  5092 }
  5093 
  5094 MapCenterItem* VymModel::getSelectedMapCenterItem()
  5095 {
  5096 	QModelIndexList list=selModel->selectedIndexes();
  5097 	if (!list.isEmpty() )
  5098 	{
  5099 		TreeItem *ti = getItem (list.first() );
  5100 		TreeItem::Type type=ti->getType();
  5101 		if (ti->getType()==TreeItem::MapCenter)
  5102 			return (MapCenterItem*)ti;
  5103 	}
  5104 	return NULL;
  5105 }
  5106 
  5107 TreeItem* VymModel::getSelectedItem()
  5108 {
  5109 	QModelIndexList list=selModel->selectedIndexes();
  5110 	if (!list.isEmpty() )
  5111 		return getItem (list.first() );
  5112 	else	
  5113 		return NULL;
  5114 }
  5115 
  5116 QModelIndex VymModel::getSelectedIndex()
  5117 {
  5118 	QModelIndexList list=selModel->selectedIndexes();
  5119 	if (list.isEmpty() )
  5120 		return QModelIndex();
  5121 	else
  5122 		return list.first();
  5123 }
  5124 
  5125 FloatImageObj* VymModel::getSelectedFloatImage()
  5126 {
  5127 	return selection.getFloatImage();	
  5128 }
  5129 
  5130 QString VymModel::getSelectString ()
  5131 {
  5132 	LinkableMapObj *lmo=getSelectedLMO();
  5133 	if (lmo) 
  5134 		return getSelectString(lmo);
  5135 	else
  5136 		return QString();
  5137 }
  5138 
  5139 QString VymModel::getSelectString (LinkableMapObj *lmo)	// FIXME-2 VM needs to use TreeModel. Port all calls to this funtion to the one using TreeItem below...
  5140 {
  5141 	if (!lmo) return QString();
  5142 	return getSelectString (lmo->getTreeItem() );
  5143 }
  5144 
  5145 QString VymModel::getSelectString (TreeItem *ti)
  5146 {
  5147 	QString s;
  5148 	if (!ti) return s;
  5149 	if (ti->getType() == TreeItem::Branch ||
  5150 	    ti->getType() == TreeItem::MapCenter)
  5151 	{	
  5152 		TreeItem *par=ti->parent();
  5153 		if (par)
  5154 		{
  5155 			if (ti->depth() ==1)
  5156 				// Mainbranch, return 
  5157 				s= "bo:" + QString("%1").arg(ti->num() );
  5158 			else	
  5159 				// Branch, call myself recursively
  5160 				s= getSelectString(par) + ",bo:" + QString("%1").arg(ti->num());
  5161 		} else
  5162 		{
  5163 			// MapCenter
  5164 			int i=rootItem->num(ti);
  5165 			if (i>=0) s=QString("mc:%1").arg(i);
  5166 		}	
  5167 	}	
  5168 	return s;
  5169 }
  5170