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