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>
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2017-10-23 13:36:16 +08:00
|
|
|
#define PLUGIN_STATE_KEY "enable"
|
2019-08-19 14:57:11 +08:00
|
|
|
#define SHOW_DATE_MIN_HEIGHT 45
|
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
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-11-20 14:45:35 +08:00
|
|
|
if (isVisible()) {
|
|
|
|
emit requestUpdateGeometry();
|
|
|
|
}
|
2016-06-27 14:33:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize DatetimeWidget::sizeHint() const
|
|
|
|
{
|
2019-08-19 14:57:11 +08:00
|
|
|
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
|
|
|
|
|
2016-06-27 14:33:21 +08:00
|
|
|
QFontMetrics fm(qApp->font());
|
|
|
|
|
2019-08-19 14:57:11 +08:00
|
|
|
QString timeString;
|
2017-06-26 15:20:16 +08:00
|
|
|
if (m_24HourFormat)
|
2019-08-19 14:57:11 +08:00
|
|
|
timeString = "88:88";
|
2017-06-26 15:20:16 +08:00
|
|
|
else
|
2019-08-19 14:57:11 +08:00
|
|
|
timeString = "88:88 A.A.";
|
|
|
|
|
|
|
|
if (displayMode == Dock::Fashion && rect().height() > SHOW_DATE_MIN_HEIGHT)
|
|
|
|
timeString += "\n8888-88-88";
|
|
|
|
|
|
|
|
return fm.size(Qt::TextExpandTabs, timeString) + QSize(20, 20);
|
2016-06-27 14:33:21 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:19:38 +08:00
|
|
|
void DatetimeWidget::resizeEvent(QResizeEvent *e)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(e);
|
|
|
|
}
|
|
|
|
|
2016-06-27 14:33:21 +08:00
|
|
|
void DatetimeWidget::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
|
|
|
|
2019-04-23 14:28:09 +08:00
|
|
|
const auto ratio = devicePixelRatioF();
|
2016-06-27 14:33:21 +08:00
|
|
|
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
|
2017-06-26 15:20:16 +08:00
|
|
|
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
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);
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2019-08-19 14:57:11 +08:00
|
|
|
QString format;
|
|
|
|
if (m_24HourFormat)
|
|
|
|
format = "hh:mm";
|
|
|
|
else
|
|
|
|
format = "hh:mm AP";
|
2016-06-27 14:33:21 +08:00
|
|
|
|
2019-08-19 14:57:11 +08:00
|
|
|
if (displayMode == Dock::Fashion && rect().height() > SHOW_DATE_MIN_HEIGHT)
|
|
|
|
format += "\nyyyy-MM-dd";
|
2016-08-19 10:19:38 +08:00
|
|
|
|
2019-08-19 14:57:11 +08:00
|
|
|
painter.setPen(Qt::white);
|
|
|
|
painter.drawText(rect(), Qt::AlignCenter, current.toString(format));
|
2016-06-30 20:44:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const QPixmap DatetimeWidget::loadSvg(const QString &fileName, const QSize size)
|
|
|
|
{
|
2019-04-23 14:28:09 +08:00
|
|
|
const auto ratio = devicePixelRatioF();
|
2017-09-15 11:23:43 +08:00
|
|
|
|
|
|
|
QPixmap pixmap(size * ratio);
|
2016-06-30 20:44:13 +08:00
|
|
|
QSvgRenderer renderer(fileName);
|
|
|
|
pixmap.fill(Qt::transparent);
|
|
|
|
|
|
|
|
QPainter painter;
|
|
|
|
painter.begin(&pixmap);
|
|
|
|
renderer.render(&painter);
|
|
|
|
painter.end();
|
|
|
|
|
2017-09-15 11:23:43 +08:00
|
|
|
pixmap.setDevicePixelRatio(ratio);
|
|
|
|
|
2016-06-30 20:44:13 +08:00
|
|
|
return pixmap;
|
2016-06-27 14:33:21 +08:00
|
|
|
}
|