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