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