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