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