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