Skip to content

Commit

Permalink
fix: 修复在选中框之外hover 和点击也有动画的问题 (linuxdeepin#238)
Browse files Browse the repository at this point in the history
checkbox和radio有框选区和文字区,应该只对框选区事件做处理

Log:
  • Loading branch information
Whale107 authored Aug 22, 2024
1 parent 159cd6b commit 14e15ef
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 144 deletions.
221 changes: 80 additions & 141 deletions styleplugins/chameleon/chameleonstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ static DDciIconPalette makeIconPalette(const QPalette &pal)
return iconPalette;
}

static void playDci(DDciIconPlayer *player, const DDciIcon &icon, DDciIcon::Mode mode) {
player->setIcon(icon);
player->play(mode);
}

void ChameleonStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt,
QPainter *p, const QWidget *w) const
{
Expand Down Expand Up @@ -446,10 +451,46 @@ void ChameleonStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOpti
if (!radioButton)
return;

auto dciIconPlayer = dciIconPlayers.value(radioButton);
DDciIconPlayer *dciIconPlayer = nullptr;

dciIconPlayer = radioButton->findChild<DDciIconPlayer *>("_d_radio_dciplayer", Qt::FindDirectChildrenOnly);
DDciIcon icon = radioButton->isChecked() ? DDciIcon::fromTheme("radio_checked") : DDciIcon::fromTheme("radio_unchecked");

if (!dciIconPlayer) {
dciIconPlayer = new DDciIconPlayer(radioButton);
dciIconPlayer->setObjectName("_d_radio_dciplayer");
dciIconPlayer->setDevicePixelRatio(qApp->devicePixelRatio());
dciIconPlayer->setIcon(icon);
dciIconPlayer->setMode(DDciIcon::Normal);
connect(dciIconPlayer, &DDciIconPlayer::updated, radioButton, [radioButton]() {
radioButton->update();
});
}

auto pa = DDciIconPalette::fromQPalette(radioButton->palette());
dciIconPlayer->setPalette(pa);
dciIconPlayer->setTheme(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? DDciIcon::Theme::Dark
: DDciIcon::Theme::Light);

p->setRenderHint(QPainter::SmoothPixmapTransform);
p->drawImage(opt->rect.adjusted(-1, -1, 1, 1), dciIconPlayer->currentImage());

static bool onceFlag = false; // 保证动画只触发一次
if (opt->state & QStyle::StateFlag::State_Sunken) {
DDciIcon icon = !radioButton->isChecked() ? DDciIcon::fromTheme("radio_checked") : DDciIcon::fromTheme("radio_unchecked");
playDci(dciIconPlayer, icon, DDciIcon::Pressed);
} else if (opt->state & QStyle::StateFlag::State_MouseOver) {
if (!onceFlag) {
playDci(dciIconPlayer, icon, DDciIcon::Hover);
onceFlag = true;
}
} else {
if (onceFlag) {
playDci(dciIconPlayer, icon, DDciIcon::Normal);
onceFlag = false;
}
}
return;
}
case PE_IndicatorCheckBox: {
Expand All @@ -460,10 +501,47 @@ void ChameleonStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOpti
if (!checkBox)
return;

auto dciIconPlayer = dciIconPlayers.value(checkBox);
DDciIconPlayer *dciIconPlayer = nullptr;

dciIconPlayer = checkBox->findChild<DDciIconPlayer *>("_d_checkbox_dciplayer", Qt::FindDirectChildrenOnly);
DDciIcon icon = checkBox->isChecked() ? DDciIcon::fromTheme("checkbox_checked") : DDciIcon::fromTheme("checkbox_unchecked");

if (!dciIconPlayer) {
dciIconPlayer = new DDciIconPlayer(checkBox);
dciIconPlayer->setObjectName("_d_checkbox_dciplayer");
dciIconPlayer->setDevicePixelRatio(qApp->devicePixelRatio());
dciIconPlayer->setIcon(icon);
dciIconPlayer->setMode(DDciIcon::Normal);
connect(dciIconPlayer, &DDciIconPlayer::updated, checkBox, [checkBox]() {
checkBox->update();
});
}

auto pa = DDciIconPalette::fromQPalette(checkBox->palette());
dciIconPlayer->setPalette(pa);
dciIconPlayer->setTheme(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? DDciIcon::Theme::Dark
: DDciIcon::Theme::Light);

p->setRenderHint(QPainter::SmoothPixmapTransform);
p->drawImage(opt->rect.adjusted(-1, -1, 1, 1), dciIconPlayer->currentImage());

static bool onceFlag = false; // 保证动画只触发一次
if (opt->state & QStyle::StateFlag::State_Sunken) {
DDciIcon icon = !checkBox->isChecked() ? DDciIcon::fromTheme("checkbox_checked") : DDciIcon::fromTheme("checkbox_unchecked");
auto pa = DDciIconPalette::fromQPalette(checkBox->palette());
playDci(dciIconPlayer, icon, DDciIcon::Pressed);
} else if (opt->state & QStyle::StateFlag::State_MouseOver) {
if (!onceFlag) {
playDci(dciIconPlayer, icon, DDciIcon::Hover);
onceFlag = true;
}
} else {
if (onceFlag) {
playDci(dciIconPlayer, icon, DDciIcon::Normal);
onceFlag = false;
}
}
return;
}
case PE_IndicatorTabClose: {
Expand Down Expand Up @@ -4407,145 +4485,6 @@ void ChameleonStyle::resetAttribute(QWidget *w, bool polish)
scrollbar->setProperty("_d_dtk_scrollbar_visible", true);
scrollbar->setAttribute(Qt::WA_OpaquePaintEvent, !polish);
}

