From adde674024cd34d42d0a8e73b04fd206710699ff Mon Sep 17 00:00:00 2001 From: wangfei Date: Tue, 9 Jul 2024 15:53:54 +0800 Subject: [PATCH] chore: add round and remove shadow for menu item add round and remove shadow for menu item Log: --- styleplugins/chameleon/CMakeLists.txt | 2 - styleplugins/chameleon/chameleonstyle.cpp | 85 +++++------------------ styleplugins/chameleon/menu_shadow.svg | 9 --- styleplugins/chameleon/resources.qrc | 5 -- 4 files changed, 16 insertions(+), 85 deletions(-) delete mode 100644 styleplugins/chameleon/menu_shadow.svg delete mode 100644 styleplugins/chameleon/resources.qrc diff --git a/styleplugins/chameleon/CMakeLists.txt b/styleplugins/chameleon/CMakeLists.txt index f31071b3..738b7f0a 100644 --- a/styleplugins/chameleon/CMakeLists.txt +++ b/styleplugins/chameleon/CMakeLists.txt @@ -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 diff --git a/styleplugins/chameleon/chameleonstyle.cpp b/styleplugins/chameleon/chameleonstyle.cpp index 497cc874..048adf84 100644 --- a/styleplugins/chameleon/chameleonstyle.cpp +++ b/styleplugins/chameleon/chameleonstyle.cpp @@ -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(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(option->styleObject)) { - w->update(shadow); - } - } - } - } else do { + if (!selected) { + do { color = option->palette.window().color(); if (color.color().isValid() && color.color().alpha() != 0) { @@ -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(option->styleObject)) { - if (!menu->geometry().contains(QCursor::pos())) - break; - } - painter->drawPixmap(shadow, shadow_pixmap); - } - } while (false); - + } while (false); + } { // 无论如何都尝试绘制,因为可能有动画存在 color = option->palette.highlight(); @@ -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); + painter->drawRoundedRect(option->rect, round, round); + painter->restore(); } } @@ -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; } diff --git a/styleplugins/chameleon/menu_shadow.svg b/styleplugins/chameleon/menu_shadow.svg deleted file mode 100644 index 471574ea..00000000 --- a/styleplugins/chameleon/menu_shadow.svg +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/styleplugins/chameleon/resources.qrc b/styleplugins/chameleon/resources.qrc deleted file mode 100644 index 2e252d8d..00000000 --- a/styleplugins/chameleon/resources.qrc +++ /dev/null @@ -1,5 +0,0 @@ - - - menu_shadow.svg - -