Skip to content

Commit 3ced55a

Browse files
wjyrichdeepin-bot[bot]
authored andcommitted
feat: implement custom PageButton for calendar navigation
1. Added new PageButton class inheriting from QFrame with hover/press visual states 2. Replaced DToolButton with PageButton for next/previous navigation in calendar 3. Implemented custom painting with rounded corners and hover effects 4. Added proper event handling for mouse interactions 5. Centralized icon positioning and styling 6. Simplified button sizing by moving fixed dimensions to PageButton class The change was made to provide more consistent and visually appealing navigation buttons in the calendar widget, with better hover/pressed state feedback compared to the standard DToolButton. The custom implementation allows for more control over the visual appearance and behavior. feat: 为日历导航实现自定义 PageButton 1. 新增继承自 QFrame 的 PageButton 类,带有悬停/按下视觉状态 2. 在日历导航中用 PageButton 替换 DToolButton 3. 实现自定义绘制,包括圆角和悬停效果 4. 添加了适当的鼠标交互事件处理 5. 集中处理图标位置和样式 6. 通过将固定尺寸移至 PageButton 类简化按钮尺寸设置 此变更为日历组件提供了更一致且视觉上更吸引人的导航按钮,相比标准 DToolButton 提供了更好的悬停/按下状态反馈。自定义实现可以更好地控制视觉 外观和行为。 Pms: BUG-321969
1 parent 329f4b4 commit 3ced55a

4 files changed

