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