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