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