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