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