Skip to content

Commit

Permalink
chore: add round and remove shadow for menu item
Browse files Browse the repository at this point in the history
add round and remove shadow for menu item

Log:
  • Loading branch information
wangfei committed Jul 9, 2024
1 parent cc748a7 commit 4ddfbe2
Showing 1 changed file with 8 additions and 63 deletions.
71 changes: 8 additions & 63 deletions styleplugins/chameleon/chameleonstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2821,42 +2821,7 @@ ChameleonMovementAnimation *ChameleonStyle::drawMenuItemBackground(const QStyleO
return nullptr;
}

// 清理旧的阴影
if (option->styleObject) {
const QRect shadow = option->styleObject->property("_d_menu_shadow_rect").toRect();
const QRect shadow_base = option->styleObject->property("_d_menu_shadow_base_rect").toRect();

// 如果当前菜单项时已选中的,并且shadow_base不等于当前区域,此时应当清理阴影区域
// 如果当前要绘制的item是触发阴影绘制的那一项,那么,此时应当清空阴影区域
if ((selected && shadow_base != option->rect)
|| (!selected && shadow_base == option->rect)
|| (!selected && shadow_base.width() != option->rect.width())) {
// 清空阴影区域
option->styleObject->setProperty("_d_menu_shadow_rect", QVariant());
option->styleObject->setProperty("_d_menu_shadow_base_rect", QVariant());

// 确保阴影区域能重绘
if (QWidget *w = qobject_cast<QWidget*>(option->styleObject)) {
w->update(shadow);
}
}
}

if (selected) {
// draw shadow
if (type == QStyleOptionMenuItem::Normal) {
if (option->styleObject) {
QRect shadow(0, 0, option->rect.width(), 7);
shadow.moveTop(option->rect.bottom() + 1);
option->styleObject->setProperty("_d_menu_shadow_rect", shadow);
option->styleObject->setProperty("_d_menu_shadow_base_rect", option->rect);

// 确保阴影区域能重绘
if (QWidget *w = qobject_cast<QWidget*>(option->styleObject)) {
w->update(shadow);
}
}
}
} else do {
color = option->palette.window().color();

Expand Down Expand Up @@ -2896,32 +2861,6 @@ ChameleonMovementAnimation *ChameleonStyle::drawMenuItemBackground(const QStyleO
if (!option->styleObject)
break;

// 为上一个item绘制阴影
const QRect shadow = option->styleObject->property("_d_menu_shadow_rect").toRect();

// 判断阴影rect是否在自己的区域
if (!option->rect.contains(shadow.center()))
break;

static QColor shadow_color;
static QPixmap shadow_pixmap;

if (shadow_color != option->palette.color(QPalette::Active, QPalette::Highlight)) {
shadow_color = option->palette.color(QPalette::Active, QPalette::Highlight);
QImage image(":/chameleonstyle/menu_shadow.svg");
QPainter pa(&image);
pa.setCompositionMode(QPainter::CompositionMode_SourceIn);
pa.fillRect(image.rect(), shadow_color);
shadow_pixmap = QPixmap::fromImage(image);
}

if (!shadow_pixmap.isNull()) {
if (QMenu *menu = qobject_cast<QMenu *>(option->styleObject)) {
if (!menu->geometry().contains(QCursor::pos()))
break;
}
painter->drawPixmap(shadow, shadow_pixmap);
}
} while (false);

{ // 无论如何都尝试绘制,因为可能有动画存在
Expand Down Expand Up @@ -2949,12 +2888,18 @@ ChameleonMovementAnimation *ChameleonStyle::drawMenuItemBackground(const QStyleO
auto opacity = painter->opacity();
// 一些状态为 disable 的 menu item 在绘制时会修改不透明度,这里暂时改回1.0。
painter->setOpacity(1.0);
painter->fillRect(animation->currentRect(), color);
painter->setBrush(color);
painter->setPen(Qt::NoPen);
QRect rect = animation->currentRect().adjusted(6, 0, -6, 0);
painter->drawRoundedRect(rect, 6, 6);
painter->setOpacity(opacity);

return animation;
} else if (selected) {
painter->fillRect(option->rect, color);
painter->setBrush(color);
painter->setPen(Qt::NoPen);
QRect rect = option->rect.adjusted(6, 0, -6, 0);
painter->drawRoundedRect(rect, 6, 6);
}
}

Expand Down

0 comments on commit 4ddfbe2

Please sign in to comment.