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