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