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