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