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