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