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