Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add round and remove shadow for menu item #231

Merged
merged 1 commit into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions styleplugins/chameleon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ dtk_add_plugin(
chameleonstyle.h
common.h
dstyleanimation.h
RESOURCES
resources.qrc
DEPENDENCIES
Dtk${VERSION_SUFFIX}::Widget
Qt${QT_VERSION_MAJOR}::CorePrivate
Expand Down
85 changes: 16 additions & 69 deletions styleplugins/chameleon/chameleonstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2821,43 +2821,8 @@ 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 {
if (!selected) {
do {
color = option->palette.window().color();

if (color.color().isValid() && color.color().alpha() != 0) {
Expand Down Expand Up @@ -2896,34 +2861,8 @@ 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);

} while (false);
}
{ // 无论如何都尝试绘制,因为可能有动画存在
color = option->palette.highlight();

Expand All @@ -2944,17 +2883,25 @@ ChameleonMovementAnimation *ChameleonStyle::drawMenuItemBackground(const QStyleO
if (selected)
animation->setTargetRect(option->rect);
}

const int round = 6;
if (animation && animation->isRuning()) {
painter->save();
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);
painter->drawRoundedRect(animation->currentRect(), round, round);
painter->setOpacity(opacity);
painter->restore();

return animation;
} else if (selected) {
painter->fillRect(option->rect, color);
painter->save();
painter->setBrush(color);
painter->setPen(Qt::NoPen);
FeiWang1119 marked this conversation as resolved.
Show resolved Hide resolved
painter->drawRoundedRect(option->rect, round, round);
painter->restore();
}
}

Expand Down Expand Up @@ -4287,7 +4234,7 @@ int ChameleonStyle::pixelMetric(QStyle::PixelMetric m, const QStyleOption *opt,
case PM_MenuVMargin:
return 8;
case PM_MenuHMargin:
return 0;
return 6;
default:
break;
}
Expand Down
9 changes: 0 additions & 9 deletions styleplugins/chameleon/menu_shadow.svg

This file was deleted.

5 changes: 0 additions & 5 deletions styleplugins/chameleon/resources.qrc

This file was deleted.

Loading