feat: 任务栏日期弹框根据设置的格式去显示

根据设置的长日期格式,调整任务栏日期弹框的显示

Log: 任务栏日期Tips根据设置的格式去显示
Influence: 任务栏日期弹框
Task: https://pms.uniontech.com/task-view-157789.html
Change-Id: I99b5acefbb82e0a583fc5902cf1bb814db07c116
This commit is contained in:
dengbo 2022-07-07 16:19:23 +08:00 committed by wubw
parent 37e7e90cbc
commit 1e08418fbf
3 changed files with 152 additions and 6 deletions

View File

@ -220,11 +220,21 @@ void DatetimePlugin::pluginSettingsChanged()
void DatetimePlugin::updateCurrentTimeString()
{
const QDateTime currentDateTime = QDateTime::currentDateTime();
auto lang = QLocale::system().language();
bool isZhLocale = lang == QLocale::Chinese || lang == QLocale::Tibetan || lang == QLocale::Uighur;
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"));
// 如果系统语言环境为中文(包含藏语和维语),按照中文的显示格式去显示,否则按照当地的日期格式显示
if (m_centralWidget->is24HourFormat()) {
if (isZhLocale)
m_dateTipsLabel->setText(m_centralWidget->getDateTime() + currentDateTime.toString(" hh:mm:ss"));
else
m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" HH:mm:ss"));
} else {
if (isZhLocale)
m_dateTipsLabel->setText(m_centralWidget->getDateTime() + currentDateTime.toString(" hh:mm:ss A"));
else
m_dateTipsLabel->setText(currentDateTime.date().toString(Qt::SystemLocaleLongDate) + currentDateTime.toString(" HH:mm:ss A"));
}
const QString currentString = currentDateTime.toString("yyyy/MM/dd hh:mm");

View File

@ -39,6 +39,8 @@ DWIDGET_USE_NAMESPACE
DatetimeWidget::DatetimeWidget(QWidget *parent)
: QWidget(parent)
, m_24HourFormat(false)
, m_longDateFormatType(0)
, m_weekdayFormatType(0)
, m_timeOffset(false)
, m_timedateInter(new Timedate("com.deepin.daemon.Timedate", "/com/deepin/daemon/Timedate", QDBusConnection::sessionBus(), this))
, m_shortDateFormat("yyyy-MM-dd")
@ -47,9 +49,14 @@ DatetimeWidget::DatetimeWidget(QWidget *parent)
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
setShortDateFormat(m_timedateInter->shortDateFormat());
setShortTimeFormat(m_timedateInter->shortTimeFormat());
setWeekdayFormat(m_timedateInter->weekdayFormat());
setLongDateFormat(m_timedateInter->longDateFormat());
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::TimeUpdate, this, [ = ]{
if (isVisible()) {
@ -115,6 +122,126 @@ 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::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;
}
}
}
/**
* @brief DatetimeWidget::updateWeekdayTimeString
*/
void DatetimeWidget::updateDateTimeString()
{
const QDateTime currentDateTime = QDateTime::currentDateTime();
int year = currentDateTime.date().year();
int month = currentDateTime.date().month();
int day = currentDateTime.date().day();
QString longTimeFormat = QString(tr("%1year%2month%3day")).arg(year).arg(month).arg(day);
switch (m_longDateFormatType) {
case 0 :
m_dateTime = longTimeFormat;
break;
case 1:
m_dateTime = longTimeFormat + QString(" ") + m_weekFormat;
break;
case 2:
m_dateTime = m_weekFormat + QString(" ") + longTimeFormat;
break;
default:
m_dateTime = longTimeFormat + QString(" ") + m_weekFormat;
break;
}
}
/**
* @brief DatetimeWidget::curTimeSize
* @return

View File

@ -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);
@ -51,18 +52,26 @@ public slots:
private Q_SLOTS:
void setShortDateFormat(int type);
void setShortTimeFormat(int type);
void setLongDateFormat(int type);
void setWeekdayFormat(int type);
private:
QSize curTimeSize() const;
void updateDateTimeString();
void updateWeekdayFormat();
private:
bool m_24HourFormat;
int m_longDateFormatType;
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;
};
#endif // DATETIMEWIDGET_H