From efe9a1b99507d742b8c685495480d4934196a59e Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Wed, 10 Mar 2021 14:59:36 +0200 Subject: [PATCH] MagnifyingGlass: get rid of C-style casts to Viewer* qobject_cast improves const correctness. QLabel::pixmap() is const-qualified => make Viewer::pixmap() const too. Return non-const QPixmap from Viewer::pixmap() to let compiler move the return value at the function's call sites. Introduce a named constant maxRelativeDimension. Change its type from float to double, which usually multiplies faster on x86-64. Remove redundant parentheses to improve readability. --- YACReader/magnifying_glass.cpp | 13 ++++++------- YACReader/viewer.cpp | 2 +- YACReader/viewer.h | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/YACReader/magnifying_glass.cpp b/YACReader/magnifying_glass.cpp index d2b8f458..839b1b6c 100644 --- a/YACReader/magnifying_glass.cpp +++ b/YACReader/magnifying_glass.cpp @@ -36,7 +36,7 @@ void MagnifyingGlass::updateImage(int x, int y) // image section augmented int zoomWidth = static_cast(width() * zoomLevel); int zoomHeight = static_cast(height() * zoomLevel); - auto p = (Viewer *)parent(); + auto *const p = qobject_cast(parentWidget()); int currentPos = p->verticalScrollBar()->sliderPosition(); const QPixmap image = p->pixmap(); int iWidth = image.width(); @@ -191,10 +191,11 @@ void MagnifyingGlass::zoomOut() } } +static constexpr auto maxRelativeDimension = 0.9; + void MagnifyingGlass::sizeUp() { - auto p = (Viewer *)parent(); - if (width() < (p->width() * 0.90f)) + if (width() < parentWidget()->width() * maxRelativeDimension) resizeAndUpdate(width() + 30, height() + 15); } @@ -206,8 +207,7 @@ void MagnifyingGlass::sizeDown() void MagnifyingGlass::heightUp() { - auto p = (Viewer *)parent(); - if (height() < (p->height() * 0.90f)) + if (height() < parentWidget()->height() * maxRelativeDimension) resizeAndUpdate(width(), height() + 15); } @@ -219,8 +219,7 @@ void MagnifyingGlass::heightDown() void MagnifyingGlass::widthUp() { - auto p = (Viewer *)parent(); - if (width() < (p->width() * 0.90f)) + if (width() < parentWidget()->width() * maxRelativeDimension) resizeAndUpdate(width() + 30, height()); } diff --git a/YACReader/viewer.cpp b/YACReader/viewer.cpp index edda73de..4a942df2 100644 --- a/YACReader/viewer.cpp +++ b/YACReader/viewer.cpp @@ -748,7 +748,7 @@ void Viewer::mouseMoveEvent(QMouseEvent *event) } } -const QPixmap Viewer::pixmap() +QPixmap Viewer::pixmap() const { #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) return content->pixmap(); diff --git a/YACReader/viewer.h b/YACReader/viewer.h index ee5bbda9..e86562d5 100644 --- a/YACReader/viewer.h +++ b/YACReader/viewer.h @@ -181,7 +181,7 @@ private: public: Viewer(QWidget *parent = nullptr); ~Viewer(); - const QPixmap pixmap(); + QPixmap pixmap() const; // Comic * getComic(){return comic;} const BookmarksDialog *getBookmarksDialog() { return bd; } // returns the current index starting in 1 [1,nPages]