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);
{
const QwtScaleMap xMap
= plot_.
data()->canvasMap
(zoomer_left_.
data()->xAxis
());
const QwtScaleMap yMap
= plot_.
data()->canvasMap
(zoomer_left_.
data()->yAxis
());
QPointF(xMap.
invTransform(mouse_rect.
x()), yMap.
invTransform(mouse_rect.
y())),
QPointF(xMap.
invTransform(mouse_rect.
right()), yMap.
invTransform(mouse_rect.
bottom())) );
}
{
const QwtScaleMap xMap
= plot_.
data()->canvasMap
(zoomer_right_.
data()->xAxis
());
const QwtScaleMap yMap
= plot_.
data()->canvasMap
(zoomer_right_.
data()->yAxis
());
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);
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);
To copy to clipboard, switch view to plain text mode
Bookmarks