From 77eae531b7633fc94ee968723493a776ea1b07c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=81=82=E6=88=90?= Date: Thu, 30 Jul 2020 17:41:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=94=B9=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=A0=8F=E7=95=8C=E9=9D=A2=E7=9A=84=E6=97=B6=E9=97=B4=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 任务栏界面时间格式根据控制中心设置来显示 Log: 更改任务栏界面的时间显示格式 Task: https://pms.uniontech.com/zentao/task-view-30918.html Change-Id: I35c47685c2efbe8f12afdc9a2bb5bb59f4b5324e Reviewed-on: http://gerrit.uniontech.com/c/dde-dock/+/932 Reviewed-by: Reviewed-by: niecheng Reviewed-by: wangwei Tested-by: --- .gerrit_sources_list | 1 - plugins/datetime/CMakeLists.txt | 8 ++- plugins/datetime/datetimewidget.cpp | 81 ++++++++++++++++++++++------- plugins/datetime/datetimewidget.h | 11 ++++ 4 files changed, 79 insertions(+), 22 deletions(-) delete mode 100644 .gerrit_sources_list diff --git a/.gerrit_sources_list b/.gerrit_sources_list deleted file mode 100644 index 89c292cee..000000000 --- a/.gerrit_sources_list +++ /dev/null @@ -1 +0,0 @@ -deb [trusted=yes] http://shuttle.corp.deepin.com/cache/repos/eagle-sp3/release-candidate/RERF5byA5Y-R5rWL6K-VMTE5OQ unstable main contrib diff --git a/plugins/datetime/CMakeLists.txt b/plugins/datetime/CMakeLists.txt index 346652900..405744489 100644 --- a/plugins/datetime/CMakeLists.txt +++ b/plugins/datetime/CMakeLists.txt @@ -11,16 +11,22 @@ find_package(Qt5Widgets REQUIRED) find_package(Qt5Svg REQUIRED) find_package(Qt5DBus REQUIRED) find_package(DtkWidget REQUIRED) +pkg_check_modules(DFrameworkDBus REQUIRED dframeworkdbus) add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN") add_library(${PLUGIN_NAME} SHARED ${SRCS} datetime.qrc) set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../) -target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} ../../interfaces) +target_include_directories(${PLUGIN_NAME} PUBLIC + ${DtkWidget_INCLUDE_DIRS} + ${DFrameworkDBus_INCLUDE_DIRS} + ../../interfaces ${Qt5DBus_INCLUDE_DIRS} + ) target_link_libraries(${PLUGIN_NAME} PRIVATE ${Qt5DBus_LIBRARIES} ${DtkWidget_LIBRARIES} ${Qt5Widgets_LIBRARIES} ${Qt5Svg_LIBRARIES} + ${DFrameworkDBus_INCLUDE_DIRS} ) install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins) diff --git a/plugins/datetime/datetimewidget.cpp b/plugins/datetime/datetimewidget.cpp index d046e8e27..5e99f412c 100644 --- a/plugins/datetime/datetimewidget.cpp +++ b/plugins/datetime/datetimewidget.cpp @@ -40,8 +40,16 @@ DatetimeWidget::DatetimeWidget(QWidget *parent) : QWidget(parent) , m_24HourFormat(false) , 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") { setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE); + setShortDateFormat(m_timedateInter->shortDateFormat()); + setShortTimeFormat(m_timedateInter->shortTimeFormat()); + + connect(m_timedateInter, &Timedate::ShortDateFormatChanged, this, &DatetimeWidget::setShortDateFormat); + connect(m_timedateInter, &Timedate::ShortTimeFormatChanged, this, &DatetimeWidget::setShortTimeFormat); } void DatetimeWidget::set24HourFormat(const bool value) @@ -59,6 +67,43 @@ void DatetimeWidget::set24HourFormat(const bool value) } } +/** + * @brief DatetimeWidget::setShortDateFormat 根据类型设置时间显示格式 + * @param type 自定义类型 + */ +void DatetimeWidget::setShortDateFormat(int type) +{ + switch (type) { + case 0: m_shortDateFormat = "yyyy/M/d"; break; + case 1: m_shortDateFormat = "yyyy-M-d"; break; + case 2: m_shortDateFormat = "yyyy.M.d"; break; + case 3: m_shortDateFormat = "yyyy/MM/dd"; break; + case 4: m_shortDateFormat = "yyyy-MM-dd"; break; + case 5: m_shortDateFormat = "yyyy.MM.dd"; break; + case 6: m_shortDateFormat = "yy/M/d"; break; + case 7: m_shortDateFormat = "yy-M-d"; break; + case 8: m_shortDateFormat = "yy.M.d"; break; + default: m_shortDateFormat = "yyyy-MM-dd"; break; + } + update(); +} + +/** + * @brief DatetimeWidget::setShortTimeFormat 根据类型设置短时间显示格式 + * @param type 自定义类型 + */ +void DatetimeWidget::setShortTimeFormat(int type) +{ + switch (type) { + case 0: m_shortTimeFormat = "h:m"; break; + case 1: m_shortTimeFormat = "hh:mm"; break; + case 2: m_shortTimeFormat = "h:m"; break; + case 3: m_shortTimeFormat = "hh:mm"; break; + default: m_shortTimeFormat = "hh:mm"; break; + } + update(); +} + QSize DatetimeWidget::curTimeSize() const { const Dock::Position position = qApp->property(PROP_POSITION).value(); @@ -66,21 +111,19 @@ QSize DatetimeWidget::curTimeSize() const m_timeFont = TIME_FONT; m_dateFont = DATE_FONT; QFontMetrics fm(m_timeFont); - QString format; - if (m_24HourFormat) - format = "hh:mm"; - else { + QString format = m_shortTimeFormat; + if (!m_24HourFormat) { if (position == Dock::Top || position == Dock::Bottom) - format = "hh:mm AP"; + format = format.append(" AP"); else - format = "hh:mm\nAP"; + format = format.append("\nAP"); } QString timeString = QDateTime::currentDateTime().toString(format); QSize timeSize = fm.boundingRect(timeString).size(); if (timeString.contains("\n")) { - QStringList SL = timeString.split("\n"); - timeSize = QSize(fm.boundingRect(SL.at(0)).width(), fm.boundingRect(SL.at(0)).height() + fm.boundingRect(SL.at(1)).height()); + QStringList SL = timeString.split("\n"); + timeSize = QSize(fm.boundingRect(SL.at(0)).width(), fm.boundingRect(SL.at(0)).height() + fm.boundingRect(SL.at(1)).height()); } QSize dateSize = QFontMetrics(m_dateFont).boundingRect("0000/00/00").size(); @@ -89,7 +132,7 @@ QSize DatetimeWidget::curTimeSize() const while (QFontMetrics(m_timeFont).boundingRect(timeString).size().height() + QFontMetrics(m_dateFont).boundingRect("0000/00/00").size().height() > height()) { m_timeFont.setPixelSize(m_timeFont.pixelSize() - 1); timeSize.setWidth(QFontMetrics(m_timeFont).boundingRect(timeString).size().width()); - if (m_timeFont.pixelSize() - m_dateFont.pixelSize() == 1){ + if (m_timeFont.pixelSize() - m_dateFont.pixelSize() == 1) { m_dateFont.setPixelSize(m_dateFont.pixelSize() - 1); dateSize.setWidth(QFontMetrics(m_dateFont).boundingRect("0000/00/00").size().width()); } @@ -103,7 +146,7 @@ QSize DatetimeWidget::curTimeSize() const } else { timeSize.setHeight(QFontMetrics(m_timeFont).boundingRect(timeString).size().height() * 2); } - if (m_timeFont.pixelSize() - m_dateFont.pixelSize() == 1){ + if (m_timeFont.pixelSize() - m_dateFont.pixelSize() == 1) { m_dateFont.setPixelSize(m_dateFont.pixelSize() - 1); dateSize.setWidth(QFontMetrics(m_dateFont).boundingRect("0000/00/00").size().height()); } @@ -128,14 +171,12 @@ void DatetimeWidget::paintEvent(QPaintEvent *e) QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - QString format; - if (m_24HourFormat) - format = "hh:mm"; - else { + QString format = m_shortTimeFormat; + if (!m_24HourFormat) { if (position == Dock::Top || position == Dock::Bottom) - format = "hh:mm AP"; + format = format.append(" AP"); else - format = "hh:mm\nAP"; + format = format.append("\nAP"); } painter.setFont(m_timeFont); @@ -144,15 +185,15 @@ void DatetimeWidget::paintEvent(QPaintEvent *e) QRect timeRect = rect(); QRect dateRect = rect(); - if (position == Dock::Top || position == Dock::Bottom){ - timeRect.setBottom(rect().center().y() + 6); - dateRect.setTop(timeRect.bottom() - 4); + if (position == Dock::Top || position == Dock::Bottom) { + timeRect.setBottom(rect().center().y() + 6); + dateRect.setTop(timeRect.bottom() - 4); } else { timeRect.setBottom(rect().center().y() + m_timeOffset); dateRect.setTop(timeRect.bottom()); } painter.drawText(timeRect, Qt::AlignBottom | Qt::AlignHCenter, current.toString(format)); - format = "yyyy/MM/dd"; + format = m_shortDateFormat; painter.setFont(m_dateFont); painter.drawText(dateRect, Qt::AlignTop | Qt::AlignHCenter, current.toString(format)); } diff --git a/plugins/datetime/datetimewidget.h b/plugins/datetime/datetimewidget.h index 2a9ed4d4f..8903ed9f1 100644 --- a/plugins/datetime/datetimewidget.h +++ b/plugins/datetime/datetimewidget.h @@ -22,8 +22,12 @@ #ifndef DATETIMEWIDGET_H #define DATETIMEWIDGET_H +#include + #include +using Timedate = com::deepin::daemon::Timedate; + class DatetimeWidget : public QWidget { Q_OBJECT @@ -40,6 +44,10 @@ signals: public slots: void set24HourFormat(const bool value); +private Q_SLOTS: + void setShortDateFormat(int type); + void setShortTimeFormat(int type); + private: void paintEvent(QPaintEvent *e); QSize curTimeSize() const; @@ -49,6 +57,9 @@ private: mutable QFont m_timeFont; mutable QFont m_dateFont; mutable int m_timeOffset; + Timedate *m_timedateInter; + QString m_shortDateFormat; + QString m_shortTimeFormat; };