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