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