mapeditor.cpp
author insilmaril
Tue, 08 Sep 2009 12:15:39 +0000
changeset 792 7d67be709091
parent 791 f1006de05c54
child 793 cac93797c580
permissions -rw-r--r--
First results in moving colliding MapCenters apart
     1 #include "mapeditor.h"
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include <typeinfo>
     6 
     7 #include <QObject>
     8 
     9 #include "branchitem.h"
    10 #include "geometry.h"
    11 #include "mainwindow.h"
    12 #include "misc.h"
    13 #include "warningdialog.h"
    14 #include "xlinkitem.h"
    15 
    16 
    17 extern int statusbarTime;
    18 extern Main *mainWindow;
    19 extern QString tmpVymDir;
    20 extern QString clipboardDir;
    21 extern QString clipboardFile;
    22 extern bool clipboardEmpty;
    23 extern bool debug;
    24 
    25 extern QMenu* branchContextMenu;
    26 extern QMenu* branchAddContextMenu;
    27 extern QMenu* branchRemoveContextMenu;
    28 extern QMenu* branchLinksContextMenu;
    29 extern QMenu* branchXLinksContextMenuEdit;
    30 extern QMenu* branchXLinksContextMenuFollow;
    31 extern QMenu* floatimageContextMenu;
    32 extern QMenu* canvasContextMenu;
    33 
    34 extern Settings settings;
    35 extern QString iconPath;
    36 
    37 ///////////////////////////////////////////////////////////////////////
    38 ///////////////////////////////////////////////////////////////////////
    39 MapEditor::MapEditor( VymModel *vm) 
    40 {
    41 	//cout << "Constructor ME "<<this<<endl;
    42 	mapScene= new QGraphicsScene(NULL);
    43 	mapScene->setBackgroundBrush (QBrush(Qt::white, Qt::SolidPattern));
    44 
    45 	zoomFactor=zoomFactorTarget=1;
    46 
    47 	model=vm;
    48 	model->setScene (mapScene);
    49 	model->registerEditor(this);
    50 	model->makeDefault();	// No changes in model so far
    51 
    52     setScene (mapScene);
    53 
    54     printer=NULL;
    55 
    56 	// Create bitmap cursors, platform dependant
    57 	HandOpenCursor=QCursor (QPixmap(iconPath+"cursorhandopen.png"),1,1);		
    58 	PickColorCursor=QCursor ( QPixmap(iconPath+"cursorcolorpicker.png"), 5,27 ); 
    59 	CopyCursor=QCursor ( QPixmap(iconPath+"cursorcopy.png"), 1,1 ); 
    60 	XLinkCursor=QCursor ( QPixmap(iconPath+"cursorxlink.png"), 1,7 ); 
    61 
    62 	//setFocusPolicy (Qt::StrongFocus);	//FIXME-3
    63 
    64 	pickingColor=false;
    65 	drawingLink=false;
    66 	copyingObj=false;
    67 
    68     editingBO=NULL;
    69     movingObj=NULL;
    70 
    71 	printFrame=true;
    72 	printFooter=true;
    73 
    74 	setAcceptDrops (true);	
    75 
    76 	//model->reposition();	//FIXME-3 really still needed?
    77 
    78 
    79 	// Shortcuts and actions
    80 	QAction *a;
    81     a = new QAction("Select upper branch", this);
    82 	a->setShortcut (Qt::Key_Up );
    83 	a->setShortcutContext (Qt::WidgetShortcut);
    84 	addAction (a);
    85     connect( a, SIGNAL( triggered() ), this, SLOT( cursorUp() ) );
    86 
    87     a = new QAction( "Select lower branch",this);
    88 	a->setShortcut ( Qt::Key_Down );
    89 	a->setShortcutContext (Qt::WidgetShortcut);
    90 	addAction (a);
    91     connect( a, SIGNAL( triggered() ), this, SLOT( cursorDown() ) );
    92 
    93     a = new QAction( "Select left branch", this);
    94 	a->setShortcut (Qt::Key_Left );
    95 //	a->setShortcutContext (Qt::WindowShortcut);
    96 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
    97 	addAction (a);
    98     connect( a, SIGNAL( triggered() ), this, SLOT( cursorLeft() ) );
    99 
   100     a = new QAction( "Select child branch", this);
   101 	a->setShortcut (Qt::Key_Right);
   102 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
   103 	addAction (a);
   104     connect( a, SIGNAL( triggered() ), this, SLOT( cursorRight() ) );
   105 
   106     a = new QAction(  "Select first branch", this);
   107 	a->setShortcut (Qt::Key_Home );
   108 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
   109 	addAction (a);
   110     connect( a, SIGNAL( triggered() ), this, SLOT( cursorFirst() ) );
   111 
   112     a = new QAction( "Select last branch",this);
   113 	a->setShortcut ( Qt::Key_End );
   114 	a->setShortcutContext (Qt::WidgetWithChildrenShortcut);
   115 	addAction (a);
   116     connect( a, SIGNAL( triggered() ), this, SLOT( cursorLast() ) );
   117 
   118 	// Action to embed LineEdit for heading in Scene
   119 	editingHeading=false;
   120 	lineEdit=new QLineEdit;
   121 	lineEdit->hide();
   122 	QGraphicsProxyWidget *pw=scene()->addWidget (lineEdit);
   123 	pw->setZValue (Z_LINEEDIT);
   124 
   125 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
   126 	a->setShortcut ( Qt::Key_Return );					//Edit heading
   127 	addAction (a);
   128     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
   129 	a = new QAction( tr( "Edit heading","MapEditor" ), this);
   130 	a->setShortcut ( Qt::Key_Enter);					//Edit heading
   131 	addAction (a);
   132     connect( a, SIGNAL( triggered() ), this, SLOT( editHeading() ) );
   133 
   134 	// Selections
   135 	selectionColor =QColor (255,255,0);
   136 	
   137 
   138 	// Attributes	//FIXME-2 testing only...
   139 	QString k;
   140 	AttributeDef *ad;
   141 	attrTable= new AttributeTable();
   142 	k="A - StringList";
   143 	ad=attrTable->addKey (k,StringList);
   144 	if (ad)
   145 	{
   146 		QStringList sl;
   147 		sl <<"val 1"<<"val 2"<< "val 3";
   148 		ad->setValue (QVariant (sl));
   149 	}
   150 	//attrTable->addValue ("Key A","P 1");
   151 	//attrTable->addValue ("Key A","P 2");
   152 	//attrTable->addValue ("Key A","P 3");
   153 	//attrTable->addValue ("Key A","P 4");
   154 	k="B - FreeString";
   155 	ad=attrTable->addKey (k,FreeString);
   156 	if (ad)
   157 	{
   158 		//attrTable->addValue ("Key B","w1");
   159 		//attrTable->addValue ("Key B","w2");
   160 	}
   161 	k="C - UniqueString";
   162 	ad=attrTable->addKey (k,UniqueString);
   163 	if (ad)
   164 	{
   165 	//attrTable->addKey ("Key Prio");
   166 	//attrTable->addValue ("Key Prio","Prio 1");
   167 	//attrTable->addValue ("Key Prio","Prio 2");
   168 	}
   169 }
   170 
   171 MapEditor::~MapEditor()
   172 {
   173 	//cout <<"Destructor MapEditor for "<<model->getMapName().toStdString()<<endl;
   174 	model->unregisterEditor(this);
   175 }
   176 
   177 VymModel* MapEditor::getModel()
   178 {
   179     return model;
   180 }
   181 
   182 QGraphicsScene * MapEditor::getScene()
   183 {
   184     return mapScene;
   185 }
   186 
   187 void MapEditor::scrollTo (const QModelIndex &index)
   188 {
   189 	if (index.isValid())
   190 	{
   191 		LinkableMapObj* lmo=NULL;
   192 		TreeItem *ti= static_cast<TreeItem*>(index.internalPointer());
   193 		if (ti->getType()==TreeItem::Image ||ti->isBranchLikeType() )
   194 			lmo=((MapItem*)ti)->getLMO();
   195 		if (lmo) setScrollBarPosTarget (lmo->getBBox() );
   196 	}
   197 }
   198 
   199 void MapEditor::setScrollBarPosTarget (const QRectF &rect)
   200 {
   201 	// Code copied from Qt sources
   202 	int xmargin=50;
   203 	int ymargin=50;
   204 
   205     qreal width = viewport()->width();
   206     qreal height = viewport()->height();
   207     QRectF viewRect = matrix().mapRect(rect);
   208 
   209     qreal left = horizontalScrollBar()->value();
   210     qreal right = left + width;
   211     qreal top = verticalScrollBar()->value();
   212     qreal bottom = top + height;
   213 
   214     if (viewRect.left() <= left + xmargin) {
   215         // need to scroll from the left
   216   //      if (!d->leftIndent)
   217             scrollBarPosTarget.setX(int(viewRect.left() - xmargin - 0.5));
   218     }
   219     if (viewRect.right() >= right - xmargin) {
   220         // need to scroll from the right
   221 //        if (!d->leftIndent)
   222             scrollBarPosTarget.setX(int(viewRect.right() - width + xmargin + 0.5));
   223     }
   224     if (viewRect.top() <= top + ymargin) {
   225         // need to scroll from the top
   226    //     if (!d->topIndent)
   227             scrollBarPosTarget.setY(int(viewRect.top() - ymargin - 0.5));
   228     }
   229     if (viewRect.bottom() >= bottom - ymargin) {
   230         // need to scroll from the bottom
   231 //        if (!d->topIndent)
   232             scrollBarPosTarget.setY(int(viewRect.bottom() - height + ymargin + 0.5));
   233     }
   234 
   235 	if (scrollBarPosAnimation.state()==QtAbstractAnimation::Running)
   236 		scrollBarPosAnimation.stop();
   237 	scrollBarPosAnimation.setTargetObject (this);
   238 	scrollBarPosAnimation.setPropertyName ("scrollBarPos");
   239 	scrollBarPosAnimation.setDuration(1000);
   240 	scrollBarPosAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
   241 	scrollBarPosAnimation.setStartValue(
   242 		QPointF (horizontalScrollBar()->value() ,
   243 		         verticalScrollBar()->value() ) );
   244 	scrollBarPosAnimation.setEndValue(scrollBarPosTarget);
   245 	scrollBarPosAnimation.start();
   246 }
   247 
   248 QPointF MapEditor::getScrollBarPosTarget()
   249 {
   250     return scrollBarPosTarget;
   251 }
   252 
   253 
   254 void MapEditor::setScrollBarPos(const QPointF &p)
   255 {
   256     scrollBarPos=p;
   257 	horizontalScrollBar()->setValue(int(p.x()));
   258 	verticalScrollBar()->setValue(int(p.y()));
   259 }
   260 
   261 QPointF MapEditor::getScrollBarPos()
   262 {
   263     return scrollBarPos;
   264 }
   265 
   266 void MapEditor::setZoomFactorTarget (const qreal &zft)
   267 {
   268 	zoomFactorTarget=zft;
   269 	if (zoomAnimation.state()==QtAbstractAnimation::Running)
   270 		zoomAnimation.stop();
   271 	//zoomAnimation=QtPropertyAnimation(this, "zoomFactor");
   272 	zoomAnimation.setTargetObject (this);
   273 	zoomAnimation.setPropertyName ("zoomFactor");
   274 	zoomAnimation.setDuration(1000);
   275 	zoomAnimation.setEasingCurve ( QtEasingCurve::OutQuint);
   276 	zoomAnimation.setStartValue(zoomFactor);
   277 	zoomAnimation.setEndValue(zft);
   278 	zoomAnimation.start();
   279 }
   280 
   281 qreal MapEditor::getZoomFactorTarget()
   282 {
   283     return zoomFactorTarget;
   284 }
   285 
   286 
   287 void MapEditor::setZoomFactor(const qreal &zf)
   288 {
   289     zoomFactor=zf;
   290 	setMatrix (QMatrix(zf, 0, 0, zf, 0, 0),false );
   291 }
   292 
   293 qreal MapEditor::getZoomFactor()
   294 {
   295     return zoomFactor;
   296 }
   297 
   298 void MapEditor::print()
   299 {
   300 	if ( !printer ) 
   301 	{
   302 		printer = new QPrinter;
   303 		printer->setColorMode (QPrinter::Color);
   304 		printer->setPrinterName (settings.value("/mainwindow/printerName",printer->printerName()).toString());
   305 		printer->setOutputFormat((QPrinter::OutputFormat)settings.value("/mainwindow/printerFormat",printer->outputFormat()).toInt());
   306 		printer->setOutputFileName(settings.value("/mainwindow/printerFileName",printer->outputFileName()).toString());
   307 	}
   308 
   309 	QRectF totalBBox=getTotalBBox();
   310 
   311 	// Try to set orientation automagically
   312 	// Note: Interpretation of generated postscript is amibiguous, if 
   313 	// there are problems with landscape mode, see
   314 	// http://sdb.suse.de/de/sdb/html/jsmeix_print-cups-landscape-81.html
   315 
   316 	if (totalBBox.width()>totalBBox.height())
   317 		// recommend landscape
   318 		printer->setOrientation (QPrinter::Landscape);
   319 	else	
   320 		// recommend portrait
   321 		printer->setOrientation (QPrinter::Portrait);
   322 
   323 	if ( printer->setup(this) ) 
   324 	// returns false, if printing is canceled
   325 	{
   326 		QPainter pp(printer);
   327 
   328 		pp.setRenderHint(QPainter::Antialiasing,true);
   329 
   330 		// Don't print the visualisation of selection
   331 		model->unselect();
   332 
   333 		QRectF mapRect=totalBBox;
   334 		QGraphicsRectItem *frame=NULL;
   335 
   336 		if (printFrame) 
   337 		{
   338 			// Print frame around map
   339 			mapRect.setRect (totalBBox.x()-10, totalBBox.y()-10, 
   340 				totalBBox.width()+20, totalBBox.height()+20);
   341 			frame=mapScene->addRect (mapRect, QPen(Qt::black),QBrush(Qt::NoBrush));
   342 			frame->setZValue(0);
   343 			frame->show();    
   344 		}		
   345 
   346 
   347 		double paperAspect = (double)printer->width()   / (double)printer->height();
   348 		double   mapAspect = (double)mapRect.width() / (double)mapRect.height();
   349 		int viewBottom;
   350 		if (mapAspect>=paperAspect)
   351 		{
   352 			// Fit horizontally to paper width
   353 			//pp.setViewport(0,0, printer->width(),(int)(printer->width()/mapAspect) );	
   354 			viewBottom=(int)(printer->width()/mapAspect);	
   355 		}	else
   356 		{
   357 			// Fit vertically to paper height
   358 			//pp.setViewport(0,0,(int)(printer->height()*mapAspect),printer->height());	
   359 			viewBottom=printer->height();	
   360 		}	
   361 		
   362 		if (printFooter) 
   363 		{
   364 			// Print footer below map
   365 			QFont font;		
   366 			font.setPointSize(10);
   367 			pp.setFont (font);
   368 			QRectF footerBox(0,viewBottom,printer->width(),15);
   369 			// FIXME-3 fileName not any longer available here: pp.drawText ( footerBox,Qt::AlignLeft,"VYM - " +fileName);
   370 			pp.drawText ( footerBox, Qt::AlignRight, QDate::currentDate().toString(Qt::TextDate));
   371 		}
   372 		mapScene->render (
   373 			&pp, 
   374 			QRectF (0,0,printer->width(),printer->height()-15),
   375 			QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height())
   376 		);
   377 		
   378 		// Viewport has paper dimension
   379 		if (frame)  delete (frame);
   380 
   381 		// Restore selection
   382 		model->reselect();
   383 
   384 		// Save settings in vymrc
   385 		settings.writeEntry("/mainwindow/printerName",printer->printerName());
   386 		settings.writeEntry("/mainwindow/printerFormat",printer->outputFormat());
   387 		settings.writeEntry("/mainwindow/printerFileName",printer->outputFileName());
   388 	}
   389 }
   390 
   391 QRectF MapEditor::getTotalBBox()	//FIXME-2 needed e.g. for image export
   392 {
   393 	QRectF r;
   394 /*
   395 	for (int i=0;i<rootItem->branchCount(); i++)
   396 		r=addBBox (rootItem->getBranchNum(i)->getTotalBBox(), r);
   397 */ 
   398 	return r;	
   399 }
   400 
   401 
   402 QPixmap MapEditor::getPixmap()
   403 {
   404 	QRectF mapRect=getTotalBBox();
   405 	QPixmap pix((int)mapRect.width()+2,(int)mapRect.height()+1);
   406 	QPainter pp (&pix);
   407 	
   408 	pp.setRenderHints(renderHints());
   409 
   410 	// Don't print the visualisation of selection
   411 	model->unselect();
   412 
   413 	mapScene->render (	&pp, 
   414 		QRectF(0,0,mapRect.width()+2,mapRect.height()+2),
   415 		QRectF(mapRect.x(),mapRect.y(),mapRect.width(),mapRect.height() ));
   416 
   417 	// Restore selection
   418 	model->reselect();
   419 	
   420 	return pix;
   421 }
   422 
   423 
   424 void MapEditor::setAntiAlias (bool b)
   425 {
   426 	setRenderHint(QPainter::Antialiasing,b);
   427 }
   428 
   429 void MapEditor::setSmoothPixmap(bool b)
   430 {
   431 	setRenderHint(QPainter::SmoothPixmapTransform,b);
   432 }
   433 
   434 TreeItem* MapEditor::findMapItem (QPointF p,TreeItem *exclude)
   435 {
   436 	// Start with mapcenter, no images allowed at rootItem
   437 	int i=0;
   438 	BranchItem *bi=model->getRootItem()->getFirstBranch();
   439 	TreeItem *found=NULL;
   440 	while (bi)
   441 	{
   442 		found=bi->findMapItem (p, exclude);
   443 		if (found) return found;
   444 		i++;
   445 		bi=model->getRootItem()->getBranchNum(i);
   446 	}
   447 	return NULL;
   448 }
   449 
   450 AttributeTable* MapEditor::attributeTable()
   451 {
   452 	return attrTable;
   453 }
   454 
   455 void MapEditor::testFunction1()
   456 {
   457 	/*
   458 	// Code copied from Qt sources
   459 	QRectF rect=model->getSelectedBranchObj()->getBBox();
   460 	int xmargin=50;
   461 	int ymargin=50;
   462 
   463     qreal width = viewport()->width();
   464     qreal height = viewport()->height();
   465     QRectF viewRect = matrix().mapRect(rect);
   466 
   467     qreal left = horizontalScrollBar()->value();
   468     qreal right = left + width;
   469     qreal top = verticalScrollBar()->value();
   470     qreal bottom = top + height;
   471 
   472     if (viewRect.left() <= left + xmargin) {
   473         // need to scroll from the left
   474   //      if (!d->leftIndent)
   475             horizontalScrollBar()->setValue(int(viewRect.left() - xmargin - 0.5));
   476     }
   477     if (viewRect.right() >= right - xmargin) {
   478         // need to scroll from the right
   479 //        if (!d->leftIndent)
   480             horizontalScrollBar()->setValue(int(viewRect.right() - width + xmargin + 0.5));
   481     }
   482     if (viewRect.top() <= top + ymargin) {
   483         // need to scroll from the top
   484    //     if (!d->topIndent)
   485             verticalScrollBar()->setValue(int(viewRect.top() - ymargin - 0.5));
   486     }
   487     if (viewRect.bottom() >= bottom - ymargin) {
   488         // need to scroll from the bottom
   489 //        if (!d->topIndent)
   490             verticalScrollBar()->setValue(int(viewRect.bottom() - height + ymargin + 0.5));
   491     }
   492 	cout << "test1:  hor="<<horizontalScrollBar()->value()<<endl;
   493 	cout << "test1:  ver="<<verticalScrollBar()->value()<<endl;
   494 }
   495 
   496 */
   497 /*
   498 	 QtPropertyAnimation *animation=new QtPropertyAnimation(this, "sceneRect");
   499 	 animation->setDuration(5000);
   500 	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
   501 	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
   502 	 animation->setStartValue(sceneRect() );
   503 	 animation->setEndValue(QRectF(50, 50, 1000, 1000));
   504 
   505 	 animation->start();
   506 */	 
   507 /*
   508 	QDialog *dia= new QDialog (this);
   509 	dia->setGeometry (50,50,10,10);
   510 
   511      dia->show();
   512      dia ->raise();
   513 
   514 	 QtPropertyAnimation *animation=new QtPropertyAnimation(dia, "geometry");
   515 	 animation->setDuration(1000);
   516 	 //animation->setEasingCurve ( QtEasingCurve::OutElastic);
   517 	 animation->setEasingCurve ( QtEasingCurve::OutQuint);
   518 	 animation->setStartValue(QRect(50, 50, 10, 10));
   519 	 animation->setEndValue(QRect(250, 250, 100, 100));
   520 
   521 	 animation->start();
   522  */
   523 
   524 /* FIXME-4 Hide hidden stuff temporary, maybe add this as regular function somewhere
   525 	if (hidemode==HideNone)
   526 	{
   527 		setHideTmpMode (HideExport);
   528 		mapCenter->calcBBoxSizeWithChilds();
   529 		QRectF totalBBox=mapCenter->getTotalBBox();
   530 		QRectF mapRect=totalBBox;
   531 		QCanvasRectangle *frame=NULL;
   532 
   533 		cout << "  map has =("<<totalBBox.x()<<","<<totalBBox.y()<<","<<totalBBox.width()<<","<<totalBBox.height()<<")\n";
   534 	
   535 		mapRect.setRect (totalBBox.x(), totalBBox.y(), 
   536 			totalBBox.width(), totalBBox.height());
   537 		frame=new QCanvasRectangle (mapRect,mapScene);
   538 		frame->setBrush (QColor(white));
   539 		frame->setPen (QColor(black));
   540 		frame->setZValue(0);
   541 		frame->show();    
   542 	}	
   543 	else	
   544 	{
   545 		setHideTmpMode (HideNone);
   546 	}	
   547 	cout <<"  hidemode="<<hidemode<<endl;
   548 	*/
   549 
   550 /*
   551 	// Toggle hidemode
   552 	if (hidemode==HideExport)
   553 		setHideTmpMode (HideNone);
   554 	else	
   555 		setHideTmpMode (HideExport);
   556 */		
   557 
   558 }
   559 	
   560 void MapEditor::testFunction2()
   561 {
   562 	// Create list with all bounding polygons
   563 	QList <MapObj*> mapobjects;
   564 	QList <ConvexPolygon> polys; 
   565 	QList <Vector> vectors;
   566 	QList <Vector> orgpos;
   567 	BranchItem *bi;
   568 	BranchObj *bo;
   569 	TreeItem *ri=model->getRootItem();
   570 	for (int i=0;i<ri->branchCount();++i)
   571 	{
   572 		bi=ri->getBranchNum (i);
   573 		bo=(BranchObj*)bi->getLMO();
   574 		if (bo)
   575 		{
   576 			mapobjects.append (bo);
   577 			polys.append(bo->getBoundingPolygon());
   578 			polys[i].calcCentroid();
   579 			vectors.append (QPointF(0,0));
   580 			orgpos.append (polys[i].at(0));
   581 		}	
   582 	}
   583 
   584 	// Iterate moving bounding polygons until we have no more collisions
   585 	int collisions=1;
   586 	while (collisions>0)
   587 	{
   588 		collisions=0;
   589 		for (int i=0; i<polys.size()-1; ++i)
   590 		{
   591 			for (int j=i+1; j<polys.size();++j)
   592 			{
   593 				if (polygonCollision (polys.at(i),polys.at(j), QPointF(0,0)).intersect )
   594 				{
   595 					collisions++;
   596 					Vector v=polys.at(j).centroid()-polys.at(i).centroid();
   597 					// Move also away if centroids are identical
   598 					if (v.isNull()) 
   599 					{
   600 						v.setX (rand()%200 -100);
   601 						v.setY (rand()%200 -100);
   602 					}
   603 					v.normalize();
   604 					cout << "v="<<v<<endl;
   605 					v.scale (2);
   606 					vectors[j]=v;
   607 					vectors[i]=v;
   608 					vectors[i].invert();
   609 				} 
   610 			}
   611 		}
   612 		for (int i=0;i<vectors.size();i++)
   613 			polys[i].translate (vectors[i]);
   614 		//cout << "Collisions: "<<collisions<<endl;
   615 	}	
   616 
   617 	// Finally move the real objects and update 
   618 	for (int i=0;i<polys.size();i++)
   619 	{
   620 		Vector v=polys[i].at(0)-orgpos[i];
   621 		mapobjects[i]->moveBy(v.x(),v.y() );
   622 	}	
   623 	model->reposition();	
   624 }
   625 
   626 BranchItem* MapEditor::getBranchDirectAbove (BranchItem *bi)
   627 {
   628 	if (bi)
   629 	{
   630 		int i=bi->num();
   631 		if (i>0) return bi->parent()->getBranchNum(i-1);
   632 	}
   633 	return NULL;
   634 }
   635 
   636 BranchItem* MapEditor::getBranchAbove (BranchItem *selbi)
   637 {
   638 	if (selbi)
   639 	{
   640 		int dz=selbi->depth();	// original depth
   641 		bool invert=false;
   642 		if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
   643 			invert=true;
   644 
   645 		BranchItem *bi;
   646 
   647 		// Look for branch with same parent but directly above
   648 		if (dz==1 && invert)
   649 			bi=getBranchDirectBelow(selbi);
   650 		else
   651 			bi=getBranchDirectAbove (selbi);
   652 
   653 		if (bi) 
   654 			// direct predecessor
   655 			return bi;
   656 
   657 		// Go towards center and look for predecessor
   658 		while (selbi->depth()>0)
   659 		{
   660 			selbi=(BranchItem*)(selbi->parent());
   661 			if (selbi->depth()==1 && invert)
   662 				bi=getBranchDirectBelow (selbi);
   663 			else
   664 				bi=getBranchDirectAbove (selbi);
   665 			if (bi)
   666 			{
   667 				// turn 
   668 				selbi=bi;
   669 				while (selbi->depth()<dz)
   670 				{
   671 					// try to get back to original depth dz
   672 					bi=selbi->getLastBranch();
   673 					if (!bi) 
   674 					{
   675 						return selbi;
   676 					}
   677 					selbi=bi;
   678 				}
   679 				return selbi;
   680 			}
   681 		}
   682 	}
   683 	return NULL;
   684 }
   685 
   686 BranchItem* MapEditor::getBranchDirectBelow(BranchItem *bi)
   687 {
   688 	if (bi)
   689 	{
   690 		int i=bi->num();
   691 		if (i+1<bi->parent()->branchCount()) return bi->parent()->getBranchNum(i+1);
   692 	}
   693 	return NULL;
   694 }
   695 
   696 BranchItem* MapEditor::getBranchBelow (BranchItem *selbi)
   697 {
   698 	if (selbi)
   699 	{
   700 		BranchItem *bi;
   701 		int dz=selbi->depth();	// original depth
   702 		bool invert=false;
   703 		if (selbi->getLMO()->getOrientation()==LinkableMapObj::LeftOfCenter)
   704 			invert=true;
   705 
   706 
   707 		// Look for branch with same parent but directly below
   708 		if (dz==1 && invert)
   709 			bi=getBranchDirectAbove (selbi);
   710 		else
   711 			bi=getBranchDirectBelow (selbi);
   712 		if (bi) 
   713 			// direct successor
   714 			return bi;
   715 
   716 
   717 		// Go towards center and look for neighbour
   718 		while (selbi->depth()>0)
   719 		{
   720 			selbi=(BranchItem*)(selbi->parent());
   721 			if (selbi->depth()==1 && invert)
   722 				bi=getBranchDirectAbove (selbi);
   723 			else
   724 				bi=getBranchDirectBelow (selbi);
   725 			if (bi)
   726 			{
   727 				// turn 
   728 				selbi=bi;
   729 				while (selbi->depth()<dz)
   730 				{
   731 					// try to get back to original depth dz
   732 					bi=selbi->getFirstBranch();
   733 					if (!bi) 
   734 					{
   735 						return selbi;
   736 					}
   737 					selbi=bi;
   738 				}
   739 				return selbi;
   740 			}
   741 		}
   742 	}
   743 	return NULL;
   744 }
   745 
   746 BranchItem* MapEditor::getLeftBranch (BranchItem *bi)
   747 {
   748 	if (bi)
   749 	{
   750 		if (bi->depth()==0)
   751 			// Special case: use alternative selection index
   752 			return bi->getLastSelectedBranchAlt();	
   753 		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::RightOfCenter)	
   754 			// right of center
   755 			return (BranchItem*)(bi->parent());
   756 		else
   757 			// left of center
   758 			if (bi->getType()== TreeItem::Branch )
   759 				return bi->getLastSelectedBranch();
   760 	}
   761 	return NULL;
   762 }
   763 
   764 BranchItem* MapEditor::getRightBranch(BranchItem *bi)
   765 {
   766 	if (bi)
   767 	{
   768 		if (bi->depth()==0) return bi->getLastSelectedBranch();	
   769 		if (bi->getBranchObj()->getOrientation()==LinkableMapObj::LeftOfCenter)	
   770 			// left of center
   771 			return (BranchItem*)(bi->parent());
   772 		else
   773 			// right of center
   774 			if (bi->getType()== TreeItem::Branch )
   775 				return (BranchItem*)bi->getLastSelectedBranch();
   776 	}
   777 	return NULL;
   778 }
   779 
   780 
   781 
   782 void MapEditor::cursorUp()
   783 {
   784 	BranchItem *bi=model->getSelectedBranch();
   785 	if (bi) model->select (getBranchAbove(bi));
   786 }
   787 
   788 void MapEditor::cursorDown()	
   789 
   790 {
   791 	BranchItem *bi=model->getSelectedBranch();
   792 	if (bi) model->select (getBranchBelow(bi));
   793 }
   794 
   795 void MapEditor::cursorLeft()
   796 {
   797 	BranchItem *bi=getLeftBranch (model->getSelectedBranch());
   798 	if (bi) model->select (bi);
   799 }
   800 
   801 void MapEditor::cursorRight()	
   802 {
   803 	BranchItem *bi=getRightBranch (model->getSelectedBranch());
   804 	if (bi) model->select (bi);
   805 }
   806 
   807 void MapEditor::cursorFirst()	
   808 {
   809 	model->selectFirstBranch();
   810 }
   811 
   812 void MapEditor::cursorLast()	
   813 {
   814 	model->selectLastBranch();
   815 }
   816 
   817 
   818 void MapEditor::editHeading()
   819 {
   820 	if (editingHeading)
   821 	{
   822 		editHeadingFinished();
   823 		return;
   824 	}
   825 	BranchObj *bo=model->getSelectedBranchObj();
   826 	BranchItem *bi=model->getSelectedBranch();
   827 	if (bo)	
   828 	{
   829 		model->setSelectionBlocked(true);
   830 
   831 		lineEdit->setText (bi->getHeading());
   832 		QPoint p = mapTo (this,bo->getAbsPos().toPoint() );
   833 		lineEdit->setGeometry(p.x(),p.y(),230,25);
   834 		lineEdit->selectAll();
   835 		lineEdit->show();
   836 		lineEdit->setFocus();
   837 		lineEdit->grabKeyboard();
   838 		editingHeading=true;
   839 	}
   840 
   841 }
   842 void MapEditor::editHeadingFinished()
   843 {
   844 	editingHeading=false;
   845 	lineEdit->releaseKeyboard();
   846 	model->setHeading (lineEdit->text() );
   847 	model->setSelectionBlocked(false);
   848 	lineEdit->hide();
   849 
   850 	// Maybe reselect previous branch 
   851 	mainWindow->editHeadingFinished (model);
   852 }
   853 
   854 
   855 void MapEditor::contextMenuEvent ( QContextMenuEvent * e )
   856 {
   857 	// Lineedits are already closed by preceding
   858 	// mouseEvent, we don't need to close here.
   859 
   860     QPointF p = mapToScene(e->pos());
   861     TreeItem *ti=findMapItem (p, NULL);
   862     LinkableMapObj* lmo=NULL;
   863 	if (ti) lmo=((MapItem*)ti)->getLMO();
   864 	
   865     if (lmo) 
   866 	{	// MapObj was found
   867 		if (model->getSelectedLMO() != lmo)
   868 		{
   869 			// select the MapObj
   870 			model->select(lmo);
   871 		}
   872 		// Context Menu 
   873 		if (model->getSelectedBranchObj() ) 
   874 		{
   875 			// Context Menu on branch or mapcenter
   876 			branchContextMenu->popup(e->globalPos() );
   877 		} else
   878 		{
   879 			if (model->getSelectedImage() )
   880 			{
   881 				// Context Menu on floatimage
   882 				floatimageContextMenu->popup(e->globalPos() );
   883 			}	
   884 		}	
   885 	} else 
   886 	{ // No MapObj found, we are on the Canvas itself
   887 		// Context Menu on scene
   888 		
   889 		// Open context menu synchronously to position new mapcenter
   890 		model->setContextPos (p);
   891 		canvasContextMenu->exec(e->globalPos() );
   892 		model->unsetContextPos ();
   893     } 
   894 	e->accept();
   895 }
   896 
   897 void MapEditor::keyPressEvent(QKeyEvent* e)
   898 {
   899 	if (e->modifiers() & Qt::ControlModifier)
   900 	{
   901 		switch (mainWindow->getModMode())
   902 		{
   903 			case Main::ModModeColor: 
   904 				setCursor (PickColorCursor);
   905 				break;
   906 			case Main::ModModeCopy: 
   907 				setCursor (CopyCursor);
   908 				break;
   909 			case Main::ModModeXLink: 
   910 				setCursor (XLinkCursor);
   911 				break;
   912 			default :
   913 				setCursor (Qt::ArrowCursor);
   914 				break;
   915 		} 
   916 	}	
   917 }
   918 
   919 void MapEditor::keyReleaseEvent(QKeyEvent* e)
   920 {
   921 	if (!(e->modifiers() & Qt::ControlModifier))
   922 		setCursor (Qt::ArrowCursor);
   923 }
   924 
   925 void MapEditor::mousePressEvent(QMouseEvent* e)
   926 {
   927 	// Ignore right clicks, these will go to context menus
   928 	if (e->button() == Qt::RightButton )
   929 	{
   930 		e->ignore();
   931 		return;
   932 	}
   933 
   934 	//Ignore clicks while editing heading
   935 	if (model->isSelectionBlocked() ) 
   936 	{
   937 		e->ignore();
   938 		return;
   939 	}
   940 
   941     QPointF p = mapToScene(e->pos());
   942     TreeItem *ti=findMapItem (p, NULL);
   943     LinkableMapObj* lmo=NULL;
   944 	if (ti) lmo=((MapItem*)ti)->getLMO();
   945 	
   946 	
   947 	e->accept();
   948 
   949 	//Take care of  system flags _or_ modifier modes
   950 	//
   951 	if (lmo && ti->isBranchLikeType() )
   952 	{
   953 		QString foname=((BranchObj*)lmo)->getSystemFlagName(p);
   954 		if (!foname.isEmpty())
   955 		{
   956 			// systemFlag clicked
   957 			model->select (lmo);	// FIXME-3 was selectInt
   958 			if (foname=="url") 
   959 			{
   960 				if (e->state() & Qt::ControlModifier)
   961 					mainWindow->editOpenURLTab();
   962 				else	
   963 					mainWindow->editOpenURL();
   964 			}	
   965 			else if (foname=="system-vymLink")
   966 			{
   967 				mainWindow->editOpenVymLink();
   968 				// tabWidget may change, better return now
   969 				// before segfaulting...
   970 			} else if (foname=="note")
   971 				mainWindow->windowToggleNoteEditor();
   972 			else if (foname=="hideInExport")		
   973 				model->toggleHideExport();
   974 			// FIXME-3 needed? xelection.update();	
   975 			return;	
   976 		} 
   977 	}	
   978 	// No system flag clicked, take care of modmodes (CTRL-Click)
   979 	if (e->state() & Qt::ControlModifier)
   980 	{
   981 		if (mainWindow->getModMode()==Main::ModModeColor)
   982 		{
   983 				pickingColor=true;
   984 				setCursor (PickColorCursor);
   985 				return;
   986 		} 
   987 		if (mainWindow->getModMode()==Main::ModModeXLink)
   988 		{	
   989 			BranchItem *bi_begin=model->getSelectedBranch();
   990 			if (bi_begin)	
   991 			{
   992 				drawingLink=true;
   993 				tmpXLink=model->createXLink(bi_begin,true);
   994 				tmpXLink->setColor(model->getMapDefXLinkColor());
   995 				tmpXLink->setWidth(model->getMapDefXLinkWidth());
   996 				tmpXLink->setEnd   (p);
   997 				tmpXLink->updateXLink();
   998 				return;
   999 			} 
  1000 		}
  1001 	}	// End of modmodes
  1002 
  1003     if (lmo) 
  1004 	{	
  1005 	/*
  1006 		cout << "ME::mouse pressed\n";
  1007 		cout << "  lmo="<<lmo<<endl;
  1008 		cout << "   ti="<<ti->getHeadingStd()<<endl;
  1009 	*/
  1010 		// Select the clicked object
  1011 
  1012 		// Get clicked LMO
  1013 		model->select (ti);
  1014 
  1015 		// Left Button	    Move Branches
  1016 		if (e->button() == Qt::LeftButton )
  1017 		{
  1018 			movingObj_start.setX( p.x() - lmo->x() );	
  1019 			movingObj_start.setY( p.y() - lmo->y() );	
  1020 			movingObj_orgPos.setX (lmo->x() );
  1021 			movingObj_orgPos.setY (lmo->y() );
  1022 			lmo->setRelPos();
  1023 			movingObj_orgRelPos=lmo->getRelPos();
  1024 
  1025 			// If modMode==copy, then we want to "move" the _new_ object around
  1026 			// then we need the offset from p to the _old_ selection, because of tmp
  1027 			if (mainWindow->getModMode()==Main::ModModeCopy &&
  1028 				e->state() & Qt::ControlModifier)
  1029 			{
  1030 				BranchItem *bi=model->getSelectedBranch();
  1031 				if (bi)
  1032 				{
  1033 					copyingObj=true;
  1034 					//FIXME-2   TreeItem::addBranch (BranchItem still missing) 
  1035 					//bi->addBranch (model->getSelectedBranch());
  1036 					model->unselect();
  1037 					model->select(bi->getLastBranch());
  1038 					model->reposition();
  1039 				}
  1040 			} 
  1041 
  1042 			movingObj=model->getSelectedLMO();	
  1043 		} else
  1044 			// Middle Button    Toggle Scroll
  1045 			// (On Mac OS X this won't work, but we still have 
  1046 			// a button in the toolbar)
  1047 			if (e->button() == Qt::MidButton )
  1048 				model->toggleScroll();
  1049 		// model->updateActions(); FIXME-3 needed?
  1050 		// FIXME-3 needed? xelection.update();
  1051 	} else 
  1052 	{ // No MapObj found, we are on the scene itself
  1053 		// Left Button	    move Pos of sceneView
  1054 		if (e->button() == Qt::LeftButton )
  1055 		{
  1056 			movingObj=NULL;	// move Content not Obj
  1057 			movingObj_start=e->globalPos();
  1058 			movingCont_start=QPointF (
  1059 				horizontalScrollBar()->value(),
  1060 				verticalScrollBar()->value());
  1061 			movingVec=QPointF(0,0);
  1062 			setCursor(HandOpenCursor);
  1063 		} 
  1064     } 
  1065 }
  1066 
  1067 void MapEditor::mouseMoveEvent(QMouseEvent* e)
  1068 {
  1069     QPointF p = mapToScene(e->pos());
  1070 	TreeItem *seli=model->getSelectedItem();
  1071 	LinkableMapObj* lmosel=NULL;	
  1072 	if (seli && (seli->isBranchLikeType() ||seli->getType()==TreeItem::Image))
  1073 		lmosel=((MapItem*)seli)->getLMO();
  1074 
  1075     // Move the selected MapObj
  1076     if ( lmosel && movingObj) 
  1077     {	
  1078 		// reset cursor if we are moving and don't copy
  1079 		if (mainWindow->getModMode()!=Main::ModModeCopy)
  1080 			setCursor (Qt::ArrowCursor);
  1081 
  1082 		// To avoid jumping of the sceneView, only 
  1083 		// show selection, if not tmp linked
  1084 		if (!lmosel->hasParObjTmp())
  1085 			model->emitShowSelection();
  1086 		
  1087 		// Now move the selection, but add relative position 
  1088 		// (movingObj_start) where selection was chosen with 
  1089 		// mousepointer. (This avoids flickering resp. jumping 
  1090 		// of selection back to absPos)
  1091 		
  1092 		// Check if we could link 
  1093 		TreeItem *ti=findMapItem (p, seli);
  1094 		BranchItem *dsti=NULL;
  1095 		LinkableMapObj* dst=NULL;
  1096 		if (ti && ti!=seli && ti->isBranchLikeType())
  1097 		{
  1098 			dsti=(BranchItem*)ti;
  1099 			dst=dsti->getLMO(); 
  1100 		} else
  1101 			dsti=NULL;
  1102 		
  1103 
  1104 		if (lmosel && seli->getType()==TreeItem::Image)	
  1105 		{
  1106 			FloatObj *fio=(FloatImageObj*)lmosel;
  1107 			fio->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  1108 			fio->setRelPos();
  1109 			fio->updateLinkGeometry(); //no need for reposition, if we update link here
  1110 			model->emitSelectionChanged();	// position has changed
  1111 
  1112 			// Relink float to new mapcenter or branch, if shift is pressed	
  1113 			// Only relink, if selection really has a new parent
  1114 			if ( e->modifiers()==Qt::ShiftModifier && dsti &&  dsti != seli->parent()  )
  1115 			{
  1116 				// Also save the move which was done so far
  1117 				QString pold=qpointFToString(movingObj_orgRelPos);
  1118 				QString pnow=qpointFToString(fio->getRelPos());
  1119 				model->saveState(
  1120 					seli,
  1121 					"moveRel "+pold,
  1122 					seli,
  1123 					"moveRel "+pnow,
  1124 					QString("Move %1 to relative position %2").arg(model->getObjectName(fio)).arg(pnow));
  1125 				fio->getParObj()->requestReposition();
  1126 				model->reposition();
  1127 
  1128 				model->relinkImage ((ImageItem*) seli,dsti);
  1129 				model->select (seli);
  1130 				//movingObj=lmosel;	//FIXME-3
  1131 				//movingObj_orgRelPos=lmosel->getRelPos();	
  1132 
  1133 				model->reposition();
  1134 			}
  1135 		} else	
  1136 		{	// selection != a FloatObj
  1137 			if (seli->depth()==0)		//FIXME-1 also moved mapcenters could be linked, but not working here...
  1138 			{
  1139 				// Move MapCenter
  1140 				if (e->buttons()== Qt::LeftButton && e->modifiers()==Qt::ShiftModifier) 
  1141 					((BranchObj*)lmosel)->moveBy(
  1142 						QPointF(p.x() -movingObj_start.x(), 
  1143 						p.y()-movingObj_start.y()) );		
  1144 				else	
  1145 					lmosel->move   (p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  1146 				model->updateRelPositions();
  1147 			} else
  1148 			{	
  1149 				if (seli->depth()==1)
  1150 				{
  1151 					// Move mainbranch
  1152 					lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() );		
  1153 					lmosel->setRelPos();
  1154 				} else
  1155 				{
  1156 					// Move ordinary branch
  1157 					if (lmosel->getOrientation() == LinkableMapObj::LeftOfCenter)
  1158 						// Add width of bbox here, otherwise alignRelTo will cause jumping around
  1159 						lmosel->move(p.x() -movingObj_start.x() , //lmosel->getBBox().width(), 
  1160 							p.y()-movingObj_start.y() +lmosel->getTopPad() );		
  1161 					else	
  1162 						lmosel->move(p.x() -movingObj_start.x(), p.y()-movingObj_start.y() -lmosel->getTopPad());
  1163 					lmosel->setRelPos();	
  1164 				} 
  1165 
  1166 				// Maybe we can relink temporary?
  1167 				if (dsti)
  1168 				{
  1169 					if (e->modifiers()==Qt::ControlModifier)
  1170 					{
  1171 						// Special case: CTRL to link below dst
  1172 						lmosel->setParObjTmp (dst,p,+1);
  1173 					}
  1174 					else if (e->modifiers()==Qt::ShiftModifier)
  1175 						lmosel->setParObjTmp (dst,p,-1);
  1176 					else
  1177 						lmosel->setParObjTmp (dst,p,0);
  1178 				} else	
  1179 				{
  1180 					lmosel->unsetParObjTmp();
  1181 				}		
  1182 				// reposition subbranch
  1183 				lmosel->reposition();	
  1184 			} // depth>0
  1185 
  1186 			QItemSelection sel=model->getSelectionModel()->selection();
  1187 			updateSelection(sel,sel);	// position has changed
  1188 
  1189 		} // no FloatImageObj
  1190 
  1191 		scene()->update();
  1192 		return;
  1193 	} // selection && moving_obj
  1194 		
  1195 	// Draw a link from one branch to another
  1196 	if (drawingLink)
  1197 	{
  1198 		 tmpXLink->setEnd (p);
  1199 		 tmpXLink->updateXLink();
  1200 	}	 
  1201 	
  1202     // Move sceneView 
  1203     if (!movingObj && !pickingColor &&!drawingLink && e->buttons() == Qt::LeftButton ) 
  1204 	{
  1205 		QPointF p=e->globalPos();
  1206 		movingVec.setX(-p.x() + movingObj_start.x() );
  1207 		movingVec.setY(-p.y() + movingObj_start.y() );
  1208 		horizontalScrollBar()->setSliderPosition((int)( movingCont_start.x()+movingVec.x() ));
  1209 		verticalScrollBar()->setSliderPosition((int)( movingCont_start.y()+movingVec.y() ) );
  1210     }
  1211 }
  1212 
  1213 
  1214 void MapEditor::mouseReleaseEvent(QMouseEvent* e)
  1215 {
  1216     QPointF p = mapToScene(e->pos());
  1217 	TreeItem *seli=model->getSelectedItem();
  1218 
  1219 	TreeItem *dsti=NULL;
  1220 	if (seli) dsti=findMapItem(p, seli);
  1221 	LinkableMapObj* dst=NULL;
  1222 	if (dsti && dsti->isBranchLikeType ()) 
  1223 		dst=((MapItem*)dsti)->getLMO();	
  1224 	else
  1225 		dsti=NULL;
  1226 
  1227 
  1228 	// Have we been picking color?
  1229 	if (pickingColor)
  1230 	{
  1231 		pickingColor=false;
  1232 		setCursor (Qt::ArrowCursor);
  1233 		// Check if we are over another branch
  1234 		if (dst) 
  1235 		{	
  1236 			if (e->state() & Qt::ShiftModifier)
  1237 				model->colorBranch (((BranchObj*)dst)->getColor());
  1238 			else	
  1239 				model->colorSubtree (((BranchObj*)dst)->getColor());
  1240 		} 
  1241 		return;
  1242 	}
  1243 
  1244 	// Have we been drawing a link?
  1245 	if (drawingLink)	
  1246 	{
  1247 		drawingLink=false;
  1248 		// Check if we are over another branch
  1249 		if (dsti)
  1250 		{	
  1251 			tmpXLink->setEnd ( ((BranchItem*)dsti) );
  1252 			tmpXLink->updateXLink();
  1253 			tmpXLink->activate(); 
  1254 			//FIXME-0 model->saveStateComplete(QString("Activate xLink from %1 to %2").arg(model->getObjectName(tmpXLink->getBegin())).arg(model->getObjectName(tmpXLink->getEnd())) );	
  1255 		} else
  1256 		{
  1257 			model->deleteItem(tmpXLink);
  1258 			tmpXLink=NULL;
  1259 		}
  1260 		return;
  1261 	}
  1262 	
  1263     // Have we been moving something?
  1264     if ( seli && movingObj ) 
  1265     {	
  1266 		if (seli->getType()==TreeItem::Image)
  1267 		{
  1268 			FloatImageObj *fio=(FloatImageObj*)( ((MapItem*)seli)->getLMO());
  1269 			if(fio)
  1270 			{
  1271 				// Moved FloatObj. Maybe we need to reposition
  1272 				QString pold=qpointFToString(movingObj_orgRelPos);
  1273 				QString pnow=qpointFToString(fio->getRelPos());
  1274 				model->saveState(
  1275 					seli,
  1276 					"moveRel "+pold,
  1277 					seli,
  1278 					"moveRel "+pnow,
  1279 					QString("Move %1 to relative position %2").arg(model->getObjectName(seli)).arg(pnow));
  1280 
  1281 				cout << "ME::release mouse\n";
  1282 				fio->getParObj()->requestReposition();
  1283 				model->reposition();
  1284 			}	
  1285 		}
  1286 
  1287 		BranchItem *bi=model->getSelectedBranch();
  1288 		if (bi && bi->depth()==0)
  1289 		{	
  1290             if (movingObj_orgPos != bi->getBranchObj()->getAbsPos())	// FIXME-3 check getBO here...
  1291             {
  1292                 QString pold=qpointFToString(movingObj_orgPos);
  1293                 QString pnow=qpointFToString(bi->getBranchObj()->getAbsPos());		// FIXME-3 check getBO here...
  1294 
  1295                 model->saveState(
  1296                     bi,
  1297                     "move "+pold,
  1298                     bi,
  1299                     "move "+pnow,
  1300                     QString("Move mapcenter %1 to position %2").arg(model->getObjectName(bi)).arg(pnow));
  1301             }
  1302 		}
  1303 	
  1304 		if (seli->isBranchLikeType() ) //(seli->getType() == TreeItem::Branch )
  1305 		{	// A branch was moved
  1306 			LinkableMapObj* lmosel=NULL;		
  1307 			lmosel=((MapItem*)seli)->getLMO();
  1308 				
  1309 			// save the position in case we link to mapcenter
  1310 			QPointF savePos=QPointF (lmosel->getAbsPos()  );
  1311 
  1312 			// Reset the temporary drawn link to the original one
  1313 			lmosel->unsetParObjTmp();
  1314 
  1315 			// For Redo we may need to save original selection
  1316 			QString preSelStr=model->getSelectString(seli);
  1317 
  1318 			copyingObj=false;	
  1319 			if (dsti)
  1320 			{
  1321 				// We have a destination, relink to that
  1322 
  1323 				BranchObj* bsel=model->getSelectedBranchObj();
  1324 
  1325 				QString preParStr=model->getSelectString (bsel->getParObj());
  1326 				QString preNum=QString::number (seli->num(),10);
  1327 				QString preDstParStr;
  1328 				bool relinked;
  1329 
  1330 				if (e->state() & Qt::ShiftModifier && dst->getParObj())
  1331 				{	// Link above dst
  1332 					preDstParStr=model->getSelectString (dst->getParObj());
  1333 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num());
  1334 				} else 
  1335 				if (e->state() & Qt::ControlModifier && dst->getParObj())
  1336 				{
  1337 					// Link below dst
  1338 					preDstParStr=model->getSelectString (dst->getParObj());
  1339 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti->parent(),((BranchItem*)dsti)->num()+1);
  1340 				} else	
  1341 				{	// Append to dst
  1342 					preDstParStr=model->getSelectString(dst);
  1343 					relinked=model->relinkBranch ((BranchItem*)seli,(BranchItem*)dsti);
  1344 					if (dsti->depth()==0) bsel->move (savePos);
  1345 				} 
  1346 				if (relinked)
  1347 				{
  1348 					QString postSelStr=model->getSelectString(lmosel);
  1349 					QString postNum=QString::number (seli->num(),10);
  1350 
  1351 					QString undoCom="relinkTo (\""+ 
  1352 						preParStr+ "\"," + preNum  +"," + 
  1353 						QString ("%1,%2").arg(movingObj_orgPos.x()).arg(movingObj_orgPos.y())+ ")";
  1354 
  1355 					QString redoCom="relinkTo (\""+ 
  1356 						preDstParStr + "\"," + postNum + "," +
  1357 						QString ("%1,%2").arg(savePos.x()).arg(savePos.y())+ ")";
  1358 
  1359 					model->saveState (
  1360 						postSelStr,undoCom,
  1361 						preSelStr, redoCom,
  1362 						QString("Relink %1 to %2").arg(model->getObjectName(bsel)).arg(model->getObjectName(dst)) );
  1363 
  1364 				}
  1365 			} else
  1366 			{
  1367 				// No destination, undo  temporary move
  1368 
  1369 				if (seli->depth()==1)
  1370 				{
  1371 					// The select string might be different _after_ moving around.
  1372 					// Therefor reposition and then use string of old selection, too
  1373 					model->reposition();
  1374 
  1375                     QPointF rp(lmosel->getRelPos());
  1376                     if (rp != movingObj_orgRelPos)
  1377                     {
  1378                         QString ps=qpointFToString(rp);
  1379                         model->saveState(
  1380                             model->getSelectString(lmosel), "moveRel "+qpointFToString(movingObj_orgRelPos), 
  1381                             preSelStr, "moveRel "+ps, 
  1382                             QString("Move %1 to relative position %2").arg(model->getObjectName(lmosel)).arg(ps));
  1383                     }
  1384 				}
  1385 
  1386 				// Draw the original link, before selection was moved around
  1387 				if (settings.value("/animation/use",false).toBool() && seli->depth()>1) 
  1388 				{
  1389 					lmosel->setRelPos();	// calc relPos first for starting point
  1390 					QPointF dst=bi->getBranchObj()->getParObj()->getChildPos();		// FIXME-3 check getBO here...
  1391 			//		if (lmosel->getOrientation()==LinkableMapObj::LeftOfCenter) dst.setX (dst.x()+lmosel->width() );
  1392 					
  1393 					model->startAnimation(
  1394 						(BranchObj*)lmosel,
  1395 						lmosel->getRelPos(),
  1396 						movingObj_orgRelPos
  1397 //						QPointF (movingObj_orgPos.x() - dst.x(), movingObj_orgPos.y() - dst.y() )
  1398 					);	
  1399 				} else	
  1400 					model->reposition();
  1401 			}
  1402 		}
  1403 		model->emitSelectionChanged();  //FIXME-3 needed? at least not after pos of selection has changed...
  1404 		// Finally resize scene, if needed
  1405 		scene()->update();
  1406 		movingObj=NULL;		
  1407 
  1408 		// Just make sure, that actions are still ok,e.g. the move branch up/down buttons...
  1409 		// model->updateActions(); FIXME-3 neeeded? 
  1410 	} else 
  1411 		// maybe we moved View: set old cursor
  1412 		setCursor (Qt::ArrowCursor);
  1413     
  1414 }
  1415 
  1416 void MapEditor::mouseDoubleClickEvent(QMouseEvent* e)
  1417 {
  1418 	if (model->isSelectionBlocked() ) 
  1419 	{
  1420 		e->ignore();
  1421 		return;
  1422 	}
  1423 
  1424 	if (e->button() == Qt::LeftButton )
  1425 	{
  1426 		QPointF p = mapToScene(e->pos());
  1427 		TreeItem *ti=findMapItem (p, NULL);
  1428 		if (ti) {	// MapObj was found
  1429 			// First select the MapObj than edit heading
  1430 			model->select (ti);
  1431 			editHeading();
  1432 		}
  1433 	}
  1434 }
  1435 
  1436 void MapEditor::resizeEvent (QResizeEvent* e)
  1437 {
  1438 	QGraphicsView::resizeEvent( e );
  1439 }
  1440 
  1441 void MapEditor::dragEnterEvent(QDragEnterEvent *event)
  1442 {
  1443 	//for (unsigned int i=0;event->format(i);i++) // Debug mime type
  1444 	//	cerr << event->format(i) << endl;
  1445 
  1446 	if (event->mimeData()->hasImage())
  1447 		event->acceptProposedAction();
  1448 	else	
  1449 		if (event->mimeData()->hasUrls())
  1450 			event->acceptProposedAction();
  1451 }
  1452 
  1453 void MapEditor::dragMoveEvent(QDragMoveEvent *)
  1454 {
  1455 }
  1456 
  1457 void MapEditor::dragLeaveEvent(QDragLeaveEvent *event)
  1458 {
  1459 	event->accept();
  1460 }
  1461 
  1462 void MapEditor::dropEvent(QDropEvent *event)
  1463 {
  1464 	BranchItem *selbi=model->getSelectedBranch();
  1465 	if (selbi)
  1466 	{
  1467 		if (debug)
  1468 			foreach (QString format,event->mimeData()->formats()) 
  1469 				cout << "MapEditor: Dropped format: "<<qPrintable (format)<<endl;
  1470 
  1471 
  1472 		QList <QUrl> uris;
  1473 		if (event->mimeData()->hasImage()) 
  1474 		{
  1475 			 QVariant imageData = event->mimeData()->imageData();
  1476 			 model->addFloatImage (qvariant_cast<QPixmap>(imageData));
  1477 		} else
  1478 		if (event->mimeData()->hasUrls())
  1479 			uris=event->mimeData()->urls();
  1480 
  1481 		if (uris.count()>0)
  1482 		{
  1483 			QStringList files;
  1484 			QString s;
  1485 			QString heading;
  1486 			BranchItem *bi;
  1487 			for (int i=0; i<uris.count();i++)
  1488 			{
  1489 				// Workaround to avoid adding empty branches
  1490 				if (!uris.at(i).toString().isEmpty())
  1491 				{
  1492 					bi=model->addNewBranch();
  1493 					if (bi)
  1494 					{
  1495 						   /* FIXME-2 
  1496 						s=uris.at(i).toLocalFile();
  1497 						if (!s.isEmpty()) 
  1498 						{
  1499 						   QString file = QDir::fromNativeSeparators(s);
  1500 						   heading = QFileInfo(file).baseName();
  1501 						   files.append(file);
  1502 						   if (file.endsWith(".vym", false))
  1503 							   bi->setVymLink(file);
  1504 						   else
  1505 							   bi->setURL(uris.at(i).toString());
  1506 					   } else 
  1507 					   {
  1508 						   bo->setURL(uris.at(i).toString());
  1509 					   }
  1510 							 */  
  1511 
  1512 					   if (!heading.isEmpty())
  1513 						   bi->setHeading(heading);
  1514 					   else
  1515 						   bi->setHeading(uris.at(i).toString());
  1516 						   
  1517 					}
  1518 				}
  1519 			}
  1520 			model->reposition();
  1521 		}
  1522 	}	
  1523 	event->acceptProposedAction();
  1524 }
  1525 
  1526 void MapEditor::updateSelection(QItemSelection newsel,QItemSelection oldsel)
  1527 {
  1528 	// Note: Here we are prepared for multiple selections, though this 
  1529 	// is not yet implemented elsewhere
  1530 
  1531 	// Here in MapEditor we can only select Branches and Images
  1532 	QList <TreeItem*> treeItemsNew;
  1533 	QList <TreeItem*> treeItemsOld;
  1534 
  1535 	QModelIndex ix;
  1536 	foreach (ix,newsel.indexes() )
  1537 	{
  1538 		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1539 		if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
  1540 			if (!treeItemsNew.contains(ti)) treeItemsNew.append (ti);
  1541 	}
  1542 	foreach (ix,oldsel.indexes() )
  1543 	{
  1544 		TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1545 		if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image )
  1546 			if (!treeItemsOld.contains(ti)) treeItemsOld.append (ti);
  1547 	}
  1548 
  1549 	// Trim list of selection rectangles 
  1550 	while (treeItemsNew.count() < selboxList.count() )
  1551 		delete selboxList.takeFirst();
  1552 
  1553 	// Take care to tmp scroll/unscroll
  1554 	if (!oldsel.isEmpty())
  1555 	{
  1556 		QModelIndex ix=oldsel.indexes().first(); 
  1557 		if (ix.isValid() )
  1558 		{
  1559 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1560 			if (ti)
  1561 			{
  1562 				if (ti->isBranchLikeType() )
  1563 				{
  1564 					// reset tmp scrolled branches
  1565 					BranchItem *bi=(BranchItem*)ti;
  1566 					bi->resetTmpUnscroll();
  1567 				}
  1568 				if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1569 					// Hide link if not needed
  1570 					((MapItem*)ti)->getLMO()->updateVisibility();
  1571 			}
  1572 		}
  1573 	}
  1574 
  1575 	if (!treeItemsNew.isEmpty())
  1576 	{
  1577 		QModelIndex ix=newsel.indexes().first(); 
  1578 		if (ix.isValid() )
  1579 		{
  1580 			// Temporary unscroll if necessary
  1581 			TreeItem *ti= static_cast<TreeItem*>(ix.internalPointer());
  1582 			if (ti->isBranchLikeType() )
  1583 			{
  1584 				BranchItem *bi=(BranchItem*)ti;
  1585 				if (bi->hasScrolledParent(bi) )
  1586 					bi->tmpUnscroll();
  1587 			}
  1588 			scrollTo (ix);
  1589 			if (ti->isBranchLikeType() || ti->getType()==TreeItem::Image)
  1590 				// Show link if needed
  1591 				((MapItem*)ti)->getLMO()->updateVisibility();
  1592 		}
  1593 	}
  1594 
  1595 	// Reduce rectangles
  1596 	while (treeItemsNew.count() < selboxList.count() )
  1597 		delete selboxList.takeFirst();
  1598 
  1599 	// Add additonal rectangles
  1600 	QGraphicsRectItem *sb;
  1601 	while (treeItemsNew.count() > selboxList.count() )
  1602 	{
  1603 		sb = mapScene->addRect(
  1604 			QRectF(0,0,0,0), 
  1605 			QPen(selectionColor),
  1606 			selectionColor);
  1607 		sb->setZValue(Z_SELBOX);
  1608 		sb->show();
  1609 		selboxList.append (sb);
  1610 	}
  1611 
  1612 
  1613 
  1614 	// Reposition rectangles
  1615 	QRectF bbox;
  1616 	QModelIndex index;
  1617 
  1618 	LinkableMapObj *lmo;
  1619 	for (int i=0; i<treeItemsNew.count();++i)
  1620 	{
  1621 		lmo=((MapItem*)treeItemsNew.at(i) )->getLMO();
  1622 		bbox=lmo->getBBox();
  1623 		sb=selboxList.at(i);
  1624 		sb->setRect (
  1625 			bbox.x(),bbox.y(), 
  1626 			bbox.width(), bbox.height());
  1627 		sb->setPen (selectionColor);	
  1628 		sb->setBrush (selectionColor);	
  1629 		i++;
  1630 	}
  1631 
  1632 	scene()->update();
  1633 }
  1634 
  1635 void MapEditor::updateData (const QModelIndex &sel)
  1636 {
  1637 	TreeItem *ti= static_cast<TreeItem*>(sel.internalPointer());
  1638 
  1639 /* testing
  1640 	cout << "ME::updateData\n";
  1641 
  1642 	cout << "  ti="<<ti<<endl;
  1643 	cout << "  h="<<ti->getHeading().toStdString()<<endl;
  1644 	*/
  1645 	
  1646 	if (ti->isBranchLikeType())
  1647 	{
  1648 	//	cout << "  ->updating...\n";
  1649 		BranchObj *bo=(BranchObj*) ( ((MapItem*)ti)->getLMO());
  1650 		bo->updateData();
  1651 	}
  1652 }
  1653 
  1654 void MapEditor::setSelectionColor (QColor col)
  1655 {
  1656 	selectionColor=col;
  1657 	QItemSelection sel=model->getSelectionModel()->selection();
  1658 	updateSelection(sel,sel);
  1659 }
  1660 
  1661 
  1662 QColor MapEditor::getSelectionColor ()
  1663 {
  1664 	return selectionColor;
  1665 }
  1666 
  1667