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