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