Skip to content

Commit 02f63a2

Browse files
committed
refactor(clock): 重构并统一Qt Widgets与QML时钟实现风格
1. 将QScopedPointer替换为std::unique_ptr管理私有实现 2. 重构析构函数为默认实现简化代码 3. 统一iOS风格调色板配色方案 4. 优化指针绘制路径与刻度样式 5. 重构QML界面代码,修复颜色选择器逻辑 6. 调整定时器更新间隔提升秒针流畅度 7. 整理代码格式与命名规范
1 parent 5bcd650 commit 02f63a2

7 files changed

Lines changed: 295 additions & 365 deletions

File tree

src/Clock/clockwidget.cc

Lines changed: 56 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22

33
#include <QPainter>
44
#include <QPainterPath>
5+
#include <QRadialGradient>
56
#include <QTime>
67
#include <QTimer>
78
#include <QtMath>
89

910
class ClockWidget::ClockWidgetPrivate
1011
{
1112
public:
12-
explicit ClockWidgetPrivate(ClockWidget *q)
13-
: q_ptr(q)
14-
{}
13+
explicit ClockWidgetPrivate(ClockWidget *q) : q_ptr(q) {}
1514

1615
ClockWidget *q_ptr;
1716

18-
// 可配置属性
19-
QColor borderColor = QColor(80, 80, 80);
20-
QColor backgroundColor = QColor(50, 50, 50);
21-
QColor hourColor = QColor(240, 240, 240);
22-
QColor minuteColor = QColor(220, 220, 220);
23-
QColor secondColor = QColor(255, 80, 80);
24-
QColor textColor = QColor(240, 240, 240);
17+
// 可配置属性 (iOS 风格调色板)
18+
QColor borderColor = QColor(209, 209, 214); // #d1d1d6
19+
QColor backgroundColor = QColor(242, 242, 247); // #f2f2f7
20+
QColor hourColor = QColor(28, 28, 30); // #1c1c1e
21+
QColor minuteColor = QColor(58, 58, 60); // #3a3a3c
22+
QColor secondColor = QColor(255, 59, 48); // #ff3b30
23+
QColor textColor = QColor(28, 28, 30); // #1c1c1e
2524

2625
// 显示设置
2726
bool smoothSeconds = true;
@@ -36,7 +35,7 @@ class ClockWidget::ClockWidgetPrivate
3635
double radius = 0;
3736

3837
// 常量定义
39-
static constexpr double BORDER_WIDTH_RATIO = 1.0 / 20.0;
38+
static constexpr double BORDER_WIDTH_RATIO = 1.0 / 30.0;
4039
static constexpr double HOUR_HAND_LENGTH_RATIO = 0.5;
4140
static constexpr double MINUTE_HAND_LENGTH_RATIO = 0.7;
4241
static constexpr double SECOND_HAND_LENGTH_RATIO = 0.8;
@@ -47,8 +46,7 @@ class ClockWidget::ClockWidgetPrivate
4746
};
4847

4948
ClockWidget::ClockWidget(QWidget *parent)
50-
: QWidget(parent)
51-
, d_ptr(new ClockWidgetPrivate(this))
49+
: QWidget(parent), d_ptr(std::make_unique<ClockWidgetPrivate>(this))
5250
{
5351
d_ptr->updateTimer = new QTimer(this);
5452
connect(d_ptr->updateTimer, &QTimer::timeout, this, &ClockWidget::updateClock);
@@ -60,9 +58,7 @@ ClockWidget::ClockWidget(QWidget *parent)
6058
ClockWidget::~ClockWidget() = default;
6159

6260
auto ClockWidget::minimumSizeHint() const -> QSize
63-
{
64-
return {150, 150};
65-
}
61+
{ return {150, 150}; }
6662

6763
// Border color
6864
void ClockWidget::setBorderColor(const QColor &color)
@@ -76,9 +72,7 @@ void ClockWidget::setBorderColor(const QColor &color)
7672
}
7773

7874
auto ClockWidget::borderColor() const -> QColor
79-
{
80-
return d_ptr->borderColor;
81-
}
75+
{ return d_ptr->borderColor; }
8276

8377
// Background color
8478
void ClockWidget::setBackgroundColor(const QColor &color)
@@ -92,9 +86,7 @@ void ClockWidget::setBackgroundColor(const QColor &color)
9286
}
9387

9488
auto ClockWidget::backgroundColor() const -> QColor
95-
{
96-
return d_ptr->backgroundColor;
97-
}
89+
{ return d_ptr->backgroundColor; }
9890

9991
// Hour color
10092
void ClockWidget::setHourColor(const QColor &color)
@@ -108,9 +100,7 @@ void ClockWidget::setHourColor(const QColor &color)
108100
}
109101

