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