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