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