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