/* * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. * * Author: sbw * * Maintainer: sbw * * 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 of the License, or * 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 . */ #include "datetimewidget.h" #include "constants.h" #include #include #include #include #include #include #define PLUGIN_STATE_KEY "enable" #define SHOW_DATE_MIN_HEIGHT 45 #define TIME_FONT DFontSizeManager::instance()->t5() #define DATE_FONT DFontSizeManager::instance()->t10() DWIDGET_USE_NAMESPACE DatetimeWidget::DatetimeWidget(QWidget *parent) : QWidget(parent) { QFontMetrics fm_time(TIME_FONT); int timeHeight = fm_time.boundingRect("88:88").height(); QFontMetrics fm_date(DATE_FONT); int dateHeight = fm_date.boundingRect("8888/88/88").height(); m_timeOffset = (timeHeight - dateHeight) / 2; } void DatetimeWidget::set24HourFormat(const bool value) { if (m_24HourFormat == value) { return; } m_24HourFormat = value; update(); if (isVisible()) { emit requestUpdateGeometry(); } } QSize DatetimeWidget::sizeHint() const { QFontMetrics fm(qApp->font()); if (m_24HourFormat) return fm.boundingRect("8888/88/88").size() + QSize(30, 10); else return fm.boundingRect("88:88 A.A.").size() + QSize(30, 20); } void DatetimeWidget::resizeEvent(QResizeEvent *e) { QWidget::resizeEvent(e); } void DatetimeWidget::paintEvent(QPaintEvent *e) { Q_UNUSED(e); const auto ratio = devicePixelRatioF(); const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value(); const Dock::Position position = qApp->property(PROP_POSITION).value(); const QDateTime current = QDateTime::currentDateTime(); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QString format; if (m_24HourFormat) format = "hh:mm"; else format = "hh:mm AP"; painter.setPen(Qt::white); if (rect().height() > SHOW_DATE_MIN_HEIGHT) { QRect timeRect = rect(); timeRect.setBottom(rect().center().y() + m_timeOffset); painter.setFont(TIME_FONT); painter.drawText(timeRect, Qt::AlignBottom | Qt::AlignHCenter, current.toString(format)); QRect dateRect = rect(); dateRect.setTop(timeRect.bottom()); format = "yyyy/MM/dd"; painter.setFont(DATE_FONT); painter.drawText(dateRect, Qt::AlignTop | Qt::AlignHCenter, current.toString(format)); } else { painter.drawText(rect(), Qt::AlignCenter, current.toString(format)); } } const QPixmap DatetimeWidget::loadSvg(const QString &fileName, const QSize size) { const auto ratio = devicePixelRatioF(); QPixmap pixmap(size * ratio); QSvgRenderer renderer(fileName); pixmap.fill(Qt::transparent); QPainter painter; painter.begin(&pixmap); renderer.render(&painter); painter.end(); pixmap.setDevicePixelRatio(ratio); return pixmap; }