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