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