2017-09-18 14:33:44 +08:00
|
|
|
/*
|
2018-02-07 11:52:47 +08:00
|
|
|
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
2017-09-18 14:33:44 +08:00
|
|
|
*
|
|
|
|
* Author: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* Maintainer: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-06-27 14:33:21 +08:00
|
|
|
#include "datetimewidget.h"
|
|
|
|
#include "constants.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
2016-06-30 20:44:13 +08:00
|
|
|
#include <QSvgRenderer>
|
2016-08-15 09:26:45 +08:00
|
|
|
#include <QMouseEvent>
|
2019-09-03 09:37:24 +08:00
|
|
|
#include <DFontSizeManager>
|
2019-09-17 13:14:23 +08:00
|
|
|
#include <DGuiApplicationHelper>
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2017-10-23 13:36:16 +08:00
|
|
|
#define PLUGIN_STATE_KEY "enable"
|
2019-11-19 11:26:11 +08:00
|
|
|
#define SHOW_DATE_MIN_HEIGHT 40
|
2019-09-12 17:04:53 +08:00
|
|
|
#define TIME_FONT DFontSizeManager::instance()->t4()
|
2019-09-03 09:37:24 +08:00
|
|
|
#define DATE_FONT DFontSizeManager::instance()->t10()
|
|
|
|
|
|
|
|
DWIDGET_USE_NAMESPACE
|
2017-10-23 13:36:16 +08:00
|
|
|
|
2016-06-27 14:33:21 +08:00
|
|
|
DatetimeWidget::DatetimeWidget(QWidget *parent)
|
2018-11-20 14:45:35 +08:00
|
|
|
: QWidget(parent)
|
2017-10-23 13:36:16 +08:00
|
|
|
{
|
2019-09-03 09:37:24 +08:00
|
|
|
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();
|
2017-10-23 13:36:16 +08:00
|
|
|
|
2019-09-03 09:37:24 +08:00
|
|
|
m_timeOffset = (timeHeight - dateHeight) / 2;
|
2019-09-06 14:44:07 +08:00
|
|
|
|
|
|
|
setMinimumSize(PLUGIN_BACKGROUND_MIN_SIZE, PLUGIN_BACKGROUND_MIN_SIZE);
|
2017-10-23 13:36:16 +08:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:45:35 +08:00
|
|
|
void DatetimeWidget::set24HourFormat(const bool value)
|
2016-06-27 14:33:21 +08:00
|
|
|
{
|
2018-11-20 14:45:35 +08:00
|
|
|
if (m_24HourFormat == value) {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2018-11-20 14:45:35 +08:00
|
|
|
m_24HourFormat = value;
|
2017-03-30 16:48:34 +08:00
|
|
|
update();
|
2017-06-26 15:20:16 +08:00
|
|
|
|
2019-11-06 16:22:19 +08:00
|
|
|
adjustSize();
|
2018-11-20 14:45:35 +08:00
|
|
|
if (isVisible()) {
|
|
|
|
emit requestUpdateGeometry();
|
|
|
|
}
|
2016-06-27 14:33:21 +08:00
|
|
|
}
|
|
|
|
|
2019-09-10 15:58:34 +08:00
|
|
|
QSize DatetimeWidget::curTimeSize() const
|
2016-08-19 10:19:38 +08:00
|
|
|
{
|
2019-09-06 14:44:07 +08:00
|
|
|
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
2019-09-12 17:04:53 +08:00
|
|
|
|
|
|
|
m_timeFont = TIME_FONT;
|
2020-01-13 17:59:55 +08:00
|
|
|
m_dateFont = DATE_FONT;
|
2019-09-12 17:04:53 +08:00
|
|
|
QFontMetrics fm(m_timeFont);
|
2019-09-06 14:44:07 +08:00
|
|
|
QString format;
|
|
|
|
if (m_24HourFormat)
|
|
|
|
format = "hh:mm";
|
2019-11-05 16:44:17 +08:00
|
|
|
else {
|
|
|
|
if (position == Dock::Top || position == Dock::Bottom)
|
|
|
|
format = "hh:mm AP";
|
|
|
|
else
|
|
|
|
format = "hh:mm\nAP";
|
|
|
|
}
|
2019-09-06 14:44:07 +08:00
|
|
|
|
2019-11-05 16:44:17 +08:00
|
|
|
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());
|
|
|
|
} else {
|
2020-01-13 17:59:55 +08:00
|
|
|
QSize dateSize = QFontMetrics(m_dateFont).boundingRect("0000/00/00").size();
|
|
|
|
if (timeSize.width() < dateSize.width() && rect().height() >= SHOW_DATE_MIN_HEIGHT)
|
2019-11-05 16:44:17 +08:00
|
|
|
timeSize.setWidth(dateSize.width());
|
|
|
|
}
|
2019-09-06 14:44:07 +08:00
|
|
|
|
|
|
|
if (position == Dock::Bottom || position == Dock::Top) {
|
2020-01-13 17:59:55 +08:00
|
|
|
if (rect().height() >= SHOW_DATE_MIN_HEIGHT) {
|
|
|
|
QStringList SL = timeString.split("\n");
|
|
|
|
int date_dec=m_timeFont.pixelSize()-m_dateFont.pixelSize();
|
|
|
|
while (QFontMetrics(m_timeFont).boundingRect(timeString).size().height() + 11 > height()) {
|
|
|
|
m_timeFont.setPixelSize(m_timeFont.pixelSize() - 1);
|
|
|
|
m_dateFont.setPixelSize(m_timeFont.pixelSize() - date_dec);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (QFontMetrics(m_timeFont).boundingRect(timeString).size().height() > height() + 10) {
|
|
|
|
m_timeFont.setPixelSize(m_timeFont.pixelSize() - 1);
|
|
|
|
}
|
|
|
|
timeSize.setWidth(QFontMetrics(m_timeFont).boundingRect(timeString).size().width());
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:22:19 +08:00
|
|
|
return QSize(timeSize.width(), height());
|
2019-09-06 14:44:07 +08:00
|
|
|
} else {
|
2019-11-05 16:44:17 +08:00
|
|
|
if (width() < timeSize.width()) {
|
|
|
|
if (timeString.contains("\n")) {
|
|
|
|
QStringList SL = timeString.split("\n");
|
|
|
|
while (QFontMetrics(m_timeFont).boundingRect(SL.at(0)).size().width() > width()) {
|
|
|
|
m_timeFont.setPixelSize(m_timeFont.pixelSize() - 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (QFontMetrics(m_timeFont).boundingRect(timeString).size().width() > width()) {
|
|
|
|
m_timeFont.setPixelSize(m_timeFont.pixelSize() - 1);
|
|
|
|
}
|
2019-09-12 17:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int timeHeight = QFontMetrics(m_timeFont).boundingRect(timeString).size().height();
|
2019-11-05 16:44:17 +08:00
|
|
|
if (format.contains("\n")) {
|
|
|
|
QStringList SL = format.split("\n");
|
|
|
|
timeHeight = QFontMetrics(m_timeFont).boundingRect(SL.at(0)).size().height() + QFontMetrics(m_timeFont).boundingRect(SL.at(1)).size().height();
|
|
|
|
}
|
2019-09-12 17:04:53 +08:00
|
|
|
|
2019-11-06 16:22:19 +08:00
|
|
|
return QSize(width(), std::max(timeHeight, PLUGIN_BACKGROUND_MIN_SIZE));
|
2019-09-12 17:04:53 +08:00
|
|
|
|
|
|
|
} else {
|
2019-11-06 16:22:19 +08:00
|
|
|
return QSize(width(), std::max(timeSize.height(), SHOW_DATE_MIN_HEIGHT));
|
2019-09-12 17:04:53 +08:00
|
|
|
}
|
2019-09-06 14:44:07 +08:00
|
|
|
}
|
2019-09-10 15:58:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize DatetimeWidget::sizeHint() const
|
|
|
|
{
|
|
|
|
return curTimeSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DatetimeWidget::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
2019-09-11 16:28:21 +08:00
|
|
|
setMaximumSize(curTimeSize() + QSize(1, 1));
|
2019-09-06 14:44:07 +08:00
|
|
|
|
2016-08-19 10:19:38 +08:00
|
|
|
QWidget::resizeEvent(e);
|
|
|
|
}
|
|
|
|
|
2016-06-27 14:33:21 +08:00
|
|
|
void DatetimeWidget::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2016-06-27 15:34:54 +08:00
|
|
|
const QDateTime current = QDateTime::currentDateTime();
|
2016-06-27 14:33:21 +08:00
|
|
|
|
|
|
|
QPainter painter(this);
|
2017-09-15 11:23:43 +08:00
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
2019-11-05 16:44:17 +08:00
|
|
|
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2019-08-19 14:57:11 +08:00
|
|
|
QString format;
|
|
|
|
if (m_24HourFormat)
|
|
|
|
format = "hh:mm";
|
2019-11-05 16:44:17 +08:00
|
|
|
else {
|
|
|
|
if (position == Dock::Top || position == Dock::Bottom)
|
|
|
|
format = "hh:mm AP";
|
|
|
|
else
|
|
|
|
format = "hh:mm\nAP";
|
|
|
|
}
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2019-09-12 17:04:53 +08:00
|
|
|
painter.setFont(m_timeFont);
|
2019-09-26 16:35:36 +08:00
|
|
|
painter.setPen(QPen(palette().brightText(), 1));
|
2019-09-03 09:37:24 +08:00
|
|
|
|
2019-09-11 16:28:21 +08:00
|
|
|
if (rect().height() >= SHOW_DATE_MIN_HEIGHT) {
|
2020-01-13 17:59:55 +08:00
|
|
|
|
2019-09-03 09:37:24 +08:00
|
|
|
QRect timeRect = rect();
|
|
|
|
timeRect.setBottom(rect().center().y() + m_timeOffset);
|
|
|
|
|
2019-11-05 16:44:17 +08:00
|
|
|
if (position == Dock::Top || position == Dock::Bottom) {
|
|
|
|
painter.drawText(timeRect, Qt::AlignBottom | Qt::AlignHCenter, current.toString(format));
|
2019-09-03 09:37:24 +08:00
|
|
|
|
2019-11-05 16:44:17 +08:00
|
|
|
QRect dateRect = rect();
|
|
|
|
dateRect.setTop(timeRect.bottom());
|
|
|
|
format = "yyyy/MM/dd";
|
2020-01-13 17:59:55 +08:00
|
|
|
painter.setFont(m_dateFont);
|
2019-11-05 16:44:17 +08:00
|
|
|
painter.drawText(dateRect, Qt::AlignTop | Qt::AlignHCenter, current.toString(format));
|
|
|
|
} else {
|
|
|
|
painter.drawText(rect(), Qt::AlignVCenter | Qt::AlignHCenter, current.toString(format));
|
|
|
|
}
|
2019-09-03 09:37:24 +08:00
|
|
|
} else {
|
|
|
|
painter.drawText(rect(), Qt::AlignCenter, current.toString(format));
|
|
|
|
}
|
2016-06-30 20:44:13 +08:00
|
|
|
}
|