mapeditor.cpp
author insilmaril
Mon, 12 Feb 2007 09:28:47 +0000
changeset 427 fb74fa7bfb4c
parent 424 d886c77ac0fe
child 428 9ae68208e2ff
permissions -rw-r--r--
1.8.66 Various fixes
     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::unScrollAll()
  3007 {
  3008 	BranchObj *bo;
  3009 	bo=mapCenter->first();
  3010 	while (bo) 
  3011 	{
  3012 		if (bo->isScrolled()) bo->toggleScroll();
  3013 		bo=bo->next();
  3014 	}
  3015 }
  3016 
  3017 void MapEditor::loadFloatImage ()
  3018 {
  3019 	BranchObj *bo=xelection.getBranch();
  3020 	if (bo)
  3021 	{
  3022 
  3023 		Q3FileDialog *fd=new Q3FileDialog( this);
  3024 		fd->setMode (Q3FileDialog::ExistingFiles);
  3025 		fd->addFilter (QString (tr("Images") + " (*.png *.bmp *.xbm *.jpg *.png *.xpm *.gif *.pnm)"));
  3026 		ImagePreview *p =new ImagePreview (fd);
  3027 		fd->setContentsPreviewEnabled( TRUE );
  3028 		fd->setContentsPreview( p, p );
  3029 		fd->setPreviewMode( Q3FileDialog::Contents );
  3030 		fd->setCaption(vymName+" - " +tr("Load image"));
  3031 		fd->setDir (lastImageDir);
  3032 		fd->show();
  3033 
  3034 		QString fn;
  3035 		if ( fd->exec() == QDialog::Accepted )
  3036 		{
  3037 			// FIXME loadFIO in QT4 use:	lastImageDir=fd->directory();
  3038 			lastImageDir=QDir (fd->dirPath());
  3039 			QStringList flist = fd->selectedFiles();
  3040 			QStringList::Iterator it = flist.begin();
  3041 			FloatImageObj *fio;
  3042 			while( it != flist.end() ) 
  3043 			{
  3044 				fn = *it;
  3045 				bo->addFloatImage();
  3046 				fio=bo->getLastFloatImage();
  3047 				fio->load(*it);
  3048 				// FIXME loadFIO check if load of fio was successful
  3049 				saveState(
  3050 					(LinkableMapObj*)fio,
  3051 					"delete ()",
  3052 					bo, 
  3053 					QString ("loadFloatImage (%1)").arg(*it),
  3054 					QString("Add floatimage %1 to %2").arg(*it).arg(getName(bo))
  3055 				);
  3056 				bo->getLastFloatImage()->setOriginalFilename(fn);
  3057 				++it;
  3058 			}
  3059 
  3060 			mapCenter->reposition();
  3061 			scene()->update();
  3062 		}
  3063 		delete (p);
  3064 		delete (fd);
  3065 	}
  3066 }
  3067 
  3068 void MapEditor::saveFloatImage ()
  3069 {
  3070 	FloatImageObj *fio=xelection.getFloatImage();
  3071 	if (fio)
  3072 	{
  3073 		QFileDialog *fd=new QFileDialog( this);
  3074 		fd->setFilters (imageIO.getFilters());
  3075 		fd->setCaption(vymName+" - " +tr("Save image"));
  3076 		fd->setFileMode( QFileDialog::AnyFile );
  3077 		fd->setDirectory (lastImageDir);
  3078 //		fd->setSelection (fio->getOriginalFilename());
  3079 		fd->show();
  3080 
  3081 		QString fn;
  3082 		if ( fd->exec() == QDialog::Accepted )
  3083 		{
  3084 			if (QFile (fd->selectedFile()).exists() )
  3085 			{
  3086 				QMessageBox mb( vymName,
  3087 					tr("The file %1 exists already.\n"
  3088 					"Do you want to overwrite it?").arg(fd->selectedFile()),
  3089 				QMessageBox::Warning,
  3090 				QMessageBox::Yes | QMessageBox::Default,
  3091 				QMessageBox::Cancel | QMessageBox::Escape,
  3092 				QMessageBox::QMessageBox::NoButton );
  3093 
  3094 				mb.setButtonText( QMessageBox::Yes, tr("Overwrite") );
  3095 				mb.setButtonText( QMessageBox::No, tr("Cancel"));
  3096 				switch( mb.exec() ) 
  3097 				{
  3098 					case QMessageBox::Yes:
  3099 						// save 
  3100 						break;;
  3101 					case QMessageBox::Cancel:
  3102 						// do nothing
  3103 						delete (fd);
  3104 						return;
  3105 						break;
  3106 				}
  3107 			}
  3108 			fio->save (fd->selectedFile(),imageIO.getType (fd->selectedFilter() ) );
  3109 		}
  3110 		delete (fd);
  3111 	}
  3112 }
  3113 
  3114 void MapEditor::setFrame(const FrameType &t)	// FIXME missing saveState
  3115 {
  3116 	BranchObj *bo=xelection.getBranch();
  3117 	if (bo)
  3118 	{
  3119 		bo->setFrameType (t);
  3120 		mapCenter->reposition();
  3121 		bo->updateLink();
  3122 	}
  3123 }
  3124 
  3125 void MapEditor::setIncludeImagesVer(bool b)	// FIXME missing saveState
  3126 {
  3127 	BranchObj *bo=xelection.getBranch();
  3128 	if (bo)
  3129 	{
  3130 		bo->setIncludeImagesVer(b);
  3131 		mapCenter->reposition();
  3132 	}	
  3133 }
  3134 
  3135 void MapEditor::setIncludeImagesHor(bool b)	// FIXME missing saveState
  3136 {
  3137 	BranchObj *bo=xelection.getBranch();
  3138 	if (bo)
  3139 	{
  3140 		bo->setIncludeImagesHor(b);
  3141 		mapCenter->reposition();
  3142 	}	
  3143 }
  3144 
  3145 void MapEditor::setHideLinkUnselected (bool b)	// FIXME missing saveState
  3146 {
  3147 	LinkableMapObj *sel=xelection.single();
  3148 	if (sel &&
  3149 		(xelection.type() == Branch || 
  3150 		xelection.type() == MapCenter  ||
  3151 		xelection.type() == FloatImage ))
  3152 		sel->setHideLinkUnselected(b);
  3153 }
  3154 
  3155 void MapEditor::importDirInt(BranchObj *dst, QDir d)	// FIXME missing saveState
  3156 {
  3157 	BranchObj *bo=xelection.getBranch();
  3158 	if (bo)
  3159 	{
  3160 		// Traverse directories
  3161 		d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
  3162 		QFileInfoList list = d.entryInfoList();
  3163 		QFileInfo fi;
  3164 
  3165 		for (int i = 0; i < list.size(); ++i) 
  3166 		{
  3167 			fi=list.at(i);
  3168 			if (fi.fileName() != "." && fi.fileName() != ".." )
  3169 			{
  3170 				dst->addBranch();
  3171 				bo=dst->getLastBranch();
  3172 				bo->setHeading (fi.fileName() );
  3173 				bo->setColor (QColor("blue"));
  3174 				bo->toggleScroll();
  3175 				if ( !d.cd(fi.fileName()) ) 
  3176 					QMessageBox::critical (0,tr("Critical Import Error"),tr("Cannot find the directory %1").arg(fi.fileName()));
  3177 				else 
  3178 				{
  3179 					// Recursively add subdirs
  3180 					importDirInt (bo,d);
  3181 					d.cdUp();
  3182 				}
  3183 			}	
  3184 		}		
  3185 		// Traverse files
  3186 		d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
  3187 		list = d.entryInfoList();
  3188 
  3189 		for (int i = 0; i < list.size(); ++i) 
  3190 		{
  3191 			fi=list.at(i);
  3192 			dst->addBranch();
  3193 			bo=dst->getLastBranch();
  3194 			bo->setHeading (fi.fileName() );
  3195 			bo->setColor (QColor("black"));
  3196 			if (fi.fileName().right(4) == ".vym" )
  3197 				bo->setVymLink (fi.filePath());
  3198 		}	
  3199 	}		
  3200 }
  3201 
  3202 void MapEditor::importDir()
  3203 {
  3204 	BranchObj *bo=xelection.getBranch();
  3205 	if (bo)
  3206 	{
  3207 		QStringList filters;
  3208 		filters <<"VYM map (*.vym)";
  3209 		QFileDialog *fd=new QFileDialog( this,vymName+ " - " +tr("Choose directory structure to import"));
  3210 		fd->setMode (QFileDialog::DirectoryOnly);
  3211 		fd->setFilters (filters);
  3212 		fd->setCaption(vymName+" - " +tr("Choose directory structure to import"));
  3213 		fd->show();
  3214 
  3215 		QString fn;
  3216 		if ( fd->exec() == QDialog::Accepted )
  3217 		{
  3218 			importDirInt (bo,QDir(fd->selectedFile()) );
  3219 			mapCenter->reposition();
  3220 			scene()->update();
  3221 		}
  3222 	}	
  3223 }
  3224 
  3225 void MapEditor::followXLink(int i)
  3226 {
  3227 	BranchObj *bo=xelection.getBranch();
  3228 	if (bo)
  3229 	{
  3230 		bo=bo->XLinkTargetAt(i);
  3231 		if (bo) 
  3232 		{
  3233 			xelection.select(bo);
  3234 			ensureSelectionVisible();
  3235 		}
  3236 	}
  3237 }
  3238 
  3239 void MapEditor::editXLink(int i)	// FIXME missing saveState
  3240 {
  3241 	BranchObj *bo=xelection.getBranch();
  3242 	if (bo)
  3243 	{
  3244 		XLinkObj *xlo=bo->XLinkAt(i);
  3245 		if (xlo) 
  3246 		{
  3247 			EditXLinkDialog dia;
  3248 			dia.setXLink (xlo);
  3249 			dia.setSelection(bo);
  3250 			if (dia.exec() == QDialog::Accepted)
  3251 			{
  3252 				if (dia.useSettingsGlobal() )
  3253 				{
  3254 					setMapDefXLinkColor (xlo->getColor() );
  3255 					setMapDefXLinkWidth (xlo->getWidth() );
  3256 				}
  3257 				if (dia.deleteXLink())
  3258 					bo->deleteXLinkAt(i);
  3259 			}
  3260 		}	
  3261 	}
  3262 }
  3263 
  3264 void MapEditor::testFunction()
  3265 {
  3266 	// This is the playground
  3267 
  3268 	WarningDialog dia;
  3269 	dia.showCancelButton (true);
  3270 	dia.setText("This is a longer \nWarning");
  3271 	/*
  3272 	dia.setCaption("Warning: Flux problem");
  3273 	dia.setShowAgainName("/warnings/mapeditor");
  3274 	*/
  3275 	if (dia.exec()==QDialog::Accepted)
  3276 		cout << "accepted!\n";
  3277 	else	
  3278 		cout << "canceled!\n";
  3279 	return;
  3280 
  3281 /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
  3282 	if (hidemode==HideNone)
  3283 	{
  3284 		setHideTmpMode (HideExport);
  3285 		mapCenter->calcBBoxSizeWithChilds();
  3286 		QRectF totalBBox=mapCenter->getTotalBBox();
  3287 		QRectF mapRect=totalBBox;
  3288 		QCanvasRectangle *frame=NULL;
  3289 
  3290 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
  3291 	
  3292 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
  3293 			totalBBox.width(), totalBBox.height());
  3294 		frame=new QCanvasRectangle (mapRect,mapScene);
  3295 		frame->setBrush (QColor(white));
  3296 		frame->setPen (QColor(black));
  3297 		frame->setZValue(0);
  3298 		frame->show();    
  3299 	}	
  3300 	else	
  3301 	{
  3302 		setHideTmpMode (HideNone);
  3303 	}	
  3304 	cout <<"  hidemode="<<hidemode<<endl;
  3305 	*/
  3306 }
  3307 
  3308 void MapEditor::ensureSelectionVisible()
  3309 {
  3310 	LinkableMapObj *lmo=xelection.single();
  3311 	if (lmo) ensureVisible (lmo->getBBox() );
  3312 }
  3313 
  3314 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
  3315 {
  3316 	// Lineedits are already closed by preceding
  3317 	// mouseEvent, we don't need to close here.
  3318 
  3319     QPointF p = mapToScene(e->pos());
  3320     LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
  3321 	
  3322     if (lmo) 
  3323 	{	// MapObj was found
  3324 		if (xelection.single() != lmo)
  3325 		{
  3326 			// select the MapObj
  3327 			xelection.select(lmo);
  3328 		}
  3329 		// Context Menu 
  3330 		if (xelection.getBranch() ) 
  3331 		{
  3332 			// Context Menu on branch or mapcenter
  3333 			updateActions();
  3334 			branchContextMenu->popup(e->globalPos() );
  3335 		} else
  3336 		{
  3337 			if (xelection.getFloatImage() )
  3338 			{
  3339 				// Context Menu on floatimage
  3340 				updateActions();
  3341 				floatimageContextMenu->popup(e->globalPos() );
  3342 			}	
  3343 		}	
  3344 	} else 
  3345 	{ // No MapObj found, we are on the Canvas itself
  3346 		// Context Menu on scene
  3347 		updateActions();
  3348 		canvasContextMenu->popup(e->globalPos() );
  3349     } 
  3350 	e->accept();
  3351 }
  3352 
  3353 void MapEditor::keyPressEvent(QKeyEvent* e)
  3354 {
  3355 	if (e->modifiers() & Qt::ControlModifier)
  3356 	{
  3357 		switch (mainWindow->getModMode())
  3358 		{
  3359 			case ModModeColor: 
  3360 				setCursor (PickColorCursor);
  3361 				break;
  3362 			case ModModeCopy: 
  3363 				setCursor (CopyCursor);
  3364 				break;
  3365 			case ModModeXLink: 
  3366 				setCursor (XLinkCursor);
  3367 				break;
  3368 			default :
  3369 				setCursor (Qt::ArrowCursor);
  3370 				break;
  3371 		} 
  3372 	}	
  3373 }
  3374 
  3375 void MapEditor::keyReleaseEvent(QKeyEvent* e)
  3376 {
  3377 	if (!(e->modifiers() & Qt::ControlModifier))
  3378 		setCursor (Qt::ArrowCursor);
  3379 }
  3380 
  3381 void MapEditor::mousePressEvent(QMouseEvent* e)
  3382 {
  3383 	// Ignore right clicks, these will go to context menus
  3384 	if (e->button() == Qt::RightButton )
  3385 	{
  3386 		e->ignore();
  3387 		return;
  3388 	}
  3389 
  3390     QPointF p = mapToScene(e->pos());
  3391     LinkableMapObj* lmo=mapCenter->findMapObj(p, NULL);
  3392 	
  3393 	e->accept();
  3394 
  3395 	//Take care of clickdesystem flags _or_ modifier modes
  3396 	//
  3397 	if (lmo && (typeid(*lmo)==typeid(BranchObj) ||
  3398 		typeid(*lmo)==typeid(MapCenterObj) ))
  3399 	{
  3400 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
  3401 		if (!foname.isEmpty())
  3402 		{
  3403 			// systemFlag clicked
  3404 			selectInt (lmo);
  3405 			if (foname=="url") 
  3406 			{
  3407 				if (e->state() & Qt::ControlModifier)
  3408 					mainWindow->editOpenURLTab();
  3409 				else	
  3410 					mainWindow->editOpenURL();
  3411 			}	
  3412 			else if (foname=="vymLink")
  3413 			{
  3414 				mainWindow->editOpenVymLink();
  3415 				// tabWidget may change, better return now
  3416 				// before segfaulting...
  3417 			} else if (foname=="note")
  3418 				mainWindow->windowToggleNoteEditor();
  3419 			else if (foname=="hideInExport")		
  3420 				toggleHideExport();
  3421 			return;	
  3422 		} 
  3423 	} 
  3424 	// No system flag clicked, take care of modmodes
  3425 
  3426 	// Special case: CTRL is pressed
  3427 	if (e->state() & Qt::ControlModifier)
  3428 	{
  3429 		if (mainWindow->getModMode()==ModModeColor)
  3430 		{
  3431 				pickingColor=true;
  3432 				setCursor (PickColorCursor);
  3433 				return;
  3434 		} 
  3435 		if (mainWindow->getModMode()==ModModeXLink)
  3436 		{	
  3437 			BranchObj *bo_begin=NULL;
  3438 			if (lmo)
  3439 				bo_begin=(BranchObj*)(lmo);
  3440 			else	
  3441 				if (xelection.getBranch() ) 
  3442 					bo_begin=xelection.getBranch();
  3443 			if (bo_begin)	
  3444 			{
  3445 				drawingLink=true;
  3446 				linkingObj_src=bo_begin;
  3447 				tmpXLink=new XLinkObj (mapScene);
  3448 				tmpXLink->setBegin (bo_begin);
  3449 				tmpXLink->setEnd   (p);
  3450 				tmpXLink->setColor(defXLinkColor);
  3451 				tmpXLink->setWidth(defXLinkWidth);
  3452 				tmpXLink->updateXLink();
  3453 				tmpXLink->setVisibility (true);
  3454 				return;
  3455 			} 
  3456 		}
  3457 	}
  3458     if (lmo) 
  3459 	{	
  3460 		selectInt (lmo);
  3461 		// Left Button	    Move Branches
  3462 		if (e->button() == Qt::LeftButton )
  3463 		{
  3464 			//movingObj_start.setX( p.x() - selection->x() );// TODO replaced selection->lmo here	
  3465 			//movingObj_start.setY( p.y() - selection->y() );	
  3466 			movingObj_start.setX( p.x() - lmo->x() );	
  3467 			movingObj_start.setY( p.y() - lmo->y() );	
  3468 			movingObj_orgPos.setX (lmo->x() );
  3469 			movingObj_orgPos.setY (lmo->y() );
  3470 			movingObj_orgRelPos=lmo->getRelPos();
  3471 
  3472 			// If modMode==copy, then we want to "move" the _new_ object around
  3473 			// then we need the offset from p to the _old_ selection, because of tmp
  3474 			if (mainWindow->getModMode()==ModModeCopy &&
  3475 				e->state() & Qt::ControlModifier)
  3476 			{
  3477 				if (xelection.type()==Branch)
  3478 				{
  3479 					copyingObj=true;
  3480 					mapCenter->addBranch ((BranchObj*)xelection.single());
  3481 					unselect();
  3482 					xelection.select(mapCenter->getLastBranch());
  3483 					mapCenter->reposition();
  3484 				}
  3485 			} 
  3486 
  3487 			movingObj=xelection.single();	
  3488 		} else
  3489 			// Middle Button    Toggle Scroll
  3490 			// (On Mac OS X this won't work, but we still have 
  3491 			// a button in the toolbar)
  3492 			if (e->button() == Qt::MidButton )
  3493 				toggleScroll();
  3494 		updateActions();
  3495 	} else 
  3496 	{ // No MapObj found, we are on the scene itself
  3497 		// Left Button	    move Pos of sceneView
  3498 		if (e->button() == Qt::LeftButton )
  3499 		{
  3500 			movingObj=NULL;	// move Content not Obj
  3501 			movingObj_start=e->globalPos();
  3502 			movingCont_start=QPointF (
  3503 				horizontalScrollBar()->value(),
  3504 				verticalScrollBar()->value());
  3505 			movingVec=QPointF(0,0);
  3506 			setCursor(HandOpenCursor);
  3507 		} 
  3508     } 
  3509 }
  3510 
  3511 void MapEditor::mouseMoveEvent(QMouseEvent* e)
  3512 {
  3513     QPointF p = mapToScene(e->pos());
  3514 	LinkableMapObj *lmosel=xelection.single();
  3515 
  3516     // Move the selected MapObj
  3517     if ( lmosel && movingObj) 
  3518     {	
  3519 		// reset cursor if we are moving and don't copy
  3520 		if (mainWindow->getModMode()!=ModModeCopy)
  3521 			setCursor (Qt::ArrowCursor);
  3522 
  3523 		// To avoid jumping of the sceneView, only 
  3524 		// ensureSelectionVisible, if not tmp linked
  3525 		if (!lmosel->hasParObjTmp())
  3526 			ensureSelectionVisible ();
  3527 		
  3528 		// Now move the selection, but add relative position 
  3529 		// (movingObj_start) where selection was chosen with 
  3530 		// mousepointer. (This avoids flickering resp. jumping 
  3531 		// of selection back to absPos)
  3532 		
  3533 		// Check if we could link 
  3534 		LinkableMapObj* lmo=mapCenter->findMapObj(p, lmosel);
  3535 		
  3536 
  3537 		FloatObj *fio=xelection.getFloatImage();
  3538 		if (fio)
  3539 		{
  3540 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3541 			fio->setRelPos();
  3542 			fio->updateLink(); //no need for reposition, if we update link here
  3543 
  3544 			// Relink float to new mapcenter or branch, if shift is pressed	
  3545 			// Only relink, if selection really has a new parent
  3546 			if ( (e->modifiers()==Qt::ShiftModifier) && lmo &&
  3547 				( (typeid(*lmo)==typeid(BranchObj)) ||
  3548 				  (typeid(*lmo)==typeid(MapCenterObj)) ) &&
  3549 				( lmo != fio->getParObj())  
  3550 				)
  3551 			{
  3552 				if (typeid(*fio) == typeid(FloatImageObj) && 
  3553 				( (typeid(*lmo)==typeid(BranchObj) ||
  3554 				  typeid(*lmo)==typeid(MapCenterObj)) ))  
  3555 				{
  3556 
  3557 					// Also save the move which was done so far
  3558 					QString pold=qpointfToString(movingObj_orgRelPos);
  3559 					QString pnow=qpointfToString(fio->getRelPos());
  3560 					saveState(
  3561 						fio,
  3562 						"moveRel "+pold,
  3563 						fio,
  3564 						"moveRel "+pnow,
  3565 						QString("Move %1 to relativ position %2").arg(getName(fio)).arg(pnow));
  3566 					fio->getParObj()->requestReposition();
  3567 					mapCenter->reposition();
  3568 
  3569 					cout << "current relPos="<<movingObj_orgRelPos<<endl;	// FIXME testing
  3570 					linkTo (lmo->getSelectString());
  3571 					cout << "setting movingObj\n";
  3572 					movingObj=lmosel;
  3573 					movingObj_orgRelPos=lmosel->getRelPos();	
  3574 					cout <<"ok 0\n";
  3575 
  3576 					mapCenter->reposition();
  3577 				}	
  3578 			}
  3579 		} else	
  3580 		{	// selection != a FloatObj
  3581 			if (lmosel->getDepth()==0)
  3582 			{
  3583 				// Move MapCenter
  3584 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
  3585 					mapCenter->moveAll(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3586 				else	
  3587 					mapCenter->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3588 				mapCenter->updateRelPositions();	
  3589 			} else
  3590 			{	
  3591 				if (lmosel->getDepth()==1)
  3592 				{
  3593 					// Move mainbranch
  3594 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  3595 					lmosel->setRelPos();
  3596 				} else
  3597 				{
  3598 					// Move ordinary branch
  3599 					if (lmosel->getOrientation() == OrientLeftOfCenter)
  3600 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
  3601 						lmosel->move(p.x() -movingObj_start.x()+lmosel->getBBox().width(), 
  3602 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
  3603 					else	
  3604 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
  3605 				} 
  3606 				// reposition subbranch
  3607 				lmosel->reposition();	
  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 			} // depth>0
  3625 
  3626 		} // no FloatImageObj
  3627 
  3628 		scene()->update();
  3629 		return;
  3630 	} // selection && moving_obj
  3631 		
  3632 	// Draw a link from one branch to another
  3633 	if (drawingLink)
  3634 	{
  3635 		 tmpXLink->setEnd (p);
  3636 		 tmpXLink->updateXLink();
  3637 	}	 
  3638 	
  3639     // Move sceneView 
  3640     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
  3641 	{
  3642 		QPointF p=e->globalPos();
  3643 		movingVec.setX(-p.x() + movingObj_start.x() );
  3644 		movingVec.setY(-p.y() + movingObj_start.y() );
  3645 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
  3646 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
  3647     }
  3648 }
  3649 
  3650 
  3651 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
  3652 {
  3653     QPointF p = mapToScene(e->pos());
  3654 	LinkableMapObj *dst;
  3655 	LinkableMapObj *lmosel=xelection.single();
  3656 	// Have we been picking color?
  3657 	if (pickingColor)
  3658 	{
  3659 		pickingColor=false;
  3660 		setCursor (Qt::ArrowCursor);
  3661 		// Check if we are over another branch
  3662 		dst=mapCenter->findMapObj(p, NULL);
  3663 		if (dst && lmosel) 
  3664 		{	
  3665 			if (e->state() & Qt::ShiftModifier)
  3666 				colorBranch (((BranchObj*)(dst))->getColor());
  3667 			else	
  3668 				colorSubtree (((BranchObj*)(dst))->getColor());
  3669 		} 
  3670 		return;
  3671 	}
  3672 
  3673 	// Have we been drawing a link?
  3674 	if (drawingLink)
  3675 	{
  3676 		drawingLink=false;
  3677 		// Check if we are over another branch
  3678 		dst=mapCenter->findMapObj(p, NULL);
  3679 		if (dst && lmosel) 
  3680 		{	
  3681 			tmpXLink->setEnd ( ((BranchObj*)(dst)) );
  3682 			tmpXLink->updateXLink();
  3683 			tmpXLink->activate();
  3684 			//saveStateComplete(QString("Activate xLink from %1 to %2").arg(getName(tmpXLink->getBegin())).arg(getName(tmpXLink->getEnd())) );	//FIXME undoCommand
  3685 		} else
  3686 		{
  3687 			delete(tmpXLink);
  3688 			tmpXLink=NULL;
  3689 		}
  3690 		return;
  3691 	}
  3692 	
  3693     // Have we been moving something?
  3694     if ( lmosel && movingObj ) 
  3695     {	
  3696 		FloatImageObj *fo=xelection.getFloatImage();
  3697 		if(fo)
  3698 		{
  3699 			// Moved FloatObj. Maybe we need to reposition
  3700 		    QString pold=qpointfToString(movingObj_orgRelPos);
  3701 		    QString pnow=qpointfToString(fo->getRelPos());
  3702 			saveState(
  3703 				fo,
  3704 				"moveRel "+pold,
  3705 				fo,
  3706 				"moveRel "+pnow,
  3707 				QString("Move %1 to relativ position %2").arg(getName(fo)).arg(pnow));
  3708 
  3709 			fo->getParObj()->requestReposition();
  3710 			mapCenter->reposition();
  3711 		}	
  3712 
  3713 		// Check if we are over another branch, but ignore 
  3714 		// any found LMOs, which are FloatObjs
  3715 		dst=mapCenter->findMapObj(mapToScene(e->pos() ), lmosel);
  3716 
  3717 		if (dst && (typeid(*dst)!=typeid(BranchObj) && typeid(*dst)!=typeid(MapCenterObj))) 
  3718 			dst=NULL;
  3719 		
  3720 		if (xelection.type() == MapCenter )
  3721 		{	// FIXME The MapCenter was moved, no savestate yet
  3722 		}
  3723 		
  3724 		if (xelection.type() == Branch )
  3725 		{	// A branch was moved
  3726 			
  3727 			// save the position in case we link to mapcenter
  3728 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  3729 
  3730 			// Reset the temporary drawn link to the original one
  3731 			lmosel->unsetParObjTmp();
  3732 
  3733 			// For Redo we may need to save original selection
  3734 			QString preSelStr=lmosel->getSelectString();
  3735 
  3736 			copyingObj=false;	
  3737 			if (dst ) 
  3738 			{
  3739 				BranchObj* bsel=xelection.getBranch();
  3740 				BranchObj* bdst=(BranchObj*)dst;
  3741 
  3742 				QString preParStr=(bsel->getParObj())->getSelectString();
  3743 				QString preNum=QString::number (bsel->getNum(),10);
  3744 				QString preDstParStr;
  3745 
  3746 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  3747 				{	// Link above dst
  3748 					preDstParStr=dst->getParObj()->getSelectString();
  3749 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum());
  3750 				} else 
  3751 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  3752 				{
  3753 					// Link below dst
  3754 					preDstParStr=dst->getParObj()->getSelectString();
  3755 					bsel->linkTo ( (BranchObj*)(bdst->getParObj()), bdst->getNum()+1);
  3756 				} else	
  3757 				{	// Append to dst
  3758 					preDstParStr=dst->getSelectString();
  3759 					bsel->linkTo (bdst,-1);
  3760 					if (dst->getDepth()==0) bsel->move (savePos);
  3761 				} 
  3762 				QString postSelStr=lmosel->getSelectString();
  3763 				QString postNum=QString::number (bsel->getNum(),10);
  3764 
  3765 				QString undoCom="linkTo (\""+ 
  3766 					preParStr+ "\"," + preNum  +"," + 
  3767 					QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  3768 
  3769 				QString redoCom="linkTo (\""+ 
  3770 					preDstParStr + "\"," + postNum + "," +
  3771 					QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  3772 
  3773 				saveState (
  3774 					postSelStr,undoCom,
  3775 					preSelStr, redoCom,
  3776 					QString("Relink %1 to %2").arg(getName(bsel)).arg(getName(dst)) );
  3777 			} else
  3778 				if (lmosel->getDepth()==1)
  3779 				{
  3780 					// The select string might be different _after_ moving around.
  3781 					// Therefor reposition and then use string of old selection, too
  3782 					mapCenter->reposition();
  3783 
  3784 					QString ps=qpointfToString ( lmosel->getRelPos() );
  3785 					saveState(
  3786 						lmosel->getSelectString(), "moveRel "+qpointfToString(movingObj_orgRelPos), 
  3787 						preSelStr, "moveRel "+ps, 
  3788 						QString("Move %1 to relative position %2").arg(getName(lmosel)).arg(ps));
  3789 			
  3790 				}
  3791 			// Draw the original link, before selection was moved around
  3792 			mapCenter->reposition();
  3793 		}
  3794 		// Finally resize scene, if needed
  3795 		scene()->update();
  3796 		movingObj=NULL;		
  3797 
  3798 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  3799 		updateActions();
  3800 	} else 
  3801 		// maybe we moved View: set old cursor
  3802 		setCursor (Qt::ArrowCursor);
  3803     
  3804 }
  3805 
  3806 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  3807 {
  3808 	if (e->button() == Qt::LeftButton )
  3809 	{
  3810 		QPointF p = mapToScene(e->pos());
  3811 		LinkableMapObj *lmo=mapCenter->findMapObj(p, NULL);
  3812 		if (lmo) {	// MapObj was found
  3813 			// First select the MapObj than edit heading
  3814 			xelection.select(lmo);
  3815 			mainWindow->editHeading();
  3816 		}
  3817 	}
  3818 }
  3819 
  3820 void MapEditor::resizeEvent (QResizeEvent* e)
  3821 {
  3822 	QGraphicsView::resizeEvent( e );
  3823 }
  3824 
  3825 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  3826 {
  3827 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  3828 	//	cerr << event->format(i) << endl;
  3829 
  3830 	if (event->mimeData()->hasImage())
  3831 		event->acceptProposedAction();
  3832 	else	
  3833 		if (event->mimeData()->hasUrls())
  3834 			event->acceptProposedAction();
  3835 }
  3836 
  3837 void MapEditor::dragMoveEvent(QDragMoveEvent *event)
  3838 {
  3839 }
  3840 
  3841 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  3842 {
  3843 	event->accept();
  3844 }
  3845 
  3846 void MapEditor::dropEvent(QDropEvent *event)
  3847 {
  3848 	BranchObj *sel=xelection.getBranch();
  3849 	if (sel)
  3850 	{
  3851 		QList <QUrl> uris;
  3852 		if (event->mimeData()->hasImage()) 
  3853 		{
  3854 			 QVariant imageData = event->mimeData()->imageData();
  3855 			 addFloatImageInt (qvariant_cast<QPixmap>(imageData));
  3856 		} else
  3857 		if (event->mimeData()->hasUrls())
  3858 			uris=event->mimeData()->urls();
  3859 
  3860 		if (uris.count()>0)
  3861 		{
  3862 			QStringList files;
  3863 			QString s;
  3864 			QString heading;
  3865 			BranchObj *bo;
  3866 			for (int i=0; i<uris.count();++i)
  3867 			{
  3868 				bo=sel->addBranch();
  3869 				if (bo)
  3870 				{
  3871 					s=uris.at(i).toLocalFile();
  3872 					if (!s.isEmpty()) 
  3873 					{
  3874                        QString file = QDir::convertSeparators(s);
  3875                        heading = QFileInfo(file).baseName();
  3876                        files.append(file);
  3877                        if (file.endsWith(".vym", false))
  3878                            bo->setVymLink(file);
  3879                        else
  3880                            bo->setURL(uris.at(i).toString());
  3881                    } else 
  3882 				   {
  3883                        bo->setURL(uris.at(i).toString());
  3884                    }
  3885 
  3886                    if (!heading.isEmpty())
  3887                        bo->setHeading(heading);
  3888                    else
  3889                        bo->setHeading(uris.at(i).toString());
  3890 				}
  3891 			}
  3892 			mapCenter->reposition();
  3893 		}
  3894 	}	
  3895 	event->acceptProposedAction();
  3896 }
  3897 
  3898 /*
  3899 void MapEditor::contentsDropEvent(QDropEvent *event) 
  3900 {
  3901 
  3902 		} else if (event->provides("application/x-moz-file-promise-url") && 
  3903 			 event->provides("application/x-moz-nativeimage")) 
  3904 		{
  3905 			// Contains url to the img src in unicode16
  3906 			QByteArray d = event->encodedData("application/x-moz-file-promise-url");
  3907 			QString url = QString((const QChar*)d.data(),d.size()/2);
  3908 			fetchImage(url);
  3909 			event->accept();
  3910 			update=true;
  3911 		} else if (event->provides ("text/uri-list"))
  3912 		{	// Uris provided e.g. by konqueror
  3913 			Q3UriDrag::decode (event,uris);
  3914 		} else if (event->provides ("_NETSCAPE_URL"))
  3915 		{	// Uris provided by Mozilla
  3916 		  QStringList l = QStringList::split("\n", event->encodedData("_NETSCAPE_URL"));
  3917 		  uris.append(l[0]);
  3918 		  heading = l[1];
  3919 		} else if (event->provides("text/html")) {
  3920 
  3921 		  // Handels text mime types
  3922 		  // Look like firefox allways handle text as unicode16 (2 bytes per char.)
  3923 		  QByteArray d = event->encodedData("text/html");
  3924 		  QString text;
  3925 		  if (isUnicode16(d)) 
  3926 		    text = QString((const QChar*)d.data(),d.size()/2);
  3927 		  else 
  3928 		    text = QString(d);
  3929 
  3930 		  textEditor->setText(text);
  3931 
  3932 		  event->accept();
  3933 		  update=true;
  3934 		} else if (event->provides("text/plain")) {
  3935 		  QByteArray d = event->encodedData("text/plain");
  3936 		  QString text;
  3937 		  if (isUnicode16(d))
  3938 		    text = QString((const QChar*)d.data(),d.size()/2);
  3939 		  else 
  3940 		    text = QString(d);
  3941 
  3942 		  textEditor->setText(text);
  3943 		  
  3944 		  event->accept();
  3945 		  update= true;
  3946 		}
  3947 
  3948 		*/
  3949 
  3950 
  3951 
  3952 bool isUnicode16(const QByteArray &d) 
  3953 {
  3954   // TODO: make more precise check for unicode 16.
  3955   // Guess unicode16 if any of second bytes are zero
  3956   unsigned int length = max(0,d.size()-2)/2;
  3957   for (unsigned int i = 0; i<length ; i++)
  3958     if (d.at(i*2+1)==0) return true;
  3959   return false;
  3960 }
  3961       
  3962 void MapEditor::addFloatImageInt (const QPixmap &img) 
  3963 {
  3964 	BranchObj *bo=xelection.getBranch();
  3965 	if (bo)
  3966   {
  3967     //FIXME XXX saveStateChangingPart(selection,QString("Add floatimage to %1").arg(getName(bo)));
  3968     //QString fn=fd->selectedFile();
  3969     //lastImageDir=fn.left(fn.findRev ("/"));
  3970 	FloatImageObj *fio=bo->addFloatImage();
  3971     fio->load(img);
  3972     fio->setOriginalFilename("Image added by Drag and Drop");
  3973     mapCenter->reposition();
  3974     scene()->update();
  3975   }
  3976 }
  3977 
  3978 /*
  3979 
  3980 void MapEditor::imageDataFetched(const QByteArray &a, Q3NetworkOperation * / *nop* /) 
  3981 {
  3982   if (!imageBuffer) imageBuffer = new QBuffer();
  3983   if (!imageBuffer->isOpen()) {
  3984     imageBuffer->open(QIODevice::WriteOnly | QIODevice::Append);
  3985   }
  3986   imageBuffer->at(imageBuffer->at()+imageBuffer->writeBlock(a));
  3987 }
  3988 
  3989 
  3990 void MapEditor::imageDataFinished(Q3NetworkOperation *nop) 
  3991 {
  3992 	if (nop->state()==Q3NetworkProtocol::StDone) {
  3993 		QPixmap img(imageBuffer->buffer());
  3994 		addFloatImageInt (img);
  3995 	}
  3996 
  3997 	if (imageBuffer) {
  3998 		imageBuffer->close();
  3999 		if (imageBuffer) {
  4000 			imageBuffer->close();
  4001 			delete imageBuffer;
  4002 			imageBuffer = 0;
  4003 		}
  4004 	}
  4005 }
  4006 
  4007 void MapEditor::fetchImage(const QString &url) 
  4008 {
  4009   if (urlOperator) {
  4010     urlOperator->stop();
  4011     disconnect(urlOperator);
  4012     delete urlOperator;
  4013   }
  4014   
  4015   urlOperator = new Q3UrlOperator(url);
  4016   connect(urlOperator, SIGNAL(finished(Q3NetworkOperation *)), 
  4017 	  this, SLOT(imageDataFinished(Q3NetworkOperation*)));
  4018 
  4019   connect(urlOperator, SIGNAL(data(const QByteArray &, Q3NetworkOperation *)),
  4020 	  this, SLOT(imageDataFetched(const QByteArray &, Q3NetworkOperation *)));
  4021   urlOperator->get();
  4022 }
  4023 */
  4024