110102
auto ClockWidget::hourColor() const -> QColor
111-
{
112-
return d_ptr->hourColor;
113-
}
103+
{ return d_ptr->hourColor; }
114104

115105
// Minute color
116106
void ClockWidget::setMinuteColor(const QColor &color)
@@ -124,9 +114,7 @@ void ClockWidget::setMinuteColor(const QColor &color)
124114
}
125115

126116
auto ClockWidget::minuteColor() const -> QColor
127-
{
128-
return d_ptr->minuteColor;
129-
}
117+
{ return d_ptr->minuteColor; }
130118

131119
// Second color
132120
void ClockWidget::setSecondColor(const QColor &color)
@@ -140,9 +128,7 @@ void ClockWidget::setSecondColor(const QColor &color)
140128
}
141129

142130
auto ClockWidget::secondColor() const -> QColor
143-
{
144-
return d_ptr->secondColor;
145-
}
131+
{ return d_ptr->secondColor; }
146132

147133
// Text color
148134
void ClockWidget::setTextColor(const QColor &color)
@@ -156,9 +142,7 @@ void ClockWidget::setTextColor(const QColor &color)
156142
}
157143

158144
auto ClockWidget::textColor() const -> QColor
159-
{
160-
return d_ptr->textColor;
161-
}
145+
{ return d_ptr->textColor; }
162146

163147
// Smooth seconds
164148
void ClockWidget::setSmoothSeconds(bool smooth)
@@ -173,9 +157,7 @@ void ClockWidget::setSmoothSeconds(bool smooth)
173157
}
174158

175159
bool ClockWidget::smoothSeconds() const
176-
{
177-
return d_ptr->smoothSeconds;
178-
}
160+
{ return d_ptr->smoothSeconds; }
179161

180162
// Show seconds
181163
void ClockWidget::setShowSeconds(bool show)
@@ -189,9 +171,7 @@ void ClockWidget::setShowSeconds(bool show)
189171
}
190172

191173
bool ClockWidget::showSeconds() const
192-
{
193-
return d_ptr->showSeconds;
194-
}
174+
{ return d_ptr->showSeconds; }
195175

196176
// Public slots
197177
void ClockWidget::updateClock()
@@ -210,8 +190,8 @@ void ClockWidget::paintEvent(QPaintEvent *event)
210190

211191
updateGeometry();
212192

213-
drawBorder(painter);
214193
drawBackground(painter);
194+
drawBorder(painter);
215195
drawScale(painter);
216196
drawScaleNumbers(painter);
217197
drawHourHand(painter);
@@ -233,7 +213,7 @@ void ClockWidget::updateTimerInterval()
233213
return;
234214

235215
if (d_ptr->smoothSeconds) {
236-
d_ptr->updateTimer->start(200); // 平滑模式:200ms更新
216+
d_ptr->updateTimer->start(50); // 平滑模式:20fps
237217
} else {
238218
d_ptr->updateTimer->start(1000); // 非平滑模式:1秒更新
239219
}
@@ -247,25 +227,30 @@ void ClockWidget::updateGeometry()
247227
d_ptr->radius = qMin(d_ptr->centerX, d_ptr->centerY) * 0.9;
248228
}
249229

250-
void ClockWidget::drawBorder(QPainter &painter)
230+
void ClockWidget::drawBackground(QPainter &painter)
251231
{
252-
const double borderWidth = d_ptr->radius * ClockWidgetPrivate::BORDER_WIDTH_RATIO;
232+
// 径向渐变 (signature: 表盘穹顶效果, 中心微亮 → 边缘微暗)
233+
QRadialGradient gradient(d_ptr->centerX, d_ptr->centerY, d_ptr->radius);
234+
gradient.setColorAt(0.0, d_ptr->backgroundColor.lighter(103));
235+
gradient.setColorAt(1.0, d_ptr->backgroundColor.darker(105));
253236

254237
painter.save();
255-
painter.setPen(QPen(d_ptr->borderColor, borderWidth));
256-
painter.setBrush(Qt::NoBrush);
257-
painter.drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY),
258-
d_ptr->radius - borderWidth / 2,
259-
d_ptr->radius - borderWidth / 2);
238+
painter.setPen(Qt::NoPen);
239+
painter.setBrush(gradient);
240+
painter.drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY), d_ptr->radius, d_ptr->radius);
260241
painter.restore();
261242
}
262243

263-
void ClockWidget::drawBackground(QPainter &painter)
244+
void ClockWidget::drawBorder(QPainter &painter)
264245
{
246+
const double borderWidth = d_ptr->radius * ClockWidgetPrivate::BORDER_WIDTH_RATIO;
247+
265248
painter.save();
266-
painter.setPen(Qt::NoPen);
267-
painter.setBrush(d_ptr->backgroundColor);
268-
painter.drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY), d_ptr->radius, d_ptr->radius);
249+
painter.setPen(QPen(d_ptr->borderColor, borderWidth));
250+
painter.setBrush(Qt::NoBrush);
251+
painter.drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY),
252+
d_ptr->radius - borderWidth / 2,
253+
d_ptr->radius - borderWidth / 2);
269254
painter.restore();
270255
}
271256

