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