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