Debian Codegraph @

kysdk-qtwidgets/src/kclocktimer.cpp

raw
1 /*
2 * libkysdk-qtwidgets's Library
3 *
4 * Copyright (C) 2023, KylinSoft Co., Ltd.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Authors: Zhenyu Wang <[email protected]>
20 *
21 */
22
23 #include "kclocktimer.h"
24 #include "themeController.h"
25 #include <QBoxLayout>
26 #include <QDebug>
27 #include <QLabel>
28 #include <QStringList>
29 #include <QTimer>
30
31 namespace kdk
32 {
33
34 class Q_DECL_HIDDEN KClockTimerPrivate : public QObject, public ThemeController
35 {
36 Q_OBJECT
37 Q_DECLARE_PUBLIC(KClockTimer)
38 public:
39 KClockTimerPrivate(KClockTimer *parent);
40 void updateTime();
41
42 private:
43 KClockTimer *q_ptr;
44 QLabel *m_pMainLabel;
45 QLabel *m_pSubLabel;
46 QVBoxLayout *m_vLayout;
47
48 QTimer *m_pTimer;
49 qint64 m_runningTime;
50 bool isRunning;
51 };
52
53 KClockTimer::KClockTimer(QWidget *parent)
54 : QWidget(parent)
55 , d_ptr(new KClockTimerPrivate(this))
56 {
57 Q_D(KClockTimer);
58 }
59
60 QString KClockTimer::time()
61 {
62 Q_D(KClockTimer);
63 return d->m_pMainLabel->text();
64 }
65
66 void KClockTimer::startStopwatch()
67 {
68 Q_D(KClockTimer);
69 if (!d->isRunning) {
70 d->m_pTimer->start();
71 d->isRunning = true;
72 }
73 }
74
75 void KClockTimer::stopStopwatch()
76 {
77 Q_D(KClockTimer);
78 if (d->isRunning) {
79 d->m_pTimer->stop();
80 d->isRunning = false;
81 }
82 }
83
84 void KClockTimer::resetStopwatch()
85 {
86 Q_D(KClockTimer);
87 if (d->isRunning) {
88 d->m_pTimer->stop();
89 d->isRunning = false;
90 }
91 d->m_runningTime = 0;
92 d->m_pMainLabel->setText("00:00.00");
93 d->m_pSubLabel->setText("00:00.00");
94 }
95
96 KClockTimerPrivate::KClockTimerPrivate(KClockTimer *parent)
97 : q_ptr(parent)
98 , isRunning(false)
99 {
100 Q_Q(KClockTimer);
101 m_vLayout = new QVBoxLayout(q);
102
103 m_pMainLabel = new QLabel();
104 m_pMainLabel->setFixedSize(210, 70);
105 m_pMainLabel->setAlignment(Qt::AlignHCenter);
106 m_pMainLabel->setText("00:00.00");
107
108 m_pSubLabel = new QLabel();
109 m_pSubLabel->setFixedSize(210, 41);
110 m_pSubLabel->setAlignment(Qt::AlignHCenter);
111 m_pSubLabel->setText("00:00.00");
112
113 QPalette pale;
114 pale.setColor(QPalette::WindowText, Qt::gray);
115 m_pSubLabel->setPalette(pale);
116 m_vLayout->setSpacing(0);
117 m_vLayout->addWidget(m_pMainLabel);
118 m_vLayout->addWidget(m_pSubLabel);
119 m_runningTime = 0;
120
121 m_pTimer = new QTimer();
122 m_pTimer->setInterval(10);
123
124 QPalette mainPalette;
125 mainPalette.setColor(QPalette::WindowText, ThemeController::getCustomColorFromDT("brighttext-active"));
126 m_pMainLabel->setPalette(mainPalette);
127
128 QFont f = q->font();
129 f.setPointSize(f.pointSize() + 15);
130 m_pMainLabel->setFont(f);
131 connect(m_gsetting, &QGSettings::changed, q, [=](QString key){
132 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
133 {
134 QPalette mainPalette;
135 mainPalette.setColor(QPalette::WindowText, ThemeController::getPaletteColorFromDT("brighttext-active"));
136 m_pMainLabel->setPalette(mainPalette);
137 }
138 if(key == "systemFontSize" || key == "systemFont")
139 {
140 QFont f = q->font();
141 f.setPointSize(f.pointSize() + 15);
142 m_pMainLabel->setFont(f);
143 }
144
145 });
146 connect(m_pTimer, &QTimer::timeout, this, [=] {
147 updateTime();
148 });
149 }
150
151 void KClockTimerPrivate::updateTime()
152 {
153 Q_Q(KClockTimer);
154 m_runningTime += 10; // 增加10毫秒
155
156 int minutes = m_runningTime / (1000 * 60);
157 int seconds = (m_runningTime / 1000) % 60;
158 int milliseconds = m_runningTime % 1000;
159
160 // 取毫秒的前两位
161 milliseconds = milliseconds / 10;
162
163 // 使用QString的arg方法格式化输出
164 m_pMainLabel->setText(QString("%1:%2.%3")
165 .arg(minutes, 2, 10, QChar('0'))
166 .arg(seconds, 2, 10, QChar('0'))
167 .arg(milliseconds, 2, 10, QChar('0')));
168 m_pSubLabel->setText(QString("%1:%2.%3")
169 .arg(minutes, 2, 10, QChar('0'))
170 .arg(seconds, 2, 10, QChar('0'))
171 .arg(milliseconds, 2, 10, QChar('0')));
172 }
173
174 }
175
176 #include "kclocktimer.moc"
177 #include "moc_kclocktimer.cpp"