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