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