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