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