PDA

View Full Version : real world coordinates in graphics view / scene



robertson1
6th December 2007, 23:43
Hello,

I'm trying to figure out the best way to retain the real world coordinates of items in a graphics scene. i.e so that items can be added at any stage as required in there correct world coordinates (like in a cad program).

I have a subclassed QGraphics view with some mouse events for panning / zooming round and display the cursor position (in scene coordinates) in a QLabel, but am struggling to get my head around the scene coordinates which are displayed when I am simply scrolling (panning) around the view.

They Y axis scene coordinates behave as expected but I cant figure out whats going on with the X axis coordinates (why do they change when I am just scrolling around? (drag with middle mouse button).

For example:



#include <QApplication>
#include <QMainWindow>
#include <QStatusBar>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsTextItem>
#include <QMouseEvent>
#include <QScrollBar>
#include <QLabel>
#include <math.h>

class MapView : public QGraphicsView
{

public:
MapView(QWidget *parent = 0);
QPoint sceneCoordinates;
QLabel *coordinateDisplayLabel;

protected:
void scaleView(qreal scaleFactor);
void wheelEvent(QWheelEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);

private:
QString cursorXString;
QString cursorYString;
void storeMouseEvent(QMouseEvent *event);
bool scrolling;
QMouseEvent lastMouseEvent;

};

MapView::MapView(QWidget *parent)
: QGraphicsView(parent), lastMouseEvent(QEvent::None,QPoint(),
Qt::NoButton,Qt::NoButton,Qt::NoModifier)

{
setAttribute (Qt::WA_SetCursor);
setCursor(Qt::CrossCursor);
scale(1,-1);
coordinateDisplayLabel = new QLabel(0);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
scrolling = false;
}

void MapView::wheelEvent(QWheelEvent *event)
{
scaleView(pow((double)2, event->delta() / 240.0));
}

void MapView::scaleView(qreal scaleFactor)
{
qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.07 || factor > 100)
return;
scale(scaleFactor, scaleFactor);
}

void MapView::storeMouseEvent(QMouseEvent *event)
{
lastMouseEvent = QMouseEvent(event->type(), event->pos(), event->globalPos(),
event->button(),event->buttons(),event->modifiers());
}

void MapView::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::MidButton)
{
setCursor(Qt::ClosedHandCursor);
scrolling = true;
storeMouseEvent(event);
event->accept();
return;
}
}

void MapView::mouseMoveEvent(QMouseEvent *event)
{
if(scrolling)
{
QPoint delta = event->globalPos() - lastMouseEvent.globalPos();
QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar();
hBar->setValue(hBar->value() - delta.x());
vBar->setValue(vBar->value() - delta.y());
storeMouseEvent(event);
event->accept();
return;
}

sceneCoordinates = mapFromScene(event->pos());
cursorXString.setNum(sceneCoordinates.x());
cursorYString.setNum(sceneCoordinates.y());
coordinateDisplayLabel->setText("x = " + cursorXString + " " + "y = " + cursorYString);
}

void MapView::mouseReleaseEvent(QMouseEvent *event)
{
if((event->button() == Qt::MidButton) && (scrolling == true))
{
setCursor(Qt::CrossCursor);
scrolling = false;
storeMouseEvent(event);
event->accept();
return;
}
event->ignore();
}


int main( int argc, char **argv )
{
QApplication a( argc, argv );
QMainWindow *mw = new QMainWindow();
QGraphicsScene *scene = new QGraphicsScene;
MapView *view = new MapView;
QStatusBar *statusbar = new QStatusBar;

scene->setSceneRect(-30000, -30000, 60000, 60000);
scene->setBackgroundBrush(Qt::black);

scene->addLine ( -50, 0, 50, 0, (QPen(Qt::white, 0)));
scene->addLine ( 0, -50, 0, 50, (QPen(Qt::white, 0)));
QGraphicsTextItem item1("0,0");
item1.setDefaultTextColor(Qt::white);
item1.setPos(0,0);
item1.scale(1,-1);
scene->addItem(&item1);

scene->addLine ( 200, 250, 300, 250, (QPen(Qt::white, 0)));
scene->addLine ( 250, 300, 250, 200, (QPen(Qt::white, 0)));
QGraphicsTextItem item2("250,250");
item2.setDefaultTextColor(Qt::white);
item2.setPos(250,250);
item2.scale(1,-1);
scene->addItem(&item2);

view->setScene(scene);
mw->setCentralWidget(view);
statusbar->addPermanentWidget(view->coordinateDisplayLabel, 0);
mw->setStatusBar(statusbar);

mw->show();

return a.exec();
}


Ultimately I'm trying to get the coordinate display to always match the original scene coordinates.


Hope someone can enlighten me a bit.


Thanks

wysota
7th December 2007, 00:40
I think that your problem is that you are using viewport coordinates in your mouse events instead of scene coordinates. You are even using mapFromScene() but passing it viewport coordinates instead of scene coordinates.

BTW. I suggest you take a look at QGraphicsView::dragMode (http://doc.trolltech.com/latest/qgraphicsview.html#dragMode-prop).

robertson1
8th December 2007, 00:21
Thanks for the pointers

Works perfectly now!