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