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