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