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