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