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