Skip to content

Commit

Permalink
chore: 更改checkbox和radio实现方式为dci图标,增加动效
Browse files Browse the repository at this point in the history
更改checkbox和radio实现方式为dci图标,增加动效

Log:
  • Loading branch information
Whale107 committed Aug 8, 2024
1 parent 7858504 commit 9f51ecf
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 54 deletions.
160 changes: 107 additions & 53 deletions styleplugins/chameleon/chameleonstyle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2017 - 2023 UnionTech Software Technology Co., Ltd.
* SPDX-FileCopyrightText: 2017 - 2024 UnionTech Software Technology Co., Ltd.
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "chameleonstyle.h"
Expand Down Expand Up @@ -114,6 +114,49 @@ static QRect spinboxIndicatorRect(const QRect &r)
return QRect(xOffset, yOffset, size, size);
}

static void checkDciIconEngine(bool checked, const QString &checkedIconName, const QString &uncheckedIconName
, QEvent *event, DDciIconPlayer *dciIconPlayer, QWidget *widget) {
DDciIcon icon = checked ? DDciIcon::fromTheme(checkedIconName) : DDciIcon::fromTheme(uncheckedIconName);
if (event->type() == QEvent::Paint) {
DDciIconPalette palette;
palette.setHighlight(widget->palette().highlight().color());
dciIconPlayer->setPalette(palette);
}
if (event->type() == QEvent::WindowActivate) {
dciIconPlayer->setIcon(icon);
dciIconPlayer->setMode(DDciIcon::Normal);
}
if (event->type() == QEvent::MouseButtonPress) {
dciIconPlayer->setIcon(icon);
dciIconPlayer->play(DDciIcon::Pressed);
} else if (event->type() == QEvent::HoverEnter) {
dciIconPlayer->setIcon(icon);
dciIconPlayer->play(DDciIcon::Hover);
} else if (event->type() == QEvent::MouseButtonRelease) {
icon = !checked ? DDciIcon::fromTheme(checkedIconName) : DDciIcon::fromTheme(uncheckedIconName);
dciIconPlayer->setIcon(icon);
dciIconPlayer->play(DDciIcon::Hover);
} else if (event->type() == QEvent::HoverLeave) {
dciIconPlayer->setIcon(icon);
dciIconPlayer->play(DDciIcon::Normal);
} else if (event->type() == QEvent::KeyPress) {
if (auto key = dynamic_cast<QKeyEvent *>(event)) {
if (key->key() == Qt::Key_Space) {
dciIconPlayer->setIcon(icon);
dciIconPlayer->play(DDciIcon::Pressed);
}
}
} else if (event->type() == QEvent::KeyRelease) {
if (auto key = dynamic_cast<QKeyEvent *>(event)) {
if (key->key() == Qt::Key_Space) {
icon = !checked ? DDciIcon::fromTheme(checkedIconName) : DDciIcon::fromTheme(uncheckedIconName);
dciIconPlayer->setIcon(icon);
dciIconPlayer->play(DDciIcon::Normal);
}
}
}
}

