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: