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