mapeditor.cpp
author insilmaril
Mon, 07 Jan 2008 14:52:49 +0000
changeset 637 0ff5fc89dc5c
parent 629 92bebad145d2
child 638 3436b8eb3d9b
permissions -rw-r--r--
Included french in vym.pro
     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 	{
  1628 		// Save complete map
  1629 		saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),NULL);
  1630 		mapChanged=false;
  1631 		mapUnsaved=false;
  1632 		autosaveTimer->stop();
  1633 	}
  1634 	else	
  1635 	{
  1636 		// Save part of map
  1637 		if (xelection.type()==Selection::FloatImage)
  1638 			saveFloatImage();
  1639 		else	
  1640 			saveFile=saveToDir (fileDir,mapName+"-",true,QPointF(),xelection.getBranch());	
  1641 		// TODO take care of multiselections
  1642 	}	
  1643 
  1644 	if (!saveStringToDisk(fileDir+fname,saveFile))
  1645 		return 1;
  1646 
  1647 	updateActions();
  1648 	return 0;
  1649 }
  1650 
  1651 void MapEditor::setZipped (bool z)
  1652 {
  1653 	zipped=z;
  1654 }
  1655 
  1656 bool MapEditor::saveZipped ()
  1657 {
  1658 	return zipped;
  1659 }
  1660 
  1661 void MapEditor::print()
  1662 {
  1663 	if ( !printer ) 
  1664 	{
  1665 		printer = new QPrinter;
  1666 		printer->setColorMode (QPrinter::Color);
  1667 		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
  1668 		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
  1669 		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
  1670 	}
  1671 
  1672 	QRectF totalBBox=mapCenter->getTotalBBox();
  1673 
  1674 	// Try to set orientation automagically
  1675 	// Note: Interpretation of generated postscript is amibiguous, if 
  1676 	// there are problems with landscape mode, see
  1677 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
  1678 
  1679 	if (totalBBox.width()>totalBBox.height())
  1680 		// recommend landscape
  1681 		printer->setOrientation (QPrinter::Landscape);
  1682 	else	
  1683 		// recommend portrait
  1684 		printer->setOrientation (QPrinter::Portrait);
  1685 
  1686 	if ( printer->setup(this) ) 
  1687 	// returns false, if printing is canceled
  1688 	{
  1689 		QPainter pp(printer);
  1690 
  1691 		pp.setRenderHint(QPainter::Antialiasing,true);
  1692 
  1693 		// Don't print the visualisation of selection
  1694 		xelection.unselect();
  1695 
  1696 		QRectF mapRect=totalBBox;
  1697 		QGraphicsRectItem *frame=NULL;
  1698 
  1699 		if (printFrame) 
  1700 		{
  1701 			// Print frame around map
  1702 			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
  1703 				totalBBox.width()+20, totalBBox.height()+20);
  1704 			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
  1705 			frame->setZValue(0);
  1706 			frame->show();    
  1707 		}		
  1708 
  1709 
  1710 		double paperAspect = (double)printer->width()   / (double)printer->height();
  1711 		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
  1712 		int viewBottom;
  1713 		if (mapAspect>=paperAspect)
  1714 		{
  1715 			// Fit horizontally to paper width
  1716 			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
  1717 			viewBottom=(int)(printer->width()/mapAspect);	
  1718 		}	else
  1719 		{
  1720 			// Fit vertically to paper height
  1721 			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
  1722 			viewBottom=printer->height();	
  1723 		}	
  1724 		
  1725 		if (printFooter) 
  1726 		{
  1727 			// Print footer below map
  1728 			QFont font;		
  1729 			font.setPointSize(10);
  1730 			pp.setFont (font);
  1731 			QRectF footerBox(0,viewBottom,printer->width(),15);
  1732 			pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
  1733 			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
  1734 		}
  1735 		mapScene->render (
  1736 			&pp, 
  1737 			QRectF (0,0,printer->width(),printer->height()-15),
  1738 			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
  1739 		);
  1740 		
  1741 		// Viewport has paper dimension
  1742 		if (frame)  delete (frame);
  1743 
  1744 		// Restore selection
  1745 		xelection.reselect();
  1746 
  1747 		// Save settings in vymrc
  1748 		settings.writeEntry("/mainwindow/printerName",printer->printerName());
  1749 		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
  1750 		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
  1751 	}
  1752 }
  1753 
  1754 void MapEditor::setAntiAlias (bool b)
  1755 {
  1756 	setRenderHint(QPainter::Antialiasing,b);
  1757 }
  1758 
  1759 void MapEditor::setSmoothPixmap(bool b)
  1760 {
  1761 	setRenderHint(QPainter::SmoothPixmapTransform,b);
  1762 }
  1763 
  1764 QPixmap MapEditor::getPixmap()
  1765 {
  1766 	QRectF mapRect=mapCenter->getTotalBBox();
  1767 	QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
  1768 	QPainter pp (&pix);
  1769 	
  1770 	pp.setRenderHints(renderHints());
  1771 
  1772 	// Don't print the visualisation of selection
  1773 	xelection.unselect();
  1774 
  1775 	mapScene->render (	&pp, 
  1776 		QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
  1777 		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
  1778 
  1779 	// Restore selection
  1780 	xelection.reselect();
  1781 	
  1782 	return pix;
  1783 }
  1784 
  1785 void MapEditor::setHideTmpMode (HideTmpMode mode)
  1786 {
  1787 	hidemode=mode;
  1788 	mapCenter->setHideTmp (hidemode);
  1789 	mapCenter->reposition();
  1790 	scene()->update();
  1791 }
  1792 
  1793 HideTmpMode MapEditor::getHideTmpMode()
  1794 {
  1795 	return hidemode;
  1796 }
  1797 
  1798 void MapEditor::setExportMode (bool b)
  1799 {
  1800 	// should be called before and after exports
  1801 	// depending on the settings
  1802 	if (b && settings.value("/export/useHideExport","true")=="true")
  1803 		setHideTmpMode (HideExport);
  1804 	else	
  1805 		setHideTmpMode (HideNone);
  1806 }
  1807 
  1808 void MapEditor::exportASCII(QString fname,bool askName)
  1809 {
  1810 	ExportASCII ex;
  1811 	ex.setMapCenter(mapCenter);
  1812 	if (fname=="") 
  1813 		ex.setFile (mapName+".txt");	
  1814 	else
  1815 		ex.setFile (fname);
  1816 
  1817 	if (askName)
  1818 	{
  1819 		//ex.addFilter ("TXT (*.txt)");
  1820 		ex.setDir(lastImageDir);
  1821 		//ex.setCaption(vymName+ " -" +tr("Export as ASCII")+" "+tr("(still experimental)"));
  1822 		ex.execDialog() ; 
  1823 	} 
  1824 	if (!ex.canceled())
  1825 	{
  1826 		setExportMode(true);
  1827 		ex.doExport();
  1828 		setExportMode(false);
  1829 	}
  1830 }
  1831 
  1832 void MapEditor::exportImage(QString fname, bool askName, QString format)
  1833 {
  1834 	if (fname=="")
  1835 	{
  1836 		fname=mapName+".png";
  1837 		format="PNG";
  1838 	} 	
  1839 
  1840 	if (askName)
  1841 	{
  1842 		QStringList fl;
  1843 		QFileDialog *fd=new QFileDialog (this);
  1844 		fd->setCaption (tr("Export map as image"));
  1845 		fd->setDirectory (lastImageDir);
  1846 		fd->setFileMode(QFileDialog::AnyFile);
  1847 		fd->setFilters  (imageIO.getFilters() );
  1848 		if (fd->exec())
  1849 		{
  1850 			fl=fd->selectedFiles();
  1851 			fname=fl.first();
  1852 			format=imageIO.getType(fd->selectedFilter());
  1853 		} 
  1854 	}
  1855 
  1856 	setExportMode (true);
  1857 	QPixmap pix (getPixmap());
  1858 	pix.save(fname, format);
  1859 	setExportMode (false);
  1860 }
  1861 
  1862 void MapEditor::exportOOPresentation(const QString &fn, const QString &cf)
  1863 {
  1864 	ExportOO ex;
  1865 	ex.setFile (fn);
  1866 	ex.setMapCenter(mapCenter);
  1867 	if (ex.setConfigFile(cf)) 
  1868 	{
  1869 		setExportMode (true);
  1870 		ex.exportPresentation();
  1871 		setExportMode (false);
  1872 	}
  1873 }
  1874 
  1875 void MapEditor::exportXHTML (const QString &dir, bool askForName)
  1876 {
  1877 			ExportXHTMLDialog dia(this);
  1878 			dia.setFilePath (filePath );
  1879 			dia.setMapName (mapName );
  1880 			dia.readSettings();
  1881 			if (dir!="") dia.setDir (dir);
  1882 
  1883 			bool ok=true;
  1884 			
  1885 			if (askForName)
  1886 			{
  1887 				if (dia.exec()!=QDialog::Accepted) 
  1888 					ok=false;
  1889 				else	
  1890 				{
  1891 					QDir d (dia.getDir());
  1892 					// Check, if warnings should be used before overwriting
  1893 					// the output directory
  1894 					if (d.exists() && d.count()>0)
  1895 					{
  1896 						WarningDialog warn;
  1897 						warn.showCancelButton (true);
  1898 						warn.setText(QString(
  1899 							"The directory %1 is not empty.\n"
  1900 							"Do you risk to overwrite some of its contents?").arg(d.path() ));
  1901 						warn.setCaption("Warning: Directory not empty");
  1902 						warn.setShowAgainName("mainwindow/overwrite-dir-xhtml");
  1903 
  1904 						if (warn.exec()!=QDialog::Accepted) ok=false;
  1905 					}
  1906 				}	
  1907 			}
  1908 
  1909 			if (ok)
  1910 			{
  1911 				exportXML (dia.getDir(),false );
  1912 				dia.doExport(mapName );
  1913 				//if (dia.hasChanged()) setChanged();
  1914 			}
  1915 }
  1916 
  1917 void MapEditor::exportXML(QString dir, bool askForName)
  1918 {
  1919 	if (askForName)
  1920 	{
  1921 		dir=browseDirectory(this,tr("Export XML to directory"));
  1922 		if (dir =="" && !reallyWriteDirectory(dir) )
  1923 		return;
  1924 	}
  1925 
  1926 	// Hide stuff during export, if settings want this
  1927 	setExportMode (true);
  1928 
  1929 	// Create subdirectories
  1930 	makeSubDirs (dir);
  1931 
  1932 	// write to directory
  1933 	QString saveFile=saveToDir (dir,mapName+"-",true,mapCenter->getTotalBBox().topLeft() ,NULL);
  1934 	QFile file;
  1935 
  1936 	file.setName ( dir + "/"+mapName+".xml");
  1937 	if ( !file.open( QIODevice::WriteOnly ) )
  1938 	{
  1939 		// This should neverever happen
  1940 		QMessageBox::critical (0,tr("Critical Export Error"),tr("MapEditor::exportXML couldn't open %1").arg(file.name()));
  1941 		return;
  1942 	}	
  1943 
  1944 	// Write it finally, and write in UTF8, no matter what 
  1945 	QTextStream ts( &file );
  1946 	ts.setEncoding (QTextStream::UnicodeUTF8);
  1947 	ts << saveFile;
  1948 	file.close();
  1949 
  1950 	// Now write image, too
  1951 	exportImage (dir+"/images/"+mapName+".png",false,"PNG");
  1952 
  1953 	setExportMode (false);
  1954 }
  1955 
  1956 void MapEditor::clear()
  1957 {
  1958 	xelection.unselect();
  1959 	mapCenter->clear();
  1960 }
  1961 
  1962 void MapEditor::copy()
  1963 {
  1964 	LinkableMapObj *sel=xelection.single();
  1965 	if (sel)
  1966 	{
  1967 		if (redosAvail == 0)
  1968 		{
  1969 			// Copy to history
  1970 			QString s=sel->getSelectString();
  1971 			saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy selection to clipboard",sel  );
  1972 			curClipboard=curStep;
  1973 		}
  1974 
  1975 		// Copy also to global clipboard, because we are at last step in history
  1976 		QString bakMapName(QString("history-%1").arg(curStep));
  1977 		QString bakMapDir(tmpMapDir +"/"+bakMapName);
  1978 		copyDir (bakMapDir,clipboardDir );
  1979 
  1980 		clipboardEmpty=false;
  1981 		updateActions();
  1982 	}	    
  1983 }
  1984 
  1985 void MapEditor::redo()
  1986 {
  1987 	// Can we undo at all?
  1988 	if (redosAvail<1) return;
  1989 
  1990 	bool blockSaveStateOrg=blockSaveState;
  1991 	blockSaveState=true;
  1992 	
  1993 	redosAvail--;
  1994 
  1995 	if (undosAvail<stepsTotal) undosAvail++;
  1996 	curStep++;
  1997 	if (curStep>stepsTotal) curStep=1;
  1998 	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
  1999 	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
  2000 	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
  2001 	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
  2002 	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
  2003 	QString version=undoSet.readEntry ("/history/version");
  2004 
  2005 	if (!checkVersion(version))
  2006 		QMessageBox::warning(0,tr("Warning"),
  2007 			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
  2008 
  2009 
  2010 	// Find out current undo directory
  2011 	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
  2012 
  2013 	if (debug)
  2014 	{
  2015 		cout << "ME::redo() begin\n";
  2016 		cout << "    undosAvail="<<undosAvail<<endl;
  2017 		cout << "    redosAvail="<<redosAvail<<endl;
  2018 		cout << "       curStep="<<curStep<<endl;
  2019 		cout << "    ---------------------------"<<endl;
  2020 		cout << "    comment="<<comment.toStdString()<<endl;
  2021 		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
  2022 		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
  2023 		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
  2024 		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
  2025 		cout << "    ---------------------------"<<endl<<endl;
  2026 	}
  2027 
  2028 	// select  object before redo
  2029 	if (!redoSelection.isEmpty())
  2030 		select (redoSelection);
  2031 
  2032 
  2033 	parseAtom (redoCommand);
  2034 	mapCenter->reposition();
  2035 
  2036 	blockSaveState=blockSaveStateOrg;
  2037 
  2038 	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
  2039 	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
  2040 	undoSet.setEntry ("/history/curStep",QString::number(curStep));
  2041 	undoSet.writeSettings(histPath);
  2042 
  2043 	mainWindow->updateHistory (undoSet);
  2044 	updateActions();
  2045 
  2046 	/* TODO remove testing
  2047 	cout << "ME::redo() end\n";
  2048 	cout << "    undosAvail="<<undosAvail<<endl;
  2049 	cout << "    redosAvail="<<redosAvail<<endl;
  2050 	cout << "       curStep="<<curStep<<endl;
  2051 	cout << "    ---------------------------"<<endl<<endl;
  2052 	*/
  2053 
  2054 
  2055 }
  2056 
  2057 bool MapEditor::isRedoAvailable()
  2058 {
  2059 	if (undoSet.readNumEntry("/history/redosAvail",0)>0)
  2060 		return true;
  2061 	else	
  2062 		return false;
  2063 }
  2064 
  2065 void MapEditor::undo()
  2066 {
  2067 	// Can we undo at all?
  2068 	if (undosAvail<1) return;
  2069 
  2070 	mainWindow->statusMessage (tr("Autosave disabled during undo."));
  2071 
  2072 	bool blockSaveStateOrg=blockSaveState;
  2073 	blockSaveState=true;
  2074 	
  2075 	QString undoCommand=  undoSet.readEntry (QString("/history/step-%1/undoCommand").arg(curStep));
  2076 	QString undoSelection=undoSet.readEntry (QString("/history/step-%1/undoSelection").arg(curStep));
  2077 	QString redoCommand=  undoSet.readEntry (QString("/history/step-%1/redoCommand").arg(curStep));
  2078 	QString redoSelection=undoSet.readEntry (QString("/history/step-%1/redoSelection").arg(curStep));
  2079 	QString comment=undoSet.readEntry (QString("/history/step-%1/comment").arg(curStep));
  2080 	QString version=undoSet.readEntry ("/history/version");
  2081 
  2082 	if (!checkVersion(version))
  2083 		QMessageBox::warning(0,tr("Warning"),
  2084 			tr("Version %1 of saved undo/redo data\ndoes not match current vym version %2.").arg(version).arg(vymVersion));
  2085 
  2086 	// Find out current undo directory
  2087 	QString bakMapDir(QString(tmpMapDir+"/undo-%1").arg(curStep));
  2088 
  2089 	// select  object before undo
  2090 	if (!undoSelection.isEmpty())
  2091 		select (undoSelection);
  2092 
  2093 	if (debug)
  2094 	{
  2095 		cout << "ME::undo() begin\n";
  2096 		cout << "    undosAvail="<<undosAvail<<endl;
  2097 		cout << "    redosAvail="<<redosAvail<<endl;
  2098 		cout << "       curStep="<<curStep<<endl;
  2099 		cout << "    ---------------------------"<<endl;
  2100 		cout << "    comment="<<comment.toStdString()<<endl;
  2101 		cout << "    undoCom="<<undoCommand.toStdString()<<endl;
  2102 		cout << "    undoSel="<<undoSelection.toStdString()<<endl;
  2103 		cout << "    redoCom="<<redoCommand.toStdString()<<endl;
  2104 		cout << "    redoSel="<<redoSelection.toStdString()<<endl;
  2105 		cout << "    ---------------------------"<<endl<<endl;
  2106 	}	
  2107 	parseAtom (undoCommand);
  2108 	mapCenter->reposition();
  2109 
  2110 	undosAvail--;
  2111 	curStep--; 
  2112 	if (curStep<1) curStep=stepsTotal;
  2113 
  2114 	redosAvail++;
  2115 
  2116 	blockSaveState=blockSaveStateOrg;
  2117 /* TODO remove testing
  2118 	cout << "ME::undo() end\n";
  2119 	cout << "    undosAvail="<<undosAvail<<endl;
  2120 	cout << "    redosAvail="<<redosAvail<<endl;
  2121 	cout << "       curStep="<<curStep<<endl;
  2122 	cout << "    ---------------------------"<<endl<<endl;
  2123 */
  2124 
  2125 	undoSet.setEntry ("/history/undosAvail",QString::number(undosAvail));
  2126 	undoSet.setEntry ("/history/redosAvail",QString::number(redosAvail));
  2127 	undoSet.setEntry ("/history/curStep",QString::number(curStep));
  2128 	undoSet.writeSettings(histPath);
  2129 
  2130 	mainWindow->updateHistory (undoSet);
  2131 	updateActions();
  2132 	xelection.update();
  2133 	ensureSelectionVisible();
  2134 }
  2135 
  2136 bool MapEditor::isUndoAvailable()
  2137 {
  2138 	if (undoSet.readNumEntry("/history/undosAvail",0)>0)
  2139 		return true;
  2140 	else	
  2141 		return false;
  2142 }
  2143 
  2144 void MapEditor::gotoHistoryStep (int i)
  2145 {
  2146 	// Restore variables
  2147 	int undosAvail=undoSet.readNumEntry (QString("/history/undosAvail"));
  2148 	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
  2149 
  2150 	if (i<0) i=undosAvail+redosAvail;
  2151 
  2152 	// Clicking above current step makes us undo things
  2153 	if (i<undosAvail) 
  2154 	{	
  2155 		for (int j=0; j<undosAvail-i; j++) undo();
  2156 		return;
  2157 	}	
  2158 	// Clicking below current step makes us redo things
  2159 	if (i>undosAvail) 
  2160 		for (int j=undosAvail; j<i; j++) 
  2161 		{
  2162 		cout << "redo "<<j<<"/"<<undosAvail<<" i="<<i<<endl;
  2163 		redo();
  2164 		}
  2165 
  2166 	// And ignore clicking the current row ;-)	
  2167 }
  2168 
  2169 void MapEditor::addMapReplaceInt(const QString &undoSel, const QString &path)
  2170 {
  2171 	QString pathDir=path.left(path.findRev("/"));
  2172 	QDir d(pathDir);
  2173 	QFile file (path);
  2174 
  2175 	if (d.exists() )
  2176 	{
  2177 		// We need to parse saved XML data
  2178 		parseVYMHandler handler;
  2179 		QXmlInputSource source( file);
  2180 		QXmlSimpleReader reader;
  2181 		reader.setContentHandler( &handler );
  2182 		reader.setErrorHandler( &handler );
  2183 		handler.setMapEditor( this );
  2184 		handler.setTmpDir ( pathDir );	// needed to load files with rel. path
  2185 		if (undoSel.isEmpty())
  2186 		{
  2187 			unselect();
  2188 			mapCenter->clear();
  2189 			handler.setLoadMode (NewMap);
  2190 		} else	
  2191 		{
  2192 			select (undoSel);
  2193 			handler.setLoadMode (ImportReplace);
  2194 		}	
  2195 		blockReposition=true;
  2196 		bool ok = reader.parse( source );
  2197 		blockReposition=false;
  2198 		if (! ok ) 
  2199 		{	
  2200 			// This should never ever happen
  2201 			QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
  2202 								    handler.errorProtocol());
  2203 		}
  2204 	} else	
  2205 		QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
  2206 }
  2207 
  2208 void MapEditor::addMapInsertInt (const QString &path, int pos)
  2209 {
  2210 	BranchObj *sel=xelection.getBranch();
  2211 	if (sel)
  2212 	{
  2213 		QString pathDir=path.left(path.findRev("/"));
  2214 		QDir d(pathDir);
  2215 		QFile file (path);
  2216 
  2217 		if (d.exists() )
  2218 		{
  2219 			// We need to parse saved XML data
  2220 			parseVYMHandler handler;
  2221 			QXmlInputSource source( file);
  2222 			QXmlSimpleReader reader;
  2223 			reader.setContentHandler( &handler );
  2224 			reader.setErrorHandler( &handler );
  2225 			handler.setMapEditor( this );
  2226 			handler.setTmpDir ( pathDir );	// needed to load files with rel. path
  2227 			handler.setLoadMode (ImportAdd);
  2228 			blockReposition=true;
  2229 			bool ok = reader.parse( source );
  2230 			blockReposition=false;
  2231 			if (! ok ) 
  2232 			{	
  2233 				// This should never ever happen
  2234 				QMessageBox::critical( 0, tr( "Critical Parse Error while reading %1").arg(path),
  2235 										handler.errorProtocol());
  2236 			}
  2237 			if (sel!=mapCenter)
  2238 				sel->getLastBranch()->linkTo (sel,pos);
  2239 		} else	
  2240 			QMessageBox::critical( 0, tr( "Critical Error" ), tr("Could not read %1").arg(path));
  2241 	}		
  2242 }
  2243 
  2244 void MapEditor::pasteNoSave(const int &n)
  2245 {
  2246 	bool old=blockSaveState;
  2247 	blockSaveState=true;
  2248 	if (redosAvail > 0 || n!=0)
  2249 	{
  2250 		// Use the "historical" buffer
  2251 		QString bakMapName(QString("history-%1").arg(n));
  2252 		QString bakMapDir(tmpMapDir +"/"+bakMapName);
  2253 		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
  2254 	} else
  2255 		// Use the global buffer
  2256 		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
  2257 	blockSaveState=old;
  2258 }
  2259 
  2260 void MapEditor::paste()		
  2261 {   
  2262 	BranchObj *sel=xelection.getBranch();
  2263 	if (sel)
  2264 	{
  2265 		saveStateChangingPart(
  2266 			sel,
  2267 			sel,
  2268 			QString ("paste (%1)").arg(curClipboard),
  2269 			QString("Paste to %1").arg( getName(sel))
  2270 		);
  2271 		pasteNoSave(0);
  2272 		mapCenter->reposition();
  2273 	}
  2274 }
  2275 
  2276 void MapEditor::cut()
  2277 {
  2278 	LinkableMapObj *sel=xelection.single();
  2279 	if ( sel && (xelection.type() == Selection::Branch ||
  2280 		xelection.type()==Selection::MapCenter ||
  2281 		xelection.type()==Selection::FloatImage))
  2282 	{
  2283 	/* No savestate! savestate is called in cutNoSave
  2284 		saveStateChangingPart(
  2285 			sel->getParObj(),
  2286 			sel,
  2287 			"cut ()",
  2288 			QString("Cut %1").arg(getName(sel ))
  2289 		);
  2290 	*/	
  2291 		copy();
  2292 		deleteSelection();
  2293 		mapCenter->reposition();
  2294 	}
  2295 }
  2296 
  2297 void MapEditor::move(const double &x, const double &y)
  2298 {
  2299 	LinkableMapObj *sel=xelection.single();
  2300 	if (sel)
  2301 	{
  2302         QPointF ap(sel->getAbsPos());
  2303         QPointF to(x, y);
  2304         if (ap != to)
  2305         {
  2306             QString ps=qpointfToString(ap);
  2307             QString s=xelection.single()->getSelectString();
  2308             saveState(
  2309                 s, "move "+ps, 
  2310                 s, "move "+qpointfToString(to), 
  2311                 QString("Move %1 to %2").arg(getName(sel)).arg(ps));
  2312             sel->move(x,y);
  2313             mapCenter->reposition();
  2314             xelection.update();
  2315         }
  2316 	}
  2317 }
  2318 
  2319 void MapEditor::moveRel (const double &x, const double &y)
  2320 {
  2321 	LinkableMapObj *sel=xelection.single();
  2322 	if (sel)
  2323 	{
  2324         QPointF rp(sel->getRelPos());
  2325         QPointF to(x, y);
  2326         if (rp != to)
  2327         {
  2328             QString ps=qpointfToString (sel->getRelPos());
  2329             QString s=sel->getSelectString();
  2330             saveState(
  2331                 s, "moveRel "+ps, 
  2332                 s, "moveRel "+qpointfToString(to), 
  2333                 QString("Move %1 to relative position %2").arg(getName(sel)).arg(ps));
  2334             ((OrnamentedObj*)sel)->move2RelPos (x,y);
  2335             mapCenter->reposition();
  2336             sel->updateLink();
  2337             xelection.update();
  2338         }
  2339 	}
  2340 }
  2341 
  2342 void MapEditor::moveBranchUp()
  2343 {
  2344 	BranchObj* bo=xelection.getBranch();
  2345 	BranchObj* par;
  2346 	if (bo)
  2347 	{
  2348 		if (!bo->canMoveBranchUp()) return;
  2349 		par=(BranchObj*)(bo->getParObj());
  2350 		BranchObj *obo=par->moveBranchUp (bo);	// bo will be the one below selection
  2351 		saveState (bo->getSelectString(),"moveBranchDown ()",obo->getSelectString(),"moveBranchUp ()",QString("Move up %1").arg(getName(bo)));
  2352 		mapCenter->reposition();
  2353 		scene()->update();
  2354 		xelection.update();
  2355 		ensureSelectionVisible();
  2356 	}
  2357 }
  2358 
  2359 void MapEditor::moveBranchDown()
  2360 {
  2361 	BranchObj* bo=xelection.getBranch();
  2362 	BranchObj* par;
  2363 	if (bo)
  2364 	{
  2365 		if (!bo->canMoveBranchDown()) return;
  2366 		par=(BranchObj*)(bo->getParObj());
  2367 		BranchObj *obo=par->moveBranchDown(bo);	// bo will be the one above selection
  2368 		saveState(bo->getSelectString(),"moveBranchUp ()",obo->getSelectString(),"moveBranchDown ()",QString("Move down %1").arg(getName(bo)));
  2369 		mapCenter->reposition();
  2370 		scene()->update();
  2371 		xelection.update();
  2372 		ensureSelectionVisible();
  2373 	}	
  2374 }
  2375 
  2376 void MapEditor::sortChildren()
  2377 {
  2378 	BranchObj* bo=xelection.getBranch();
  2379 	if (bo)
  2380 	{
  2381 		if(bo->countBranches()>1)
  2382 		{
  2383 			saveStateChangingPart(bo,bo, "sortChildren ()",QString("Sort children of %1").arg(getName(bo)));
  2384 			bo->sortChildren();
  2385 			mapCenter->reposition();
  2386 			ensureSelectionVisible();
  2387 		}
  2388 	}
  2389 }
  2390 
  2391 void MapEditor::linkTo(const QString &dstString)	
  2392 {
  2393 	FloatImageObj *fio=xelection.getFloatImage();
  2394 	if (fio)
  2395 	{
  2396 		BranchObj *dst=(BranchObj*)(mapCenter->findObjBySelect(dstString));
  2397 		if (dst && (typeid(*dst)==typeid (BranchObj) || 
  2398 					typeid(*dst)==typeid (MapCenterObj)))
  2399 		{			
  2400 			LinkableMapObj *dstPar=dst->getParObj();
  2401 			QString parString=dstPar->getSelectString();
  2402 			QString fioPreSelectString=fio->getSelectString();
  2403 			QString fioPreParentSelectString=fio->getParObj()->getSelectString();
  2404 			((BranchObj*)(dst))->addFloatImage (fio);
  2405 			xelection.unselect();
  2406 			((BranchObj*)(fio->getParObj()))->removeFloatImage (fio);
  2407 			fio=((BranchObj*)(dst))->getLastFloatImage();
  2408 			fio->setRelPos();
  2409 			fio->reposition();
  2410 			xelection.select(fio);
  2411 			saveState(
  2412 				fio->getSelectString(),
  2413 				QString("linkTo (\"%1\")").arg(fioPreParentSelectString), 
  2414 				fioPreSelectString, 
  2415 				QString ("linkTo (\"%1\")").arg(dstString),
  2416 				QString ("Link floatimage to %1").arg(getName(dst)));
  2417 		}
  2418 	}
  2419 }
  2420 
  2421 QString MapEditor::getHeading(bool &ok, QPoint &p)
  2422 {
  2423 	BranchObj *bo=xelection.getBranch();
  2424 	if (bo)
  2425 	{
  2426 		ok=true;
  2427 		p=mapFromScene(bo->getAbsPos());
  2428 		return bo->getHeading();
  2429 	}
  2430 	ok=false;
  2431 	return QString();
  2432 }
  2433 
  2434 void MapEditor::setHeading(const QString &s)
  2435 {
  2436 	BranchObj *sel=xelection.getBranch();
  2437 	if (sel)
  2438 	{
  2439 		saveState(
  2440 			sel,
  2441 			"setHeading (\""+sel->getHeading()+"\")", 
  2442 			sel,
  2443 			"setHeading (\""+s+"\")", 
  2444 			QString("Set heading of %1 to \"%2\"").arg(getName(sel)).arg(s) );
  2445 		sel->setHeading(s );
  2446 		mapCenter->reposition();
  2447 		xelection.update();
  2448 		ensureSelectionVisible();
  2449 	}
  2450 }
  2451 
  2452 void MapEditor::setHeadingInt(const QString &s)
  2453 {
  2454 	BranchObj *bo=xelection.getBranch();
  2455 	if (bo)
  2456 	{
  2457 		bo->setHeading(s);
  2458 		mapCenter->reposition();
  2459 		xelection.update();
  2460 		ensureSelectionVisible();
  2461 	}
  2462 }
  2463 
  2464 void MapEditor::setVymLinkInt (const QString &s)
  2465 {
  2466 	// Internal function, no saveState needed
  2467 	BranchObj *bo=xelection.getBranch();
  2468 	if (bo)
  2469 	{
  2470 		bo->setVymLink(s);
  2471 		mapCenter->reposition();
  2472 		updateActions();
  2473 		xelection.update();
  2474 		ensureSelectionVisible();
  2475 	}
  2476 }
  2477 
  2478 BranchObj* MapEditor::addNewBranchInt(int num)
  2479 {
  2480 	// Depending on pos:
  2481 	// -3		insert in childs of parent  above selection 
  2482 	// -2		add branch to selection 
  2483 	// -1		insert in childs of parent below selection 
  2484 	// 0..n		insert in childs of parent at pos
  2485 	BranchObj *newbo=NULL;
  2486 	BranchObj *bo=xelection.getBranch();
  2487 	if (bo)
  2488 	{
  2489 		if (num==-2)
  2490 		{
  2491 			// save scroll state. If scrolled, automatically select
  2492 			// new branch in order to tmp unscroll parent...
  2493 			newbo=bo->addBranch();
  2494 			
  2495 		}else if (num==-1)
  2496 		{
  2497 			num=bo->getNum()+1;
  2498 			bo=(BranchObj*)bo->getParObj();
  2499 			if (bo) newbo=bo->insertBranch(num);
  2500 		}else if (num==-3)
  2501 		{
  2502 			num=bo->getNum();
  2503 			bo=(BranchObj*)bo->getParObj();
  2504 			if (bo) newbo=bo->insertBranch(num);
  2505 		}
  2506 		if (!newbo) return NULL;
  2507 	}	
  2508 	return newbo;
  2509 }	
  2510 
  2511 BranchObj* MapEditor::addNewBranch(int pos)
  2512 {
  2513 	// Different meaning than num in addNewBranchInt!
  2514 	// -1	add above
  2515 	//  0	add as child
  2516 	// +1	add below
  2517 	BranchObj *bo = xelection.getBranch();
  2518 	BranchObj *newbo=NULL;
  2519 
  2520 	if (bo)
  2521 	{
  2522 		setCursor (Qt::ArrowCursor);
  2523 
  2524 		newbo=addNewBranchInt (pos-2);
  2525 
  2526 		if (newbo)
  2527 		{
  2528 			saveState(
  2529 				newbo,		
  2530 				"delete ()",
  2531 				bo,
  2532 				QString ("addBranch (%1)").arg(pos),
  2533 				QString ("Add new branch to %1").arg(getName(bo)));	
  2534 
  2535 			mapCenter->reposition();
  2536 			xelection.update();
  2537 			latestSelection=newbo->getSelectString();
  2538 			// In Network mode, the client needs to know where the new branch is,
  2539 			// so we have to pass on this information via saveState.
  2540 			// TODO: Get rid of this positioning workaround
  2541 			QString ps=qpointfToString (newbo->getAbsPos());
  2542 			sendData ("selectLatestAdded ()");
  2543 			sendData (QString("move %1").arg(ps));
  2544 			sendSelection();
  2545 		}
  2546 	}	
  2547 	return newbo;
  2548 }
  2549 
  2550 
  2551 BranchObj* MapEditor::addNewBranchBefore()
  2552 {
  2553 	BranchObj *newbo=NULL;
  2554 	BranchObj *bo = xelection.getBranch();
  2555 	if (bo && xelection.type()==Selection::Branch)
  2556 		 // We accept no MapCenterObj here, so we _have_ a parent
  2557 	{
  2558 		QPointF p=bo->getRelPos();
  2559 
  2560 
  2561 		BranchObj *parbo=(BranchObj*)(bo->getParObj());
  2562 
  2563 		// add below selection
  2564 		newbo=parbo->insertBranch(bo->getNum()+1);
  2565 		if (newbo)
  2566 		{
  2567 			newbo->move2RelPos (p);
  2568 
  2569 			// Move selection to new branch
  2570 			bo->linkTo (newbo,-1);
  2571 
  2572 			saveState (newbo, "deleteKeepChilds ()", newbo, "addBranchBefore ()", 
  2573 				QString ("Add branch before %1").arg(getName(bo)));
  2574 
  2575 			mapCenter->reposition();
  2576 			xelection.update();
  2577 		}
  2578 	}	
  2579 	latestSelection=xelection.getSelectString();
  2580 	return newbo;
  2581 }
  2582 
  2583 void MapEditor::deleteSelection()
  2584 {
  2585 	BranchObj *bo = xelection.getBranch();
  2586 	if (bo && xelection.type()==Selection::Branch)
  2587 	{
  2588 		BranchObj* par=(BranchObj*)(bo->getParObj());
  2589 		xelection.unselect();
  2590 		saveStateRemovingPart (bo, QString ("Delete %1").arg(getName(bo)));
  2591 		par->removeBranch(bo);
  2592 		xelection.select (par);
  2593 		ensureSelectionVisible();
  2594 		mapCenter->reposition();
  2595 		xelection.update();
  2596 		xelection.update();
  2597 		return;
  2598 	}
  2599 	FloatImageObj *fio=xelection.getFloatImage();
  2600 	if (fio)
  2601 	{
  2602 		BranchObj* par=(BranchObj*)(fio->getParObj());
  2603 		saveStateChangingPart(
  2604 			par, 
  2605 			fio,
  2606 			"delete ()",
  2607 			QString("Delete %1").arg(getName(fio))
  2608 		);
  2609 		xelection.unselect();
  2610 		par->removeFloatImage(fio);
  2611 		xelection.select (par);
  2612 		mapCenter->reposition();
  2613 		xelection.update();
  2614 		ensureSelectionVisible();
  2615 		return;
  2616 	}
  2617 }
  2618 
  2619 LinkableMapObj* MapEditor::getSelection()
  2620 {
  2621 	return xelection.single();
  2622 }
  2623 
  2624 BranchObj* MapEditor::getSelectedBranch()
  2625 {
  2626 	return xelection.getBranch();
  2627 }
  2628 
  2629 FloatImageObj* MapEditor::getSelectedFloatImage()
  2630 {
  2631 	return xelection.getFloatImage();
  2632 }
  2633 
  2634 void MapEditor::unselect()
  2635 {
  2636 	xelection.unselect();
  2637 }	
  2638 
  2639 void MapEditor::reselect()
  2640 {
  2641 	xelection.reselect();
  2642 }	
  2643 
  2644 bool MapEditor::select (const QString &s)
  2645 {
  2646 	LinkableMapObj *lmo=mapCenter->findObjBySelect(s);
  2647 
  2648 	// Finally select the found object
  2649 	if (lmo)
  2650 	{
  2651 		xelection.unselect();
  2652 		xelection.select(lmo);
  2653 		xelection.update();
  2654 		ensureSelectionVisible();
  2655 		sendSelection ();
  2656 		return true;
  2657 	} 
  2658 	return false;
  2659 }
  2660 
  2661 QString MapEditor::getSelectString()
  2662 {
  2663 	return xelection.getSelectString();
  2664 }
  2665 
  2666 void MapEditor::selectInt (LinkableMapObj *lmo)
  2667 {
  2668 	if (lmo && xelection.single()!= lmo && isSelectBlocked()==false )
  2669 	{
  2670 		xelection.select(lmo);
  2671 		xelection.update();
  2672 		sendSelection ();
  2673 	}	
  2674 }
  2675 
  2676 void MapEditor::selectNextBranchInt()
  2677 {
  2678 	// Increase number of branch
  2679 	LinkableMapObj *sel=xelection.single();
  2680 	if (sel)
  2681 	{
  2682 		QString s=sel->getSelectString();
  2683 		QString part;
  2684 		QString typ;
  2685 		QString num;
  2686 
  2687 		// Where am I? 
  2688 		part=s.section(",",-1);
  2689 		typ=part.left (3);
  2690 		num=part.right(part.length() - 3);
  2691 
  2692 		s=s.left (s.length() -num.length());
  2693 
  2694 		// Go to next lmo
  2695 		num=QString ("%1").arg(num.toUInt()+1);
  2696 
  2697 		s=s+num;
  2698 		
  2699 		// Try to select this one
  2700 		if (select (s)) return;
  2701 
  2702 		// We have no direct successor, 
  2703 		// try to increase the parental number in order to
  2704 		// find a successor with same depth
  2705 
  2706 		int d=xelection.single()->getDepth();
  2707 		int oldDepth=d;
  2708 		int i;
  2709 		bool found=false;
  2710 		bool b;
  2711 		while (!found && d>0)
  2712 		{
  2713 			s=s.section (",",0,d-1);
  2714 			// replace substring of current depth in s with "1"
  2715 			part=s.section(",",-1);
  2716 			typ=part.left (3);
  2717 			num=part.right(part.length() - 3);
  2718 
  2719 			if (d>1)
  2720 			{	
  2721 				// increase number of parent
  2722 				num=QString ("%1").arg(num.toUInt()+1);
  2723 				s=s.section (",",0,d-2) + ","+ typ+num;
  2724 			} else
  2725 			{
  2726 				// Special case, look at orientation
  2727 				if (xelection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
  2728 					num=QString ("%1").arg(num.toUInt()+1);
  2729 				else	
  2730 					num=QString ("%1").arg(num.toUInt()-1);
  2731 				s=typ+num;
  2732 			}	
  2733 
  2734 			if (select (s))
  2735 				// pad to oldDepth, select the first branch for each depth
  2736 				for (i=d;i<oldDepth;i++)
  2737 				{
  2738 					b=select (s);
  2739 					if (b)
  2740 					{	
  2741 						if ( xelection.getBranch()->countBranches()>0)
  2742 							s+=",bo:0";
  2743 						else	
  2744 							break;
  2745 					} else
  2746 						break;
  2747 				}	
  2748 
  2749 			// try to select the freshly built string
  2750 			found=select(s);
  2751 			d--;
  2752 		}
  2753 		return;
  2754 	}	
  2755 }
  2756 
  2757 void MapEditor::selectPrevBranchInt()
  2758 {
  2759 	// Decrease number of branch
  2760 	BranchObj *bo=xelection.getBranch();
  2761 	if (bo)
  2762 	{
  2763 		QString s=bo->getSelectString();
  2764 		QString part;
  2765 		QString typ;
  2766 		QString num;
  2767 
  2768 		// Where am I? 
  2769 		part=s.section(",",-1);
  2770 		typ=part.left (3);
  2771 		num=part.right(part.length() - 3);
  2772 
  2773 		s=s.left (s.length() -num.length());
  2774 
  2775 		int n=num.toInt()-1;
  2776 		
  2777 		// Go to next lmo
  2778 		num=QString ("%1").arg(n);
  2779 		s=s+num;
  2780 		
  2781 		// Try to select this one
  2782 		if (n>=0 && select (s)) return;
  2783 
  2784 		// We have no direct precessor, 
  2785 		// try to decrease the parental number in order to
  2786 		// find a precessor with same depth
  2787 
  2788 		int d=xelection.single()->getDepth();
  2789 		int oldDepth=d;
  2790 		int i;
  2791 		bool found=false;
  2792 		bool b;
  2793 		while (!found && d>0)
  2794 		{
  2795 			s=s.section (",",0,d-1);
  2796 			// replace substring of current depth in s with "1"
  2797 			part=s.section(",",-1);
  2798 			typ=part.left (3);
  2799 			num=part.right(part.length() - 3);
  2800 
  2801 			if (d>1)
  2802 			{
  2803 				// decrease number of parent
  2804 				num=QString ("%1").arg(num.toInt()-1);
  2805 				s=s.section (",",0,d-2) + ","+ typ+num;
  2806 			} else
  2807 			{
  2808 				// Special case, look at orientation
  2809 				if (xelection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
  2810 					num=QString ("%1").arg(num.toInt()-1);
  2811 				else	
  2812 					num=QString ("%1").arg(num.toInt()+1);
  2813 				s=typ+num;
  2814 			}	
  2815 
  2816 			if (select(s))
  2817 				// pad to oldDepth, select the last branch for each depth
  2818 				for (i=d;i<oldDepth;i++)
  2819 				{
  2820 					b=select (s);
  2821 					if (b)
  2822 						if ( xelection.getBranch()->countBranches()>0)
  2823 							s+=",bo:"+ QString ("%1").arg( xelection.getBranch()->countBranches()-1 );
  2824 						else	
  2825 							break;
  2826 					else
  2827 						break;
  2828 				}	
  2829 			
  2830 			// try to select the freshly built string
  2831 			found=select(s);
  2832 			d--;
  2833 		}
  2834 		return;
  2835 	}	
  2836 }
  2837 
  2838 void MapEditor::selectUpperBranch()
  2839 {
  2840 	if (isSelectBlocked() ) return;
  2841 
  2842 	BranchObj *bo=xelection.getBranch();
  2843 	if (bo && xelection.type()==Selection::Branch)
  2844 	{
  2845 		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
  2846 			selectPrevBranchInt();
  2847 		else
  2848 			if (bo->getDepth()==1)
  2849 				selectNextBranchInt();
  2850 			else
  2851 				selectPrevBranchInt();
  2852 	}
  2853 }
  2854 
  2855 void MapEditor::selectLowerBranch()
  2856 {
  2857 	if (isSelectBlocked() ) return;
  2858 
  2859 	BranchObj *bo=xelection.getBranch();
  2860 	if (bo && xelection.type()==Selection::Branch)
  2861 		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
  2862 			selectNextBranchInt();
  2863 		else
  2864 			if (bo->getDepth()==1)
  2865 				selectPrevBranchInt();
  2866 			else
  2867 				selectNextBranchInt();
  2868 }
  2869 
  2870 
  2871 void MapEditor::selectLeftBranch()
  2872 {
  2873 	if (isSelectBlocked() ) return;
  2874 
  2875 	BranchObj* bo;
  2876 	BranchObj* par;
  2877 	LinkableMapObj *sel=xelection.single();
  2878 	if (sel)
  2879 	{
  2880 		if (xelection.type()== Selection::MapCenter)
  2881 		{
  2882 			par=xelection.getBranch();
  2883 			bo=par->getLastSelectedBranch();
  2884 			if (bo)
  2885 			{
  2886 				// Workaround for reselecting on left and right side
  2887 				if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
  2888 					bo=par->getLastBranch();
  2889 				if (bo)
  2890 				{
  2891 					bo=par->getLastBranch();
  2892 					xelection.select(bo);
  2893 					xelection.update();
  2894 					ensureSelectionVisible();
  2895 					sendSelection();
  2896 				}
  2897 			}	
  2898 		} else
  2899 		{
  2900 			par=(BranchObj*)(sel->getParObj());
  2901 			if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
  2902 			{
  2903 				if (xelection.type() == Selection::Branch ||
  2904 					xelection.type() == Selection::FloatImage)
  2905 				{
  2906 					xelection.select(par);
  2907 					xelection.update();
  2908 					ensureSelectionVisible();
  2909 					sendSelection();
  2910 				}
  2911 			} else
  2912 			{
  2913 				if (xelection.type() == Selection::Branch )
  2914 				{
  2915 					bo=xelection.getBranch()->getLastSelectedBranch();
  2916 					if (bo) 
  2917 					{
  2918 						xelection.select(bo);
  2919 						xelection.update();
  2920 						ensureSelectionVisible();
  2921 					sendSelection();
  2922 					}
  2923 				}
  2924 			}
  2925 		}	
  2926 	}
  2927 }
  2928 
  2929 void MapEditor::selectRightBranch()
  2930 {
  2931 	if (isSelectBlocked() ) return;
  2932 
  2933 	BranchObj* bo;
  2934 	BranchObj* par;
  2935 	LinkableMapObj *sel=xelection.single();
  2936 	if (sel)
  2937 	{
  2938 		if (xelection.type()==Selection::MapCenter) 
  2939 		{
  2940 			par=xelection.getBranch();
  2941 			bo=par->getLastSelectedBranch();
  2942 			if (bo)
  2943 			{
  2944 				// Workaround for reselecting on left and right side
  2945 				if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
  2946 					bo=par->getFirstBranch();
  2947 				if (bo)
  2948 				{
  2949 					xelection.select(bo);
  2950 					xelection.update();
  2951 					ensureSelectionVisible();
  2952 					sendSelection();
  2953 				}
  2954 			}
  2955 		} else
  2956 		{
  2957 			par=(BranchObj*)(xelection.single()->getParObj());
  2958 			if (xelection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
  2959 			{
  2960 				if (xelection.type() == Selection::Branch ||
  2961 					xelection.type() == Selection::FloatImage)
  2962 				{
  2963 					xelection.select(par);
  2964 					xelection.update();
  2965 					ensureSelectionVisible();
  2966 					sendSelection();
  2967 				}
  2968 			} else
  2969 			{
  2970 				if (xelection.type()  == Selection::Branch) 
  2971 				{
  2972 					bo=xelection.getBranch()->getLastSelectedBranch();
  2973 					if (bo) 
  2974 					{
  2975 						xelection.select(bo);
  2976 						xelection.update();
  2977 						ensureSelectionVisible();
  2978 					sendSelection();
  2979 					}
  2980 				}
  2981 			}
  2982 		}
  2983 	}
  2984 }
  2985 
  2986 void MapEditor::selectFirstBranch()
  2987 {
  2988 	BranchObj *bo1=xelection.getBranch();
  2989 	BranchObj *bo2;
  2990 	BranchObj* par;
  2991 	if (bo1)
  2992 	{
  2993 		par=(BranchObj*)(bo1->getParObj());
  2994 		bo2=par->getFirstBranch();
  2995 		if (bo2) {
  2996 			xelection.select(bo2);
  2997 			xelection.update();
  2998 			ensureSelectionVisible();
  2999 			sendSelection();
  3000 		}
  3001 	}		
  3002 }
  3003 
  3004 void MapEditor::selectLastBranch()
  3005 {
  3006 	BranchObj *bo1=xelection.getBranch();
  3007 	BranchObj *bo2;
  3008 	BranchObj* par;
  3009 	if (bo1)
  3010 	{
  3011 		par=(BranchObj*)(bo1->getParObj());
  3012 		bo2=par->getLastBranch();
  3013 		if (bo2) 
  3014 		{
  3015 			xelection.select(bo2);
  3016 			xelection.update();
  3017 			ensureSelectionVisible();
  3018 			sendSelection();
  3019 		}
  3020 	}		
  3021 }
  3022 
  3023 void MapEditor::selectMapBackgroundImage ()
  3024 {
  3025 	Q3FileDialog *fd=new Q3FileDialog( this);
  3026 	fd->setMode (Q3FileDialog::ExistingFile);
  3027 	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  3028 	ImagePreview *p =new ImagePreview (fd);
  3029 	fd->setContentsPreviewEnabled( TRUE );
  3030 	fd->setContentsPreview( p, p );
  3031 	fd->setPreviewMode( Q3FileDialog::Contents );
  3032 	fd->setCaption(vymName+" - " +tr("Load background image"));
  3033 	fd->setDir (lastImageDir);
  3034 	fd->show();
  3035 
  3036 	if ( fd->exec() == QDialog::Accepted )
  3037 	{
  3038 		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
  3039 		lastImageDir=QDir (fd->dirPath());
  3040 		setMapBackgroundImage (fd->selectedFile());
  3041 	}
  3042 }	
  3043 
  3044 void MapEditor::setMapBackgroundImage (const QString &fn)	//FIXME missing savestate
  3045 {
  3046 	QColor oldcol=mapScene->backgroundBrush().color();
  3047 	/*
  3048 	saveState(
  3049 		selection,
  3050 		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
  3051 		selection,
  3052 		QString ("setMapBackgroundImage (%1)").arg(col.name()),
  3053 		QString("Set background color of map to %1").arg(col.name()));
  3054 	*/	
  3055 	QBrush brush;
  3056 	brush.setTextureImage (QPixmap (fn));
  3057 	mapScene->setBackgroundBrush(brush);
  3058 }
  3059 
  3060 void MapEditor::selectMapBackgroundColor()
  3061 {
  3062 	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), this );
  3063 	if ( !col.isValid() ) return;
  3064 	setMapBackgroundColor( col );
  3065 }
  3066 
  3067 
  3068 void MapEditor::setMapBackgroundColor(QColor col)
  3069 {
  3070 	QColor oldcol=mapScene->backgroundBrush().color();
  3071 	saveState(
  3072 		mapCenter,
  3073 		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
  3074 		mapCenter,
  3075 		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
  3076 		QString("Set background color of map to %1").arg(col.name()));
  3077 	mapScene->setBackgroundBrush(col);
  3078 }
  3079 
  3080 QColor MapEditor::getMapBackgroundColor()
  3081 {
  3082     return mapScene->backgroundBrush().color();
  3083 }
  3084 
  3085 QColor MapEditor::getCurrentHeadingColor()
  3086 {
  3087 	BranchObj *bo=xelection.getBranch();
  3088 	if (bo) return bo->getColor(); 
  3089 	
  3090 	QMessageBox::warning(0,tr("Warning"),tr("Can't get color of heading,\nthere's no branch selected"));
  3091 	return Qt::black;
  3092 }
  3093 
  3094 void MapEditor::colorBranch (QColor c)
  3095 {
  3096 	BranchObj *bo=xelection.getBranch();
  3097 	if (bo)
  3098 	{
  3099 		saveState(
  3100 			bo, 
  3101 			QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
  3102 			bo,
  3103 			QString ("colorBranch (\"%1\")").arg(c.name()),
  3104 			QString("Set color of %1 to %2").arg(getName(bo)).arg(c.name())
  3105 		);	
  3106 		bo->setColor(c); // color branch
  3107 	}
  3108 }
  3109 
  3110 void MapEditor::colorSubtree (QColor c)
  3111 {
  3112 	BranchObj *bo=xelection.getBranch();
  3113 	if (bo) 
  3114 	{
  3115 		saveStateChangingPart(
  3116 			bo, 
  3117 			bo,
  3118 			QString ("colorSubtree (\"%1\")").arg(c.name()),
  3119 			QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(c.name())
  3120 		);	
  3121 		bo->setColorSubtree (c); // color links, color childs
  3122 	}
  3123 }
  3124 
  3125 
  3126 void MapEditor::toggleStandardFlag(QString f)
  3127 {
  3128 	BranchObj *bo=xelection.getBranch();
  3129 	if (bo) 
  3130 	{
  3131 		QString u,r;
  3132 		if (bo->isSetStandardFlag(f))
  3133 		{
  3134 			r="unsetFlag";
  3135 			u="setFlag";
  3136 		}	
  3137 		else
  3138 		{
  3139 			u="unsetFlag";
  3140 			r="setFlag";
  3141 		}	
  3142 		saveState(
  3143 			bo,
  3144 			QString("%1 (\"%2\")").arg(u).arg(f), 
  3145 			bo,
  3146 			QString("%1 (\"%2\")").arg(r).arg(f),
  3147 			QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
  3148 		bo->toggleStandardFlag (f,mainWindow->useFlagGroups());
  3149 		xelection.update();
  3150 	}
  3151 }
  3152 
  3153 
  3154 BranchObj* MapEditor::findText (QString s, bool cs)
  3155 {
  3156 	QTextDocument::FindFlags flags=0;
  3157 	if (cs) flags=QTextDocument::FindCaseSensitively;
  3158 
  3159 	if (!itFind) 
  3160 	{	// Nothing found or new find process
  3161 		if (EOFind)
  3162 			// nothing found, start again
  3163 			EOFind=false;
  3164 		itFind=mapCenter->first();
  3165 	}	
  3166 	bool searching=true;
  3167 	bool foundNote=false;
  3168 	while (searching && !EOFind)
  3169 	{
  3170 		if (itFind)
  3171 		{
  3172 			// Searching in Note
  3173 			if (itFind->getNote().contains(s,cs))
  3174 			{
  3175 				if (xelection.single()!=itFind) 
  3176 				{
  3177 					xelection.select(itFind);
  3178 					ensureSelectionVisible();
  3179 				}
  3180 				if (textEditor->findText(s,flags)) 
  3181 				{
  3182 					searching=false;
  3183 					foundNote=true;
  3184 				}	
  3185 			}
  3186 			// Searching in Heading
  3187 			if (searching && itFind->getHeading().contains (s,cs) ) 
  3188 			{
  3189 				xelection.select(itFind);
  3190 				ensureSelectionVisible();
  3191 				searching=false;
  3192 			}
  3193 		}	
  3194 		if (!foundNote)
  3195 		{
  3196 			itFind=itFind->next();
  3197 			if (!itFind) EOFind=true;
  3198 		}
  3199 	}	
  3200 	if (!searching)
  3201 		return xelection.getBranch();
  3202 	else
  3203 		return NULL;
  3204 }
  3205 
  3206 void MapEditor::findReset()
  3207 {	// Necessary if text to find changes during a find process
  3208 	itFind=NULL;
  3209 	EOFind=false;
  3210 }
  3211 void MapEditor::setURL(const QString &url)
  3212 {
  3213 	BranchObj *bo=xelection.getBranch();
  3214 	if (bo)
  3215 	{
  3216 		QString oldurl=bo->getURL();
  3217 		bo->setURL (url);
  3218 		saveState (
  3219 			bo,
  3220 			QString ("setURL (\"%1\")").arg(oldurl),
  3221 			bo,
  3222 			QString ("setURL (\"%1\")").arg(url),
  3223 			QString ("set URL of %1 to %2").arg(getName(bo)).arg(url)
  3224 		);
  3225 		updateActions();
  3226 		mapCenter->reposition();
  3227 		xelection.update();
  3228 		ensureSelectionVisible();
  3229 	}
  3230 }	
  3231 
  3232 void MapEditor::editURL()
  3233 {
  3234 	BranchObj *bo=xelection.getBranch();
  3235 	if (bo)
  3236 	{		
  3237 		bool ok;
  3238 		QString text = QInputDialog::getText(
  3239 				"VYM", tr("Enter URL:"), QLineEdit::Normal,
  3240 				bo->getURL(), &ok, this );
  3241 		if ( ok) 
  3242 			// user entered something and pressed OK
  3243 			setURL (text);
  3244 	}
  3245 }
  3246 
  3247 void MapEditor::editLocalURL()
  3248 {
  3249 	BranchObj *bo=xelection.getBranch();
  3250 	if (bo)
  3251 	{		
  3252 		QStringList filters;
  3253 		filters <<"All files (*)";
  3254 		filters << tr("Text","Filedialog") + " (*.txt)";
  3255 		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
  3256 		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
  3257 		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
  3258 		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Set URL to a local file"));
  3259 		fd->setFilters (filters);
  3260 		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
  3261 		fd->setDirectory (lastFileDir);
  3262 		if (! bo->getVymLink().isEmpty() )
  3263 			fd->selectFile( bo->getURL() );
  3264 		fd->show();
  3265 
  3266 		if ( fd->exec() == QDialog::Accepted )
  3267 		{
  3268 			lastFileDir=QDir (fd->directory().path());
  3269 			setURL (fd->selectedFile() );
  3270 		}
  3271 	}
  3272 }
  3273 
  3274 QString MapEditor::getURL()
  3275 {
  3276 	BranchObj *bo=xelection.getBranch();
  3277 	if (bo)
  3278 		return bo->getURL();
  3279 	else
  3280 		return "";
  3281 }
  3282 
  3283 QStringList MapEditor::getURLs()
  3284 {
  3285 	QStringList urls;
  3286 	BranchObj *bo=xelection.getBranch();
  3287 	if (bo)
  3288 	{		
  3289 		bo=bo->first();	
  3290 		while (bo) 
  3291 		{
  3292 			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
  3293 			bo=bo->next();
  3294 		}	
  3295 	}	
  3296 	return urls;
  3297 }
  3298 
  3299 
  3300 void MapEditor::editHeading2URL()
  3301 {
  3302 	BranchObj *bo=xelection.getBranch();
  3303 	if (bo)
  3304 		setURL (bo->getHeading());
  3305 }	
  3306 
  3307 void MapEditor::editBugzilla2URL()
  3308 {
  3309 	BranchObj *bo=xelection.getBranch();
  3310 	if (bo)
  3311 	{		
  3312 		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
  3313 		setURL (url);
  3314 	}
  3315 }	
  3316 
  3317 void MapEditor::editFATE2URL()
  3318 {
  3319 	BranchObj *bo=xelection.getBranch();
  3320 	if (bo)
  3321 	{		
  3322 		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
  3323 		saveState(
  3324 			bo,
  3325 			"setURL (\""+bo->getURL()+"\")",
  3326 			bo,
  3327 			"setURL (\""+url+"\")",
  3328 			QString("Use heading of %1 as link to FATE").arg(getName(bo))
  3329 		);	
  3330 		bo->setURL (url);
  3331 		updateActions();
  3332 	}
  3333 }	
  3334 
  3335 void MapEditor::editVymLink()
  3336 {
  3337 	BranchObj *bo=xelection.getBranch();
  3338 	if (bo)
  3339 	{		
  3340 		QStringList filters;
  3341 		filters <<"VYM map (*.vym)";
  3342 		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Link to another map"));
  3343 		fd->setFilters (filters);
  3344 		fd->setCaption(vymName+" - " +tr("Link to another map"));
  3345 		fd->setDirectory (lastFileDir);
  3346 		if (! bo->getVymLink().isEmpty() )
  3347 			fd->selectFile( bo->getVymLink() );
  3348 		fd->show();
  3349 
  3350 		QString fn;
  3351 		if ( fd->exec() == QDialog::Accepted )
  3352 		{
  3353 			lastFileDir=QDir (fd->directory().path());
  3354 			saveState(
  3355 				bo,
  3356 				"setVymLink (\""+bo->getVymLink()+"\")",
  3357 				bo,
  3358 				"setVymLink (\""+fd->selectedFile()+"\")",
  3359 				QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile())
  3360 			);	
  3361 			setVymLinkInt (fd->selectedFile() );
  3362 		}
  3363 	}
  3364 }
  3365 
  3366 void MapEditor::deleteVymLink()
  3367 {
  3368 	BranchObj *bo=xelection.getBranch();
  3369 	if (bo)
  3370 	{		
  3371 		saveState(
  3372 			bo,
  3373 			"setVymLink (\""+bo->getVymLink()+"\")",
  3374 			bo,
  3375 			"setVymLink (\"\")",
  3376 			QString("Unset vymlink of %1").arg(getName(bo))
  3377 		);	
  3378 		bo->setVymLink ("" );
  3379 		updateActions();
  3380 		mapCenter->reposition();
  3381 		scene()->update();
  3382 	}
  3383 }
  3384 
  3385 void MapEditor::setHideExport(bool b)
  3386 {
  3387 	BranchObj *bo=xelection.getBranch();
  3388 	if (bo)
  3389 	{
  3390 		bo->setHideInExport (b);
  3391 		QString u= b ? "false" : "true";
  3392 		QString r=!b ? "false" : "true";
  3393 		
  3394 		saveState(
  3395 			bo,
  3396 			QString ("setHideExport (%1)").arg(u),
  3397 			bo,
  3398 			QString ("setHideExport (%1)").arg(r),
  3399 			QString ("Set HideExport flag of %1 to %2").arg(getName(bo)).arg (r)
  3400 		);	
  3401 		updateActions();
  3402 		mapCenter->reposition();
  3403 		xelection.update();
  3404 		scene()->update();
  3405 	}
  3406 }
  3407 
  3408 void MapEditor::toggleHideExport()
  3409 {
  3410 	BranchObj *bo=xelection.getBranch();
  3411 	if (bo)
  3412 		setHideExport ( !bo->hideInExport() );
  3413 }
  3414 
  3415 QString MapEditor::getVymLink()
  3416 {
  3417 	BranchObj *bo=xelection.getBranch();
  3418 	if (bo)
  3419 		return bo->getVymLink();
  3420 	else	
  3421 		return "";
  3422 	
  3423 }
  3424 
  3425 QStringList MapEditor::getVymLinks()
  3426 {
  3427 	QStringList links;
  3428 	BranchObj *bo=xelection.getBranch();
  3429 	if (bo)
  3430 	{		
  3431 		bo=bo->first();	
  3432 		while (bo) 
  3433 		{
  3434 			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
  3435 			bo=bo->next();
  3436 		}	
  3437 	}	
  3438 	return links;
  3439 }
  3440 
  3441 
  3442 void MapEditor::deleteKeepChilds()
  3443 {
  3444 	BranchObj *bo=xelection.getBranch();
  3445 	BranchObj *par;
  3446 	if (bo)
  3447 	{
  3448 		par=(BranchObj*)(bo->getParObj());
  3449 		QPointF p=bo->getRelPos();
  3450 		saveStateChangingPart(
  3451 			bo->getParObj(),
  3452 			bo,
  3453 			"deleteKeepChilds ()",
  3454 			QString("Remove %1 and keep its childs").arg(getName(bo))
  3455 		);
  3456 
  3457 		QString sel=bo->getSelectString();
  3458 		unselect();
  3459 		par->removeBranchHere(bo);
  3460 		mapCenter->reposition();
  3461 		select (sel);
  3462 		xelection.getBranch()->move2RelPos (p);
  3463 		mapCenter->reposition();
  3464 	}	
  3465 }
  3466 
  3467 void MapEditor::deleteChilds()
  3468 {
  3469 	BranchObj *bo=xelection.getBranch();
  3470 	if (bo)
  3471 	{		
  3472 		saveStateChangingPart(
  3473 			bo, 
  3474 			bo,
  3475 			"deleteChilds ()",
  3476 			QString( "Remove childs of branch %1").arg(getName(bo))
  3477 		);
  3478 		bo->removeChilds();
  3479 		mapCenter->reposition();
  3480 	}	
  3481 }
  3482 
  3483 void MapEditor::editMapInfo()
  3484 {
  3485 	ExtraInfoDialog dia;
  3486 	dia.setMapName (getFileName() );
  3487 	dia.setAuthor (mapCenter->getAuthor() );
  3488 	dia.setComment(mapCenter->getComment() );
  3489 
  3490 	// Calc some stats
  3491 	QString stats;
  3492     stats+=tr("%1 items on map\n","Info about map").arg (mapScene->items().size(),6);
  3493 
  3494 	uint b=0;
  3495 	uint f=0;
  3496 	uint n=0;
  3497 	uint xl=0;
  3498 	BranchObj *bo;
  3499 	bo=mapCenter->first();
  3500 	while (bo) 
  3501 	{
  3502 		if (!bo->getNote().isEmpty() ) n++;
  3503 		f+= bo->countFloatImages();
  3504 		b++;
  3505 		xl+=bo->countXLinks();
  3506 		bo=bo->next();
  3507 	}
  3508     stats+=QString ("%1 branches\n").arg (b-1,6);
  3509     stats+=QString ("%1 xLinks \n").arg (xl,6);
  3510     stats+=QString ("%1 notes\n").arg (n,6);
  3511     stats+=QString ("%1 images\n").arg (f,6);
  3512 	dia.setStats (stats);
  3513 
  3514 	// Finally show dialog
  3515 	if (dia.exec() == QDialog::Accepted)
  3516 	{
  3517 		setMapAuthor (dia.getAuthor() );
  3518 		setMapComment (dia.getComment() );
  3519 	}
  3520 }
  3521 
  3522 void MapEditor::ensureSelectionVisible()
  3523 {
  3524 	LinkableMapObj *lmo=xelection.single();
  3525 	if (lmo) ensureVisible (lmo->getBBox() );
  3526 	
  3527 }
  3528 
  3529 void MapEditor::updateSelection()
  3530 {
  3531 	// Tell selection to update geometries
  3532 	xelection.update();
  3533 }
  3534 
  3535 void MapEditor::updateActions()
  3536 {
  3537 	// Tell mainwindow to update states of actions
  3538 	mainWindow->updateActions();
  3539 	// TODO maybe don't update if blockReposition is set
  3540 }
  3541 
  3542 void MapEditor::updateNoteFlag()
  3543 {
  3544 	setChanged();
  3545 	BranchObj *bo=xelection.getBranch();
  3546 	if (bo) 
  3547 	{
  3548 		bo->updateNoteFlag();
  3549 		mainWindow->updateActions();
  3550 	}	
  3551 }
  3552 
  3553 void MapEditor::setMapAuthor (const QString &s)
  3554 {
  3555 	saveState (
  3556 		mapCenter,
  3557 		QString ("setMapAuthor (\"%1\")").arg(mapCenter->getAuthor()),
  3558 		mapCenter,
  3559 		QString ("setMapAuthor (\"%1\")").arg(s),
  3560 		QString ("Set author of map to \"%1\"").arg(s)
  3561 	);
  3562 	mapCenter->setAuthor (s);
  3563 }
  3564 
  3565 void MapEditor::setMapComment (const QString &s)
  3566 {
  3567 	saveState (
  3568 		mapCenter,
  3569 		QString ("setMapComment (\"%1\")").arg(mapCenter->getComment()),
  3570 		mapCenter,
  3571 		QString ("setMapComment (\"%1\")").arg(s),
  3572 		QString ("Set comment of map")
  3573 	);
  3574 	mapCenter->setComment (s);
  3575 }
  3576 
  3577 void MapEditor::setMapLinkStyle (const QString & s)
  3578 {
  3579 	saveStateChangingPart (
  3580 		mapCenter,
  3581 		mapCenter,
  3582 		QString("setMapLinkStyle (\"%1\")").arg(s),
  3583 		QString("Set map link style (\"%1\")").arg(s)
  3584 	);	
  3585 
  3586 	if (s=="StyleLine")
  3587 		linkstyle=LinkableMapObj::Line;
  3588 	else if (s=="StyleParabel")
  3589 		linkstyle=LinkableMapObj::Parabel;
  3590 	else if (s=="StylePolyLine")
  3591 		linkstyle=LinkableMapObj::PolyLine;
  3592 	else	
  3593 		linkstyle=LinkableMapObj::PolyParabel;
  3594 
  3595 	BranchObj *bo;
  3596 	bo=mapCenter->first();
  3597 	bo=bo->next();
  3598 	while (bo) 
  3599 	{
  3600 		bo->setLinkStyle(bo->getDefLinkStyle());
  3601 		bo=bo->next();
  3602 	}
  3603 	mapCenter->reposition();
  3604 }
  3605 
  3606 LinkableMapObj::Style MapEditor::getMapLinkStyle ()
  3607 {
  3608 	return linkstyle;
  3609 }	
  3610 
  3611 void MapEditor::setMapDefLinkColor(QColor c)
  3612 {
  3613 	defLinkColor=c;
  3614 	BranchObj *bo;
  3615 	bo=mapCenter->first();
  3616 	while (bo) 
  3617 	{
  3618 		bo->setLinkColor();
  3619 		bo=bo->next();
  3620 	}
  3621 	updateActions();
  3622 }
  3623 
  3624 void MapEditor::setMapLinkColorHintInt()
  3625 {
  3626 	// called from setMapLinkColorHint(lch) or at end of parse
  3627 	BranchObj *bo;
  3628 	bo=mapCenter->first();
  3629 	while (bo) 
  3630 	{
  3631 		bo->setLinkColor();
  3632 		bo=bo->next();
  3633 	}
  3634 }
  3635 
  3636 void MapEditor::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
  3637 {
  3638 	linkcolorhint=lch;
  3639 	setMapLinkColorHintInt();
  3640 }
  3641 
  3642 void MapEditor::toggleMapLinkColorHint()
  3643 {
  3644 	if (linkcolorhint==LinkableMapObj::HeadingColor)
  3645 		linkcolorhint=LinkableMapObj::DefaultColor;
  3646 	else	
  3647 		linkcolorhint=LinkableMapObj::HeadingColor;
  3648 	BranchObj *bo;
  3649 	bo=mapCenter->first();
  3650 	while (bo) 
  3651 	{
  3652 		bo->setLinkColor();
  3653 		bo=bo->next();
  3654 	}
  3655 }
  3656 
  3657 LinkableMapObj::ColorHint MapEditor::getMapLinkColorHint()
  3658 {
  3659 	return linkcolorhint;
  3660 }
  3661 
  3662 QColor MapEditor::getMapDefLinkColor()
  3663 {
  3664 	return defLinkColor;
  3665 }
  3666 
  3667 void MapEditor::setMapDefXLinkColor(QColor col)
  3668 {
  3669 	defXLinkColor=col;
  3670 }
  3671 
  3672 QColor MapEditor::getMapDefXLinkColor()
  3673 {
  3674 	return defXLinkColor;
  3675 }
  3676 
  3677 void MapEditor::setMapDefXLinkWidth (int w)
  3678 {
  3679 	defXLinkWidth=w;
  3680 }
  3681 
  3682 int MapEditor::getMapDefXLinkWidth()
  3683 {
  3684 	return defXLinkWidth;
  3685 }
  3686 
  3687 void MapEditor::selectMapLinkColor()
  3688 {
  3689 	QColor col = QColorDialog::getColor( defLinkColor, this );
  3690 	if ( !col.isValid() ) return;
  3691 	saveState (
  3692 		mapCenter,
  3693 		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
  3694 		mapCenter,
  3695 		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
  3696 		QString("Set map link color to %1").arg(col.name())
  3697 	);
  3698 	setMapDefLinkColor( col );
  3699 }
  3700 
  3701 void MapEditor::selectMapSelectionColor()
  3702 {
  3703 	QColor col = QColorDialog::getColor( defLinkColor, this );
  3704 	setSelectionColor (col);
  3705 }
  3706 
  3707 void MapEditor::setSelectionColorInt (QColor col)
  3708 {
  3709 	if ( !col.isValid() ) return;
  3710 	xelection.setColor (col);
  3711 }
  3712 
  3713 void MapEditor::setSelectionColor(QColor col)
  3714 {
  3715 	if ( !col.isValid() ) return;
  3716 	saveState (
  3717 		mapCenter,
  3718 		QString("setSelectionColor (%1)").arg(xelection.getColor().name()),
  3719 		mapCenter,
  3720 		QString("setSelectionColor (%1)").arg(col.name()),
  3721 		QString("Set color of selection box to %1").arg(col.name())
  3722 	);
  3723 	setSelectionColorInt (col);
  3724 }
  3725 
  3726 QColor MapEditor::getSelectionColor()
  3727 {
  3728 	return xelection.getColor();
  3729 }
  3730 
  3731 bool MapEditor::scrollBranch(BranchObj *bo)
  3732 {
  3733 	if (bo)
  3734 	{
  3735 		if (bo->isScrolled()) return false;
  3736 		if (bo->countBranches()==0) return false;
  3737 		if (bo->getDepth()==0) return false;
  3738 		QString u,r;
  3739 		r="scroll";
  3740 		u="unscroll";
  3741 		saveState(
  3742 			bo,
  3743 			QString ("%1 ()").arg(u),
  3744 			bo,
  3745 			QString ("%1 ()").arg(r),
  3746 			QString ("%1 %2").arg(r).arg(getName(bo))
  3747 		);
  3748 		bo->toggleScroll();
  3749 		xelection.update();
  3750 		scene()->update();
  3751 		return true;
  3752 	}	
  3753 	return false;
  3754 }
  3755 
  3756 bool MapEditor::unscrollBranch(BranchObj *bo)
  3757 {
  3758 	if (bo)
  3759 	{
  3760 		if (!bo->isScrolled()) return false;
  3761 		if (bo->countBranches()==0) return false;
  3762 		if (bo->getDepth()==0) return false;
  3763 		QString u,r;
  3764 		u="scroll";
  3765 		r="unscroll";
  3766 		saveState(
  3767 			bo,
  3768 			QString ("%1 ()").arg(u),
  3769 			bo,
  3770 			QString ("%1 ()").arg(r),
  3771 			QString ("%1 %2").arg(r).arg(getName(bo))
  3772 		);
  3773 		bo->toggleScroll();
  3774 		xelection.update();
  3775 		scene()->update();
  3776 		return true;
  3777 	}	
  3778 	return false;
  3779 }
  3780 
  3781 void MapEditor::toggleScroll()
  3782 {
  3783 	BranchObj *bo=xelection.getBranch();
  3784 	if (xelection.type()==Selection::Branch )
  3785 	{
  3786 		if (bo->isScrolled())
  3787 			unscrollBranch (bo);
  3788 		else
  3789 			scrollBranch (bo);
  3790 	}
  3791 }
  3792 
  3793 void MapEditor::unscrollChilds() 
  3794 {
  3795 	BranchObj *bo=xelection.getBranch();
  3796 	if (bo)
  3797 	{
  3798 		bo->first();
  3799 		while (bo) 
  3800 		{
  3801 			if (bo->isScrolled()) unscrollBranch (bo);
  3802 			bo=bo->next();
  3803 		}
  3804 	}	
  3805 }
  3806 
  3807 FloatImageObj* MapEditor::loadFloatImageInt (QString fn)
  3808 {
  3809 	BranchObj *bo=xelection.getBranch();
  3810 	if (bo)
  3811 	{
  3812 		FloatImageObj *fio;
  3813 		bo->addFloatImage();
  3814 		fio=bo->getLastFloatImage();
  3815 		fio->load(fn);
  3816 		mapCenter->reposition();
  3817 		scene()->update();
  3818 		return fio;
  3819 	}
  3820 	return NULL;
  3821 }	
  3822 
  3823 void MapEditor::loadFloatImage ()
  3824 {
  3825 	BranchObj *bo=xelection.getBranch();
  3826 	if (bo)
  3827 	{
  3828 
  3829 		Q3FileDialog *fd=new Q3FileDialog( this);
  3830 		fd->setMode (Q3FileDialog::ExistingFiles);
  3831 		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  3832 		ImagePreview *p =new ImagePreview (fd);
  3833 		fd->setContentsPreviewEnabled( TRUE );
  3834 		fd->setContentsPreview( p, p );
  3835 		fd->setPreviewMode( Q3FileDialog::Contents );
  3836 		fd->setCaption(vymName+" - " +tr("Load image"));
  3837 		fd->setDir (lastImageDir);
  3838 		fd->show();
  3839 
  3840 		if ( fd->exec() == QDialog::Accepted )
  3841 		{
  3842 			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
  3843 			lastImageDir=QDir (fd->dirPath());
  3844 			QString s;
  3845 			FloatImageObj *fio;
  3846 			for (int j=0; j<fd->selectedFiles().count(); j++)
  3847 			{
  3848 				s=fd->selectedFiles().at(j);
  3849 				fio=loadFloatImageInt (s);
  3850 				if (fio)
  3851 					saveState(
  3852 						(LinkableMapObj*)fio,
  3853 						"delete ()",
  3854 						bo, 
  3855 						QString ("loadImage (%1)").arg(s ),
  3856 						QString("Add image %1 to %2").arg(s).arg(getName(bo))
  3857 					);
  3858 				else
  3859 					// TODO loadFIO error handling
  3860 					qWarning ("Failed to load "+s);
  3861 			}
  3862 		}
  3863 		delete (p);
  3864 		delete (fd);
  3865 	}
  3866 }
  3867 
  3868 void MapEditor::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
  3869 {
  3870 	fio->save (fn,type);
  3871 }
  3872 
  3873 void MapEditor::saveFloatImage ()
  3874 {
  3875 	FloatImageObj *fio=xelection.getFloatImage();
  3876 	if (fio)
  3877 	{
  3878 		QFileDialog *fd=new QFileDialog( this);
  3879 		fd->setFilters (imageIO.getFilters());
  3880 		fd->setCaption(vymName+" - " +tr("Save image"));
  3881 		fd->setFileMode( QFileDialog::AnyFile );
  3882 		fd->setDirectory (lastImageDir);
  3883 //		fd->setSelection (fio->getOriginalFilename());
  3884 		fd->show();
  3885 
  3886 		QString fn;
  3887 		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
  3888 		{
  3889 			fn=fd->selectedFiles().at(0);
  3890 			if (QFile (fn).exists() )
  3891 			{
  3892 				QMessageBox mb( vymName,
  3893 					tr("The file %1 exists already.\n"
  3894 					"Do you want to overwrite it?").arg(fn),
  3895 				QMessageBox::Warning,
  3896 				QMessageBox::Yes | QMessageBox::Default,
  3897 				QMessageBox::Cancel | QMessageBox::Escape,
  3898 				QMessageBox::QMessageBox::NoButton );
  3899 
  3900 				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
  3901 				mb.setButtonText( QMessageBox::No, tr("Cancel"));
  3902 				switch( mb.exec() ) 
  3903 				{
  3904 					case QMessageBox::Yes:
  3905 						// save 
  3906 						break;;
  3907 					case QMessageBox::Cancel:
  3908 						// do nothing
  3909 						delete (fd);
  3910 						return;
  3911 						break;
  3912 				}
  3913 			}
  3914 			saveFloatImageInt (fio,fd->selectedFilter(),fn );
  3915 		}
  3916 		delete (fd);
  3917 	}
  3918 }
  3919 
  3920 void MapEditor::setFrameType(const FrameObj::FrameType &t)
  3921 {
  3922 	BranchObj *bo=xelection.getBranch();
  3923 	if (bo)
  3924 	{
  3925 		QString s=bo->getFrameTypeName();
  3926 		bo->setFrameType (t);
  3927 		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
  3928 			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
  3929 		mapCenter->reposition();
  3930 		bo->updateLink();
  3931 	}
  3932 }
  3933 
  3934 void MapEditor::setFrameType(const QString &s)	
  3935 {
  3936 	BranchObj *bo=xelection.getBranch();
  3937 	if (bo)
  3938 	{
  3939 		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
  3940 			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
  3941 		bo->setFrameType (s);
  3942 		mapCenter->reposition();
  3943 		bo->updateLink();
  3944 	}
  3945 }
  3946 
  3947 void MapEditor::setFramePenColor(const QColor &c)	
  3948 {
  3949 	BranchObj *bo=xelection.getBranch();
  3950 	if (bo)
  3951 	{
  3952 		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
  3953 			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
  3954 		bo->setFramePenColor (c);
  3955 	}	
  3956 }
  3957 
  3958 void MapEditor::setFrameBrushColor(const QColor &c)	
  3959 {
  3960 	BranchObj *bo=xelection.getBranch();
  3961 	if (bo)
  3962 	{
  3963 		saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
  3964 			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
  3965 		bo->setFrameBrushColor (c);
  3966 	}	
  3967 }
  3968 
  3969 void MapEditor::setFramePadding (const int &i)
  3970 {
  3971 	BranchObj *bo=xelection.getBranch();
  3972 	if (bo)
  3973 	{
  3974 		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
  3975 			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
  3976 		bo->setFramePadding (i);
  3977 		mapCenter->reposition();
  3978 		bo->updateLink();
  3979 	}	
  3980 }
  3981 
  3982 void MapEditor::setFrameBorderWidth(const int &i)
  3983 {
  3984 	BranchObj *bo=xelection.getBranch();
  3985 	if (bo)
  3986 	{
  3987 		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
  3988 			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
  3989 		bo->setFrameBorderWidth (i);
  3990 		mapCenter->reposition();
  3991 		bo->updateLink();
  3992 	}	
  3993 }
  3994 
  3995 void MapEditor::setIncludeImagesVer(bool b)	
  3996 {
  3997 	BranchObj *bo=xelection.getBranch();
  3998 	if (bo)
  3999 	{
  4000 		QString u= b ? "false" : "true";
  4001 		QString r=!b ? "false" : "true";
  4002 		
  4003 		saveState(
  4004 			bo,
  4005 			QString("setIncludeImagesVertically (%1)").arg(u),
  4006 			bo, 
  4007 			QString("setIncludeImagesVertically (%1)").arg(r),
  4008 			QString("Include images vertically in %1").arg(getName(bo))
  4009 		);	
  4010 		bo->setIncludeImagesVer(b);
  4011 		mapCenter->reposition();
  4012 	}	
  4013 }
  4014 
  4015 void MapEditor::setIncludeImagesHor(bool b)	
  4016 {
  4017 	BranchObj *bo=xelection.getBranch();
  4018 	if (bo)
  4019 	{
  4020 		QString u= b ? "false" : "true";
  4021 		QString r=!b ? "false" : "true";
  4022 		
  4023 		saveState(
  4024 			bo,
  4025 			QString("setIncludeImagesHorizontally (%1)").arg(u),
  4026 			bo, 
  4027 			QString("setIncludeImagesHorizontally (%1)").arg(r),
  4028 			QString("Include images horizontally in %1").arg(getName(bo))
  4029 		);	
  4030 		bo->setIncludeImagesHor(b);
  4031 		mapCenter->reposition();
  4032 	}	
  4033 }
  4034 
  4035 void MapEditor::setHideLinkUnselected (bool b)
  4036 {
  4037 	LinkableMapObj *sel=xelection.single();
  4038 	if (sel &&
  4039 		(xelection.type() == Selection::Branch || 
  4040 		xelection.type() == Selection::MapCenter  ||
  4041 		xelection.type() == Selection::FloatImage ))
  4042 	{
  4043 		QString u= b ? "false" : "true";
  4044 		QString r=!b ? "false" : "true";
  4045 		
  4046 		saveState(
  4047 			sel,
  4048 			QString("setHideLinkUnselected (%1)").arg(u),
  4049 			sel, 
  4050 			QString("setHideLinkUnselected (%1)").arg(r),
  4051 			QString("Hide link of %1 if unselected").arg(getName(sel))
  4052 		);	
  4053 		sel->setHideLinkUnselected(b);
  4054 	}
  4055 }
  4056 
  4057 void MapEditor::importDirInt(BranchObj *dst, QDir d)
  4058 {
  4059 	BranchObj *bo=xelection.getBranch();
  4060 	if (bo)
  4061 	{
  4062 		// Traverse directories
  4063 		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
  4064 		QFileInfoList list = d.entryInfoList();
  4065 		QFileInfo fi;
  4066 
  4067 		for (int i = 0; i < list.size(); ++i) 
  4068 		{
  4069 			fi=list.at(i);
  4070 			if (fi.fileName() != "." && fi.fileName() != ".." )
  4071 			{
  4072 				dst->addBranch();
  4073 				bo=dst->getLastBranch();
  4074 				bo->setHeading (fi.fileName() );
  4075 				bo->setColor (QColor("blue"));
  4076 				bo->toggleScroll();
  4077 				if ( !d.cd(fi.fileName()) ) 
  4078 					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
  4079 				else 
  4080 				{
  4081 					// Recursively add subdirs
  4082 					importDirInt (bo,d);
  4083 					d.cdUp();
  4084 				}
  4085 			}	
  4086 		}		
  4087 		// Traverse files
  4088 		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
  4089 		list = d.entryInfoList();
  4090 
  4091 		for (int i = 0; i < list.size(); ++i) 
  4092 		{
  4093 			fi=list.at(i);
  4094 			dst->addBranch();
  4095 			bo=dst->getLastBranch();
  4096 			bo->setHeading (fi.fileName() );
  4097 			bo->setColor (QColor("black"));
  4098 			if (fi.fileName().right(4) == ".vym" )
  4099 				bo->setVymLink (fi.filePath());
  4100 		}	
  4101 	}		
  4102 }
  4103 
  4104 void MapEditor::importDirInt (const QString &s)
  4105 {
  4106 	BranchObj *bo=xelection.getBranch();
  4107 	if (bo)
  4108 	{
  4109 		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
  4110 
  4111 		QDir d(s);
  4112 		importDirInt (bo,d);
  4113 	}
  4114 }	
  4115 
  4116 void MapEditor::importDir()
  4117 {
  4118 	BranchObj *bo=xelection.getBranch();
  4119 	if (bo)
  4120 	{
  4121 		QStringList filters;
  4122 		filters <<"VYM map (*.vym)";
  4123 		QFileDialog *fd=new QFileDialog( this,vymName+ " - " +tr("Choose directory structure to import"));
  4124 		fd->setMode (QFileDialog::DirectoryOnly);
  4125 		fd->setFilters (filters);
  4126 		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
  4127 		fd->show();
  4128 
  4129 		QString fn;
  4130 		if ( fd->exec() == QDialog::Accepted )
  4131 		{
  4132 			importDirInt (fd->selectedFile() );
  4133 			mapCenter->reposition();
  4134 			scene()->update();
  4135 		}
  4136 	}	
  4137 }
  4138 
  4139 void MapEditor::followXLink(int i)
  4140 {
  4141 	BranchObj *bo=xelection.getBranch();
  4142 	if (bo)
  4143 	{
  4144 		bo=bo->XLinkTargetAt(i);
  4145 		if (bo) 
  4146 		{
  4147 			xelection.select(bo);
  4148 			ensureSelectionVisible();
  4149 		}
  4150 	}
  4151 }
  4152 
  4153 void MapEditor::editXLink(int i)	// FIXME missing saveState
  4154 {
  4155 	BranchObj *bo=xelection.getBranch();
  4156 	if (bo)
  4157 	{
  4158 		XLinkObj *xlo=bo->XLinkAt(i);
  4159 		if (xlo) 
  4160 		{
  4161 			EditXLinkDialog dia;
  4162 			dia.setXLink (xlo);
  4163 			dia.setSelection(bo);
  4164 			if (dia.exec() == QDialog::Accepted)
  4165 			{
  4166 				if (dia.useSettingsGlobal() )
  4167 				{
  4168 					setMapDefXLinkColor (xlo->getColor() );
  4169 					setMapDefXLinkWidth (xlo->getWidth() );
  4170 				}
  4171 				if (dia.deleteXLink())
  4172 					bo->deleteXLinkAt(i);
  4173 			}
  4174 		}	
  4175 	}
  4176 }
  4177 
  4178 AttributeTable* MapEditor::attributeTable()
  4179 {
  4180 	return attrTable;
  4181 }
  4182 
  4183 void MapEditor::testFunction1()
  4184 {
  4185 /*
  4186 	BranchObj *bo=xelection.getBranch();
  4187 	if (bo) animObjList.append( bo );
  4188 */	
  4189 	
  4190 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
  4191 	if (hidemode==HideNone)
  4192 	{
  4193 		setHideTmpMode (HideExport);
  4194 		mapCenter->calcBBoxSizeWithChilds();
  4195 		QRectF totalBBox=mapCenter->getTotalBBox();
  4196 		QRectF mapRect=totalBBox;
  4197 		QCanvasRectangle *frame=NULL;
  4198 
  4199 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
  4200 	
  4201 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
  4202 			totalBBox.width(), totalBBox.height());
  4203 		frame=new QCanvasRectangle (mapRect,mapScene);
  4204 		frame->setBrush (QColor(white));
  4205 		frame->setPen (QColor(black));
  4206 		frame->setZValue(0);
  4207 		frame->show();    
  4208 	}	
  4209 	else	
  4210 	{
  4211 		setHideTmpMode (HideNone);
  4212 	}	
  4213 	cout <<"  hidemode="<<hidemode<<endl;
  4214 	*/
  4215 }	
  4216 	
  4217 void MapEditor::testFunction2()
  4218 {
  4219 	// Toggle hidemode
  4220 	if (hidemode==HideExport)
  4221 		setHideTmpMode (HideNone);
  4222 	else	
  4223 		setHideTmpMode (HideExport);
  4224 		
  4225 /*
  4226 	LinkableMapObj *lmo=xelection.getBranch();
  4227 	if (lmo) 
  4228 	{
  4229 		cout << "LMO::id="<<lmo->getID().ascii()<<endl;
  4230 		cout << " BO::id="<<((BranchObj*)lmo)->getID().ascii()<<endl;
  4231 	}	
  4232 */	
  4233 }
  4234 
  4235 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
  4236 {
  4237 	// Lineedits are already closed by preceding
  4238 	// mouseEvent, we don't need to close here.
  4239 
  4240     QPointF p = mapToScene(e->pos());
  4241     LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
  4242 	
  4243     if (lmo) 
  4244 	{	// MapObj was found
  4245 		if (xelection.single() != lmo)
  4246 		{
  4247 			// select the MapObj
  4248 			xelection.select(lmo);
  4249 		}
  4250 		// Context Menu 
  4251 		if (xelection.getBranch() ) 
  4252 		{
  4253 			// Context Menu on branch or mapcenter
  4254 			updateActions();
  4255 			branchContextMenu->popup(e->globalPos() );
  4256 		} else
  4257 		{
  4258 			if (xelection.getFloatImage() )
  4259 			{
  4260 				// Context Menu on floatimage
  4261 				updateActions();
  4262 				floatimageContextMenu->popup(e->globalPos() );
  4263 			}	
  4264 		}	
  4265 	} else 
  4266 	{ // No MapObj found, we are on the Canvas itself
  4267 		// Context Menu on scene
  4268 		updateActions();
  4269 		canvasContextMenu->popup(e->globalPos() );
  4270     } 
  4271 	e->accept();
  4272 }
  4273 
  4274 void MapEditor::keyPressEvent(QKeyEvent* e)
  4275 {
  4276 	if (e->modifiers() & Qt::ControlModifier)
  4277 	{
  4278 		switch (mainWindow->getModMode())
  4279 		{
  4280 			case Main::ModModeColor: 
  4281 				setCursor (PickColorCursor);
  4282 				break;
  4283 			case Main::ModModeCopy: 
  4284 				setCursor (CopyCursor);
  4285 				break;
  4286 			case Main::ModModeXLink: 
  4287 				setCursor (XLinkCursor);
  4288 				break;
  4289 			default :
  4290 				setCursor (Qt::ArrowCursor);
  4291 				break;
  4292 		} 
  4293 	}	
  4294 }
  4295 
  4296 void MapEditor::keyReleaseEvent(QKeyEvent* e)
  4297 {
  4298 	if (!(e->modifiers() & Qt::ControlModifier))
  4299 		setCursor (Qt::ArrowCursor);
  4300 }
  4301 
  4302 void MapEditor::mousePressEvent(QMouseEvent* e)
  4303 {
  4304 	// Ignore right clicks, these will go to context menus
  4305 	if (e->button() == Qt::RightButton )
  4306 	{
  4307 		e->ignore();
  4308 		return;
  4309 	}
  4310 
  4311 	//Ignore clicks while editing heading
  4312 	if (isSelectBlocked() ) 
  4313 	{
  4314 		e->ignore();
  4315 		return;
  4316 	}
  4317 
  4318     QPointF p = mapToScene(e->pos());
  4319     LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
  4320 	
  4321 	e->accept();
  4322 
  4323 	//Take care of  system flags _or_ modifier modes
  4324 	//
  4325 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
  4326 		typeid(*lmo)==typeid(MapCenterObj) ))
  4327 	{
  4328 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
  4329 		if (!foname.isEmpty())
  4330 		{
  4331 			// systemFlag clicked
  4332 			selectInt (lmo);
  4333 			if (foname=="url") 
  4334 			{
  4335 				if (e->state() & Qt::ControlModifier)
  4336 					mainWindow->editOpenURLTab();
  4337 				else	
  4338 					mainWindow->editOpenURL();
  4339 			}	
  4340 			else if (foname=="vymLink")
  4341 			{
  4342 				mainWindow->editOpenVymLink();
  4343 				// tabWidget may change, better return now
  4344 				// before segfaulting...
  4345 			} else if (foname=="note")
  4346 				mainWindow->windowToggleNoteEditor();
  4347 			else if (foname=="hideInExport")		
  4348 				toggleHideExport();
  4349 			xelection.update();	
  4350 			return;	
  4351 		} 
  4352 	} 
  4353 
  4354 	// No system flag clicked, take care of modmodes (CTRL-Click)
  4355 	if (e->state() & Qt::ControlModifier)
  4356 	{
  4357 		if (mainWindow->getModMode()==Main::ModModeColor)
  4358 		{
  4359 				pickingColor=true;
  4360 				setCursor (PickColorCursor);
  4361 				return;
  4362 		} 
  4363 		if (mainWindow->getModMode()==Main::ModModeXLink)
  4364 		{	
  4365 			BranchObj *bo_begin=NULL;
  4366 			if (lmo)
  4367 				bo_begin=(BranchObj*)(lmo);
  4368 			else	
  4369 				if (xelection.getBranch() ) 
  4370 					bo_begin=xelection.getBranch();
  4371 			if (bo_begin)	
  4372 			{
  4373 				drawingLink=true;
  4374 				linkingObj_src=bo_begin;
  4375 				tmpXLink=new XLinkObj (mapScene);
  4376 				tmpXLink->setBegin (bo_begin);
  4377 				tmpXLink->setEnd   (p);
  4378 				tmpXLink->setColor(defXLinkColor);
  4379 				tmpXLink->setWidth(defXLinkWidth);
  4380 				tmpXLink->updateXLink();
  4381 				tmpXLink->setVisibility (true);
  4382 				return;
  4383 			} 
  4384 		}
  4385 	}	// End of modmodes
  4386 
  4387     if (lmo) 
  4388 	{	
  4389 		// Select the clicked object
  4390 		selectInt (lmo);
  4391 
  4392 		// Left Button	    Move Branches
  4393 		if (e->button() == Qt::LeftButton )
  4394 		{
  4395 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
  4396 			//movingObj_start.setY( p.y() - selection->y() );	
  4397 			movingObj_start.setX( p.x() - lmo->x() );	
  4398 			movingObj_start.setY( p.y() - lmo->y() );	
  4399 			movingObj_orgPos.setX (lmo->x() );
  4400 			movingObj_orgPos.setY (lmo->y() );
  4401 			movingObj_orgRelPos=lmo->getRelPos();
  4402 
  4403 			// If modMode==copy, then we want to "move" the _new_ object around
  4404 			// then we need the offset from p to the _old_ selection, because of tmp
  4405 			if (mainWindow->getModMode()==Main::ModModeCopy &&
  4406 				e->state() & Qt::ControlModifier)
  4407 			{
  4408 				if (xelection.type()==Selection::Branch)
  4409 				{
  4410 					copyingObj=true;
  4411 					mapCenter->addBranch ((BranchObj*)xelection.single());
  4412 					unselect();
  4413 					xelection.select(mapCenter->getLastBranch());
  4414 					mapCenter->reposition();
  4415 				}
  4416 			} 
  4417 
  4418 			movingObj=xelection.single();	
  4419 		} else
  4420 			// Middle Button    Toggle Scroll
  4421 			// (On Mac OS X this won't work, but we still have 
  4422 			// a button in the toolbar)
  4423 			if (e->button() == Qt::MidButton )
  4424 				toggleScroll();
  4425 		updateActions();
  4426 		xelection.update();
  4427 	} else 
  4428 	{ // No MapObj found, we are on the scene itself
  4429 		// Left Button	    move Pos of sceneView
  4430 		if (e->button() == Qt::LeftButton )
  4431 		{
  4432 			movingObj=NULL;	// move Content not Obj
  4433 			movingObj_start=e->globalPos();
  4434 			movingCont_start=QPointF (
  4435 				horizontalScrollBar()->value(),
  4436 				verticalScrollBar()->value());
  4437 			movingVec=QPointF(0,0);
  4438 			setCursor(HandOpenCursor);
  4439 		} 
  4440     } 
  4441 }
  4442 
  4443 void MapEditor::mouseMoveEvent(QMouseEvent* e)
  4444 {
  4445     QPointF p = mapToScene(e->pos());
  4446 	LinkableMapObj *lmosel=xelection.single();
  4447 
  4448     // Move the selected MapObj
  4449     if ( lmosel && movingObj) 
  4450     {	
  4451 		// reset cursor if we are moving and don't copy
  4452 		if (mainWindow->getModMode()!=Main::ModModeCopy)
  4453 			setCursor (Qt::ArrowCursor);
  4454 
  4455 		// To avoid jumping of the sceneView, only 
  4456 		// ensureSelectionVisible, if not tmp linked
  4457 		if (!lmosel->hasParObjTmp())
  4458 			ensureSelectionVisible ();
  4459 		
  4460 		// Now move the selection, but add relative position 
  4461 		// (movingObj_start) where selection was chosen with 
  4462 		// mousepointer. (This avoids flickering resp. jumping 
  4463 		// of selection back to absPos)
  4464 		
  4465 		// Check if we could link 
  4466 		LinkableMapObj* lmo=mapCenter->findMapObj(p, lmosel);
  4467 		
  4468 
  4469 		FloatObj *fio=xelection.getFloatImage();
  4470 		if (fio)
  4471 		{
  4472 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4473 			fio->setRelPos();
  4474 			fio->updateLink(); //no need for reposition, if we update link here
  4475 			xelection.update();
  4476 
  4477 			// Relink float to new mapcenter or branch, if shift is pressed	
  4478 			// Only relink, if selection really has a new parent
  4479 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
  4480 				( (typeid(*lmo)==typeid(BranchObj)) ||
  4481 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
  4482 				( lmo != fio->getParObj())  
  4483 				)
  4484 			{
  4485 				if (typeid(*fio) == typeid(FloatImageObj) && 
  4486 				( (typeid(*lmo)==typeid(BranchObj) ||
  4487 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
  4488 				{
  4489 
  4490 					// Also save the move which was done so far
  4491 					QString pold=qpointfToString(movingObj_orgRelPos);
  4492 					QString pnow=qpointfToString(fio->getRelPos());
  4493 					saveState(
  4494 						fio,
  4495 						"moveRel "+pold,
  4496 						fio,
  4497 						"moveRel "+pnow,
  4498 						QString("Move %1 to relative position %2").arg(getName(fio)).arg(pnow));
  4499 					fio->getParObj()->requestReposition();
  4500 					mapCenter->reposition();
  4501 
  4502 					linkTo (lmo->getSelectString());
  4503 					//movingObj=lmosel;
  4504 					//movingObj_orgRelPos=lmosel->getRelPos();	
  4505 
  4506 					mapCenter->reposition();
  4507 				}	
  4508 			}
  4509 		} else	
  4510 		{	// selection != a FloatObj
  4511 			if (lmosel->getDepth()==0)
  4512 			{
  4513 				// Move MapCenter
  4514 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
  4515 					mapCenter->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4516 				else	
  4517 					mapCenter->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4518 				mapCenter->updateRelPositions();	
  4519 			} else
  4520 			{	
  4521 				if (lmosel->getDepth()==1)
  4522 				{
  4523 					// Move mainbranch
  4524 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4525 					lmosel->setRelPos();
  4526 				} else
  4527 				{
  4528 					// Move ordinary branch
  4529 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
  4530 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
  4531 						lmosel->move(p.x() -movingObj_start.x()+lmosel->getBBox().width(), 
  4532 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
  4533 					else	
  4534 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
  4535 				} 
  4536 
  4537 				// Maybe we can relink temporary?
  4538 				if (lmo && (lmo!=lmosel) && xelection.getBranch() && 
  4539 					 (typeid(*lmo)==typeid(BranchObj) ||
  4540 					  typeid(*lmo)==typeid(MapCenterObj)) ) 
  4541 
  4542 				{
  4543 					if (e->modifiers()==Qt::ControlModifier)
  4544 					{
  4545 						// Special case: CTRL to link below lmo
  4546 						lmosel->setParObjTmp (lmo,p,+1);
  4547 					}
  4548 					else if (e->modifiers()==Qt::ShiftModifier)
  4549 						lmosel->setParObjTmp (lmo,p,-1);
  4550 					else
  4551 						lmosel->setParObjTmp (lmo,p,0);
  4552 				} else	
  4553 				{
  4554 					lmosel->unsetParObjTmp();
  4555 				}		
  4556 				// reposition subbranch
  4557 				lmosel->reposition();	
  4558 			} // depth>0
  4559 
  4560 			xelection.update();
  4561 		} // no FloatImageObj
  4562 
  4563 		scene()->update();
  4564 		return;
  4565 	} // selection && moving_obj
  4566 		
  4567 	// Draw a link from one branch to another
  4568 	if (drawingLink)
  4569 	{
  4570 		 tmpXLink->setEnd (p);
  4571 		 tmpXLink->updateXLink();
  4572 	}	 
  4573 	
  4574     // Move sceneView 
  4575     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
  4576 	{
  4577 		QPointF p=e->globalPos();
  4578 		movingVec.setX(-p.x() + movingObj_start.x() );
  4579 		movingVec.setY(-p.y() + movingObj_start.y() );
  4580 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
  4581 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
  4582     }
  4583 }
  4584 
  4585 
  4586 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
  4587 {
  4588     QPointF p = mapToScene(e->pos());
  4589 	LinkableMapObj *dst;
  4590 	LinkableMapObj *lmosel=xelection.single();
  4591 	// Have we been picking color?
  4592 	if (pickingColor)
  4593 	{
  4594 		pickingColor=false;
  4595 		setCursor (Qt::ArrowCursor);
  4596 		// Check if we are over another branch
  4597 		dst=mapCenter->findMapObj(p, NULL);
  4598 		if (dst && lmosel) 
  4599 		{	
  4600 			if (e->state() & Qt::ShiftModifier)
  4601 				colorBranch (((BranchObj*)(dst))->getColor());
  4602 			else	
  4603 				colorSubtree (((BranchObj*)(dst))->getColor());
  4604 		} 
  4605 		return;
  4606 	}
  4607 
  4608 	// Have we been drawing a link?
  4609 	if (drawingLink)	
  4610 	{
  4611 		drawingLink=false;
  4612 		// Check if we are over another branch
  4613 		dst=mapCenter->findMapObj(p, NULL);
  4614 		if (dst && lmosel) 
  4615 		{	
  4616 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
  4617 			tmpXLink->updateXLink();
  4618 			tmpXLink->activate(); //FIXME savestate missing
  4619 			//saveStateComplete(QString("Activate xLink from %1 to %2").arg(getName(tmpXLink->getBegin())).arg(getName(tmpXLink->getEnd())) );	
  4620 		} else
  4621 		{
  4622 			delete(tmpXLink);
  4623 			tmpXLink=NULL;
  4624 		}
  4625 		return;
  4626 	}
  4627 	
  4628     // Have we been moving something?
  4629     if ( lmosel && movingObj ) 
  4630     {	
  4631 		FloatImageObj *fo=xelection.getFloatImage();
  4632 		if(fo)
  4633 		{
  4634 			// Moved FloatObj. Maybe we need to reposition
  4635 		    QString pold=qpointfToString(movingObj_orgRelPos);
  4636 		    QString pnow=qpointfToString(fo->getRelPos());
  4637 			saveState(
  4638 				fo,
  4639 				"moveRel "+pold,
  4640 				fo,
  4641 				"moveRel "+pnow,
  4642 				QString("Move %1 to relative position %2").arg(getName(fo)).arg(pnow));
  4643 
  4644 			fo->getParObj()->requestReposition();
  4645 			mapCenter->reposition();
  4646 		}	
  4647 
  4648 		// Check if we are over another branch, but ignore 
  4649 		// any found LMOs, which are FloatObjs
  4650 		dst=mapCenter->findMapObj(mapToScene(e->pos() ), lmosel);
  4651 
  4652 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
  4653 			dst=NULL;
  4654 		
  4655 		if (xelection.type() == Selection::MapCenter )
  4656 		{	
  4657 			// TODO: Check for problems if graphicsview is resized for 
  4658 			// undo/redo...
  4659             if (movingObj_orgPos != mapCenter->getAbsPos())
  4660             {
  4661                 QString pold=qpointfToString(movingObj_orgPos);
  4662                 QString pnow=qpointfToString(mapCenter->getAbsPos());
  4663                 saveState(
  4664                     fo,
  4665                     "move "+pold,
  4666                     fo,
  4667                     "move "+pnow,
  4668                     QString("Move mapcenter %1 to position %2").arg(getName(mapCenter)).arg(pnow));
  4669             }
  4670 		}
  4671 
  4672 		if (xelection.type() == Selection::Branch )
  4673 		{	// A branch was moved
  4674 			
  4675 			// save the position in case we link to mapcenter
  4676 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  4677 
  4678 			// Reset the temporary drawn link to the original one
  4679 			lmosel->unsetParObjTmp();
  4680 
  4681 			// For Redo we may need to save original selection
  4682 			QString preSelStr=lmosel->getSelectString();
  4683 
  4684 			copyingObj=false;	
  4685 			if (dst ) 
  4686 			{
  4687 				BranchObj* bsel=xelection.getBranch();
  4688 				BranchObj* bdst=(BranchObj*)dst;
  4689 
  4690 				QString preParStr=(bsel->getParObj())->getSelectString();
  4691 				QString preNum=QString::number (bsel->getNum(),10);
  4692 				QString preDstParStr;
  4693 
  4694 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  4695 				{	// Link above dst
  4696 					preDstParStr=dst->getParObj()->getSelectString();
  4697 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
  4698 				} else 
  4699 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  4700 				{
  4701 					// Link below dst
  4702 					preDstParStr=dst->getParObj()->getSelectString();
  4703 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
  4704 				} else	
  4705 				{	// Append to dst
  4706 					preDstParStr=dst->getSelectString();
  4707 					bsel->linkTo (bdst,-1);
  4708 					if (dst->getDepth()==0) bsel->move (savePos);
  4709 				} 
  4710 				QString postSelStr=lmosel->getSelectString();
  4711 				QString postNum=QString::number (bsel->getNum(),10);
  4712 
  4713 				QString undoCom="linkTo (\""+ 
  4714 					preParStr+ "\"," + preNum  +"," + 
  4715 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  4716 
  4717 				QString redoCom="linkTo (\""+ 
  4718 					preDstParStr + "\"," + postNum + "," +
  4719 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  4720 
  4721 				saveState (
  4722 					postSelStr,undoCom,
  4723 					preSelStr, redoCom,
  4724 					QString("Relink %1 to %2").arg(getName(bsel)).arg(getName(dst)) );
  4725 			} else
  4726 				if (lmosel->getDepth()==1)
  4727 				{
  4728 					// The select string might be different _after_ moving around.
  4729 					// Therefor reposition and then use string of old selection, too
  4730 					mapCenter->reposition();
  4731 
  4732                     QPointF rp(lmosel->getRelPos());
  4733                     if (rp != movingObj_orgRelPos)
  4734                     {
  4735                         QString ps=qpointfToString(rp);
  4736                         saveState(
  4737                             lmosel->getSelectString(), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  4738                             preSelStr, "moveRel "+ps, 
  4739                             QString("Move %1 to relative position %2").arg(getName(lmosel)).arg(ps));
  4740                     }
  4741 				}
  4742 			// Draw the original link, before selection was moved around
  4743 			mapCenter->reposition();
  4744 		}
  4745 		xelection.update();
  4746 		// Finally resize scene, if needed
  4747 		scene()->update();
  4748 		movingObj=NULL;		
  4749 
  4750 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  4751 		updateActions();
  4752 	} else 
  4753 		// maybe we moved View: set old cursor
  4754 		setCursor (Qt::ArrowCursor);
  4755     
  4756 }
  4757 
  4758 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  4759 {
  4760 	if (isSelectBlocked() ) 
  4761 	{
  4762 		e->ignore();
  4763 		return;
  4764 	}
  4765 
  4766 	if (e->button() == Qt::LeftButton )
  4767 	{
  4768 		QPointF p = mapToScene(e->pos());
  4769 		LinkableMapObj *lmo=mapCenter->findMapObj(p, NULL);
  4770 		if (lmo) {	// MapObj was found
  4771 			// First select the MapObj than edit heading
  4772 			xelection.select(lmo);
  4773 			mainWindow->editHeading();
  4774 		}
  4775 	}
  4776 }
  4777 
  4778 void MapEditor::resizeEvent (QResizeEvent* e)
  4779 {
  4780 	QGraphicsView::resizeEvent( e );
  4781 }
  4782 
  4783 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  4784 {
  4785 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  4786 	//	cerr << event->format(i) << endl;
  4787 
  4788 	if (event->mimeData()->hasImage())
  4789 		event->acceptProposedAction();
  4790 	else	
  4791 		if (event->mimeData()->hasUrls())
  4792 			event->acceptProposedAction();
  4793 }
  4794 
  4795 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  4796 {
  4797 }
  4798 
  4799 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  4800 {
  4801 	event->accept();
  4802 }
  4803 
  4804 void MapEditor::dropEvent(QDropEvent *event)
  4805 {
  4806 	BranchObj *sel=xelection.getBranch();
  4807 	if (sel)
  4808 	{
  4809 		if (debug)
  4810 			foreach (QString format,event->mimeData()->formats()) 
  4811 				cout << "MapEditor: Dropped format: "<<format.ascii()<<endl;
  4812 
  4813 
  4814 		QList <QUrl> uris;
  4815 		if (event->mimeData()->hasImage()) 
  4816 		{
  4817 			 QVariant imageData = event->mimeData()->imageData();
  4818 			 addFloatImageInt (qvariant_cast<QPixmap>(imageData));
  4819 		} else
  4820 		if (event->mimeData()->hasUrls())
  4821 			uris=event->mimeData()->urls();
  4822 
  4823 		if (uris.count()>0)
  4824 		{
  4825 			QStringList files;
  4826 			QString s;
  4827 			QString heading;
  4828 			BranchObj *bo;
  4829 			for (int i=0; i<uris.count();i++)
  4830 			{
  4831 				// Workaround to avoid adding empty branches
  4832 				if (!uris.at(i).toString().isEmpty())
  4833 				{
  4834 					bo=sel->addBranch();
  4835 					if (bo)
  4836 					{
  4837 						s=uris.at(i).toLocalFile();
  4838 						if (!s.isEmpty()) 
  4839 						{
  4840 						   QString file = QDir::fromNativeSeparators(s);
  4841 						   heading = QFileInfo(file).baseName();
  4842 						   files.append(file);
  4843 						   if (file.endsWith(".vym", false))
  4844 							   bo->setVymLink(file);
  4845 						   else
  4846 							   bo->setURL(uris.at(i).toString());
  4847 					   } else 
  4848 					   {
  4849 						   bo->setURL(uris.at(i).toString());
  4850 					   }
  4851 
  4852 					   if (!heading.isEmpty())
  4853 						   bo->setHeading(heading);
  4854 					   else
  4855 						   bo->setHeading(uris.at(i).toString());
  4856 					}
  4857 				}
  4858 			}
  4859 			mapCenter->reposition();
  4860 		}
  4861 	}	
  4862 	event->acceptProposedAction();
  4863 }
  4864 
  4865 void MapEditor::timerEvent(QTimerEvent *event)  //TODO animation
  4866 {
  4867     Q_UNUSED(event);
  4868 
  4869 	cout << "ME::timerEvent\n";
  4870 
  4871 	for (int i=0; i<animObjList.size(); ++i)
  4872 	{
  4873 		animObjList.at(i)->animate();
  4874 		((BranchObj*)animObjList.at(i))->move2RelPos (((BranchObj*)animObjList.at(i))->getRelPos() );
  4875 	}
  4876 	mapCenter->reposition();
  4877 }
  4878 
  4879 
  4880 void MapEditor::sendSelection()
  4881 {
  4882 	if (netstate!=Server) return;
  4883 	sendData (QString("select (\"%1\")").arg(xelection.getSelectString()) );
  4884 }
  4885 
  4886 void MapEditor::newServer()
  4887 {
  4888 	port=54321;
  4889 	sendCounter=0;
  4890     tcpServer = new QTcpServer(this);
  4891     if (!tcpServer->listen(QHostAddress::Any,port)) {
  4892         QMessageBox::critical(this, "vym server",
  4893                               QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
  4894         close();
  4895         return;
  4896     }
  4897 	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
  4898 	netstate=Server;
  4899 	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
  4900 }
  4901 
  4902 void MapEditor::connectToServer()
  4903 {
  4904 	port=54321;
  4905 	server="salam.suse.de";
  4906 	server="localhost";
  4907 	clientSocket = new QTcpSocket (this);
  4908 	clientSocket->abort();
  4909     clientSocket->connectToHost(server ,port);
  4910 	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
  4911     connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
  4912             this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
  4913 	netstate=Client;		
  4914 	cout<<"connected to "<<server.ascii()<<" port "<<port<<endl;
  4915 
  4916 	
  4917 }
  4918 
  4919 void MapEditor::newClient()
  4920 {
  4921     QTcpSocket *newClient = tcpServer->nextPendingConnection();
  4922     connect(newClient, SIGNAL(disconnected()),
  4923             newClient, SLOT(deleteLater()));
  4924 
  4925 	cout <<"ME::newClient  at "<<newClient->peerAddress().toString().ascii()<<endl;
  4926 
  4927 	clientList.append (newClient);
  4928 }
  4929 
  4930 
  4931 void MapEditor::sendData(const QString &s)
  4932 {
  4933 	if (clientList.size()==0) return;
  4934 
  4935 	// Create bytearray to send
  4936 	QByteArray block;
  4937     QDataStream out(&block, QIODevice::WriteOnly);
  4938     out.setVersion(QDataStream::Qt_4_0);
  4939 
  4940 	// Reserve some space for blocksize
  4941     out << (quint16)0;
  4942 
  4943 	// Write sendCounter
  4944     out << sendCounter++;
  4945 
  4946 	// Write data
  4947     out << s;
  4948 
  4949 	// Go back and write blocksize so far
  4950     out.device()->seek(0);
  4951     quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
  4952 	out << bs;
  4953 
  4954 	if (debug)
  4955 		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<s.ascii()<<endl;
  4956 
  4957 	for (int i=0; i<clientList.size(); ++i)
  4958 	{
  4959 		//cout << "Sending \""<<s.ascii()<<"\" to "<<clientList.at(i)->peerAddress().toString().ascii()<<endl;
  4960 		clientList.at(i)->write (block);
  4961 	}
  4962 }
  4963 
  4964 void MapEditor::readData ()
  4965 {
  4966 	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
  4967 	{
  4968 		if (debug)
  4969 			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
  4970 		quint16 recCounter;
  4971 		quint16 blockSize;
  4972 
  4973 		QDataStream in(clientSocket);
  4974 		in.setVersion(QDataStream::Qt_4_0);
  4975 
  4976 		in >> blockSize;
  4977 		in >> recCounter;
  4978 		
  4979 		QString t;
  4980 		in >>t;
  4981 		if (debug)
  4982 			cout << "  t="<<t.ascii()<<endl;
  4983 		parseAtom (t);
  4984 	}
  4985 	return;
  4986 }
  4987 
  4988 void MapEditor::displayNetworkError(QAbstractSocket::SocketError socketError)
  4989 {
  4990     switch (socketError) {
  4991     case QAbstractSocket::RemoteHostClosedError:
  4992         break;
  4993     case QAbstractSocket::HostNotFoundError:
  4994         QMessageBox::information(this, __VYM_NAME " Network client",
  4995                                  "The host was not found. Please check the "
  4996                                     "host name and port settings.");
  4997         break;
  4998     case QAbstractSocket::ConnectionRefusedError:
  4999         QMessageBox::information(this, __VYM_NAME " Network client",
  5000                                  "The connection was refused by the peer. "
  5001                                     "Make sure the fortune server is running, "
  5002                                     "and check that the host name and port "
  5003                                     "settings are correct.");
  5004         break;
  5005     default:
  5006         QMessageBox::information(this, __VYM_NAME " Network client",
  5007                                  QString("The following error occurred: %1.")
  5008                                  .arg(clientSocket->errorString()));
  5009     }
  5010 }
  5011 
  5012 void MapEditor::autosave()
  5013 {
  5014 	// Disable autosave, while we have gone back in history
  5015 	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
  5016 	if (redosAvail>0) return;
  5017 
  5018 
  5019 	if (mapUnsaved &&mapChanged && settings.value ("/mapeditor/autosave/use",true).toBool() )
  5020 		mainWindow->fileSave (this);
  5021 }
  5022 
  5023 
  5024 /*TODO not needed? void MapEditor::contentsDropEvent(QDropEvent *event) 
  5025 {
  5026 
  5027 		} else if (event->provides("application/x-moz-file-promise-url") && 
  5028 			 event->provides("application/x-moz-nativeimage")) 
  5029 		{
  5030 			// Contains url to the img src in unicode16
  5031 			QByteArray d = event->encodedData("application/x-moz-file-promise-url");
  5032 			QString url = QString((const QChar*)d.data(),d.size()/2);
  5033 			fetchImage(url);
  5034 			event->accept();
  5035 			update=true;
  5036 		} else if (event->provides ("text/uri-list"))
  5037 		{	// Uris provided e.g. by konqueror
  5038 			Q3UriDrag::decode (event,uris);
  5039 		} else if (event->provides ("_NETSCAPE_URL"))
  5040 		{	// Uris provided by Mozilla
  5041 		  QStringList l = QStringList::split("\n", event->encodedData("_NETSCAPE_URL"));
  5042 		  uris.append(l[0]);
  5043 		  heading = l[1];
  5044 		} else if (event->provides("text/html")) {
  5045 
  5046 		  // Handels text mime types
  5047 		  // Look like firefox allways handle text as unicode16 (2 bytes per char.)
  5048 		  QByteArray d = event->encodedData("text/html");
  5049 		  QString text;
  5050 		  if (isUnicode16(d)) 
  5051 		    text = QString((const QChar*)d.data(),d.size()/2);
  5052 		  else 
  5053 		    text = QString(d);
  5054 
  5055 		  textEditor->setText(text);
  5056 
  5057 		  event->accept();
  5058 		  update=true;
  5059 		} else if (event->provides("text/plain")) {
  5060 		  QByteArray d = event->encodedData("text/plain");
  5061 		  QString text;
  5062 		  if (isUnicode16(d))
  5063 		    text = QString((const QChar*)d.data(),d.size()/2);
  5064 		  else 
  5065 		    text = QString(d);
  5066 
  5067 		  textEditor->setText(text);
  5068 		  
  5069 		  event->accept();
  5070 		  update= true;
  5071 		}
  5072 
  5073 		*/
  5074 
  5075 
  5076 
  5077 bool isUnicode16(const QByteArray &d) 
  5078 {
  5079   // TODO: make more precise check for unicode 16.
  5080   // Guess unicode16 if any of second bytes are zero
  5081   unsigned int length = max(0,d.size()-2)/2;
  5082   for (unsigned int i = 0; i<length ; i++)
  5083     if (d.at(i*2+1)==0) return true;
  5084   return false;
  5085 }
  5086       
  5087 void MapEditor::addFloatImageInt (const QPixmap &img) 
  5088 {
  5089 	BranchObj *bo=xelection.getBranch();
  5090 	if (bo)
  5091   {
  5092 	FloatImageObj *fio=bo->addFloatImage();
  5093     fio->load(img);
  5094     fio->setOriginalFilename("No original filename (image added by dropevent)");	
  5095 	QString s=bo->getSelectString();
  5096 	saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio  );
  5097 	saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
  5098     mapCenter->reposition();
  5099     scene()->update();
  5100   }
  5101 }
  5102 
  5103 /*
  5104 
  5105 void MapEditor::imageDataFetched(const QByteArray &a, Q3NetworkOperation * / *nop* /) 
  5106 {
  5107   if (!imageBuffer) imageBuffer = new QBuffer();
  5108   if (!imageBuffer->isOpen()) {
  5109     imageBuffer->open(QIODevice::WriteOnly | QIODevice::Append);
  5110   }
  5111   imageBuffer->at(imageBuffer->at()+imageBuffer->writeBlock(a));
  5112 }
  5113 
  5114 
  5115 void MapEditor::imageDataFinished(Q3NetworkOperation *nop) 
  5116 {
  5117 	if (nop->state()==Q3NetworkProtocol::StDone) {
  5118 		QPixmap img(imageBuffer->buffer());
  5119 		addFloatImageInt (img);
  5120 	}
  5121 
  5122 	if (imageBuffer) {
  5123 		imageBuffer->close();
  5124 		if (imageBuffer) {
  5125 			imageBuffer->close();
  5126 			delete imageBuffer;
  5127 			imageBuffer = 0;
  5128 		}
  5129 	}
  5130 }
  5131 
  5132 void MapEditor::fetchImage(const QString &url) 
  5133 {
  5134   if (urlOperator) {
  5135     urlOperator->stop();
  5136     disconnect(urlOperator);
  5137     delete urlOperator;
  5138   }
  5139   
  5140   urlOperator = new Q3UrlOperator(url);
  5141   connect(urlOperator, SIGNAL(finished(Q3NetworkOperation *)), 
  5142 	  this, SLOT(imageDataFinished(Q3NetworkOperation*)));
  5143 
  5144   connect(urlOperator, SIGNAL(data(const QByteArray &, Q3NetworkOperation *)),
  5145 	  this, SLOT(imageDataFetched(const QByteArray &, Q3NetworkOperation *)));
  5146   urlOperator->get();
  5147 }
  5148 */
  5149