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