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