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