PDA

View Full Version : Unable to work with wheelevent and rubberbanddrag on GraphicsViewItem.



gaganbm
28th June 2013, 11:52
I am facing some real weird behavior with rubberband drag and wheelevent. If I disable the other events (mousepress, mouse move and mouse release), the wheelevent works properly and zooms my screen as it should have done.

However, if I add the other three events, the wheel event doesnt zoom with mouse position at center anymore. It zooms, but on its own way, irrespective of the mouse position.

Here are the sample codes.

Kindly help.




class MyGraphics : public QGraphicsView{

public :
MyGraphics(QWidget *parent = NULL);

public slots:
void zoomIn() { scale(1.2, 1.2); }
void zoomOut() { scale(1 / 1.2, 1 / 1.2); }

protected :
QRubberBand *rubberBand;
QPoint origin;
QPointF InitialCenterPoint;
QPointF CurrentCenterPoint;
QPoint rubberBandOrigin;
bool rubberBandActive;
QPoint LastPanPoint;
int _numScheduledScalings;

//if I uncomment these three event handlers, the wheelevent doesnt zoom properly!

// virtual void mousePressEvent(QMouseEvent *event);
// virtual void mouseMoveEvent(QMouseEvent *event);
// virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void wheelEvent(QWheelEvent *);
virtual void resizeEvent(QResizeEvent *event);

};


MyGraphics::MyGraphics(QWidget *parent) : QGraphicsView(parent){
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);

this->installEventFilter(this);

this->setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
this->setResizeAnchor( QGraphicsView::AnchorUnderMouse );
QGraphicsScene *graphScene = new QGraphicsScene(this);

this->setScene(graphScene);

// QDebug << "Left " << sceneRect().
QGraphicsItem* pEllipse = graphScene->addEllipse(0,100,50,50);
QGraphicsItem* pRect = graphScene->addRect(200,100,50,50);

pEllipse->setFlag(QGraphicsItem::ItemIsSelectable, true);
pRect->setFlag(QGraphicsItem::ItemIsSelectable, true);

setSceneRect(0, 0, 1000, 1000);
rubberBandOrigin = QPoint(0,0);
}

void MyGraphics::wheelEvent(QWheelEvent *event){

double scaleFactor = 1.2;

if(event->delta() > 0){
//Zoom in
this->zoomIn();
//this->scale(scaleFactor,scaleFactor);
} else {
this->zoomOut();
//Zoom Out
//this->scale(1.0/scaleFactor, 1.0/scaleFactor);
}

//QGraphicsView::wheelEvent(event);
}

void MyGraphics::resizeEvent(QResizeEvent* event) {
//Get the rectangle of the visible area in scene coords
QRectF visibleArea = this->mapToScene(rect()).boundingRect();

this->fitInView(0,0,visibleArea.width(), visibleArea.height(),Qt::KeepAspectRatio);

//Call the subclass resize so the scrollbars are updated correctly
QGraphicsView::resizeEvent(event);

}

/*
void MyGraphics::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton) {
rubberBandOrigin = event->pos();
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(event->x(),event->y(),0, 0);
rubberBand->show();
rubberBandActive = true;
}
if(event->button() == Qt::LeftButton){
LastPanPoint = event->pos();
}

}

void MyGraphics::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::MiddleButton && rubberBandActive == true){
rubberBand->resize( event->x()-rubberBandOrigin.x(), event->y()-rubberBandOrigin.y() );
}
else{
if(!LastPanPoint.isNull()) {
//Get how much we panned
QGraphicsView * view = static_cast<QGraphicsView *>(this);

QPointF delta = view->mapToScene(LastPanPoint) - view->mapToScene(event->pos());
LastPanPoint = event->pos();
}
}
}

void MyGraphics::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton){
QGraphicsView * view = static_cast<QGraphicsView *>(this);

QPoint rubberBandEnd = event->pos();

QRectF zoomRectInScene = QRectF(view->mapToScene(rubberBandOrigin), view->mapToScene(rubberBandEnd));
QPointF center = zoomRectInScene.center();

view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
rubberBandActive = false;
delete rubberBand;
}
else{
LastPanPoint = QPoint();
}
}
*/