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