if (auto radioButton = qobject_cast<QRadioButton *>(w)) {
if (polish) {
radioButton->installEventFilter(this);
} else {
radioButton->removeEventFilter(this);
}
auto dciIconPlayer = new DDciIconPlayer(radioButton);
connect(dciIconPlayer, &DDciIconPlayer::updated, radioButton, [radioButton]() {
radioButton->update();
});
dciIconPlayers.insert(radioButton, dciIconPlayer);
}

if (auto checkBox = qobject_cast<QCheckBox *>(w)) {
if (polish) {
checkBox->installEventFilter(this);
} else {
checkBox->removeEventFilter(this);
}
auto dciIconPlayer = new DDciIconPlayer(checkBox);
connect(dciIconPlayer, &DDciIconPlayer::updated, checkBox, [checkBox]() {
checkBox->update();
});
dciIconPlayers.insert(checkBox, dciIconPlayer);
}
}

static void playDci(DDciIconPlayer *player, const DDciIcon &icon, DDciIcon::Mode mode)
{
player->setIcon(icon);
player->play(mode);
}

bool ChameleonStyle::eventFilter(QObject *watched, QEvent *event)
{
if (auto radioButton = qobject_cast<QRadioButton *>(watched)) {

auto player = dciIconPlayers.value(radioButton);
if (!player)
return DStyle::eventFilter(watched, event);

DDciIcon icon = radioButton->isChecked() ? DDciIcon::fromTheme("radio_checked") : DDciIcon::fromTheme("radio_unchecked");
auto pa = DDciIconPalette::fromQPalette(radioButton->palette());

switch (event->type()) {
case QEvent::Paint:
player->setPalette(pa);
player->setDevicePixelRatio(qApp->devicePixelRatio());
player->setTheme(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? DDciIcon::Theme::Dark
: DDciIcon::Theme::Light);
break;
case QEvent::WindowActivate:
player->setIcon(icon);
player->setMode(DDciIcon::Normal);
break;
case QEvent::MouseButtonPress:
playDci(player, icon, DDciIcon::Pressed);
break;
case QEvent::HoverEnter:
playDci(player, icon, DDciIcon::Hover);
break;
case QEvent::MouseButtonRelease:
icon = !radioButton->isChecked() ? DDciIcon::fromTheme("radio_checked") : DDciIcon::fromTheme("radio_unchecked");
playDci(player, icon, DDciIcon::Hover);
break;
case QEvent::HoverLeave:
playDci(player, icon, DDciIcon::Normal);
break;
case QEvent::KeyPress:
if (auto key = dynamic_cast<QKeyEvent *>(event)) {
if (key->key() == Qt::Key_Space) {
playDci(player, icon, DDciIcon::Pressed);
}
}
break;
case QEvent::KeyRelease:
if (auto key = dynamic_cast<QKeyEvent *>(event)) {
if (key->key() == Qt::Key_Space) {
icon = !radioButton->isChecked() ? DDciIcon::fromTheme("radio_checked") : DDciIcon::fromTheme("radio_checked");
playDci(player, icon, DDciIcon::Normal);
}
}
break;
}
}
if (auto checkBox = qobject_cast<QCheckBox *>(watched)) {

auto player = dciIconPlayers.value(checkBox);
if (!player)
return DStyle::eventFilter(watched, event);

DDciIcon icon = checkBox->isChecked() ? DDciIcon::fromTheme("checkbox_checked") : DDciIcon::fromTheme("checkbox_unchecked");
auto pa = DDciIconPalette::fromQPalette(checkBox->palette());

switch (event->type()) {
case QEvent::Paint:
player->setPalette(pa);
player->setDevicePixelRatio(qApp->devicePixelRatio());
player->setTheme(DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType
? DDciIcon::Theme::Dark
: DDciIcon::Theme::Light);
break;
case QEvent::WindowActivate:
player->setIcon(icon);
player->setMode(DDciIcon::Normal);
break;
case QEvent::MouseButtonPress:
playDci(player, icon, DDciIcon::Pressed);
break;
case QEvent::HoverEnter:
playDci(player, icon, DDciIcon::Hover);
break;
case QEvent::MouseButtonRelease:
icon = !checkBox->isChecked() ? DDciIcon::fromTheme("checkbox_checked") : DDciIcon::fromTheme("checkbox_unchecked");
playDci(player, icon, DDciIcon::Hover);
break;
case QEvent::HoverLeave:
playDci(player, icon, DDciIcon::Normal);
break;
case QEvent::KeyPress:
if (auto key = dynamic_cast<QKeyEvent *>(event)) {
if (key->key() == Qt::Key_Space) {
playDci(player, icon, DDciIcon::Pressed);
}
}
break;
case QEvent::KeyRelease:
if (auto key = dynamic_cast<QKeyEvent *>(event)) {
if (key->key() == Qt::Key_Space) {
icon = !checkBox->isChecked() ? DDciIcon::fromTheme("checkbox_checked") : DDciIcon::fromTheme("checkbox_checked");
playDci(player, icon, DDciIcon::Normal);
}
}
break;
}
}
return DStyle::eventFilter(watched, event);
}

static void updateWeekendTextFormat(QCalendarWidget *calendar, QColor)
Expand Down
3 changes: 0 additions & 3 deletions styleplugins/chameleon/chameleonstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ class ChameleonStyle : public DStyle

private:
mutable QHash<const QObject*, dstyle::DStyleAnimation*> animations;
mutable QHash<const QObject*, DDciIconPlayer* > dciIconPlayers;

bool eventFilter(QObject *watched, QEvent *event) override;
};
} // namespace chameleon

Expand Down

0 comments on commit 14e15ef

Please sign in to comment.