File: datetimeui.cpp

package info (click to toggle)
ukui-control-center 3.22.1.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,420 kB
  • sloc: cpp: 77,963; xml: 2,408; sh: 30; makefile: 4
file content (380 lines) | stat: -rw-r--r-- 14,136 bytes parent folder | download
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
/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/
#include "datetimeui.h"
#include <QApplication>

DatetimeUi::DatetimeUi()
{
    setAttribute(Qt::WA_DeleteOnClose);
    uiLayout            = new QVBoxLayout(this);
    currentDateLayout   = new QVBoxLayout(this);
    otherTimezoneLayout = new QVBoxLayout(this);
    uiLayout->addLayout(currentDateLayout);
    uiLayout->addLayout(otherTimezoneLayout);
    uiLayout->addStretch();
    uiLayout->setSpacing(40);
    uiLayout->setMargin(0);
    currentDateLayout->setSpacing(8);
    initUi();
}

DatetimeUi::~DatetimeUi()
{

}

void DatetimeUi::initUi()
{
    initObject();
    initCurrentDate();
    initOtherTimezone();
    initSignals();
}

void DatetimeUi::initObject()
{
    // 当前时间
    currentTitleLabel = new TitleLabel(this); // 标题
    clockWidget       = new ClockWidget(); // 时钟
    timeLabel         = new TimeLabel(this); // 时间
    mDateLabel        = new FixLabel(this); // 日期
    changeTZbutton    = new FixButton(this); // 修改时区
    setTimeModeLabel  = new FixLabel(this);  // 设置时间
    manualRadioButton = new QRadioButton(this); // 手动设置时间
    syncRadioButton   = new QRadioButton(this); // 自动同步时间
    syncResultLabel   = new FixLabel(); // 同步时间结果
    setDateLabel      = new FixLabel(this); // 手动设置日期
    dategroupWidget   = new DategroupWidget(this);
    timeBtnGroup      = new QButtonGroup(this);
    //~ contents_path /Date/Sync Server
    ntpSerWidget      = new ComboxWidget(tr("Sync Server"), this);
    setNtpLabel       = new FixLabel(this);
    setNtpLineEdit    = new QLineEdit(this);
    setNtpButton      = new FixButton(this);

    timeBtnGroup->addButton(manualRadioButton, 0);
    timeBtnGroup->addButton(syncRadioButton, 1);
    syncResultLabel->setStyleSheet("QLabel{color: #D9F82929;}");

    otherTimezoneTitleLabel = new TitleLabel(this);
    addTimezoneButton       = new AddButton(this, UkccFrame::BorderRadiusStyle::Around);
}

