vymmodel.cpp
author insilmaril
Wed, 10 Feb 2010 13:48:42 +0000
changeset 822 c2ce9944148c
parent 821 4a84d7e444d8
child 823 0bba81dde1bc
permissions -rw-r--r--
More fixes and sorting lexically backwards
     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 "editxlinkdialog.h"
    10 #include "exports.h"
    11 #include "exportxhtmldialog.h"
    12 #include "file.h"
    13 #include "geometry.h"		// for addBBox
    14 #include "mainwindow.h"
    15 #include "misc.h"
    16 #include "parser.h"
    17 #include "process.h"
    18 
    19 #include "warningdialog.h"
    20 #include "xlinkitem.h"
    21 #include "xml-freemind.h"
    22 #include "xmlobj.h"
    23 #include "xml-vym.h"
    24 
    25 
    26 extern bool debug;
    27 extern Main *mainWindow;
    28 extern QDBusConnection dbusConnection;
    29 
    30 extern Settings settings;
    31 extern QString tmpVymDir;
    32 
    33 extern TextEditor *textEditor;
    34 extern FlagRow *standardFlagsMaster;
    35 
    36 extern QString clipboardDir;
    37 extern QString clipboardFile;
    38 extern bool clipboardEmpty;
    39 
    40 extern ImageIO imageIO;
    41 
    42 extern QString vymName;
    43 extern QString vymVersion;
    44 extern QDir vymBaseDir;
    45 
    46 extern QDir lastImageDir;
    47 extern QDir lastFileDir;
    48 
    49 extern Settings settings;
    50 
    51 
    52 
    53 int VymModel::mapNum=0;	// make instance
    54 
    55 VymModel::VymModel()
    56 {
    57     //cout << "Const VymModel\n";
    58 	init();
    59 	rootItem->setModel (this);
    60 }
    61 
    62 
    63 VymModel::~VymModel() 
    64 {
    65     //cout << "Destr VymModel\n";
    66 	autosaveTimer->stop();
    67 	fileChangedTimer->stop();
    68 	clear();
    69 }	
    70 
    71 void VymModel::clear() 
    72 {
    73 	while (rootItem->childCount() >0)
    74 		deleteItem (rootItem->getChildNum(0) );
    75 }
    76 
    77 void VymModel::init () 
    78 {
    79 	// No MapEditor yet
    80 	mapEditor=NULL;
    81 
    82 	// Also no scene yet (should not be needed anyway)  FIXME-3 VM
    83 	mapScene=NULL;
    84 
    85 	// History 
    86 	mapNum++;
    87     mapChanged=false;
    88 	mapDefault=true;
    89 	mapUnsaved=false;
    90 
    91 	curStep=0;
    92 	redosAvail=0;
    93 	undosAvail=0;
    94 
    95  	stepsTotal=settings.readNumEntry("/history/stepsTotal",100);
    96 	undoSet.setEntry ("/history/stepsTotal",QString::number(stepsTotal));
    97 	mainWindow->updateHistory (undoSet);
    98 
    99 	// Create tmp dirs
   100 	makeTmpDirectories();
   101 	
   102 	// Files
   103 	zipped=true;
   104 	filePath="";
   105 	fileName=tr("unnamed");
   106 	mapName="";
   107 	blockReposition=false;
   108 	blockSaveState=false;
   109 
   110 	autosaveTimer=new QTimer (this);
   111 	connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
   112 
   113 	fileChangedTimer=new QTimer (this);
   114 	fileChangedTimer->start(3000);
   115 	connect(fileChangedTimer, SIGNAL(timeout()), this, SLOT(fileChanged()));
   116 
   117 
   118 	// selections
   119 	selModel=NULL;
   120 	selectionBlocked=false;
   121 
   122 	// find routine
   123 	findReset();
   124 
   125 	// animations	// FIXME-3 switch to new animation system 
   126 	animationUse=settings.readBoolEntry("/animation/use",false);	// FIXME-3 add options to control _what_ is animated
   127 	animationTicks=settings.readNumEntry("/animation/ticks",10);
   128 	animationInterval=settings.readNumEntry("/animation/interval",50);
   129 	animObjList.clear();
   130 	animationTimer=new QTimer (this);
   131 	connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
   132 
   133 	// View - map
   134 	defLinkColor=QColor (0,0,255);
   135 	defXLinkColor=QColor (180,180,180);
   136 	linkcolorhint=LinkableMapObj::DefaultColor;
   137 	linkstyle=LinkableMapObj::PolyParabel;
   138 	defXLinkWidth=1;
   139 	defXLinkColor=QColor (230,230,230);
   140 
   141 	hidemode=TreeItem::HideNone;
   142 
   143 	// Network
   144 	netstate=Offline;
   145 
   146 	//Initialize DBUS object
   147 	adaptorModel=new AdaptorModel(this);	// Created and not deleted as documented in Qt
   148     if (!dbusConnection.registerObject (QString("/vymmodel_%1").arg(mapNum),this))
   149 		qWarning ("VymModel: Couldn't register DBUS object!");
   150 }
   151 
   152 void VymModel::makeTmpDirectories()
   153 {
   154 	// Create unique temporary directories
   155 	tmpMapDir = tmpVymDir+QString("/model-%1").arg(mapNum);
   156 	histPath = tmpMapDir+"/history";
   157 	QDir d;
   158 	d.mkdir (tmpMapDir);
   159 }
   160 
   161 
   162 MapEditor* VymModel::getMapEditor()	// FIXME-3 VM better return favourite editor here
   163 {
   164 	return mapEditor;
   165 }
   166 
   167 bool VymModel::isRepositionBlocked()
   168 {
   169 	return blockReposition;
   170 }
   171 
   172 void VymModel::updateActions()	// FIXME-2  maybe don't update if blockReposition is set
   173 {
   174 	//cout << "VM::updateActions \n";
   175 	// Tell mainwindow to update states of actions
   176 	mainWindow->updateActions();
   177 }
   178 
   179 
   180 
   181 QString VymModel::saveToDir(const QString &tmpdir, const QString &prefix, bool writeflags, const QPointF &offset, TreeItem *saveSel)
   182 {
   183 	// tmpdir		temporary directory to which data will be written
   184 	// prefix		mapname, which will be appended to images etc.
   185 	// writeflags	Only write flags for "real" save of map, not undo
   186 	// offset		offset of bbox of whole map in scene. 
   187 	//				Needed for XML export
   188 	
   189 
   190 	XMLObj xml;
   191 
   192 	// Save Header
   193 	QString ls;
   194 	switch (linkstyle)
   195 	{
   196 		case LinkableMapObj::Line: 
   197 			ls="StyleLine";
   198 			break;
   199 		case LinkableMapObj::Parabel:
   200 			ls="StyleParabel";
   201 			break;
   202 		case LinkableMapObj::PolyLine:	
   203 			ls="StylePolyLine";
   204 			break;
   205 		default:
   206 			ls="StylePolyParabel";
   207 			break;
   208 	}	
   209 
   210 	QString s="<?xml version=\"1.0\" encoding=\"utf-8\"?><!DOCTYPE vymmap>\n";
   211 	QString colhint="";
   212 	if (linkcolorhint==LinkableMapObj::HeadingColor) 
   213 		colhint=xml.attribut("linkColorHint","HeadingColor");
   214 
   215 	QString mapAttr=xml.attribut("version",vymVersion);
   216 	if (!saveSel)
   217 		mapAttr+= xml.attribut("author",author) +
   218 				  xml.attribut("comment",comment) +
   219 			      xml.attribut("date",getDate()) +
   220 				  xml.attribut("branchCount", QString().number(branchCount())) +
   221 		          xml.attribut("backgroundColor", mapScene->backgroundBrush().color().name() ) +
   222 		          xml.attribut("selectionColor", mapEditor->getSelectionColor().name() ) +
   223 		          xml.attribut("linkStyle", ls ) +
   224 		          xml.attribut("linkColor", defLinkColor.name() ) +
   225 		          xml.attribut("defXLinkColor", defXLinkColor.name() ) +
   226 		          xml.attribut("defXLinkWidth", QString().setNum(defXLinkWidth,10) ) +
   227 		          colhint; 
   228 	s+=xml.beginElement("vymmap",mapAttr);
   229 	xml.incIndent();
   230 
   231 	// Find the used flags while traversing the tree	// FIXME-3 this can be done local to vymmodel maybe...
   232 	standardFlagsMaster->resetUsedCounter();
   233 	
   234 	// Build xml recursivly
   235 	if (!saveSel)
   236 		// Save all mapcenters as complete map, if saveSel not set
   237 		s+=saveTreeToDir(tmpdir,prefix,offset);
   238 	else
   239 	{
   240 		switch (saveSel->getType())
   241 		{
   242 			case TreeItem::Branch:
   243 				// Save Subtree
   244 				s+=((BranchItem*)saveSel)->saveToDir(tmpdir,prefix,offset);
   245 				break;
   246 			case TreeItem::MapCenter:
   247 				// Save Subtree
   248 				s+=((BranchItem*)saveSel)->saveToDir(tmpdir,prefix,offset);
   249 				break;
   250 			case TreeItem::Image:
   251 				// Save Image
   252 				s+=((ImageItem*)saveSel)->saveToDir(tmpdir,prefix);
   253 				break;
   254 			default: 
   255 				// other types shouldn't be safed directly...
   256 				break;
   257 		}
   258 	}
   259 
   260 	// Save local settings
   261 	s+=settings.getDataXML (destPath);
   262 
   263 	// Save selection
   264 	if (getSelectedItem() && !saveSel ) 
   265 		s+=xml.valueElement("select",getSelectString());
   266 
   267 	xml.decIndent();
   268 	s+=xml.endElement("vymmap");
   269 
   270 	//cout << s.toStdString() << endl;
   271 
   272 	if (writeflags) standardFlagsMaster->saveToDir (tmpdir+"/flags/","",writeflags);
   273 	return s;
   274 }
   275 
   276 QString VymModel::saveTreeToDir (const QString &tmpdir,const QString &prefix, const QPointF &offset)
   277 {
   278     QString s;
   279 
   280 	for (int i=0; i<rootItem->branchCount(); i++)
   281 		s+=rootItem->getBranchNum(i)->saveToDir (tmpdir,prefix,offset);
   282     return s;
   283 }
   284 
   285 void VymModel::setFilePath(QString fpath, QString destname)
   286 {
   287 	if (fpath.isEmpty() || fpath=="")
   288 	{
   289 		filePath="";
   290 		fileName="";
   291 		destPath="";
   292 	} else
   293 	{
   294 		filePath=fpath;		// becomes absolute path
   295 		fileName=fpath;		// gets stripped of path
   296 		destPath=destname;	// needed for vymlinks and during load to reset fileChangedTime
   297 
   298 		// If fpath is not an absolute path, complete it
   299 		filePath=QDir(fpath).absPath();
   300 		fileDir=filePath.left (1+filePath.findRev ("/"));
   301 
   302 		// Set short name, too. Search from behind:
   303 		int i=fileName.findRev("/");
   304 		if (i>=0) fileName=fileName.remove (0,i+1);
   305 
   306 		// Forget the .vym (or .xml) for name of map
   307 		mapName=fileName.left(fileName.findRev(".",-1,true) );
   308 	}
   309 }
   310 
   311 void VymModel::setFilePath(QString fpath)
   312 {
   313 	setFilePath (fpath,fpath);
   314 }
   315 
   316 QString VymModel::getFilePath()
   317 {
   318 	return filePath;
   319 }
   320 
   321 QString VymModel::getFileName()
   322 {
   323 	return fileName;
   324 }
   325 
   326 QString VymModel::getMapName()
   327 {
   328 	return mapName;
   329 }
   330 
   331 QString VymModel::getDestPath()
   332 {
   333 	return destPath;
   334 }
   335 
   336 ErrorCode VymModel::load (QString fname, const LoadMode &lmode, const FileType &ftype)
   337 {
   338 	ErrorCode err=success;
   339 
   340 	parseBaseHandler *handler;
   341 	fileType=ftype;
   342 	switch (fileType)
   343 	{
   344 		case VymMap: handler=new parseVYMHandler; break;
   345 		case FreemindMap : handler=new parseFreemindHandler; break;
   346 		default: 
   347 			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   348 				   "Unknown FileType in VymModel::load()");
   349 		return aborted;	
   350 	}
   351 
   352 	bool zipped_org=zipped;
   353 
   354 	if (lmode==NewMap)
   355 	{
   356 		selModel->clearSelection();
   357 		// FIXME-2 VM not needed??? model->setMapEditor(this);
   358 		// (map state is set later at end of load...)
   359 	} else
   360 	{
   361 		BranchItem *bi=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();	// FIXME-2 VM reposition the view instead...
   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-2 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 		emitNoteHasChanged(selti);
  1557 		saveState(
  1558 			selti,
  1559 			"setNote (\""+selti->getNote()+"\")", 
  1560 			selti,
  1561 			"setNote (\""+s+"\")", 
  1562 			QString("Set note of %1 ").arg(getObjectName(selti)) );
  1563 	}
  1564 		selti->setNote(s);
  1565 }
  1566 
  1567 QString VymModel::getNote()
  1568 {
  1569 	TreeItem *selti=getSelectedItem();
  1570 	if (selti)
  1571 		return selti->getNote();
  1572 	else	
  1573 		return QString();
  1574 }
  1575 
  1576 void VymModel::findDuplicateURLs()	// FIXME-3 needs GUI
  1577 {
  1578 	// Generate map containing _all_ URLs and branches
  1579 	QString u;
  1580 	QMap <QString,BranchItem*> map;
  1581 	BranchItem *cur=NULL;
  1582 	BranchItem *prev=NULL;
  1583 	nextBranch(cur,prev);
  1584 	while (cur) 
  1585 	{
  1586 		u=cur->getURL();
  1587 		if (!u.isEmpty() )
  1588 			map.insertMulti (u,cur);
  1589 		nextBranch(cur,prev);
  1590 	}
  1591 
  1592 	// Extract duplicate URLs
  1593 	QMap <QString, BranchItem*>::const_iterator i=map.constBegin();
  1594 	QMap <QString, BranchItem*>::const_iterator firstdup=map.constEnd();	//invalid
  1595 	while (i != map.constEnd())
  1596 	{
  1597 		if (i!=map.constBegin() && i.key()==firstdup.key())
  1598 		{
  1599 			if (  i-1==firstdup )
  1600 			{
  1601 				cout << firstdup.key().toStdString() << endl;
  1602 				cout << " - "<< firstdup.value() <<" - "<<firstdup.value()->getHeadingStd()<<endl;
  1603 			}	
  1604 			cout << " - "<< i.value() <<" - "<<i.value()->getHeadingStd()<<endl;
  1605 		} else
  1606 			firstdup=i;
  1607 
  1608 		++i;
  1609 	}
  1610 }
  1611 
  1612 BranchItem* VymModel::findText (QString s, bool cs)   
  1613 {
  1614 	if (!s.isEmpty() && s!=findString)
  1615 	{
  1616 		findReset();
  1617 		findString=s;
  1618 	}
  1619 
  1620 	QTextDocument::FindFlags flags=0;
  1621 	if (cs) flags=QTextDocument::FindCaseSensitively;
  1622 
  1623 	if (!findCurrent) 
  1624 	{	// Nothing found or new find process
  1625 		if (EOFind)
  1626 			// nothing found, start again
  1627 			EOFind=false;
  1628 		findCurrent=NULL;	
  1629 		findPrevious=NULL;	
  1630 		nextBranch (findCurrent,findPrevious);
  1631 	}	
  1632 	bool searching=true;
  1633 	bool foundNote=false;
  1634 	while (searching && !EOFind)
  1635 	{
  1636 		if (findCurrent)
  1637 		{
  1638 			// Searching in Note
  1639 			if (findCurrent->getNote().contains(findString,cs))
  1640 			{
  1641 				select (findCurrent);
  1642 				/*
  1643 				if (getSelectedBranch()!=itFind) 
  1644 				{
  1645 					select(itFind);
  1646 					emitShowSelection();
  1647 				}
  1648 				*/
  1649 				if (textEditor->findText(findString,flags)) 
  1650 				{
  1651 					searching=false;
  1652 					foundNote=true;
  1653 				}	
  1654 			}
  1655 			// Searching in Heading
  1656 			if (searching && findCurrent->getHeading().contains (findString,cs) ) 
  1657 			{
  1658 				select(findCurrent);
  1659 				searching=false;
  1660 			}
  1661 		}	
  1662 		if (!foundNote)
  1663 		{
  1664 			if (!nextBranch(findCurrent,findPrevious) )
  1665 				EOFind=true;
  1666 		}
  1667 	//cout <<"still searching...  "<<qPrintable( itFind->getHeading())<<endl;
  1668 	}	
  1669 	if (!searching)
  1670 		return getSelectedBranch();
  1671 	else
  1672 		return NULL;
  1673 }
  1674 
  1675 void VymModel::findReset()
  1676 {	// Necessary if text to find changes during a find process
  1677 	findString.clear();
  1678 	findCurrent=NULL;
  1679 	findPrevious=NULL;
  1680 	EOFind=false;
  1681 }
  1682 
  1683 void VymModel::emitShowFindWidget()
  1684 {
  1685 	emit (showFindWidget());
  1686 }
  1687 
  1688 void VymModel::setScene (QGraphicsScene *s)
  1689 {
  1690 	mapScene=s;	
  1691 }
  1692 
  1693 void VymModel::setURL(const QString &url) 
  1694 {
  1695 	TreeItem *selti=getSelectedItem();
  1696 	if (selti)
  1697 	{
  1698 		QString oldurl=selti->getURL();
  1699 		selti->setURL (url);
  1700 		saveState (
  1701 			selti,
  1702 			QString ("setURL (\"%1\")").arg(oldurl),
  1703 			selti,
  1704 			QString ("setURL (\"%1\")").arg(url),
  1705 			QString ("set URL of %1 to %2").arg(getObjectName(selti)).arg(url)
  1706 		);
  1707 		reposition();
  1708 		emitDataHasChanged (selti);
  1709 		emitShowSelection();
  1710 	}
  1711 }	
  1712 
  1713 QString VymModel::getURL()	
  1714 {
  1715 	TreeItem *selti=getSelectedItem();
  1716 	if (selti)
  1717 		return selti->getURL();
  1718 	else	
  1719 		return QString();
  1720 }
  1721 
  1722 QStringList VymModel::getURLs(bool ignoreScrolled)	
  1723 {
  1724 	QStringList urls;
  1725 	BranchItem *selbi=getSelectedBranch();
  1726 	BranchItem *cur=selbi;
  1727 	BranchItem *prev=NULL;
  1728 	while (cur) 
  1729 	{
  1730 		if (!cur->getURL().isEmpty()  && !(ignoreScrolled && cur->hasScrolledParent(cur) )) 
  1731 			urls.append( cur->getURL());
  1732 		cur=nextBranch (cur,prev,true,selbi);
  1733 	}	
  1734 	return urls;
  1735 }
  1736 
  1737 
  1738 void VymModel::setFrameType(const FrameObj::FrameType &t)	//FIXME-4 not saved if there is no LMO
  1739 {
  1740 	BranchItem *bi=getSelectedBranch();
  1741 	if (bi)
  1742 	{
  1743 		BranchObj *bo=(BranchObj*)(bi->getLMO());
  1744 		if (bo)
  1745 		{
  1746 			QString s=bo->getFrameTypeName();
  1747 			bo->setFrameType (t);
  1748 			saveState (bi, QString("setFrameType (\"%1\")").arg(s),
  1749 				bi, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
  1750 			reposition();
  1751 			bo->updateLinkGeometry();
  1752 		}
  1753 	}
  1754 }
  1755 
  1756 void VymModel::setFrameType(const QString &s)	//FIXME-4 not saved if there is no LMO
  1757 {
  1758 	BranchItem *bi=getSelectedBranch();
  1759 	if (bi)
  1760 	{
  1761 		BranchObj *bo=(BranchObj*)(bi->getLMO());
  1762 		if (bo)
  1763 		{
  1764 			saveState (bi, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
  1765 				bi, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
  1766 			bo->setFrameType (s);
  1767 			reposition();
  1768 			bo->updateLinkGeometry();
  1769 		}
  1770 	}
  1771 }
  1772 
  1773 void VymModel::setFramePenColor(const QColor &c)	//FIXME-4 not saved if there is no LMO
  1774 
  1775 {
  1776 	BranchItem *bi=getSelectedBranch();
  1777 	if (bi)
  1778 	{
  1779 		BranchObj *bo=(BranchObj*)(bi->getLMO());
  1780 		if (bo)
  1781 		{
  1782 			saveState (bi, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
  1783 				bi, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
  1784 			bo->setFramePenColor (c);
  1785 		}	
  1786 	}	
  1787 }
  1788 
  1789 void VymModel::setFrameBrushColor(const QColor &c)	//FIXME-4 not saved if there is no LMO
  1790 {
  1791 	BranchItem *bi=getSelectedBranch();
  1792 	if (bi)
  1793 	{
  1794 		BranchObj *bo=(BranchObj*)(bi->getLMO());
  1795 		if (bo)
  1796 		{
  1797 			saveState (bi, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
  1798 				bi, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
  1799 			bo->setFrameBrushColor (c);
  1800 		}	
  1801 	}	
  1802 }
  1803 
  1804 void VymModel::setFramePadding (const int &i) //FIXME-4 not saved if there is no LMO
  1805 {
  1806 	BranchItem *bi=getSelectedBranch();
  1807 	if (bi)
  1808 	{
  1809 		BranchObj *bo=(BranchObj*)(bi->getLMO());
  1810 		if (bo)
  1811 		{
  1812 			saveState (bi, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
  1813 				bi, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
  1814 			bo->setFramePadding (i);
  1815 			reposition();
  1816 			bo->updateLinkGeometry();
  1817 		}	
  1818 	}	
  1819 }
  1820 
  1821 void VymModel::setFrameBorderWidth(const int &i) //FIXME-4 not saved if there is no LMO
  1822 {
  1823 	BranchItem *bi=getSelectedBranch();
  1824 	if (bi)
  1825 	{
  1826 		BranchObj *bo=(BranchObj*)(bi->getLMO());
  1827 		if (bo)
  1828 		{
  1829 			saveState (bi, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
  1830 				bi, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
  1831 			bo->setFrameBorderWidth (i);
  1832 			reposition();
  1833 			bo->updateLinkGeometry();
  1834 		}	
  1835 	}	
  1836 }
  1837 
  1838 void VymModel::setIncludeImagesVer(bool b)
  1839 {
  1840 	BranchItem *bi=getSelectedBranch();
  1841 	if (bi)
  1842 	{
  1843 		QString u= b ? "false" : "true";
  1844 		QString r=!b ? "false" : "true";
  1845 		
  1846 		saveState(
  1847 			bi,
  1848 			QString("setIncludeImagesVertically (%1)").arg(u),
  1849 			bi, 
  1850 			QString("setIncludeImagesVertically (%1)").arg(r),
  1851 			QString("Include images vertically in %1").arg(getObjectName(bi))
  1852 		);	
  1853 		bi->setIncludeImagesVer(b);
  1854 		emitDataHasChanged ( bi);	
  1855 		reposition();
  1856 	}	
  1857 }
  1858 
  1859 void VymModel::setIncludeImagesHor(bool b)	
  1860 {
  1861 	BranchItem *bi=getSelectedBranch();
  1862 	if (bi)
  1863 	{
  1864 		QString u= b ? "false" : "true";
  1865 		QString r=!b ? "false" : "true";
  1866 		
  1867 		saveState(
  1868 			bi,
  1869 			QString("setIncludeImagesHorizontally (%1)").arg(u),
  1870 			bi, 
  1871 			QString("setIncludeImagesHorizontally (%1)").arg(r),
  1872 			QString("Include images horizontally in %1").arg(getObjectName(bi))
  1873 		);	
  1874 		bi->setIncludeImagesHor(b);
  1875 		emitDataHasChanged ( bi);
  1876 		reposition();
  1877 	}	
  1878 }
  1879 
  1880 void VymModel::setHideLinkUnselected (bool b) // FIXME-2 Images still have visible link after load
  1881 {
  1882 	TreeItem *ti=getSelectedItem();
  1883 	if (ti && (ti->getType()==TreeItem::Image ||ti->isBranchLikeType()))
  1884 	{
  1885 		QString u= b ? "false" : "true";
  1886 		QString r=!b ? "false" : "true";
  1887 		
  1888 		saveState(
  1889 			ti,
  1890 			QString("setHideLinkUnselected (%1)").arg(u),
  1891 			ti, 
  1892 			QString("setHideLinkUnselected (%1)").arg(r),
  1893 			QString("Hide link of %1 if unselected").arg(getObjectName(ti))
  1894 		);	
  1895 		((MapItem*)ti)->setHideLinkUnselected(b);
  1896 	}
  1897 }
  1898 
  1899 void VymModel::setHideExport(bool b)
  1900 {
  1901 	TreeItem *ti=getSelectedItem();
  1902 	if (ti && 
  1903 		(ti->getType()==TreeItem::Image ||ti->isBranchLikeType()))
  1904 	{
  1905 		ti->setHideInExport (b);
  1906 		QString u= b ? "false" : "true";
  1907 		QString r=!b ? "false" : "true";
  1908 		
  1909 		saveState(
  1910 			ti,
  1911 			QString ("setHideExport (%1)").arg(u),
  1912 			ti,
  1913 			QString ("setHideExport (%1)").arg(r),
  1914 			QString ("Set HideExport flag of %1 to %2").arg(getObjectName(ti)).arg (r)
  1915 		);	
  1916 			emitDataHasChanged(ti);
  1917 			emitSelectionChanged();
  1918 		updateActions();
  1919 		reposition();
  1920 		// emitSelectionChanged();
  1921 		// FIXME-3 VM needed? scene()->update();
  1922 	}
  1923 }
  1924 
  1925 void VymModel::toggleHideExport()
  1926 {
  1927 	TreeItem *selti=getSelectedItem();
  1928 	if (selti)
  1929 		setHideExport ( !selti->hideInExport() );
  1930 }
  1931 
  1932 void VymModel::addTimestamp()	//FIXME-3 new function, localize
  1933 {
  1934 	BranchItem *selbi=addNewBranch();
  1935 	if (selbi)
  1936 	{
  1937 		QDate today=QDate::currentDate();
  1938 		QChar c='0';
  1939 		selbi->setHeading (QString ("%1-%2-%3")
  1940 			.arg(today.year(),4,10,c)
  1941 			.arg(today.month(),2,10,c)
  1942 			.arg(today.day(),2,10,c));
  1943 		emitDataHasChanged ( selbi);	//FIXME-3 maybe emit signal from TreeItem? 
  1944 		reposition();
  1945 		select (selbi);
  1946 	}
  1947 }
  1948 
  1949 
  1950 void VymModel::copy()	
  1951 {
  1952 	TreeItem *selti=getSelectedItem();
  1953 	if (selti &&
  1954 		(selti->getType() == TreeItem::Branch || 
  1955 		selti->getType() == TreeItem::MapCenter  ||
  1956 		selti->getType() == TreeItem::Image ))
  1957 	{
  1958 		if (redosAvail == 0)
  1959 		{
  1960 			// Copy to history
  1961 			QString s=getSelectString(selti);
  1962 			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",selti  );
  1963 			curClipboard=curStep;
  1964 		}
  1965 
  1966 		// Copy also to global clipboard, because we are at last step in history
  1967 		QString bakMapName(QString("history-%1").arg(curStep));
  1968 		QString bakMapDir(tmpMapDir +"/"+bakMapName);
  1969 		copyDir (bakMapDir,clipboardDir );
  1970 
  1971 		clipboardEmpty=false;
  1972 		updateActions();
  1973 	}	    
  1974 }
  1975 
  1976 
  1977 void VymModel::pasteNoSave(const int &n)
  1978 {
  1979 	bool old=blockSaveState;
  1980 	blockSaveState=true;
  1981 	bool zippedOrg=zipped;
  1982 	if (redosAvail > 0 || n!=0)
  1983 	{
  1984 		// Use the "historical" buffer
  1985 		QString bakMapName(QString("history-%1").arg(n));
  1986 		QString bakMapDir(tmpMapDir +"/"+bakMapName);
  1987 		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
  1988 	} else
  1989 		// Use the global buffer
  1990 		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
  1991 	zipped=zippedOrg;
  1992 	blockSaveState=old;
  1993 }
  1994 
  1995 void VymModel::paste()	
  1996 {   
  1997 	BranchItem *selbi=getSelectedBranch();
  1998 	if (selbi)
  1999 	{
  2000 		saveStateChangingPart(
  2001 			selbi,
  2002 			selbi,
  2003 			QString ("paste (%1)").arg(curClipboard),
  2004 			QString("Paste to %1").arg( getObjectName(selbi))
  2005 		);
  2006 		pasteNoSave(0);
  2007 		reposition();
  2008 	}
  2009 }
  2010 
  2011 void VymModel::cut()	
  2012 {
  2013 	TreeItem *selti=getSelectedItem();
  2014 	if ( selti && (selti->isBranchLikeType() ||selti->getType()==TreeItem::Image))
  2015 	{
  2016 		copy();
  2017 		deleteSelection();
  2018 		reposition();
  2019 	}
  2020 }
  2021 
  2022 bool VymModel::moveUp(BranchItem *bi)	
  2023 {
  2024 	if (bi && bi->canMoveUp()) 
  2025 		return relinkBranch (bi,(BranchItem*)bi->parent(),bi->num()-1);
  2026 	else	
  2027 		return false;
  2028 }
  2029 
  2030 void VymModel::moveUp()	
  2031 {
  2032 	BranchItem *selbi=getSelectedBranch();
  2033 	if (selbi)
  2034 	{
  2035 		QString oldsel=getSelectString();
  2036 		if (moveUp (selbi))
  2037 			saveState (
  2038 				getSelectString(),"moveDown ()",
  2039 				oldsel,"moveUp ()",
  2040 				QString("Move up %1").arg(getObjectName(selbi)));
  2041 	}
  2042 }
  2043 
  2044 bool VymModel::moveDown(BranchItem *bi)	
  2045 {
  2046 	if (bi && bi->canMoveDown())
  2047 		return relinkBranch (bi,(BranchItem*)bi->parent(),bi->num()+1);
  2048 	else
  2049 		return false;
  2050 }
  2051 
  2052 void VymModel::moveDown()	
  2053 {
  2054 	BranchItem *selbi=getSelectedBranch();
  2055 	if (selbi)
  2056 	{
  2057 		QString oldsel=getSelectString();
  2058 		if ( moveDown(selbi))
  2059 			saveState (
  2060 				getSelectString(),"moveUp ()",
  2061 				oldsel,"moveDown ()",
  2062 				QString("Move down %1").arg(getObjectName(selbi)));
  2063 	}
  2064 }
  2065 
  2066 void VymModel::detach()	
  2067 {
  2068 	BranchItem *selbi=getSelectedBranch();
  2069 	if (selbi && selbi->depth()>0)
  2070 	{
  2071 		// if no relPos have been set before, try to use current rel positions   
  2072 		if (selbi->getLMO())
  2073 			for (int i=0; i<selbi->branchCount();++i)
  2074 				selbi->getBranchNum(i)->getBranchObj()->setRelPos();
  2075 		
  2076 		QString oldsel=getSelectString();
  2077 		int n=selbi->num();
  2078 		QPointF p;
  2079 		BranchObj *bo=selbi->getBranchObj();
  2080 		if (bo) p=bo->getAbsPos();
  2081 		QString parsel=getSelectString(selbi->parent());
  2082 		if ( relinkBranch (selbi,rootItem,-1) )
  2083 			saveState (
  2084 				getSelectString (selbi),
  2085 				QString("relinkTo (\"%1\",%2,%3,%4)").arg(parsel).arg(n).arg(p.x()).arg(p.y()),
  2086 				oldsel,
  2087 				"detach ()",
  2088 				QString("Detach %1").arg(getObjectName(selbi))
  2089 			);
  2090 	}
  2091 }
  2092 
  2093 void VymModel::sortChildren(bool inverse)
  2094 {
  2095 	BranchItem* selbi=getSelectedBranch();
  2096 	if (selbi)
  2097 	{
  2098 		if(selbi->branchCount()>1)
  2099 		{
  2100 			if (!inverse)
  2101 			{
  2102 			saveStateChangingPart(
  2103 				selbi,selbi, "sortChildren ()",
  2104 				QString("Sort children of %1").arg(getObjectName(selbi)));
  2105 			}
  2106 			selbi->sortChildren(inverse);
  2107 			reposition();
  2108 			emitShowSelection();
  2109 		}
  2110 	}
  2111 }
  2112 
  2113 BranchItem* VymModel::createMapCenter()
  2114 {
  2115 	BranchItem *newbi=addMapCenter (QPointF (0,0) );
  2116 	return newbi;
  2117 }
  2118 
  2119 BranchItem* VymModel::createBranch(BranchItem *dst)	
  2120 {
  2121 	if (dst)
  2122 		return addNewBranchInt (dst,-2);
  2123 	else
  2124 		return NULL;
  2125 }
  2126 
  2127 ImageItem* VymModel::createImage(BranchItem *dst)
  2128 {
  2129 	if (dst)
  2130 	{
  2131 		QModelIndex parix;
  2132 		int n;
  2133 
  2134 		QList<QVariant> cData;
  2135 		cData << "new" << "undef";
  2136 
  2137 		ImageItem *newii=new ImageItem(cData) ;	
  2138 		//newii->setHeading (QApplication::translate("Heading of new image in map", "new image"));
  2139 
  2140 		emit (layoutAboutToBeChanged() );
  2141 
  2142 			parix=index(dst);
  2143 			if (!parix.isValid()) cout << "VM::createII invalid index\n";
  2144 			n=dst->getRowNumAppend(newii);
  2145 			beginInsertRows (parix,n,n);
  2146 			dst->appendChild (newii);	
  2147 			endInsertRows ();
  2148 
  2149 		emit (layoutChanged() );
  2150 
  2151 		// save scroll state. If scrolled, automatically select
  2152 		// new branch in order to tmp unscroll parent...
  2153 		newii->createMapObj(mapScene);
  2154 		reposition();
  2155 		return newii;
  2156 	} 
  2157 	return NULL;
  2158 }
  2159 
  2160 XLinkItem* VymModel::createXLink(BranchItem *bi,bool createMO)
  2161 {
  2162 	if (bi)
  2163 	{
  2164 		QModelIndex parix;
  2165 		int n;
  2166 
  2167 		QList<QVariant> cData;
  2168 		cData << "new xLink"<<"undef";
  2169 
  2170 		XLinkItem *newxli=new XLinkItem(cData) ;	
  2171 		newxli->setBegin (bi);
  2172 
  2173 		emit (layoutAboutToBeChanged() );
  2174 
  2175 			parix=index(bi);
  2176 			n=bi->getRowNumAppend(newxli);
  2177 			beginInsertRows (parix,n,n);
  2178 			bi->appendChild (newxli);	
  2179 			endInsertRows ();
  2180 
  2181 		emit (layoutChanged() );
  2182 
  2183 		// save scroll state. If scrolled, automatically select
  2184 		// new branch in order to tmp unscroll parent...
  2185 		if (createMO) 
  2186 		{
  2187 			newxli->createMapObj(mapScene);
  2188 			reposition();
  2189 		}
  2190 		return newxli;
  2191 	} 
  2192 	return NULL;
  2193 }
  2194 
  2195 AttributeItem* VymModel::addAttribute()	
  2196 {
  2197 	BranchItem *selbi=getSelectedBranch();
  2198 	if (selbi)
  2199 	{
  2200 		QList<QVariant> cData;
  2201 		cData << "new attribute" << "undef";
  2202 		AttributeItem *a=new AttributeItem (cData);
  2203 		if (addAttribute (a)) return a;
  2204 	}
  2205 	return NULL;
  2206 }
  2207 
  2208 AttributeItem* VymModel::addAttribute(AttributeItem *ai)	// FIXME-2 savestate missing
  2209 {
  2210 	BranchItem *selbi=getSelectedBranch();
  2211 	if (selbi)
  2212 	{
  2213 		emit (layoutAboutToBeChanged() );
  2214 
  2215 		QModelIndex parix=index(selbi);
  2216 		int n=selbi->getRowNumAppend (ai);
  2217 		beginInsertRows (parix,n,n);	
  2218 		selbi->appendChild (ai);	
  2219 		endInsertRows ();
  2220 
  2221 		emit (layoutChanged() );
  2222 
  2223 		ai->createMapObj(mapScene);		//FIXME-2 check that...
  2224 		reposition();
  2225 		return ai;
  2226 	}
  2227 	return NULL;
  2228 }
  2229 
  2230 BranchItem* VymModel::addMapCenter ()
  2231 {
  2232 	BranchItem *bi=addMapCenter (contextPos);
  2233 	updateActions();
  2234 	emitShowSelection();
  2235 	saveState (
  2236 		bi,
  2237 		"delete()",
  2238 		NULL,
  2239 		QString ("addMapCenter (%1,%2)").arg (contextPos.x()).arg(contextPos.y()),
  2240 		QString ("Adding MapCenter to (%1,%2)").arg (contextPos.x()).arg(contextPos.y())
  2241 	);	
  2242 	return bi;	
  2243 }
  2244 
  2245 BranchItem* VymModel::addMapCenter(QPointF absPos)	
  2246 // createMapCenter could then probably be merged with createBranch
  2247 {
  2248 
  2249 	// Create TreeItem
  2250 	QModelIndex parix=index(rootItem);
  2251 
  2252 	QList<QVariant> cData;
  2253 	cData << "VM:addMapCenter" << "undef";
  2254 	BranchItem *newbi=new BranchItem (cData,rootItem);
  2255 	newbi->setHeading (QApplication::translate("Heading of mapcenter in new map", "New map"));
  2256 	int n=rootItem->getRowNumAppend (newbi);
  2257 
  2258 	emit (layoutAboutToBeChanged() );
  2259 	beginInsertRows (parix,n,n);
  2260 
  2261 	rootItem->appendChild (newbi);
  2262 
  2263 	endInsertRows();
  2264 	emit (layoutChanged() );
  2265 
  2266 	// Create MapObj
  2267 	newbi->setPositionMode (MapItem::Absolute);
  2268 	BranchObj *bo=newbi->createMapObj(mapScene);
  2269 	if (bo) bo->move (absPos);
  2270 		
  2271 	return newbi;
  2272 }
  2273 
  2274 BranchItem* VymModel::addNewBranchInt(BranchItem *dst,int num)
  2275 {
  2276 	// Depending on pos:
  2277 	// -3		insert in children of parent  above selection 
  2278 	// -2		add branch to selection 
  2279 	// -1		insert in children of parent below selection 
  2280 	// 0..n		insert in children of parent at pos
  2281 
  2282 	// Create TreeItem
  2283 	QList<QVariant> cData;
  2284 	cData << "" << "undef";
  2285 
  2286 	BranchItem *parbi;
  2287 	QModelIndex parix;
  2288 	int n;
  2289 	BranchItem *newbi=new BranchItem (cData);	
  2290 	//newbi->setHeading (QApplication::translate("Heading of new branch in map", "new"));
  2291 
  2292 	emit (layoutAboutToBeChanged() );
  2293 
  2294 	if (num==-2)
  2295 	{
  2296 		parbi=dst;
  2297 		parix=index(parbi);
  2298 		n=parbi->getRowNumAppend (newbi);
  2299 		beginInsertRows (parix,n,n);	
  2300 		parbi->appendChild (newbi);	
  2301 		endInsertRows ();
  2302 	}else if (num==-1 || num==-3)
  2303 	{
  2304 		// insert below selection
  2305 		parbi=(BranchItem*)dst->parent();
  2306 		parix=index(parbi);  
  2307 		
  2308 		n=dst->childNumber() + (3+num)/2;	//-1 |-> 1;-3 |-> 0
  2309 		beginInsertRows (parix,n,n);	
  2310 		parbi->insertBranch(n,newbi);	
  2311 		endInsertRows ();
  2312 	}
  2313 	emit (layoutChanged() );
  2314 
  2315 	// Set color of heading to that of parent
  2316 	newbi->setHeadingColor (parbi->getHeadingColor());
  2317 
  2318 	// save scroll state. If scrolled, automatically select
  2319 	// new branch in order to tmp unscroll parent...
  2320 	newbi->createMapObj(mapScene);
  2321 	reposition();
  2322 	return newbi;
  2323 }	
  2324 
  2325 BranchItem* VymModel::addNewBranch(int pos)
  2326 {
  2327 	// Different meaning than num in addNewBranchInt!
  2328 	// -1	add above
  2329 	//  0	add as child
  2330 	// +1	add below
  2331 	BranchItem *newbi=NULL;
  2332 	BranchItem *selbi=getSelectedBranch();
  2333 
  2334 	if (selbi)
  2335 	{
  2336 		// FIXME-3 setCursor (Qt::ArrowCursor);  //Still needed?
  2337 
  2338 		newbi=addNewBranchInt (selbi,pos-2);
  2339 
  2340 		if (newbi)
  2341 		{
  2342 			saveState(
  2343 				newbi,		
  2344 				"delete ()",
  2345 				selbi,
  2346 				QString ("addBranch (%1)").arg(pos),
  2347 				QString ("Add new branch to %1").arg(getObjectName(selbi)));	
  2348 
  2349 			reposition();
  2350 			// emitSelectionChanged(); FIXME-3
  2351 			latestAddedItem=newbi;
  2352 			// In Network mode, the client needs to know where the new branch is,
  2353 			// so we have to pass on this information via saveState.
  2354 			// TODO: Get rid of this positioning workaround
  2355 			/* FIXME-4  network problem:  QString ps=qpointfToString (newbo->getAbsPos());
  2356 			sendData ("selectLatestAdded ()");
  2357 			sendData (QString("move %1").arg(ps));
  2358 			sendSelection();
  2359 			*/
  2360 		}
  2361 	}	
  2362 	return newbi;
  2363 }
  2364 
  2365 
  2366 BranchItem* VymModel::addNewBranchBefore()	
  2367 {
  2368 	BranchItem *newbi=NULL;
  2369 	BranchItem *selbi=getSelectedBranch();
  2370 	if (selbi && selbi->getType()==TreeItem::Branch)
  2371 		 // We accept no MapCenter here, so we _have_ a parent
  2372 	{
  2373 		//QPointF p=bo->getRelPos();
  2374 
  2375 
  2376 		// add below selection
  2377 		newbi=addNewBranchInt (selbi,-1);
  2378 
  2379 		if (newbi)
  2380 		{
  2381 			//newbi->move2RelPos (p);
  2382 
  2383 			// Move selection to new branch
  2384 			relinkBranch (selbi,newbi,0);
  2385 
  2386 			saveState (newbi, "deleteKeepChildren ()", newbi, "addBranchBefore ()", 
  2387 				QString ("Add branch before %1").arg(getObjectName(selbi)));
  2388 
  2389 			// FIXME-3 needed? reposition();
  2390 			// emitSelectionChanged(); FIXME-3 
  2391 		}
  2392 	}	
  2393 	return newbi;
  2394 }
  2395 
  2396 bool VymModel::relinkBranch (BranchItem *branch, BranchItem *dst, int pos)
  2397 {
  2398 	if (branch && dst)
  2399 	{
  2400 		unselect();
  2401  
  2402 		// Do we need to update frame type?
  2403 		bool keepFrame=false;
  2404 		 
  2405 
  2406 		emit (layoutAboutToBeChanged() );
  2407 		BranchItem *branchpi=(BranchItem*)branch->parent();
  2408 		// Remove at current position
  2409 		int n=branch->childNum();
  2410 
  2411 /* FIXME-4 seg since 20091207, if ModelTest active. strange.
  2412 		// error occured if relinking branch to empty mainbranch
  2413 		cout<<"VM::relinkBranch:\n";
  2414 		cout<<"    b="<<branch->getHeadingStd()<<endl;
  2415 		cout<<"  dst="<<dst->getHeadingStd()<<endl;
  2416 		cout<<"  pos="<<pos<<endl;
  2417 		cout<<"   n1="<<n<<endl;
  2418 */		
  2419 		beginRemoveRows (index(branchpi),n,n);
  2420 		branchpi->removeChild (n);
  2421 		endRemoveRows();
  2422 
  2423 		if (pos<0 ||pos>dst->branchCount() ) pos=dst->branchCount();
  2424 
  2425 		// Append as last branch to dst
  2426 		if (dst->branchCount()==0)
  2427 			n=0;
  2428 		else	
  2429 			n=dst->getFirstBranch()->childNumber(); 
  2430 		beginInsertRows (index(dst),n+pos,n+pos);
  2431 		dst->insertBranch (pos,branch);
  2432 		endInsertRows();
  2433 
  2434 		// Correct type if necessesary
  2435 		if (branch->getType()==TreeItem::MapCenter) 
  2436 			branch->setType(TreeItem::Branch);
  2437 
  2438 		// reset parObj, fonts, frame, etc in related LMO or other view-objects
  2439 		branch->updateStyles(keepFrame);
  2440 
  2441 		emit (layoutChanged() );
  2442 		reposition();	// both for moveUp/Down and relinking
  2443 		if (dst->isScrolled() )
  2444 		{
  2445 			select (dst);	
  2446 			branch->updateVisibility();
  2447 		}
  2448 		else	
  2449 			select (branch);
  2450 		return true;
  2451 	}
  2452 	return false;
  2453 }
  2454 
  2455 bool VymModel::relinkImage (ImageItem *image, BranchItem *dst)
  2456 {
  2457 	if (image && dst)
  2458 	{
  2459 		emit (layoutAboutToBeChanged() );
  2460 
  2461 		BranchItem *pi=(BranchItem*)(image->parent());
  2462 		QString oldParString=getSelectString (pi);
  2463 		// Remove at current position
  2464 		int n=image->childNum();
  2465 		beginRemoveRows (index(pi),n,n);
  2466 		pi->removeChild (n);
  2467 		endRemoveRows();
  2468 
  2469 		// Add at dst
  2470 		QModelIndex dstix=index(dst);
  2471 		n=dst->getRowNumAppend (image);
  2472 		beginInsertRows (dstix,n,n+1);	
  2473 		dst->appendChild (image);	
  2474 		endInsertRows ();
  2475 
  2476 		// Set new parent also for lmo
  2477 		if (image->getLMO() && dst->getLMO() )
  2478 			image->getLMO()->setParObj (dst->getLMO() );
  2479 
  2480 		emit (layoutChanged() );
  2481 		saveState(
  2482 			image,
  2483 			QString("relinkTo (\"%1\")").arg(oldParString), 
  2484 			image,
  2485 			QString ("relinkTo (\"%1\")").arg(getSelectString (dst)),
  2486 			QString ("Relink floatimage to %1").arg(getObjectName(dst)));
  2487 		return true;	
  2488 	}
  2489 	return false;
  2490 }
  2491 
  2492 void VymModel::deleteSelection()	
  2493 {
  2494 	BranchItem *selbi=getSelectedBranch();
  2495 
  2496 	if (selbi)
  2497 	{	// Delete branch
  2498 		unselect();
  2499 		saveStateRemovingPart (selbi, QString ("Delete %1").arg(getObjectName(selbi)));
  2500 
  2501 		BranchItem *pi=(BranchItem*)(deleteItem (selbi));
  2502 		if (pi)
  2503 		{
  2504 			if (pi->isScrolled() && pi->branchCount()==0)
  2505 			{
  2506 				pi->unScroll();
  2507 				emitDataHasChanged(pi);
  2508 			}	
  2509 			select (pi);
  2510 			emitShowSelection();
  2511 		}
  2512 		return;
  2513 	}
  2514 	TreeItem *ti=getSelectedItem();
  2515 	if (ti)
  2516 	{	// Delete other item
  2517 		TreeItem *pi=ti->parent();
  2518 		if (!pi) return;
  2519 		if (ti->getType()==TreeItem::Image || ti->getType()==TreeItem::Attribute)
  2520 		{
  2521 			saveStateChangingPart(
  2522 				pi, 
  2523 				ti,
  2524 				"delete ()",
  2525 				QString("Delete %1").arg(getObjectName(ti))
  2526 			);
  2527 			unselect();
  2528 			deleteItem (ti);
  2529 			emitDataHasChanged (pi);
  2530 			select (pi);
  2531 			reposition();
  2532 			emitShowSelection();
  2533 		} else if (ti->getType()==TreeItem::XLink)
  2534 		{
  2535 			//FIXME-2 savestate missing
  2536 			deleteItem (ti);
  2537 		} else
  2538 			qWarning ("VymmModel::deleteSelection()  unknown type?!");
  2539 	}
  2540 }
  2541 
  2542 void VymModel::deleteKeepChildren()	//FIXME-3 does not work yet for mapcenters
  2543 
  2544 {
  2545 	BranchItem *selbi=getSelectedBranch();
  2546 	BranchItem *pi;
  2547 	if (selbi)
  2548 	{
  2549 		// Don't use this on mapcenter
  2550 		if (selbi->depth()<2) return;
  2551 
  2552 		pi=(BranchItem*)(selbi->parent());
  2553 		// Check if we have childs at all to keep
  2554 		if (selbi->branchCount()==0) 
  2555 		{
  2556 			deleteSelection();
  2557 			return;
  2558 		}
  2559 
  2560 		QPointF p;
  2561 		if (selbi->getLMO()) p=selbi->getLMO()->getRelPos();
  2562 		saveStateChangingPart(
  2563 			pi,
  2564 			selbi,
  2565 			"deleteKeepChildren ()",
  2566 			QString("Remove %1 and keep its children").arg(getObjectName(selbi))
  2567 		);
  2568 
  2569 		QString sel=getSelectString(selbi);
  2570 		unselect();
  2571 		int pos=selbi->num();
  2572 		BranchItem *bi=selbi->getFirstBranch();
  2573 		while (bi)
  2574 		{
  2575 			relinkBranch (bi,pi,pos);
  2576 			bi=selbi->getFirstBranch();
  2577 			pos++;
  2578 		}
  2579 		deleteItem (selbi);
  2580 		reposition();
  2581 		select (sel);
  2582 		BranchObj *bo=getSelectedBranchObj();
  2583 		if (bo) 
  2584 		{
  2585 			bo->move2RelPos (p);
  2586 			reposition();
  2587 		}
  2588 	}	
  2589 }
  2590 
  2591 void VymModel::deleteChildren()		
  2592 
  2593 {
  2594 	BranchItem *selbi=getSelectedBranch();
  2595 	if (selbi)
  2596 	{		
  2597 		saveStateChangingPart(
  2598 			selbi, 
  2599 			selbi,
  2600 			"deleteChildren ()",
  2601 			QString( "Remove children of branch %1").arg(getObjectName(selbi))
  2602 		);
  2603 		emit (layoutAboutToBeChanged() );
  2604 
  2605 		QModelIndex ix=index (selbi);
  2606 		int n=selbi->branchCount()-1;
  2607 		beginRemoveRows (ix,0,n);
  2608 		removeRows (0,n+1,ix);
  2609 		endRemoveRows();
  2610 		if (selbi->isScrolled()) selbi->unScroll();
  2611 		emit (layoutChanged() );
  2612 		reposition();
  2613 	}	
  2614 }
  2615 
  2616 TreeItem* VymModel::deleteItem (TreeItem *ti)
  2617 {
  2618 	if (ti)
  2619 	{
  2620 		TreeItem *pi=ti->parent();
  2621 		QModelIndex parentIndex=index(pi);
  2622 
  2623 		emit (layoutAboutToBeChanged() );
  2624 
  2625 		int n=ti->childNum();
  2626 		beginRemoveRows (parentIndex,n,n);
  2627 		removeRows (n,1,parentIndex);
  2628 		endRemoveRows();
  2629 		reposition();
  2630 
  2631 		emit (layoutChanged() );
  2632 		if (pi->depth()>=0) return pi;
  2633 	}	
  2634 	return NULL;
  2635 }
  2636 
  2637 void VymModel::clearItem (TreeItem *ti)
  2638 {
  2639 	if (ti)
  2640 	{
  2641 		QModelIndex parentIndex=index(ti);
  2642 		if (!parentIndex.isValid()) return;
  2643 
  2644 		int n=ti->childCount();
  2645 		if (n==0) return;
  2646 
  2647 		emit (layoutAboutToBeChanged() );
  2648 
  2649 		beginRemoveRows (parentIndex,0,n-1);
  2650 		removeRows (0,n,parentIndex);
  2651 		endRemoveRows();
  2652 		reposition();
  2653 
  2654 		emit (layoutChanged() );
  2655 
  2656 	}	
  2657 	return ;
  2658 }
  2659 
  2660 bool VymModel::scrollBranch(BranchItem *bi)
  2661 {
  2662 	if (bi)	
  2663 	{
  2664 		if (bi->isScrolled()) return false;
  2665 		if (bi->branchCount()==0) return false;
  2666 		if (bi->depth()==0) return false;
  2667 		if (bi->toggleScroll())
  2668 		{
  2669 			reposition();
  2670 			QString u,r;
  2671 			r="scroll";
  2672 			u="unscroll";
  2673 			saveState(
  2674 				bi,
  2675 				QString ("%1 ()").arg(u),
  2676 				bi,
  2677 				QString ("%1 ()").arg(r),
  2678 				QString ("%1 %2").arg(r).arg(getObjectName(bi))
  2679 			);
  2680 			emitDataHasChanged(bi);
  2681 			emitSelectionChanged();
  2682 			mapScene->update(); //Needed for _quick_ update,  even in 1.13.x 
  2683 			return true;
  2684 		}
  2685 	}	
  2686 	return false;
  2687 }
  2688 
  2689 bool VymModel::unscrollBranch(BranchItem *bi)
  2690 {
  2691 	if (bi)
  2692 	{
  2693 		if (!bi->isScrolled()) return false;
  2694 		if (bi->branchCount()==0) return false;
  2695 		if (bi->depth()==0) return false;
  2696 		if (bi->toggleScroll())
  2697 		{
  2698 			reposition();
  2699 			QString u,r;
  2700 			u="scroll";
  2701 			r="unscroll";
  2702 			saveState(
  2703 				bi,
  2704 				QString ("%1 ()").arg(u),
  2705 				bi,
  2706 				QString ("%1 ()").arg(r),
  2707 				QString ("%1 %2").arg(r).arg(getObjectName(bi))
  2708 			);
  2709 			emitDataHasChanged(bi);
  2710 			emitSelectionChanged();
  2711 			mapScene->update(); //Needed for _quick_ update,  even in 1.13.x 
  2712 			return true;
  2713 		}	
  2714 	}	
  2715 	return false;
  2716 }
  2717 
  2718 void VymModel::toggleScroll()	
  2719 {
  2720 	BranchItem *bi=(BranchItem*)getSelectedBranch();
  2721 	if (bi && bi->isBranchLikeType() )
  2722 	{
  2723 		if (bi->isScrolled())
  2724 			unscrollBranch (bi);
  2725 		else
  2726 			scrollBranch (bi);
  2727 	}
  2728 	// saveState & reposition are called in above functions
  2729 }
  2730 
  2731 void VymModel::unscrollChildren() 	//FIXME-2 does not update flag yet, possible segfault
  2732 {
  2733 	BranchItem *selbi=getSelectedBranch();
  2734 	BranchItem *prev=NULL;
  2735 	BranchItem *cur=selbi;
  2736 	if (selbi)
  2737 	{
  2738 		saveStateChangingPart(
  2739 			selbi,
  2740 			selbi,
  2741 			QString ("unscrollChildren ()"),
  2742 			QString ("unscroll all children of %1").arg(getObjectName(selbi))
  2743 		);	
  2744 		while (cur) 
  2745 		{
  2746 			if (cur->isScrolled())
  2747 			{
  2748 				cur->toggleScroll(); 
  2749 				emitDataHasChanged (cur);
  2750 		}
  2751 			cur=nextBranch (cur,prev,true,selbi);
  2752 		}	
  2753 		updateActions();
  2754 		reposition();
  2755 	}	
  2756 }
  2757 
  2758 void VymModel::emitExpandAll()	
  2759 {
  2760 	emit (expandAll() );
  2761 }
  2762 
  2763 void VymModel::emitExpandOneLevel()	
  2764 {
  2765 	emit (expandOneLevel () );
  2766 }
  2767 
  2768 void VymModel::emitCollapseOneLevel()	
  2769 {
  2770 	emit (collapseOneLevel () );
  2771 }
  2772 
  2773 void VymModel::toggleStandardFlag (const QString &name, FlagRow *master)
  2774 {
  2775 	BranchItem *bi=getSelectedBranch();
  2776 	if (bi) 
  2777 	{
  2778 		QString u,r;
  2779 		if (bi->isActiveStandardFlag(name))
  2780 		{
  2781 			r="unsetFlag";
  2782 			u="setFlag";
  2783 		}	
  2784 		else
  2785 		{
  2786 			u="unsetFlag";
  2787 			r="setFlag";
  2788 		}	
  2789 		saveState(
  2790 			bi,
  2791 			QString("%1 (\"%2\")").arg(u).arg(name), 
  2792 			bi,
  2793 			QString("%1 (\"%2\")").arg(r).arg(name),
  2794 			QString("Toggling standard flag \"%1\" of %2").arg(name).arg(getObjectName(bi)));
  2795 			bi->toggleStandardFlag (name, master);
  2796 		reposition();
  2797 		emitSelectionChanged();	
  2798 	}
  2799 }
  2800 
  2801 void VymModel::addFloatImage (const QPixmap &img) 
  2802 {
  2803 	BranchItem *selbi=getSelectedBranch();
  2804 	if (selbi)
  2805 	{
  2806 		ImageItem *ii=createImage (selbi);
  2807 		ii->load(img);
  2808 		ii->setOriginalFilename("No original filename (image added by dropevent)");	
  2809 		QString s=getSelectString(selbi);
  2810 		saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",ii  );
  2811 		saveState (ii,"delete ()", selbi,QString("paste(%1)").arg(curStep),"Pasting dropped image");
  2812 		reposition();
  2813 		// FIXME-3 VM needed? scene()->update();
  2814 	}
  2815 }
  2816 
  2817 
  2818 void VymModel::colorBranch (QColor c)	
  2819 {
  2820 	BranchItem *selbi=getSelectedBranch();
  2821 	if (selbi)
  2822 	{
  2823 		saveState(
  2824 			selbi, 
  2825 			QString ("colorBranch (\"%1\")").arg(selbi->getHeadingColor().name()),
  2826 			selbi,
  2827 			QString ("colorBranch (\"%1\")").arg(c.name()),
  2828 			QString("Set color of %1 to %2").arg(getObjectName(selbi)).arg(c.name())
  2829 		);	
  2830 		selbi->setHeadingColor(c); // color branch
  2831 		mapScene->update();
  2832 	}
  2833 }
  2834 
  2835 void VymModel::colorSubtree (QColor c) 
  2836 {
  2837 	BranchItem *selbi=getSelectedBranch();
  2838 	if (selbi)
  2839 	{
  2840 		saveStateChangingPart(
  2841 			selbi,
  2842 			selbi,
  2843 			QString ("colorSubtree (\"%1\")").arg(c.name()),
  2844 			QString ("Set color of %1 and children to %2").arg(getObjectName(selbi)).arg(c.name())
  2845 		);	
  2846 		BranchItem *prev=NULL;
  2847 		BranchItem *cur=selbi;
  2848 		while (cur) 
  2849 		{
  2850 			cur->setHeadingColor(c); // color links, color children
  2851 			cur=nextBranch (cur,prev,true,selbi);
  2852 		}	
  2853 	mapScene->update();
  2854 	}
  2855 }
  2856 
  2857 QColor VymModel::getCurrentHeadingColor()	
  2858 {
  2859 	BranchItem *selbi=getSelectedBranch();
  2860 	if (selbi)	return selbi->getHeadingColor();
  2861 		
  2862 	QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
  2863 	return Qt::black;
  2864 }
  2865 
  2866 
  2867 
  2868 void VymModel::editURL()	
  2869 {
  2870 	TreeItem *selti=getSelectedItem();
  2871 	if (selti)
  2872 	{		
  2873 		bool ok;
  2874 		QString text = QInputDialog::getText(
  2875 				"VYM", tr("Enter URL:"), QLineEdit::Normal,
  2876 				selti->getURL(), &ok, NULL);
  2877 		if ( ok) 
  2878 			// user entered something and pressed OK
  2879 			setURL(text);
  2880 	}
  2881 }
  2882 
  2883 void VymModel::editLocalURL()
  2884 {
  2885 	TreeItem *selti=getSelectedItem();
  2886 	if (selti)
  2887 	{		
  2888 		QStringList filters;
  2889 		filters <<"All files (*)";
  2890 		filters << tr("Text","Filedialog") + " (*.txt)";
  2891 		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
  2892 		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
  2893 		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
  2894 		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
  2895 		fd->setFilters (filters);
  2896 		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
  2897 		fd->setDirectory (lastFileDir);
  2898 		if (! selti->getVymLink().isEmpty() )
  2899 			fd->selectFile( selti->getURL() );
  2900 		fd->show();
  2901 
  2902 		if ( fd->exec() == QDialog::Accepted )
  2903 		{
  2904 			lastFileDir=QDir (fd->directory().path());
  2905 			setURL (fd->selectedFile() );
  2906 		}
  2907 	}
  2908 }
  2909 
  2910 
  2911 void VymModel::editHeading2URL() 
  2912 {
  2913 	TreeItem *selti=getSelectedItem();
  2914 	if (selti)
  2915 		setURL (selti->getHeading());
  2916 }	
  2917 
  2918 void VymModel::editBugzilla2URL()	
  2919 {
  2920 	TreeItem *selti=getSelectedItem();
  2921 	if (selti)
  2922 	{		
  2923 		QString h=selti->getHeading();
  2924 		QRegExp rx("^(\\d+)");
  2925 		if (rx.indexIn(h) !=-1)
  2926 			setURL ("https://bugzilla.novell.com/show_bug.cgi?id="+rx.cap(1) );
  2927 	}
  2928 }	
  2929 
  2930 void VymModel::getBugzillaData()	
  2931 {
  2932 	TreeItem *selti=getSelectedItem();
  2933 	if (selti)
  2934 	{		
  2935 		QString url=selti->getURL();
  2936 		if (!url.isEmpty())
  2937 		{
  2938 			QRegExp rx("(\\d+)");
  2939 			if (rx.indexIn(url) !=-1)
  2940 			{
  2941 				QString bugID=rx.cap(1);
  2942 				cout << "VM::getBugzillaData bug="<<bugID.toStdString()<<endl;
  2943 
  2944 				Process p;
  2945 				//QString script="test/sleep.sh";
  2946 				QString script="test/vym-bug.pl";
  2947 				p.start (script,QStringList()<<bugID);
  2948 				if (!p.waitForStarted())
  2949 				{
  2950 					cout <<"VM::getBugzillaData couldn't start "<<script.toStdString()<<endl;
  2951 					return;
  2952 				}	
  2953 				if (!p.waitForFinished())
  2954 				{
  2955 					cout <<"VM::getBugzillaData couldn't finish "<<script.toStdString()<<endl;
  2956 					return;
  2957 				}
  2958 				//QByteArray result=p.readAll();
  2959 				QString result=p.getStdout();
  2960 				while (result.endsWith("\n")) result.chop(1); 
  2961 				//cout << QString(result).toStdString()<<endl;
  2962 				QString err=p.getErrout();
  2963 				if (!err.isEmpty())
  2964 				{
  2965 					cout << "VM::getBugzillaData Error:\n";
  2966 					cout <<err.toStdString()<<endl;
  2967 				}
  2968 				else if (!result.isEmpty())
  2969 				{
  2970 					QString heading,cdate,mdate,state,whiteboard;
  2971 					QRegExp re("short_desc:(.*)\n");
  2972 					re.setMinimal(true);
  2973 					if (re.indexIn (result) !=-1) heading=re.cap(1);
  2974 
  2975 					re.setPattern ("creation_ts:(.*)\\s");
  2976 					if (re.indexIn (result) !=-1) cdate=re.cap(1);
  2977 
  2978 					re.setPattern ("delta_ts:(.*)\\s");
  2979 					if (re.indexIn (result) !=-1) mdate=re.cap(1);
  2980 
  2981 					re.setPattern ("bug_status:(.*)\n");
  2982 					if (re.indexIn (result) !=-1) state=re.cap(1);
  2983 
  2984 					re.setPattern ("status_whiteboard:(.*)\n");
  2985 					if (re.indexIn (result) !=-1) whiteboard=re.cap(1);
  2986 
  2987 					setHeading (bugID + " - " + heading);
  2988 					cout << "VM: heading="<<heading.toStdString()<<endl;
  2989 					cout << "VM:   cdate="<<cdate.toStdString()<<endl;
  2990 					cout << "VM:   mdate="<<mdate.toStdString()<<endl;
  2991 					cout << "VM:   state="<<state.toStdString()<<endl;
  2992 					cout << "VM:  wboard="<<whiteboard.toStdString()<<endl;
  2993 					
  2994 					//cout <<"VM::getBugzillaData  "<<script.toStdString()<<" returned:\n";
  2995 					//cout <<QString(result).toStdString()<<endl;
  2996 				} else	
  2997 					cout << "VM::getBugzillaData "<<script.toStdString()<<"  returned nothing\n";
  2998 			}	
  2999 		}
  3000 	}
  3001 }	
  3002 
  3003 void VymModel::editFATE2URL()
  3004 {
  3005 	TreeItem *selti=getSelectedItem();
  3006 	if (selti)
  3007 	{		
  3008 		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+selti->getHeading();
  3009 		saveState(
  3010 			selti,
  3011 			"setURL (\""+selti->getURL()+"\")",
  3012 			selti,
  3013 			"setURL (\""+url+"\")",
  3014 			QString("Use heading of %1 as link to FATE").arg(getObjectName(selti))
  3015 		);	
  3016 		selti->setURL (url);
  3017 		// FIXME-4 updateActions();
  3018 	}
  3019 }	
  3020 
  3021 void VymModel::editVymLink()
  3022 {
  3023 	BranchItem *bi=getSelectedBranch();
  3024 	if (bi)
  3025 	{		
  3026 		QStringList filters;
  3027 		filters <<"VYM map (*.vym)";
  3028 		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
  3029 		fd->setFilters (filters);
  3030 		fd->setCaption(vymName+" - " +tr("Link to another map"));
  3031 		fd->setDirectory (lastFileDir);
  3032 		if (! bi->getVymLink().isEmpty() )
  3033 			fd->selectFile( bi->getVymLink() );
  3034 		fd->show();
  3035 
  3036 		QString fn;
  3037 		if ( fd->exec() == QDialog::Accepted )
  3038 		{
  3039 			lastFileDir=QDir (fd->directory().path());
  3040 			saveState(
  3041 				bi,
  3042 				"setVymLink (\""+bi->getVymLink()+"\")",
  3043 				bi,
  3044 				"setVymLink (\""+fd->selectedFile()+"\")",
  3045 				QString("Set vymlink of %1 to %2").arg(getObjectName(bi)).arg(fd->selectedFile())
  3046 			);	
  3047 			setVymLink (fd->selectedFile() );	// FIXME-2 ok?
  3048 		}
  3049 	}
  3050 }
  3051 
  3052 void VymModel::setVymLink (const QString &s)	// FIXME-3 no savestate?
  3053 {
  3054 	// Internal function, no saveState needed
  3055 	TreeItem *selti=getSelectedItem();
  3056 	if (selti)
  3057 	{
  3058 		selti->setVymLink(s);
  3059 		reposition();
  3060 		emitDataHasChanged (selti);
  3061 		emitShowSelection();
  3062 	}
  3063 }
  3064 
  3065 void VymModel::deleteVymLink()
  3066 {
  3067 	BranchItem *bi=getSelectedBranch();
  3068 	if (bi)
  3069 	{		
  3070 		saveState(
  3071 			bi,
  3072 			"setVymLink (\""+bi->getVymLink()+"\")", 
  3073 			bi,
  3074 			"setVymLink (\"\")",
  3075 			QString("Unset vymlink of %1").arg(getObjectName(bi))
  3076 		);	
  3077 		bi->setVymLink ("" );
  3078 		updateActions();
  3079 		reposition();
  3080 	}
  3081 }
  3082 
  3083 QString VymModel::getVymLink()
  3084 {
  3085 	BranchItem *bi=getSelectedBranch();
  3086 	if (bi)
  3087 		return bi->getVymLink();
  3088 	else	
  3089 		return "";
  3090 	
  3091 }
  3092 
  3093 QStringList VymModel::getVymLinks()	
  3094 {
  3095 	QStringList links;
  3096 	BranchItem *selbi=getSelectedBranch();
  3097 	BranchItem *cur=selbi;
  3098 	BranchItem *prev=NULL;
  3099 	while (cur) 
  3100 	{
  3101 		if (!cur->getVymLink().isEmpty()) links.append( cur->getVymLink());
  3102 		cur=nextBranch (cur,prev,true,selbi);
  3103 	}	
  3104 	return links;
  3105 }
  3106 
  3107 
  3108 void VymModel::followXLink(int i)	
  3109 {
  3110 	i=0;
  3111 	BranchItem *selbi=getSelectedBranch();
  3112 	if (selbi)
  3113 	{
  3114 		selbi=selbi->getXLinkNum(i)->getPartnerBranch();
  3115 		if (selbi) select (selbi);
  3116 	}
  3117 }
  3118 
  3119 void VymModel::editXLink(int i)	
  3120 {
  3121 	i=0;
  3122 	BranchItem *selbi=getSelectedBranch();
  3123 	if (selbi)
  3124 	{
  3125 		XLinkItem *xli=selbi->getXLinkNum(i);
  3126 		if (xli) 
  3127 		{
  3128 			EditXLinkDialog dia;
  3129 			dia.setXLink (xli);
  3130 			dia.setSelection(selbi);
  3131 			if (dia.exec() == QDialog::Accepted)
  3132 			{
  3133 				if (dia.useSettingsGlobal() )
  3134 				{
  3135 					setMapDefXLinkColor (xli->getColor() );
  3136 					setMapDefXLinkWidth (xli->getWidth() );
  3137 				}
  3138 				if (dia.deleteXLink()) deleteItem (xli);
  3139 			}
  3140 		}	
  3141 	}
  3142 }
  3143 
  3144 
  3145 
  3146 
  3147 
  3148 //////////////////////////////////////////////
  3149 // Scripting
  3150 //////////////////////////////////////////////
  3151 
  3152 QVariant VymModel::parseAtom(const QString &atom, bool &noErr, QString &errorMsg)
  3153 {
  3154 	TreeItem* selti=getSelectedItem();
  3155 	BranchItem *selbi=getSelectedBranch();
  3156 	QString s,t;
  3157 	double x,y;
  3158 	int n;
  3159 	bool b,ok;
  3160 	QVariant returnValue;
  3161 
  3162 	// Split string s into command and parameters
  3163 	parser.parseAtom (atom);
  3164 	QString com=parser.getCommand();
  3165 	
  3166 	// External commands
  3167 	/////////////////////////////////////////////////////////////////////
  3168 	if (com=="addBranch")
  3169 	{
  3170 		if (!selti)
  3171 		{
  3172 			parser.setError (Aborted,"Nothing selected");
  3173 		} else if (! selbi )
  3174 		{				  
  3175 			parser.setError (Aborted,"Type of selection is not a branch");
  3176 		} else 
  3177 		{	
  3178 			QList <int> pl;
  3179 			pl << 0 <<1;
  3180 			if (parser.checkParCount(pl))
  3181 			{
  3182 				if (parser.parCount()==0)
  3183 					addNewBranch (0);
  3184 				else
  3185 				{
  3186 					n=parser.parInt (ok,0);
  3187 					if (ok ) addNewBranch (n);
  3188 				}
  3189 			}
  3190 		}
  3191 	/////////////////////////////////////////////////////////////////////
  3192 	} else if (com=="addBranchBefore")
  3193 	{
  3194 		if (!selti)
  3195 		{
  3196 			parser.setError (Aborted,"Nothing selected");
  3197 		} else if (! selbi )
  3198 		{				  
  3199 			parser.setError (Aborted,"Type of selection is not a branch");
  3200 		} else 
  3201 		{	
  3202 			if (parser.parCount()==0)
  3203 			{
  3204 				addNewBranchBefore ();
  3205 			}	
  3206 		}
  3207 	/////////////////////////////////////////////////////////////////////
  3208 	} else if (com==QString("addMapCenter"))
  3209 	{
  3210 		if (parser.checkParCount(2))
  3211 		{
  3212 			x=parser.parDouble (ok,0);
  3213 			if (ok)
  3214 			{
  3215 				y=parser.parDouble (ok,1);
  3216 				if (ok) addMapCenter (QPointF(x,y));
  3217 			}
  3218 		}	
  3219 	/////////////////////////////////////////////////////////////////////
  3220 	} else if (com==QString("addMapReplace"))
  3221 	{
  3222 		if (!selti)
  3223 		{
  3224 			parser.setError (Aborted,"Nothing selected");
  3225 		} else if (! selbi )
  3226 		{				  
  3227 			parser.setError (Aborted,"Type of selection is not a branch");
  3228 		} else if (parser.checkParCount(1))
  3229 		{
  3230 			//s=parser.parString (ok,0);	// selection
  3231 			t=parser.parString (ok,0);	// path to map
  3232 			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  3233 			addMapReplaceInt(getSelectString(selbi),t);	
  3234 		}
  3235 	/////////////////////////////////////////////////////////////////////
  3236 	} else if (com==QString("addMapInsert"))
  3237 	{
  3238 		if (parser.parCount()==2)
  3239 		{
  3240 
  3241 			if (!selti)
  3242 			{
  3243 				parser.setError (Aborted,"Nothing selected");
  3244 			} else if (! selbi )
  3245 			{				  
  3246 				parser.setError (Aborted,"Type of selection is not a branch");
  3247 			} else 
  3248 			{	
  3249 				t=parser.parString (ok,0);	// path to map
  3250 				n=parser.parInt(ok,1);		// position
  3251 				if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  3252 				addMapInsertInt(t,n);	
  3253 			}
  3254 		} else if (parser.parCount()==1)
  3255 		{
  3256 			t=parser.parString (ok,0);	// path to map
  3257 			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  3258 			addMapInsertInt(t);	
  3259 		} else
  3260 			parser.setError (Aborted,"Wrong number of parameters");
  3261 	/////////////////////////////////////////////////////////////////////
  3262 	} else if (com==QString("addXLink"))
  3263 	{
  3264 		if (parser.parCount()>1)
  3265 		{
  3266 			s=parser.parString (ok,0);	// begin
  3267 			t=parser.parString (ok,1);	// end
  3268 			BranchItem *begin=(BranchItem*)findBySelectString(s);
  3269 			BranchItem *end=(BranchItem*)findBySelectString(t);
  3270 			if (begin && end)
  3271 			{
  3272 				if (begin->isBranchLikeType() && end->isBranchLikeType())
  3273 				{
  3274 					XLinkItem *xl=createXLink (begin,true);
  3275 					if (xl)
  3276 					{
  3277 						xl->setEnd (end);
  3278 						xl->activate();
  3279 					} else
  3280 						parser.setError (Aborted,"Failed to create xLink");
  3281 				}
  3282 				else
  3283 					parser.setError (Aborted,"begin or end of xLink are not branch or mapcenter");
  3284 				
  3285 			} else
  3286 				parser.setError (Aborted,"Couldn't select begin or end of xLink");
  3287 		} else
  3288 			parser.setError (Aborted,"Need at least 2 parameters for begin and end");
  3289 	/////////////////////////////////////////////////////////////////////
  3290 	} else if (com=="clearFlags")	
  3291 	{
  3292 		if (!selti )
  3293 		{
  3294 			parser.setError (Aborted,"Nothing selected");
  3295 		} else if (! selbi )
  3296 		{				  
  3297 			parser.setError (Aborted,"Type of selection is not a branch");
  3298 		} else if (parser.checkParCount(0))
  3299 		{
  3300 			selbi->deactivateAllStandardFlags();	
  3301 		}
  3302 	/////////////////////////////////////////////////////////////////////
  3303 	} else if (com=="colorBranch")
  3304 	{
  3305 		if (!selti)
  3306 		{
  3307 			parser.setError (Aborted,"Nothing selected");
  3308 		} else if (! selbi )
  3309 		{				  
  3310 			parser.setError (Aborted,"Type of selection is not a branch");
  3311 		} else if (parser.checkParCount(1))
  3312 		{	
  3313 			QColor c=parser.parColor (ok,0);
  3314 			if (ok) colorBranch (c);
  3315 		}	
  3316 	/////////////////////////////////////////////////////////////////////
  3317 	} else if (com=="colorSubtree")
  3318 	{
  3319 		if (!selti)
  3320 		{
  3321 			parser.setError (Aborted,"Nothing selected");
  3322 		} else if (! selbi )
  3323 		{				  
  3324 			parser.setError (Aborted,"Type of selection is not a branch");
  3325 		} else if (parser.checkParCount(1))
  3326 		{	
  3327 			QColor c=parser.parColor (ok,0);
  3328 			if (ok) colorSubtree (c);
  3329 		}	
  3330 	/////////////////////////////////////////////////////////////////////
  3331 	} else if (com=="copy")
  3332 	{
  3333 		if (!selti)
  3334 		{
  3335 			parser.setError (Aborted,"Nothing selected");
  3336 		} else if ( selectionType()!=TreeItem::Branch  && 
  3337 					selectionType()!=TreeItem::MapCenter  &&
  3338 					selectionType()!=TreeItem::Image )
  3339 		{				  
  3340 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3341 		} else if (parser.checkParCount(0))
  3342 		{	
  3343 			copy();
  3344 		}	
  3345 	/////////////////////////////////////////////////////////////////////
  3346 	} else if (com=="cut")
  3347 	{
  3348 		if (!selti)
  3349 		{
  3350 			parser.setError (Aborted,"Nothing selected");
  3351 		} else if ( selectionType()!=TreeItem::Branch  && 
  3352 					selectionType()!=TreeItem::MapCenter  &&
  3353 					selectionType()!=TreeItem::Image )
  3354 		{				  
  3355 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3356 		} else if (parser.checkParCount(0))
  3357 		{	
  3358 			cut();
  3359 		}	
  3360 	/////////////////////////////////////////////////////////////////////
  3361 	} else if (com=="delete")
  3362 	{
  3363 		if (!selti)
  3364 		{
  3365 			parser.setError (Aborted,"Nothing selected");
  3366 		} 
  3367 		/*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
  3368 		{
  3369 			parser.setError (Aborted,"Type of selection is wrong.");
  3370 		} 
  3371 		*/
  3372 		else if (parser.checkParCount(0))
  3373 		{	
  3374 			deleteSelection();
  3375 		}	
  3376 	/////////////////////////////////////////////////////////////////////
  3377 	} else if (com=="deleteKeepChildren")
  3378 	{
  3379 		if (!selti)
  3380 		{
  3381 			parser.setError (Aborted,"Nothing selected");
  3382 		} else if (! selbi )
  3383 		{
  3384 			parser.setError (Aborted,"Type of selection is not a branch");
  3385 		} else if (parser.checkParCount(0))
  3386 		{	
  3387 			deleteKeepChildren();
  3388 		}	
  3389 	/////////////////////////////////////////////////////////////////////
  3390 	} else if (com=="deleteChildren")
  3391 	{
  3392 		if (!selti)
  3393 		{
  3394 			parser.setError (Aborted,"Nothing selected");
  3395 		} else if (! selbi)
  3396 		{
  3397 			parser.setError (Aborted,"Type of selection is not a branch");
  3398 		} else if (parser.checkParCount(0))
  3399 		{	
  3400 			deleteChildren();
  3401 		}	
  3402 	/////////////////////////////////////////////////////////////////////
  3403 	} else if (com=="exportAO")
  3404 	{
  3405 		QString fname="";
  3406 		ok=true;
  3407 		if (parser.parCount()>=1)
  3408 			// Hey, we even have a filename
  3409 			fname=parser.parString(ok,0); 
  3410 		if (!ok)
  3411 		{
  3412 			parser.setError (Aborted,"Could not read filename");
  3413 		} else
  3414 		{
  3415 				exportAO (fname,false);
  3416 		}
  3417 	/////////////////////////////////////////////////////////////////////
  3418 	} else if (com=="exportASCII")
  3419 	{
  3420 		QString fname="";
  3421 		ok=true;
  3422 		if (parser.parCount()>=1)
  3423 			// Hey, we even have a filename
  3424 			fname=parser.parString(ok,0); 
  3425 		if (!ok)
  3426 		{
  3427 			parser.setError (Aborted,"Could not read filename");
  3428 		} else
  3429 		{
  3430 				exportASCII (fname,false);
  3431 		}
  3432 	/////////////////////////////////////////////////////////////////////
  3433 	} else if (com=="exportImage")
  3434 	{
  3435 		QString fname="";
  3436 		ok=true;
  3437 		if (parser.parCount()>=2)
  3438 			// Hey, we even have a filename
  3439 			fname=parser.parString(ok,0); 
  3440 		if (!ok)
  3441 		{
  3442 			parser.setError (Aborted,"Could not read filename");
  3443 		} else
  3444 		{
  3445 			QString format="PNG";
  3446 			if (parser.parCount()>=2)
  3447 			{
  3448 				format=parser.parString(ok,1);
  3449 			}
  3450 			exportImage (fname,false,format);
  3451 		}
  3452 	/////////////////////////////////////////////////////////////////////
  3453 	} else if (com=="exportXHTML")
  3454 	{
  3455 		QString fname="";
  3456 		ok=true;
  3457 		if (parser.parCount()>=2)
  3458 			// Hey, we even have a filename
  3459 			fname=parser.parString(ok,1); 
  3460 		if (!ok)
  3461 		{
  3462 			parser.setError (Aborted,"Could not read filename");
  3463 		} else
  3464 		{
  3465 			exportXHTML (fname,false);
  3466 		}
  3467 	/////////////////////////////////////////////////////////////////////
  3468 	} else if (com=="exportXML")
  3469 	{
  3470 		QString fname="";
  3471 		ok=true;
  3472 		if (parser.parCount()>=2)
  3473 			// Hey, we even have a filename
  3474 			fname=parser.parString(ok,1); 
  3475 		if (!ok)
  3476 		{
  3477 			parser.setError (Aborted,"Could not read filename");
  3478 		} else
  3479 		{
  3480 			exportXML (fname,false);
  3481 		}
  3482 	/////////////////////////////////////////////////////////////////////
  3483 	} else if (com=="getHeading")
  3484 	{ 
  3485 		if (!selti)
  3486 		{
  3487 			parser.setError (Aborted,"Nothing selected");
  3488 		} else if (parser.checkParCount(0))
  3489 			returnValue=selti->getHeading();
  3490 	/////////////////////////////////////////////////////////////////////
  3491 	} else if (com=="importDir")
  3492 	{
  3493 		if (!selti)
  3494 		{
  3495 			parser.setError (Aborted,"Nothing selected");
  3496 		} else if (! selbi )
  3497 		{				  
  3498 			parser.setError (Aborted,"Type of selection is not a branch");
  3499 		} else if (parser.checkParCount(1))
  3500 		{
  3501 			s=parser.parString(ok,0);
  3502 			if (ok) importDirInt(s);
  3503 		}	
  3504 	/////////////////////////////////////////////////////////////////////
  3505 	} else if (com=="relinkTo")
  3506 	{
  3507 		if (!selti)
  3508 		{
  3509 			parser.setError (Aborted,"Nothing selected");
  3510 		} else if ( selbi)
  3511 		{
  3512 			if (parser.checkParCount(4))
  3513 			{
  3514 				// 0	selectstring of parent
  3515 				// 1	num in parent (for branches)
  3516 				// 2,3	x,y of mainbranch or mapcenter
  3517 				s=parser.parString(ok,0);
  3518 				TreeItem *dst=findBySelectString (s);
  3519 				if (dst)
  3520 				{	
  3521 					if (dst->getType()==TreeItem::Branch) 
  3522 					{
  3523 						// Get number in parent
  3524 						n=parser.parInt (ok,1);
  3525 						if (ok)
  3526 						{
  3527 							relinkBranch (selbi,(BranchItem*)dst,n);
  3528 							emitSelectionChanged();
  3529 						}	
  3530 					} else if (dst->getType()==TreeItem::MapCenter) 
  3531 					{
  3532 						relinkBranch (selbi,(BranchItem*)dst);
  3533 						// Get coordinates of mainbranch
  3534 						x=parser.parDouble(ok,2);
  3535 						if (ok)
  3536 						{
  3537 							y=parser.parDouble(ok,3);
  3538 							if (ok) 
  3539 							{
  3540 								if (selbi->getLMO()) selbi->getLMO()->move (x,y);
  3541 								emitSelectionChanged();
  3542 							}
  3543 						}
  3544 					}	
  3545 				}	
  3546 			}	
  3547 		} else if ( selti->getType() == TreeItem::Image) 
  3548 		{
  3549 			if (parser.checkParCount(1))
  3550 			{
  3551 				// 0	selectstring of parent
  3552 				s=parser.parString(ok,0);
  3553 				TreeItem *dst=findBySelectString (s);
  3554 				if (dst)
  3555 				{	
  3556 					if (dst->isBranchLikeType())
  3557 						relinkImage ( ((ImageItem*)selti),(BranchItem*)dst);
  3558 				} else	
  3559 					parser.setError (Aborted,"Destination is not a branch");
  3560 			}		
  3561 		} else
  3562 			parser.setError (Aborted,"Type of selection is not a floatimage or branch");
  3563 	/////////////////////////////////////////////////////////////////////
  3564 	} else if (com=="loadImage")
  3565 	{
  3566 		if (!selti)
  3567 		{
  3568 			parser.setError (Aborted,"Nothing selected");
  3569 		} else if (! selbi )
  3570 		{				  
  3571 			parser.setError (Aborted,"Type of selection is not a branch");
  3572 		} else if (parser.checkParCount(1))
  3573 		{
  3574 			s=parser.parString(ok,0);
  3575 			if (ok) loadFloatImageInt (selbi,s);
  3576 		}	
  3577 	/////////////////////////////////////////////////////////////////////
  3578 	} else if (com=="moveUp")
  3579 	{
  3580 		if (!selti )
  3581 		{
  3582 			parser.setError (Aborted,"Nothing selected");
  3583 		} else if (! selbi )
  3584 		{				  
  3585 			parser.setError (Aborted,"Type of selection is not a branch");
  3586 		} else if (parser.checkParCount(0))
  3587 		{
  3588 			moveUp();
  3589 		}	
  3590 	/////////////////////////////////////////////////////////////////////
  3591 	} else if (com=="moveDown")
  3592 	{
  3593 		if (!selti )
  3594 		{
  3595 			parser.setError (Aborted,"Nothing selected");
  3596 		} else if (! selbi )
  3597 		{				  
  3598 			parser.setError (Aborted,"Type of selection is not a branch");
  3599 		} else if (parser.checkParCount(0))
  3600 		{
  3601 			moveDown();
  3602 		}	
  3603 	/////////////////////////////////////////////////////////////////////
  3604 	} else if (com=="move")
  3605 	{
  3606 		if (!selti )
  3607 		{
  3608 			parser.setError (Aborted,"Nothing selected");
  3609 		} else if ( selectionType()!=TreeItem::Branch  && 
  3610 					selectionType()!=TreeItem::MapCenter  &&
  3611 					selectionType()!=TreeItem::Image )
  3612 		{				  
  3613 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3614 		} else if (parser.checkParCount(2))
  3615 		{	
  3616 			x=parser.parDouble (ok,0);
  3617 			if (ok)
  3618 			{
  3619 				y=parser.parDouble (ok,1);
  3620 				if (ok) move (x,y);
  3621 			}
  3622 		}	
  3623 	/////////////////////////////////////////////////////////////////////
  3624 	} else if (com=="moveRel")
  3625 	{
  3626 		if (!selti )
  3627 		{
  3628 			parser.setError (Aborted,"Nothing selected");
  3629 		} else if ( selectionType()!=TreeItem::Branch  && 
  3630 					selectionType()!=TreeItem::MapCenter  &&
  3631 					selectionType()!=TreeItem::Image )
  3632 		{				  
  3633 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3634 		} else if (parser.checkParCount(2))
  3635 		{	
  3636 			x=parser.parDouble (ok,0);
  3637 			if (ok)
  3638 			{
  3639 				y=parser.parDouble (ok,1);
  3640 				if (ok) moveRel (x,y);
  3641 			}
  3642 		}	
  3643 	/////////////////////////////////////////////////////////////////////
  3644 	} else if (com=="nop")
  3645 	{
  3646 	/////////////////////////////////////////////////////////////////////
  3647 	} else if (com=="paste")
  3648 	{
  3649 		if (!selti )
  3650 		{
  3651 			parser.setError (Aborted,"Nothing selected");
  3652 		} else if (! selbi )
  3653 		{				  
  3654 			parser.setError (Aborted,"Type of selection is not a branch");
  3655 		} else if (parser.checkParCount(1))
  3656 		{	
  3657 			n=parser.parInt (ok,0);
  3658 			if (ok) pasteNoSave(n);
  3659 		}	
  3660 	/////////////////////////////////////////////////////////////////////
  3661 	} else if (com=="qa")
  3662 	{
  3663 		if (!selti )
  3664 		{
  3665 			parser.setError (Aborted,"Nothing selected");
  3666 		} else if (! selbi )
  3667 		{				  
  3668 			parser.setError (Aborted,"Type of selection is not a branch");
  3669 		} else if (parser.checkParCount(4))
  3670 		{	
  3671 			QString c,u;
  3672 			c=parser.parString (ok,0);
  3673 			if (!ok)
  3674 			{
  3675 				parser.setError (Aborted,"No comment given");
  3676 			} else
  3677 			{
  3678 				s=parser.parString (ok,1);
  3679 				if (!ok)
  3680 				{
  3681 					parser.setError (Aborted,"First parameter is not a string");
  3682 				} else
  3683 				{
  3684 					t=parser.parString (ok,2);
  3685 					if (!ok)
  3686 					{
  3687 						parser.setError (Aborted,"Condition is not a string");
  3688 					} else
  3689 					{
  3690 						u=parser.parString (ok,3);
  3691 						if (!ok)
  3692 						{
  3693 							parser.setError (Aborted,"Third parameter is not a string");
  3694 						} else
  3695 						{
  3696 							if (s!="heading")
  3697 							{
  3698 								parser.setError (Aborted,"Unknown type: "+s);
  3699 							} else
  3700 							{
  3701 								if (! (t=="eq") ) 
  3702 								{
  3703 									parser.setError (Aborted,"Unknown operator: "+t);
  3704 								} else
  3705 								{
  3706 									if (! selbi    )
  3707 									{
  3708 										parser.setError (Aborted,"Type of selection is not a branch");
  3709 									} else
  3710 									{
  3711 										if (selbi->getHeading() == u)
  3712 										{
  3713 											cout << "PASSED: " << qPrintable (c)  << endl;
  3714 										} else
  3715 										{
  3716 											cout << "FAILED: " << qPrintable (c)  << endl;
  3717 										}
  3718 									}
  3719 								}
  3720 							}
  3721 						} 
  3722 					} 
  3723 				} 
  3724 			}
  3725 		}	
  3726 	/////////////////////////////////////////////////////////////////////
  3727 	} else if (com=="saveImage")
  3728 	{
  3729 		ImageItem *ii=getSelectedImage();
  3730 		if (!ii )
  3731 		{
  3732 			parser.setError (Aborted,"No image selected");
  3733 		} else if (parser.checkParCount(2))
  3734 		{
  3735 			s=parser.parString(ok,0);
  3736 			if (ok)
  3737 			{
  3738 				t=parser.parString(ok,1);
  3739 				if (ok) saveFloatImageInt (ii,t,s);
  3740 			}
  3741 		}	
  3742 	/////////////////////////////////////////////////////////////////////
  3743 	} else if (com=="scroll")
  3744 	{
  3745 		if (!selti)
  3746 		{
  3747 			parser.setError (Aborted,"Nothing selected");
  3748 		} else if (! selbi )
  3749 		{				  
  3750 			parser.setError (Aborted,"Type of selection is not a branch");
  3751 		} else if (parser.checkParCount(0))
  3752 		{	
  3753 			if (!scrollBranch (selbi))	
  3754 				parser.setError (Aborted,"Could not scroll branch");
  3755 		}	
  3756 	/////////////////////////////////////////////////////////////////////
  3757 	} else if (com=="select")
  3758 	{
  3759 		if (parser.checkParCount(1))
  3760 		{
  3761 			s=parser.parString(ok,0);
  3762 			if (ok) select (s);
  3763 		}	
  3764 	/////////////////////////////////////////////////////////////////////
  3765 	} else if (com=="selectLastBranch")
  3766 	{
  3767 		if (!selti )
  3768 		{
  3769 			parser.setError (Aborted,"Nothing selected");
  3770 		} else if (! selbi )
  3771 		{				  
  3772 			parser.setError (Aborted,"Type of selection is not a branch");
  3773 		} else if (parser.checkParCount(0))
  3774 		{	
  3775 			BranchItem *bi=selbi->getLastBranch();
  3776 			if (!bi)
  3777 				parser.setError (Aborted,"Could not select last branch");
  3778 			select (bi);		// FIXME-3 was selectInt
  3779 				
  3780 		}	
  3781 	/////////////////////////////////////////////////////////////////////
  3782 	} else /* FIXME-2 if (com=="selectLastImage")
  3783 	{
  3784 		if (!selti )
  3785 		{
  3786 			parser.setError (Aborted,"Nothing selected");
  3787 		} else if (! selbi )
  3788 		{				  
  3789 			parser.setError (Aborted,"Type of selection is not a branch");
  3790 		} else if (parser.checkParCount(0))
  3791 		{	
  3792 			FloatImageObj *fio=selb->getLastFloatImage();
  3793 			if (!fio)
  3794 				parser.setError (Aborted,"Could not select last image");
  3795 			select (fio);		// FIXME-3 was selectInt
  3796 				
  3797 		}	
  3798 	/////////////////////////////////////////////////////////////////////
  3799 	} else */ if (com=="selectLatestAdded")
  3800 	{
  3801 		if (!latestAddedItem)
  3802 		{
  3803 			parser.setError (Aborted,"No latest added object");
  3804 		} else
  3805 		{	
  3806 			if (!select (latestAddedItem))
  3807 				parser.setError (Aborted,"Could not select latest added object ");
  3808 		}	
  3809 	/////////////////////////////////////////////////////////////////////
  3810 	} else if (com=="setFlag")
  3811 	{
  3812 		if (!selti )
  3813 		{
  3814 			parser.setError (Aborted,"Nothing selected");
  3815 		} else if (! selbi )
  3816 		{				  
  3817 			parser.setError (Aborted,"Type of selection is not a branch");
  3818 		} else if (parser.checkParCount(1))
  3819 		{
  3820 			s=parser.parString(ok,0);
  3821 			if (ok) 
  3822 				selbi->activateStandardFlag(s);
  3823 		}
  3824 	/////////////////////////////////////////////////////////////////////
  3825 	} else if (com=="setFrameType")
  3826 	{
  3827 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3828 		{
  3829 			parser.setError (Aborted,"Type of selection does not allow setting frame type");
  3830 		}
  3831 		else if (parser.checkParCount(1))
  3832 		{
  3833 			s=parser.parString(ok,0);
  3834 			if (ok) setFrameType (s);
  3835 		}	
  3836 	/////////////////////////////////////////////////////////////////////
  3837 	} else if (com=="setFramePenColor")
  3838 	{
  3839 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3840 		{
  3841 			parser.setError (Aborted,"Type of selection does not allow setting of pen color");
  3842 		}
  3843 		else if (parser.checkParCount(1))
  3844 		{
  3845 			QColor c=parser.parColor(ok,0);
  3846 			if (ok) setFramePenColor (c);
  3847 		}	
  3848 	/////////////////////////////////////////////////////////////////////
  3849 	} else if (com=="setFrameBrushColor")
  3850 	{
  3851 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3852 		{
  3853 			parser.setError (Aborted,"Type of selection does not allow setting brush color");
  3854 		}
  3855 		else if (parser.checkParCount(1))
  3856 		{
  3857 			QColor c=parser.parColor(ok,0);
  3858 			if (ok) setFrameBrushColor (c);
  3859 		}	
  3860 	/////////////////////////////////////////////////////////////////////
  3861 	} else if (com=="setFramePadding")
  3862 	{
  3863 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3864 		{
  3865 			parser.setError (Aborted,"Type of selection does not allow setting frame padding");
  3866 		}
  3867 		else if (parser.checkParCount(1))
  3868 		{
  3869 			n=parser.parInt(ok,0);
  3870 			if (ok) setFramePadding(n);
  3871 		}	
  3872 	/////////////////////////////////////////////////////////////////////
  3873 	} else if (com=="setFrameBorderWidth")
  3874 	{
  3875 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3876 		{
  3877 			parser.setError (Aborted,"Type of selection does not allow setting frame border width");
  3878 		}
  3879 		else if (parser.checkParCount(1))
  3880 		{
  3881 			n=parser.parInt(ok,0);
  3882 			if (ok) setFrameBorderWidth (n);
  3883 		}	
  3884 	/////////////////////////////////////////////////////////////////////
  3885 	/* FIXME-2  else if (com=="setFrameType")
  3886 	{
  3887 		if (!selti )
  3888 		{
  3889 			parser.setError (Aborted,"Nothing selected");
  3890 		} else if (! selb )
  3891 		{				  
  3892 			parser.setError (Aborted,"Type of selection is not a branch");
  3893 		} else if (parser.checkParCount(1))
  3894 		{
  3895 			s=parser.parString(ok,0);
  3896 			if (ok) 
  3897 				setFrameType (s);
  3898 		}
  3899 	/////////////////////////////////////////////////////////////////////
  3900 	} else*/ 
  3901 	/////////////////////////////////////////////////////////////////////
  3902 	} else if (com=="setHeading")
  3903 	{
  3904 		if (!selti )
  3905 		{
  3906 			parser.setError (Aborted,"Nothing selected");
  3907 		} else if (! selbi )
  3908 		{				  
  3909 			parser.setError (Aborted,"Type of selection is not a branch");
  3910 		} else if (parser.checkParCount(1))
  3911 		{
  3912 			s=parser.parString (ok,0);
  3913 			if (ok) 
  3914 				setHeading (s);
  3915 		}	
  3916 	/////////////////////////////////////////////////////////////////////
  3917 	} else if (com=="setHideExport")
  3918 	{
  3919 		if (!selti )
  3920 		{
  3921 			parser.setError (Aborted,"Nothing selected");
  3922 		} else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
  3923 		{				  
  3924 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3925 		} else if (parser.checkParCount(1))
  3926 		{
  3927 			b=parser.parBool(ok,0);
  3928 			if (ok) setHideExport (b);
  3929 		}
  3930 	/////////////////////////////////////////////////////////////////////
  3931 	} else if (com=="setIncludeImagesHorizontally")
  3932 	{ 
  3933 		if (!selti )
  3934 		{
  3935 			parser.setError (Aborted,"Nothing selected");
  3936 		} else if (! selbi)
  3937 		{				  
  3938 			parser.setError (Aborted,"Type of selection is not a branch");
  3939 		} else if (parser.checkParCount(1))
  3940 		{
  3941 			b=parser.parBool(ok,0);
  3942 			if (ok) setIncludeImagesHor(b);
  3943 		}
  3944 	/////////////////////////////////////////////////////////////////////
  3945 	} else if (com=="setIncludeImagesVertically")
  3946 	{
  3947 		if (!selti )
  3948 		{
  3949 			parser.setError (Aborted,"Nothing selected");
  3950 		} else if (! selbi)
  3951 		{				  
  3952 			parser.setError (Aborted,"Type of selection is not a branch");
  3953 		} else if (parser.checkParCount(1))
  3954 		{
  3955 			b=parser.parBool(ok,0);
  3956 			if (ok) setIncludeImagesVer(b);
  3957 		}
  3958 	/////////////////////////////////////////////////////////////////////
  3959 	} else if (com=="setHideLinkUnselected")
  3960 	{
  3961 		if (!selti )
  3962 		{
  3963 			parser.setError (Aborted,"Nothing selected");
  3964 		} else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3965 		{				  
  3966 			parser.setError (Aborted,"Type of selection does not allow hiding the link");
  3967 		} else if (parser.checkParCount(1))
  3968 		{
  3969 			b=parser.parBool(ok,0);
  3970 			if (ok) setHideLinkUnselected(b);
  3971 		}
  3972 	/////////////////////////////////////////////////////////////////////
  3973 	} else if (com=="setMapAuthor")
  3974 	{
  3975 		if (parser.checkParCount(1))
  3976 		{
  3977 			s=parser.parString(ok,0);
  3978 			if (ok) setAuthor (s);
  3979 		}	
  3980 	/////////////////////////////////////////////////////////////////////
  3981 	} else if (com=="setMapComment")
  3982 	{
  3983 		if (parser.checkParCount(1))
  3984 		{
  3985 			s=parser.parString(ok,0);
  3986 			if (ok) setComment(s);
  3987 		}	
  3988 	/////////////////////////////////////////////////////////////////////
  3989 	} else if (com=="setMapBackgroundColor")
  3990 	{
  3991 		if (!selti )
  3992 		{
  3993 			parser.setError (Aborted,"Nothing selected");
  3994 		} else if (! selbi )
  3995 		{				  
  3996 			parser.setError (Aborted,"Type of selection is not a branch");
  3997 		} else if (parser.checkParCount(1))
  3998 		{
  3999 			QColor c=parser.parColor (ok,0);
  4000 			if (ok) setMapBackgroundColor (c);
  4001 		}	
  4002 	/////////////////////////////////////////////////////////////////////
  4003 	} else if (com=="setMapDefLinkColor")
  4004 	{
  4005 		if (!selti )
  4006 		{
  4007 			parser.setError (Aborted,"Nothing selected");
  4008 		} else if (! selbi )
  4009 		{				  
  4010 			parser.setError (Aborted,"Type of selection is not a branch");
  4011 		} else if (parser.checkParCount(1))
  4012 		{
  4013 			QColor c=parser.parColor (ok,0);
  4014 			if (ok) setMapDefLinkColor (c);
  4015 		}	
  4016 	/////////////////////////////////////////////////////////////////////
  4017 	} else if (com=="setMapLinkStyle")
  4018 	{
  4019 		if (parser.checkParCount(1))
  4020 		{
  4021 			s=parser.parString (ok,0);
  4022 			if (ok) setMapLinkStyle(s);
  4023 		}	
  4024 	/////////////////////////////////////////////////////////////////////
  4025 	} else if (com=="setNote")
  4026 	{
  4027 		if (!selti )
  4028 		{
  4029 			parser.setError (Aborted,"Nothing selected");
  4030 		} else if (! selbi )
  4031 		{				  
  4032 			parser.setError (Aborted,"Type of selection is not a branch");
  4033 		} else if (parser.checkParCount(1))
  4034 		{
  4035 			s=parser.parString (ok,0);
  4036 			if (ok) 
  4037 				setNote (s);
  4038 		}	
  4039 	/////////////////////////////////////////////////////////////////////
  4040 	} else if (com=="setSelectionColor")
  4041 	{
  4042 		if (parser.checkParCount(1))
  4043 		{
  4044 			QColor c=parser.parColor (ok,0);
  4045 			if (ok) setSelectionColorInt (c);
  4046 		}	
  4047 	/////////////////////////////////////////////////////////////////////
  4048 	} else if (com=="setURL")
  4049 	{
  4050 		if (!selti )
  4051 		{
  4052 			parser.setError (Aborted,"Nothing selected");
  4053 		} else if (! selbi )
  4054 		{				  
  4055 			parser.setError (Aborted,"Type of selection is not a branch");
  4056 		} else if (parser.checkParCount(1))
  4057 		{
  4058 			s=parser.parString (ok,0);
  4059 			if (ok) setURL(s);
  4060 		}	
  4061 	/////////////////////////////////////////////////////////////////////
  4062 	} else if (com=="setVymLink")
  4063 	{
  4064 		if (!selti )
  4065 		{
  4066 			parser.setError (Aborted,"Nothing selected");
  4067 		} else if (! selbi )
  4068 		{				  
  4069 			parser.setError (Aborted,"Type of selection is not a branch");
  4070 		} else if (parser.checkParCount(1))
  4071 		{
  4072 			s=parser.parString (ok,0);
  4073 			if (ok) setVymLink(s);
  4074 		}	
  4075 	} else if (com=="sortChildren")
  4076 	{
  4077 		if (!selti )
  4078 		{
  4079 			parser.setError (Aborted,"Nothing selected");
  4080 		} else if (! selbi )
  4081 		{				  
  4082 			parser.setError (Aborted,"Type of selection is not a branch");
  4083 		} else if (parser.checkParCount(0))
  4084 		{
  4085 			sortChildren();
  4086 		} else if (parser.checkParCount(1))
  4087 		{
  4088 			b=parser.parBool(ok,0);
  4089 			if (ok) sortChildren(b);
  4090 		}
  4091 	/////////////////////////////////////////////////////////////////////
  4092 	} else if (com=="toggleFlag")
  4093 	{
  4094 		if (!selti )
  4095 		{
  4096 			parser.setError (Aborted,"Nothing selected");
  4097 		} else if (! selbi )
  4098 		{				  
  4099 			parser.setError (Aborted,"Type of selection is not a branch");
  4100 		} else if (parser.checkParCount(1))
  4101 		{
  4102 			s=parser.parString(ok,0);
  4103 			if (ok) 
  4104 				selbi->toggleStandardFlag(s);	
  4105 		}
  4106 	/////////////////////////////////////////////////////////////////////
  4107 	} else  if (com=="unscroll")
  4108 	{
  4109 		if (!selti)
  4110 		{
  4111 			parser.setError (Aborted,"Nothing selected");
  4112 		} else if (! selbi )
  4113 		{				  
  4114 			parser.setError (Aborted,"Type of selection is not a branch");
  4115 		} else if (parser.checkParCount(0))
  4116 		{	
  4117 			if (!unscrollBranch (selbi))	
  4118 				parser.setError (Aborted,"Could not unscroll branch");
  4119 		}	
  4120 	/////////////////////////////////////////////////////////////////////
  4121 	} else if (com=="unscrollChildren")
  4122 	{
  4123 		if (!selti)
  4124 		{
  4125 			parser.setError (Aborted,"Nothing selected");
  4126 		} else if (! selbi )
  4127 		{				  
  4128 			parser.setError (Aborted,"Type of selection is not a branch");
  4129 		} else if (parser.checkParCount(0))
  4130 		{	
  4131 			unscrollChildren ();
  4132 		}	
  4133 	/////////////////////////////////////////////////////////////////////
  4134 	} else if (com=="unsetFlag")
  4135 	{
  4136 		if (!selti)
  4137 		{
  4138 			parser.setError (Aborted,"Nothing selected");
  4139 		} else if (! selbi )
  4140 		{				  
  4141 			parser.setError (Aborted,"Type of selection is not a branch");
  4142 		} else if (parser.checkParCount(1))
  4143 		{
  4144 			s=parser.parString(ok,0);
  4145 			if (ok) 
  4146 				selbi->deactivateStandardFlag(s);
  4147 		}
  4148 	} else 
  4149 		parser.setError (Aborted,"Unknown command");
  4150 
  4151 	// Any errors?
  4152 	if (parser.errorLevel()==NoError)
  4153 	{
  4154 		// setChanged();  FIXME-2 should not be called e.g. for export?!
  4155 		reposition();
  4156 		errorMsg.clear();
  4157 		noErr=true;
  4158 	}	
  4159 	else	
  4160 	{
  4161 		// TODO Error handling
  4162 		qWarning("VymModel::parseAtom: Error!");
  4163 
  4164 		qWarning(parser.errorMessage());
  4165 		noErr=false;
  4166 		errorMsg=parser.errorMessage();
  4167 	} 
  4168 	return returnValue;
  4169 }
  4170 
  4171 QVariant VymModel::runScript (const QString &script)
  4172 {
  4173 	parser.setScript (script);
  4174 	parser.runScript();
  4175 	QVariant r;
  4176 	bool noErr=true;
  4177 	QString errMsg;
  4178 	while (parser.next() && noErr) 
  4179 	{
  4180 		r=parseAtom(parser.getAtom(),noErr,errMsg);
  4181 		if (!noErr)	//FIXME-3 need dialog box here
  4182 			cout << "VM::runScript aborted:\n"<<errMsg.toStdString()<<endl;
  4183 	}	
  4184 	return r;
  4185 }
  4186 
  4187 void VymModel::setExportMode (bool b)
  4188 {
  4189 	// should be called before and after exports
  4190 	// depending on the settings
  4191 	if (b && settings.value("/export/useHideExport","true")=="true")
  4192 		setHideTmpMode (TreeItem::HideExport);
  4193 	else	
  4194 		setHideTmpMode (TreeItem::HideNone);
  4195 }
  4196 
  4197 void VymModel::exportImage(QString fname, bool askName, QString format)
  4198 {
  4199 	if (fname=="")
  4200 	{
  4201 		fname=getMapName()+".png";
  4202 		format="PNG";
  4203 	} 	
  4204 
  4205 	if (askName)
  4206 	{
  4207 		QStringList fl;
  4208 		QFileDialog *fd=new QFileDialog (NULL);
  4209 		fd->setCaption (tr("Export map as image"));
  4210 		fd->setDirectory (lastImageDir);
  4211 		fd->setFileMode(QFileDialog::AnyFile);
  4212 		fd->setFilters  (imageIO.getFilters() );
  4213 		if (fd->exec())
  4214 		{
  4215 			fl=fd->selectedFiles();
  4216 			fname=fl.first();
  4217 			format=imageIO.getType(fd->selectedFilter());
  4218 		} 
  4219 	}
  4220 
  4221 	setExportMode (true);
  4222 	mapEditor->getScene()->update();		// FIXME-3 check this...
  4223 	QImage img (mapEditor->getImage());	//FIXME-3 calls getTotalBBox, but also in ExportHTML::doExport()
  4224 	img.save(fname, format);
  4225 	setExportMode (false);
  4226 }
  4227 
  4228 
  4229 void VymModel::exportXML(QString dir, bool askForName)
  4230 {
  4231 	if (askForName)
  4232 	{
  4233 		dir=browseDirectory(NULL,tr("Export XML to directory"));
  4234 		if (dir =="" && !reallyWriteDirectory(dir) )
  4235 		return;
  4236 	}
  4237 
  4238 	// Hide stuff during export, if settings want this
  4239 	setExportMode (true);
  4240 
  4241 	// Create subdirectories
  4242 	makeSubDirs (dir);
  4243 
  4244 	// write to directory	//FIXME-4 check totalBBox here...
  4245 	QString saveFile=saveToDir (dir,mapName+"-",true,mapEditor->getTotalBBox().topLeft() ,NULL); 
  4246 	QFile file;
  4247 
  4248 	file.setName ( dir + "/"+mapName+".xml");
  4249 	if ( !file.open( QIODevice::WriteOnly ) )
  4250 	{
  4251 		// This should neverever happen
  4252 		QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
  4253 		return;
  4254 	}	
  4255 
  4256 	// Write it finally, and write in UTF8, no matter what 
  4257 	QTextStream ts( &file );
  4258 	ts.setEncoding (QTextStream::UnicodeUTF8);
  4259 	ts << saveFile;
  4260 	file.close();
  4261 
  4262 	// Now write image, too
  4263 	exportImage (dir+"/images/"+mapName+".png",false,"PNG");
  4264 
  4265 	setExportMode (false);
  4266 }
  4267 
  4268 void VymModel::exportAO (QString fname,bool askName)
  4269 {
  4270 	ExportAO ex;
  4271 	ex.setModel (this);
  4272 	if (fname=="") 
  4273 		ex.setFile (mapName+".txt");	
  4274 	else
  4275 		ex.setFile (fname);
  4276 
  4277 	if (askName)
  4278 	{
  4279 		//ex.addFilter ("TXT (*.txt)");
  4280 		ex.setDir(lastImageDir);
  4281 		//ex.setCaption(vymName+ " -" +tr("Export as A&O report")+" "+tr("(still experimental)"));
  4282 		ex.execDialog() ; 
  4283 	} 
  4284 	if (!ex.canceled())
  4285 	{
  4286 		setExportMode(true);
  4287 		ex.doExport();
  4288 		setExportMode(false);
  4289 	}
  4290 }
  4291 
  4292 void VymModel::exportASCII(QString fname,bool askName)
  4293 {
  4294 	ExportASCII ex;
  4295 	ex.setModel (this);
  4296 	if (fname=="") 
  4297 		ex.setFile (mapName+".txt");	
  4298 	else
  4299 		ex.setFile (fname);
  4300 
  4301 	if (askName)
  4302 	{
  4303 		//ex.addFilter ("TXT (*.txt)");
  4304 		ex.setDir(lastImageDir);
  4305 		//ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
  4306 		ex.execDialog() ; 
  4307 	} 
  4308 	if (!ex.canceled())
  4309 	{
  4310 		setExportMode(true);
  4311 		ex.doExport();
  4312 		setExportMode(false);
  4313 	}
  4314 }
  4315 
  4316 void VymModel::exportHTML (const QString &dir, bool askForName)	//FIXME-2 own dialogue missing and also option to save settings in map
  4317 {
  4318 	ExportXHTMLDialog dia(NULL);	
  4319 	dia.setFilePath (filePath );
  4320 	dia.setMapName (mapName );
  4321 	dia.readSettings();
  4322 	if (dir!="") dia.setDir (dir);
  4323 	QDir d;
  4324 	bool ok=true;
  4325 	
  4326 	if (askForName)
  4327 	{
  4328 		if (dia.exec()!=QDialog::Accepted) 
  4329 			ok=false;
  4330 		else	
  4331 		{
  4332 			d=dia.getDir();
  4333 			// Check, if warnings should be used before overwriting
  4334 			// the output directory
  4335 			if (d.exists() && d.count()>0)
  4336 			{
  4337 				WarningDialog warn;
  4338 				warn.showCancelButton (true);
  4339 				warn.setText(QString(
  4340 					"The directory %1 is not empty.\n"
  4341 					"Do you risk to overwrite some of its contents?").arg(d.path() ));
  4342 				warn.setCaption("Warning: Directory not empty");
  4343 				warn.setShowAgainName("mainwindow/export-XML-overwrite-dir");
  4344 
  4345 				if (warn.exec()!=QDialog::Accepted) ok=false;
  4346 			}
  4347 		}	
  4348 	}
  4349 	if (ok)
  4350 	{
  4351 		// Hide stuff during export, if settings want this
  4352 		setExportMode (true);
  4353 
  4354 		ExportHTML ex (this);
  4355 		ex.setFile (d.path()+"/"+mapName+".html");
  4356 		cout << "VM::exportHTML  writing "<<ex.getFile().toStdString()<<endl;
  4357 		ex.doExport();
  4358 		setExportMode (false);
  4359 
  4360 		//exportXML (dia.getDir(),false );
  4361 		//dia.doExport(mapName );
  4362 		//if (dia.hasChanged()) setChanged();
  4363 
  4364 		// Write image, too
  4365 		exportImage (d.path()+"/"+mapName+".png",false,"PNG");
  4366 
  4367 	}
  4368 }
  4369 
  4370 void VymModel::exportXHTML (const QString &dir, bool askForName)
  4371 {
  4372 			ExportXHTMLDialog dia(NULL);
  4373 			dia.setFilePath (filePath );
  4374 			dia.setMapName (mapName );
  4375 			dia.readSettings();
  4376 			if (dir!="") dia.setDir (dir);
  4377 
  4378 			bool ok=true;
  4379 			
  4380 			if (askForName)
  4381 			{
  4382 				if (dia.exec()!=QDialog::Accepted) 
  4383 					ok=false;
  4384 				else	
  4385 				{
  4386 					QDir d (dia.getDir());
  4387 					// Check, if warnings should be used before overwriting
  4388 					// the output directory
  4389 					if (d.exists() && d.count()>0)
  4390 					{
  4391 						WarningDialog warn;
  4392 						warn.showCancelButton (true);
  4393 						warn.setText(QString(
  4394 							"The directory %1 is not empty.\n"
  4395 							"Do you risk to overwrite some of its contents?").arg(d.path() ));
  4396 						warn.setCaption("Warning: Directory not empty");
  4397 						warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
  4398 
  4399 						if (warn.exec()!=QDialog::Accepted) ok=false;
  4400 					}
  4401 				}	
  4402 			}
  4403 
  4404 			if (ok)
  4405 			{
  4406 				exportXML (dia.getDir(),false );
  4407 				dia.doExport(mapName );
  4408 				//if (dia.hasChanged()) setChanged();
  4409 			}
  4410 }
  4411 
  4412 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
  4413 {
  4414 	ExportOO ex;
  4415 	ex.setFile (fn);
  4416 	ex.setModel (this);
  4417 	if (ex.setConfigFile(cf)) 
  4418 	{
  4419 		setExportMode (true);
  4420 		ex.exportPresentation();
  4421 		setExportMode (false);
  4422 	}
  4423 }
  4424 
  4425 
  4426 
  4427 
  4428 //////////////////////////////////////////////
  4429 // View related
  4430 //////////////////////////////////////////////
  4431 
  4432 void VymModel::registerEditor(QWidget *me)
  4433 {
  4434 	mapEditor=(MapEditor*)me;
  4435 }
  4436 
  4437 void VymModel::unregisterEditor(QWidget *)
  4438 {
  4439 	mapEditor=NULL;
  4440 }
  4441 
  4442 void VymModel::setContextPos(QPointF p)
  4443 {
  4444 	contextPos=p;
  4445 }
  4446 
  4447 void VymModel::unsetContextPos()
  4448 {
  4449 	contextPos=QPointF();
  4450 }
  4451 
  4452 void VymModel::updateNoteFlag()
  4453 {
  4454 	TreeItem *selti=getSelectedItem();
  4455 	if (selti)
  4456 	{
  4457 		if (!mapChanged)
  4458 		{
  4459 			setChanged();
  4460 			updateActions();
  4461 		}
  4462 
  4463 		if (textEditor->isEmpty()) 
  4464 			selti->clearNote();
  4465 		else
  4466 			selti->setNote (textEditor->getText());
  4467 		emitDataHasChanged(selti);		
  4468 	}
  4469 }
  4470 
  4471 void VymModel::reposition()	//FIXME-4 VM should have no need to reposition, but the views...
  4472 {
  4473 	//cout << "VM::reposition blocked="<<blockReposition<<endl;
  4474 	if (blockReposition) return;
  4475 
  4476 	for (int i=0;i<rootItem->branchCount(); i++)
  4477 		rootItem->getBranchObjNum(i)->reposition();	//	for positioning heading
  4478 	//emitSelectionChanged();	
  4479 }
  4480 
  4481 
  4482 void VymModel::setMapLinkStyle (const QString & s)
  4483 {
  4484 	QString snow;
  4485 	switch (linkstyle)
  4486 	{
  4487 		case LinkableMapObj::Line :
  4488 			snow="StyleLine";
  4489 			break;
  4490 		case LinkableMapObj::Parabel:
  4491 			snow="StyleParabel";
  4492 			break;
  4493 		case LinkableMapObj::PolyLine:
  4494 			snow="StylePolyLine";
  4495 			break;
  4496 		case LinkableMapObj::PolyParabel:
  4497 			snow="StylePolyParabel";
  4498 			break;
  4499 		default:	
  4500 			snow="UndefinedStyle";
  4501 			break;
  4502 	}
  4503 
  4504 	saveState (
  4505 		QString("setMapLinkStyle (\"%1\")").arg(s),
  4506 		QString("setMapLinkStyle (\"%1\")").arg(snow),
  4507 		QString("Set map link style (\"%1\")").arg(s)
  4508 	);	
  4509 
  4510 	if (s=="StyleLine")
  4511 		linkstyle=LinkableMapObj::Line;
  4512 	else if (s=="StyleParabel")
  4513 		linkstyle=LinkableMapObj::Parabel;
  4514 	else if (s=="StylePolyLine")
  4515 		linkstyle=LinkableMapObj::PolyLine;
  4516 	else if (s=="StylePolyParabel")	
  4517 		linkstyle=LinkableMapObj::PolyParabel;
  4518 	else
  4519 		linkstyle=LinkableMapObj::UndefinedStyle;
  4520 
  4521 	BranchItem *cur=NULL;
  4522 	BranchItem *prev=NULL;
  4523 	BranchObj *bo;
  4524 	nextBranch (cur,prev);
  4525 	while (cur) 
  4526 	{
  4527 		bo=(BranchObj*)(cur->getLMO() );
  4528 		bo->setLinkStyle(bo->getDefLinkStyle(cur->parent() ));	//FIXME-3 better emit dataCHanged and leave the changes to View
  4529 		cur=nextBranch(cur,prev);
  4530 	}
  4531 	reposition();
  4532 }
  4533 
  4534 LinkableMapObj::Style VymModel::getMapLinkStyle ()
  4535 {
  4536 	return linkstyle;
  4537 }	
  4538 
  4539 void VymModel::setMapDefLinkColor(QColor col)
  4540 {
  4541 	if ( !col.isValid() ) return;
  4542 	saveState (
  4543 		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
  4544 		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
  4545 		QString("Set map link color to %1").arg(col.name())
  4546 	);
  4547 
  4548 	defLinkColor=col;
  4549 	BranchItem *cur=NULL;
  4550 	BranchItem *prev=NULL;
  4551 	BranchObj *bo;
  4552 	cur=nextBranch(cur,prev);
  4553 	while (cur) 
  4554 	{
  4555 		bo=(BranchObj*)(cur->getLMO() );
  4556 		bo->setLinkColor();
  4557 		nextBranch(cur,prev);
  4558 	}
  4559 	updateActions();
  4560 }
  4561 
  4562 void VymModel::setMapLinkColorHintInt()
  4563 {
  4564 	// called from setMapLinkColorHint(lch) or at end of parse
  4565 	BranchItem *cur=NULL;
  4566 	BranchItem *prev=NULL;
  4567 	BranchObj *bo;
  4568 	cur=nextBranch(cur,prev);
  4569 	while (cur) 
  4570 	{
  4571 		bo=(BranchObj*)(cur->getLMO() );
  4572 		bo->setLinkColor();
  4573 		cur=nextBranch(cur,prev);
  4574 	}
  4575 }
  4576 
  4577 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
  4578 {
  4579 	linkcolorhint=lch;
  4580 	setMapLinkColorHintInt();
  4581 }
  4582 
  4583 void VymModel::toggleMapLinkColorHint()
  4584 {
  4585 	if (linkcolorhint==LinkableMapObj::HeadingColor)
  4586 		linkcolorhint=LinkableMapObj::DefaultColor;
  4587 	else	
  4588 		linkcolorhint=LinkableMapObj::HeadingColor;
  4589 	BranchItem *cur=NULL;
  4590 	BranchItem *prev=NULL;
  4591 	BranchObj *bo;
  4592 	cur=nextBranch(cur,prev);
  4593 	while (cur) 
  4594 	{
  4595 		bo=(BranchObj*)(cur->getLMO() );
  4596 		bo->setLinkColor();
  4597 		nextBranch(cur,prev);
  4598 	}
  4599 }
  4600 
  4601 void VymModel::selectMapBackgroundImage ()	// FIXME-3 for using background image: view.setCacheMode(QGraphicsView::CacheBackground);  Also this belongs into ME
  4602 {
  4603 	Q3FileDialog *fd=new Q3FileDialog( NULL);
  4604 	fd->setMode (Q3FileDialog::ExistingFile);
  4605 	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  4606 	ImagePreview *p =new ImagePreview (fd);
  4607 	fd->setContentsPreviewEnabled( TRUE );
  4608 	fd->setContentsPreview( p, p );
  4609 	fd->setPreviewMode( Q3FileDialog::Contents );
  4610 	fd->setCaption(vymName+" - " +tr("Load background image"));
  4611 	fd->setDir (lastImageDir);
  4612 	fd->show();
  4613 
  4614 	if ( fd->exec() == QDialog::Accepted )
  4615 	{
  4616 		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
  4617 		lastImageDir=QDir (fd->dirPath());
  4618 		setMapBackgroundImage (fd->selectedFile());
  4619 	}
  4620 }	
  4621 
  4622 void VymModel::setMapBackgroundImage (const QString &fn)	//FIXME-3 missing savestate, move to ME
  4623 {
  4624 	QColor oldcol=mapScene->backgroundBrush().color();
  4625 	/*
  4626 	saveState(
  4627 		selection,
  4628 		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
  4629 		selection,
  4630 		QString ("setMapBackgroundImage (%1)").arg(col.name()),
  4631 		QString("Set background color of map to %1").arg(col.name()));
  4632 	*/	
  4633 	QBrush brush;
  4634 	brush.setTextureImage (QPixmap (fn));
  4635 	mapScene->setBackgroundBrush(brush);
  4636 }
  4637 
  4638 void VymModel::selectMapBackgroundColor()	// FIXME-3 move to ME
  4639 {
  4640 	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
  4641 	if ( !col.isValid() ) return;
  4642 	setMapBackgroundColor( col );
  4643 }
  4644 
  4645 
  4646 void VymModel::setMapBackgroundColor(QColor col)	// FIXME-3 move to ME
  4647 {
  4648 	QColor oldcol=mapScene->backgroundBrush().color();
  4649 	saveState(
  4650 		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
  4651 		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
  4652 		QString("Set background color of map to %1").arg(col.name()));
  4653 	mapScene->setBackgroundBrush(col);
  4654 }
  4655 
  4656 QColor VymModel::getMapBackgroundColor()	// FIXME-3 move to ME
  4657 {
  4658     return mapScene->backgroundBrush().color();
  4659 }
  4660 
  4661 
  4662 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()	// FIXME-3 move to ME
  4663 {
  4664 	return linkcolorhint;
  4665 }
  4666 
  4667 QColor VymModel::getMapDefLinkColor()	// FIXME-3 move to ME
  4668 {
  4669 	return defLinkColor;
  4670 }
  4671 
  4672 void VymModel::setMapDefXLinkColor(QColor col)	// FIXME-3 move to ME
  4673 {
  4674 	defXLinkColor=col;
  4675 }
  4676 
  4677 QColor VymModel::getMapDefXLinkColor()	// FIXME-3 move to ME
  4678 {
  4679 	return defXLinkColor;
  4680 }
  4681 
  4682 void VymModel::setMapDefXLinkWidth (int w)	// FIXME-3 move to ME
  4683 {
  4684 	defXLinkWidth=w;
  4685 }
  4686 
  4687 int VymModel::getMapDefXLinkWidth()	// FIXME-3 move to ME
  4688 {
  4689 	return defXLinkWidth;
  4690 }
  4691 
  4692 void VymModel::move(const double &x, const double &y)
  4693 {
  4694 	int i=x; i=y;
  4695 	MapItem *seli = (MapItem*)getSelectedItem();
  4696 	if (seli && (seli->isBranchLikeType() || seli->getType()==TreeItem::Image))
  4697 	{
  4698 		LinkableMapObj *lmo=seli->getLMO();
  4699 		if (lmo)
  4700 		{
  4701 			QPointF ap(lmo->getAbsPos());
  4702 			QPointF to(x, y);
  4703 			if (ap != to)
  4704 			{
  4705 				QString ps=qpointFToString(ap);
  4706 				QString s=getSelectString(seli);
  4707 				saveState(
  4708 					s, "move "+ps, 
  4709 					s, "move "+qpointFToString(to), 
  4710 					QString("Move %1 to %2").arg(getObjectName(seli)).arg(ps));
  4711 				lmo->move(x,y);
  4712 				reposition();
  4713 				emitSelectionChanged();
  4714 			}
  4715 		}
  4716 	}
  4717 }
  4718 
  4719 void VymModel::moveRel (const double &x, const double &y)	
  4720 {
  4721 	int i=x; i=y;
  4722 	MapItem *seli = (MapItem*)getSelectedItem();
  4723 	if (seli && (seli->isBranchLikeType() || seli->getType()==TreeItem::Image))
  4724 	{
  4725 		LinkableMapObj *lmo=seli->getLMO();
  4726 		if (lmo)
  4727 		{
  4728 			QPointF rp(lmo->getRelPos());
  4729 			QPointF to(x, y);
  4730 			if (rp != to)
  4731 			{
  4732 				QString ps=qpointFToString (lmo->getRelPos());
  4733 				QString s=getSelectString(seli);
  4734 				saveState(
  4735 					s, "moveRel "+ps, 
  4736 					s, "moveRel "+qpointFToString(to), 
  4737 					QString("Move %1 to relative position %2").arg(getObjectName(seli)).arg(ps));
  4738 				((OrnamentedObj*)lmo)->move2RelPos (x,y);
  4739 				reposition();
  4740 				lmo->updateLinkGeometry();
  4741 				emitSelectionChanged();
  4742 			}
  4743 		}	
  4744 	}
  4745 }
  4746 
  4747 
  4748 void VymModel::animate()
  4749 {
  4750 	animationTimer->stop();
  4751 	BranchObj *bo;
  4752 	int i=0;
  4753 	while (i<animObjList.size() )
  4754 	{
  4755 		bo=(BranchObj*)animObjList.at(i);
  4756 		if (!bo->animate())
  4757 		{
  4758 			if (i>=0) 
  4759 			{	
  4760 				animObjList.removeAt(i);
  4761 				i--;
  4762 			}
  4763 		}
  4764 		bo->reposition();
  4765 		i++;
  4766 	} 
  4767 	QItemSelection sel=selModel->selection();
  4768 	emit (selectionChanged(sel,sel));
  4769 
  4770 	mapScene->update();
  4771 	if (!animObjList.isEmpty()) animationTimer->start();
  4772 }
  4773 
  4774 
  4775 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
  4776 {
  4777 	if (start==dest) return;
  4778 	if (bo && bo->getTreeItem()->depth()>0) 
  4779 	{
  4780 		AnimPoint ap;
  4781 		ap.setStart (start);
  4782 		ap.setDest  (dest);
  4783 		ap.setTicks (animationTicks);
  4784 		ap.setAnimated (true);
  4785 		bo->setAnimation (ap);
  4786 		animObjList.append( bo );
  4787 		animationTimer->setSingleShot (true);
  4788 		animationTimer->start(animationInterval);
  4789 	}
  4790 }
  4791 
  4792 void VymModel::stopAnimation (MapObj *mo)
  4793 {
  4794 	int i=animObjList.indexOf(mo);
  4795     if (i>=0)
  4796 		animObjList.removeAt (i);
  4797 }
  4798 
  4799 void VymModel::sendSelection()
  4800 {
  4801 	if (netstate!=Server) return;
  4802 	sendData (QString("select (\"%1\")").arg(getSelectString()) );
  4803 }
  4804 
  4805 void VymModel::newServer()
  4806 {
  4807 	port=54321;
  4808 	sendCounter=0;
  4809     tcpServer = new QTcpServer(this);
  4810     if (!tcpServer->listen(QHostAddress::Any,port)) {
  4811         QMessageBox::critical(NULL, "vym server",
  4812                               QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
  4813         //FIXME-3 needed? we are no widget any longer... close();
  4814         return;
  4815     }
  4816 	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
  4817 	netstate=Server;
  4818 	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
  4819 }
  4820 
  4821 void VymModel::connectToServer()
  4822 {
  4823 	port=54321;
  4824 	server="salam.suse.de";
  4825 	server="localhost";
  4826 	clientSocket = new QTcpSocket (this);
  4827 	clientSocket->abort();
  4828     clientSocket->connectToHost(server ,port);
  4829 	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
  4830     connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
  4831             this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
  4832 	netstate=Client;		
  4833 	cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
  4834 
  4835 	
  4836 }
  4837 
  4838 void VymModel::newClient()
  4839 {
  4840     QTcpSocket *newClient = tcpServer->nextPendingConnection();
  4841     connect(newClient, SIGNAL(disconnected()),
  4842             newClient, SLOT(deleteLater()));
  4843 
  4844 	cout <<"ME::newClient  at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
  4845 
  4846 	clientList.append (newClient);
  4847 }
  4848 
  4849 
  4850 void VymModel::sendData(const QString &s)
  4851 {
  4852 	if (clientList.size()==0) return;
  4853 
  4854 	// Create bytearray to send
  4855 	QByteArray block;
  4856     QDataStream out(&block, QIODevice::WriteOnly);
  4857     out.setVersion(QDataStream::Qt_4_0);
  4858 
  4859 	// Reserve some space for blocksize
  4860     out << (quint16)0;
  4861 
  4862 	// Write sendCounter
  4863     out << sendCounter++;
  4864 
  4865 	// Write data
  4866     out << s;
  4867 
  4868 	// Go back and write blocksize so far
  4869     out.device()->seek(0);
  4870     quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
  4871 	out << bs;
  4872 
  4873 	if (debug)
  4874 		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<qPrintable(s)<<endl;
  4875 
  4876 	for (int i=0; i<clientList.size(); ++i)
  4877 	{
  4878 		//cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
  4879 		clientList.at(i)->write (block);
  4880 	}
  4881 }
  4882 
  4883 void VymModel::readData ()
  4884 {
  4885 	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
  4886 	{
  4887 		if (debug)
  4888 			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
  4889 		quint16 recCounter;
  4890 		quint16 blockSize;
  4891 
  4892 		QDataStream in(clientSocket);
  4893 		in.setVersion(QDataStream::Qt_4_0);
  4894 
  4895 		in >> blockSize;
  4896 		in >> recCounter;
  4897 		
  4898 		QString t;
  4899 		in >>t;
  4900 		if (debug)
  4901 			cout << "VymModel::readData  command="<<qPrintable (t)<<endl;
  4902 		bool noErr;
  4903 		QString errMsg;
  4904 		parseAtom (t,noErr,errMsg);
  4905 
  4906 	}
  4907 	return;
  4908 }
  4909 
  4910 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
  4911 {
  4912     switch (socketError) {
  4913     case QAbstractSocket::RemoteHostClosedError:
  4914         break;
  4915     case QAbstractSocket::HostNotFoundError:
  4916         QMessageBox::information(NULL, vymName +" Network client",
  4917                                  "The host was not found. Please check the "
  4918                                     "host name and port settings.");
  4919         break;
  4920     case QAbstractSocket::ConnectionRefusedError:
  4921         QMessageBox::information(NULL, vymName + " Network client",
  4922                                  "The connection was refused by the peer. "
  4923                                     "Make sure the fortune server is running, "
  4924                                     "and check that the host name and port "
  4925                                     "settings are correct.");
  4926         break;
  4927     default:
  4928         QMessageBox::information(NULL, vymName + " Network client",
  4929                                  QString("The following error occurred: %1.")
  4930                                  .arg(clientSocket->errorString()));
  4931     }
  4932 }
  4933 
  4934 /* FIXME-3 Playing with DBUS...
  4935 QDBusVariant VymModel::query (const QString &query)
  4936 {
  4937 	TreeItem *selti=getSelectedItem();
  4938 	if (selti)
  4939 		return QDBusVariant (selti->getHeading());
  4940 	else
  4941 		return QDBusVariant ("Nothing selected.");
  4942 }
  4943 */
  4944 
  4945 void VymModel::testslot()	//FIXME-3 Playing with DBUS
  4946 {
  4947 	cout << "VM::testslot called\n";
  4948 }
  4949 
  4950 void VymModel::selectMapSelectionColor()
  4951 {
  4952 	QColor col = QColorDialog::getColor( defLinkColor, NULL);
  4953 	setSelectionColor (col);
  4954 }
  4955 
  4956 void VymModel::setSelectionColorInt (QColor col)
  4957 {
  4958 	if ( !col.isValid() ) return;
  4959 	saveState (
  4960 		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
  4961 		QString("setSelectionColor (%1)").arg(col.name()),
  4962 		QString("Set color of selection box to %1").arg(col.name())
  4963 	);
  4964 
  4965 	mapEditor->setSelectionColor (col);
  4966 }
  4967 
  4968 void VymModel::emitSelectionChanged(const QItemSelection &newsel)
  4969 {
  4970 	emit (selectionChanged(newsel,newsel));	// needed e.g. to update geometry in editor
  4971 	//FIXME-3 emitShowSelection();
  4972 	sendSelection();
  4973 }
  4974 
  4975 void VymModel::emitSelectionChanged()
  4976 {
  4977 	QItemSelection newsel=selModel->selection();
  4978 	emitSelectionChanged (newsel);
  4979 }
  4980 
  4981 void VymModel::setSelectionColor(QColor col)
  4982 {
  4983 	if ( !col.isValid() ) return;
  4984 	saveState (
  4985 		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
  4986 		QString("setSelectionColor (%1)").arg(col.name()),
  4987 		QString("Set color of selection box to %1").arg(col.name())
  4988 	);
  4989 	setSelectionColorInt (col);
  4990 }
  4991 
  4992 QColor VymModel::getSelectionColor()
  4993 {
  4994 	return mapEditor->getSelectionColor();
  4995 }
  4996 
  4997 void VymModel::setHideTmpMode (TreeItem::HideTmpMode mode)
  4998 {
  4999 	hidemode=mode;
  5000 	for (int i=0;i<rootItem->branchCount();i++)
  5001 		rootItem->getBranchNum(i)->setHideTmp (mode);	
  5002 	reposition();
  5003 	if (mode==TreeItem::HideExport)
  5004 		unselect();
  5005 	else
  5006 		reselect();
  5007 }
  5008 
  5009 //////////////////////////////////////////////
  5010 // Selection related
  5011 //////////////////////////////////////////////
  5012 
  5013 void VymModel::setSelectionModel (QItemSelectionModel *sm)
  5014 {
  5015 	selModel=sm;
  5016 }
  5017 
  5018 QItemSelectionModel* VymModel::getSelectionModel()
  5019 {
  5020 	return selModel;
  5021 }
  5022 
  5023 void VymModel::setSelectionBlocked (bool b)
  5024 {
  5025 	selectionBlocked=b;
  5026 }
  5027 
  5028 bool VymModel::isSelectionBlocked()
  5029 {
  5030 	return selectionBlocked;
  5031 }
  5032 
  5033 bool VymModel::select ()
  5034 {
  5035 	return select (selModel->selectedIndexes().first());	// TODO no multiselections yet
  5036 }
  5037 
  5038 bool VymModel::select (const QString &s)
  5039 {
  5040 	if (s.isEmpty())
  5041 	{
  5042 		unselect();
  5043 		return true;
  5044 	}
  5045 	TreeItem *ti=findBySelectString(s);
  5046 	if (ti) return select (index(ti));
  5047 	return false;
  5048 }
  5049 
  5050 bool VymModel::select (LinkableMapObj *lmo)
  5051 {
  5052 	QItemSelection oldsel=selModel->selection();
  5053 
  5054 	if (lmo)
  5055 		return select (index (lmo->getTreeItem()) );
  5056 	else	
  5057 		return false;
  5058 }
  5059 
  5060 bool VymModel::select (TreeItem *ti)
  5061 {
  5062 	if (ti) return select (index(ti));
  5063 	return false;
  5064 }
  5065 
  5066 bool VymModel::select (const QModelIndex &index)
  5067 {
  5068 	if (index.isValid() )
  5069 	{
  5070 		selModel->select (index,QItemSelectionModel::ClearAndSelect  );
  5071 		BranchItem *bi=getSelectedBranch();
  5072 		if (bi) bi->setLastSelectedBranch();
  5073 		return true;
  5074 	}
  5075 	return false;
  5076 }
  5077 
  5078 void VymModel::unselect()
  5079 {
  5080 	if (!selModel->selectedIndexes().isEmpty())
  5081 	{
  5082 		lastSelectString=getSelectString();
  5083 		selModel->clearSelection();
  5084 	}
  5085 }	
  5086 
  5087 bool VymModel::reselect()
  5088 {
  5089 	return select (lastSelectString);
  5090 }	
  5091 
  5092 void VymModel::emitShowSelection()	
  5093 {
  5094 	if (!blockReposition)
  5095 		emit (showSelection() );
  5096 }
  5097 
  5098 void VymModel::emitNoteHasChanged (TreeItem *ti)
  5099 {
  5100 	QModelIndex ix=index(ti);
  5101 	emit (noteHasChanged (ix) );
  5102 }
  5103 
  5104 void VymModel::emitDataHasChanged (TreeItem *ti)
  5105 {
  5106 	QModelIndex ix=index(ti);
  5107 	emit (dataChanged (ix,ix) );
  5108 }
  5109 
  5110 
  5111 bool VymModel::selectFirstBranch()
  5112 {
  5113 	TreeItem *ti=getSelectedBranch();
  5114 	if (ti)
  5115 	{
  5116 		TreeItem *par=ti->parent();
  5117 		if (par) 
  5118 		{
  5119 			TreeItem *ti2=par->getFirstBranch();
  5120 			if (ti2) return  select(ti2);
  5121 		}
  5122 	}		
  5123 	return false;
  5124 }
  5125 
  5126 bool VymModel::selectLastBranch()
  5127 {
  5128 	TreeItem *ti=getSelectedBranch();
  5129 	if (ti)
  5130 	{
  5131 		TreeItem *par=ti->parent();
  5132 		if (par) 
  5133 		{
  5134 			TreeItem *ti2=par->getLastBranch();
  5135 			if (ti2) return select(ti2);
  5136 		}
  5137 	}		
  5138 	return false;
  5139 }
  5140 
  5141 bool VymModel::selectLastSelectedBranch()
  5142 {
  5143 	BranchItem *bi=getSelectedBranch();
  5144 	if (bi)
  5145 	{
  5146 		bi=bi->getLastSelectedBranch();
  5147 		if (bi) return select (bi);
  5148 	}		
  5149 	return false;
  5150 }
  5151 
  5152 bool VymModel::selectParent()
  5153 {
  5154 	TreeItem *ti=getSelectedItem();
  5155 	TreeItem *par;
  5156 	if (ti)
  5157 	{
  5158 		par=ti->parent();
  5159 		if (par) 
  5160 			return select(par);
  5161 	}		
  5162 	return false;
  5163 }
  5164 
  5165 TreeItem::Type VymModel::selectionType()
  5166 {
  5167 	QModelIndexList list=selModel->selectedIndexes();
  5168 	if (list.isEmpty()) return TreeItem::Undefined;	
  5169 	TreeItem *ti = getItem (list.first() );
  5170 	return ti->getType();
  5171 
  5172 }
  5173 
  5174 LinkableMapObj* VymModel::getSelectedLMO()
  5175 {
  5176 	QModelIndexList list=selModel->selectedIndexes();
  5177 	if (!list.isEmpty() )
  5178 	{
  5179 		TreeItem *ti = getItem (list.first() );
  5180 		TreeItem::Type type=ti->getType();
  5181 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
  5182 			return ((MapItem*)ti)->getLMO();
  5183 	}
  5184 	return NULL;
  5185 }
  5186 
  5187 BranchObj* VymModel::getSelectedBranchObj()	// FIXME-3 this should not be needed in the end!!!
  5188 {
  5189 	TreeItem *ti = getSelectedBranch();
  5190 	if (ti)
  5191 		return (BranchObj*)(  ((MapItem*)ti)->getLMO());
  5192 	else	
  5193 		return NULL;
  5194 }
  5195 
  5196 BranchItem* VymModel::getSelectedBranch()
  5197 {
  5198 	QModelIndexList list=selModel->selectedIndexes();
  5199 	if (!list.isEmpty() )
  5200 	{
  5201 		TreeItem *ti = getItem (list.first() );
  5202 		TreeItem::Type type=ti->getType();
  5203 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
  5204 			return (BranchItem*)ti;
  5205 	}
  5206 	return NULL;
  5207 }
  5208 
  5209 ImageItem* VymModel::getSelectedImage()
  5210 {
  5211 	QModelIndexList list=selModel->selectedIndexes();
  5212 	if (!list.isEmpty())
  5213 	{
  5214 		TreeItem *ti=getItem (list.first());
  5215 		if (ti && ti->getType()==TreeItem::Image)
  5216 			return (ImageItem*)ti;
  5217 	}
  5218 	return NULL;
  5219 }
  5220 
  5221 AttributeItem* VymModel::getSelectedAttribute()	
  5222 {
  5223 	QModelIndexList list=selModel->selectedIndexes();
  5224 	if (!list.isEmpty() )
  5225 	{
  5226 		TreeItem *ti = getItem (list.first() );
  5227 		TreeItem::Type type=ti->getType();
  5228 		if (type ==TreeItem::Attribute)
  5229 			return (AttributeItem*)ti;
  5230 	} 
  5231 	return NULL;
  5232 }
  5233 
  5234 TreeItem* VymModel::getSelectedItem()	
  5235 {
  5236 	QModelIndexList list=selModel->selectedIndexes();
  5237 	if (!list.isEmpty() )
  5238 		return getItem (list.first() );
  5239 	else	
  5240 		return NULL;
  5241 }
  5242 
  5243 QModelIndex VymModel::getSelectedIndex()
  5244 {
  5245 	QModelIndexList list=selModel->selectedIndexes();
  5246 	if (list.isEmpty() )
  5247 		return QModelIndex();
  5248 	else
  5249 		return list.first();
  5250 }
  5251 
  5252 QString VymModel::getSelectString ()
  5253 {
  5254 	return getSelectString (getSelectedItem());
  5255 }
  5256 
  5257 QString VymModel::getSelectString (LinkableMapObj *lmo)	// only for convenience. Used in MapEditor
  5258 {
  5259 	if (!lmo) return QString();
  5260 	return getSelectString (lmo->getTreeItem() );
  5261 }
  5262 
  5263 QString VymModel::getSelectString (TreeItem *ti) 
  5264 {
  5265 	QString s;
  5266 	if (!ti) return s;
  5267 	switch (ti->getType())
  5268 	{
  5269 		case TreeItem::MapCenter: s="mc:"; break;
  5270 		case TreeItem::Branch: s="bo:";break;
  5271 		case TreeItem::Image: s="fi:";break;
  5272 		case TreeItem::Attribute: s="ai:";break;
  5273 		case TreeItem::XLink: s="xl:";break;
  5274 		default:
  5275 			s="unknown type in VymModel::getSelectString()";
  5276 			break;
  5277 	}
  5278 	s=  s + QString("%1").arg(ti->num());
  5279 	if (ti->depth() >0)
  5280 		// call myself recursively
  5281 		s= getSelectString(ti->parent()) +","+s;
  5282 			
  5283 	return s;
  5284 }
  5285