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