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