// the object is the class's instance.
template<class T>
inline static bool isTheClassObject(QObject *object)
Expand Down Expand Up @@ -439,67 +482,31 @@ void ChameleonStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOpti
return;
}
case PE_IndicatorRadioButton: {
QRect standard = opt->rect;

p->setRenderHint(QPainter::Antialiasing, true);

if (opt->state & State_On) { //Qt::Checked
int padding = qCeil(standard.width() / 2.0 / 2.0);
QPainterPath path;
auto radioButton = qobject_cast<QRadioButton *>(opt->styleObject);
if (!radioButton)
radioButton = dynamic_cast<QRadioButton *>(p->device());

path.addEllipse(standard);
path.addEllipse(standard.adjusted(padding, padding, -padding, -padding));

p->fillPath(path, getColor(opt, DPalette::Highlight));
if (!radioButton)
return;

// 内圈填充
QPainterPath innerCirclePath;
innerCirclePath.addEllipse(standard.adjusted(padding, padding, -padding, -padding));
p->fillPath(innerCirclePath, getThemTypeColor(Qt::white, Qt::black));
} else if (opt->state & State_Off) {
p->setPen(QPen(getColor(opt, DPalette::WindowText), 1));
p->drawEllipse(standard.adjusted(1, 1, -1, -1));

// 内圈填充
QPainterPath innerCirclePath;
innerCirclePath.addEllipse(standard.adjusted(1, 1, -1, -1));
p->fillPath(innerCirclePath, getThemTypeColor(Qt::transparent, QColor(0, 0, 0, qCeil(255 * 0.5))));
}
auto dciIconPlayer = dciIconPlayers.value(radioButton);

p->setRenderHint(QPainter::SmoothPixmapTransform);
p->drawImage(opt->rect.adjusted(-1, -1, 1, 1), dciIconPlayer->currentImage());
return;
}
case PE_IndicatorCheckBox: {
QRectF standard = opt->rect;

if (opt->state & State_NoChange) { //Qt::PartiallyChecked
DDrawUtils::drawBorder(p, standard, getColor(opt, DPalette::WindowText), 1, 2);

// 内部矩形填充
p->setBrush(getThemTypeColor(Qt::transparent, QColor(0, 0, 0, qCeil(255 * 0.5))));
p->drawRoundedRect(standard.adjusted(1, 1, -1, -1), 2, 2);

QRectF lineRect(0, 0, standard.width() / 2.0, 2);
lineRect.moveCenter(standard.center());
p->fillRect(lineRect, getColor(opt, DPalette::TextTitle, w));
} else if (opt->state & State_On) { //Qt::Checked
// 填充整个矩形
p->setPen(Qt::NoPen);
p->setBrush(getThemTypeColor(Qt::transparent, Qt::black));
p->drawRoundedRect(standard.adjusted(1, 1, -1, -1), 2, 2);
auto checkBox = qobject_cast<QCheckBox *>(opt->styleObject);
if (!checkBox)
checkBox = dynamic_cast<QCheckBox *>(p->device());

p->setPen(getColor(opt, DPalette::Highlight));
p->setBrush(Qt::NoBrush);

QIcon icon = QIcon::fromTheme("checked");
icon.paint(p, opt->rect.adjusted(-1, -1, 1, 1));
} else {
DDrawUtils::drawBorder(p, standard, getColor(opt, DPalette::WindowText), 1, 2);
if (!checkBox)
return;

// 内部矩形填充
p->setBrush(getThemTypeColor(Qt::transparent, getThemTypeColor(Qt::transparent, QColor(0, 0, 0, qCeil(255 * 0.5)))));
p->drawRoundedRect(standard.adjusted(1, 1, -1, -1), 2, 2);
}
auto dciIconPlayer = dciIconPlayers.value(checkBox);

p->setRenderHint(QPainter::SmoothPixmapTransform);
p->drawImage(opt->rect.adjusted(-1, -1, 1, 1), dciIconPlayer->currentImage());
return;
}
case PE_IndicatorTabClose: {
Expand Down Expand Up @@ -4495,6 +4502,53 @@ 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);
}
}

bool ChameleonStyle::eventFilter(QObject *watched, QEvent *event)
{
if (auto radioButton = qobject_cast<QRadioButton *>(watched)) {
checkDciIconEngine(radioButton->isChecked()
, "radio_checked"
, "radio_unchecked"
, event
, dciIconPlayers.value(radioButton)
, radioButton);
}
if (auto checkBox = qobject_cast<QCheckBox *>(watched)) {
checkDciIconEngine(checkBox->isChecked()
, "checkbox_checked"
, "checkbox_unchecked"
, event
, dciIconPlayers.value(checkBox)
, checkBox);
}
return false;
}

static void updateWeekendTextFormat(QCalendarWidget *calendar, QColor)
Expand Down
6 changes: 5 additions & 1 deletion styleplugins/chameleon/chameleonstyle.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/*
* SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
* SPDX-FileCopyrightText: 2017 - 2024 UnionTech Software Technology Co., Ltd.
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#ifndef CHAMELEONSTYLE_H
#define CHAMELEONSTYLE_H

#include <DStyle>
#include <DDciIconPlayer>
#include <QVariantAnimation>

DWIDGET_USE_NAMESPACE
Expand Down Expand Up @@ -125,6 +126,9 @@ 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 9f51ecf

Please sign in to comment.