-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathclockwidget.cpp
More file actions
437 lines (351 loc) · 10.8 KB
/
clockwidget.cpp
File metadata and controls
437 lines (351 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include "clockwidget.h"
#include <QPainter>
#include <QPainterPath>
#include <QTime>
#include <QTimer>
#include <QtMath>
class ClockWidget::ClockWidgetPrivate
{
public:
explicit ClockWidgetPrivate(ClockWidget *q)
: q_ptr(q)
{}
ClockWidget *q_ptr;
// 可配置属性
QColor borderColor = QColor(80, 80, 80);
QColor backgroundColor = QColor(50, 50, 50);
QColor hourColor = QColor(240, 240, 240);
QColor minuteColor = QColor(220, 220, 220);
QColor secondColor = QColor(255, 80, 80);
QColor textColor = QColor(240, 240, 240);
// 显示设置
bool smoothSeconds = true;
bool showSeconds = true;
// 定时器
QTimer *updateTimer = nullptr;
// 计算属性
double centerX = 0;
double centerY = 0;
double radius = 0;
// 常量定义
static constexpr double BORDER_WIDTH_RATIO = 1.0 / 20.0;
static constexpr double HOUR_HAND_LENGTH_RATIO = 0.5;
static constexpr double MINUTE_HAND_LENGTH_RATIO = 0.7;
static constexpr double SECOND_HAND_LENGTH_RATIO = 0.8;
static constexpr double HAND_WIDTH_RATIO = 1.0 / 100.0;
static constexpr double CENTER_DOT_RADIUS_RATIO = 1.0 / 40.0;
static constexpr double SCALE_LENGTH_RATIO = 1.0 / 15.0;
static constexpr double NUMBER_RADIUS_RATIO = 0.75;
};
ClockWidget::ClockWidget(QWidget *parent)
: QWidget(parent)
, d_ptr(new ClockWidgetPrivate(this))
{
d_ptr->updateTimer = new QTimer(this);
connect(d_ptr->updateTimer, &QTimer::timeout, this, &ClockWidget::updateClock);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
updateTimerInterval();
}
ClockWidget::~ClockWidget() = default;
auto ClockWidget::minimumSizeHint() const -> QSize
{
return {150, 150};
}
// Border color
void ClockWidget::setBorderColor(const QColor &color)
{
if (d_ptr->borderColor == color)
return;
d_ptr->borderColor = color;
update();
emit borderColorChanged(color);
}
auto ClockWidget::borderColor() const -> QColor
{
return d_ptr->borderColor;
}
// Background color
void ClockWidget::setBackgroundColor(const QColor &color)
{
if (d_ptr->backgroundColor == color)
return;
d_ptr->backgroundColor = color;
update();
emit backgroundColorChanged(color);
}
auto ClockWidget::backgroundColor() const -> QColor
{
return d_ptr->backgroundColor;
}
// Hour color
void ClockWidget::setHourColor(const QColor &color)
{
if (d_ptr->hourColor == color)
return;
d_ptr->hourColor = color;
update();
emit hourColorChanged(color);
}
auto ClockWidget::hourColor() const -> QColor
{
return d_ptr->hourColor;
}
// Minute color
void ClockWidget::setMinuteColor(const QColor &color)
{
if (d_ptr->minuteColor == color)
return;
d_ptr->minuteColor = color;
update();
emit minuteColorChanged(color);
}
auto ClockWidget::minuteColor() const -> QColor
{
return d_ptr->minuteColor;
}
// Second color
void ClockWidget::setSecondColor(const QColor &color)
{
if (d_ptr->secondColor == color)
return;
d_ptr->secondColor = color;
update();
emit secondColorChanged(color);
}
auto ClockWidget::secondColor() const -> QColor
{
return d_ptr->secondColor;
}
// Text color
void ClockWidget::setTextColor(const QColor &color)
{
if (d_ptr->textColor == color)
return;
d_ptr->textColor = color;
update();
emit textColorChanged(color);
}
auto ClockWidget::textColor() const -> QColor
{
return d_ptr->textColor;
}
// Smooth seconds
void ClockWidget::setSmoothSeconds(bool smooth)
{
if (d_ptr->smoothSeconds == smooth)
return;
d_ptr->smoothSeconds = smooth;
updateTimerInterval();
update();
emit smoothSecondsChanged(smooth);
}
bool ClockWidget::smoothSeconds() const
{
return d_ptr->smoothSeconds;
}
// Show seconds
void ClockWidget::setShowSeconds(bool show)
{
if (d_ptr->showSeconds == show)
return;
d_ptr->showSeconds = show;
update();
emit showSecondsChanged(show);
}
bool ClockWidget::showSeconds() const
{
return d_ptr->showSeconds;
}
// Public slots
void ClockWidget::updateClock()
{
update();
emit timeUpdated(QTime::currentTime());
}
// Painting
void ClockWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
updateGeometry();
drawBorder(&painter);
drawBackground(&painter);
drawScale(&painter);
drawScaleNumbers(&painter);
drawHourHand(&painter);
drawMinuteHand(&painter);
drawSecondHand(&painter);
drawCenterDot(&painter);
}
void ClockWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
updateGeometry();
}
// Private slots
void ClockWidget::updateTimerInterval()
{
if (!d_ptr->updateTimer)
return;
if (d_ptr->smoothSeconds) {
d_ptr->updateTimer->start(200); // 平滑模式:200ms更新
} else {
d_ptr->updateTimer->start(1000); // 非平滑模式:1秒更新
}
}
// Private methods
void ClockWidget::updateGeometry()
{
d_ptr->centerX = width() / 2.0;
d_ptr->centerY = height() / 2.0;
d_ptr->radius = qMin(d_ptr->centerX, d_ptr->centerY) * 0.9;
}
void ClockWidget::drawBorder(QPainter *painter)
{
const double borderWidth = d_ptr->radius * ClockWidgetPrivate::BORDER_WIDTH_RATIO;
painter->save();
painter->setPen(QPen(d_ptr->borderColor, borderWidth));
painter->setBrush(Qt::NoBrush);
painter->drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY),
d_ptr->radius - borderWidth / 2,
d_ptr->radius - borderWidth / 2);
painter->restore();
}
void ClockWidget::drawBackground(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
painter->setBrush(d_ptr->backgroundColor);
painter->drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY), d_ptr->radius, d_ptr->radius);
painter->restore();
}
void ClockWidget::drawScale(QPainter *painter)
{
painter->save();
painter->translate(d_ptr->centerX, d_ptr->centerY);
const double scaleLength = d_ptr->radius * ClockWidgetPrivate::SCALE_LENGTH_RATIO;
QPen scalePen(d_ptr->textColor);
for (int i = 0; i < 60; ++i) {
if (i % 5 == 0) {
// 小时刻度
scalePen.setWidthF(qMax(1.0, d_ptr->radius * 0.02));
} else {
// 分钟刻度
scalePen.setWidthF(qMax(1.0, d_ptr->radius * 0.01));
}
painter->setPen(scalePen);
if (i % 5 == 0) {
painter->drawLine(0,
-d_ptr->radius + scaleLength * 0.5,
0,
-d_ptr->radius + scaleLength);
} else {
painter->drawLine(0,
-d_ptr->radius + scaleLength * 0.7,
0,
-d_ptr->radius + scaleLength);
}
painter->rotate(6.0);
}
painter->restore();
}
void ClockWidget::drawScaleNumbers(QPainter *painter)
{
painter->save();
painter->setPen(d_ptr->textColor);
QFont font = this->font();
font.setPixelSize(qMax(10, int(d_ptr->radius * 0.1)));
font.setBold(true);
painter->setFont(font);
const double numberRadius = d_ptr->radius * ClockWidgetPrivate::NUMBER_RADIUS_RATIO;
for (int i = 1; i <= 12; ++i) {
const double angle = (i - 3) * 30.0 * M_PI / 180.0;
const double x = d_ptr->centerX + numberRadius * qCos(angle);
const double y = d_ptr->centerY + numberRadius * qSin(angle);
const QString number = QString::number(i);
const QRectF textRect(x - d_ptr->radius * 0.1,
y - d_ptr->radius * 0.05,
d_ptr->radius * 0.2,
d_ptr->radius * 0.1);
painter->drawText(textRect, Qt::AlignCenter, number);
}
painter->restore();
}
void ClockWidget::drawHourHand(QPainter *painter)
{
const QTime currentTime = QTime::currentTime();
const double hourAngle = 30.0 * (currentTime.hour() + currentTime.minute() / 60.0);
painter->save();
painter->translate(d_ptr->centerX, d_ptr->centerY);
painter->rotate(hourAngle);
const double handLength = d_ptr->radius * ClockWidgetPrivate::HOUR_HAND_LENGTH_RATIO;
const double handWidth = d_ptr->radius * ClockWidgetPrivate::HAND_WIDTH_RATIO;
QPainterPath path;
path.moveTo(0, handLength * 0.1);
path.lineTo(-handWidth, 0);
path.lineTo(0, -handLength);
path.lineTo(handWidth, 0);
path.closeSubpath();
painter->setPen(Qt::NoPen);
painter->setBrush(d_ptr->hourColor);
painter->drawPath(path);
painter->restore();
}
void ClockWidget::drawMinuteHand(QPainter *painter)
{
const QTime currentTime = QTime::currentTime();
const double minuteAngle = 6.0 * (currentTime.minute() + currentTime.second() / 60.0);
painter->save();
painter->translate(d_ptr->centerX, d_ptr->centerY);
painter->rotate(minuteAngle);
const double handLength = d_ptr->radius * ClockWidgetPrivate::MINUTE_HAND_LENGTH_RATIO;
const double handWidth = d_ptr->radius * ClockWidgetPrivate::HAND_WIDTH_RATIO * 0.8;
QPainterPath path;
path.moveTo(0, handLength * 0.1);
path.lineTo(-handWidth, 0);
path.lineTo(0, -handLength);
path.lineTo(handWidth, 0);
path.closeSubpath();
painter->setPen(Qt::NoPen);
painter->setBrush(d_ptr->minuteColor);
painter->drawPath(path);
painter->restore();
}
void ClockWidget::drawSecondHand(QPainter *painter)
{
if (!d_ptr->showSeconds)
return;
const QTime currentTime = QTime::currentTime();
double secondAngle = 6.0 * currentTime.second();
if (d_ptr->smoothSeconds) {
secondAngle += currentTime.msec() / 1000.0 * 6.0;
}
painter->save();
painter->translate(d_ptr->centerX, d_ptr->centerY);
painter->rotate(secondAngle);
const double handLength = d_ptr->radius * ClockWidgetPrivate::SECOND_HAND_LENGTH_RATIO;
const double handWidth = d_ptr->radius * ClockWidgetPrivate::HAND_WIDTH_RATIO * 0.4;
QPainterPath path;
path.moveTo(0, handLength * 0.2);
path.lineTo(-handWidth, 0);
path.lineTo(0, -handLength);
path.lineTo(handWidth, 0);
path.closeSubpath();
painter->setPen(Qt::NoPen);
painter->setBrush(d_ptr->secondColor);
painter->drawPath(path);
painter->restore();
}
void ClockWidget::drawCenterDot(QPainter *painter)
{
const double dotRadius = d_ptr->radius * ClockWidgetPrivate::CENTER_DOT_RADIUS_RATIO;
painter->save();
painter->setPen(Qt::NoPen);
QRadialGradient gradient(d_ptr->centerX, d_ptr->centerY, dotRadius);
gradient.setColorAt(0, d_ptr->hourColor);
gradient.setColorAt(1, d_ptr->borderColor);
painter->setBrush(gradient);
painter->drawEllipse(QPointF(d_ptr->centerX, d_ptr->centerY), dotRadius, dotRadius);
painter->restore();
}