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