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