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