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