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