mapeditor.cpp
author insilmaril
Mon, 14 Jan 2008 16:27:00 +0000
changeset 647 bef71af3f6ab
parent 645 2abfdb7e85f4
child 650 65c5a0c28d20
permissions -rw-r--r--
Added french manual by Claude
     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 	LinkableMapObj *lmo=model->findObjBySelect(s);
  2689 
  2690 	// Finally select the found object
  2691 	if (lmo)
  2692 	{
  2693 		xelection.unselect();
  2694 		xelection.select(lmo);
  2695 		xelection.update();
  2696 		ensureSelectionVisible();
  2697 		sendSelection ();
  2698 		return true;
  2699 	} 
  2700 	return false;
  2701 }
  2702 
  2703 QString MapEditor::getSelectString()
  2704 {
  2705 	return xelection.getSelectString();
  2706 }
  2707 
  2708 void MapEditor::selectInt (LinkableMapObj *lmo)
  2709 {
  2710 	if (lmo && xelection.single()!= lmo && isSelectBlocked()==false )
  2711 	{
  2712 		xelection.select(lmo);
  2713 		xelection.update();
  2714 		sendSelection ();
  2715 	}	
  2716 }
  2717 
  2718 void MapEditor::selectNextBranchInt()
  2719 {
  2720 	// Increase number of branch
  2721 	LinkableMapObj *sel=xelection.single();
  2722 	if (sel)
  2723 	{
  2724 		QString s=sel->getSelectString();
  2725 		QString part;
  2726 		QString typ;
  2727 		QString num;
  2728 
  2729 		// Where am I? 
  2730 		part=s.section(",",-1);
  2731 		typ=part.left (3);
  2732 		num=part.right(part.length() - 3);
  2733 
  2734 		s=s.left (s.length() -num.length());
  2735 
  2736 		// Go to next lmo
  2737 		num=QString ("%1").arg(num.toUInt()+1);
  2738 
  2739 		s=s+num;
  2740 		
  2741 		// Try to select this one
  2742 		if (select (s)) return;
  2743 
  2744 		// We have no direct successor, 
  2745 		// try to increase the parental number in order to
  2746 		// find a successor with same depth
  2747 
  2748 		int d=xelection.single()->getDepth();
  2749 		int oldDepth=d;
  2750 		int i;
  2751 		bool found=false;
  2752 		bool b;
  2753 		while (!found && d>0)
  2754 		{
  2755 			s=s.section (",",0,d-1);
  2756 			// replace substring of current depth in s with "1"
  2757 			part=s.section(",",-1);
  2758 			typ=part.left (3);
  2759 			num=part.right(part.length() - 3);
  2760 
  2761 			if (d>1)
  2762 			{	
  2763 				// increase number of parent
  2764 				num=QString ("%1").arg(num.toUInt()+1);
  2765 				s=s.section (",",0,d-2) + ","+ typ+num;
  2766 			} else
  2767 			{
  2768 				// Special case, look at orientation
  2769 				if (xelection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
  2770 					num=QString ("%1").arg(num.toUInt()+1);
  2771 				else	
  2772 					num=QString ("%1").arg(num.toUInt()-1);
  2773 				s=typ+num;
  2774 			}	
  2775 
  2776 			if (select (s))
  2777 				// pad to oldDepth, select the first branch for each depth
  2778 				for (i=d;i<oldDepth;i++)
  2779 				{
  2780 					b=select (s);
  2781 					if (b)
  2782 					{	
  2783 						if ( xelection.getBranch()->countBranches()>0)
  2784 							s+=",bo:0";
  2785 						else	
  2786 							break;
  2787 					} else
  2788 						break;
  2789 				}	
  2790 
  2791 			// try to select the freshly built string
  2792 			found=select(s);
  2793 			d--;
  2794 		}
  2795 		return;
  2796 	}	
  2797 }
  2798 
  2799 void MapEditor::selectPrevBranchInt()
  2800 {
  2801 	// Decrease number of branch
  2802 	BranchObj *bo=xelection.getBranch();
  2803 	if (bo)
  2804 	{
  2805 		QString s=bo->getSelectString();
  2806 		QString part;
  2807 		QString typ;
  2808 		QString num;
  2809 
  2810 		// Where am I? 
  2811 		part=s.section(",",-1);
  2812 		typ=part.left (3);
  2813 		num=part.right(part.length() - 3);
  2814 
  2815 		s=s.left (s.length() -num.length());
  2816 
  2817 		int n=num.toInt()-1;
  2818 		
  2819 		// Go to next lmo
  2820 		num=QString ("%1").arg(n);
  2821 		s=s+num;
  2822 		
  2823 		// Try to select this one
  2824 		if (n>=0 && select (s)) return;
  2825 
  2826 		// We have no direct precessor, 
  2827 		// try to decrease the parental number in order to
  2828 		// find a precessor with same depth
  2829 
  2830 		int d=xelection.single()->getDepth();
  2831 		int oldDepth=d;
  2832 		int i;
  2833 		bool found=false;
  2834 		bool b;
  2835 		while (!found && d>0)
  2836 		{
  2837 			s=s.section (",",0,d-1);
  2838 			// replace substring of current depth in s with "1"
  2839 			part=s.section(",",-1);
  2840 			typ=part.left (3);
  2841 			num=part.right(part.length() - 3);
  2842 
  2843 			if (d>1)
  2844 			{
  2845 				// decrease number of parent
  2846 				num=QString ("%1").arg(num.toInt()-1);
  2847 				s=s.section (",",0,d-2) + ","+ typ+num;
  2848 			} else
  2849 			{
  2850 				// Special case, look at orientation
  2851 				if (xelection.single()->getOrientation()==LinkableMapObj::RightOfCenter)
  2852 					num=QString ("%1").arg(num.toInt()-1);
  2853 				else	
  2854 					num=QString ("%1").arg(num.toInt()+1);
  2855 				s=typ+num;
  2856 			}	
  2857 
  2858 			if (select(s))
  2859 				// pad to oldDepth, select the last branch for each depth
  2860 				for (i=d;i<oldDepth;i++)
  2861 				{
  2862 					b=select (s);
  2863 					if (b)
  2864 						if ( xelection.getBranch()->countBranches()>0)
  2865 							s+=",bo:"+ QString ("%1").arg( xelection.getBranch()->countBranches()-1 );
  2866 						else	
  2867 							break;
  2868 					else
  2869 						break;
  2870 				}	
  2871 			
  2872 			// try to select the freshly built string
  2873 			found=select(s);
  2874 			d--;
  2875 		}
  2876 		return;
  2877 	}	
  2878 }
  2879 
  2880 void MapEditor::selectUpperBranch()
  2881 {
  2882 	if (isSelectBlocked() ) return;
  2883 
  2884 	BranchObj *bo=xelection.getBranch();
  2885 	if (bo && xelection.type()==Selection::Branch)
  2886 	{
  2887 		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
  2888 			selectPrevBranchInt();
  2889 		else
  2890 			if (bo->getDepth()==1)
  2891 				selectNextBranchInt();
  2892 			else
  2893 				selectPrevBranchInt();
  2894 	}
  2895 }
  2896 
  2897 void MapEditor::selectLowerBranch()
  2898 {
  2899 	if (isSelectBlocked() ) return;
  2900 
  2901 	BranchObj *bo=xelection.getBranch();
  2902 	if (bo && xelection.type()==Selection::Branch)
  2903 		if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
  2904 			selectNextBranchInt();
  2905 		else
  2906 			if (bo->getDepth()==1)
  2907 				selectPrevBranchInt();
  2908 			else
  2909 				selectNextBranchInt();
  2910 }
  2911 
  2912 
  2913 void MapEditor::selectLeftBranch()
  2914 {
  2915 	if (isSelectBlocked() ) return;
  2916 
  2917 	BranchObj* bo;
  2918 	BranchObj* par;
  2919 	LinkableMapObj *sel=xelection.single();
  2920 	if (sel)
  2921 	{
  2922 		if (xelection.type()== Selection::MapCenter)
  2923 		{
  2924 			par=xelection.getBranch();
  2925 			bo=par->getLastSelectedBranch();
  2926 			if (bo)
  2927 			{
  2928 				// Workaround for reselecting on left and right side
  2929 				if (bo->getOrientation()==LinkableMapObj::RightOfCenter)
  2930 					bo=par->getLastBranch();
  2931 				if (bo)
  2932 				{
  2933 					bo=par->getLastBranch();
  2934 					xelection.select(bo);
  2935 					xelection.update();
  2936 					ensureSelectionVisible();
  2937 					sendSelection();
  2938 				}
  2939 			}	
  2940 		} else
  2941 		{
  2942 			par=(BranchObj*)(sel->getParObj());
  2943 			if (sel->getOrientation()==LinkableMapObj::RightOfCenter)
  2944 			{
  2945 				if (xelection.type() == Selection::Branch ||
  2946 					xelection.type() == Selection::FloatImage)
  2947 				{
  2948 					xelection.select(par);
  2949 					xelection.update();
  2950 					ensureSelectionVisible();
  2951 					sendSelection();
  2952 				}
  2953 			} else
  2954 			{
  2955 				if (xelection.type() == Selection::Branch )
  2956 				{
  2957 					bo=xelection.getBranch()->getLastSelectedBranch();
  2958 					if (bo) 
  2959 					{
  2960 						xelection.select(bo);
  2961 						xelection.update();
  2962 						ensureSelectionVisible();
  2963 					sendSelection();
  2964 					}
  2965 				}
  2966 			}
  2967 		}	
  2968 	}
  2969 }
  2970 
  2971 void MapEditor::selectRightBranch()
  2972 {
  2973 	if (isSelectBlocked() ) return;
  2974 
  2975 	BranchObj* bo;
  2976 	BranchObj* par;
  2977 	LinkableMapObj *sel=xelection.single();
  2978 	if (sel)
  2979 	{
  2980 		if (xelection.type()==Selection::MapCenter) 
  2981 		{
  2982 			par=xelection.getBranch();
  2983 			bo=par->getLastSelectedBranch();
  2984 			if (bo)
  2985 			{
  2986 				// Workaround for reselecting on left and right side
  2987 				if (bo->getOrientation()==LinkableMapObj::LeftOfCenter)
  2988 					bo=par->getFirstBranch();
  2989 				if (bo)
  2990 				{
  2991 					xelection.select(bo);
  2992 					xelection.update();
  2993 					ensureSelectionVisible();
  2994 					sendSelection();
  2995 				}
  2996 			}
  2997 		} else
  2998 		{
  2999 			par=(BranchObj*)(xelection.single()->getParObj());
  3000 			if (xelection.single()->getOrientation()==LinkableMapObj::LeftOfCenter)
  3001 			{
  3002 				if (xelection.type() == Selection::Branch ||
  3003 					xelection.type() == Selection::FloatImage)
  3004 				{
  3005 					xelection.select(par);
  3006 					xelection.update();
  3007 					ensureSelectionVisible();
  3008 					sendSelection();
  3009 				}
  3010 			} else
  3011 			{
  3012 				if (xelection.type()  == Selection::Branch) 
  3013 				{
  3014 					bo=xelection.getBranch()->getLastSelectedBranch();
  3015 					if (bo) 
  3016 					{
  3017 						xelection.select(bo);
  3018 						xelection.update();
  3019 						ensureSelectionVisible();
  3020 					sendSelection();
  3021 					}
  3022 				}
  3023 			}
  3024 		}
  3025 	}
  3026 }
  3027 
  3028 void MapEditor::selectFirstBranch()
  3029 {
  3030 	BranchObj *bo1=xelection.getBranch();
  3031 	BranchObj *bo2;
  3032 	BranchObj* par;
  3033 	if (bo1)
  3034 	{
  3035 		par=(BranchObj*)(bo1->getParObj());
  3036 		bo2=par->getFirstBranch();
  3037 		if (bo2) {
  3038 			xelection.select(bo2);
  3039 			xelection.update();
  3040 			ensureSelectionVisible();
  3041 			sendSelection();
  3042 		}
  3043 	}		
  3044 }
  3045 
  3046 void MapEditor::selectLastBranch()
  3047 {
  3048 	BranchObj *bo1=xelection.getBranch();
  3049 	BranchObj *bo2;
  3050 	BranchObj* par;
  3051 	if (bo1)
  3052 	{
  3053 		par=(BranchObj*)(bo1->getParObj());
  3054 		bo2=par->getLastBranch();
  3055 		if (bo2) 
  3056 		{
  3057 			xelection.select(bo2);
  3058 			xelection.update();
  3059 			ensureSelectionVisible();
  3060 			sendSelection();
  3061 		}
  3062 	}		
  3063 }
  3064 
  3065 void MapEditor::selectMapBackgroundImage ()
  3066 {
  3067 	Q3FileDialog *fd=new Q3FileDialog( this);
  3068 	fd->setMode (Q3FileDialog::ExistingFile);
  3069 	fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  3070 	ImagePreview *p =new ImagePreview (fd);
  3071 	fd->setContentsPreviewEnabled( TRUE );
  3072 	fd->setContentsPreview( p, p );
  3073 	fd->setPreviewMode( Q3FileDialog::Contents );
  3074 	fd->setCaption(vymName+" - " +tr("Load background image"));
  3075 	fd->setDir (lastImageDir);
  3076 	fd->show();
  3077 
  3078 	if ( fd->exec() == QDialog::Accepted )
  3079 	{
  3080 		// TODO selectMapBackgroundImg in QT4 use:	lastImageDir=fd->directory();
  3081 		lastImageDir=QDir (fd->dirPath());
  3082 		setMapBackgroundImage (fd->selectedFile());
  3083 	}
  3084 }	
  3085 
  3086 void MapEditor::setMapBackgroundImage (const QString &fn)	//FIXME missing savestate
  3087 {
  3088 	QColor oldcol=mapScene->backgroundBrush().color();
  3089 	/*
  3090 	saveState(
  3091 		selection,
  3092 		QString ("setMapBackgroundImage (%1)").arg(oldcol.name()),
  3093 		selection,
  3094 		QString ("setMapBackgroundImage (%1)").arg(col.name()),
  3095 		QString("Set background color of map to %1").arg(col.name()));
  3096 	*/	
  3097 	QBrush brush;
  3098 	brush.setTextureImage (QPixmap (fn));
  3099 	mapScene->setBackgroundBrush(brush);
  3100 }
  3101 
  3102 void MapEditor::selectMapBackgroundColor()
  3103 {
  3104 	QColor col = QColorDialog::getColor( mapScene->backgroundBrush().color(), this );
  3105 	if ( !col.isValid() ) return;
  3106 	setMapBackgroundColor( col );
  3107 }
  3108 
  3109 
  3110 void MapEditor::setMapBackgroundColor(QColor col)
  3111 {
  3112 	QColor oldcol=mapScene->backgroundBrush().color();
  3113 	saveState(
  3114 		QString ("setMapBackgroundColor (\"%1\")").arg(oldcol.name()),
  3115 		QString ("setMapBackgroundColor (\"%1\")").arg(col.name()),
  3116 		QString("Set background color of map to %1").arg(col.name()));
  3117 	mapScene->setBackgroundBrush(col);
  3118 }
  3119 
  3120 QColor MapEditor::getMapBackgroundColor()
  3121 {
  3122     return mapScene->backgroundBrush().color();
  3123 }
  3124 
  3125 QColor MapEditor::getCurrentHeadingColor()
  3126 {
  3127 	BranchObj *bo=xelection.getBranch();
  3128 	if (bo) return bo->getColor(); 
  3129 	
  3130 	QMessageBox::warning(0,tr("Warning"),tr("Can't get color of heading,\nthere's no branch selected"));
  3131 	return Qt::black;
  3132 }
  3133 
  3134 void MapEditor::colorBranch (QColor c)
  3135 {
  3136 	BranchObj *bo=xelection.getBranch();
  3137 	if (bo)
  3138 	{
  3139 		saveState(
  3140 			bo, 
  3141 			QString ("colorBranch (\"%1\")").arg(bo->getColor().name()),
  3142 			bo,
  3143 			QString ("colorBranch (\"%1\")").arg(c.name()),
  3144 			QString("Set color of %1 to %2").arg(getName(bo)).arg(c.name())
  3145 		);	
  3146 		bo->setColor(c); // color branch
  3147 	}
  3148 }
  3149 
  3150 void MapEditor::colorSubtree (QColor c)
  3151 {
  3152 	BranchObj *bo=xelection.getBranch();
  3153 	if (bo) 
  3154 	{
  3155 		saveStateChangingPart(
  3156 			bo, 
  3157 			bo,
  3158 			QString ("colorSubtree (\"%1\")").arg(c.name()),
  3159 			QString ("Set color of %1 and childs to %2").arg(getName(bo)).arg(c.name())
  3160 		);	
  3161 		bo->setColorSubtree (c); // color links, color childs
  3162 	}
  3163 }
  3164 
  3165 
  3166 void MapEditor::toggleStandardFlag(QString f)
  3167 {
  3168 	BranchObj *bo=xelection.getBranch();
  3169 	if (bo) 
  3170 	{
  3171 		QString u,r;
  3172 		if (bo->isSetStandardFlag(f))
  3173 		{
  3174 			r="unsetFlag";
  3175 			u="setFlag";
  3176 		}	
  3177 		else
  3178 		{
  3179 			u="unsetFlag";
  3180 			r="setFlag";
  3181 		}	
  3182 		saveState(
  3183 			bo,
  3184 			QString("%1 (\"%2\")").arg(u).arg(f), 
  3185 			bo,
  3186 			QString("%1 (\"%2\")").arg(r).arg(f),
  3187 			QString("Toggling standard flag \"%1\" of %2").arg(f).arg(getName(bo)));
  3188 		bo->toggleStandardFlag (f,mainWindow->useFlagGroups());
  3189 		xelection.update();
  3190 	}
  3191 }
  3192 
  3193 
  3194 BranchObj* MapEditor::findText (QString s, bool cs)
  3195 {
  3196 	QTextDocument::FindFlags flags=0;
  3197 	if (cs) flags=QTextDocument::FindCaseSensitively;
  3198 
  3199 	if (!itFind) 
  3200 	{	// Nothing found or new find process
  3201 		if (EOFind)
  3202 			// nothing found, start again
  3203 			EOFind=false;
  3204 		itFind=model->first();
  3205 	}	
  3206 	bool searching=true;
  3207 	bool foundNote=false;
  3208 	while (searching && !EOFind)
  3209 	{
  3210 		if (itFind)
  3211 		{
  3212 			// Searching in Note
  3213 			if (itFind->getNote().contains(s,cs))
  3214 			{
  3215 				if (xelection.single()!=itFind) 
  3216 				{
  3217 					xelection.select(itFind);
  3218 					ensureSelectionVisible();
  3219 				}
  3220 				if (textEditor->findText(s,flags)) 
  3221 				{
  3222 					searching=false;
  3223 					foundNote=true;
  3224 				}	
  3225 			}
  3226 			// Searching in Heading
  3227 			if (searching && itFind->getHeading().contains (s,cs) ) 
  3228 			{
  3229 				xelection.select(itFind);
  3230 				ensureSelectionVisible();
  3231 				searching=false;
  3232 			}
  3233 		}	
  3234 		if (!foundNote)
  3235 		{
  3236 			itFind=model->next(itFind);
  3237 			if (!itFind) EOFind=true;
  3238 		}
  3239 	}	
  3240 	if (!searching)
  3241 		return xelection.getBranch();
  3242 	else
  3243 		return NULL;
  3244 }
  3245 
  3246 void MapEditor::findReset()
  3247 {	// Necessary if text to find changes during a find process
  3248 	itFind=NULL;
  3249 	EOFind=false;
  3250 }
  3251 void MapEditor::setURL(const QString &url)
  3252 {
  3253 	BranchObj *bo=xelection.getBranch();
  3254 	if (bo)
  3255 	{
  3256 		QString oldurl=bo->getURL();
  3257 		bo->setURL (url);
  3258 		saveState (
  3259 			bo,
  3260 			QString ("setURL (\"%1\")").arg(oldurl),
  3261 			bo,
  3262 			QString ("setURL (\"%1\")").arg(url),
  3263 			QString ("set URL of %1 to %2").arg(getName(bo)).arg(url)
  3264 		);
  3265 		updateActions();
  3266 		model->reposition();
  3267 		xelection.update();
  3268 		ensureSelectionVisible();
  3269 	}
  3270 }	
  3271 
  3272 void MapEditor::editURL()
  3273 {
  3274 	BranchObj *bo=xelection.getBranch();
  3275 	if (bo)
  3276 	{		
  3277 		bool ok;
  3278 		QString text = QInputDialog::getText(
  3279 				"VYM", tr("Enter URL:"), QLineEdit::Normal,
  3280 				bo->getURL(), &ok, this );
  3281 		if ( ok) 
  3282 			// user entered something and pressed OK
  3283 			setURL (text);
  3284 	}
  3285 }
  3286 
  3287 void MapEditor::editLocalURL()
  3288 {
  3289 	BranchObj *bo=xelection.getBranch();
  3290 	if (bo)
  3291 	{		
  3292 		QStringList filters;
  3293 		filters <<"All files (*)";
  3294 		filters << tr("Text","Filedialog") + " (*.txt)";
  3295 		filters << tr("Spreadsheet","Filedialog") + " (*.odp,*.sxc)";
  3296 		filters << tr("Textdocument","Filedialog") +" (*.odw,*.sxw)";
  3297 		filters << tr("Images","Filedialog") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)";
  3298 		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Set URL to a local file"));
  3299 		fd->setFilters (filters);
  3300 		fd->setCaption(vymName+" - " +tr("Set URL to a local file"));
  3301 		fd->setDirectory (lastFileDir);
  3302 		if (! bo->getVymLink().isEmpty() )
  3303 			fd->selectFile( bo->getURL() );
  3304 		fd->show();
  3305 
  3306 		if ( fd->exec() == QDialog::Accepted )
  3307 		{
  3308 			lastFileDir=QDir (fd->directory().path());
  3309 			setURL (fd->selectedFile() );
  3310 		}
  3311 	}
  3312 }
  3313 
  3314 QString MapEditor::getURL()
  3315 {
  3316 	BranchObj *bo=xelection.getBranch();
  3317 	if (bo)
  3318 		return bo->getURL();
  3319 	else
  3320 		return "";
  3321 }
  3322 
  3323 QStringList MapEditor::getURLs()
  3324 {
  3325 	QStringList urls;
  3326 	BranchObj *bo=xelection.getBranch();
  3327 	if (bo)
  3328 	{		
  3329 		bo=bo->first();	
  3330 		while (bo) 
  3331 		{
  3332 			if (!bo->getURL().isEmpty()) urls.append( bo->getURL());
  3333 			bo=bo->next();
  3334 		}	
  3335 	}	
  3336 	return urls;
  3337 }
  3338 
  3339 
  3340 void MapEditor::editHeading2URL()
  3341 {
  3342 	BranchObj *bo=xelection.getBranch();
  3343 	if (bo)
  3344 		setURL (bo->getHeading());
  3345 }	
  3346 
  3347 void MapEditor::editBugzilla2URL()
  3348 {
  3349 	BranchObj *bo=xelection.getBranch();
  3350 	if (bo)
  3351 	{		
  3352 		QString url= "https://bugzilla.novell.com/show_bug.cgi?id="+bo->getHeading();
  3353 		setURL (url);
  3354 	}
  3355 }	
  3356 
  3357 void MapEditor::editFATE2URL()
  3358 {
  3359 	BranchObj *bo=xelection.getBranch();
  3360 	if (bo)
  3361 	{		
  3362 		QString url= "http://keeper.suse.de:8080/webfate/match/id?value=ID"+bo->getHeading();
  3363 		saveState(
  3364 			bo,
  3365 			"setURL (\""+bo->getURL()+"\")",
  3366 			bo,
  3367 			"setURL (\""+url+"\")",
  3368 			QString("Use heading of %1 as link to FATE").arg(getName(bo))
  3369 		);	
  3370 		bo->setURL (url);
  3371 		updateActions();
  3372 	}
  3373 }	
  3374 
  3375 void MapEditor::editVymLink()
  3376 {
  3377 	BranchObj *bo=xelection.getBranch();
  3378 	if (bo)
  3379 	{		
  3380 		QStringList filters;
  3381 		filters <<"VYM map (*.vym)";
  3382 		QFileDialog *fd=new QFileDialog( this,vymName+" - " +tr("Link to another map"));
  3383 		fd->setFilters (filters);
  3384 		fd->setCaption(vymName+" - " +tr("Link to another map"));
  3385 		fd->setDirectory (lastFileDir);
  3386 		if (! bo->getVymLink().isEmpty() )
  3387 			fd->selectFile( bo->getVymLink() );
  3388 		fd->show();
  3389 
  3390 		QString fn;
  3391 		if ( fd->exec() == QDialog::Accepted )
  3392 		{
  3393 			lastFileDir=QDir (fd->directory().path());
  3394 			saveState(
  3395 				bo,
  3396 				"setVymLink (\""+bo->getVymLink()+"\")",
  3397 				bo,
  3398 				"setVymLink (\""+fd->selectedFile()+"\")",
  3399 				QString("Set vymlink of %1 to %2").arg(getName(bo)).arg(fd->selectedFile())
  3400 			);	
  3401 			setVymLinkInt (fd->selectedFile() );
  3402 		}
  3403 	}
  3404 }
  3405 
  3406 void MapEditor::deleteVymLink()
  3407 {
  3408 	BranchObj *bo=xelection.getBranch();
  3409 	if (bo)
  3410 	{		
  3411 		saveState(
  3412 			bo,
  3413 			"setVymLink (\""+bo->getVymLink()+"\")",
  3414 			bo,
  3415 			"setVymLink (\"\")",
  3416 			QString("Unset vymlink of %1").arg(getName(bo))
  3417 		);	
  3418 		bo->setVymLink ("" );
  3419 		updateActions();
  3420 		model->reposition();
  3421 		scene()->update();
  3422 	}
  3423 }
  3424 
  3425 void MapEditor::setHideExport(bool b)
  3426 {
  3427 	BranchObj *bo=xelection.getBranch();
  3428 	if (bo)
  3429 	{
  3430 		bo->setHideInExport (b);
  3431 		QString u= b ? "false" : "true";
  3432 		QString r=!b ? "false" : "true";
  3433 		
  3434 		saveState(
  3435 			bo,
  3436 			QString ("setHideExport (%1)").arg(u),
  3437 			bo,
  3438 			QString ("setHideExport (%1)").arg(r),
  3439 			QString ("Set HideExport flag of %1 to %2").arg(getName(bo)).arg (r)
  3440 		);	
  3441 		updateActions();
  3442 		model->reposition();
  3443 		xelection.update();
  3444 		scene()->update();
  3445 	}
  3446 }
  3447 
  3448 void MapEditor::toggleHideExport()
  3449 {
  3450 	BranchObj *bo=xelection.getBranch();
  3451 	if (bo)
  3452 		setHideExport ( !bo->hideInExport() );
  3453 }
  3454 
  3455 QString MapEditor::getVymLink()
  3456 {
  3457 	BranchObj *bo=xelection.getBranch();
  3458 	if (bo)
  3459 		return bo->getVymLink();
  3460 	else	
  3461 		return "";
  3462 	
  3463 }
  3464 
  3465 QStringList MapEditor::getVymLinks()
  3466 {
  3467 	QStringList links;
  3468 	BranchObj *bo=xelection.getBranch();
  3469 	if (bo)
  3470 	{		
  3471 		bo=bo->first();	
  3472 		while (bo) 
  3473 		{
  3474 			if (!bo->getVymLink().isEmpty()) links.append( bo->getVymLink());
  3475 			bo=bo->next();
  3476 		}	
  3477 	}	
  3478 	return links;
  3479 }
  3480 
  3481 
  3482 void MapEditor::deleteKeepChilds()
  3483 {
  3484 	BranchObj *bo=xelection.getBranch();
  3485 	BranchObj *par;
  3486 	if (bo)
  3487 	{
  3488 		par=(BranchObj*)(bo->getParObj());
  3489 		QPointF p=bo->getRelPos();
  3490 		saveStateChangingPart(
  3491 			bo->getParObj(),
  3492 			bo,
  3493 			"deleteKeepChilds ()",
  3494 			QString("Remove %1 and keep its childs").arg(getName(bo))
  3495 		);
  3496 
  3497 		QString sel=bo->getSelectString();
  3498 		unselect();
  3499 		par->removeBranchHere(bo);
  3500 		model->reposition();
  3501 		select (sel);
  3502 		xelection.getBranch()->move2RelPos (p);
  3503 		model->reposition();
  3504 	}	
  3505 }
  3506 
  3507 void MapEditor::deleteChilds()
  3508 {
  3509 	BranchObj *bo=xelection.getBranch();
  3510 	if (bo)
  3511 	{		
  3512 		saveStateChangingPart(
  3513 			bo, 
  3514 			bo,
  3515 			"deleteChilds ()",
  3516 			QString( "Remove childs of branch %1").arg(getName(bo))
  3517 		);
  3518 		bo->removeChilds();
  3519 		model->reposition();
  3520 	}	
  3521 }
  3522 
  3523 void MapEditor::editMapInfo()
  3524 {
  3525 	ExtraInfoDialog dia;
  3526 	dia.setMapName (getFileName() );
  3527 	dia.setAuthor (model->getAuthor() );
  3528 	dia.setComment(model->getComment() );
  3529 
  3530 	// Calc some stats
  3531 	QString stats;
  3532     stats+=tr("%1 items on map\n","Info about map").arg (mapScene->items().size(),6);
  3533 
  3534 	uint b=0;
  3535 	uint f=0;
  3536 	uint n=0;
  3537 	uint xl=0;
  3538 	BranchObj *bo;
  3539 	bo=model->first();
  3540 	while (bo) 
  3541 	{
  3542 		if (!bo->getNote().isEmpty() ) n++;
  3543 		f+= bo->countFloatImages();
  3544 		b++;
  3545 		xl+=bo->countXLinks();
  3546 		bo=model->next(bo);
  3547 	}
  3548     stats+=QString ("%1 branches\n").arg (b-1,6);
  3549     stats+=QString ("%1 xLinks \n").arg (xl,6);
  3550     stats+=QString ("%1 notes\n").arg (n,6);
  3551     stats+=QString ("%1 images\n").arg (f,6);
  3552 	dia.setStats (stats);
  3553 
  3554 	// Finally show dialog
  3555 	if (dia.exec() == QDialog::Accepted)
  3556 	{
  3557 		setMapAuthor (dia.getAuthor() );
  3558 		setMapComment (dia.getComment() );
  3559 	}
  3560 }
  3561 
  3562 void MapEditor::ensureSelectionVisible()
  3563 {
  3564 	LinkableMapObj *lmo=xelection.single();
  3565 	if (lmo) ensureVisible (lmo->getBBox() );
  3566 	
  3567 }
  3568 
  3569 void MapEditor::updateSelection()
  3570 {
  3571 	// Tell selection to update geometries
  3572 	xelection.update();
  3573 }
  3574 
  3575 void MapEditor::updateActions()
  3576 {
  3577 	// Tell mainwindow to update states of actions
  3578 	mainWindow->updateActions();
  3579 	// TODO maybe don't update if blockReposition is set
  3580 }
  3581 
  3582 void MapEditor::updateNoteFlag()
  3583 {
  3584 	setChanged();
  3585 	BranchObj *bo=xelection.getBranch();
  3586 	if (bo) 
  3587 	{
  3588 		bo->updateNoteFlag();
  3589 		mainWindow->updateActions();
  3590 	}	
  3591 }
  3592 
  3593 void MapEditor::setMapAuthor (const QString &s)
  3594 {
  3595 	saveState (
  3596 		QString ("setMapAuthor (\"%1\")").arg(model->getAuthor()),
  3597 		QString ("setMapAuthor (\"%1\")").arg(s),
  3598 		QString ("Set author of map to \"%1\"").arg(s)
  3599 	);
  3600 	model->setAuthor (s);
  3601 }
  3602 
  3603 void MapEditor::setMapComment (const QString &s)
  3604 {
  3605 	saveState (
  3606 		QString ("setMapComment (\"%1\")").arg(model->getComment()),
  3607 		QString ("setMapComment (\"%1\")").arg(s),
  3608 		QString ("Set comment of map")
  3609 	);
  3610 	model->setComment (s);
  3611 }
  3612 
  3613 void MapEditor::setMapLinkStyle (const QString & s)
  3614 {
  3615 	QString snow;
  3616 	if (linkstyle==LinkableMapObj::Line)
  3617 		snow="StyleLine";
  3618 	else if (linkstyle==LinkableMapObj::Parabel)
  3619 		snow="StyleParabel";
  3620 	else if (linkstyle==LinkableMapObj::PolyLine)
  3621 		snow="StylePolyLine";
  3622 	else if (linkstyle==LinkableMapObj::PolyParabel)
  3623 		snow="StyleParabel";
  3624 
  3625 	saveState (
  3626 		QString("setMapLinkStyle (\"%1\")").arg(s),
  3627 		QString("setMapLinkStyle (\"%1\")").arg(snow),
  3628 		QString("Set map link style (\"%1\")").arg(s)
  3629 	);	
  3630 
  3631 	if (s=="StyleLine")
  3632 		linkstyle=LinkableMapObj::Line;
  3633 	else if (s=="StyleParabel")
  3634 		linkstyle=LinkableMapObj::Parabel;
  3635 	else if (s=="StylePolyLine")
  3636 		linkstyle=LinkableMapObj::PolyLine;
  3637 	else	
  3638 		linkstyle=LinkableMapObj::PolyParabel;
  3639 
  3640 	BranchObj *bo;
  3641 	bo=model->first();
  3642 	bo=model->next(bo);
  3643 	while (bo) 
  3644 	{
  3645 		bo->setLinkStyle(bo->getDefLinkStyle());
  3646 		bo=model->next(bo);
  3647 	}
  3648 	model->reposition();
  3649 }
  3650 
  3651 LinkableMapObj::Style MapEditor::getMapLinkStyle ()
  3652 {
  3653 	return linkstyle;
  3654 }	
  3655 
  3656 void MapEditor::setMapDefLinkColor(QColor c)
  3657 {
  3658 	defLinkColor=c;
  3659 	BranchObj *bo;
  3660 	bo=model->first();
  3661 	while (bo) 
  3662 	{
  3663 		bo->setLinkColor();
  3664 		bo=model->next(bo);
  3665 	}
  3666 	updateActions();
  3667 }
  3668 
  3669 void MapEditor::setMapLinkColorHintInt()
  3670 {
  3671 	// called from setMapLinkColorHint(lch) or at end of parse
  3672 	BranchObj *bo;
  3673 	bo=model->first();
  3674 	while (bo) 
  3675 	{
  3676 		bo->setLinkColor();
  3677 		bo=model->next(bo);
  3678 	}
  3679 }
  3680 
  3681 void MapEditor::setMapLinkColorHint(LinkableMapObj::ColorHint lch)
  3682 {
  3683 	linkcolorhint=lch;
  3684 	setMapLinkColorHintInt();
  3685 }
  3686 
  3687 void MapEditor::toggleMapLinkColorHint()
  3688 {
  3689 	if (linkcolorhint==LinkableMapObj::HeadingColor)
  3690 		linkcolorhint=LinkableMapObj::DefaultColor;
  3691 	else	
  3692 		linkcolorhint=LinkableMapObj::HeadingColor;
  3693 	BranchObj *bo;
  3694 	bo=model->first();
  3695 	while (bo) 
  3696 	{
  3697 		bo->setLinkColor();
  3698 		bo=model->next(bo);
  3699 	}
  3700 }
  3701 
  3702 LinkableMapObj::ColorHint MapEditor::getMapLinkColorHint()
  3703 {
  3704 	return linkcolorhint;
  3705 }
  3706 
  3707 QColor MapEditor::getMapDefLinkColor()
  3708 {
  3709 	return defLinkColor;
  3710 }
  3711 
  3712 void MapEditor::setMapDefXLinkColor(QColor col)
  3713 {
  3714 	defXLinkColor=col;
  3715 }
  3716 
  3717 QColor MapEditor::getMapDefXLinkColor()
  3718 {
  3719 	return defXLinkColor;
  3720 }
  3721 
  3722 void MapEditor::setMapDefXLinkWidth (int w)
  3723 {
  3724 	defXLinkWidth=w;
  3725 }
  3726 
  3727 int MapEditor::getMapDefXLinkWidth()
  3728 {
  3729 	return defXLinkWidth;
  3730 }
  3731 
  3732 void MapEditor::selectMapLinkColor()
  3733 {
  3734 	QColor col = QColorDialog::getColor( defLinkColor, this );
  3735 	if ( !col.isValid() ) return;
  3736 	saveState (
  3737 		QString("setMapDefLinkColor (\"%1\")").arg(getMapDefLinkColor().name()),
  3738 		QString("setMapDefLinkColor (\"%1\")").arg(col.name()),
  3739 		QString("Set map link color to %1").arg(col.name())
  3740 	);
  3741 	setMapDefLinkColor( col );
  3742 }
  3743 
  3744 void MapEditor::selectMapSelectionColor()
  3745 {
  3746 	QColor col = QColorDialog::getColor( defLinkColor, this );
  3747 	setSelectionColor (col);
  3748 }
  3749 
  3750 void MapEditor::setSelectionColorInt (QColor col)
  3751 {
  3752 	if ( !col.isValid() ) return;
  3753 	xelection.setColor (col);
  3754 }
  3755 
  3756 void MapEditor::setSelectionColor(QColor col)
  3757 {
  3758 	if ( !col.isValid() ) return;
  3759 	saveState (
  3760 		QString("setSelectionColor (%1)").arg(xelection.getColor().name()),
  3761 		QString("setSelectionColor (%1)").arg(col.name()),
  3762 		QString("Set color of selection box to %1").arg(col.name())
  3763 	);
  3764 	setSelectionColorInt (col);
  3765 }
  3766 
  3767 QColor MapEditor::getSelectionColor()
  3768 {
  3769 	return xelection.getColor();
  3770 }
  3771 
  3772 bool MapEditor::scrollBranch(BranchObj *bo)
  3773 {
  3774 	if (bo)
  3775 	{
  3776 		if (bo->isScrolled()) return false;
  3777 		if (bo->countBranches()==0) return false;
  3778 		if (bo->getDepth()==0) return false;
  3779 		QString u,r;
  3780 		r="scroll";
  3781 		u="unscroll";
  3782 		saveState(
  3783 			bo,
  3784 			QString ("%1 ()").arg(u),
  3785 			bo,
  3786 			QString ("%1 ()").arg(r),
  3787 			QString ("%1 %2").arg(r).arg(getName(bo))
  3788 		);
  3789 		bo->toggleScroll();
  3790 		xelection.update();
  3791 		scene()->update();
  3792 		return true;
  3793 	}	
  3794 	return false;
  3795 }
  3796 
  3797 bool MapEditor::unscrollBranch(BranchObj *bo)
  3798 {
  3799 	if (bo)
  3800 	{
  3801 		if (!bo->isScrolled()) return false;
  3802 		if (bo->countBranches()==0) return false;
  3803 		if (bo->getDepth()==0) return false;
  3804 		QString u,r;
  3805 		u="scroll";
  3806 		r="unscroll";
  3807 		saveState(
  3808 			bo,
  3809 			QString ("%1 ()").arg(u),
  3810 			bo,
  3811 			QString ("%1 ()").arg(r),
  3812 			QString ("%1 %2").arg(r).arg(getName(bo))
  3813 		);
  3814 		bo->toggleScroll();
  3815 		xelection.update();
  3816 		scene()->update();
  3817 		return true;
  3818 	}	
  3819 	return false;
  3820 }
  3821 
  3822 void MapEditor::toggleScroll()
  3823 {
  3824 	BranchObj *bo=xelection.getBranch();
  3825 	if (xelection.type()==Selection::Branch )
  3826 	{
  3827 		if (bo->isScrolled())
  3828 			unscrollBranch (bo);
  3829 		else
  3830 			scrollBranch (bo);
  3831 	}
  3832 }
  3833 
  3834 void MapEditor::unscrollChilds() 
  3835 {
  3836 	BranchObj *bo=xelection.getBranch();
  3837 	if (bo)
  3838 	{
  3839 		bo->first();
  3840 		while (bo) 
  3841 		{
  3842 			if (bo->isScrolled()) unscrollBranch (bo);
  3843 			bo=bo->next();
  3844 		}
  3845 	}	
  3846 }
  3847 
  3848 FloatImageObj* MapEditor::loadFloatImageInt (QString fn)
  3849 {
  3850 	BranchObj *bo=xelection.getBranch();
  3851 	if (bo)
  3852 	{
  3853 		FloatImageObj *fio;
  3854 		bo->addFloatImage();
  3855 		fio=bo->getLastFloatImage();
  3856 		fio->load(fn);
  3857 		model->reposition();
  3858 		scene()->update();
  3859 		return fio;
  3860 	}
  3861 	return NULL;
  3862 }	
  3863 
  3864 void MapEditor::loadFloatImage ()
  3865 {
  3866 	BranchObj *bo=xelection.getBranch();
  3867 	if (bo)
  3868 	{
  3869 
  3870 		Q3FileDialog *fd=new Q3FileDialog( this);
  3871 		fd->setMode (Q3FileDialog::ExistingFiles);
  3872 		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  3873 		ImagePreview *p =new ImagePreview (fd);
  3874 		fd->setContentsPreviewEnabled( TRUE );
  3875 		fd->setContentsPreview( p, p );
  3876 		fd->setPreviewMode( Q3FileDialog::Contents );
  3877 		fd->setCaption(vymName+" - " +tr("Load image"));
  3878 		fd->setDir (lastImageDir);
  3879 		fd->show();
  3880 
  3881 		if ( fd->exec() == QDialog::Accepted )
  3882 		{
  3883 			// TODO loadFIO in QT4 use:	lastImageDir=fd->directory();
  3884 			lastImageDir=QDir (fd->dirPath());
  3885 			QString s;
  3886 			FloatImageObj *fio;
  3887 			for (int j=0; j<fd->selectedFiles().count(); j++)
  3888 			{
  3889 				s=fd->selectedFiles().at(j);
  3890 				fio=loadFloatImageInt (s);
  3891 				if (fio)
  3892 					saveState(
  3893 						(LinkableMapObj*)fio,
  3894 						"delete ()",
  3895 						bo, 
  3896 						QString ("loadImage (%1)").arg(s ),
  3897 						QString("Add image %1 to %2").arg(s).arg(getName(bo))
  3898 					);
  3899 				else
  3900 					// TODO loadFIO error handling
  3901 					qWarning ("Failed to load "+s);
  3902 			}
  3903 		}
  3904 		delete (p);
  3905 		delete (fd);
  3906 	}
  3907 }
  3908 
  3909 void MapEditor::saveFloatImageInt  (FloatImageObj *fio, const QString &type, const QString &fn)
  3910 {
  3911 	fio->save (fn,type);
  3912 }
  3913 
  3914 void MapEditor::saveFloatImage ()
  3915 {
  3916 	FloatImageObj *fio=xelection.getFloatImage();
  3917 	if (fio)
  3918 	{
  3919 		QFileDialog *fd=new QFileDialog( this);
  3920 		fd->setFilters (imageIO.getFilters());
  3921 		fd->setCaption(vymName+" - " +tr("Save image"));
  3922 		fd->setFileMode( QFileDialog::AnyFile );
  3923 		fd->setDirectory (lastImageDir);
  3924 //		fd->setSelection (fio->getOriginalFilename());
  3925 		fd->show();
  3926 
  3927 		QString fn;
  3928 		if ( fd->exec() == QDialog::Accepted && fd->selectedFiles().count()==1)
  3929 		{
  3930 			fn=fd->selectedFiles().at(0);
  3931 			if (QFile (fn).exists() )
  3932 			{
  3933 				QMessageBox mb( vymName,
  3934 					tr("The file %1 exists already.\n"
  3935 					"Do you want to overwrite it?").arg(fn),
  3936 				QMessageBox::Warning,
  3937 				QMessageBox::Yes | QMessageBox::Default,
  3938 				QMessageBox::Cancel | QMessageBox::Escape,
  3939 				QMessageBox::QMessageBox::NoButton );
  3940 
  3941 				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
  3942 				mb.setButtonText( QMessageBox::No, tr("Cancel"));
  3943 				switch( mb.exec() ) 
  3944 				{
  3945 					case QMessageBox::Yes:
  3946 						// save 
  3947 						break;;
  3948 					case QMessageBox::Cancel:
  3949 						// do nothing
  3950 						delete (fd);
  3951 						return;
  3952 						break;
  3953 				}
  3954 			}
  3955 			saveFloatImageInt (fio,fd->selectedFilter(),fn );
  3956 		}
  3957 		delete (fd);
  3958 	}
  3959 }
  3960 
  3961 void MapEditor::setFrameType(const FrameObj::FrameType &t)
  3962 {
  3963 	BranchObj *bo=xelection.getBranch();
  3964 	if (bo)
  3965 	{
  3966 		QString s=bo->getFrameTypeName();
  3967 		bo->setFrameType (t);
  3968 		saveState (bo, QString("setFrameType (\"%1\")").arg(s),
  3969 			bo, QString ("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),QString ("set type of frame to %1").arg(s));
  3970 		model->reposition();
  3971 		bo->updateLink();
  3972 	}
  3973 }
  3974 
  3975 void MapEditor::setFrameType(const QString &s)	
  3976 {
  3977 	BranchObj *bo=xelection.getBranch();
  3978 	if (bo)
  3979 	{
  3980 		saveState (bo, QString("setFrameType (\"%1\")").arg(bo->getFrameTypeName()),
  3981 			bo, QString ("setFrameType (\"%1\")").arg(s),QString ("set type of frame to %1").arg(s));
  3982 		bo->setFrameType (s);
  3983 		model->reposition();
  3984 		bo->updateLink();
  3985 	}
  3986 }
  3987 
  3988 void MapEditor::setFramePenColor(const QColor &c)	
  3989 {
  3990 	BranchObj *bo=xelection.getBranch();
  3991 	if (bo)
  3992 	{
  3993 		saveState (bo, QString("setFramePenColor (\"%1\")").arg(bo->getFramePenColor().name() ),
  3994 			bo, QString ("setFramePenColor (\"%1\")").arg(c.name() ),QString ("set pen color of frame to %1").arg(c.name() ));
  3995 		bo->setFramePenColor (c);
  3996 	}	
  3997 }
  3998 
  3999 void MapEditor::setFrameBrushColor(const QColor &c)	
  4000 {
  4001 	BranchObj *bo=xelection.getBranch();
  4002 	if (bo)
  4003 	{
  4004 		saveState (bo, QString("setFrameBrushColor (\"%1\")").arg(bo->getFrameBrushColor().name() ),
  4005 			bo, QString ("setFrameBrushColor (\"%1\")").arg(c.name() ),QString ("set brush color of frame to %1").arg(c.name() ));
  4006 		bo->setFrameBrushColor (c);
  4007 	}	
  4008 }
  4009 
  4010 void MapEditor::setFramePadding (const int &i)
  4011 {
  4012 	BranchObj *bo=xelection.getBranch();
  4013 	if (bo)
  4014 	{
  4015 		saveState (bo, QString("setFramePadding (\"%1\")").arg(bo->getFramePadding() ),
  4016 			bo, QString ("setFramePadding (\"%1\")").arg(i),QString ("set brush color of frame to %1").arg(i));
  4017 		bo->setFramePadding (i);
  4018 		model->reposition();
  4019 		bo->updateLink();
  4020 	}	
  4021 }
  4022 
  4023 void MapEditor::setFrameBorderWidth(const int &i)
  4024 {
  4025 	BranchObj *bo=xelection.getBranch();
  4026 	if (bo)
  4027 	{
  4028 		saveState (bo, QString("setFrameBorderWidth (\"%1\")").arg(bo->getFrameBorderWidth() ),
  4029 			bo, QString ("setFrameBorderWidth (\"%1\")").arg(i),QString ("set border width of frame to %1").arg(i));
  4030 		bo->setFrameBorderWidth (i);
  4031 		model->reposition();
  4032 		bo->updateLink();
  4033 	}	
  4034 }
  4035 
  4036 void MapEditor::setIncludeImagesVer(bool b)	
  4037 {
  4038 	BranchObj *bo=xelection.getBranch();
  4039 	if (bo)
  4040 	{
  4041 		QString u= b ? "false" : "true";
  4042 		QString r=!b ? "false" : "true";
  4043 		
  4044 		saveState(
  4045 			bo,
  4046 			QString("setIncludeImagesVertically (%1)").arg(u),
  4047 			bo, 
  4048 			QString("setIncludeImagesVertically (%1)").arg(r),
  4049 			QString("Include images vertically in %1").arg(getName(bo))
  4050 		);	
  4051 		bo->setIncludeImagesVer(b);
  4052 		model->reposition();
  4053 	}	
  4054 }
  4055 
  4056 void MapEditor::setIncludeImagesHor(bool b)	
  4057 {
  4058 	BranchObj *bo=xelection.getBranch();
  4059 	if (bo)
  4060 	{
  4061 		QString u= b ? "false" : "true";
  4062 		QString r=!b ? "false" : "true";
  4063 		
  4064 		saveState(
  4065 			bo,
  4066 			QString("setIncludeImagesHorizontally (%1)").arg(u),
  4067 			bo, 
  4068 			QString("setIncludeImagesHorizontally (%1)").arg(r),
  4069 			QString("Include images horizontally in %1").arg(getName(bo))
  4070 		);	
  4071 		bo->setIncludeImagesHor(b);
  4072 		model->reposition();
  4073 	}	
  4074 }
  4075 
  4076 void MapEditor::setHideLinkUnselected (bool b)
  4077 {
  4078 	LinkableMapObj *sel=xelection.single();
  4079 	if (sel &&
  4080 		(xelection.type() == Selection::Branch || 
  4081 		xelection.type() == Selection::MapCenter  ||
  4082 		xelection.type() == Selection::FloatImage ))
  4083 	{
  4084 		QString u= b ? "false" : "true";
  4085 		QString r=!b ? "false" : "true";
  4086 		
  4087 		saveState(
  4088 			sel,
  4089 			QString("setHideLinkUnselected (%1)").arg(u),
  4090 			sel, 
  4091 			QString("setHideLinkUnselected (%1)").arg(r),
  4092 			QString("Hide link of %1 if unselected").arg(getName(sel))
  4093 		);	
  4094 		sel->setHideLinkUnselected(b);
  4095 	}
  4096 }
  4097 
  4098 void MapEditor::importDirInt(BranchObj *dst, QDir d)
  4099 {
  4100 	BranchObj *bo=xelection.getBranch();
  4101 	if (bo)
  4102 	{
  4103 		// Traverse directories
  4104 		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
  4105 		QFileInfoList list = d.entryInfoList();
  4106 		QFileInfo fi;
  4107 
  4108 		for (int i = 0; i < list.size(); ++i) 
  4109 		{
  4110 			fi=list.at(i);
  4111 			if (fi.fileName() != "." && fi.fileName() != ".." )
  4112 			{
  4113 				dst->addBranch();
  4114 				bo=dst->getLastBranch();
  4115 				bo->setHeading (fi.fileName() );
  4116 				bo->setColor (QColor("blue"));
  4117 				bo->toggleScroll();
  4118 				if ( !d.cd(fi.fileName()) ) 
  4119 					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
  4120 				else 
  4121 				{
  4122 					// Recursively add subdirs
  4123 					importDirInt (bo,d);
  4124 					d.cdUp();
  4125 				}
  4126 			}	
  4127 		}		
  4128 		// Traverse files
  4129 		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
  4130 		list = d.entryInfoList();
  4131 
  4132 		for (int i = 0; i < list.size(); ++i) 
  4133 		{
  4134 			fi=list.at(i);
  4135 			dst->addBranch();
  4136 			bo=dst->getLastBranch();
  4137 			bo->setHeading (fi.fileName() );
  4138 			bo->setColor (QColor("black"));
  4139 			if (fi.fileName().right(4) == ".vym" )
  4140 				bo->setVymLink (fi.filePath());
  4141 		}	
  4142 	}		
  4143 }
  4144 
  4145 void MapEditor::importDirInt (const QString &s)
  4146 {
  4147 	BranchObj *bo=xelection.getBranch();
  4148 	if (bo)
  4149 	{
  4150 		saveStateChangingPart (bo,bo,QString ("importDir (\"%1\")").arg(s),QString("Import directory structure from %1").arg(s));
  4151 
  4152 		QDir d(s);
  4153 		importDirInt (bo,d);
  4154 	}
  4155 }	
  4156 
  4157 void MapEditor::importDir()
  4158 {
  4159 	BranchObj *bo=xelection.getBranch();
  4160 	if (bo)
  4161 	{
  4162 		QStringList filters;
  4163 		filters <<"VYM map (*.vym)";
  4164 		QFileDialog *fd=new QFileDialog( this,vymName+ " - " +tr("Choose directory structure to import"));
  4165 		fd->setMode (QFileDialog::DirectoryOnly);
  4166 		fd->setFilters (filters);
  4167 		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
  4168 		fd->show();
  4169 
  4170 		QString fn;
  4171 		if ( fd->exec() == QDialog::Accepted )
  4172 		{
  4173 			importDirInt (fd->selectedFile() );
  4174 			model->reposition();
  4175 			scene()->update();
  4176 		}
  4177 	}	
  4178 }
  4179 
  4180 void MapEditor::followXLink(int i)
  4181 {
  4182 	BranchObj *bo=xelection.getBranch();
  4183 	if (bo)
  4184 	{
  4185 		bo=bo->XLinkTargetAt(i);
  4186 		if (bo) 
  4187 		{
  4188 			xelection.select(bo);
  4189 			ensureSelectionVisible();
  4190 		}
  4191 	}
  4192 }
  4193 
  4194 void MapEditor::editXLink(int i)	// FIXME missing saveState
  4195 {
  4196 	BranchObj *bo=xelection.getBranch();
  4197 	if (bo)
  4198 	{
  4199 		XLinkObj *xlo=bo->XLinkAt(i);
  4200 		if (xlo) 
  4201 		{
  4202 			EditXLinkDialog dia;
  4203 			dia.setXLink (xlo);
  4204 			dia.setSelection(bo);
  4205 			if (dia.exec() == QDialog::Accepted)
  4206 			{
  4207 				if (dia.useSettingsGlobal() )
  4208 				{
  4209 					setMapDefXLinkColor (xlo->getColor() );
  4210 					setMapDefXLinkWidth (xlo->getWidth() );
  4211 				}
  4212 				if (dia.deleteXLink())
  4213 					bo->deleteXLinkAt(i);
  4214 			}
  4215 		}	
  4216 	}
  4217 }
  4218 
  4219 AttributeTable* MapEditor::attributeTable()
  4220 {
  4221 	return attrTable;
  4222 }
  4223 
  4224 void MapEditor::testFunction1()
  4225 {
  4226 /*
  4227 	BranchObj *bo=xelection.getBranch();
  4228 	if (bo) animObjList.append( bo );
  4229 */	
  4230 	
  4231 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
  4232 	if (hidemode==HideNone)
  4233 	{
  4234 		setHideTmpMode (HideExport);
  4235 		mapCenter->calcBBoxSizeWithChilds();
  4236 		QRectF totalBBox=mapCenter->getTotalBBox();
  4237 		QRectF mapRect=totalBBox;
  4238 		QCanvasRectangle *frame=NULL;
  4239 
  4240 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
  4241 	
  4242 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
  4243 			totalBBox.width(), totalBBox.height());
  4244 		frame=new QCanvasRectangle (mapRect,mapScene);
  4245 		frame->setBrush (QColor(white));
  4246 		frame->setPen (QColor(black));
  4247 		frame->setZValue(0);
  4248 		frame->show();    
  4249 	}	
  4250 	else	
  4251 	{
  4252 		setHideTmpMode (HideNone);
  4253 	}	
  4254 	cout <<"  hidemode="<<hidemode<<endl;
  4255 	*/
  4256 }	
  4257 	
  4258 void MapEditor::testFunction2()
  4259 {
  4260 
  4261 	cout << "Selection: "<<xelection.getSelectString().ascii()<<endl;
  4262 //	model->addMapCenter();
  4263 
  4264 /*
  4265 	// Toggle hidemode
  4266 	if (hidemode==HideExport)
  4267 		setHideTmpMode (HideNone);
  4268 	else	
  4269 		setHideTmpMode (HideExport);
  4270 */		
  4271 /*
  4272 	LinkableMapObj *lmo=xelection.getBranch();
  4273 	if (lmo) 
  4274 	{
  4275 		cout << "LMO::id="<<lmo->getID().ascii()<<endl;
  4276 		cout << " BO::id="<<((BranchObj*)lmo)->getID().ascii()<<endl;
  4277 	}	
  4278 */	
  4279 }
  4280 
  4281 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
  4282 {
  4283 	// Lineedits are already closed by preceding
  4284 	// mouseEvent, we don't need to close here.
  4285 
  4286     QPointF p = mapToScene(e->pos());
  4287     LinkableMapObj* lmo=model->findMapObj(p, NULL);
  4288 	
  4289     if (lmo) 
  4290 	{	// MapObj was found
  4291 		if (xelection.single() != lmo)
  4292 		{
  4293 			// select the MapObj
  4294 			xelection.select(lmo);
  4295 		}
  4296 		// Context Menu 
  4297 		if (xelection.getBranch() ) 
  4298 		{
  4299 			// Context Menu on branch or mapcenter
  4300 			updateActions();
  4301 			branchContextMenu->popup(e->globalPos() );
  4302 		} else
  4303 		{
  4304 			if (xelection.getFloatImage() )
  4305 			{
  4306 				// Context Menu on floatimage
  4307 				updateActions();
  4308 				floatimageContextMenu->popup(e->globalPos() );
  4309 			}	
  4310 		}	
  4311 	} else 
  4312 	{ // No MapObj found, we are on the Canvas itself
  4313 		// Context Menu on scene
  4314 		updateActions();
  4315 		canvasContextMenu->popup(e->globalPos() );
  4316     } 
  4317 	e->accept();
  4318 }
  4319 
  4320 void MapEditor::keyPressEvent(QKeyEvent* e)
  4321 {
  4322 	if (e->modifiers() & Qt::ControlModifier)
  4323 	{
  4324 		switch (mainWindow->getModMode())
  4325 		{
  4326 			case Main::ModModeColor: 
  4327 				setCursor (PickColorCursor);
  4328 				break;
  4329 			case Main::ModModeCopy: 
  4330 				setCursor (CopyCursor);
  4331 				break;
  4332 			case Main::ModModeXLink: 
  4333 				setCursor (XLinkCursor);
  4334 				break;
  4335 			default :
  4336 				setCursor (Qt::ArrowCursor);
  4337 				break;
  4338 		} 
  4339 	}	
  4340 }
  4341 
  4342 void MapEditor::keyReleaseEvent(QKeyEvent* e)
  4343 {
  4344 	if (!(e->modifiers() & Qt::ControlModifier))
  4345 		setCursor (Qt::ArrowCursor);
  4346 }
  4347 
  4348 void MapEditor::mousePressEvent(QMouseEvent* e)
  4349 {
  4350 	// Ignore right clicks, these will go to context menus
  4351 	if (e->button() == Qt::RightButton )
  4352 	{
  4353 		e->ignore();
  4354 		return;
  4355 	}
  4356 
  4357 	//Ignore clicks while editing heading
  4358 	if (isSelectBlocked() ) 
  4359 	{
  4360 		e->ignore();
  4361 		return;
  4362 	}
  4363 
  4364     QPointF p = mapToScene(e->pos());
  4365     LinkableMapObj* lmo=model->findMapObj(p, NULL);
  4366 	
  4367 	e->accept();
  4368 
  4369 	//Take care of  system flags _or_ modifier modes
  4370 	//
  4371 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
  4372 		typeid(*lmo)==typeid(MapCenterObj) ))
  4373 	{
  4374 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
  4375 		if (!foname.isEmpty())
  4376 		{
  4377 			// systemFlag clicked
  4378 			selectInt (lmo);
  4379 			if (foname=="url") 
  4380 			{
  4381 				if (e->state() & Qt::ControlModifier)
  4382 					mainWindow->editOpenURLTab();
  4383 				else	
  4384 					mainWindow->editOpenURL();
  4385 			}	
  4386 			else if (foname=="vymLink")
  4387 			{
  4388 				mainWindow->editOpenVymLink();
  4389 				// tabWidget may change, better return now
  4390 				// before segfaulting...
  4391 			} else if (foname=="note")
  4392 				mainWindow->windowToggleNoteEditor();
  4393 			else if (foname=="hideInExport")		
  4394 				toggleHideExport();
  4395 			xelection.update();	
  4396 			return;	
  4397 		} 
  4398 	} 
  4399 
  4400 	// No system flag clicked, take care of modmodes (CTRL-Click)
  4401 	if (e->state() & Qt::ControlModifier)
  4402 	{
  4403 		if (mainWindow->getModMode()==Main::ModModeColor)
  4404 		{
  4405 				pickingColor=true;
  4406 				setCursor (PickColorCursor);
  4407 				return;
  4408 		} 
  4409 		if (mainWindow->getModMode()==Main::ModModeXLink)
  4410 		{	
  4411 			BranchObj *bo_begin=NULL;
  4412 			if (lmo)
  4413 				bo_begin=(BranchObj*)(lmo);
  4414 			else	
  4415 				if (xelection.getBranch() ) 
  4416 					bo_begin=xelection.getBranch();
  4417 			if (bo_begin)	
  4418 			{
  4419 				drawingLink=true;
  4420 				linkingObj_src=bo_begin;
  4421 				tmpXLink=new XLinkObj (mapScene);
  4422 				tmpXLink->setBegin (bo_begin);
  4423 				tmpXLink->setEnd   (p);
  4424 				tmpXLink->setColor(defXLinkColor);
  4425 				tmpXLink->setWidth(defXLinkWidth);
  4426 				tmpXLink->updateXLink();
  4427 				tmpXLink->setVisibility (true);
  4428 				return;
  4429 			} 
  4430 		}
  4431 	}	// End of modmodes
  4432 
  4433     if (lmo) 
  4434 	{	
  4435 		// Select the clicked object
  4436 		selectInt (lmo);
  4437 
  4438 		// Left Button	    Move Branches
  4439 		if (e->button() == Qt::LeftButton )
  4440 		{
  4441 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
  4442 			//movingObj_start.setY( p.y() - selection->y() );	
  4443 			movingObj_start.setX( p.x() - lmo->x() );	
  4444 			movingObj_start.setY( p.y() - lmo->y() );	
  4445 			movingObj_orgPos.setX (lmo->x() );
  4446 			movingObj_orgPos.setY (lmo->y() );
  4447 			movingObj_orgRelPos=lmo->getRelPos();
  4448 
  4449 			// If modMode==copy, then we want to "move" the _new_ object around
  4450 			// then we need the offset from p to the _old_ selection, because of tmp
  4451 			if (mainWindow->getModMode()==Main::ModModeCopy &&
  4452 				e->state() & Qt::ControlModifier)
  4453 			{
  4454 				BranchObj *bo=xelection.getBranch();
  4455 				if (bo)
  4456 				{
  4457 					copyingObj=true;
  4458 					bo->addBranch ((BranchObj*)xelection.single());
  4459 					unselect();
  4460 					xelection.select(bo->getLastBranch());
  4461 					model->reposition();
  4462 				}
  4463 			} 
  4464 
  4465 			movingObj=xelection.single();	
  4466 		} else
  4467 			// Middle Button    Toggle Scroll
  4468 			// (On Mac OS X this won't work, but we still have 
  4469 			// a button in the toolbar)
  4470 			if (e->button() == Qt::MidButton )
  4471 				toggleScroll();
  4472 		updateActions();
  4473 		xelection.update();
  4474 	} else 
  4475 	{ // No MapObj found, we are on the scene itself
  4476 		// Left Button	    move Pos of sceneView
  4477 		if (e->button() == Qt::LeftButton )
  4478 		{
  4479 			movingObj=NULL;	// move Content not Obj
  4480 			movingObj_start=e->globalPos();
  4481 			movingCont_start=QPointF (
  4482 				horizontalScrollBar()->value(),
  4483 				verticalScrollBar()->value());
  4484 			movingVec=QPointF(0,0);
  4485 			setCursor(HandOpenCursor);
  4486 		} 
  4487     } 
  4488 }
  4489 
  4490 void MapEditor::mouseMoveEvent(QMouseEvent* e)
  4491 {
  4492     QPointF p = mapToScene(e->pos());
  4493 	LinkableMapObj *lmosel=xelection.single();
  4494 
  4495     // Move the selected MapObj
  4496     if ( lmosel && movingObj) 
  4497     {	
  4498 		// reset cursor if we are moving and don't copy
  4499 		if (mainWindow->getModMode()!=Main::ModModeCopy)
  4500 			setCursor (Qt::ArrowCursor);
  4501 
  4502 		// To avoid jumping of the sceneView, only 
  4503 		// ensureSelectionVisible, if not tmp linked
  4504 		if (!lmosel->hasParObjTmp())
  4505 			ensureSelectionVisible ();
  4506 		
  4507 		// Now move the selection, but add relative position 
  4508 		// (movingObj_start) where selection was chosen with 
  4509 		// mousepointer. (This avoids flickering resp. jumping 
  4510 		// of selection back to absPos)
  4511 		
  4512 		// Check if we could link 
  4513 		LinkableMapObj* lmo=model->findMapObj(p, lmosel);
  4514 		
  4515 
  4516 		FloatObj *fio=xelection.getFloatImage();
  4517 		if (fio)
  4518 		{
  4519 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4520 			fio->setRelPos();
  4521 			fio->updateLink(); //no need for reposition, if we update link here
  4522 			xelection.update();
  4523 
  4524 			// Relink float to new mapcenter or branch, if shift is pressed	
  4525 			// Only relink, if selection really has a new parent
  4526 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
  4527 				( (typeid(*lmo)==typeid(BranchObj)) ||
  4528 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
  4529 				( lmo != fio->getParObj())  
  4530 				)
  4531 			{
  4532 				if (typeid(*fio) == typeid(FloatImageObj) && 
  4533 				( (typeid(*lmo)==typeid(BranchObj) ||
  4534 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
  4535 				{
  4536 
  4537 					// Also save the move which was done so far
  4538 					QString pold=qpointfToString(movingObj_orgRelPos);
  4539 					QString pnow=qpointfToString(fio->getRelPos());
  4540 					saveState(
  4541 						fio,
  4542 						"moveRel "+pold,
  4543 						fio,
  4544 						"moveRel "+pnow,
  4545 						QString("Move %1 to relative position %2").arg(getName(fio)).arg(pnow));
  4546 					fio->getParObj()->requestReposition();
  4547 					model->reposition();
  4548 
  4549 					linkTo (lmo->getSelectString());
  4550 					//movingObj=lmosel;
  4551 					//movingObj_orgRelPos=lmosel->getRelPos();	
  4552 
  4553 					model->reposition();
  4554 				}	
  4555 			}
  4556 		} else	
  4557 		{	// selection != a FloatObj
  4558 			if (lmosel->getDepth()==0)
  4559 			{
  4560 				// Move MapCenter
  4561 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
  4562 					((MapCenterObj*)lmosel)->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4563 				else	
  4564 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4565 				model->updateRelPositions();
  4566 			} else
  4567 			{	
  4568 				if (lmosel->getDepth()==1)
  4569 				{
  4570 					// Move mainbranch
  4571 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  4572 					lmosel->setRelPos();
  4573 				} else
  4574 				{
  4575 					// Move ordinary branch
  4576 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
  4577 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
  4578 						lmosel->move(p.x() -movingObj_start.x()+lmosel->getBBox().width(), 
  4579 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
  4580 					else	
  4581 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
  4582 				} 
  4583 
  4584 				// Maybe we can relink temporary?
  4585 				if (lmo && (lmo!=lmosel) && xelection.getBranch() && 
  4586 					 (typeid(*lmo)==typeid(BranchObj) ||
  4587 					  typeid(*lmo)==typeid(MapCenterObj)) ) 
  4588 
  4589 				{
  4590 					if (e->modifiers()==Qt::ControlModifier)
  4591 					{
  4592 						// Special case: CTRL to link below lmo
  4593 						lmosel->setParObjTmp (lmo,p,+1);
  4594 					}
  4595 					else if (e->modifiers()==Qt::ShiftModifier)
  4596 						lmosel->setParObjTmp (lmo,p,-1);
  4597 					else
  4598 						lmosel->setParObjTmp (lmo,p,0);
  4599 				} else	
  4600 				{
  4601 					lmosel->unsetParObjTmp();
  4602 				}		
  4603 				// reposition subbranch
  4604 				lmosel->reposition();	
  4605 			} // depth>0
  4606 
  4607 			xelection.update();
  4608 		} // no FloatImageObj
  4609 
  4610 		scene()->update();
  4611 		return;
  4612 	} // selection && moving_obj
  4613 		
  4614 	// Draw a link from one branch to another
  4615 	if (drawingLink)
  4616 	{
  4617 		 tmpXLink->setEnd (p);
  4618 		 tmpXLink->updateXLink();
  4619 	}	 
  4620 	
  4621     // Move sceneView 
  4622     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
  4623 	{
  4624 		QPointF p=e->globalPos();
  4625 		movingVec.setX(-p.x() + movingObj_start.x() );
  4626 		movingVec.setY(-p.y() + movingObj_start.y() );
  4627 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
  4628 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
  4629     }
  4630 }
  4631 
  4632 
  4633 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
  4634 {
  4635     QPointF p = mapToScene(e->pos());
  4636 	LinkableMapObj *dst;
  4637 	LinkableMapObj *lmosel=xelection.single();
  4638 	// Have we been picking color?
  4639 	if (pickingColor)
  4640 	{
  4641 		pickingColor=false;
  4642 		setCursor (Qt::ArrowCursor);
  4643 		// Check if we are over another branch
  4644 		dst=model->findMapObj(p, NULL);
  4645 		if (dst && lmosel) 
  4646 		{	
  4647 			if (e->state() & Qt::ShiftModifier)
  4648 				colorBranch (((BranchObj*)dst)->getColor());
  4649 			else	
  4650 				colorSubtree (((BranchObj*)dst)->getColor());
  4651 		} 
  4652 		return;
  4653 	}
  4654 
  4655 	// Have we been drawing a link?
  4656 	if (drawingLink)	
  4657 	{
  4658 		drawingLink=false;
  4659 		// Check if we are over another branch
  4660 		dst=model->findMapObj(p, NULL);
  4661 		if (dst && lmosel) 
  4662 		{	
  4663 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
  4664 			tmpXLink->updateXLink();
  4665 			tmpXLink->activate(); //FIXME savestate missing
  4666 			//saveStateComplete(QString("Activate xLink from %1 to %2").arg(getName(tmpXLink->getBegin())).arg(getName(tmpXLink->getEnd())) );	
  4667 		} else
  4668 		{
  4669 			delete(tmpXLink);
  4670 			tmpXLink=NULL;
  4671 		}
  4672 		return;
  4673 	}
  4674 	
  4675     // Have we been moving something?
  4676     if ( lmosel && movingObj ) 
  4677     {	
  4678 		FloatImageObj *fo=xelection.getFloatImage();
  4679 		if(fo)
  4680 		{
  4681 			// Moved FloatObj. Maybe we need to reposition
  4682 		    QString pold=qpointfToString(movingObj_orgRelPos);
  4683 		    QString pnow=qpointfToString(fo->getRelPos());
  4684 			saveState(
  4685 				fo,
  4686 				"moveRel "+pold,
  4687 				fo,
  4688 				"moveRel "+pnow,
  4689 				QString("Move %1 to relative position %2").arg(getName(fo)).arg(pnow));
  4690 
  4691 			fo->getParObj()->requestReposition();
  4692 			model->reposition();
  4693 		}	
  4694 
  4695 		// Check if we are over another branch, but ignore 
  4696 		// any found LMOs, which are FloatObjs
  4697 		dst=model->findMapObj(mapToScene(e->pos() ), lmosel);
  4698 
  4699 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
  4700 			dst=NULL;
  4701 		
  4702 		BranchObj *bo=xelection.getBranch();
  4703 		if (bo && bo->getDepth()==0)
  4704 		{	
  4705             if (movingObj_orgPos != bo->getAbsPos())
  4706             {
  4707                 QString pold=qpointfToString(movingObj_orgPos);
  4708                 QString pnow=qpointfToString(bo->getAbsPos());
  4709                 saveState(
  4710                     fo,
  4711                     "move "+pold,
  4712                     fo,
  4713                     "move "+pnow,
  4714                     QString("Move mapcenter %1 to position %2").arg(getName(bo)).arg(pnow));
  4715             }
  4716 		}
  4717 	
  4718 		if (xelection.type() == Selection::Branch )
  4719 		{	// A branch was moved
  4720 			
  4721 			// save the position in case we link to mapcenter
  4722 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  4723 
  4724 			// Reset the temporary drawn link to the original one
  4725 			lmosel->unsetParObjTmp();
  4726 
  4727 			// For Redo we may need to save original selection
  4728 			QString preSelStr=lmosel->getSelectString();
  4729 
  4730 			copyingObj=false;	
  4731 			if (dst ) 
  4732 			{
  4733 				BranchObj* bsel=xelection.getBranch();
  4734 				BranchObj* bdst=(BranchObj*)dst;
  4735 
  4736 				QString preParStr=(bsel->getParObj())->getSelectString();
  4737 				QString preNum=QString::number (bsel->getNum(),10);
  4738 				QString preDstParStr;
  4739 
  4740 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  4741 				{	// Link above dst
  4742 					preDstParStr=dst->getParObj()->getSelectString();
  4743 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
  4744 				} else 
  4745 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  4746 				{
  4747 					// Link below dst
  4748 					preDstParStr=dst->getParObj()->getSelectString();
  4749 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
  4750 				} else	
  4751 				{	// Append to dst
  4752 					preDstParStr=dst->getSelectString();
  4753 					bsel->linkTo (bdst,-1);
  4754 					if (dst->getDepth()==0) bsel->move (savePos);
  4755 				} 
  4756 				QString postSelStr=lmosel->getSelectString();
  4757 				QString postNum=QString::number (bsel->getNum(),10);
  4758 
  4759 				QString undoCom="linkTo (\""+ 
  4760 					preParStr+ "\"," + preNum  +"," + 
  4761 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  4762 
  4763 				QString redoCom="linkTo (\""+ 
  4764 					preDstParStr + "\"," + postNum + "," +
  4765 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  4766 
  4767 				saveState (
  4768 					postSelStr,undoCom,
  4769 					preSelStr, redoCom,
  4770 					QString("Relink %1 to %2").arg(getName(bsel)).arg(getName(dst)) );
  4771 			} else
  4772 				if (lmosel->getDepth()==1)
  4773 				{
  4774 					// The select string might be different _after_ moving around.
  4775 					// Therefor reposition and then use string of old selection, too
  4776 					model->reposition();
  4777 
  4778                     QPointF rp(lmosel->getRelPos());
  4779                     if (rp != movingObj_orgRelPos)
  4780                     {
  4781                         QString ps=qpointfToString(rp);
  4782                         saveState(
  4783                             lmosel->getSelectString(), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  4784                             preSelStr, "moveRel "+ps, 
  4785                             QString("Move %1 to relative position %2").arg(getName(lmosel)).arg(ps));
  4786                     }
  4787 				}
  4788 			// Draw the original link, before selection was moved around
  4789 			model->reposition();
  4790 		}
  4791 		xelection.update();
  4792 		// Finally resize scene, if needed
  4793 		scene()->update();
  4794 		movingObj=NULL;		
  4795 
  4796 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  4797 		updateActions();
  4798 	} else 
  4799 		// maybe we moved View: set old cursor
  4800 		setCursor (Qt::ArrowCursor);
  4801     
  4802 }
  4803 
  4804 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  4805 {
  4806 	if (isSelectBlocked() ) 
  4807 	{
  4808 		e->ignore();
  4809 		return;
  4810 	}
  4811 
  4812 	if (e->button() == Qt::LeftButton )
  4813 	{
  4814 		QPointF p = mapToScene(e->pos());
  4815 		LinkableMapObj *lmo=model->findMapObj(p, NULL);
  4816 		if (lmo) {	// MapObj was found
  4817 			// First select the MapObj than edit heading
  4818 			xelection.select(lmo);
  4819 			mainWindow->editHeading();
  4820 		}
  4821 	}
  4822 }
  4823 
  4824 void MapEditor::resizeEvent (QResizeEvent* e)
  4825 {
  4826 	QGraphicsView::resizeEvent( e );
  4827 }
  4828 
  4829 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  4830 {
  4831 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  4832 	//	cerr << event->format(i) << endl;
  4833 
  4834 	if (event->mimeData()->hasImage())
  4835 		event->acceptProposedAction();
  4836 	else	
  4837 		if (event->mimeData()->hasUrls())
  4838 			event->acceptProposedAction();
  4839 }
  4840 
  4841 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  4842 {
  4843 }
  4844 
  4845 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  4846 {
  4847 	event->accept();
  4848 }
  4849 
  4850 void MapEditor::dropEvent(QDropEvent *event)
  4851 {
  4852 	BranchObj *sel=xelection.getBranch();
  4853 	if (sel)
  4854 	{
  4855 		if (debug)
  4856 			foreach (QString format,event->mimeData()->formats()) 
  4857 				cout << "MapEditor: Dropped format: "<<format.ascii()<<endl;
  4858 
  4859 
  4860 		QList <QUrl> uris;
  4861 		if (event->mimeData()->hasImage()) 
  4862 		{
  4863 			 QVariant imageData = event->mimeData()->imageData();
  4864 			 addFloatImageInt (qvariant_cast<QPixmap>(imageData));
  4865 		} else
  4866 		if (event->mimeData()->hasUrls())
  4867 			uris=event->mimeData()->urls();
  4868 
  4869 		if (uris.count()>0)
  4870 		{
  4871 			QStringList files;
  4872 			QString s;
  4873 			QString heading;
  4874 			BranchObj *bo;
  4875 			for (int i=0; i<uris.count();i++)
  4876 			{
  4877 				// Workaround to avoid adding empty branches
  4878 				if (!uris.at(i).toString().isEmpty())
  4879 				{
  4880 					bo=sel->addBranch();
  4881 					if (bo)
  4882 					{
  4883 						s=uris.at(i).toLocalFile();
  4884 						if (!s.isEmpty()) 
  4885 						{
  4886 						   QString file = QDir::fromNativeSeparators(s);
  4887 						   heading = QFileInfo(file).baseName();
  4888 						   files.append(file);
  4889 						   if (file.endsWith(".vym", false))
  4890 							   bo->setVymLink(file);
  4891 						   else
  4892 							   bo->setURL(uris.at(i).toString());
  4893 					   } else 
  4894 					   {
  4895 						   bo->setURL(uris.at(i).toString());
  4896 					   }
  4897 
  4898 					   if (!heading.isEmpty())
  4899 						   bo->setHeading(heading);
  4900 					   else
  4901 						   bo->setHeading(uris.at(i).toString());
  4902 					}
  4903 				}
  4904 			}
  4905 			model->reposition();
  4906 		}
  4907 	}	
  4908 	event->acceptProposedAction();
  4909 }
  4910 
  4911 void MapEditor::timerEvent(QTimerEvent *event)  //TODO animation
  4912 {
  4913     Q_UNUSED(event);
  4914 
  4915 	cout << "ME::timerEvent\n";
  4916 
  4917 	for (int i=0; i<animObjList.size(); ++i)
  4918 	{
  4919 		animObjList.at(i)->animate();
  4920 		((BranchObj*)animObjList.at(i))->move2RelPos (((BranchObj*)animObjList.at(i))->getRelPos() );
  4921 	}
  4922 	model->reposition();
  4923 }
  4924 
  4925 
  4926 void MapEditor::sendSelection()
  4927 {
  4928 	if (netstate!=Server) return;
  4929 	sendData (QString("select (\"%1\")").arg(xelection.getSelectString()) );
  4930 }
  4931 
  4932 void MapEditor::newServer()
  4933 {
  4934 	port=54321;
  4935 	sendCounter=0;
  4936     tcpServer = new QTcpServer(this);
  4937     if (!tcpServer->listen(QHostAddress::Any,port)) {
  4938         QMessageBox::critical(this, "vym server",
  4939                               QString("Unable to start the server: %1.").arg(tcpServer->errorString()));
  4940         close();
  4941         return;
  4942     }
  4943 	connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newClient()));
  4944 	netstate=Server;
  4945 	cout<<"Server is running on port "<<tcpServer->serverPort()<<endl;
  4946 }
  4947 
  4948 void MapEditor::connectToServer()
  4949 {
  4950 	port=54321;
  4951 	server="salam.suse.de";
  4952 	server="localhost";
  4953 	clientSocket = new QTcpSocket (this);
  4954 	clientSocket->abort();
  4955     clientSocket->connectToHost(server ,port);
  4956 	connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readData()));
  4957     connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)),
  4958             this, SLOT(displayNetworkError(QAbstractSocket::SocketError)));
  4959 	netstate=Client;		
  4960 	cout<<"connected to "<<server.ascii()<<" port "<<port<<endl;
  4961 
  4962 	
  4963 }
  4964 
  4965 void MapEditor::newClient()
  4966 {
  4967     QTcpSocket *newClient = tcpServer->nextPendingConnection();
  4968     connect(newClient, SIGNAL(disconnected()),
  4969             newClient, SLOT(deleteLater()));
  4970 
  4971 	cout <<"ME::newClient  at "<<newClient->peerAddress().toString().ascii()<<endl;
  4972 
  4973 	clientList.append (newClient);
  4974 }
  4975 
  4976 
  4977 void MapEditor::sendData(const QString &s)
  4978 {
  4979 	if (clientList.size()==0) return;
  4980 
  4981 	// Create bytearray to send
  4982 	QByteArray block;
  4983     QDataStream out(&block, QIODevice::WriteOnly);
  4984     out.setVersion(QDataStream::Qt_4_0);
  4985 
  4986 	// Reserve some space for blocksize
  4987     out << (quint16)0;
  4988 
  4989 	// Write sendCounter
  4990     out << sendCounter++;
  4991 
  4992 	// Write data
  4993     out << s;
  4994 
  4995 	// Go back and write blocksize so far
  4996     out.device()->seek(0);
  4997     quint16 bs=(quint16)(block.size() - 2*sizeof(quint16));
  4998 	out << bs;
  4999 
  5000 	if (debug)
  5001 		cout << "ME::sendData  bs="<<bs<<"  counter="<<sendCounter<<"  s="<<s.ascii()<<endl;
  5002 
  5003 	for (int i=0; i<clientList.size(); ++i)
  5004 	{
  5005 		//cout << "Sending \""<<s.ascii()<<"\" to "<<clientList.at(i)->peerAddress().toString().ascii()<<endl;
  5006 		clientList.at(i)->write (block);
  5007 	}
  5008 }
  5009 
  5010 void MapEditor::readData ()
  5011 {
  5012 	while (clientSocket->bytesAvailable() >=(int)sizeof(quint16) )
  5013 	{
  5014 		if (debug)
  5015 			cout <<"readData  bytesAvail="<<clientSocket->bytesAvailable();
  5016 		quint16 recCounter;
  5017 		quint16 blockSize;
  5018 
  5019 		QDataStream in(clientSocket);
  5020 		in.setVersion(QDataStream::Qt_4_0);
  5021 
  5022 		in >> blockSize;
  5023 		in >> recCounter;
  5024 		
  5025 		QString t;
  5026 		in >>t;
  5027 		if (debug)
  5028 			cout << "  t="<<t.ascii()<<endl;
  5029 		parseAtom (t);
  5030 	}
  5031 	return;
  5032 }
  5033 
  5034 void MapEditor::displayNetworkError(QAbstractSocket::SocketError socketError)
  5035 {
  5036     switch (socketError) {
  5037     case QAbstractSocket::RemoteHostClosedError:
  5038         break;
  5039     case QAbstractSocket::HostNotFoundError:
  5040         QMessageBox::information(this, vymName +" Network client",
  5041                                  "The host was not found. Please check the "
  5042                                     "host name and port settings.");
  5043         break;
  5044     case QAbstractSocket::ConnectionRefusedError:
  5045         QMessageBox::information(this, vymName + " Network client",
  5046                                  "The connection was refused by the peer. "
  5047                                     "Make sure the fortune server is running, "
  5048                                     "and check that the host name and port "
  5049                                     "settings are correct.");
  5050         break;
  5051     default:
  5052         QMessageBox::information(this, vymName + " Network client",
  5053                                  QString("The following error occurred: %1.")
  5054                                  .arg(clientSocket->errorString()));
  5055     }
  5056 }
  5057 
  5058 void MapEditor::autosave()
  5059 {
  5060 	// Disable autosave, while we have gone back in history
  5061 	int redosAvail=undoSet.readNumEntry (QString("/history/redosAvail"));
  5062 	if (redosAvail>0) return;
  5063 
  5064 
  5065 	if (mapUnsaved &&mapChanged && settings.value ("/mapeditor/autosave/use",true).toBool() )
  5066 		mainWindow->fileSave (this);
  5067 }
  5068 
  5069 
  5070 /*TODO not needed? void MapEditor::contentsDropEvent(QDropEvent *event) 
  5071 {
  5072 
  5073 		} else if (event->provides("application/x-moz-file-promise-url") && 
  5074 			 event->provides("application/x-moz-nativeimage")) 
  5075 		{
  5076 			// Contains url to the img src in unicode16
  5077 			QByteArray d = event->encodedData("application/x-moz-file-promise-url");
  5078 			QString url = QString((const QChar*)d.data(),d.size()/2);
  5079 			fetchImage(url);
  5080 			event->accept();
  5081 			update=true;
  5082 		} else if (event->provides ("text/uri-list"))
  5083 		{	// Uris provided e.g. by konqueror
  5084 			Q3UriDrag::decode (event,uris);
  5085 		} else if (event->provides ("_NETSCAPE_URL"))
  5086 		{	// Uris provided by Mozilla
  5087 		  QStringList l = QStringList::split("\n", event->encodedData("_NETSCAPE_URL"));
  5088 		  uris.append(l[0]);
  5089 		  heading = l[1];
  5090 		} else if (event->provides("text/html")) {
  5091 
  5092 		  // Handels text mime types
  5093 		  // Look like firefox allways handle text as unicode16 (2 bytes per char.)
  5094 		  QByteArray d = event->encodedData("text/html");
  5095 		  QString text;
  5096 		  if (isUnicode16(d)) 
  5097 		    text = QString((const QChar*)d.data(),d.size()/2);
  5098 		  else 
  5099 		    text = QString(d);
  5100 
  5101 		  textEditor->setText(text);
  5102 
  5103 		  event->accept();
  5104 		  update=true;
  5105 		} else if (event->provides("text/plain")) {
  5106 		  QByteArray d = event->encodedData("text/plain");
  5107 		  QString text;
  5108 		  if (isUnicode16(d))
  5109 		    text = QString((const QChar*)d.data(),d.size()/2);
  5110 		  else 
  5111 		    text = QString(d);
  5112 
  5113 		  textEditor->setText(text);
  5114 		  
  5115 		  event->accept();
  5116 		  update= true;
  5117 		}
  5118 
  5119 		*/
  5120 
  5121 
  5122 
  5123 bool isUnicode16(const QByteArray &d) 
  5124 {
  5125   // TODO: make more precise check for unicode 16.
  5126   // Guess unicode16 if any of second bytes are zero
  5127   unsigned int length = max(0,d.size()-2)/2;
  5128   for (unsigned int i = 0; i<length ; i++)
  5129     if (d.at(i*2+1)==0) return true;
  5130   return false;
  5131 }
  5132       
  5133 void MapEditor::addFloatImageInt (const QPixmap &img) 
  5134 {
  5135 	BranchObj *bo=xelection.getBranch();
  5136 	if (bo)
  5137   {
  5138 	FloatImageObj *fio=bo->addFloatImage();
  5139     fio->load(img);
  5140     fio->setOriginalFilename("No original filename (image added by dropevent)");	
  5141 	QString s=bo->getSelectString();
  5142 	saveState (PartOfMap, s, "nop ()", s, "copy ()","Copy dropped image to clipboard",fio  );
  5143 	saveState (fio,"delete ()", bo,QString("paste(%1)").arg(curStep),"Pasting dropped image");
  5144     model->reposition();
  5145     scene()->update();
  5146   }
  5147 }
  5148 
  5149 /*
  5150 
  5151 void MapEditor::imageDataFetched(const QByteArray &a, Q3NetworkOperation * / *nop* /) 
  5152 {
  5153   if (!imageBuffer) imageBuffer = new QBuffer();
  5154   if (!imageBuffer->isOpen()) {
  5155     imageBuffer->open(QIODevice::WriteOnly | QIODevice::Append);
  5156   }
  5157   imageBuffer->at(imageBuffer->at()+imageBuffer->writeBlock(a));
  5158 }
  5159 
  5160 
  5161 void MapEditor::imageDataFinished(Q3NetworkOperation *nop) 
  5162 {
  5163 	if (nop->state()==Q3NetworkProtocol::StDone) {
  5164 		QPixmap img(imageBuffer->buffer());
  5165 		addFloatImageInt (img);
  5166 	}
  5167 
  5168 	if (imageBuffer) {
  5169 		imageBuffer->close();
  5170 		if (imageBuffer) {
  5171 			imageBuffer->close();
  5172 			delete imageBuffer;
  5173 			imageBuffer = 0;
  5174 		}
  5175 	}
  5176 }
  5177 
  5178 void MapEditor::fetchImage(const QString &url) 
  5179 {
  5180   if (urlOperator) {
  5181     urlOperator->stop();
  5182     disconnect(urlOperator);
  5183     delete urlOperator;
  5184   }
  5185   
  5186   urlOperator = new Q3UrlOperator(url);
  5187   connect(urlOperator, SIGNAL(finished(Q3NetworkOperation *)), 
  5188 	  this, SLOT(imageDataFinished(Q3NetworkOperation*)));
  5189 
  5190   connect(urlOperator, SIGNAL(data(const QByteArray &, Q3NetworkOperation *)),
  5191 	  this, SLOT(imageDataFetched(const QByteArray &, Q3NetworkOperation *)));
  5192   urlOperator->get();
  5193 }
  5194 */
  5195