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