PDA

View Full Version : Zooming with rubberband



zgulser
23rd February 2010, 17:11
Hi,

I want to zoom into my graphics view with the rubberband. I want also my view center to be the rubberband's shape center. In other words, I want fill my whole view with that rubberband shape(rectangle).

I looked at the documentation and I saw that setting dragMode as RubberBandDrag could be adequate to perform the task above but, I couldn't be quite sure about scale function of QGraphicsView. Will it be better to implement my own rubberband? Any suggestions? For example what abour using fitInView()?


Thanks

Lodorot
23rd February 2010, 18:52
Hi,

QGraphicsView::RubberBandDrag is normally used to select items within the rubberband but you can use it to display a rubberband and then do the zooming yourself.

You just have to set the drag mode to rubberband and install a event filter to the view's viewport to handle the click events:


view->setDragMode(QGraphicsView::RubberBandDrag)
view->viewport()->installEventFilter(viewEventHandler)


The view event handler than gets called and can do the zooming:



bool ViewEventHandler::eventFilter(QObject *o, QEvent *event )
{
bool filterEvent = false;

switch(event->type()) {

case QEvent::MouseButtonPress:
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
rubberBandOrigin = mouseEvent->pos();
rubberBandActive = true;
break;
}


case QEvent::MouseButtonRelease:
{
if (rubberBandActive) {
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
QPoint rubberBandEnd = mouseEvent->pos();

QGraphicsView * view = static_cast<QGraphicsView *>(o->parent());
QRectF zoomRectInScene = QRectF(view->mapToScene(rubberBandOrigin),
view->mapToScene(rubberBandEnd));

view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
rubberBandActive = false;
}
break;
}
}

return filterEvent;
}



Another possibility would be to use QRubberBand.

Regards

Lodorot

zgulser
23rd February 2010, 19:01
Hi again,

I have few trivial questions..

1. Then setting drag mode is just to visualize it?

2. What about the items inside the view? Does the view framework will automatically do all transformations for the items?

3. Is it possible to get rid of item selection when rubberbanding?

4. What about centering? Will it be OK after?

Finally, you said the other possibility is using QRubberBand..hımm but I think it would be very similar to yours.

Thanks again

Lodorot
23rd February 2010, 21:49
Hi,

about your questions:

1. Yes, in this example the drag mode is only set to visualize the rubberband.

2. The view will do the scaling and translation of the items

3. To get rid of item selection you could use QRubberBand. Then you have to do the resizing of the QRubberBand as well.

4. I quote the fitInView documentation:

"Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport. ... rect will be centered in the view if it does not fit tightly."

I would say just try it out.

Regards


Lodorot

zgulser
24th February 2010, 06:55
Thanks again for your interest mate

zgulser
24th February 2010, 12:00
Hi again,

There appeared a problematic case. When I do fitInView, there happens a miscoordination between the cursor position and the start position of rubberband shape. I'm reimplementing mouse events in my view, not in the scene since it doesn't exist as a seperate class. Maybe the reason is that?

By the way, after I do fitInView, I observed that left corner point, which is my origin, is not 0,0 anymore..

Thanks