PDA

View Full Version : QwtPlotZoomer - zooming in on a cursor position



torrentss
20th August 2010, 14:15
I'm using Qwt6 SVN and I want to have a "Zoom In" action in a right-click context menu, which should take a rectangle of some size (say, 100x100 px) around the cursor position and zoom in on that. I'm using QwtPlotZoomer (I know that there's also QwtPlotMagnifier, but I need a single zoom stack).

I can't figure out what to pass to QwtPlotZoomer::zoom(const QRectF &rect), all I have is a mouse position as provided by customContextMenuRequested(QPoint) signal.

Thanks in advance!

P.S. I tried constructing a rectangle around that position and passing that, but it didn't work (didn't think it would).

Uwe
20th August 2010, 18:03
I wouldn't recommend to use QwtPlotZoomer for your way of zooming. Simply write an event filter for the plot canvas where you catch context menu events. If you want to zoom in translate your zoom rectangle into scale coordinates ( using the scale maps ) and call setAxisScale(). For the zoom stack use QStack.

Uwe

torrentss
20th August 2010, 18:55
I'm already using two full-blown zoomers (one on each axis pair), so reimplementing that manually would be no fun.
I don't think I need the event filter - the context menu shows up fine. I just store the position the mouse cursor was at when the menu was opened.

I think I solved this problem with the following code in the action handler (thanks to your tip about scale maps). Note: plot_ is QwtPlot, last_menu_pos_ is the parameter of customContextMenuRequested() as attached to plot_, and zoomer_left_ / zoomer_right_ are the two zoomers.



QPoint mouse_pos = plot_.data()->canvas()->mapFromGlobal(
plot_.data()->mapToGlobal(last_menu_pos_));

// Choose a rectangle around mouse pos and zoom to it.
QRectF mouse_rect(mouse_pos.x() - 50, mouse_pos.y() - 50, 100, 100);

QRectF scale_rect_left;
{
const QwtScaleMap xMap = plot_.data()->canvasMap(zoomer_left_.data()->xAxis());
const QwtScaleMap yMap = plot_.data()->canvasMap(zoomer_left_.data()->yAxis());
scale_rect_left = QRectF(
QPointF(xMap.invTransform(mouse_rect.x()), yMap.invTransform(mouse_rect.y())),
QPointF(xMap.invTransform(mouse_rect.right()), yMap.invTransform(mouse_rect.bottom())) );
}

QRectF scale_rect_right;
{
const QwtScaleMap xMap = plot_.data()->canvasMap(zoomer_right_.data()->xAxis());
const QwtScaleMap yMap = plot_.data()->canvasMap(zoomer_right_.data()->yAxis());
scale_rect_right = QRectF(
QPointF(xMap.invTransform(mouse_rect.x()), yMap.invTransform(mouse_rect.y())),
QPointF(xMap.invTransform(mouse_rect.right()), yMap.invTransform(mouse_rect.bottom())) );
}

zoomer_left_.data()->zoom(scale_rect_left);
zoomer_right_.data()->zoom(scale_rect_right);