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