Skip to content

Commit

Permalink
fix: animation issue
Browse files Browse the repository at this point in the history
desktop sort animation optizimation

Log: fix animation issue
Bug: https://pms.uniontech.com/bug-view-269645.html
  • Loading branch information
Lighto-Ku committed Sep 24, 2024
1 parent 201806e commit 650f31c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/dfm-base/utils/iconutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,19 @@ IconUtils::IconStyle IconUtils::getIconStyle(int size)

return style;
}

QPixmap IconUtils::renderIconBackground(const QSizeF &size, const IconStyle &style)
{
QSize pixSize(qRound(size.width()), qRound(size.height()));
QPixmap pm { pixSize };
pm.fill(Qt::transparent);
QPainter p(&pm);
p.setRenderHints(p.renderHints() | QPainter::Antialiasing | QPainter::SmoothPixmapTransform, true);
p.setPen(Qt::NoPen);
p.setBrush(Qt::white);

QRectF rect { { 0, 0 }, size };
p.drawRoundedRect(rect, style.radius, style.radius);
p.end();
return pm;
}
1 change: 1 addition & 0 deletions src/dfm-base/utils/iconutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct IconStyle
int shadowRange { 3 };
};
QPixmap renderIconBackground(const QSize &size, const IconStyle &style = IconStyle {});
QPixmap renderIconBackground(const QSizeF &size, const IconStyle &style = IconStyle {});
QPixmap addShadowToPixmap(const QPixmap &originalPixmap, int shadowOffsetY, qreal blurRadius, qreal shadowOpacity);
IconStyle getIconStyle(int size);
} // end namespace IconUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ QSize CanvasItemDelegate::paintDragIcon(QPainter *painter, const QStyleOptionVie
return paintIcon(painter, indexOption.icon,
{ indexOption.rect, Qt::AlignCenter, QIcon::Normal,
QIcon::Off, isThumnailIconIndex(index) })
.size();
.size().toSize();
}

int CanvasItemDelegate::textLineHeight() const
Expand Down Expand Up @@ -735,7 +735,7 @@ void CanvasItemDelegate::initStyleOption(QStyleOptionViewItem *option, const QMo
* \param mode: icon mode (Normal, Disabled, Active, Selected )
* \param state: The state for which a pixmap is intended to be used. (On, Off)
*/
QRect CanvasItemDelegate::paintIcon(QPainter *painter, const QIcon &icon, const PaintIconOpts &opts)
QRectF CanvasItemDelegate::paintIcon(QPainter *painter, const QIcon &icon, const PaintIconOpts &opts)
{
// Copy of QStyle::alignedRect
Qt::Alignment alignment { visualAlignment(painter->layoutDirection(), opts.alignment) };
Expand All @@ -759,29 +759,29 @@ QRect CanvasItemDelegate::paintIcon(QPainter *painter, const QIcon &icon, const
painter->setRenderHints(painter->renderHints() | QPainter::Antialiasing | QPainter::SmoothPixmapTransform, true);

auto iconStyle { IconUtils::getIconStyle(opts.rect.size().toSize().width()) };
QRect backgroundRect { qRound(x), qRound(y), qRound(w), qRound(h) };
QRect imageRect { backgroundRect };
QRectF backgroundRect { x, y, w, h };
QRectF imageRect { backgroundRect };

// 绘制带有阴影的背景
auto stroke { iconStyle.stroke };
backgroundRect.adjust(-stroke, -stroke, stroke, stroke);
const auto &originPixmap { IconUtils::renderIconBackground(backgroundRect.size(), iconStyle) };
const auto &shadowPixmap { IconUtils::addShadowToPixmap(originPixmap, iconStyle.shadowOffset, iconStyle.shadowRange, 0.2) };
painter->drawPixmap(backgroundRect, shadowPixmap);
painter->drawPixmap(backgroundRect, shadowPixmap, px.rect());
imageRect.adjust(iconStyle.shadowRange, iconStyle.shadowRange, -iconStyle.shadowRange, -iconStyle.shadowRange);

QPainterPath clipPath;
auto radius { iconStyle.radius - iconStyle.stroke };
clipPath.addRoundedRect(imageRect, radius, radius);
painter->setClipPath(clipPath);
painter->drawPixmap(imageRect, px);
painter->drawPixmap(imageRect, px, px.rect());
painter->restore();

return backgroundRect;
}
painter->drawPixmap(qRound(x), qRound(y), px);
painter->drawPixmap(QPointF(x, y), px);
// return rect before scale
return QRect(qRound(x), qRound(y), w, h);
return QRectF(x, y, w, h);
}

QRectF CanvasItemDelegate::paintEmblems(QPainter *painter, const QRectF &rect, const FileInfoPointer &info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CanvasItemDelegate : public QStyledItemDelegate
protected:
void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const override;
QRect textPaintRect(const QStyleOptionViewItem &option, const QModelIndex &index, const QRect &rText, bool elide) const;
static QRect paintIcon(QPainter *painter, const QIcon &icon, const PaintIconOpts &opts);
static QRectF paintIcon(QPainter *painter, const QIcon &icon, const PaintIconOpts &opts);
static QRectF paintEmblems(QPainter *painter, const QRectF &rect, const FileInfoPointer &info);

void paintLabel(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index, const QRect &rLabel) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ void ViewPainter::drawMove(QStyleOptionViewItem option)

QPixmap itemPix = d->sortAnimOper->findPixmap(animationItem);
if (itemPix.isNull()) {
itemPix = QPixmap(end.size() * scale);
auto pixWidth = end.size().width() * scale;
auto pixHeight = end.size().height() * scale;
itemPix = QPixmap(QSize(qRound(pixWidth), qRound(pixHeight)));
itemPix.setDevicePixelRatio(scale);
itemPix.fill(Qt::transparent);

Expand All @@ -272,7 +274,7 @@ void ViewPainter::drawMove(QStyleOptionViewItem option)
d->sortAnimOper->setItemPixmap(animationItem, itemPix);
}

drawPixmap(option.rect, itemPix);
drawPixmap(QPointF(nx, ny), itemPix);
}
}
}
Expand Down

0 comments on commit 650f31c

Please sign in to comment.