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