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