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