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