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