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