Lines changed: 155 additions & 10 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-FileCopyrightText: 2019 - 2025 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: LGPL-3.0-or-later
4+
#include "pagebutton.h"
5+
6+
#include <QMouseEvent>
7+
#include <QPainter>
8+
#include <QHBoxLayout>
9+
10+
#include <DPaletteHelper>
11+
12+
DGUI_USE_NAMESPACE
13+
DWIDGET_USE_NAMESPACE
14+
15+
PageButton::PageButton(QWidget *parent)
16+
: QFrame(parent)
17+
, m_hover(false)
18+
, m_mousePress(false)
19+
, m_iconButton(new CommonIconButton(this))
20+
{
21+
initUI();
22+
}
23+
24+
PageButton::~PageButton()
25+
{
26+
}
27+
28+
void PageButton::initUI()
29+
{
30+
setFixedSize(30, 30);
31+
setForegroundRole(QPalette::BrightText);
32+
m_iconButton->setFixedSize(12, 12);
33+
m_iconButton->setForegroundRole(QPalette::BrightText);
34+
35+
// 居中放置图标
36+
QHBoxLayout* hLayout = new QHBoxLayout(this);
37+
hLayout->addWidget(m_iconButton, 0, Qt::AlignCenter);
38+
}
39+
40+
void PageButton::setIcon(const QIcon &icon)
41+
{
42+
m_iconButton->setIcon(icon, Qt::black, Qt::white);
43+
}
44+
45+
bool PageButton::event(QEvent* e)
46+
{
47+
switch (e->type()) {
48+
case QEvent::Leave:
49+
case QEvent::Enter:
50+
m_hover = e->type() == QEvent::Enter;
51+
update();
52+
break;
53+
default:
54+
break;
55+
}
56+
return QWidget::event(e);
57+
}
58+
59+
void PageButton::paintEvent(QPaintEvent* e)
60+
{
61+
Q_UNUSED(e)
62+
QPainter p(this);
63+
QPalette palette = this->palette();
64+
QColor bgColor, textColor;
65+
if (m_mousePress) {
66+
textColor = palette.highlight().color();
67+
bgColor = palette.windowText().color();
68+
bgColor.setAlphaF(0.15);
69+
} else if(m_hover) {
70+
textColor = palette.windowText().color();
71+
bgColor = palette.windowText().color();
72+
bgColor.setAlphaF(0.1);
73+
} else {
74+
textColor = palette.windowText().color();
75+
bgColor = DPaletteHelper::instance()->palette(this).color(DPalette::ItemBackground);
76+
bgColor.setAlphaF(0);
77+
}
78+
palette.setBrush(QPalette::BrightText, textColor);
79+
m_iconButton->setPalette(palette);
80+
81+
p.setBrush(bgColor);
82+
p.setRenderHint(QPainter::Antialiasing);
83+
p.setPen(Qt::NoPen);
84+
p.drawRoundedRect(rect(), 8, 8);
85+
return QFrame::paintEvent(e);
86+
}
87+
88+
void PageButton::mousePressEvent(QMouseEvent *event)
89+
{
90+
if(m_mousePress != true) {
91+
m_mousePress = true;
92+
update();
93+
}
94+
QWidget::mousePressEvent(event);
95+
}
96+
97+
void PageButton::mouseReleaseEvent(QMouseEvent* event)
98+
{
99+
if(m_mousePress == true) {
100+
m_mousePress = false;
101+
update();
102+
}
103+
104+
if (underMouse() && this->rect().contains(event->pos())) {
105+
Q_EMIT clicked();
106+
}
107+
108+
QWidget::mouseReleaseEvent(event);
109+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-FileCopyrightText: 2019 - 2025 UnionTech Software Technology Co., Ltd.
2+
//
3+
// SPDX-License-Identifier: LGPL-3.0-or-later
4+
#ifndef PAGEBUTTON_H
5+
#define PAGEBUTTON_H
6+
7+
#include "commoniconbutton.h"
8+
9+
#include <QFrame>
10+
11+
class PageButton : public QFrame
12+
{
13+
Q_OBJECT
14+
public:
15+
explicit PageButton(QWidget *parent = nullptr);
16+
~PageButton();
17+
18+
void setIcon(const QIcon &icon);
19+
20+
signals:
21+
void clicked();
22+
23+
protected:
24+
bool event(QEvent* e) override;
25+
void paintEvent(QPaintEvent* e) override;
26+
void mousePressEvent(QMouseEvent* event) override;
27+
void mouseReleaseEvent(QMouseEvent* event) override;
28+
29+
private:
30+
void initUI();
31+
32+
private:
33+
bool m_hover;
34+
bool m_mousePress;
35+
CommonIconButton *m_iconButton;
36+
};
37+
38+
#endif // PAGEBUTTON_H

plugins/dde-dock/datetime/calendar/sidebarcalendarwidget.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "constants.h"
77
#include "units.h"
88
#include "jumpcalendarbutton.h"
9+
#include "pagebutton.h"
910
#include "regionFormat.h"
1011

1112
#include <DPaletteHelper>
@@ -27,8 +28,8 @@ SidebarCalendarWidget::SidebarCalendarWidget(RegionFormat *regionFormat, QWidget
2728
, m_keyWidget(new QWidget(this))
2829
, m_keyLayout(new QGridLayout)
2930
, m_dateLabel(new QLabel(this))
30-
, m_nextPage(new DToolButton(this))
31-
, m_previousPage(new DToolButton(this))
31+
, m_nextPage(new PageButton(this))
32+
, m_previousPage(new PageButton(this))
3233
, m_firstday(Qt::Sunday)
3334
, m_dateTitleWidget(new DateTitleWidget(this))
3435
, m_weekLabel(new DLabel(this))
@@ -85,10 +86,6 @@ void SidebarCalendarWidget::initView()
8586
DFontSizeManager::instance()->bind(m_dateLabel, DFontSizeManager::T5, 65);
8687
m_nextPage->setIcon(DStyle().standardIcon(QStyle::SP_ArrowRight));
8788
m_previousPage->setIcon(DStyle().standardIcon(QStyle::SP_ArrowLeft));
88-
m_nextPage->setIconSize(QSize(12, 12));
89-
m_previousPage->setIconSize(QSize(12, 12));
90-
m_nextPage->setFixedSize(30, 30);
91-
m_previousPage->setFixedSize(30, 30);
9289

9390
m_nextPage->setFocusPolicy(Qt::NoFocus);
9491
m_previousPage->setFocusPolicy(Qt::NoFocus);
@@ -154,8 +151,8 @@ void SidebarCalendarWidget::initView()
154151
*/
155152
void SidebarCalendarWidget::initConnection()
156153
{
157-
connect(m_nextPage, &QPushButton::clicked, this, &SidebarCalendarWidget::onNextPageClicked);
158-
connect(m_previousPage, &QPushButton::clicked, this, &SidebarCalendarWidget::onPreviousPageClicked);
154+
connect(m_nextPage, &PageButton::clicked, this, &SidebarCalendarWidget::onNextPageClicked);
155+
connect(m_previousPage, &PageButton::clicked, this, &SidebarCalendarWidget::onPreviousPageClicked);
159156
connect(m_manager, &CalendarManager::sidebarFirstDayChanged, this, &SidebarCalendarWidget::onFirstDayChanged);
160157
// connect(m_manager, &CalendarManager::dateFormatChanged, this, &SidebarCalendarWidget::onDateFormatChanged);
161158
connect(m_regionFormat, &RegionFormat::shortDateFormatChanged, this, &SidebarCalendarWidget::onShortDateFormatChanged);

plugins/dde-dock/datetime/calendar/sidebarcalendarwidget.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class CalendarManager;
2424
class SidebarCalendarKeyButton;
2525
class JumpCalendarButton;
2626
class RegionFormat;
27+
class PageButton;
2728
//小日历类
2829
class SidebarCalendarWidget : public QWidget
2930
{
@@ -99,8 +100,8 @@ private slots:
99100
QWidget* m_keyWidget; //日期按钮区域控件
100101
QGridLayout* m_keyLayout; //按钮布局
101102
QLabel* m_dateLabel; //头部日期显示label
102-
DToolButton* m_nextPage; //下一页切换按键
103-
DToolButton* m_previousPage; //上一页切换按键
103+
PageButton* m_nextPage; //下一页切换按键
104+
PageButton* m_previousPage; //上一页切换按键
104105
QList<SidebarCalendarKeyButton*> m_keyButtonList; //所有的日期按钮控件
105106
int m_firstday;
106107
DateTitleWidget *m_dateTitleWidget;

0 commit comments

Comments
 (0)