@@ -275,29 +260,24 @@ void ClockWidget::drawScale(QPainter &painter)
275260
painter.translate(d_ptr->centerX, d_ptr->centerY);
276261

277262
const double scaleLength = d_ptr->radius * ClockWidgetPrivate::SCALE_LENGTH_RATIO;
278-
QPen scalePen(d_ptr->textColor);
279263

280264
for (int i = 0; i < 60; ++i) {
281265
if (i % 5 == 0) {
282-
// 小时刻度
283-
scalePen.setWidthF(qMax(1.0, d_ptr->radius * 0.02));
284-
} else {
285-
// 分钟刻度
286-
scalePen.setWidthF(qMax(1.0, d_ptr->radius * 0.01));
287-
}
288-
289-
painter.setPen(scalePen);
290-
291-
if (i % 5 == 0) {
292-
painter.drawLine(0,
293-
-d_ptr->radius + scaleLength * 0.5,
294-
0,
295-
-d_ptr->radius + scaleLength);
266+
// 小时刻度: 深色粗线
267+
QPen scalePen(d_ptr->textColor);
268+
scalePen.setWidthF(qMax(1.5, d_ptr->radius * 0.025));
269+
scalePen.setCapStyle(Qt::RoundCap);
270+
painter.setPen(scalePen);
271+
painter.drawLine(
272+
0, -d_ptr->radius + scaleLength * 0.5, 0, -d_ptr->radius + scaleLength);
296273
} else {
297-
painter.drawLine(0,
298-
-d_ptr->radius + scaleLength * 0.7,
299-
0,
300-
-d_ptr->radius + scaleLength);
274+
// 分钟刻度: 浅色细线
275+
QPen scalePen(QColor(199, 199, 204)); // #c7c7cc
276+
scalePen.setWidthF(qMax(0.8, d_ptr->radius * 0.008));
277+
scalePen.setCapStyle(Qt::RoundCap);
278+
painter.setPen(scalePen);
279+
painter.drawLine(
280+
0, -d_ptr->radius + scaleLength * 0.7, 0, -d_ptr->radius + scaleLength);
301281
}
302282

303283
painter.rotate(6.0);
@@ -346,6 +326,7 @@ void ClockWidget::drawHourHand(QPainter &painter)
346326
const double handLength = d_ptr->radius * ClockWidgetPrivate::HOUR_HAND_LENGTH_RATIO;
347327
const double handWidth = d_ptr->radius * ClockWidgetPrivate::HAND_WIDTH_RATIO;
348328

329+
// 菱形指针: 尾端 → 左中 → 尖端 → 右中
349330
QPainterPath path;
350331
path.moveTo(0, handLength * 0.1);
351332
path.lineTo(-handWidth, 0);
@@ -405,6 +386,7 @@ void ClockWidget::drawSecondHand(QPainter &painter)
405386
const double handLength = d_ptr->radius * ClockWidgetPrivate::SECOND_HAND_LENGTH_RATIO;
406387
const double handWidth = d_ptr->radius * ClockWidgetPrivate::HAND_WIDTH_RATIO * 0.4;
407388

389+
// 秒针: 较长尾端配重, 细针身
408390
QPainterPath path;
409391
path.moveTo(0, handLength * 0.2);
410392
path.lineTo(-handWidth, 0);
@@ -426,9 +408,10 @@ void ClockWidget::drawCenterDot(QPainter &painter)
426408
painter.save();
427409
painter.setPen(Qt::NoPen);
428410

411+
// 径向渐变中心帽: 深色中心 → 边缘
429412
QRadialGradient gradient(d_ptr->centerX, d_ptr->centerY, dotRadius);
430413
gradient.setColorAt(0, d_ptr->hourColor);
431-
gradient.setColorAt(1, d_ptr->borderColor);
414+
gradient.setColorAt(1, d_ptr->hourColor.darker(130));
432415

433416
painter.setBrush(gradient);
434417
painter.drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY), dotRadius, dotRadius);

src/Clock/clockwidget.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <QWidget>
44

5+
#include <memory>
6+
57
class ClockWidget : public QWidget
68
{
79
Q_OBJECT
@@ -81,5 +83,5 @@ private slots:
8183
void updateGeometry();
8284

8385
class ClockWidgetPrivate;
84-
QScopedPointer<ClockWidgetPrivate> d_ptr;
86+
std::unique_ptr<ClockWidgetPrivate> d_ptr;
8587
};

0 commit comments

Comments
 (0)