MagnifyingGlass: get rid of C-style casts to Viewer*

qobject_cast<const Viewer *> 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.
This commit is contained in:
Igor Kushnir 2021-03-10 14:59:36 +02:00
parent fdba938fe8
commit efe9a1b995
3 changed files with 8 additions and 9 deletions

View File

@ -36,7 +36,7 @@ void MagnifyingGlass::updateImage(int x, int y)
// image section augmented // image section augmented
int zoomWidth = static_cast<int>(width() * zoomLevel); int zoomWidth = static_cast<int>(width() * zoomLevel);
int zoomHeight = static_cast<int>(height() * zoomLevel); int zoomHeight = static_cast<int>(height() * zoomLevel);
auto p = (Viewer *)parent(); auto *const p = qobject_cast<const Viewer *>(parentWidget());
int currentPos = p->verticalScrollBar()->sliderPosition(); int currentPos = p->verticalScrollBar()->sliderPosition();
const QPixmap image = p->pixmap(); const QPixmap image = p->pixmap();
int iWidth = image.width(); int iWidth = image.width();
@ -191,10 +191,11 @@ void MagnifyingGlass::zoomOut()
} }
} }
static constexpr auto maxRelativeDimension = 0.9;
void MagnifyingGlass::sizeUp() void MagnifyingGlass::sizeUp()
{ {
auto p = (Viewer *)parent(); if (width() < parentWidget()->width() * maxRelativeDimension)
if (width() < (p->width() * 0.90f))
resizeAndUpdate(width() + 30, height() + 15); resizeAndUpdate(width() + 30, height() + 15);
} }
@ -206,8 +207,7 @@ void MagnifyingGlass::sizeDown()
void MagnifyingGlass::heightUp() void MagnifyingGlass::heightUp()
{ {
auto p = (Viewer *)parent(); if (height() < parentWidget()->height() * maxRelativeDimension)
if (height() < (p->height() * 0.90f))
resizeAndUpdate(width(), height() + 15); resizeAndUpdate(width(), height() + 15);
} }
@ -219,8 +219,7 @@ void MagnifyingGlass::heightDown()
void MagnifyingGlass::widthUp() void MagnifyingGlass::widthUp()
{ {
auto p = (Viewer *)parent(); if (width() < parentWidget()->width() * maxRelativeDimension)
if (width() < (p->width() * 0.90f))
resizeAndUpdate(width() + 30, height()); resizeAndUpdate(width() + 30, height());
} }

View File

@ -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) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return content->pixmap(); return content->pixmap();

View File

@ -181,7 +181,7 @@ private:
public: public:
Viewer(QWidget *parent = nullptr); Viewer(QWidget *parent = nullptr);
~Viewer(); ~Viewer();
const QPixmap pixmap(); QPixmap pixmap() const;
// Comic * getComic(){return comic;} // Comic * getComic(){return comic;}
const BookmarksDialog *getBookmarksDialog() { return bd; } const BookmarksDialog *getBookmarksDialog() { return bd; }
// returns the current index starting in 1 [1,nPages] // returns the current index starting in 1 [1,nPages]