Zoom the 2D image by magnification, also with a touchpad

The wheel used to shrink the viewport by a fixed 18% per event whatever
the size of the step. A touchpad sends many small steps for one
movement, so the view jumped straight to the largest magnification.
The step is now proportional to the scroll amount, and a pinch zooms
as well.

Zooming changes the magnification around the cursor and the visible
area follows from the size of the plot area. A view chosen by the user
therefore always fills the plot area, also after a resize, where it
used to keep the shape of the whole image.

A viewport set by the program (axis ranges of the plot tab, the gain
plot following the data plot) is shown exactly. The image is clipped to
it, where neighbouring pixels used to be drawn outside it without grid,
labels or ticks.
This commit is contained in:
Erik Fröjdh committed 2026-11-13 10:06:08 +01:00
1 parent 356423fadf
commit 0e7e8ac909
4 files changed
+221 -142

No files matched your search

@@ -26,9 +26,9 @@ namespace sls {
* [ix, ix+1) x [iy, iy+1), y points up and row 0 is at the bottom. The
* viewport, the overlays and the signals use these coordinates.
*
* The wheel zooms around the cursor, dragging pans and a double click shows
* the whole image again. Once the pixels are large enough they are outlined
* and labelled with their value.
* The wheel or a pinch zooms around the cursor, dragging pans and a double
* click shows the whole image again. Once the pixels are large enough they
* are outlined and labelled with their value.
*/
class SlsQtFastImage : public QWidget {
Q_OBJECT
@@ -65,9 +65,13 @@ class SlsQtFastImage : public QWidget {
const std::vector<int> &histogram() const;
void histogramRange(double &lo, double &hi) const;
/** Does not emit viewportChanged() */
/** Shows this part of the image and nothing else, with empty borders
* where it does not have the shape of the widget. Does not emit
* visibleAreaChanged(). */
void setViewport(double x1, double y1, double x2, double y2);
void viewport(double &x, double &y, double &w, double &h) const;
/** The part of the image that is shown, QRectF(x, y, width, height)
* with (x, y) the lower left corner */
QRectF visibleArea() const;
void resetView();
QPointF widgetToData(const QPointF &widgetPos) const;
QPointF dataToWidget(const QPointF &dataPos) const;
@@ -96,14 +100,14 @@ class SlsQtFastImage : public QWidget {
QFont axisTitleFont() const;
signals:
/** The user zoomed or panned, or a resize changed the visible area.
* QRectF(x, y, width, height) with (x, y) the lower left corner. */
void viewportChanged(const QRectF &viewport);
/** The user zoomed or panned, or the widget was resized */
void visibleAreaChanged(const QRectF &area);
/** Palette, log scale or colour range changed */
void colorMapChanged();
void histogramChanged();
protected:
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
@@ -113,9 +117,10 @@ class SlsQtFastImage : public QWidget {
void resizeEvent(QResizeEvent *event) override;
private:
/** Where the viewport ends up in the widget */
/** Where the image ends up in the widget */
struct Mapping {
QRect plot; // widget area the image is drawn in
QRectF area; // visibleArea()
double scale = 1.0; // widget pixels per image pixel
QPointF origin; // widget position of image coordinate (0, 0)
@@ -125,12 +130,18 @@ class SlsQtFastImage : public QWidget {
QPointF toData(const QPointF &w) const {
return {(w.x() - origin.x()) / scale, (origin.y() - w.y()) / scale};
}
/** Widget area covered by the visible part of the image */
QRectF areaRect() const {
return QRectF(toWidget(area.x(), area.y() + area.height()),
area.size() * scale)
.intersected(plot);
}
};
QRect plotRect() const;
Mapping mapping() const;
void clampViewport();
void matchViewportToWindow();
void showArea(const QRectF &area);
void zoomAt(const QPointF &widgetPos, double factor);
void renderImage();
void updateHistogram();
@@ -158,12 +169,13 @@ class SlsQtFastImage : public QWidget {
double m_histogramHi = 1.0;
QElapsedTimer m_histogramAge;
QRectF m_viewport; // (x, y) is the lower left corner
QRectF m_viewport; // (x, y) is the lower left corner
bool m_fillPlot = true; // show more than m_viewport to fill the plot area
bool m_zoomEnabled = true;
bool m_axesVisible = true;
bool m_panning = false;
QPointF m_panStart; // widget position of the press
QPointF m_panStartViewport; // lower left corner at the press
QPointF m_panStart; // widget position of the press
QRectF m_panStartArea; // visibleArea() at the press
std::vector<QRectF> m_overlayRects;
std::vector<QLineF> m_overlayLines;
@@ -32,7 +32,7 @@ SlsQt2DPlot::SlsQt2DPlot(QWidget *parent, bool gain)
m_view = new SlsQtFastImage(this);
layout->addWidget(m_view, 1);
connect(m_view, &SlsQtFastImage::viewportChanged, this,
connect(m_view, &SlsQtFastImage::visibleAreaChanged, this,
[this](const QRectF &v) {
emit PlotZoomedSignal(
QRectF(m_hist.GetXMin() + v.x() * m_hist.GetXBinWidth(),
@@ -4,6 +4,7 @@
#include <QFontMetrics>
#include <QMouseEvent>
#include <QNativeGestureEvent>
#include <QPainter>
#include <QWheelEvent>
@@ -50,6 +51,7 @@ void SlsQtFastImage::setImage(const double *data, int nx, int ny) {
m_nx = nx;
m_ny = ny;
m_viewport = QRectF(0, 0, nx, ny);
m_fillPlot = true;
m_histogramAge.invalidate();
}
m_imageDirty = true;
@@ -196,24 +198,40 @@ void SlsQtFastImage::setViewport(double x1, double y1, double x2, double y2) {
if (x2 - x1 < kMinimumViewport || y2 - y1 < kMinimumViewport)
return;
m_viewport = QRectF(x1, y1, x2 - x1, y2 - y1);
m_fillPlot = false;
clampViewport();
update();
}
void SlsQtFastImage::viewport(double &x, double &y, double &w,
double &h) const {
x = m_viewport.x();
y = m_viewport.y();
w = m_viewport.width();
h = m_viewport.height();
// A viewport chosen by the user is grown to the shape of the plot area, as far
// as the image goes, so that it still fills the plot area after a resize.
QRectF SlsQtFastImage::visibleArea() const {
if (!hasImage())
return {};
if (!m_fillPlot)
return m_viewport;
const QRect plot = plotRect();
const double scale = std::min(plot.width() / m_viewport.width(),
plot.height() / m_viewport.height());
const double w = std::min<double>(m_nx, plot.width() / scale);
const double h = std::min<double>(m_ny, plot.height() / scale);
const QPointF center = m_viewport.center();
return QRectF(std::clamp(center.x() - w / 2, 0.0, m_nx - w),
std::clamp(center.y() - h / 2, 0.0, m_ny - h), w, h);
}
void SlsQtFastImage::resetView() {
if (!hasImage())
return;
m_viewport = QRectF(0, 0, m_nx, m_ny);
if (hasImage())
showArea(QRectF(0, 0, m_nx, m_ny));
}
// A change made by the user
void SlsQtFastImage::showArea(const QRectF &area) {
m_viewport = area;
m_fillPlot = true;
clampViewport();
update();
emit viewportChanged(m_viewport);
emit visibleAreaChanged(visibleArea());
}
void SlsQtFastImage::clampViewport() {
@@ -230,47 +248,24 @@ void SlsQtFastImage::clampViewport() {
m_viewport = QRectF(x, y, w, h);
}
// A zoomed viewport takes the shape of the plot area so the image fills it;
// the whole image is shown with empty borders instead.
void SlsQtFastImage::matchViewportToWindow() {
if (!hasImage())
return;
double w = m_viewport.width();
double h = m_viewport.height();
if (w >= m_nx - 0.5 && h >= m_ny - 0.5)
return;
const QRect plot = plotRect();
const double aspect = double(plot.width()) / plot.height();
if (w / h > aspect)
h = w / aspect;
else
w = h * aspect;
if (w > m_nx) {
h *= m_nx / w;
w = m_nx;
}
if (h > m_ny) {
w *= m_ny / h;
h = m_ny;
}
const QPointF center = m_viewport.center();
m_viewport = QRectF(center.x() - w / 2, center.y() - h / 2, w, h);
clampViewport();
emit viewportChanged(m_viewport);
}
// Magnifies by factor around a position in the widget
void SlsQtFastImage::zoomAt(const QPointF &widgetPos, double factor) {
const QPointF anchor = mapping().toData(widgetPos);
const double fx = (anchor.x() - m_viewport.x()) / m_viewport.width();
const double fy = (anchor.y() - m_viewport.y()) / m_viewport.height();
m_viewport.setSize(m_viewport.size() * factor);
clampViewport();
// keep the point under the cursor where it is
m_viewport.moveTo(anchor.x() - fx * m_viewport.width(),
anchor.y() - fy * m_viewport.height());
clampViewport();
update();
emit viewportChanged(m_viewport);
const Mapping m = mapping();
const QPointF anchor = m.toData(widgetPos);
// between the whole image and kMinimumViewport pixels
const double minScale =
std::min(m.plot.width() / double(m_nx), m.plot.height() / double(m_ny));
const double maxScale = std::max(
minScale, std::min(m.plot.width(), m.plot.height()) / kMinimumViewport);
const double scale = std::clamp(m.scale * factor, minScale, maxScale);
const double w = std::min<double>(m_nx, m.plot.width() / scale);
const double h = std::min<double>(m_ny, m.plot.height() / scale);
// The anchor stays where it is, except in a direction in which the whole
// image is visible and therefore centred.
const double left = m.plot.left() + (m.plot.width() - w * scale) / 2;
const double top = m.plot.top() + (m.plot.height() - h * scale) / 2;
showArea(QRectF(anchor.x() - (widgetPos.x() - left) / scale,
anchor.y() + (widgetPos.y() - top) / scale - h, w, h));
}
QRect SlsQtFastImage::plotRect() const {
@@ -297,15 +292,16 @@ SlsQtFastImage::Mapping SlsQtFastImage::mapping() const {
m.plot = plotRect();
if (!hasImage())
return m;
m.scale = std::min(m.plot.width() / m_viewport.width(),
m.plot.height() / m_viewport.height());
// the viewport is centred in the plot area
m.area = visibleArea();
m.scale = std::min(m.plot.width() / m.area.width(),
m.plot.height() / m.area.height());
// centred where the whole image does not fill the plot area
const double left =
m.plot.left() + (m.plot.width() - m_viewport.width() * m.scale) / 2;
m.plot.left() + (m.plot.width() - m.area.width() * m.scale) / 2;
const double top =
m.plot.top() + (m.plot.height() - m_viewport.height() * m.scale) / 2;
m.origin = QPointF(left - m_viewport.x() * m.scale,
top + (m_viewport.y() + m_viewport.height()) * m.scale);
m.plot.top() + (m.plot.height() - m.area.height() * m.scale) / 2;
m.origin = QPointF(left - m.area.x() * m.scale,
top + (m.area.y() + m.area.height()) * m.scale);
return m;
}
@@ -406,7 +402,7 @@ void SlsQtFastImage::paintEvent(QPaintEvent *) {
void SlsQtFastImage::drawImage(QPainter &p, const Mapping &m) const {
p.save();
p.setClipRect(m.plot);
p.setClipRect(m.areaRect());
p.setRenderHint(QPainter::SmoothPixmapTransform,
m_interpolate && m.scale < kOutlineMinScale);
const QRectF target(m.toWidget(0, m_ny),
@@ -419,10 +415,10 @@ void SlsQtFastImage::drawAxes(QPainter &p, const Mapping &m) const {
if (!m_axesVisible)
return;
const QRect plot = m.plot;
const double x0 = m_viewport.x();
const double x1 = x0 + m_viewport.width();
const double y0 = m_viewport.y();
const double y1 = y0 + m_viewport.height();
const double x0 = m.area.x();
const double x1 = x0 + m.area.width();
const double y0 = m.area.y();
const double y1 = y0 + m.area.height();
const auto ticksX = plot::computeLinearTicks(x0, x1, kMaxTicks);
const auto ticksY = plot::computeLinearTicks(y0, y1, kMaxTicks);
const double stepX = plot::niceStep(x1 - x0, kMaxTicks);
@@ -467,15 +463,15 @@ void SlsQtFastImage::drawPixelDetail(QPainter &p, const Mapping &m) const {
if (m.scale < kOutlineMinScale)
return;
const QRect plot = m.plot;
const double right = m_viewport.x() + m_viewport.width();
const double top = m_viewport.y() + m_viewport.height();
const int ix0 = std::max(0, static_cast<int>(std::floor(m_viewport.x())));
const int iy0 = std::max(0, static_cast<int>(std::floor(m_viewport.y())));
const double right = m.area.x() + m.area.width();
const double top = m.area.y() + m.area.height();
const int ix0 = std::max(0, static_cast<int>(std::floor(m.area.x())));
const int iy0 = std::max(0, static_cast<int>(std::floor(m.area.y())));
const int ix1 = std::min(m_nx, static_cast<int>(std::ceil(right)));
const int iy1 = std::min(m_ny, static_cast<int>(std::ceil(top)));
p.save();
p.setClipRect(plot);
p.setClipRect(m.areaRect());
p.setPen(QColor(55, 55, 55));
for (int ix = ix0; ix <= ix1; ++ix) {
const double wx = m.toWidget(ix, 0).x();
@@ -510,7 +506,7 @@ void SlsQtFastImage::drawPixelDetail(QPainter &p, const Mapping &m) const {
void SlsQtFastImage::drawOverlays(QPainter &p, const Mapping &m) const {
p.save();
p.setClipRect(m.plot);
p.setClipRect(m.areaRect());
p.setBrush(Qt::NoBrush);
p.setPen(QColor(255, 255, 255, 190));
@@ -559,7 +555,7 @@ void SlsQtFastImage::mousePressEvent(QMouseEvent *e) {
return;
m_panning = true;
m_panStart = e->position();
m_panStartViewport = m_viewport.topLeft();
m_panStartArea = visibleArea();
setCursor(Qt::ClosedHandCursor);
}
@@ -569,11 +565,7 @@ void SlsQtFastImage::mouseMoveEvent(QMouseEvent *e) {
const Mapping m = mapping();
if (m_panning) {
const QPointF delta = (e->position() - m_panStart) / m.scale;
m_viewport.moveTo(m_panStartViewport.x() - delta.x(),
m_panStartViewport.y() + delta.y());
clampViewport();
update();
emit viewportChanged(m_viewport);
showArea(m_panStartArea.translated(-delta.x(), delta.y()));
return;
}
@@ -603,12 +595,29 @@ void SlsQtFastImage::mouseDoubleClickEvent(QMouseEvent *) {
}
void SlsQtFastImage::wheelEvent(QWheelEvent *e) {
if (!hasImage() || !m_zoomEnabled || e->angleDelta().y() == 0)
const int delta = e->angleDelta().y();
if (!hasImage() || !m_zoomEnabled || delta == 0)
return;
zoomAt(e->position(),
e->angleDelta().y() > 0 ? 1 / kWheelZoomFactor : kWheelZoomFactor);
// A mouse wheel sends 120 per notch, a touchpad many small steps
zoomAt(e->position(), std::pow(kWheelZoomFactor, delta / 120.0));
}
void SlsQtFastImage::resizeEvent(QResizeEvent *) { matchViewportToWindow(); }
// Pinch on a touchpad
bool SlsQtFastImage::event(QEvent *e) {
if (e->type() == QEvent::NativeGesture && hasImage() && m_zoomEnabled) {
const auto *gesture = static_cast<QNativeGestureEvent *>(e);
if (gesture->gestureType() == Qt::ZoomNativeGesture) {
// value() is the change of size since the previous event
zoomAt(gesture->position(), std::max(0.1, 1 + gesture->value()));
return true;
}
}
return QWidget::event(e);
}
void SlsQtFastImage::resizeEvent(QResizeEvent *) {
if (hasImage())
emit visibleAreaChanged(visibleArea());
}
} // namespace sls
+108 -50
View File
@@ -44,6 +44,11 @@ bool near(double a, double b, double eps = 1e-6) {
return std::abs(a - b) <= eps;
}
bool nearRect(const QRectF &a, const QRectF &b) {
return near(a.x(), b.x()) && near(a.y(), b.y()) &&
near(a.width(), b.width()) && near(a.height(), b.height());
}
// Repaint synchronously (runs renderImage + paintEvent) and grab the result.
QImage render(QWidget &w) {
w.repaint();
@@ -209,6 +214,7 @@ class PlottingTest : public QObject {
void imageHistogram();
void imageViewport();
void imageWheelZoom();
void imageTouchpadZoom();
void imagePan();
void imageIsClippedToPlotArea();
void imageOverlays();
@@ -491,40 +497,43 @@ void PlottingTest::imageViewport() {
const int nx = 64, ny = 32;
const std::vector<double> ramp = xRamp(nx, ny);
view.setImage(ramp.data(), nx, ny);
QSignalSpy spy(&view, &sls::SlsQtFastImage::viewportChanged);
double x, y, w, h;
QSignalSpy spy(&view, &sls::SlsQtFastImage::visibleAreaChanged);
// the whole image to start with
view.viewport(x, y, w, h);
QVERIFY(near(x, 0.0) && near(y, 0.0) && near(w, nx) && near(h, ny));
QVERIFY(nearRect(view.visibleArea(), QRectF(0, 0, nx, ny)));
// clamped to the image
view.setViewport(-100.0, -100.0, 5000.0, 5000.0);
view.viewport(x, y, w, h);
QVERIFY(near(x, 0.0) && near(y, 0.0) && near(w, nx) && near(h, ny));
QVERIFY(nearRect(view.visibleArea(), QRectF(0, 0, nx, ny)));
// moved back inside
view.setViewport(60.0, 28.0, 70.0, 38.0);
view.viewport(x, y, w, h);
QVERIFY(near(x, 54.0) && near(y, 22.0) && near(w, 10.0) && near(h, 10.0));
QVERIFY(nearRect(view.visibleArea(), QRectF(54, 22, 10, 10)));
// too small or not finite: ignored
view.setViewport(10.0, 10.0, 10.5, 10.5);
view.setViewport(std::numeric_limits<double>::quiet_NaN(), 0.0, 9.0, 9.0);
view.viewport(x, y, w, h);
QVERIFY(near(x, 54.0) && near(y, 22.0) && near(w, 10.0) && near(h, 10.0));
QVERIFY(nearRect(view.visibleArea(), QRectF(54, 22, 10, 10)));
// exactly this part is shown, the rest of the plot area stays empty
const QImage img = render(view);
const QRect plot = plotArea(view);
QVERIFY(near(widgetPos(view, 64, 22).x() - widgetPos(view, 54, 22).x(),
plot.height()));
QCOMPARE(img.pixel(plot.left() + 5, plot.center().y() + 3), kBackground);
QCOMPARE(img.pixel(plot.right() - 5, plot.center().y() + 3), kBackground);
QVERIFY(img.pixel(plot.center() + QPoint(3, 3)) != kBackground);
// only the user and resetView() are reported
QCOMPARE(spy.count(), 0);
view.resetView();
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.at(0).at(0).toRectF(), QRectF(0, 0, nx, ny));
QVERIFY(nearRect(spy.at(0).at(0).toRectF(), QRectF(0, 0, nx, ny)));
// a new image size shows the whole image again
view.setViewport(10.0, 10.0, 20.0, 20.0);
view.setImage(ramp.data(), ny, nx);
view.viewport(x, y, w, h);
QVERIFY(near(x, 0.0) && near(y, 0.0) && near(w, ny) && near(h, nx));
QVERIFY(nearRect(view.visibleArea(), QRectF(0, 0, ny, nx)));
}
void PlottingTest::imageWheelZoom() {
@@ -534,39 +543,82 @@ void PlottingTest::imageWheelZoom() {
const int n = 64;
const std::vector<double> ramp = xRamp(n, n);
view.setImage(ramp.data(), n, n);
QSignalSpy spy(&view, &sls::SlsQtFastImage::viewportChanged);
// zooming in keeps the point under the cursor
QSignalSpy spy(&view, &sls::SlsQtFastImage::visibleAreaChanged);
const QRect plot = plotArea(view);
const double plotShape = double(plot.width()) / plot.height();
const QPointF cursor(200.0, 150.0);
// the image grows until it fills the plot area
for (int i = 0; i < 3; i++)
sendWheel(view, cursor, 120);
QCOMPARE(spy.count(), 3);
QRectF area = view.visibleArea();
QVERIFY(area.width() < n && area.height() < n);
QVERIFY(near(area.width() / area.height(), plotShape));
QVERIFY(nearRect(spy.at(2).at(0).toRectF(), area));
// and then keeps the point under the cursor
for (int i = 0; i < 5; i++) {
const QPointF before = view.widgetToData(cursor);
sendWheel(view, cursor, 120);
const QPointF after = view.widgetToData(cursor);
QVERIFY(near(before.x(), after.x()) && near(before.y(), after.y()));
}
QCOMPARE(spy.count(), 5);
double x, y, w, h;
view.viewport(x, y, w, h);
QVERIFY(w < n && h < n);
// down to the minimum viewport
// a resize shows more or less of the image, at the same magnification
area = view.visibleArea();
view.resize(500, 300);
QVERIFY(near(view.visibleArea().height(), area.height()));
QVERIFY(view.visibleArea().width() > area.width());
view.resize(400, 300);
QVERIFY(nearRect(view.visibleArea(), area));
// down to the smallest viewport
for (int i = 0; i < 40; i++)
sendWheel(view, cursor, 120);
view.viewport(x, y, w, h);
QVERIFY(near(w, sls::SlsQtFastImage::kMinimumViewport));
QVERIFY(near(h, sls::SlsQtFastImage::kMinimumViewport));
area = view.visibleArea();
QVERIFY(near(area.height(), sls::SlsQtFastImage::kMinimumViewport));
QVERIFY(near(area.width() / area.height(), plotShape));
// and back out to the whole image
for (int i = 0; i < 60; i++)
sendWheel(view, cursor, -120);
view.viewport(x, y, w, h);
QVERIFY(near(x, 0.0) && near(y, 0.0) && near(w, n) && near(h, n));
QVERIFY(nearRect(view.visibleArea(), QRectF(0, 0, n, n)));
// not when zooming is disabled
view.setZoomEnabled(false);
sendWheel(view, cursor, 120);
view.viewport(x, y, w, h);
QVERIFY(near(w, n) && near(h, n));
QVERIFY(nearRect(view.visibleArea(), QRectF(0, 0, n, n)));
}
void PlottingTest::imageTouchpadZoom() {
sls::SlsQtFastImage view;
view.resize(400, 300);
view.show();
const int n = 64;
const std::vector<double> ramp = xRamp(n, n);
view.setImage(ramp.data(), n, n);
const QPointF cursor(200.0, 150.0);
for (int i = 0; i < 4; i++)
sendWheel(view, cursor, 120);
const QRectF wheel = view.visibleArea();
// many small steps add up to the same as the notches of a wheel
view.resetView();
for (int i = 0; i < 4 * 120 / 8; i++)
sendWheel(view, cursor, 8);
QVERIFY(near(view.visibleArea().width(), wheel.width()));
QVERIFY(near(view.visibleArea().height(), wheel.height()));
// a pinch magnifies by 1 + value
view.resetView();
for (int i = 0; i < 4; i++) {
QNativeGestureEvent pinch(Qt::ZoomNativeGesture,
QPointingDevice::primaryPointingDevice(), 2,
cursor, cursor, cursor, 0.18, QPointF());
QCoreApplication::sendEvent(&view, &pinch);
}
QVERIFY(nearRect(view.visibleArea(), wheel));
}
void PlottingTest::imagePan() {
@@ -576,8 +628,10 @@ void PlottingTest::imagePan() {
const int n = 64;
const std::vector<double> ramp = xRamp(n, n);
view.setImage(ramp.data(), n, n);
view.setViewport(20.0, 20.0, 40.0, 40.0);
QSignalSpy spy(&view, &sls::SlsQtFastImage::viewportChanged);
for (int i = 0; i < 6; i++)
sendWheel(view, QPointF(200.0, 150.0), 120);
const QRectF before = view.visibleArea();
QSignalSpy spy(&view, &sls::SlsQtFastImage::visibleAreaChanged);
// the grabbed point follows the cursor
const QPointF from(200.0, 150.0), to(230.0, 130.0);
@@ -589,16 +643,26 @@ void PlottingTest::imagePan() {
Qt::NoButton);
const QPointF dropped = view.widgetToData(to);
QVERIFY(near(grabbed.x(), dropped.x()) && near(grabbed.y(), dropped.y()));
QVERIFY(spy.count() >= 1);
double x, y, w, h;
view.viewport(x, y, w, h);
QVERIFY(x < 20.0 && y < 20.0 && near(w, 20.0) && near(h, 20.0));
QCOMPARE(spy.count(), 1);
const QRectF after = view.visibleArea();
QVERIFY(after.x() < before.x() && after.y() < before.y());
QVERIFY(near(after.width(), before.width()));
QVERIFY(near(after.height(), before.height()));
// not out of the image
sendMouse(view, QEvent::MouseButtonPress, from, Qt::LeftButton,
Qt::LeftButton);
sendMouse(view, QEvent::MouseMove, from + QPointF(5000.0, -5000.0),
Qt::NoButton, Qt::LeftButton);
sendMouse(view, QEvent::MouseButtonRelease, from, Qt::LeftButton,
Qt::NoButton);
QVERIFY(near(view.visibleArea().x(), 0.0));
QVERIFY(near(view.visibleArea().y(), 0.0));
// a double click shows the whole image
sendMouse(view, QEvent::MouseButtonDblClick, to, Qt::LeftButton,
Qt::LeftButton);
view.viewport(x, y, w, h);
QVERIFY(near(x, 0.0) && near(y, 0.0) && near(w, n) && near(h, n));
QVERIFY(nearRect(view.visibleArea(), QRectF(0, 0, n, n)));
}
// ---------------------------------------------------------------------------
@@ -1360,7 +1424,6 @@ void PlottingTest::plot2dRoiAndZoom() {
const std::vector<double> d = xRamp(100, 80);
// the data coordinates are the pixels shifted by 1000 and 2000
plot.SetData(100, 1000.0, 1100.0, 80, 2000.0, 2080.0, d.data());
double x = 0.0, y = 0.0, w = 0.0, h = 0.0;
// pixels 10..29 x 20..39
plot.EnableRoiBoxes({slsDetectorDefs::ROI(10, 29, 20, 39)});
@@ -1371,28 +1434,25 @@ void PlottingTest::plot2dRoiAndZoom() {
QVERIFY(view->overlayRects().empty());
plot.SetZoom(QRectF(1020.0, 2020.0, 20.0, 20.0));
view->viewport(x, y, w, h);
QVERIFY(near(x, 20.0) && near(y, 20.0) && near(w, 20.0) && near(h, 20.0));
QVERIFY(nearRect(view->visibleArea(), QRectF(20, 20, 20, 20)));
// the user zooming is reported in data coordinates
QSignalSpy spy(&plot, &sls::SlsQt2DPlot::PlotZoomedSignal);
sendWheel(*view, widgetPos(*view, 30.0, 30.0), 120);
QCOMPARE(spy.count(), 1);
const QRectF zoomed = spy.at(0).at(0).toRectF();
QVERIFY(zoomed.width() < 20.0 && zoomed.height() < 20.0);
QVERIFY(QRectF(1020.0, 2020.0, 20.0, 20.0).contains(zoomed));
QVERIFY(zoomed.height() < 20.0);
QVERIFY(zoomed.contains(QPointF(1030.0, 2030.0)));
QVERIFY(QRectF(1000.0, 2000.0, 100.0, 80.0).contains(zoomed));
// axis ranges from the plot tab
plot.SetXMinMax(1040.0, 1060.0);
view->viewport(x, y, w, h);
QVERIFY(near(x, 40.0) && near(w, 20.0) && near(y, 0.0) && near(h, 80.0));
QVERIFY(nearRect(view->visibleArea(), QRectF(40, 0, 20, 80)));
plot.SetYMinMax(2030.0, 2010.0);
view->viewport(x, y, w, h);
QVERIFY(near(x, 40.0) && near(w, 20.0) && near(y, 10.0) && near(h, 20.0));
QVERIFY(nearRect(view->visibleArea(), QRectF(40, 10, 20, 20)));
plot.EnableXAutoScaling();
plot.EnableYAutoScaling();
view->viewport(x, y, w, h);
QVERIFY(near(x, 0.0) && near(y, 0.0) && near(w, 100.0) && near(h, 80.0));
QVERIFY(nearRect(view->visibleArea(), QRectF(0, 0, 100, 80)));
plot.DisableZoom(true);
QVERIFY(!view->zoomEnabled());
@@ -1431,9 +1491,7 @@ void PlottingTest::plot2dGain() {
QVERIFY(near(lo, 0.0) && near(hi, 3.0));
QVERIFY(!view->logScale());
gain.SetZoom(QRectF(2.0, 2.0, 4.0, 4.0));
double x = 0.0, y = 0.0, w = 0.0, h = 0.0;
view->viewport(x, y, w, h);
QVERIFY(near(x, 2.0) && near(y, 2.0) && near(w, 4.0) && near(h, 4.0));
QVERIFY(nearRect(view->visibleArea(), QRectF(2, 2, 4, 4)));
}
// ---------------------------------------------------------------------------