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