Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions libimageviewer/viewpanel/contents/imgviewdelegate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2020-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -165,10 +165,20 @@ void LibImgViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
QPainterPath bp1;
bp1.addRoundedRect(pixmapRect, 4, 4);
painter->setClipPath(bp1);
_pixmap = _pixmap.scaled(pixmapRect.size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
// 根据选中状态使用不同的缩放模式
if (backgroundRect.width() != LibImgViewListView::ITEM_NORMAL_WIDTH) {
// 选中状态:保持宽高比
_pixmap = _pixmap.scaled(pixmapRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
} else {
// 未选中状态:填充整个区域
_pixmap = _pixmap.scaled(pixmapRect.size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
}
qreal adjustx = _pixmap.width() - pixmapRect.width();
qreal adjusty = _pixmap.height() - pixmapRect.height();
painter->drawImage(pixmapRect,_pixmap,_pixmap.rect().adjusted(adjustx / 2, -adjusty / 2, -adjustx / 2, adjusty / 2));

// 修复:居中显示时,偏移量应该是正值,负责旋转时计算
QRect sourceRect = _pixmap.rect().adjusted(adjustx / 2, adjusty / 2, -adjustx / 2, -adjusty / 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · Odd crop sizes distort thumbnails

When the scaled image's width or height differs from pixmapRect by an odd number of pixels, when the scaled image differs from the target by an odd number of pixels, converting the half-deltas to QRect::adjusted integer arguments truncates them on both sides. The resulting source rectangle is one pixel off, so drawImage slightly rescales the image and the crop is not exactly centered.

Use a floating-point source rectangle or distribute odd-pixel crop margins asymmetrically so the source rectangle matches the target dimensions exactly.

Prompt for AI agents
In `libimageviewer/viewpanel/contents/imgviewdelegate.cpp` at line 180:

**Odd crop sizes distort thumbnails**

When the scaled image's width or height differs from `pixmapRect` by an odd number of pixels, when the scaled image differs from the target by an odd number of pixels, converting the half-deltas to `QRect::adjusted` integer arguments truncates them on both sides. The resulting source rectangle is one pixel off, so `drawImage` slightly rescales the image and the crop is not exactly centered.

Use a floating-point source rectangle or distribute odd-pixel crop margins asymmetrically so the source rectangle matches the target dimensions exactly.

painter->drawImage(pixmapRect, _pixmap, sourceRect);

painter->restore();
}
Expand Down
Loading