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