void DatetimeUi::initCurrentDate()
{
    SettingGroup *currentDateGroup  = new SettingGroup(this);
    UkccFrame    *clockFrame        = new UkccFrame(currentDateGroup);
    UkccFrame    *setTimeFrame      = new UkccFrame(currentDateGroup, UkccFrame::BorderRadiusStyle::Around, true);
    UkccFrame    *setDateFrame      = new UkccFrame(currentDateGroup, UkccFrame::BorderRadiusStyle::Around, true);
    UkccFrame    *ntpAddressFrame   = new UkccFrame(currentDateGroup, UkccFrame::BorderRadiusStyle::Around, true);

    //~ contents_path /Date/Current Date
    currentTitleLabel->setText(tr("Current Date"));
    currentDateLayout->addWidget(currentTitleLabel);
    currentDateLayout->addWidget(currentDateGroup);
    currentDateGroup->addWidget(clockFrame);
    currentDateGroup->addWidget(setTimeFrame);
    currentDateGroup->addWidget(setDateFrame);
    currentDateGroup->addWidget(ntpSerWidget);
    currentDateGroup->addWidget(ntpAddressFrame);

    QHBoxLayout *clockLayout      = new QHBoxLayout(clockFrame);
    UkccFrame   *clockRightFrame  = new UkccFrame(clockFrame);
    QVBoxLayout *clockRightLayout = new QVBoxLayout(clockRightFrame);
    UkccFrame   *dataZoneFrame    = new UkccFrame(clockRightFrame);
    QHBoxLayout *dataZoneLayout   = new QHBoxLayout(dataZoneFrame);
    clockLayout->addWidget(clockWidget);
    clockLayout->addWidget(clockRightFrame);
    clockLayout->setSpacing(26);
    clockRightLayout->setMargin(0);
    clockRightLayout->addWidget(timeLabel);
    clockRightLayout->addWidget(dataZoneFrame);
    dataZoneLayout->setMargin(0);
    dataZoneLayout->setSpacing(16);
    dataZoneLayout->addWidget(mDateLabel);
    dataZoneLayout->addWidget(changeTZbutton);
    dataZoneLayout->addStretch();
    clockFrame->setFixedHeight(156);
    clockRightFrame->setFixedHeight(102);
    mDateLabel->setMinimumWidth(280);
    changeTZbutton->setFixedWidth(150);
    //~ contents_path /Date/Timezone
    changeTZbutton->setText(tr("Timezone"));

    QHBoxLayout *setTimeLayout = new QHBoxLayout(setTimeFrame);
    QSpacerItem *setTimeSpacer = new QSpacerItem(10, 24, QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
    setTimeModeLabel->setFixedWidth(136);
    //~ contents_path /Date/Set Time
    setTimeModeLabel->setText(tr("Set Time"));
    //~ contents_path /Date/Manual Time
    manualRadioButton->setText(tr("Manual Time"));
    //~ contents_path /Date/Sync Time
    syncRadioButton->setText(tr("Sync Time"));
    setTimeLayout->setContentsMargins(16, 0, 0, 0);
    setTimeLayout->setSpacing(16);
    setTimeLayout->addWidget(setTimeModeLabel);
    setTimeLayout->addWidget(manualRadioButton);
    setTimeLayout->addSpacerItem(setTimeSpacer);
    setTimeLayout->addWidget(syncRadioButton);
    setTimeLayout->addWidget(syncResultLabel);
    setTimeLayout->addStretch();

    QHBoxLayout *setDateLayout  = new QHBoxLayout(setDateFrame);
    //~ contents_path /Date/Set Date Manually
    setDateLabel->setText(tr("Set Date Manually"));
    setDateLabel->setFixedWidth(136);
    setDateLayout->setSpacing(16);
    setDateLayout->setContentsMargins(16, 0, 0, 0);
    setDateLayout->addWidget(setDateLabel);
    setDateLayout->addWidget(dategroupWidget);

    connect(timeLabel, &TimeLabel::dateChanged, mDateLabel, [=](){
        updateDate();
    });
    connect(timeBtnGroup, QOverload<int, bool>::of(&QButtonGroup::buttonToggled), this, [=](int id, bool clicked){
        toggledBtn = id;
        if ((id == 0 && clicked) || (id == 1 && !clicked)) {
            ntpAddressFrame->hide();
            ntpSerWidget->hide();
            setDateFrame->show();
        } else {
            setDateFrame->hide();
            ntpSerWidget->show();
            if (ntpSerWidget->comboBox()->currentIndex() == (ntpSerWidget->comboBox()->count() - 1)) {
                ntpAddressFrame->show();
            } else {
                ntpAddressFrame->hide();
            }
            qApp->processEvents();
        }
    });

    ntpSerWidget->setTitleFixedWidth(136);
    QHBoxLayout *ntpAddresslayout = new QHBoxLayout(ntpAddressFrame);
    ntpAddresslayout->setContentsMargins(16, 8, 16, 8);
    ntpAddresslayout->setSpacing(16);
    ntpAddresslayout->addWidget(setNtpLabel);
    ntpAddresslayout->addWidget(setNtpLineEdit);
    ntpAddresslayout->addWidget(setNtpButton);
    setNtpLabel->setText(tr("Server Address"));
    setNtpLabel->setFixedWidth(136);
    setNtpLineEdit->setPlaceholderText(tr("Required"));
    setNtpButton->setText(tr("Save"));
    setNtpButton->setEnabled(false); // 初始状态不可点击

    connect(ntpSerWidget->comboBox(), &QComboBox::currentTextChanged, this, [=](QString text){
        if (text == tr("Customize") && !ntpSerWidget->isHidden()) {
            setNtpLineEdit->clear();
            ntpAddressFrame->show();
        } else {
            ntpAddressFrame->hide();
        }
    });
    connect(setNtpLineEdit, &QLineEdit::textChanged, this, [=](){
        setNtpLineEdit->blockSignals(true);
        while (setNtpLineEdit->text().front() == " ") {
            setNtpLineEdit->setText(setNtpLineEdit->text().remove(0, 1)); //去掉首空格
        }
        setNtpLineEdit->blockSignals(false);
        setNtpButton->setEnabled(!setNtpLineEdit->text().isEmpty());   //为空时不允许保存
    });
#ifdef Nile
    QDBusConnection::sessionBus().connect("com.kylin.kysdk.DateServer",
                                          "/com/kylin/kysdk/Date",
                                          "com.kylin.kysdk.DateInterface",
                                          "LongDateSignal",
                                          this,
                                          SLOT(updateDateFormat(QString)));
#endif
}

void DatetimeUi::initOtherTimezone()
{
    //~ contents_path /Date/Other Timezone
    otherTimezoneTitleLabel->setText(tr("Other Timezone"));
    otherTimezoneLayout->setSpacing(0);
    otherTimezoneLayout->addWidget(otherTimezoneTitleLabel);
    otherTimezoneLayout->addSpacing(8);
    otherTimezoneLayout->addWidget(addTimezoneButton);
    otherTimezoneLayout->addStretch();
}

void DatetimeUi::initSignals()
{
    connect(dategroupWidget, &DategroupWidget::dateChanged, this, &DatetimeUi::dateTimeChanged);
    connect(timeBtnGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, [=](int id){
        if (id == 0) {
            syncResultLabel->setPixmap(QPixmap(""));
            syncResultLabel->setText("");
            Q_EMIT timeModeChanged(toggledBtn, "manual");
        } else {
            Q_EMIT timeModeChanged(toggledBtn, "automatic");
        }
    });
    connect(changeTZbutton, &FixButton::clicked, this, &DatetimeUi::timezoneButtonClicked);
    connect(ntpSerWidget->comboBox(), QOverload<const QString&>::of(&QComboBox::activated), this, [=](QString server){
        if (ntpSerWidget->comboBox()->currentIndex() == 0) {
            server = "default";
            Q_EMIT ntpServerChanged(server);
        } else if (ntpSerWidget->comboBox()->currentIndex() == ntpSerWidget->comboBox()->count() - 1) {

        } else {
            Q_EMIT ntpServerChanged(server);
        }
    });
    connect(setNtpButton, &FixButton::clicked, this, [=](){
        Q_EMIT ntpServerChanged(setNtpLineEdit->text());
    });
    connect(addTimezoneButton, &AddButton::clicked, this, &DatetimeUi::addTimezoneButtonClicked);
}

void DatetimeUi::updateDate()
{
#ifdef Nile
    updateDateFormat(kdk_system_get_longformat_date());
#else
    QString timeAndWeek;
    timeAndWeek = timeLabel->dateText();
    if (!timeAndWeek.isEmpty() && !timezoneStr.isEmpty())
    {
        mDateLabel->setText(timeAndWeek + "  " + timezoneStr);
    }
#endif
}

void DatetimeUi::updateDateFormat(QString date)
{
    QString timeAndWeek;
    int longindex = 0;
    QString mLongFormatDate = date;
    if ((mLongFormatDate.at(mLongFormatDate.length() - 4) == ',')
            || ((mLongFormatDate.at(2) < '0' || mLongFormatDate.at(2) > '9')
                && (mLongFormatDate.at(2) < 'a' || mLongFormatDate.at(2) > 'z')))
    {
        longindex = 1;
     }

    QLocale l1ocale = QLocale::system();
    if ("zh_CN" == l1ocale.name()){
        l1ocale = QLocale::Chinese;
    } else if ("bo_CN" == l1ocale.name()){
        l1ocale = QLocale::Tibetan;
    } else {
        l1ocale = QLocale::English;
    }
    if (longindex)
        timeAndWeek = l1ocale.toString(QDateTime::currentDateTime(), tr("MMMM d, yy ddd")).replace("周","星期");
    else
        timeAndWeek = l1ocale.toString(QDateTime::currentDateTime(), tr("MMMM dd, yyyy ddd")).replace("周","星期");

    if (!timeAndWeek.isEmpty() && !timezoneStr.isEmpty()) {
        mDateLabel->setText(timeAndWeek + "  " + timezoneStr);
    }
}

void DatetimeUi::setTimeMode(QString mode)
{
    if (mode == "manual") {
        manualRadioButton->setChecked(true);
        toggledBtn = 0;
    } else {
        syncRadioButton->setChecked(true);
        toggledBtn = 1;
    }
}

void DatetimeUi::setSyncResultLabel(int flag)
{
    if (flag == 0) { // 失败,显示图标
        static quint32 times = 0;
        quint8 picNum = times - qFloor(times/8)*8; //限制在0~7
        QString pixName = QString(":/images/loading%1.svg").arg(picNum+10);
        QPixmap pix(pixName);
        qApp->processEvents();
        syncResultLabel->setText("");
        syncResultLabel->setPixmap(pix);
        times++;
    } else if (flag == 1) { // 失败,显示文字
        syncResultLabel->setPixmap(QPixmap(""));
        syncResultLabel->setText(tr("Sync failed"));
    } else {
        syncResultLabel->setPixmap(QPixmap("")); //成功,不显示任何内容
        syncResultLabel->setText("");
    }
}

void DatetimeUi::setNtpServers(QStringList sl)
{
    ntpSerWidget->comboBox()->clear();
    sl.insert(0, tr("Default"));
    sl.append(tr("Customize"));
    ntpSerWidget->comboBox()->addItems(sl);
}

void DatetimeUi::setNtpServer(QString s)
{
    if (s.isEmpty()) { // 系统默认
        ntpSerWidget->setCurrentIndex(0);
    } else {
        bool isContains = false;
        for (int i = 1; i < ntpSerWidget->comboBox()->count() - 1; ++i) {
            if (ntpSerWidget->comboBox()->itemText(i) == s) {
                ntpSerWidget->setCurrentIndex(i);
                isContains = true; // 列表中
                break;
            }
        }
        if (!isContains) { // 自定义
            ntpSerWidget->setCurrentIndex(ntpSerWidget->comboBox()->count() - 1);
            setNtpLineEdit->blockSignals(true);
            setNtpLineEdit->setText(s);
            setNtpLineEdit->blockSignals(false);
        }
    }
}

void DatetimeUi::setTimezoneStr(QString str)
{
    timezoneStr = str;
    updateDate();
}

void DatetimeUi::addOtherTimezone(const QString &timezone, const QString &timezoneName)
{
    TimeBtn *timeBtn = new TimeBtn(timezone, timezoneName);
    QSpacerItem *spaceIterm = new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Fixed);
    int index = otherTimezoneLayout->count() - 2; // button and Stretch
    otherTimezoneLayout->insertWidget(index, timeBtn);
    otherTimezoneLayout->insertSpacerItem(index + 1, spaceIterm);
    connect(timeBtn, &TimeBtn::deleted, this, [=](){
        otherTimezoneLayout->removeItem(spaceIterm);
        delete spaceIterm;
        Q_EMIT timezoneRemoved(timezone);
    });
}

void DatetimeUi::setAddTimezoneBtnEnabled(bool b)
{
    addTimezoneButton->setEnabled(b);
}

void DatetimeUi::resetDateFormat() {
#ifdef Nile
    dategroupWidget->resetDateEdit(kdk_system_get_shortformat());
#endif
}