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