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