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