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