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