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