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: 更改checkbox和radio实现方式为dci图标,增加动效 #235

Closed
wants to merge 4 commits into from
Closed
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
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ endfunction()
find_package(Dtk${VERSION_SUFFIX} REQUIRED COMPONENTS Widget)

option(ENABLE_QT_XDG_ICON_LOADER "Enable QtXdgIconLoader" ON)
find_package(Qt${QT_VERSION_MAJOR}XdgIconLoader)
find_package(Qt${QT_VERSION_MAJOR}Xdg REQUIRED)
if (NOT Qt${QT_VERSION_MAJOR}XdgIconLoader_FOUND)
find_package(Qt${QT_VERSION_MAJOR}Xdg)
if (NOT Qt${QT_VERSION_MAJOR}Xdg_FOUND)
message(WARNING " Qt${QT_VERSION_MAJOR}XdgIconLoader Not Found, DISABLE QtXdgIconLoader !")
set (ENABLE_QT_XDG_ICON_LOADER OFF)
endif()
Expand Down
7 changes: 7 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
qt5integration (5.6.32) unstable; urgency=medium

* fix: can't set transparent for Menu's background
* chore: adapt to qt6xdg

-- Deepin Packages Builder <[email protected]> Mon, 08 Jul 2024 02:29:21 +0000

qt5integration (5.6.31) unstable; urgency=medium

[ root ]
Expand Down
144 changes: 91 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,34 @@ 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);
}
}

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

p->setRenderHint(QPainter::Antialiasing, true);
auto radioButton = qobject_cast<QRadioButton *>(opt->styleObject);
if (!radioButton)
radioButton = dynamic_cast<QRadioButton *>(p->device());

if (opt->state & State_On) { //Qt::Checked
int padding = qCeil(standard.width() / 2.0 / 2.0);
QPainterPath path;

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

p->fillPath(path, getColor(opt, DPalette::Highlight));

// 内圈填充
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));
if (!radioButton)
return;

// 内圈填充
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);
auto checkBox = qobject_cast<QCheckBox *>(opt->styleObject);
if (!checkBox)
checkBox = dynamic_cast<QCheckBox *>(p->device());

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);

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 +4487,52 @@ 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
Loading