File: timeBtn.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 (158 lines) | stat: -rw-r--r-- 5,831 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
/*
 * 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 "timeBtn.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QDateTime>
#include "datetime.h"

TimeBtn::TimeBtn(const QString &timezone, const QString &kyTimezone)
{
    this->setFixedHeight(80);
    this->setObjectName("TimeBtn");
    this->setAttribute(Qt::WA_DeleteOnClose);

    QHBoxLayout *timeShowLayout = new QHBoxLayout(this);
    QWidget     *timeWid      = new QWidget(this);
    QVBoxLayout *timeLayout   = new QVBoxLayout(timeWid);
    labelInfo    = new FixLabel(this);    //时间,和标题字号一致
    labelTime    = new LightLabel(this);    //日期
    deleteBtn    = new QPushButton(this);

    areaInterface = new QDBusInterface("org.ukui.ukcc.session",
                                       "/Area",
                                       "org.ukui.ukcc.session.Area",
                                       QDBusConnection::sessionBus(),
                                       this);
    if (!areaInterface->isValid()) {
        qCritical() << "org.ukui.ukcc.session.Area DBus error:" << areaInterface->lastError();
    }

    timeShowLayout->setContentsMargins(0,0,18,0);
    timeLayout->setContentsMargins(18,0,18,0);
    timeShowLayout->addWidget(timeWid);
    timeShowLayout->addWidget(deleteBtn);
    deleteBtn->setFixedSize(36, 36);
    deleteBtn->setProperty("useButtonPalette", true);
    deleteBtn->setFlat(true);
    deleteBtn->setIcon(QIcon::fromTheme("edit-delete-symbolic"));
    deleteBtn->setVisible(false);

    timeLayout->addStretch();
    timeLayout->addWidget(labelInfo);
    timeLayout->addWidget(labelTime);
    timeLayout->addStretch();
    labelInfo->setObjectName("DateTime_Info");
    labelTime->setObjectName("DateTime_Time");

    thisZone = QTimeZone(timezone.toLatin1().data());
    int utcOff = thisZone.offsetFromUtc(QDateTime::currentDateTime())/3600;
    QString gmData;
    if (utcOff >= 0) {
        gmData = QString("(GMT+%1:%2)").arg(utcOff, 2, 10, QLatin1Char('0')).arg(utcOff / 60, 2, 10, QLatin1Char('0'));
    } else {
        gmData = QString("(GMT%1:%2)").arg(utcOff, 3, 10, QLatin1Char('0')).arg(utcOff / 60, 2, 10, QLatin1Char('0'));
    }
    labelInfo->setText(kyTimezone + "   " + gmData);
    QFont font;
    QGSettings *m_fontSetting = new QGSettings("org.ukui.style");
    font.setFamily(m_fontSetting->get("systemFont").toString());
    font.setPixelSize(m_fontSetting->get("systemFontSize").toInt() * 18 / 11);  //设置的是pt,按照公式计算为px,标题默认字为18px
    font.setWeight(QFont::Medium);
    labelInfo->setFont(font);

    timerId = startTimer(1000);

    updateTime(areaInterface->property("timeFormat").toString() == "24");
    connect(deleteBtn, &QPushButton::clicked, this, [=](){
        this->close();
        Q_EMIT deleted();
    });
}


TimeBtn::~TimeBtn()
{
    killTimer(timerId);
}

void TimeBtn::enterEvent(QEvent *event) {
    Q_UNUSED(event);
    deleteBtn->setVisible(true);
}

void TimeBtn::leaveEvent(QEvent *event) {
    Q_UNUSED(event);
    deleteBtn->setVisible(false);
}

void TimeBtn::timerEvent(QTimerEvent *e)
{
    if (e->timerId() == timerId) {
        updateTime(areaInterface->property("timeFormat").toString() == "24");
    }
}

void TimeBtn::updateTime(bool hour_24) {
    QString localizedTimezone = "";
    QTimeZone currentZone  = QTimeZone(localizedTimezone.toLatin1().data());
    QString time;
    QDateTime thisZoneTime;
    const double timeDelta = (thisZone.offsetFromUtc(QDateTime::currentDateTime()) - currentZone.offsetFromUtc(QDateTime::currentDateTime())) / 3600.0;
    QString dateLiteral;
    if (QDateTime::currentDateTime().toTimeZone(thisZone).time().hour() + timeDelta >= 24) {
        dateLiteral = tr("Tomorrow");
    } else if (QDateTime::currentDateTime().toTimeZone(thisZone).time().hour() + timeDelta < 0) {
        dateLiteral = tr("Yesterday");
    } else {
        dateLiteral = tr("Today");
    }
    int decimalNumber = 1;
    //小时取余,再取分钟,将15分钟的双倍只显示一位小数,其他的都显示两位小数
    switch ((thisZone.offsetFromUtc(QDateTime::currentDateTime()) - currentZone.offsetFromUtc(QDateTime::currentDateTime())) / 3600 / 60 / 15) {
    case -1:
    case -3:
    case 1:
    case 3:
        decimalNumber = 2;
        break;
    default:
        decimalNumber = 1;
        break;
    }

    QString compareLiteral;
    if (timeDelta > 0) {
        compareLiteral = tr("%1 hours earlier than local").arg(QString::number(timeDelta, 'f', decimalNumber));
    } else {
        compareLiteral = tr("%1 hours later than local").arg(QString::number(-timeDelta, 'f', decimalNumber));
    }

    thisZoneTime = QDateTime::currentDateTime().toTimeZone(thisZone);
    if (hour_24) {
        time = thisZoneTime.toString("hh : mm : ss");
    } else {
        if (QLocale::system().amText() == QString("上午") || QLocale::system().amText() == QString("སྔ་དྲོ་")) {
            time = thisZoneTime.toString("AP hh: mm : ss");
        } else {
            time = thisZoneTime.toString("hh: mm : ss AP");
        }
    }

    labelTime->setText(QString("%1  %2     %3").arg(dateLiteral).arg(time).arg(compareLiteral));
}