From bbfc2cb909ef6ebf5f5824cb65486ce2941df997 Mon Sep 17 00:00:00 2001 From: liweigang Date: Mon, 25 Mar 2024 14:03:15 +0800 Subject: [PATCH] feat: add longdate and weekday format Signed-off-by: liweigang --- plugins/datetime/datetimeplugin.cpp | 8 +- plugins/datetime/datetimewidget.cpp | 199 ++++++++++++++++++++++++++++ plugins/datetime/datetimewidget.h | 17 ++- 3 files changed, 218 insertions(+), 6 deletions(-) diff --git a/plugins/datetime/datetimeplugin.cpp b/plugins/datetime/datetimeplugin.cpp index 93c4e89..feaf50f 100644 --- a/plugins/datetime/datetimeplugin.cpp +++ b/plugins/datetime/datetimeplugin.cpp @@ -221,10 +221,10 @@ void DatetimePlugin::updateCurrentTimeString() { const QDateTime currentDateTime = QDateTime::currentDateTime(); - if (m_centralWidget->is24HourFormat()) - m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" HH:mm:ss")); - else - m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" hh:mm:ss A")); + // 实时刷新日期,防止日期显示错误 + m_centralWidget->updateDateTimeString(); + + m_dateTipsLabel->setText(m_centralWidget->getDateTime()); const QString currentString = currentDateTime.toString("yyyy/MM/dd hh:mm"); diff --git a/plugins/datetime/datetimewidget.cpp b/plugins/datetime/datetimewidget.cpp index 08bfbb3..4e0b252 100644 --- a/plugins/datetime/datetimewidget.cpp +++ b/plugins/datetime/datetimewidget.cpp @@ -39,17 +39,29 @@ DWIDGET_USE_NAMESPACE DatetimeWidget::DatetimeWidget(QWidget *parent) : QWidget(parent) , m_24HourFormat(false) + , m_longDateFormatType(0) + , m_longTimeFormatType(0) , m_timeOffset(false) , m_timedateInter(new Timedate("com.deepin.daemon.Timedate", "/com/deepin/daemon/Timedate", QDBusConnection::sessionBus(), this)) , m_shortDateFormat("yyyy-MM-dd") , m_shortTimeFormat("hh:mm") + , m_longTimeFormat(" hh:mm:ss") + , m_weekdayFormatType(0) { setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE); setShortDateFormat(m_timedateInter->shortDateFormat()); setShortTimeFormat(m_timedateInter->shortTimeFormat()); + setLongDateFormat(m_timedateInter->longDateFormat()); + setWeekdayFormat(m_timedateInter->weekdayFormat()); + setLongTimeFormat(m_timedateInter->longTimeFormat()); + set24HourFormat(m_timedateInter->use24HourFormat()); + updateDateTimeString(); connect(m_timedateInter, &Timedate::ShortDateFormatChanged, this, &DatetimeWidget::setShortDateFormat); connect(m_timedateInter, &Timedate::ShortTimeFormatChanged, this, &DatetimeWidget::setShortTimeFormat); + connect(m_timedateInter, &Timedate::LongDateFormatChanged, this, &DatetimeWidget::setLongDateFormat); + connect(m_timedateInter, &Timedate::WeekdayFormatChanged, this, &DatetimeWidget::setWeekdayFormat); + connect(m_timedateInter, &Timedate::LongTimeFormatChanged, this, &DatetimeWidget::setLongTimeFormat); //连接日期时间修改信号,更新日期时间插件的布局 connect(m_timedateInter, &Timedate::TimeUpdate, this, [ = ]{ if (isVisible()) { @@ -65,6 +77,7 @@ void DatetimeWidget::set24HourFormat(const bool value) } m_24HourFormat = value; + updateLongTimeFormat(); update(); if (isVisible()) { @@ -115,6 +128,192 @@ void DatetimeWidget::setShortTimeFormat(int type) } } +/** + * @brief DatetimeWidget::setLongDateFormat 根据类型设置长日期显示格式 + * @param type 自定义类型 + */ +void DatetimeWidget::setLongDateFormat(int type) +{ + if (m_longDateFormatType == type) + return; + + m_longDateFormatType = type; + updateDateTimeString(); +} + +/** + * @brief DatetimeWidget::setLongTimeFormat 根据类型设置长时间显示格式 + * @param type 自定义类型 + */ +void DatetimeWidget::setLongTimeFormat(int type) +{ + if (m_longTimeFormatType == type) + return; + + m_longTimeFormatType = type; + updateLongTimeFormat(); + updateDateTimeString(); +} + +/** + * @brief DatetimeWidget::setWeekdayFormat 根据类型设置周显示格式 + * @param type 自定义类型 + */ +void DatetimeWidget::setWeekdayFormat(int type) +{ + if (m_weekdayFormatType == type) + return; + + m_weekdayFormatType = type; + updateWeekdayFormat(); + updateDateTimeString(); +} + +/** + * @brief DatetimeWidget::updateWeekdayFormat 更新周的显示格式 + */ +void DatetimeWidget::updateWeekdayFormat() +{ + const QDateTime currentDateTime = QDateTime::currentDateTime(); + auto dayOfWeek = currentDateTime.date().dayOfWeek(); + + if (0 == m_weekdayFormatType) { + switch (dayOfWeek) { + case 1: + m_weekFormat = tr("Monday"); //星期一 + break; + case 2: + m_weekFormat = tr("Tuesday"); //星期二 + break; + case 3: + m_weekFormat = tr("Wednesday"); //星期三 + break; + case 4: + m_weekFormat = tr("Thursday"); //星期四 + break; + case 5: + m_weekFormat = tr("Friday"); //星期五 + break; + case 6: + m_weekFormat = tr("Saturday"); //星期六 + break; + case 7: + m_weekFormat = tr("Sunday"); //星期天 + break; + default: + m_weekFormat = tr("Monday"); //星期一 + break; + } + } else { + switch (dayOfWeek) { + case 1: + m_weekFormat = tr("monday"); //周一 + break; + case 2: + m_weekFormat = tr("tuesday"); //周二 + break; + case 3: + m_weekFormat = tr("wednesday"); //周三 + break; + case 4: + m_weekFormat = tr("thursday"); //周四 + break; + case 5: + m_weekFormat = tr("friday"); //周五 + break; + case 6: + m_weekFormat = tr("saturday"); //周六 + break; + case 7: + m_weekFormat = tr("sunday"); //周天 + break; + default: + m_weekFormat = tr("monday"); //周一 + break; + } + } +} + +void DatetimeWidget::updateLongTimeFormat() +{ + if (m_24HourFormat) { + switch (m_longTimeFormatType) { + case 0: m_longTimeFormat = " h:mm:ss"; break; + case 1: m_longTimeFormat = " hh:mm:ss"; break; + default: m_longTimeFormat = " hh:mm:ss"; break; + } + } else { + switch (m_longTimeFormatType) { + case 0: m_longTimeFormat = " h:mm:ss A"; break; + case 1: m_longTimeFormat = " hh:mm:ss A"; break; + default: m_longTimeFormat = " hh:mm:ss A"; break; + } + } +} + +/** + * @brief DatetimeWidget::updateWeekdayTimeString 更新任务栏时间标签的显示 + */ +void DatetimeWidget::updateDateTimeString() +{ + QString longTimeFormat(""); + const QDateTime currentDateTime = QDateTime::currentDateTime(); + int year = currentDateTime.date().year(); + int month = currentDateTime.date().month(); + int day = currentDateTime.date().day(); + + auto lang = QLocale::system().language(); + bool isZhLocale = lang == QLocale::Chinese || lang == QLocale::Tibetan || lang == QLocale::Uighur; + + // 根据相应语言去显示对应的格式 + // 中文: 格式为xxxx年xx月xx日 星期x hh:mm:ss,如: 2024年3月25日 星期一 13:27:55 + // 英文: 格式为x x, xxxx, x hh:mm:ss,如 Mar 25, 2024, Monday 13:27:55 + // 其他语言: 按照国际当地长时间格式显示 + if (isZhLocale) { + QString longTimeFormat = QString(tr("%1year%2month%3day")).arg(year).arg(month).arg(day); + + // 实时更新周的日期显示 + updateWeekdayFormat(); + + switch (m_longDateFormatType) { + case 0: + m_dateTime = longTimeFormat + currentDateTime.toString(m_longTimeFormat); + break; + case 1: + m_dateTime = longTimeFormat + QString(" ") + m_weekFormat + currentDateTime.toString(m_longTimeFormat); + break; + case 2: + m_dateTime = m_weekFormat + QString(" ") + longTimeFormat + currentDateTime.toString(m_longTimeFormat); + break; + default: + m_dateTime = longTimeFormat + QString(" ") + m_weekFormat + currentDateTime.toString(m_longTimeFormat); + break; + } + } else if (lang == QLocale::English) { + auto longDateString = currentDateTime.date().toString(Qt::SystemLocaleLongDate); + auto week = longDateString.split(",").at(0); + // 获取英文的日期格式字符串,-2是去掉","和" " + auto longDateTimeFormat = longDateString.right(longDateString.size() - week.size() - 2); + + switch (m_longDateFormatType) { + case 0: + m_dateTime = longDateTimeFormat + currentDateTime.toString(m_longTimeFormat); + break; + case 1: + m_dateTime = longDateTimeFormat + QString(", ") + week + currentDateTime.toString(m_longTimeFormat); + break; + case 2: + m_dateTime = week + QString(", ") + longDateTimeFormat + currentDateTime.toString(m_longTimeFormat); + break; + default: + m_dateTime = longDateTimeFormat + QString(", ") + week + currentDateTime.toString(m_longTimeFormat); + break; + } + } else { + m_dateTime = currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(m_longTimeFormat); + } +} + /** * @brief DatetimeWidget::curTimeSize 调整时间日期字体大小 * @return 返回时间和日期绘制的区域大小 diff --git a/plugins/datetime/datetimewidget.h b/plugins/datetime/datetimewidget.h index 42cba49..d6ab548 100644 --- a/plugins/datetime/datetimewidget.h +++ b/plugins/datetime/datetimewidget.h @@ -33,10 +33,11 @@ class DatetimeWidget : public QWidget Q_OBJECT public: - explicit DatetimeWidget(QWidget *parent = 0); + explicit DatetimeWidget(QWidget *parent = nullptr); - bool is24HourFormat() const { return m_24HourFormat; } QSize sizeHint() const; + inline bool is24HourFormat() const { return m_24HourFormat; } + inline QString getDateTime() { return m_dateTime; } protected: void resizeEvent(QResizeEvent *event); @@ -47,22 +48,34 @@ signals: public slots: void set24HourFormat(const bool value); + void updateDateTimeString(); private Q_SLOTS: void setShortDateFormat(int type); void setShortTimeFormat(int type); + void setLongDateFormat(int type); + void setWeekdayFormat(int type); + void setLongTimeFormat(int type); private: QSize curTimeSize() const; + void updateWeekdayFormat(); + void updateLongTimeFormat(); private: bool m_24HourFormat; + int m_longDateFormatType; + int m_longTimeFormatType; + int m_weekdayFormatType; mutable QFont m_timeFont; mutable QFont m_dateFont; mutable int m_timeOffset; Timedate *m_timedateInter; QString m_shortDateFormat; QString m_shortTimeFormat; + QString m_dateTime; + QString m_weekFormat; + QString m_longTimeFormat; }; #endif // DATETIMEWIDGET_H -- 2.33.1