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