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