From c8697ccd2d346ba198b85cc64c2a18f1318ae463 Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Wed, 10 Mar 2021 17:16:05 +0200 Subject: [PATCH] Always limit Magnifying glass's height MagnifyingGlass::sizeUp() and MagnifyingGlass::sizeDown() grow/shrink both width and height, but check only width's limits. Thus the user can first increase Magnifying glass's height, then increase its size and make the height greater than the main window's height. The user can also first increase the width, then decrease the size until the height shrinks to 0 and Magnifying glass disappears. When Magnifying glass disappears, the only way to make it visible again is to restore its default size by restarting YACReader, because the invisible MagnifyingGlass widget does not receive wheel events and Viewer::keyPressEvent() propagates shortcuts to mglass only if it is visible. And even this workaround is possible only because YACReader does not save/restore Magnifying glass's size (should it?). Always checking both width and height limits fixes the bug. If one of the dimensions reaches a limit, only the other dimension is modified. If both dimensions reach their limits, neither is modified. --- YACReader/magnifying_glass.cpp | 74 +++++++++++++++++++++++++++------- YACReader/magnifying_glass.h | 9 +++++ 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/YACReader/magnifying_glass.cpp b/YACReader/magnifying_glass.cpp index 839b1b6c..9eb628fd 100644 --- a/YACReader/magnifying_glass.cpp +++ b/YACReader/magnifying_glass.cpp @@ -191,42 +191,48 @@ void MagnifyingGlass::zoomOut() } } -static constexpr auto maxRelativeDimension = 0.9; - void MagnifyingGlass::sizeUp() { - if (width() < parentWidget()->width() * maxRelativeDimension) - resizeAndUpdate(width() + 30, height() + 15); + auto w = width(); + auto h = height(); + if (growWidth(w) | growHeight(h)) // bitwise OR prevents short-circuiting + resizeAndUpdate(w, h); } void MagnifyingGlass::sizeDown() { - if (width() > 175) - resizeAndUpdate(width() - 30, height() - 15); + auto w = width(); + auto h = height(); + if (shrinkWidth(w) | shrinkHeight(h)) // bitwise OR prevents short-circuiting + resizeAndUpdate(w, h); } void MagnifyingGlass::heightUp() { - if (height() < parentWidget()->height() * maxRelativeDimension) - resizeAndUpdate(width(), height() + 15); + auto h = height(); + if (growHeight(h)) + resizeAndUpdate(width(), h); } void MagnifyingGlass::heightDown() { - if (height() > 80) - resizeAndUpdate(width(), height() - 15); + auto h = height(); + if (shrinkHeight(h)) + resizeAndUpdate(width(), h); } void MagnifyingGlass::widthUp() { - if (width() < parentWidget()->width() * maxRelativeDimension) - resizeAndUpdate(width() + 30, height()); + auto w = width(); + if (growWidth(w)) + resizeAndUpdate(w, height()); } void MagnifyingGlass::widthDown() { - if (width() > 175) - resizeAndUpdate(width() - 30, height()); + auto w = width(); + if (shrinkWidth(w)) + resizeAndUpdate(w, height()); } void MagnifyingGlass::resizeAndUpdate(int w, int h) @@ -235,6 +241,46 @@ void MagnifyingGlass::resizeAndUpdate(int w, int h) updateImage(); } +static constexpr auto maxRelativeDimension = 0.9; +static constexpr auto widthStep = 30; +static constexpr auto heightStep = 15; + +bool MagnifyingGlass::growWidth(int &w) const +{ + const auto maxWidth = parentWidget()->width() * maxRelativeDimension; + if (w >= maxWidth) + return false; + w += widthStep; + return true; +} + +bool MagnifyingGlass::shrinkWidth(int &w) const +{ + constexpr auto minWidth = 175; + if (w <= minWidth) + return false; + w -= widthStep; + return true; +} + +bool MagnifyingGlass::growHeight(int &h) const +{ + const auto maxHeight = parentWidget()->height() * maxRelativeDimension; + if (h >= maxHeight) + return false; + h += heightStep; + return true; +} + +bool MagnifyingGlass::shrinkHeight(int &h) const +{ + constexpr auto minHeight = 80; + if (h <= minHeight) + return false; + h -= heightStep; + return true; +} + void MagnifyingGlass::keyPressEvent(QKeyEvent *event) { bool validKey = false; diff --git a/YACReader/magnifying_glass.h b/YACReader/magnifying_glass.h index a0933e3c..69bf35f4 100644 --- a/YACReader/magnifying_glass.h +++ b/YACReader/magnifying_glass.h @@ -13,6 +13,15 @@ private: float zoomLevel; void setup(const QSize &size); void resizeAndUpdate(int w, int h); + + // The following 4 functions increase/decrease their argument and return true, + // unless the maximum dimension value has been reached, in which case they + // do not modify the argument and return false. + bool growWidth(int &w) const; + bool shrinkWidth(int &w) const; + bool growHeight(int &h) const; + bool shrinkHeight(int &h) const; + void keyPressEvent(QKeyEvent *event) override; public: