*Fix:#154776 #157635 日历部分英文显示不全

This commit is contained in:
zhangyinjie 2023-02-17 15:11:17 +08:00
parent 29b3d834ce
commit f93a4e5686
5 changed files with 422 additions and 151 deletions

View File

@ -9,6 +9,7 @@ set(HEADERS
ukuicalendarplugin.h
ukuicalendarwidget.h
calendarbutton.h
calendarbuttontext.h
ukuiwebviewdialog.h
lunarcalendarwidget/frmlunarcalendarwidget.h
lunarcalendarwidget/lunarcalendarinfo.h
@ -25,6 +26,7 @@ set(SOURCES
ukuicalendarplugin.cpp
ukuicalendarwidget.cpp
calendarbutton.cpp
calendarbuttontext.cpp
ukuiwebviewdialog.cpp
lunarcalendarwidget/frmlunarcalendarwidget.cpp
lunarcalendarwidget/lunarcalendarinfo.cpp

View File

@ -49,7 +49,9 @@
#define UKUI_CONTROL_CENTER_PATH "/org/freedesktop/Accounts/User1000"
#define UKUI_CONTROL_CENTER_INTERFACE "org.freedesktop.Accounts.User"
#define PROPERTIES_NAME "FormatsLocale"
#define US_FORMATS "en_US.UTF-8"
#define US_FORMATS "en_US.UTF-8"
#define ZH_CN_FORMATS "zh_CN.UTF-8"
#define BO_CN_FORMATS "bo_CN.UTF-8" //藏文
#define PANEL_SIZE_LARGE 92
#define PANEL_SIZE_MEDIUM 70
@ -90,22 +92,22 @@ CalendarButton::CalendarButton(IUKUIPanelPlugin *plugin, QWidget *parent):
KYSDK_TIMERINTERFACE,
"TimeSignal",
this,
SLOT(updateTimeText(QString))
SLOT(updateBtnText(QString))
);
QDBusConnection::systemBus().connect(KYSDK_TIMERSERVER,
KYSDK_TIMERPATH,
KYSDK_TIMERINTERFACE,
"TimeChangeSignal",
this,
SLOT(updateTimeText(QString))
SLOT(updateBtnText(QString))
);
m_listenGsettings = new ListenGsettings();
QObject::connect(m_listenGsettings,&ListenGsettings::iconsizechanged,[this]{updateTimeText(QString());});
QObject::connect(m_listenGsettings,&ListenGsettings::panelpositionchanged,[this]{updateTimeText(QString());});
QObject::connect(m_listenGsettings,&ListenGsettings::iconsizechanged,[this]{updateBtnText(QString());});
QObject::connect(m_listenGsettings,&ListenGsettings::panelpositionchanged,[this]{updateBtnText(QString());});
//更新日历按钮内容
updateTimeText(QString());
updateBtnText(QString());
}
CalendarButton::~CalendarButton()
@ -162,7 +164,7 @@ void CalendarButton::initTimeGsettings()
} else if(key == CALENDAR_FORMAT) {
m_calendarFormat = m_timeGsettings->get(CALENDAR_FORMAT).toString();
}
updateTimeText(QString());
updateBtnText(QString());
});
QStringList ketList = m_timeGsettings->keys();
@ -187,7 +189,7 @@ void CalendarButton::initFontGsettings()
if(keys == SYSTEM_FONT_SIZE || keys == SYSTEM_FONT){
m_systemFontSize = m_fontGsettings->get(SYSTEM_FONT_SIZE).toString();
m_systemFont = m_fontGsettings->get(SYSTEM_FONT).toString();
updateTimeText(QString());
updateBtnText(QString());
}
});
@ -218,13 +220,13 @@ void CalendarButton::setControlTime()
/*
*
*
* ()
* 24
* hh:mm ddd hh:mm ddd
* yyyy/MM/dd MM/dd
* hh:mm ddd
* yyyy/MM/dd
* 12
* AM hh:mm ddd AM hh:mm
* yyyy/MM/dd ddd MM/dd
* AM hh:mm ddd
* yyyy/MM/dd
*
* :
* 24
@ -237,148 +239,21 @@ void CalendarButton::setControlTime()
* ddd ddd
* yyyy/MM/dd MM/dd
*/
void CalendarButton::updateTimeText(QString timerStr)
void CalendarButton::updateBtnText(QString timerStr)
{
Q_UNUSED(timerStr);
//获取 上下午、时间、星期、日期(年、月、日)
QDateTime dataTimeNow = QDateTime::currentDateTime();
QString time = dataTimeNow.toString("A hh:mm");
QStringList timeList = time.split(" ", QString::SkipEmptyParts);
QString timeAMPM = timeList.at(0);
QString time12 = timeList.at(1);
QString time24 = dataTimeNow.toString("hh:mm");
QString week = dataTimeNow.toString("dddd");
QString year = dataTimeNow.toString("yyyy");
QString mon = dataTimeNow.toString("MM");
QString day = dataTimeNow.toString("dd");
QList<double_t> systemFonts;
systemFonts.append(10);
systemFonts.append(12);
systemFonts.append(13.5);
systemFonts.append(15);
double fontSize = m_fontGsettings->get(SYSTEM_FONT_SIZE).toDouble();
//根据时间日期样式,组合时间文本
QString formatsLocale = getFormatsLocale();
QString displayText;
if(m_plugin->panel()->isHorizontal()) {
//字号限制,防止调整任务栏大小或者系统增加字号时导致显示不全
m_panelSize = m_listenGsettings->m_panelGsettings->get(PANEL_SIZE_KEY).toInt();
if(m_panelSize < PANEL_SIZE_MEDIUM) {
if(fontSize >= systemFonts.first())
this->setFont(QFont(m_systemFont, systemFonts.first()));
else
this->setFont(QFont(m_systemFont, fontSize));
} else if(m_panelSize >= PANEL_SIZE_MEDIUM) {
if(fontSize >= systemFonts.last())
this->setFont(QFont(m_systemFont, systemFonts.last()));
else
this->setFont(QFont(m_systemFont, fontSize));
}
if(!QString::compare("24", m_timeFormat)) {
//拼接 时间 和 星期
displayText = time24 + " " + week + "\n";
//拼接 年月日
QString data = year + " " + mon + " " + day;
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(data);
bool isShowYear = true;
if(fontSize > CALENDAR_BUTTON_WIDTH-5) {
isShowYear = false;
}
if(QString::compare("cn", m_dataFormat)) {
if(isShowYear)
data = year + "-" + mon + "-" + day;
else
data = mon + "-" + day;
} else {
if(isShowYear)
data = year + "/" + mon + "/" + day;\
else
data = mon + "/" + day;
}
displayText += data;
} else {
// 拼接 AM 时间 星期
QString formatsLocale = getFormatsLocale();
QString data;
QString timeGroup;
if(formatsLocale == US_FORMATS) {
data = time12 + " " + timeAMPM + " " + week;
timeGroup = time12 + " " + timeAMPM;
} else {
data = timeAMPM + " " + time12 + " " + week;
timeGroup = timeAMPM + " " + time12;
}
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(data);
bool isWrap = false;
if(fontSize > CALENDAR_BUTTON_WIDTH-5) {
isWrap = true;
}
//拼接 星期 和 年月日
if(QString::compare("cn", m_dataFormat)) {
if(isWrap)
displayText = timeGroup + "\n" + week + " " + mon + "-" + day;
else
displayText = timeGroup + " " + week + "\n" + year + "-" + mon + "-" + day;
} else {
if(isWrap)
displayText = timeGroup + "\n" + week + " " + mon + "/" + day;
else
displayText = timeGroup + " " + week + "\n" + year + "/" + mon + "/" + day;
}
}
CalendarButtonText *m_btnText;
if(formatsLocale == ZH_CN_FORMATS) {
m_btnText = new ZhCnText(m_plugin,this);
} else if(formatsLocale == US_FORMATS) {
m_btnText = new EnUsText(m_plugin,this);
} else {
//字号择优策略,防止调整任务栏大小或者系统增加字号时导致显示不全
QFontMetrics fontMetrics(QFont(m_systemFont, fontSize));
int fontWidth = fontMetrics.width(week);
if(fontWidth > m_plugin->panel()->panelSize()-5) {
int index = systemFonts.indexOf(fontSize);
if(index>0) {
for(int i=index-1; i>=0; i--) {
QFont font(m_systemFont, systemFonts.at(i));
QFontMetrics fontMetrics(font);
int fontWidth = fontMetrics.width(week);
if(fontWidth <= m_plugin->panel()->panelSize()-5) {
this->setFont(QFont(m_systemFont, systemFonts.at(i)));
break;
}
}
} else {
//容错处理,如果系统 org.ukui.style system-font-size 设置错误,也可保证日期正常显示不遮挡
this->setFont(QFont(m_systemFont, systemFonts.first()));
}
} else {
this->setFont(QFont(m_systemFont, fontSize));
}
if(!QString::compare("24", m_timeFormat)) {
displayText = time24 + "\n" + week + "\n";
} else {
displayText = timeAMPM + "\n" + time12 + "\n" + week + "\n";
}
if(QString::compare("cn", m_dataFormat)) {
QString data = year + "-" + mon + "-" + day;
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(data);
if(fontSize > m_plugin->panel()->panelSize()-10) {
data = mon + "-" + day;
}
displayText += data;
} else {
QString data = year + "/" + mon + "/" + day;
QFontMetrics fontMetrics(this->font());
int fontSize = fontMetrics.width(data);
if(fontSize > m_plugin->panel()->panelSize()-10) {
data = mon + "/" + day;
}
displayText += data;
}
m_btnText = new OtherLanguagText(m_plugin,this);
}
//设置按钮文字
displayText = m_btnText->getBtnText();
m_btnText->deleteLater();
this->setText(displayText);
}

View File

@ -32,6 +32,7 @@
#include <QPushButton>
#include "../panel/iukuipanelplugin.h"
#include "../panel/common_fun/listengsettings.h"
#include "calendarbuttontext.h"
class CalendarButton : public QPushButton
@ -53,7 +54,7 @@ Q_SIGNALS:
void pressShowHideCalendar();
private Q_SLOTS:
void setControlTime();
void updateTimeText(QString timerStr);
void updateBtnText(QString timerStr);
private:
IUKUIPanelPlugin * m_plugin;
QWidget *m_parent;

View File

@ -0,0 +1,319 @@
/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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/&gt;.
*
*/
#include "calendarbuttontext.h"
#include <QtDBus/QtDBus>
#define HOUR_SYSTEM_CONTROL "org.ukui.control-center.panel.plugins"
#define DATA_FORMAT "date" //日期格式yyyy/MM/dd、yyyy-MM-dd
#define TIME_FORMAT "hoursystem" //时间格式12小时制、24小时制
#define SYSTEM_FONT_SET "org.ukui.style"
#define SYSTEM_FONT_SIZE "systemFontSize"
#define SYSTEM_FONT "systemFont"
#define SYSTEM_MIN_FONT 10
#define PANEL_SETTINGS "org.ukui.panel.settings"
#define PANEL_POSITION_KEY "panelposition"
#define ICON_SIZE_KEY "iconsize"
#define PANEL_SIZE_KEY "panelsize"
#define CALENDAR_BUTTON_WIDTH 120
#define PANEL_SIZE_LARGE 92
#define PANEL_SIZE_MEDIUM 70
#define PANEL_SIZE_SMALL 46
#define PANEL_SIZE_KEY "panelsize"
#define UKUI_CONTROL_CENTER_SERVER "org.freedesktop.Accounts"
#define UKUI_CONTROL_CENTER_PATH "/org/freedesktop/Accounts/User1000"
#define UKUI_CONTROL_CENTER_INTERFACE "org.freedesktop.Accounts.User"
#define PROPERTIES_NAME "FormatsLocale"
#define US_FORMATS "en_US.UTF-8"
#define ZH_CN_FORMATS "zh_CN.UTF-8"
#define INSIDE_SPAC 6
CalendarButtonText::CalendarButtonText(IUKUIPanelPlugin *plugin, QPushButton *parent):
m_plugin(plugin),
m_parent(parent)
{
m_dataTime = QDateTime::currentDateTime();
const QByteArray controlId(HOUR_SYSTEM_CONTROL);
if (QGSettings::isSchemaInstalled(controlId)) {
m_timeFormat = new QGSettings(controlId);
}
const QByteArray fontId(SYSTEM_FONT_SET);
if (QGSettings::isSchemaInstalled(fontId)) {
m_fontGsettings = new QGSettings(fontId);
}
const QByteArray panelId(PANEL_SETTINGS);
if (QGSettings::isSchemaInstalled(panelId)) {
m_panelGsettings = new QGSettings(panelId);
}
}
CalendarButtonText::~CalendarButtonText()
{
if(m_timeFormat) {
m_timeFormat->deleteLater();
}
if(m_fontGsettings) {
m_fontGsettings->deleteLater();
}
if(m_panelGsettings) {
m_panelGsettings->deleteLater();
}
}
QString CalendarButtonText::getAmPm()
{
QString time = m_dataTime.toString("A hh:mm");
QStringList timeList = time.split(" ", QString::SkipEmptyParts);
return timeList.first();
}
QString CalendarButtonText::getTime()
{
QString time = m_dataTime.toString("A hh:mm");
QStringList timeList = time.split(" ", QString::SkipEmptyParts);
QString time12 = timeList.last();
QString time24 = m_dataTime.toString("hh:mm");
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
return time12;
} else {
return time24;
}
}
QString CalendarButtonText::getWeek()
{
QDBusInterface interface(UKUI_CONTROL_CENTER_SERVER,
UKUI_CONTROL_CENTER_PATH,
UKUI_CONTROL_CENTER_INTERFACE,
QDBusConnection::systemBus());
QString language = interface.property(PROPERTIES_NAME).toString();
if(language == US_FORMATS) {
return m_dataTime.toString("ddd");
} else if(language == ZH_CN_FORMATS) {
return m_dataTime.toString("dddd");
}
}
QString CalendarButtonText::getDate(DateStyle dateStyle)
{
QString year = m_dataTime.toString("yyyy");
QString mon = m_dataTime.toString("MM");
QString day = m_dataTime.toString("dd");
QString spacer = "/";
if(m_timeFormat && m_timeFormat->get(DATA_FORMAT).toString() == "en") {
spacer = "-";
} else {
spacer = "/";
}
if(dateStyle == DateStyle::YEAR_MON_DAY) {
return year + spacer + mon + spacer + day;
}
else {
return mon + spacer + day;
}
}
void CalendarButtonText::updateFontSize(QString btnText)
{
QStringList textList = btnText.split("\n", QString::SkipEmptyParts);
int maxLength;
if(m_plugin->panel()->isHorizontal()) {
if(m_panelGsettings->get(PANEL_SIZE_KEY).toInt() == PANEL_SIZE_SMALL) {
QString systemFont = m_fontGsettings->get(SYSTEM_FONT).toString();
m_parent->setFont(QFont(systemFont, SYSTEM_MIN_FONT));
return;
}
maxLength = CALENDAR_BUTTON_WIDTH;
setOptimalFont(textList, maxLength);
} else {
int m_panelSize = m_panelGsettings->get(PANEL_SIZE_KEY).toInt();
if(m_panelSize >= PANEL_SIZE_SMALL && m_panelSize < PANEL_SIZE_MEDIUM) {
maxLength = PANEL_SIZE_SMALL;
} else if(m_panelSize >= PANEL_SIZE_MEDIUM && m_panelSize < PANEL_SIZE_LARGE) {
maxLength = PANEL_SIZE_MEDIUM;
} else if(m_panelSize == PANEL_SIZE_LARGE) {
maxLength = PANEL_SIZE_LARGE;
} else {
maxLength = PANEL_SIZE_SMALL;
}
setOptimalFont(textList, maxLength);
}
}
//判断最长字体的是否可以显示下,选取最优字体大小显示
void CalendarButtonText::setOptimalFont(QStringList textList, int btnMaxLength)
{
QString systemFont = m_fontGsettings->get(SYSTEM_FONT).toString();
double fontSizeCurrent = m_fontGsettings->get(SYSTEM_FONT_SIZE).toDouble();
QList<double> systemFonts;
systemFonts<<10<<12<<13.5<<15;
//获取最长的行
QFontMetrics fontMetrics(QFont(systemFont, fontSizeCurrent));
int fontWidth = fontMetrics.width(textList.first());
int maxIndex = 0;
for(int i=1; i<textList.size(); i++) {
int width = fontMetrics.width(textList.at(i));
if(width > fontWidth) {
fontWidth = width;
maxIndex = i;
}
}
//判断最长字体的是否可以显示下,选取最优字体
int maxFontWidth = fontMetrics.width(textList.at(maxIndex));
if(maxFontWidth > btnMaxLength-INSIDE_SPAC) {
int index = systemFonts.indexOf(fontSizeCurrent);
if(index>0) {
for(int i=index-1; i>=0; i--) {
QFontMetrics fontMetrics(QFont(systemFont, systemFonts.at(i)));
int width = fontMetrics.width(textList.at(maxIndex));
if(width <= btnMaxLength-INSIDE_SPAC) {
m_parent->setFont(QFont(systemFont, systemFonts.at(i)));
break;
}
}
} else {
//容错处理,如果系统 org.ukui.style system-font-size 设置错误,也可保证日期正常显示不遮挡
m_parent->setFont(QFont(systemFont, systemFonts.first()));
}
} else {
m_parent->setFont(QFont(systemFont, fontSizeCurrent));
}
}
EnUsText::EnUsText(IUKUIPanelPlugin *plugin,QPushButton *parent):
CalendarButtonText(plugin,parent)
{
}
QString EnUsText::getBtnText()
{
QString amPmStr = getAmPm();
QString timeStr = getTime();
QString weekStr = getWeek();
QString dataStr;
QString btnTextStr;
if(m_plugin->panel()->isHorizontal()) {
dataStr = getDate(DateStyle::YEAR_MON_DAY);
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
btnTextStr = timeStr + " " + amPmStr + " " + weekStr + "\n" + dataStr;
} else {
btnTextStr = timeStr + " " + weekStr + "\n" + dataStr;
}
} else {
if(m_panelGsettings->get(PANEL_SIZE_KEY).toInt() < PANEL_SIZE_LARGE) {
dataStr = getDate(DateStyle::MON_DAY);
} else {
dataStr = getDate(DateStyle::YEAR_MON_DAY);
}
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
btnTextStr = timeStr + "\n" + amPmStr + "\n" + weekStr + "\n" + dataStr;
} else {
btnTextStr = timeStr + "\n" + weekStr + "\n" + dataStr;
}
}
updateFontSize(btnTextStr);
return btnTextStr;
}
ZhCnText::ZhCnText(IUKUIPanelPlugin *plugin,QPushButton *parent):
CalendarButtonText(plugin,parent)
{
}
QString ZhCnText::getBtnText()
{
QString amPmStr = getAmPm();
QString timeStr = getTime();
QString weekStr = getWeek();
QString dataStr;
QString btnTextStr;
if(m_plugin->panel()->isHorizontal()) {
dataStr = getDate(DateStyle::YEAR_MON_DAY);
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
btnTextStr = amPmStr + " " + timeStr + " " + weekStr + "\n" + dataStr;
} else {
btnTextStr = timeStr + " " + weekStr + "\n" + dataStr;
}
} else {
if(m_panelGsettings->get(PANEL_SIZE_KEY).toInt() < PANEL_SIZE_LARGE) {
dataStr = getDate(DateStyle::MON_DAY);
} else {
dataStr = getDate(DateStyle::YEAR_MON_DAY);
}
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
btnTextStr = amPmStr + "\n" + timeStr + "\n" + weekStr + "\n" + dataStr;
} else {
btnTextStr = timeStr + "\n" + weekStr + "\n" + dataStr;
}
}
updateFontSize(btnTextStr);
return btnTextStr;
}
OtherLanguagText::OtherLanguagText(IUKUIPanelPlugin *plugin,QPushButton *parent):
CalendarButtonText(plugin,parent)
{
}
QString OtherLanguagText::getBtnText()
{
QString amPmStr = getAmPm();
QString timeStr = getTime();
QString dataStr;
QString btnTextStr;
if(m_plugin->panel()->isHorizontal()) {
dataStr = getDate(DateStyle::YEAR_MON_DAY);
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
btnTextStr = amPmStr + " " + timeStr + "\n" + dataStr;
} else {
btnTextStr = timeStr + "\n" + dataStr;
}
} else {
//任务栏纵向状态下大尺寸以下均不显示year
if(m_panelGsettings->get(PANEL_SIZE_KEY).toInt() < PANEL_SIZE_LARGE) {
dataStr = getDate(DateStyle::MON_DAY);
} else {
dataStr = getDate(DateStyle::YEAR_MON_DAY);
}
if(m_timeFormat && m_timeFormat->get(TIME_FORMAT).toString() == "12") {
btnTextStr = amPmStr + "\n" + timeStr + "\n" + dataStr;
} else {
btnTextStr = timeStr + "\n" + dataStr;
}
}
updateFontSize(btnTextStr);
return btnTextStr;
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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/&gt;.
*
*/
#ifndef CALENDARBUTTONTEXT_H
#define CALENDARBUTTONTEXT_H
#include <QObject>
#include <QDateTime>
#include <QGSettings>
#include <QPushButton>
#include "../panel/iukuipanelplugin.h"
class CalendarButtonText : public QObject
{
Q_OBJECT
public:
CalendarButtonText(IUKUIPanelPlugin *plugin,QPushButton *parent);
~CalendarButtonText();
enum DateStyle{YEAR_MON_DAY=0, MON_DAY};
public:
virtual QString getBtnText() = 0;
QString getAmPm();
QString getTime();
QString getWeek();
QString getDate(DateStyle dateStyle);
void updateFontSize(QString btnText);
void setOptimalFont(QStringList textList, int btnMaxLength);
public:
QDateTime m_dataTime;
QPushButton *m_parent;
IUKUIPanelPlugin * m_plugin;
QGSettings *m_timeFormat;
QGSettings *m_fontGsettings;
QGSettings *m_panelGsettings;
};
class EnUsText : public CalendarButtonText
{
public:
EnUsText(IUKUIPanelPlugin *plugin,QPushButton *parent);
QString getBtnText() override;
};
class ZhCnText : public CalendarButtonText
{
public:
ZhCnText(IUKUIPanelPlugin *plugin,QPushButton *parent);
QString getBtnText() override;
};
class OtherLanguagText : public CalendarButtonText
{
public:
OtherLanguagText(IUKUIPanelPlugin *plugin,QPushButton *parent);
QString getBtnText() override;
};
#endif // CALENDARBUTTONTEXT_H