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