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