vymmodel.cpp
author insilmaril
Thu, 18 Mar 2010 11:38:14 +0000
changeset 838 e4a44912646c
parent 837 5ecd0462f76b
child 839 fbb927bbdda3
permissions -rw-r--r--
Added missing return values
     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 {
  2546 	BranchItem *selbi=getSelectedBranch();
  2547 
  2548 	if (selbi)
  2549 	{	// Delete branch
  2550 		unselect();
  2551 		saveStateRemovingPart (selbi, QString ("Delete %1").arg(getObjectName(selbi)));
  2552 
  2553 		BranchItem *pi=(BranchItem*)(deleteItem (selbi));
  2554 		if (pi)
  2555 		{
  2556 			if (pi->isScrolled() && pi->branchCount()==0)
  2557 			{
  2558 				pi->unScroll();
  2559 				emitDataHasChanged(pi);
  2560 			}	
  2561 			select (pi);
  2562 			emitShowSelection();
  2563 		}
  2564 		return;
  2565 	}
  2566 	TreeItem *ti=getSelectedItem();
  2567 	if (ti)
  2568 	{	// Delete other item
  2569 		TreeItem *pi=ti->parent();
  2570 		if (!pi) return;
  2571 		if (ti->getType()==TreeItem::Image || ti->getType()==TreeItem::Attribute)
  2572 		{
  2573 			saveStateChangingPart(
  2574 				pi, 
  2575 				ti,
  2576 				"delete ()",
  2577 				QString("Delete %1").arg(getObjectName(ti))
  2578 			);
  2579 			unselect();
  2580 			deleteItem (ti);
  2581 			emitDataHasChanged (pi);
  2582 			select (pi);
  2583 			reposition();
  2584 			emitShowSelection();
  2585 		} else if (ti->getType()==TreeItem::XLink)
  2586 		{
  2587 			//FIXME-2 savestate for deleting xlink missing
  2588 			deleteItem (ti);
  2589 		} else
  2590 			qWarning ("VymmModel::deleteSelection()  unknown type?!");
  2591 	}
  2592 }
  2593 
  2594 void VymModel::deleteKeepChildren()	//FIXME-3 does not work yet for mapcenters
  2595 
  2596 {
  2597 	BranchItem *selbi=getSelectedBranch();
  2598 	BranchItem *pi;
  2599 	if (selbi)
  2600 	{
  2601 		// Don't use this on mapcenter
  2602 		if (selbi->depth()<2) return;
  2603 
  2604 		pi=(BranchItem*)(selbi->parent());
  2605 		// Check if we have children at all to keep
  2606 		if (selbi->branchCount()==0) 
  2607 		{
  2608 			deleteSelection();
  2609 			return;
  2610 		}
  2611 
  2612 		QPointF p;
  2613 		if (selbi->getLMO()) p=selbi->getLMO()->getRelPos();
  2614 		saveStateChangingPart(
  2615 			pi,
  2616 			selbi,
  2617 			"deleteKeepChildren ()",
  2618 			QString("Remove %1 and keep its children").arg(getObjectName(selbi))
  2619 		);
  2620 
  2621 		QString sel=getSelectString(selbi);
  2622 		unselect();
  2623 		int pos=selbi->num();
  2624 		BranchItem *bi=selbi->getFirstBranch();
  2625 		while (bi)
  2626 		{
  2627 			relinkBranch (bi,pi,pos);
  2628 			bi=selbi->getFirstBranch();
  2629 			pos++;
  2630 		}
  2631 		deleteItem (selbi);
  2632 		reposition();
  2633 		select (sel);
  2634 		BranchObj *bo=getSelectedBranchObj();
  2635 		if (bo) 
  2636 		{
  2637 			bo->move2RelPos (p);
  2638 			reposition();
  2639 		}
  2640 	}	
  2641 }
  2642 
  2643 void VymModel::deleteChildren()		
  2644 
  2645 {
  2646 	BranchItem *selbi=getSelectedBranch();
  2647 	if (selbi)
  2648 	{		
  2649 		saveStateChangingPart(
  2650 			selbi, 
  2651 			selbi,
  2652 			"deleteChildren ()",
  2653 			QString( "Remove children of branch %1").arg(getObjectName(selbi))
  2654 		);
  2655 		emit (layoutAboutToBeChanged() );
  2656 
  2657 		QModelIndex ix=index (selbi);
  2658 		int n=selbi->branchCount()-1;
  2659 		beginRemoveRows (ix,0,n);
  2660 		removeRows (0,n+1,ix);
  2661 		endRemoveRows();
  2662 		if (selbi->isScrolled()) selbi->unScroll();
  2663 		emit (layoutChanged() );
  2664 		reposition();
  2665 	}	
  2666 }
  2667 
  2668 TreeItem* VymModel::deleteItem (TreeItem *ti)
  2669 {
  2670 	if (ti)
  2671 	{
  2672 		TreeItem *pi=ti->parent();
  2673 		QModelIndex parentIndex=index(pi);
  2674 
  2675 		emit (layoutAboutToBeChanged() );
  2676 
  2677 		int n=ti->childNum();
  2678 		beginRemoveRows (parentIndex,n,n);
  2679 		removeRows (n,1,parentIndex);
  2680 		endRemoveRows();
  2681 		reposition();
  2682 
  2683 		emit (layoutChanged() );
  2684 		if (pi->depth()>=0) return pi;
  2685 	}	
  2686 	return NULL;
  2687 }
  2688 
  2689 void VymModel::clearItem (TreeItem *ti)
  2690 {
  2691 	if (ti)
  2692 	{
  2693 		QModelIndex parentIndex=index(ti);
  2694 		if (!parentIndex.isValid()) return;
  2695 
  2696 		int n=ti->childCount();
  2697 		if (n==0) return;
  2698 
  2699 		emit (layoutAboutToBeChanged() );
  2700 
  2701 		beginRemoveRows (parentIndex,0,n-1);
  2702 		removeRows (0,n,parentIndex);
  2703 		endRemoveRows();
  2704 		reposition();
  2705 
  2706 		emit (layoutChanged() );
  2707 
  2708 	}	
  2709 	return ;
  2710 }
  2711 
  2712 bool VymModel::scrollBranch(BranchItem *bi)
  2713 {
  2714 	if (bi)	
  2715 	{
  2716 		if (bi->isScrolled()) return false;
  2717 		if (bi->branchCount()==0) return false;
  2718 		if (bi->depth()==0) return false;
  2719 		if (bi->toggleScroll())
  2720 		{
  2721 			reposition();
  2722 			QString u,r;
  2723 			r="scroll";
  2724 			u="unscroll";
  2725 			saveState(
  2726 				bi,
  2727 				QString ("%1 ()").arg(u),
  2728 				bi,
  2729 				QString ("%1 ()").arg(r),
  2730 				QString ("%1 %2").arg(r).arg(getObjectName(bi))
  2731 			);
  2732 			emitDataHasChanged(bi);
  2733 			emitSelectionChanged();
  2734 			mapScene->update(); //Needed for _quick_ update,  even in 1.13.x 
  2735 			return true;
  2736 		}
  2737 	}	
  2738 	return false;
  2739 }
  2740 
  2741 bool VymModel::unscrollBranch(BranchItem *bi)
  2742 {
  2743 	if (bi)
  2744 	{
  2745 		if (!bi->isScrolled()) return false;
  2746 		if (bi->branchCount()==0) return false;
  2747 		if (bi->depth()==0) return false;
  2748 		if (bi->toggleScroll())
  2749 		{
  2750 			reposition();
  2751 			QString u,r;
  2752 			u="scroll";
  2753 			r="unscroll";
  2754 			saveState(
  2755 				bi,
  2756 				QString ("%1 ()").arg(u),
  2757 				bi,
  2758 				QString ("%1 ()").arg(r),
  2759 				QString ("%1 %2").arg(r).arg(getObjectName(bi))
  2760 			);
  2761 			emitDataHasChanged(bi);
  2762 			emitSelectionChanged();
  2763 			mapScene->update(); //Needed for _quick_ update,  even in 1.13.x 
  2764 			return true;
  2765 		}	
  2766 	}	
  2767 	return false;
  2768 }
  2769 
  2770 void VymModel::toggleScroll()	
  2771 {
  2772 	BranchItem *bi=(BranchItem*)getSelectedBranch();
  2773 	if (bi && bi->isBranchLikeType() )
  2774 	{
  2775 		if (bi->isScrolled())
  2776 			unscrollBranch (bi);
  2777 		else
  2778 			scrollBranch (bi);
  2779 	}
  2780 	// saveState & reposition are called in above functions
  2781 }
  2782 
  2783 void VymModel::unscrollChildren() 
  2784 {
  2785 	BranchItem *selbi=getSelectedBranch();
  2786 	BranchItem *prev=NULL;
  2787 	BranchItem *cur=selbi;
  2788 	if (selbi)
  2789 	{
  2790 		saveStateChangingPart(
  2791 			selbi,
  2792 			selbi,
  2793 			QString ("unscrollChildren ()"),
  2794 			QString ("unscroll all children of %1").arg(getObjectName(selbi))
  2795 		);	
  2796 		while (cur) 
  2797 		{
  2798 			if (cur->isScrolled())
  2799 			{
  2800 				cur->toggleScroll(); 
  2801 				emitDataHasChanged (cur);
  2802 		}
  2803 			cur=nextBranch (cur,prev,true,selbi);
  2804 		}	
  2805 		updateActions();
  2806 		reposition();
  2807 	}	
  2808 }
  2809 
  2810 void VymModel::emitExpandAll()	
  2811 {
  2812 	emit (expandAll() );
  2813 }
  2814 
  2815 void VymModel::emitExpandOneLevel()	
  2816 {
  2817 	emit (expandOneLevel () );
  2818 }
  2819 
  2820 void VymModel::emitCollapseOneLevel()	
  2821 {
  2822 	emit (collapseOneLevel () );
  2823 }
  2824 
  2825 void VymModel::toggleStandardFlag (const QString &name, FlagRow *master)
  2826 {
  2827 	BranchItem *bi=getSelectedBranch();
  2828 	if (bi) 
  2829 	{
  2830 		QString u,r;
  2831 		if (bi->isActiveStandardFlag(name))
  2832 		{
  2833 			r="unsetFlag";
  2834 			u="setFlag";
  2835 		}	
  2836 		else
  2837 		{
  2838 			u="unsetFlag";
  2839 			r="setFlag";
  2840 		}	
  2841 		saveState(
  2842 			bi,
  2843 			QString("%1 (\"%2\")").arg(u).arg(name), 
  2844 			bi,
  2845 			QString("%1 (\"%2\")").arg(r).arg(name),
  2846 			QString("Toggling standard flag \"%1\" of %2").arg(name).arg(getObjectName(bi)));
  2847 			bi->toggleStandardFlag (name, master);
  2848 		reposition();
  2849 		emitSelectionChanged();	
  2850 	}
  2851 }
  2852 
  2853 void VymModel::addFloatImage (const QPixmap &img) 
  2854 {
  2855 	BranchItem *selbi=getSelectedBranch();
  2856 	if (selbi)
  2857 	{
  2858 		ImageItem *ii=createImage (selbi);
  2859 		ii->load(img);
  2860 		ii->setOriginalFilename("No original filename (image added by dropevent)");	
  2861 		QString s=getSelectString(selbi);
  2862 		saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",ii  );
  2863 		saveState (ii,"delete ()", selbi,QString("paste(%1)").arg(curStep),"Pasting dropped image");
  2864 		reposition();
  2865 		// FIXME-3 VM needed? scene()->update();
  2866 	}
  2867 }
  2868 
  2869 
  2870 void VymModel::colorBranch (QColor c)	
  2871 {
  2872 	BranchItem *selbi=getSelectedBranch();
  2873 	if (selbi)
  2874 	{
  2875 		saveState(
  2876 			selbi, 
  2877 			QString ("colorBranch (\"%1\")").arg(selbi->getHeadingColor().name()),
  2878 			selbi,
  2879 			QString ("colorBranch (\"%1\")").arg(c.name()),
  2880 			QString("Set color of %1 to %2").arg(getObjectName(selbi)).arg(c.name())
  2881 		);	
  2882 		selbi->setHeadingColor(c); // color branch
  2883 		mapScene->update();
  2884 	}
  2885 }
  2886 
  2887 void VymModel::colorSubtree (QColor c) 
  2888 {
  2889 	BranchItem *selbi=getSelectedBranch();
  2890 	if (selbi)
  2891 	{
  2892 		saveStateChangingPart(
  2893 			selbi,
  2894 			selbi,
  2895 			QString ("colorSubtree (\"%1\")").arg(c.name()),
  2896 			QString ("Set color of %1 and children to %2").arg(getObjectName(selbi)).arg(c.name())
  2897 		);	
  2898 		BranchItem *prev=NULL;
  2899 		BranchItem *cur=selbi;
  2900 		while (cur) 
  2901 		{
  2902 			cur->setHeadingColor(c); // color links, color children
  2903 			cur=nextBranch (cur,prev,true,selbi);
  2904 		}	
  2905 	mapScene->update();
  2906 	}
  2907 }
  2908 
  2909 QColor VymModel::getCurrentHeadingColor()	
  2910 {
  2911 	BranchItem *selbi=getSelectedBranch();
  2912 	if (selbi)	return selbi->getHeadingColor();
  2913 		
  2914 	QMessageBox::warning(0,"Warning","Can't get color of heading,\nthere's no branch selected");
  2915 	return Qt::black;
  2916 }
  2917 
  2918 
  2919 
  2920 void VymModel::editURL()	
  2921 {
  2922 	TreeItem *selti=getSelectedItem();
  2923 	if (selti)
  2924 	{		
  2925 		bool ok;
  2926 		QString text = QInputDialog::getText(
  2927 				"VYM", tr("Enter URL:"), QLineEdit::Normal,
  2928 				selti->getURL(), &ok, NULL);
  2929 		if ( ok) 
  2930 			// user entered something and pressed OK
  2931 			setURL(text);
  2932 	}
  2933 }
  2934 
  2935 void VymModel::editLocalURL()
  2936 {
  2937 	TreeItem *selti=getSelectedItem();
  2938 	if (selti)
  2939 	{		
  2940 		QStringList filters;
  2941 		filters <<"All files (*)";
  2942 		filters << tr("Text","Filedialog") + " (*.txt)";
  2943 		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
  2944 		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
  2945 		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
  2946 		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Set URL to a local file"));
  2947 		fd->setFilters (filters);
  2948 		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
  2949 		fd->setDirectory (lastFileDir);
  2950 		if (! selti->getVymLink().isEmpty() )
  2951 			fd->selectFile( selti->getURL() );
  2952 		fd->show();
  2953 
  2954 		if ( fd->exec() == QDialog::Accepted )
  2955 		{
  2956 			lastFileDir=QDir (fd->directory().path());
  2957 			setURL (fd->selectedFile() );
  2958 		}
  2959 	}
  2960 }
  2961 
  2962 
  2963 void VymModel::editHeading2URL() 
  2964 {
  2965 	TreeItem *selti=getSelectedItem();
  2966 	if (selti)
  2967 		setURL (selti->getHeading());
  2968 }	
  2969 
  2970 void VymModel::editBugzilla2URL()	
  2971 {
  2972 	TreeItem *selti=getSelectedItem();
  2973 	if (selti)
  2974 	{		
  2975 		QString h=selti->getHeading();
  2976 		QRegExp rx("^(\\d+)");
  2977 		if (rx.indexIn(h) !=-1)
  2978 			setURL ("https://bugzilla.novell.com/show_bug.cgi?id="+rx.cap(1) );
  2979 	}
  2980 }	
  2981 
  2982 void VymModel::getBugzillaData()	
  2983 {
  2984 	BranchItem *selbi=getSelectedBranch();
  2985 	if (selbi)
  2986 	{		
  2987 		QString url=selbi->getURL();
  2988 		if (!url.isEmpty())
  2989 		{
  2990 			QRegExp rx("(\\d+)");
  2991 			if (rx.indexIn(url) !=-1)
  2992 			{
  2993 				QString bugID=rx.cap(1);
  2994 				qDebug()<< "VM::getBugzillaData bug="<<bugID;
  2995 
  2996 				new BugAgent (selbi,bugID);
  2997 			}	
  2998 		}
  2999 	}
  3000 }	
  3001 
  3002 void VymModel::editFATE2URL()
  3003 {
  3004 	TreeItem *selti=getSelectedItem();
  3005 	if (selti)
  3006 	{		
  3007 		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+selti->getHeading();
  3008 		saveState(
  3009 			selti,
  3010 			"setURL (\""+selti->getURL()+"\")",
  3011 			selti,
  3012 			"setURL (\""+url+"\")",
  3013 			QString("Use heading of %1 as link to FATE").arg(getObjectName(selti))
  3014 		);	
  3015 		selti->setURL (url);
  3016 		// FIXME-4 updateActions();
  3017 	}
  3018 }	
  3019 
  3020 void VymModel::editVymLink()
  3021 {
  3022 	BranchItem *bi=getSelectedBranch();
  3023 	if (bi)
  3024 	{		
  3025 		QStringList filters;
  3026 		filters <<"VYM map (*.vym)";
  3027 		QFileDialog *fd=new QFileDialog( NULL,vymName+" - " +tr("Link to another map"));
  3028 		fd->setFilters (filters);
  3029 		fd->setCaption(vymName+" - " +tr("Link to another map"));
  3030 		fd->setDirectory (lastFileDir);
  3031 		if (! bi->getVymLink().isEmpty() )
  3032 			fd->selectFile( bi->getVymLink() );
  3033 		fd->show();
  3034 
  3035 		QString fn;
  3036 		if ( fd->exec() == QDialog::Accepted )
  3037 		{
  3038 			lastFileDir=QDir (fd->directory().path());
  3039 			saveState(
  3040 				bi,
  3041 				"setVymLink (\""+bi->getVymLink()+"\")",
  3042 				bi,
  3043 				"setVymLink (\""+fd->selectedFile()+"\")",
  3044 				QString("Set vymlink of %1 to %2").arg(getObjectName(bi)).arg(fd->selectedFile())
  3045 			);	
  3046 			setVymLink (fd->selectedFile() );	
  3047 		}
  3048 	}
  3049 }
  3050 
  3051 void VymModel::setVymLink (const QString &s)	
  3052 {
  3053 	// Internal function, no saveState needed
  3054 	TreeItem *selti=getSelectedItem();
  3055 	if (selti)
  3056 	{
  3057 		selti->setVymLink(s);
  3058 		reposition();
  3059 		emitDataHasChanged (selti);
  3060 		emitShowSelection();
  3061 	}
  3062 }
  3063 
  3064 void VymModel::deleteVymLink()
  3065 {
  3066 	BranchItem *bi=getSelectedBranch();
  3067 	if (bi)
  3068 	{		
  3069 		saveState(
  3070 			bi,
  3071 			"setVymLink (\""+bi->getVymLink()+"\")", 
  3072 			bi,
  3073 			"setVymLink (\"\")",
  3074 			QString("Unset vymlink of %1").arg(getObjectName(bi))
  3075 		);	
  3076 		bi->setVymLink ("" );
  3077 		updateActions();
  3078 		reposition();
  3079 	}
  3080 }
  3081 
  3082 QString VymModel::getVymLink()
  3083 {
  3084 	BranchItem *bi=getSelectedBranch();
  3085 	if (bi)
  3086 		return bi->getVymLink();
  3087 	else	
  3088 		return "";
  3089 	
  3090 }
  3091 
  3092 QStringList VymModel::getVymLinks()	
  3093 {
  3094 	QStringList links;
  3095 	BranchItem *selbi=getSelectedBranch();
  3096 	BranchItem *cur=selbi;
  3097 	BranchItem *prev=NULL;
  3098 	while (cur) 
  3099 	{
  3100 		if (!cur->getVymLink().isEmpty()) links.append( cur->getVymLink());
  3101 		cur=nextBranch (cur,prev,true,selbi);
  3102 	}	
  3103 	return links;
  3104 }
  3105 
  3106 
  3107 void VymModel::followXLink(int i)	
  3108 {
  3109 	i=0;
  3110 	BranchItem *selbi=getSelectedBranch();
  3111 	if (selbi)
  3112 	{
  3113 		selbi=selbi->getXLinkNum(i)->getPartnerBranch();
  3114 		if (selbi) select (selbi);
  3115 	}
  3116 }
  3117 
  3118 void VymModel::editXLink(int i)	
  3119 {
  3120 	i=0;
  3121 	BranchItem *selbi=getSelectedBranch();
  3122 	if (selbi)
  3123 	{
  3124 		XLinkItem *xli=selbi->getXLinkNum(i);
  3125 		if (xli) 
  3126 		{
  3127 			EditXLinkDialog dia;
  3128 			dia.setXLink (xli);
  3129 			dia.setSelection(selbi);
  3130 			if (dia.exec() == QDialog::Accepted)
  3131 			{
  3132 				if (dia.useSettingsGlobal() )
  3133 				{
  3134 					setMapDefXLinkColor (xli->getColor() );
  3135 					setMapDefXLinkWidth (xli->getWidth() );
  3136 				}
  3137 				if (dia.deleteXLink()) deleteItem (xli);
  3138 			}
  3139 		}	
  3140 	}
  3141 }
  3142 
  3143 
  3144 
  3145 
  3146 
  3147 //////////////////////////////////////////////
  3148 // Scripting
  3149 //////////////////////////////////////////////
  3150 
  3151 QVariant VymModel::parseAtom(const QString &atom, bool &noErr, QString &errorMsg)
  3152 {
  3153 	TreeItem* selti=getSelectedItem();
  3154 	BranchItem *selbi=getSelectedBranch();
  3155 	QString s,t;
  3156 	double x,y;
  3157 	int n;
  3158 	bool b,ok;
  3159 	QVariant returnValue="";
  3160 
  3161 	// Split string s into command and parameters
  3162 	parser.parseAtom (atom);
  3163 	QString com=parser.getCommand();
  3164 	
  3165 	// External commands
  3166 	/////////////////////////////////////////////////////////////////////
  3167 	if (com=="addBranch")
  3168 	{
  3169 		if (!selti)
  3170 		{
  3171 			parser.setError (Aborted,"Nothing selected");
  3172 		} else if (! selbi )
  3173 		{				  
  3174 			parser.setError (Aborted,"Type of selection is not a branch");
  3175 		} else 
  3176 		{	
  3177 			QList <int> pl;
  3178 			pl << 0 <<1;
  3179 			if (parser.checkParCount(pl))
  3180 			{
  3181 				if (parser.parCount()==0)
  3182 					addNewBranch (0);
  3183 				else
  3184 				{
  3185 					n=parser.parInt (ok,0);
  3186 					if (ok ) addNewBranch (n);
  3187 				}
  3188 			}
  3189 		}
  3190 	/////////////////////////////////////////////////////////////////////
  3191 	} else if (com=="addBranchBefore")
  3192 	{
  3193 		if (!selti)
  3194 		{
  3195 			parser.setError (Aborted,"Nothing selected");
  3196 		} else if (! selbi )
  3197 		{				  
  3198 			parser.setError (Aborted,"Type of selection is not a branch");
  3199 		} else 
  3200 		{	
  3201 			if (parser.parCount()==0)
  3202 			{
  3203 				addNewBranchBefore ();
  3204 			}	
  3205 		}
  3206 	/////////////////////////////////////////////////////////////////////
  3207 	} else if (com==QString("addMapCenter"))
  3208 	{
  3209 		if (parser.checkParCount(2))
  3210 		{
  3211 			x=parser.parDouble (ok,0);
  3212 			if (ok)
  3213 			{
  3214 				y=parser.parDouble (ok,1);
  3215 				if (ok) addMapCenter (QPointF(x,y));
  3216 			}
  3217 		}	
  3218 	/////////////////////////////////////////////////////////////////////
  3219 	} else if (com==QString("addMapReplace"))
  3220 	{
  3221 		if (!selti)
  3222 		{
  3223 			parser.setError (Aborted,"Nothing selected");
  3224 		} else if (! selbi )
  3225 		{				  
  3226 			parser.setError (Aborted,"Type of selection is not a branch");
  3227 		} else if (parser.checkParCount(1))
  3228 		{
  3229 			//s=parser.parString (ok,0);	// selection
  3230 			t=parser.parString (ok,0);	// path to map
  3231 			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  3232 			addMapReplaceInt(getSelectString(selbi),t);	
  3233 		}
  3234 	/////////////////////////////////////////////////////////////////////
  3235 	} else if (com==QString("addMapInsert"))
  3236 	{
  3237 		if (parser.parCount()==2)
  3238 		{
  3239 
  3240 			if (!selti)
  3241 			{
  3242 				parser.setError (Aborted,"Nothing selected");
  3243 			} else if (! selbi )
  3244 			{				  
  3245 				parser.setError (Aborted,"Type of selection is not a branch");
  3246 			} else 
  3247 			{	
  3248 				t=parser.parString (ok,0);	// path to map
  3249 				n=parser.parInt(ok,1);		// position
  3250 				if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  3251 				addMapInsertInt(t,n);	
  3252 			}
  3253 		} else if (parser.parCount()==1)
  3254 		{
  3255 			t=parser.parString (ok,0);	// path to map
  3256 			if (QDir::isRelativePath(t)) t=(tmpMapDir + "/"+t);
  3257 			addMapInsertInt(t);	
  3258 		} else
  3259 			parser.setError (Aborted,"Wrong number of parameters");
  3260 	/////////////////////////////////////////////////////////////////////
  3261 	} else if (com==QString("addXLink"))
  3262 	{
  3263 		if (parser.parCount()>1)
  3264 		{
  3265 			s=parser.parString (ok,0);	// begin
  3266 			t=parser.parString (ok,1);	// end
  3267 			BranchItem *begin=(BranchItem*)findBySelectString(s);
  3268 			BranchItem *end=(BranchItem*)findBySelectString(t);
  3269 			if (begin && end)
  3270 			{
  3271 				if (begin->isBranchLikeType() && end->isBranchLikeType())
  3272 				{
  3273 					XLinkItem *xl=createXLink (begin,true);
  3274 					if (xl)
  3275 					{
  3276 						xl->setEnd (end);
  3277 						xl->activate();
  3278 					} else
  3279 						parser.setError (Aborted,"Failed to create xLink");
  3280 				}
  3281 				else
  3282 					parser.setError (Aborted,"begin or end of xLink are not branch or mapcenter");
  3283 				
  3284 			} else
  3285 				parser.setError (Aborted,"Couldn't select begin or end of xLink");
  3286 		} else
  3287 			parser.setError (Aborted,"Need at least 2 parameters for begin and end");
  3288 	/////////////////////////////////////////////////////////////////////
  3289 	} else if (com=="clearFlags")	
  3290 	{
  3291 		if (!selti )
  3292 		{
  3293 			parser.setError (Aborted,"Nothing selected");
  3294 		} else if (! selbi )
  3295 		{				  
  3296 			parser.setError (Aborted,"Type of selection is not a branch");
  3297 		} else if (parser.checkParCount(0))
  3298 		{
  3299 			selbi->deactivateAllStandardFlags();	//FIXME-2 this probably should emitDataChanged and also setChanged. Similar all other direct changes should be done...
  3300 		}
  3301 	/////////////////////////////////////////////////////////////////////
  3302 	} else if (com=="colorBranch")
  3303 	{
  3304 		if (!selti)
  3305 		{
  3306 			parser.setError (Aborted,"Nothing selected");
  3307 		} else if (! selbi )
  3308 		{				  
  3309 			parser.setError (Aborted,"Type of selection is not a branch");
  3310 		} else if (parser.checkParCount(1))
  3311 		{	
  3312 			QColor c=parser.parColor (ok,0);
  3313 			if (ok) colorBranch (c);
  3314 		}	
  3315 	/////////////////////////////////////////////////////////////////////
  3316 	} else if (com=="colorSubtree")
  3317 	{
  3318 		if (!selti)
  3319 		{
  3320 			parser.setError (Aborted,"Nothing selected");
  3321 		} else if (! selbi )
  3322 		{				  
  3323 			parser.setError (Aborted,"Type of selection is not a branch");
  3324 		} else if (parser.checkParCount(1))
  3325 		{	
  3326 			QColor c=parser.parColor (ok,0);
  3327 			if (ok) colorSubtree (c);
  3328 		}	
  3329 	/////////////////////////////////////////////////////////////////////
  3330 	} else if (com=="copy")
  3331 	{
  3332 		if (!selti)
  3333 		{
  3334 			parser.setError (Aborted,"Nothing selected");
  3335 		} else if ( selectionType()!=TreeItem::Branch  && 
  3336 					selectionType()!=TreeItem::MapCenter  &&
  3337 					selectionType()!=TreeItem::Image )
  3338 		{				  
  3339 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3340 		} else if (parser.checkParCount(0))
  3341 		{	
  3342 			copy();
  3343 		}	
  3344 	/////////////////////////////////////////////////////////////////////
  3345 	} else if (com=="cut")
  3346 	{
  3347 		if (!selti)
  3348 		{
  3349 			parser.setError (Aborted,"Nothing selected");
  3350 		} else if ( selectionType()!=TreeItem::Branch  && 
  3351 					selectionType()!=TreeItem::MapCenter  &&
  3352 					selectionType()!=TreeItem::Image )
  3353 		{				  
  3354 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3355 		} else if (parser.checkParCount(0))
  3356 		{	
  3357 			cut();
  3358 		}	
  3359 	/////////////////////////////////////////////////////////////////////
  3360 	} else if (com=="delete")
  3361 	{
  3362 		if (!selti)
  3363 		{
  3364 			parser.setError (Aborted,"Nothing selected");
  3365 		} 
  3366 		/*else if (selectionType() != TreeItem::Branch && selectionType() != TreeItem::Image )
  3367 		{
  3368 			parser.setError (Aborted,"Type of selection is wrong.");
  3369 		} 
  3370 		*/
  3371 		else if (parser.checkParCount(0))
  3372 		{	
  3373 			deleteSelection();
  3374 		}	
  3375 	/////////////////////////////////////////////////////////////////////
  3376 	} else if (com=="deleteKeepChildren")
  3377 	{
  3378 		if (!selti)
  3379 		{
  3380 			parser.setError (Aborted,"Nothing selected");
  3381 		} else if (! selbi )
  3382 		{
  3383 			parser.setError (Aborted,"Type of selection is not a branch");
  3384 		} else if (parser.checkParCount(0))
  3385 		{	
  3386 			deleteKeepChildren();
  3387 		}	
  3388 	/////////////////////////////////////////////////////////////////////
  3389 	} else if (com=="deleteChildren")
  3390 	{
  3391 		if (!selti)
  3392 		{
  3393 			parser.setError (Aborted,"Nothing selected");
  3394 		} else if (! selbi)
  3395 		{
  3396 			parser.setError (Aborted,"Type of selection is not a branch");
  3397 		} else if (parser.checkParCount(0))
  3398 		{	
  3399 			deleteChildren();
  3400 		}	
  3401 	/////////////////////////////////////////////////////////////////////
  3402 	} else if (com=="exportAO")
  3403 	{
  3404 		QString fname="";
  3405 		ok=true;
  3406 		if (parser.parCount()>=1)
  3407 			// Hey, we even have a filename
  3408 			fname=parser.parString(ok,0); 
  3409 		if (!ok)
  3410 		{
  3411 			parser.setError (Aborted,"Could not read filename");
  3412 		} else
  3413 		{
  3414 				exportAO (fname,false);
  3415 		}
  3416 	/////////////////////////////////////////////////////////////////////
  3417 	} else if (com=="exportASCII")
  3418 	{
  3419 		QString fname="";
  3420 		ok=true;
  3421 		if (parser.parCount()>=1)
  3422 			// Hey, we even have a filename
  3423 			fname=parser.parString(ok,0); 
  3424 		if (!ok)
  3425 		{
  3426 			parser.setError (Aborted,"Could not read filename");
  3427 		} else
  3428 		{
  3429 				exportASCII (fname,false);
  3430 		}
  3431 	/////////////////////////////////////////////////////////////////////
  3432 	} else if (com=="exportImage")
  3433 	{
  3434 		QString fname="";
  3435 		ok=true;
  3436 		if (parser.parCount()>=2)
  3437 			// Hey, we even have a filename
  3438 			fname=parser.parString(ok,0); 
  3439 		if (!ok)
  3440 		{
  3441 			parser.setError (Aborted,"Could not read filename");
  3442 		} else
  3443 		{
  3444 			QString format="PNG";
  3445 			if (parser.parCount()>=2)
  3446 			{
  3447 				format=parser.parString(ok,1);
  3448 			}
  3449 			exportImage (fname,false,format);
  3450 		}
  3451 	/////////////////////////////////////////////////////////////////////
  3452 	} else if (com=="exportXHTML")
  3453 	{
  3454 		QString fname="";
  3455 		ok=true;
  3456 		if (parser.parCount()>=2)
  3457 			// Hey, we even have a filename
  3458 			fname=parser.parString(ok,1); 
  3459 		if (!ok)
  3460 		{
  3461 			parser.setError (Aborted,"Could not read filename");
  3462 		} else
  3463 		{
  3464 			exportXHTML (fname,false);
  3465 		}
  3466 	/////////////////////////////////////////////////////////////////////
  3467 	} else if (com=="exportXML")
  3468 	{
  3469 		QString fname="";
  3470 		ok=true;
  3471 		if (parser.parCount()>=2)
  3472 			// Hey, we even have a filename
  3473 			fname=parser.parString(ok,1); 
  3474 		if (!ok)
  3475 		{
  3476 			parser.setError (Aborted,"Could not read filename");
  3477 		} else
  3478 		{
  3479 			exportXML (fname,false);
  3480 		}
  3481 	/////////////////////////////////////////////////////////////////////
  3482 	} else if (com=="getHeading")
  3483 	{ 
  3484 		if (!selti)
  3485 		{
  3486 			parser.setError (Aborted,"Nothing selected");
  3487 		} else if (parser.checkParCount(0))
  3488 			returnValue=selti->getHeading();
  3489 	/////////////////////////////////////////////////////////////////////
  3490 	} else if (com=="importDir")
  3491 	{
  3492 		if (!selti)
  3493 		{
  3494 			parser.setError (Aborted,"Nothing selected");
  3495 		} else if (! selbi )
  3496 		{				  
  3497 			parser.setError (Aborted,"Type of selection is not a branch");
  3498 		} else if (parser.checkParCount(1))
  3499 		{
  3500 			s=parser.parString(ok,0);
  3501 			if (ok) importDirInt(s);
  3502 		}	
  3503 	/////////////////////////////////////////////////////////////////////
  3504 	} else if (com=="relinkTo")
  3505 	{
  3506 		if (!selti)
  3507 		{
  3508 			parser.setError (Aborted,"Nothing selected");
  3509 		} else if ( selbi)
  3510 		{
  3511 			if (parser.checkParCount(4))
  3512 			{
  3513 				// 0	selectstring of parent
  3514 				// 1	num in parent (for branches)
  3515 				// 2,3	x,y of mainbranch or mapcenter
  3516 				s=parser.parString(ok,0);
  3517 				TreeItem *dst=findBySelectString (s);
  3518 				if (dst)
  3519 				{	
  3520 					if (dst->getType()==TreeItem::Branch) 
  3521 					{
  3522 						// Get number in parent
  3523 						n=parser.parInt (ok,1);
  3524 						if (ok)
  3525 						{
  3526 							relinkBranch (selbi,(BranchItem*)dst,n);
  3527 							emitSelectionChanged();
  3528 						}	
  3529 					} else if (dst->getType()==TreeItem::MapCenter) 
  3530 					{
  3531 						relinkBranch (selbi,(BranchItem*)dst);
  3532 						// Get coordinates of mainbranch
  3533 						x=parser.parDouble(ok,2);
  3534 						if (ok)
  3535 						{
  3536 							y=parser.parDouble(ok,3);
  3537 							if (ok) 
  3538 							{
  3539 								if (selbi->getLMO()) selbi->getLMO()->move (x,y);
  3540 								emitSelectionChanged();
  3541 							}
  3542 						}
  3543 					}	
  3544 				}	
  3545 			}	
  3546 		} else if ( selti->getType() == TreeItem::Image) 
  3547 		{
  3548 			if (parser.checkParCount(1))
  3549 			{
  3550 				// 0	selectstring of parent
  3551 				s=parser.parString(ok,0);
  3552 				TreeItem *dst=findBySelectString (s);
  3553 				if (dst)
  3554 				{	
  3555 					if (dst->isBranchLikeType())
  3556 						relinkImage ( ((ImageItem*)selti),(BranchItem*)dst);
  3557 				} else	
  3558 					parser.setError (Aborted,"Destination is not a branch");
  3559 			}		
  3560 		} else
  3561 			parser.setError (Aborted,"Type of selection is not a floatimage or branch");
  3562 	/////////////////////////////////////////////////////////////////////
  3563 	} else if (com=="loadImage")
  3564 	{
  3565 		if (!selti)
  3566 		{
  3567 			parser.setError (Aborted,"Nothing selected");
  3568 		} else if (! selbi )
  3569 		{				  
  3570 			parser.setError (Aborted,"Type of selection is not a branch");
  3571 		} else if (parser.checkParCount(1))
  3572 		{
  3573 			s=parser.parString(ok,0);
  3574 			if (ok) loadFloatImageInt (selbi,s);
  3575 		}	
  3576 	/////////////////////////////////////////////////////////////////////
  3577 	} else if (com=="loadNote")
  3578 	{
  3579 		if (!selti)
  3580 		{
  3581 			parser.setError (Aborted,"Nothing selected");
  3582 		} else if (! selbi )
  3583 		{				  
  3584 			parser.setError (Aborted,"Type of selection is not a branch");
  3585 		} else if (parser.checkParCount(1))
  3586 		{
  3587 			s=parser.parString(ok,0);
  3588 			if (ok) loadNote (s);
  3589 		}	
  3590 	/////////////////////////////////////////////////////////////////////
  3591 	} else if (com=="moveUp")
  3592 	{
  3593 		if (!selti )
  3594 		{
  3595 			parser.setError (Aborted,"Nothing selected");
  3596 		} else if (! selbi )
  3597 		{				  
  3598 			parser.setError (Aborted,"Type of selection is not a branch");
  3599 		} else if (parser.checkParCount(0))
  3600 		{
  3601 			moveUp();
  3602 		}	
  3603 	/////////////////////////////////////////////////////////////////////
  3604 	} else if (com=="moveDown")
  3605 	{
  3606 		if (!selti )
  3607 		{
  3608 			parser.setError (Aborted,"Nothing selected");
  3609 		} else if (! selbi )
  3610 		{				  
  3611 			parser.setError (Aborted,"Type of selection is not a branch");
  3612 		} else if (parser.checkParCount(0))
  3613 		{
  3614 			moveDown();
  3615 		}	
  3616 	/////////////////////////////////////////////////////////////////////
  3617 	} else if (com=="move")
  3618 	{
  3619 		if (!selti )
  3620 		{
  3621 			parser.setError (Aborted,"Nothing selected");
  3622 		} else if ( selectionType()!=TreeItem::Branch  && 
  3623 					selectionType()!=TreeItem::MapCenter  &&
  3624 					selectionType()!=TreeItem::Image )
  3625 		{				  
  3626 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3627 		} else if (parser.checkParCount(2))
  3628 		{	
  3629 			x=parser.parDouble (ok,0);
  3630 			if (ok)
  3631 			{
  3632 				y=parser.parDouble (ok,1);
  3633 				if (ok) move (x,y);
  3634 			}
  3635 		}	
  3636 	/////////////////////////////////////////////////////////////////////
  3637 	} else if (com=="moveRel")
  3638 	{
  3639 		if (!selti )
  3640 		{
  3641 			parser.setError (Aborted,"Nothing selected");
  3642 		} else if ( selectionType()!=TreeItem::Branch  && 
  3643 					selectionType()!=TreeItem::MapCenter  &&
  3644 					selectionType()!=TreeItem::Image )
  3645 		{				  
  3646 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3647 		} else if (parser.checkParCount(2))
  3648 		{	
  3649 			x=parser.parDouble (ok,0);
  3650 			if (ok)
  3651 			{
  3652 				y=parser.parDouble (ok,1);
  3653 				if (ok) moveRel (x,y);
  3654 			}
  3655 		}	
  3656 	/////////////////////////////////////////////////////////////////////
  3657 	} else if (com=="nop")
  3658 	{
  3659 	/////////////////////////////////////////////////////////////////////
  3660 	} else if (com=="paste")
  3661 	{
  3662 		if (!selti )
  3663 		{
  3664 			parser.setError (Aborted,"Nothing selected");
  3665 		} else if (! selbi )
  3666 		{				  
  3667 			parser.setError (Aborted,"Type of selection is not a branch");
  3668 		} else if (parser.checkParCount(1))
  3669 		{	
  3670 			n=parser.parInt (ok,0);
  3671 			if (ok) pasteNoSave(n);
  3672 		}	
  3673 	/////////////////////////////////////////////////////////////////////
  3674 	} else if (com=="qa")
  3675 	{
  3676 		if (!selti )
  3677 		{
  3678 			parser.setError (Aborted,"Nothing selected");
  3679 		} else if (! selbi )
  3680 		{				  
  3681 			parser.setError (Aborted,"Type of selection is not a branch");
  3682 		} else if (parser.checkParCount(4))
  3683 		{	
  3684 			QString c,u;
  3685 			c=parser.parString (ok,0);
  3686 			if (!ok)
  3687 			{
  3688 				parser.setError (Aborted,"No comment given");
  3689 			} else
  3690 			{
  3691 				s=parser.parString (ok,1);
  3692 				if (!ok)
  3693 				{
  3694 					parser.setError (Aborted,"First parameter is not a string");
  3695 				} else
  3696 				{
  3697 					t=parser.parString (ok,2);
  3698 					if (!ok)
  3699 					{
  3700 						parser.setError (Aborted,"Condition is not a string");
  3701 					} else
  3702 					{
  3703 						u=parser.parString (ok,3);
  3704 						if (!ok)
  3705 						{
  3706 							parser.setError (Aborted,"Third parameter is not a string");
  3707 						} else
  3708 						{
  3709 							if (s!="heading")
  3710 							{
  3711 								parser.setError (Aborted,"Unknown type: "+s);
  3712 							} else
  3713 							{
  3714 								if (! (t=="eq") ) 
  3715 								{
  3716 									parser.setError (Aborted,"Unknown operator: "+t);
  3717 								} else
  3718 								{
  3719 									if (! selbi    )
  3720 									{
  3721 										parser.setError (Aborted,"Type of selection is not a branch");
  3722 									} else
  3723 									{
  3724 										if (selbi->getHeading() == u)
  3725 										{
  3726 											cout << "PASSED: " << qPrintable (c)  << endl;
  3727 										} else
  3728 										{
  3729 											cout << "FAILED: " << qPrintable (c)  << endl;
  3730 										}
  3731 									}
  3732 								}
  3733 							}
  3734 						} 
  3735 					} 
  3736 				} 
  3737 			}
  3738 		}	
  3739 	/////////////////////////////////////////////////////////////////////
  3740 	} else if (com=="saveImage")
  3741 	{
  3742 		ImageItem *ii=getSelectedImage();
  3743 		if (!ii )
  3744 		{
  3745 			parser.setError (Aborted,"No image selected");
  3746 		} else if (parser.checkParCount(2))
  3747 		{
  3748 			s=parser.parString(ok,0);
  3749 			if (ok)
  3750 			{
  3751 				t=parser.parString(ok,1);
  3752 				if (ok) saveFloatImageInt (ii,t,s);
  3753 			}
  3754 		}	
  3755 	/////////////////////////////////////////////////////////////////////
  3756 	} else if (com=="saveNote")
  3757 	{
  3758 		if (!selti)
  3759 		{
  3760 			parser.setError (Aborted,"Nothing selected");
  3761 		} else if (! selbi )
  3762 		{				  
  3763 			parser.setError (Aborted,"Type of selection is not a branch");
  3764 		} else if (parser.checkParCount(1))
  3765 		{
  3766 			s=parser.parString(ok,0);
  3767 			if (ok) saveNote (s);
  3768 		}	
  3769 	/////////////////////////////////////////////////////////////////////
  3770 	} else if (com=="scroll")
  3771 	{
  3772 		if (!selti)
  3773 		{
  3774 			parser.setError (Aborted,"Nothing selected");
  3775 		} else if (! selbi )
  3776 		{				  
  3777 			parser.setError (Aborted,"Type of selection is not a branch");
  3778 		} else if (parser.checkParCount(0))
  3779 		{	
  3780 			if (!scrollBranch (selbi))	
  3781 				parser.setError (Aborted,"Could not scroll branch");
  3782 		}	
  3783 	/////////////////////////////////////////////////////////////////////
  3784 	} else if (com=="select")
  3785 	{
  3786 		if (parser.checkParCount(1))
  3787 		{
  3788 			s=parser.parString(ok,0);
  3789 			if (ok) select (s);
  3790 		}	
  3791 	/////////////////////////////////////////////////////////////////////
  3792 	} else if (com=="selectLastBranch")
  3793 	{
  3794 		if (!selti )
  3795 		{
  3796 			parser.setError (Aborted,"Nothing selected");
  3797 		} else if (! selbi )
  3798 		{				  
  3799 			parser.setError (Aborted,"Type of selection is not a branch");
  3800 		} else if (parser.checkParCount(0))
  3801 		{	
  3802 			BranchItem *bi=selbi->getLastBranch();
  3803 			if (!bi)
  3804 				parser.setError (Aborted,"Could not select last branch");
  3805 			select (bi);		// FIXME-3 was selectInt
  3806 				
  3807 		}	
  3808 	/////////////////////////////////////////////////////////////////////
  3809 	} else /* FIXME-2 if (com=="selectLastImage")
  3810 	{
  3811 		if (!selti )
  3812 		{
  3813 			parser.setError (Aborted,"Nothing selected");
  3814 		} else if (! selbi )
  3815 		{				  
  3816 			parser.setError (Aborted,"Type of selection is not a branch");
  3817 		} else if (parser.checkParCount(0))
  3818 		{	
  3819 			FloatImageObj *fio=selb->getLastFloatImage();
  3820 			if (!fio)
  3821 				parser.setError (Aborted,"Could not select last image");
  3822 			select (fio);		// FIXME-3 was selectInt
  3823 				
  3824 		}	
  3825 	/////////////////////////////////////////////////////////////////////
  3826 	} else */ if (com=="selectLatestAdded")
  3827 	{
  3828 		if (!latestAddedItem)
  3829 		{
  3830 			parser.setError (Aborted,"No latest added object");
  3831 		} else
  3832 		{	
  3833 			if (!select (latestAddedItem))
  3834 				parser.setError (Aborted,"Could not select latest added object ");
  3835 		}	
  3836 	/////////////////////////////////////////////////////////////////////
  3837 	} else if (com=="setFlag")
  3838 	{
  3839 		if (!selti )
  3840 		{
  3841 			parser.setError (Aborted,"Nothing selected");
  3842 		} else if (! selbi )
  3843 		{				  
  3844 			parser.setError (Aborted,"Type of selection is not a branch");
  3845 		} else if (parser.checkParCount(1))
  3846 		{
  3847 			s=parser.parString(ok,0);
  3848 			if (ok) 
  3849 				selbi->activateStandardFlag(s);
  3850 		}
  3851 	/////////////////////////////////////////////////////////////////////
  3852 	} else if (com=="setFrameType")
  3853 	{
  3854 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3855 		{
  3856 			parser.setError (Aborted,"Type of selection does not allow setting frame type");
  3857 		}
  3858 		else if (parser.checkParCount(1))
  3859 		{
  3860 			s=parser.parString(ok,0);
  3861 			if (ok) setFrameType (s);
  3862 		}	
  3863 	/////////////////////////////////////////////////////////////////////
  3864 	} else if (com=="setFramePenColor")
  3865 	{
  3866 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3867 		{
  3868 			parser.setError (Aborted,"Type of selection does not allow setting of pen color");
  3869 		}
  3870 		else if (parser.checkParCount(1))
  3871 		{
  3872 			QColor c=parser.parColor(ok,0);
  3873 			if (ok) setFramePenColor (c);
  3874 		}	
  3875 	/////////////////////////////////////////////////////////////////////
  3876 	} else if (com=="setFrameBrushColor")
  3877 	{
  3878 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3879 		{
  3880 			parser.setError (Aborted,"Type of selection does not allow setting brush color");
  3881 		}
  3882 		else if (parser.checkParCount(1))
  3883 		{
  3884 			QColor c=parser.parColor(ok,0);
  3885 			if (ok) setFrameBrushColor (c);
  3886 		}	
  3887 	/////////////////////////////////////////////////////////////////////
  3888 	} else if (com=="setFramePadding")
  3889 	{
  3890 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3891 		{
  3892 			parser.setError (Aborted,"Type of selection does not allow setting frame padding");
  3893 		}
  3894 		else if (parser.checkParCount(1))
  3895 		{
  3896 			n=parser.parInt(ok,0);
  3897 			if (ok) setFramePadding(n);
  3898 		}	
  3899 	/////////////////////////////////////////////////////////////////////
  3900 	} else if (com=="setFrameBorderWidth")
  3901 	{
  3902 		if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3903 		{
  3904 			parser.setError (Aborted,"Type of selection does not allow setting frame border width");
  3905 		}
  3906 		else if (parser.checkParCount(1))
  3907 		{
  3908 			n=parser.parInt(ok,0);
  3909 			if (ok) setFrameBorderWidth (n);
  3910 		}	
  3911 	/////////////////////////////////////////////////////////////////////
  3912 	/* FIXME-2  else if (com=="setFrameType")
  3913 	{
  3914 		if (!selti )
  3915 		{
  3916 			parser.setError (Aborted,"Nothing selected");
  3917 		} else if (! selb )
  3918 		{				  
  3919 			parser.setError (Aborted,"Type of selection is not a branch");
  3920 		} else if (parser.checkParCount(1))
  3921 		{
  3922 			s=parser.parString(ok,0);
  3923 			if (ok) 
  3924 				setFrameType (s);
  3925 		}
  3926 	/////////////////////////////////////////////////////////////////////
  3927 	} else*/ 
  3928 	/////////////////////////////////////////////////////////////////////
  3929 	} else if (com=="setHeading")
  3930 	{
  3931 		if (!selti )
  3932 		{
  3933 			parser.setError (Aborted,"Nothing selected");
  3934 		} else if (! selbi )
  3935 		{				  
  3936 			parser.setError (Aborted,"Type of selection is not a branch");
  3937 		} else if (parser.checkParCount(1))
  3938 		{
  3939 			s=parser.parString (ok,0);
  3940 			if (ok) 
  3941 				setHeading (s);
  3942 		}	
  3943 	/////////////////////////////////////////////////////////////////////
  3944 	} else if (com=="setHideExport")
  3945 	{
  3946 		if (!selti )
  3947 		{
  3948 			parser.setError (Aborted,"Nothing selected");
  3949 		} else if (selectionType()!=TreeItem::Branch && selectionType() != TreeItem::MapCenter &&selectionType()!=TreeItem::Image)
  3950 		{				  
  3951 			parser.setError (Aborted,"Type of selection is not a branch or floatimage");
  3952 		} else if (parser.checkParCount(1))
  3953 		{
  3954 			b=parser.parBool(ok,0);
  3955 			if (ok) setHideExport (b);
  3956 		}
  3957 	/////////////////////////////////////////////////////////////////////
  3958 	} else if (com=="setIncludeImagesHorizontally")
  3959 	{ 
  3960 		if (!selti )
  3961 		{
  3962 			parser.setError (Aborted,"Nothing selected");
  3963 		} else if (! selbi)
  3964 		{				  
  3965 			parser.setError (Aborted,"Type of selection is not a branch");
  3966 		} else if (parser.checkParCount(1))
  3967 		{
  3968 			b=parser.parBool(ok,0);
  3969 			if (ok) setIncludeImagesHor(b);
  3970 		}
  3971 	/////////////////////////////////////////////////////////////////////
  3972 	} else if (com=="setIncludeImagesVertically")
  3973 	{
  3974 		if (!selti )
  3975 		{
  3976 			parser.setError (Aborted,"Nothing selected");
  3977 		} else if (! selbi)
  3978 		{				  
  3979 			parser.setError (Aborted,"Type of selection is not a branch");
  3980 		} else if (parser.checkParCount(1))
  3981 		{
  3982 			b=parser.parBool(ok,0);
  3983 			if (ok) setIncludeImagesVer(b);
  3984 		}
  3985 	/////////////////////////////////////////////////////////////////////
  3986 	} else if (com=="setHideLinkUnselected")
  3987 	{
  3988 		if (!selti )
  3989 		{
  3990 			parser.setError (Aborted,"Nothing selected");
  3991 		} else if ( selectionType()!=TreeItem::Branch && selectionType()!= TreeItem::MapCenter && selectionType()!=TreeItem::Image)
  3992 		{				  
  3993 			parser.setError (Aborted,"Type of selection does not allow hiding the link");
  3994 		} else if (parser.checkParCount(1))
  3995 		{
  3996 			b=parser.parBool(ok,0);
  3997 			if (ok) setHideLinkUnselected(b);
  3998 		}
  3999 	/////////////////////////////////////////////////////////////////////
  4000 	} else if (com=="setMapAuthor")
  4001 	{
  4002 		if (parser.checkParCount(1))
  4003 		{
  4004 			s=parser.parString(ok,0);
  4005 			if (ok) setAuthor (s);
  4006 		}	
  4007 	/////////////////////////////////////////////////////////////////////
  4008 	} else if (com=="setMapComment")
  4009 	{
  4010 		if (parser.checkParCount(1))
  4011 		{
  4012 			s=parser.parString(ok,0);
  4013 			if (ok) setComment(s);
  4014 		}	
  4015 	/////////////////////////////////////////////////////////////////////
  4016 	} else if (com=="setMapBackgroundColor")
  4017 	{
  4018 		if (!selti )
  4019 		{
  4020 			parser.setError (Aborted,"Nothing selected");
  4021 		} else if (! selbi )
  4022 		{				  
  4023 			parser.setError (Aborted,"Type of selection is not a branch");
  4024 		} else if (parser.checkParCount(1))
  4025 		{
  4026 			QColor c=parser.parColor (ok,0);
  4027 			if (ok) setMapBackgroundColor (c);
  4028 		}	
  4029 	/////////////////////////////////////////////////////////////////////
  4030 	} else if (com=="setMapDefLinkColor")
  4031 	{
  4032 		if (!selti )
  4033 		{
  4034 			parser.setError (Aborted,"Nothing selected");
  4035 		} else if (! selbi )
  4036 		{				  
  4037 			parser.setError (Aborted,"Type of selection is not a branch");
  4038 		} else if (parser.checkParCount(1))
  4039 		{
  4040 			QColor c=parser.parColor (ok,0);
  4041 			if (ok) setMapDefLinkColor (c);
  4042 		}	
  4043 	/////////////////////////////////////////////////////////////////////
  4044 	} else if (com=="setMapLinkStyle")
  4045 	{
  4046 		if (parser.checkParCount(1))
  4047 		{
  4048 			s=parser.parString (ok,0);
  4049 			if (ok) setMapLinkStyle(s);
  4050 		}	
  4051 	/////////////////////////////////////////////////////////////////////
  4052 	} else if (com=="setNote")
  4053 	{
  4054 		if (!selti )
  4055 		{
  4056 			parser.setError (Aborted,"Nothing selected");
  4057 		} else if (! selbi )
  4058 		{				  
  4059 			parser.setError (Aborted,"Type of selection is not a branch");
  4060 		} else if (parser.checkParCount(1))
  4061 		{
  4062 			s=parser.parString (ok,0);
  4063 			if (ok) 
  4064 				setNote (s);
  4065 		}	
  4066 	/////////////////////////////////////////////////////////////////////
  4067 	} else if (com=="setSelectionColor")
  4068 	{
  4069 		if (parser.checkParCount(1))
  4070 		{
  4071 			QColor c=parser.parColor (ok,0);
  4072 			if (ok) setSelectionColorInt (c);
  4073 		}	
  4074 	/////////////////////////////////////////////////////////////////////
  4075 	} else if (com=="setURL")
  4076 	{
  4077 		if (!selti )
  4078 		{
  4079 			parser.setError (Aborted,"Nothing selected");
  4080 		} else if (! selbi )
  4081 		{				  
  4082 			parser.setError (Aborted,"Type of selection is not a branch");
  4083 		} else if (parser.checkParCount(1))
  4084 		{
  4085 			s=parser.parString (ok,0);
  4086 			if (ok) setURL(s);
  4087 		}	
  4088 	/////////////////////////////////////////////////////////////////////
  4089 	} else if (com=="setVymLink")
  4090 	{
  4091 		if (!selti )
  4092 		{
  4093 			parser.setError (Aborted,"Nothing selected");
  4094 		} else if (! selbi )
  4095 		{				  
  4096 			parser.setError (Aborted,"Type of selection is not a branch");
  4097 		} else if (parser.checkParCount(1))
  4098 		{
  4099 			s=parser.parString (ok,0);
  4100 			if (ok) setVymLink(s);
  4101 		}	
  4102 	} else if (com=="sortChildren")
  4103 	{
  4104 		if (!selti )
  4105 		{
  4106 			parser.setError (Aborted,"Nothing selected");
  4107 		} else if (! selbi )
  4108 		{				  
  4109 			parser.setError (Aborted,"Type of selection is not a branch");
  4110 		} else if (parser.checkParCount(0))
  4111 		{
  4112 			sortChildren();
  4113 		} else if (parser.checkParCount(1))
  4114 		{
  4115 			b=parser.parBool(ok,0);
  4116 			if (ok) sortChildren(b);
  4117 		}
  4118 	/////////////////////////////////////////////////////////////////////
  4119 	} else if (com=="toggleFlag")
  4120 	{
  4121 		if (!selti )
  4122 		{
  4123 			parser.setError (Aborted,"Nothing selected");
  4124 		} else if (! selbi )
  4125 		{				  
  4126 			parser.setError (Aborted,"Type of selection is not a branch");
  4127 		} else if (parser.checkParCount(1))
  4128 		{
  4129 			s=parser.parString(ok,0);
  4130 			if (ok) 
  4131 				selbi->toggleStandardFlag(s);	
  4132 		}
  4133 	/////////////////////////////////////////////////////////////////////
  4134 	} else  if (com=="unscroll")
  4135 	{
  4136 		if (!selti)
  4137 		{
  4138 			parser.setError (Aborted,"Nothing selected");
  4139 		} else if (! selbi )
  4140 		{				  
  4141 			parser.setError (Aborted,"Type of selection is not a branch");
  4142 		} else if (parser.checkParCount(0))
  4143 		{	
  4144 			if (!unscrollBranch (selbi))	
  4145 				parser.setError (Aborted,"Could not unscroll branch");
  4146 		}	
  4147 	/////////////////////////////////////////////////////////////////////
  4148 	} else if (com=="unscrollChildren")
  4149 	{
  4150 		if (!selti)
  4151 		{
  4152 			parser.setError (Aborted,"Nothing selected");
  4153 		} else if (! selbi )
  4154 		{				  
  4155 			parser.setError (Aborted,"Type of selection is not a branch");
  4156 		} else if (parser.checkParCount(0))
  4157 		{	
  4158 			unscrollChildren ();
  4159 		}	
  4160 	/////////////////////////////////////////////////////////////////////
  4161 	} else if (com=="unsetFlag")
  4162 	{
  4163 		if (!selti)
  4164 		{
  4165 			parser.setError (Aborted,"Nothing selected");
  4166 		} else if (! selbi )
  4167 		{				  
  4168 			parser.setError (Aborted,"Type of selection is not a branch");
  4169 		} else if (parser.checkParCount(1))
  4170 		{
  4171 			s=parser.parString(ok,0);
  4172 			if (ok) 
  4173 				selbi->deactivateStandardFlag(s);
  4174 		}
  4175 	} else 
  4176 		parser.setError (Aborted,"Unknown command");
  4177 
  4178 	// Any errors?
  4179 	if (parser.errorLevel()==NoError)
  4180 	{
  4181 		reposition();
  4182 		errorMsg.clear();
  4183 		noErr=true;
  4184 	}	
  4185 	else	
  4186 	{
  4187 		// TODO Error handling
  4188 		qWarning("VymModel::parseAtom: Error!");
  4189 
  4190 		qWarning(parser.errorMessage());
  4191 		noErr=false;
  4192 		errorMsg=parser.errorMessage();
  4193 		returnValue=errorMsg;
  4194 	} 
  4195 	return returnValue;
  4196 }
  4197 
  4198 QVariant VymModel::runScript (const QString &script)
  4199 {
  4200 	parser.setScript (script);
  4201 	parser.runScript();
  4202 	QVariant r;
  4203 	bool noErr=true;
  4204 	QString errMsg;
  4205 	while (parser.next() && noErr) 
  4206 	{
  4207 		r=parseAtom(parser.getAtom(),noErr,errMsg);
  4208 		if (!noErr)	//FIXME-3 need dialog box here
  4209 			cout << "VM::runScript aborted:\n"<<errMsg.toStdString()<<endl;
  4210 	}	
  4211 	return r;
  4212 }
  4213 
  4214 void VymModel::setExportMode (bool b)
  4215 {
  4216 	// should be called before and after exports
  4217 	// depending on the settings
  4218 	if (b && settings.value("/export/useHideExport","true")=="true")
  4219 		setHideTmpMode (TreeItem::HideExport);
  4220 	else	
  4221 		setHideTmpMode (TreeItem::HideNone);
  4222 }
  4223 
  4224 void VymModel::exportImage(QString fname, bool askName, QString format)
  4225 {
  4226 	if (fname=="")
  4227 	{
  4228 		fname=getMapName()+".png";
  4229 		format="PNG";
  4230 	} 	
  4231 
  4232 	if (askName)
  4233 	{
  4234 		QStringList fl;
  4235 		QFileDialog *fd=new QFileDialog (NULL);
  4236 		fd->setCaption (tr("Export map as image"));
  4237 		fd->setDirectory (lastImageDir);
  4238 		fd->setFileMode(QFileDialog::AnyFile);
  4239 		fd->setFilters  (imageIO.getFilters() );
  4240 		if (fd->exec())
  4241 		{
  4242 			fl=fd->selectedFiles();
  4243 			fname=fl.first();
  4244 			format=imageIO.getType(fd->selectedFilter());
  4245 		} 
  4246 	}
  4247 
  4248 	setExportMode (true);
  4249 	mapEditor->getScene()->update();		// FIXME-3 check this...
  4250 	QImage img (mapEditor->getImage());	//FIXME-3 calls getTotalBBox, but also in ExportHTML::doExport()
  4251 	img.save(fname, format);
  4252 	setExportMode (false);
  4253 }
  4254 
  4255 
  4256 void VymModel::exportXML(QString dir, bool askForName)
  4257 {
  4258 	if (askForName)
  4259 	{
  4260 		dir=browseDirectory(NULL,tr("Export XML to directory"));
  4261 		if (dir =="" && !reallyWriteDirectory(dir) )
  4262 		return;
  4263 	}
  4264 
  4265 	// Hide stuff during export, if settings want this
  4266 	setExportMode (true);
  4267 
  4268 	// Create subdirectories
  4269 	makeSubDirs (dir);
  4270 
  4271 	// write to directory	//FIXME-4 check totalBBox here...
  4272 	QString saveFile=saveToDir (dir,mapName+"-",true,mapEditor->getTotalBBox().topLeft() ,NULL); 
  4273 	QFile file;
  4274 
  4275 	file.setName ( dir + "/"+mapName+".xml");
  4276 	if ( !file.open( QIODevice::WriteOnly ) )
  4277 	{
  4278 		// This should neverever happen
  4279 		QMessageBox::critical (0,tr("Critical Export Error"),tr("VymModel::exportXML couldn't open %1").arg(file.name()));
  4280 		return;
  4281 	}	
  4282 
  4283 	// Write it finally, and write in UTF8, no matter what 
  4284 	QTextStream ts( &file );
  4285 	ts.setEncoding (QTextStream::UnicodeUTF8);
  4286 	ts << saveFile;
  4287 	file.close();
  4288 
  4289 	// Now write image, too
  4290 	exportImage (dir+"/images/"+mapName+".png",false,"PNG");
  4291 
  4292 	setExportMode (false);
  4293 }
  4294 
  4295 void VymModel::exportAO (QString fname,bool askName)
  4296 {
  4297 	ExportAO ex;
  4298 	ex.setModel (this);
  4299 	if (fname=="") 
  4300 		ex.setFile (mapName+".txt");	
  4301 	else
  4302 		ex.setFile (fname);
  4303 
  4304 	if (askName)
  4305 	{
  4306 		//ex.addFilter ("TXT (*.txt)");
  4307 		ex.setDir(lastImageDir);
  4308 		//ex.setCaption(vymName+ " -" +tr("Export as A&O report")+" "+tr("(still experimental)"));
  4309 		ex.execDialog() ; 
  4310 	} 
  4311 	if (!ex.canceled())
  4312 	{
  4313 		setExportMode(true);
  4314 		ex.doExport();
  4315 		setExportMode(false);
  4316 	}
  4317 }
  4318 
  4319 void VymModel::exportASCII(QString fname,bool askName)
  4320 {
  4321 	ExportASCII ex;
  4322 	ex.setModel (this);
  4323 	if (fname=="") 
  4324 		ex.setFile (mapName+".txt");	
  4325 	else
  4326 		ex.setFile (fname);
  4327 
  4328 	if (askName)
  4329 	{
  4330 		//ex.addFilter ("TXT (*.txt)");
  4331 		ex.setDir(lastImageDir);
  4332 		//ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
  4333 		ex.execDialog() ; 
  4334 	} 
  4335 	if (!ex.canceled())
  4336 	{
  4337 		setExportMode(true);
  4338 		ex.doExport();
  4339 		setExportMode(false);
  4340 	}
  4341 }
  4342 
  4343 void VymModel::exportHTML (const QString &dir, bool useDialog)	
  4344 {
  4345 	ExportHTML ex (this);
  4346 	ex.setDir (dir);
  4347 	ex.doExport(useDialog);
  4348 }
  4349 
  4350 void VymModel::exportXHTML (const QString &dir, bool askForName)
  4351 {
  4352 	ExportXHTMLDialog dia(NULL);
  4353 	dia.setFilePath (filePath );
  4354 	dia.setMapName (mapName );
  4355 	dia.readSettings();
  4356 	if (dir!="") dia.setDir (dir);
  4357 
  4358 	bool ok=true;
  4359 	
  4360 	if (askForName)
  4361 	{
  4362 		if (dia.exec()!=QDialog::Accepted) 
  4363 			ok=false;
  4364 		else	
  4365 		{
  4366 			QDir d (dia.getDir());
  4367 			// Check, if warnings should be used before overwriting
  4368 			// the output directory
  4369 			if (d.exists() && d.count()>0)
  4370 			{
  4371 				WarningDialog warn;
  4372 				warn.showCancelButton (true);
  4373 				warn.setText(QString(
  4374 					"The directory %1 is not empty.\n"
  4375 					"Do you risk to overwrite some of its contents?").arg(d.path() ));
  4376 				warn.setCaption("Warning: Directory not empty");
  4377 				warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
  4378 
  4379 				if (warn.exec()!=QDialog::Accepted) ok=false;
  4380 			}
  4381 		}	
  4382 	}
  4383 
  4384 	if (ok)
  4385 	{
  4386 		exportXML (dia.getDir(),false );
  4387 		dia.doExport(mapName );
  4388 		//if (dia.hasChanged()) setChanged();
  4389 	}
  4390 }
  4391 
  4392 void VymModel::exportOOPresentation(const QString &fn, const QString &cf)
  4393 {
  4394 	ExportOO ex;
  4395 	ex.setFile (fn);
  4396 	ex.setModel (this);
  4397 	if (ex.setConfigFile(cf)) 
  4398 	{
  4399 		setExportMode (true);
  4400 		ex.exportPresentation();
  4401 		setExportMode (false);
  4402 	}
  4403 }
  4404 
  4405 
  4406 
  4407 
  4408 //////////////////////////////////////////////
  4409 // View related
  4410 //////////////////////////////////////////////
  4411 
  4412 void VymModel::registerEditor(QWidget *me)
  4413 {
  4414 	mapEditor=(MapEditor*)me;
  4415 }
  4416 
  4417 void VymModel::unregisterEditor(QWidget *)
  4418 {
  4419 	mapEditor=NULL;
  4420 }
  4421 
  4422 void VymModel::setMapZoomFactor (const double &d)
  4423 {
  4424 	zoomFactor=d;
  4425 }
  4426 
  4427 void VymModel::setContextPos(QPointF p)
  4428 {
  4429 	contextPos=p;
  4430 }
  4431 
  4432 void VymModel::unsetContextPos()
  4433 {
  4434 	contextPos=QPointF();
  4435 }
  4436 
  4437 void VymModel::updateNoteFlag()
  4438 {
  4439 	TreeItem *selti=getSelectedItem();
  4440 	if (selti)
  4441 	{
  4442 		if (!mapChanged)
  4443 		{
  4444 			setChanged();
  4445 			updateActions();
  4446 		}
  4447 
  4448 		if (textEditor->isEmpty()) 
  4449 			selti->clearNote();
  4450 		else
  4451 			selti->setNote (textEditor->getText());
  4452 		emitDataHasChanged(selti);		
  4453 	}
  4454 }
  4455 
  4456 void VymModel::reposition()	//FIXME-4 VM should have no need to reposition, but the views...
  4457 {
  4458 	//cout << "VM::reposition blocked="<<blockReposition<<endl;
  4459 	if (blockReposition) return;
  4460 
  4461 	for (int i=0;i<rootItem->branchCount(); i++)
  4462 		rootItem->getBranchObjNum(i)->reposition();	//	for positioning heading
  4463 	//emitSelectionChanged();	
  4464 }
  4465 
  4466 
  4467 void VymModel::setMapLinkStyle (const QString & s)
  4468 {
  4469 	QString snow;
  4470 	switch (linkstyle)
  4471 	{
  4472 		case LinkableMapObj::Line :
  4473 			snow="StyleLine";
  4474 			break;
  4475 		case LinkableMapObj::Parabel:
  4476 			snow="StyleParabel";
  4477 			break;
  4478 		case LinkableMapObj::PolyLine:
  4479 			snow="StylePolyLine";
  4480 			break;
  4481 		case LinkableMapObj::PolyParabel:
  4482 			snow="StylePolyParabel";
  4483 			break;
  4484 		default:	
  4485 			snow="UndefinedStyle";
  4486 			break;
  4487 	}
  4488 
  4489 	saveState (
  4490 		QString("setMapLinkStyle (\"%1\")").arg(s),
  4491 		QString("setMapLinkStyle (\"%1\")").arg(snow),
  4492 		QString("Set map link style (\"%1\")").arg(s)
  4493 	);	
  4494 
  4495 	if (s=="StyleLine")
  4496 		linkstyle=LinkableMapObj::Line;
  4497 	else if (s=="StyleParabel")
  4498 		linkstyle=LinkableMapObj::Parabel;
  4499 	else if (s=="StylePolyLine")
  4500 		linkstyle=LinkableMapObj::PolyLine;
  4501 	else if (s=="StylePolyParabel")	
  4502 		linkstyle=LinkableMapObj::PolyParabel;
  4503 	else
  4504 		linkstyle=LinkableMapObj::UndefinedStyle;
  4505 
  4506 	BranchItem *cur=NULL;
  4507 	BranchItem *prev=NULL;
  4508 	BranchObj *bo;
  4509 	nextBranch (cur,prev);
  4510 	while (cur) 
  4511 	{
  4512 		bo=(BranchObj*)(cur->getLMO() );
  4513 		bo->setLinkStyle(bo->getDefLinkStyle(cur->parent() ));	//FIXME-3 better emit dataCHanged and leave the changes to View
  4514 		cur=nextBranch(cur,prev);
  4515 	}
  4516 	reposition();
  4517 }
  4518 
  4519 LinkableMapObj::Style VymModel::getMapLinkStyle ()
  4520 {
  4521 	return linkstyle;
  4522 }	
  4523 
  4524 uint VymModel::getID()
  4525 {
  4526 	return mapID;
  4527 }
  4528 
  4529 void VymModel::setMapDefLinkColor(QColor col)
  4530 {
  4531 	if ( !col.isValid() ) return;
  4532 	saveState (
  4533 		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
  4534 		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
  4535 		QString("Set map link color to %1").arg(col.name())
  4536 	);
  4537 
  4538 	defLinkColor=col;
  4539 	BranchItem *cur=NULL;
  4540 	BranchItem *prev=NULL;
  4541 	BranchObj *bo;
  4542 	cur=nextBranch(cur,prev);
  4543 	while (cur) 
  4544 	{
  4545 		bo=(BranchObj*)(cur->getLMO() );
  4546 		bo->setLinkColor();
  4547 		nextBranch(cur,prev);
  4548 	}
  4549 	updateActions();
  4550 }
  4551 
  4552 void VymModel::setMapLinkColorHintInt()
  4553 {
  4554 	// called from setMapLinkColorHint(lch) or at end of parse
  4555 	BranchItem *cur=NULL;
  4556 	BranchItem *prev=NULL;
  4557 	BranchObj *bo;
  4558 	cur=nextBranch(cur,prev);
  4559 	while (cur) 
  4560 	{
  4561 		bo=(BranchObj*)(cur->getLMO() );
  4562 		bo->setLinkColor();
  4563 		cur=nextBranch(cur,prev);
  4564 	}
  4565 }
  4566 
  4567 void VymModel::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
  4568 {
  4569 	linkcolorhint=lch;
  4570 	setMapLinkColorHintInt();
  4571 }
  4572 
  4573 void VymModel::toggleMapLinkColorHint()
  4574 {
  4575 	if (linkcolorhint==LinkableMapObj::HeadingColor)
  4576 		linkcolorhint=LinkableMapObj::DefaultColor;
  4577 	else	
  4578 		linkcolorhint=LinkableMapObj::HeadingColor;
  4579 	BranchItem *cur=NULL;
  4580 	BranchItem *prev=NULL;
  4581 	BranchObj *bo;
  4582 	cur=nextBranch(cur,prev);
  4583 	while (cur) 
  4584 	{
  4585 		bo=(BranchObj*)(cur->getLMO() );
  4586 		bo->setLinkColor();
  4587 		nextBranch(cur,prev);
  4588 	}
  4589 }
  4590 
  4591 void VymModel::selectMapBackgroundImage ()	// FIXME-3 for using background image: view.setCacheMode(QGraphicsView::CacheBackground);  Also this belongs into ME
  4592 {
  4593 	Q3FileDialog *fd=new Q3FileDialog( NULL);
  4594 	fd->setMode (Q3FileDialog::ExistingFile);
  4595 	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  4596 	ImagePreview *p =new ImagePreview (fd);
  4597 	fd->setContentsPreviewEnabled( TRUE );
  4598 	fd->setContentsPreview( p, p );
  4599 	fd->setPreviewMode( Q3FileDialog::Contents );
  4600 	fd->setCaption(vymName+" - " +tr("Load background image"));
  4601 	fd->setDir (lastImageDir);
  4602 	fd->show();
  4603 
  4604 	if ( fd->exec() == QDialog::Accepted )
  4605 	{
  4606 		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
  4607 		lastImageDir=QDir (fd->dirPath());
  4608 		setMapBackgroundImage (fd->selectedFile());
  4609 	}
  4610 }	
  4611 
  4612 void VymModel::setMapBackgroundImage (const QString &fn)	//FIXME-3 missing savestate, move to ME
  4613 {
  4614 	QColor oldcol=mapScene->backgroundBrush().color();
  4615 	/*
  4616 	saveState(
  4617 		selection,
  4618 		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
  4619 		selection,
  4620 		QString ("setMapBackgroundImage (%1)").arg(col.name()),
  4621 		QString("Set background color of map to %1").arg(col.name()));
  4622 	*/	
  4623 	QBrush brush;
  4624 	brush.setTextureImage (QPixmap (fn));
  4625 	mapScene->setBackgroundBrush(brush);
  4626 }
  4627 
  4628 void VymModel::selectMapBackgroundColor()	// FIXME-3 move to ME
  4629 {
  4630 	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), NULL);
  4631 	if ( !col.isValid() ) return;
  4632 	setMapBackgroundColor( col );
  4633 }
  4634 
  4635 
  4636 void VymModel::setMapBackgroundColor(QColor col)	// FIXME-3 move to ME
  4637 {
  4638 	QColor oldcol=mapScene->backgroundBrush().color();
  4639 	saveState(
  4640 		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
  4641 		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
  4642 		QString("Set background color of map to %1").arg(col.name()));
  4643 	mapScene->setBackgroundBrush(col);
  4644 }
  4645 
  4646 QColor VymModel::getMapBackgroundColor()	// FIXME-3 move to ME
  4647 {
  4648     return mapScene->backgroundBrush().color();
  4649 }
  4650 
  4651 
  4652 LinkableMapObj::ColorHint VymModel::getMapLinkColorHint()	// FIXME-3 move to ME
  4653 {
  4654 	return linkcolorhint;
  4655 }
  4656 
  4657 QColor VymModel::getMapDefLinkColor()	// FIXME-3 move to ME
  4658 {
  4659 	return defLinkColor;
  4660 }
  4661 
  4662 void VymModel::setMapDefXLinkColor(QColor col)	// FIXME-3 move to ME
  4663 {
  4664 	defXLinkColor=col;
  4665 }
  4666 
  4667 QColor VymModel::getMapDefXLinkColor()	// FIXME-3 move to ME
  4668 {
  4669 	return defXLinkColor;
  4670 }
  4671 
  4672 void VymModel::setMapDefXLinkWidth (int w)	// FIXME-3 move to ME
  4673 {
  4674 	defXLinkWidth=w;
  4675 }
  4676 
  4677 int VymModel::getMapDefXLinkWidth()	// FIXME-3 move to ME
  4678 {
  4679 	return defXLinkWidth;
  4680 }
  4681 
  4682 void VymModel::move(const double &x, const double &y)
  4683 {
  4684 	int i=x; i=y;
  4685 	MapItem *seli = (MapItem*)getSelectedItem();
  4686 	if (seli && (seli->isBranchLikeType() || seli->getType()==TreeItem::Image))
  4687 	{
  4688 		LinkableMapObj *lmo=seli->getLMO();
  4689 		if (lmo)
  4690 		{
  4691 			QPointF ap(lmo->getAbsPos());
  4692 			QPointF to(x, y);
  4693 			if (ap != to)
  4694 			{
  4695 				QString ps=qpointFToString(ap);
  4696 				QString s=getSelectString(seli);
  4697 				saveState(
  4698 					s, "move "+ps, 
  4699 					s, "move "+qpointFToString(to), 
  4700 					QString("Move %1 to %2").arg(getObjectName(seli)).arg(ps));
  4701 				lmo->move(x,y);
  4702 				reposition();
  4703 				emitSelectionChanged();
  4704 			}
  4705 		}
  4706 	}
  4707 }
  4708 
  4709 void VymModel::moveRel (const double &x, const double &y)	
  4710 {
  4711 	int i=x; i=y;
  4712 	MapItem *seli = (MapItem*)getSelectedItem();
  4713 	if (seli && (seli->isBranchLikeType() || seli->getType()==TreeItem::Image))
  4714 	{
  4715 		LinkableMapObj *lmo=seli->getLMO();
  4716 		if (lmo)
  4717 		{
  4718 			QPointF rp(lmo->getRelPos());
  4719 			QPointF to(x, y);
  4720 			if (rp != to)
  4721 			{
  4722 				QString ps=qpointFToString (lmo->getRelPos());
  4723 				QString s=getSelectString(seli);
  4724 				saveState(
  4725 					s, "moveRel "+ps, 
  4726 					s, "moveRel "+qpointFToString(to), 
  4727 					QString("Move %1 to relative position %2").arg(getObjectName(seli)).arg(ps));
  4728 				((OrnamentedObj*)lmo)->move2RelPos (x,y);
  4729 				reposition();
  4730 				lmo->updateLinkGeometry();
  4731 				emitSelectionChanged();
  4732 			}
  4733 		}	
  4734 	}
  4735 }
  4736 
  4737 
  4738 void VymModel::animate()	
  4739 {
  4740 	animationTimer->stop();
  4741 	BranchObj *bo;
  4742 	int i=0;
  4743 	while (i<animObjList.size() )
  4744 	{
  4745 		bo=(BranchObj*)animObjList.at(i);
  4746 		if (!bo->animate())
  4747 		{
  4748 			if (i>=0) 
  4749 			{	
  4750 				animObjList.removeAt(i);
  4751 				i--;
  4752 			}
  4753 		}
  4754 		bo->reposition();
  4755 		i++;
  4756 	} 
  4757 	QItemSelection sel=selModel->selection();
  4758 	emit (selectionChanged(sel,sel));
  4759 
  4760 	mapScene->update();
  4761 	if (!animObjList.isEmpty()) animationTimer->start();
  4762 }
  4763 
  4764 
  4765 void VymModel::startAnimation(BranchObj *bo, const QPointF &v)
  4766 {
  4767 	if (!bo) return;
  4768 
  4769 	if (bo->getUseRelPos())
  4770 		startAnimation (bo,bo->getRelPos(),bo->getRelPos()+v);
  4771 	else
  4772 		startAnimation (bo,bo->getAbsPos(),bo->getAbsPos()+v);
  4773 }
  4774 
  4775 void VymModel::startAnimation(BranchObj *bo, const QPointF &start, const QPointF &dest)
  4776 {
  4777 	if (start==dest) return;
  4778 	if (bo && bo->getTreeItem()->depth()>=0) 
  4779 	{
  4780 		AnimPoint ap;
  4781 		ap.setStart (start);
  4782 		ap.setDest  (dest);
  4783 		ap.setTicks (animationTicks);
  4784 		ap.setAnimated (true);
  4785 		bo->setAnimation (ap);
  4786 		if (!animObjList.contains(bo))
  4787 			animObjList.append( bo );
  4788 		animationTimer->setSingleShot (true);
  4789 		animationTimer->start(animationInterval);
  4790 	}
  4791 }
  4792 
  4793 void VymModel::stopAnimation (MapObj *mo)
  4794 {
  4795 	int i=animObjList.indexOf(mo);
  4796     if (i>=0)
  4797 		animObjList.removeAt (i);
  4798 }
  4799 
  4800 void VymModel::sendSelection()
  4801 {
  4802 	if (netstate!=Server) return;
  4803 	sendData (QString("select (\"%1\")").arg(getSelectString()) );
  4804 }
  4805 
  4806 void VymModel::newServer()
  4807 {
  4808 	port=54321;
  4809 	sendCounter=0;
  4810     tcpServer = new QTcpServer(this);
  4811     if (!tcpServer->listen(QHostAddress::Any,port)) {
  4812         QMessageBox::critical(NULL, "vym server",
  4813                               QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
  4814         //FIXME-3 needed? we are no widget any longer... close();
  4815         return;
  4816     }
  4817 	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
  4818 	netstate=Server;
  4819 	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
  4820 }
  4821 
  4822 void VymModel::connectToServer()
  4823 {
  4824 	port=54321;
  4825 	server="salam.suse.de";
  4826 	server="localhost";
  4827 	clientSocket = new QTcpSocket (this);
  4828 	clientSocket->abort();
  4829     clientSocket->connectToHost(server ,port);
  4830 	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
  4831     connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
  4832             this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
  4833 	netstate=Client;		
  4834 	cout<<"connected to "<<qPrintable (server)<<" port "<<port<<endl;
  4835 
  4836 	
  4837 }
  4838 
  4839 void VymModel::newClient()
  4840 {
  4841     QTcpSocket *newClient = tcpServer->nextPendingConnection();
  4842     connect(newClient, SIGNAL(disconnected()),
  4843             newClient, SLOT(deleteLater()));
  4844 
  4845 	cout <<"ME::newClient  at "<<qPrintable( newClient->peerAddress().toString() )<<endl;
  4846 
  4847 	clientList.append (newClient);
  4848 }
  4849 
  4850 
  4851 void VymModel::sendData(const QString &s)
  4852 {
  4853 	if (clientList.size()==0) return;
  4854 
  4855 	// Create bytearray to send
  4856 	QByteArray block;
  4857     QDataStream out(&block, QIODevice::WriteOnly);
  4858     out.setVersion(QDataStream::Qt_4_0);
  4859 
  4860 	// Reserve some space for blocksize
  4861     out << (quint16)0;
  4862 
  4863 	// Write sendCounter
  4864     out << sendCounter++;
  4865 
  4866 	// Write data
  4867     out << s;
  4868 
  4869 	// Go back and write blocksize so far
  4870     out.device()->seek(0);
  4871     quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
  4872 	out << bs;
  4873 
  4874 	if (debug)
  4875 		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<qPrintable(s)<<endl;
  4876 
  4877 	for (int i=0; i<clientList.size(); ++i)
  4878 	{
  4879 		//cout << "Sending \""<<qPrintable (s)<<"\" to "<<qPrintable (clientList.at(i)->peerAddress().toString())<<endl;
  4880 		clientList.at(i)->write (block);
  4881 	}
  4882 }
  4883 
  4884 void VymModel::readData ()
  4885 {
  4886 	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
  4887 	{
  4888 		if (debug)
  4889 			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
  4890 		quint16 recCounter;
  4891 		quint16 blockSize;
  4892 
  4893 		QDataStream in(clientSocket);
  4894 		in.setVersion(QDataStream::Qt_4_0);
  4895 
  4896 		in >> blockSize;
  4897 		in >> recCounter;
  4898 		
  4899 		QString t;
  4900 		in >>t;
  4901 		if (debug)
  4902 			cout << "VymModel::readData  command="<<qPrintable (t)<<endl;
  4903 		bool noErr;
  4904 		QString errMsg;
  4905 		parseAtom (t,noErr,errMsg);
  4906 
  4907 	}
  4908 	return;
  4909 }
  4910 
  4911 void VymModel::displayNetworkError(QAbstractSocket::SocketError socketError)
  4912 {
  4913     switch (socketError) {
  4914     case QAbstractSocket::RemoteHostClosedError:
  4915         break;
  4916     case QAbstractSocket::HostNotFoundError:
  4917         QMessageBox::information(NULL, vymName +" Network client",
  4918                                  "The host was not found. Please check the "
  4919                                     "host name and port settings.");
  4920         break;
  4921     case QAbstractSocket::ConnectionRefusedError:
  4922         QMessageBox::information(NULL, vymName + " Network client",
  4923                                  "The connection was refused by the peer. "
  4924                                     "Make sure the fortune server is running, "
  4925                                     "and check that the host name and port "
  4926                                     "settings are correct.");
  4927         break;
  4928     default:
  4929         QMessageBox::information(NULL, vymName + " Network client",
  4930                                  QString("The following error occurred: %1.")
  4931                                  .arg(clientSocket->errorString()));
  4932     }
  4933 }
  4934 
  4935 /* FIXME-3 Playing with DBUS...
  4936 QDBusVariant VymModel::query (const QString &query)
  4937 {
  4938 	TreeItem *selti=getSelectedItem();
  4939 	if (selti)
  4940 		return QDBusVariant (selti->getHeading());
  4941 	else
  4942 		return QDBusVariant ("Nothing selected.");
  4943 }
  4944 */
  4945 
  4946 void VymModel::testslot()	//FIXME-3 Playing with DBUS
  4947 {
  4948 	cout << "VM::testslot called\n";
  4949 }
  4950 
  4951 void VymModel::selectMapSelectionColor()
  4952 {
  4953 	QColor col = QColorDialog::getColor( defLinkColor, NULL);
  4954 	setSelectionColor (col);
  4955 }
  4956 
  4957 void VymModel::setSelectionColorInt (QColor col)
  4958 {
  4959 	if ( !col.isValid() ) return;
  4960 	saveState (
  4961 		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
  4962 		QString("setSelectionColor (%1)").arg(col.name()),
  4963 		QString("Set color of selection box to %1").arg(col.name())
  4964 	);
  4965 
  4966 	mapEditor->setSelectionColor (col);
  4967 }
  4968 
  4969 void VymModel::emitSelectionChanged(const QItemSelection &newsel)
  4970 {
  4971 	emit (selectionChanged(newsel,newsel));	// needed e.g. to update geometry in editor
  4972 	//FIXME-3 emitShowSelection();
  4973 	sendSelection();
  4974 }
  4975 
  4976 void VymModel::emitSelectionChanged()
  4977 {
  4978 	QItemSelection newsel=selModel->selection();
  4979 	emitSelectionChanged (newsel);
  4980 }
  4981 
  4982 void VymModel::setSelectionColor(QColor col)
  4983 {
  4984 	if ( !col.isValid() ) return;
  4985 	saveState (
  4986 		QString("setSelectionColor (%1)").arg(mapEditor->getSelectionColor().name()),
  4987 		QString("setSelectionColor (%1)").arg(col.name()),
  4988 		QString("Set color of selection box to %1").arg(col.name())
  4989 	);
  4990 	setSelectionColorInt (col);
  4991 }
  4992 
  4993 QColor VymModel::getSelectionColor()
  4994 {
  4995 	return mapEditor->getSelectionColor();
  4996 }
  4997 
  4998 void VymModel::setHideTmpMode (TreeItem::HideTmpMode mode)
  4999 {
  5000 	hidemode=mode;
  5001 	for (int i=0;i<rootItem->branchCount();i++)
  5002 		rootItem->getBranchNum(i)->setHideTmp (mode);	
  5003 	reposition();
  5004 	if (mode==TreeItem::HideExport)
  5005 		unselect();
  5006 	else
  5007 		reselect();
  5008 }
  5009 
  5010 //////////////////////////////////////////////
  5011 // Selection related
  5012 //////////////////////////////////////////////
  5013 
  5014 void VymModel::setSelectionModel (QItemSelectionModel *sm)
  5015 {
  5016 	selModel=sm;
  5017 }
  5018 
  5019 QItemSelectionModel* VymModel::getSelectionModel()
  5020 {
  5021 	return selModel;
  5022 }
  5023 
  5024 void VymModel::setSelectionBlocked (bool b)
  5025 {
  5026 	selectionBlocked=b;
  5027 }
  5028 
  5029 bool VymModel::isSelectionBlocked()
  5030 {
  5031 	return selectionBlocked;
  5032 }
  5033 
  5034 bool VymModel::select ()
  5035 {
  5036 	return select (selModel->selectedIndexes().first());	// TODO no multiselections yet
  5037 }
  5038 
  5039 bool VymModel::select (const QString &s)
  5040 {
  5041 	if (s.isEmpty())
  5042 	{
  5043 		unselect();
  5044 		return true;
  5045 	}
  5046 	TreeItem *ti=findBySelectString(s);
  5047 	if (ti) return select (index(ti));
  5048 	return false;
  5049 }
  5050 
  5051 bool VymModel::select (LinkableMapObj *lmo)
  5052 {
  5053 	QItemSelection oldsel=selModel->selection();
  5054 
  5055 	if (lmo)
  5056 		return select (index (lmo->getTreeItem()) );
  5057 	else	
  5058 		return false;
  5059 }
  5060 
  5061 bool VymModel::select (TreeItem *ti)
  5062 {
  5063 	if (ti) return select (index(ti));
  5064 	return false;
  5065 }
  5066 
  5067 bool VymModel::select (const QModelIndex &index)
  5068 {
  5069 	if (index.isValid() )
  5070 	{
  5071 		selModel->select (index,QItemSelectionModel::ClearAndSelect  );
  5072 		BranchItem *bi=getSelectedBranch();
  5073 		if (bi) bi->setLastSelectedBranch();
  5074 		return true;
  5075 	}
  5076 	return false;
  5077 }
  5078 
  5079 void VymModel::unselect()
  5080 {
  5081 	if (!selModel->selectedIndexes().isEmpty())
  5082 	{
  5083 		lastSelectString=getSelectString();
  5084 		selModel->clearSelection();
  5085 	}
  5086 }	
  5087 
  5088 bool VymModel::reselect()
  5089 {
  5090 	return select (lastSelectString);
  5091 }	
  5092 
  5093 void VymModel::emitShowSelection()	
  5094 {
  5095 	if (!blockReposition)
  5096 		emit (showSelection() );
  5097 }
  5098 
  5099 void VymModel::emitNoteHasChanged (TreeItem *ti)
  5100 {
  5101 	QModelIndex ix=index(ti);
  5102 	emit (noteHasChanged (ix) );
  5103 }
  5104 
  5105 void VymModel::emitDataHasChanged (TreeItem *ti)
  5106 {
  5107 	QModelIndex ix=index(ti);
  5108 	emit (dataChanged (ix,ix) );
  5109 }
  5110 
  5111 void VymModel::emitUpdateLayout()
  5112 {
  5113 	if (settings.value("/mainwindow/autoLayout/use","true")=="true")
  5114 		emit (updateLayout());
  5115 }
  5116 
  5117 bool VymModel::selectFirstBranch()
  5118 {
  5119 	TreeItem *ti=getSelectedBranch();
  5120 	if (ti)
  5121 	{
  5122 		TreeItem *par=ti->parent();
  5123 		if (par) 
  5124 		{
  5125 			TreeItem *ti2=par->getFirstBranch();
  5126 			if (ti2) return  select(ti2);
  5127 		}
  5128 	}		
  5129 	return false;
  5130 }
  5131 
  5132 bool VymModel::selectLastBranch()
  5133 {
  5134 	TreeItem *ti=getSelectedBranch();
  5135 	if (ti)
  5136 	{
  5137 		TreeItem *par=ti->parent();
  5138 		if (par) 
  5139 		{
  5140 			TreeItem *ti2=par->getLastBranch();
  5141 			if (ti2) return select(ti2);
  5142 		}
  5143 	}		
  5144 	return false;
  5145 }
  5146 
  5147 bool VymModel::selectLastSelectedBranch()
  5148 {
  5149 	BranchItem *bi=getSelectedBranch();
  5150 	if (bi)
  5151 	{
  5152 		bi=bi->getLastSelectedBranch();
  5153 		if (bi) return select (bi);
  5154 	}		
  5155 	return false;
  5156 }
  5157 
  5158 bool VymModel::selectParent()
  5159 {
  5160 	TreeItem *ti=getSelectedItem();
  5161 	TreeItem *par;
  5162 	if (ti)
  5163 	{
  5164 		par=ti->parent();
  5165 		if (par) 
  5166 			return select(par);
  5167 	}		
  5168 	return false;
  5169 }
  5170 
  5171 TreeItem::Type VymModel::selectionType()
  5172 {
  5173 	QModelIndexList list=selModel->selectedIndexes();
  5174 	if (list.isEmpty()) return TreeItem::Undefined;	
  5175 	TreeItem *ti = getItem (list.first() );
  5176 	return ti->getType();
  5177 
  5178 }
  5179 
  5180 LinkableMapObj* VymModel::getSelectedLMO()
  5181 {
  5182 	QModelIndexList list=selModel->selectedIndexes();
  5183 	if (!list.isEmpty() )
  5184 	{
  5185 		TreeItem *ti = getItem (list.first() );
  5186 		TreeItem::Type type=ti->getType();
  5187 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter || type==TreeItem::Image)
  5188 			return ((MapItem*)ti)->getLMO();
  5189 	}
  5190 	return NULL;
  5191 }
  5192 
  5193 BranchObj* VymModel::getSelectedBranchObj()	// FIXME-3 this should not be needed in the end!!!
  5194 {
  5195 	TreeItem *ti = getSelectedBranch();
  5196 	if (ti)
  5197 		return (BranchObj*)(  ((MapItem*)ti)->getLMO());
  5198 	else	
  5199 		return NULL;
  5200 }
  5201 
  5202 BranchItem* VymModel::getSelectedBranch()
  5203 {
  5204 	QModelIndexList list=selModel->selectedIndexes();
  5205 	if (!list.isEmpty() )
  5206 	{
  5207 		TreeItem *ti = getItem (list.first() );
  5208 		TreeItem::Type type=ti->getType();
  5209 		if (type ==TreeItem::Branch || type==TreeItem::MapCenter)
  5210 			return (BranchItem*)ti;
  5211 	}
  5212 	return NULL;
  5213 }
  5214 
  5215 ImageItem* VymModel::getSelectedImage()
  5216 {
  5217 	QModelIndexList list=selModel->selectedIndexes();
  5218 	if (!list.isEmpty())
  5219 	{
  5220 		TreeItem *ti=getItem (list.first());
  5221 		if (ti && ti->getType()==TreeItem::Image)
  5222 			return (ImageItem*)ti;
  5223 	}
  5224 	return NULL;
  5225 }
  5226 
  5227 AttributeItem* VymModel::getSelectedAttribute()	
  5228 {
  5229 	QModelIndexList list=selModel->selectedIndexes();
  5230 	if (!list.isEmpty() )
  5231 	{
  5232 		TreeItem *ti = getItem (list.first() );
  5233 		TreeItem::Type type=ti->getType();
  5234 		if (type ==TreeItem::Attribute)
  5235 			return (AttributeItem*)ti;
  5236 	} 
  5237 	return NULL;
  5238 }
  5239 
  5240 TreeItem* VymModel::getSelectedItem()	
  5241 {
  5242 	QModelIndexList list=selModel->selectedIndexes();
  5243 	if (!list.isEmpty() )
  5244 		return getItem (list.first() );
  5245 	else	
  5246 		return NULL;
  5247 }
  5248 
  5249 QModelIndex VymModel::getSelectedIndex()
  5250 {
  5251 	QModelIndexList list=selModel->selectedIndexes();
  5252 	if (list.isEmpty() )
  5253 		return QModelIndex();
  5254 	else
  5255 		return list.first();
  5256 }
  5257 
  5258 QString VymModel::getSelectString ()
  5259 {
  5260 	return getSelectString (getSelectedItem());
  5261 }
  5262 
  5263 QString VymModel::getSelectString (LinkableMapObj *lmo)	// only for convenience. Used in MapEditor
  5264 {
  5265 	if (!lmo) return QString();
  5266 	return getSelectString (lmo->getTreeItem() );
  5267 }
  5268 
  5269 QString VymModel::getSelectString (TreeItem *ti) 
  5270 {
  5271 	QString s;
  5272 	if (!ti) return s;
  5273 	switch (ti->getType())
  5274 	{
  5275 		case TreeItem::MapCenter: s="mc:"; break;
  5276 		case TreeItem::Branch: s="bo:";break;
  5277 		case TreeItem::Image: s="fi:";break;
  5278 		case TreeItem::Attribute: s="ai:";break;
  5279 		case TreeItem::XLink: s="xl:";break;
  5280 		default:
  5281 			s="unknown type in VymModel::getSelectString()";
  5282 			break;
  5283 	}
  5284 	s=  s + QString("%1").arg(ti->num());
  5285 	if (ti->depth() >0)
  5286 		// call myself recursively
  5287 		s= getSelectString(ti->parent()) +","+s;
  5288 			
  5289 	return s;
  5290 }
  5291