feat:datetime plugin format changed

This commit is contained in:
shaojun 2019-08-19 14:57:11 +08:00
parent b92759d379
commit 01500100b9
2 changed files with 20 additions and 108 deletions

View File

@ -29,6 +29,7 @@
#include <QMouseEvent>
#define PLUGIN_STATE_KEY "enable"
#define SHOW_DATE_MIN_HEIGHT 45
DatetimeWidget::DatetimeWidget(QWidget *parent)
: QWidget(parent)
@ -44,7 +45,6 @@ void DatetimeWidget::set24HourFormat(const bool value)
m_24HourFormat = value;
m_cachedTime.clear();
update();
if (isVisible()) {
@ -54,18 +54,24 @@ void DatetimeWidget::set24HourFormat(const bool value)
QSize DatetimeWidget::sizeHint() const
{
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
QFontMetrics fm(qApp->font());
QString timeString;
if (m_24HourFormat)
return fm.boundingRect("88:88").size() + QSize(20, 10);
timeString = "88:88";
else
return fm.boundingRect("88:88 A.A.").size() + QSize(20, 20);
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);
}
void DatetimeWidget::resizeEvent(QResizeEvent *e)
{
m_cachedTime.clear();
QWidget::resizeEvent(e);
}
@ -81,109 +87,17 @@ void DatetimeWidget::paintEvent(QPaintEvent *e)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
if (displayMode == Dock::Efficient)
{
QString format;
if (m_24HourFormat)
format = "hh:mm";
else
{
if (position == Dock::Top || position == Dock::Bottom)
format = "hh:mm AP";
else
format = "hh:mm\nAP";
}
QString format;
if (m_24HourFormat)
format = "hh:mm";
else
format = "hh:mm AP";
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, current.time().toString(format));
return;
}
if (displayMode == Dock::Fashion && rect().height() > SHOW_DATE_MIN_HEIGHT)
format += "\nyyyy-MM-dd";
// use language Chinese to fix can not find image resources which will be drawn
const QString currentTimeString = QLocale(QLocale::Chinese, QLocale::system().country())
.toString(current, m_24HourFormat ? "hhmm" : "hhmma");
// check cache valid
if (m_cachedTime != currentTimeString)
{
m_cachedTime = currentTimeString;
// draw new pixmap
m_cachedTime = currentTimeString;
m_cachedIcon = QPixmap(size() * ratio);
m_cachedIcon.fill(Qt::transparent);
m_cachedIcon.setDevicePixelRatio(ratio);
QPainter p(&m_cachedIcon);
// draw fashion mode datetime plugin
const int perfectIconSize = qMin(width(), height()) * 0.8;
const QRect r = rect();
// draw background
QPixmap background = loadSvg(":/icons/resources/icons/background.svg", QSize(perfectIconSize, perfectIconSize));
const QPoint backgroundOffset = r.center() - background.rect().center() / ratio;
p.drawPixmap(backgroundOffset, background);
const int bigNumHeight = perfectIconSize / 2.5;
const int bigNumWidth = double(bigNumHeight) * 8 / 18;
const int smallNumHeight = bigNumHeight / 2;
const int smallNumWidth = double(smallNumHeight) * 5 / 9;
// draw big num 1
const QString bigNum1Path = QString(":/icons/resources/icons/big%1.svg").arg(currentTimeString[0]);
const QPixmap bigNum1 = loadSvg(bigNum1Path, QSize(bigNumWidth, bigNumHeight));
const QPoint bigNum1Offset = backgroundOffset + QPoint(perfectIconSize / 2 - bigNumWidth * 2 + 1, perfectIconSize / 2 - bigNumHeight / 2);
p.drawPixmap(bigNum1Offset, bigNum1);
// draw big num 2
const QString bigNum2Path = QString(":/icons/resources/icons/big%1.svg").arg(currentTimeString[1]);
const QPixmap bigNum2 = loadSvg(bigNum2Path, QSize(bigNumWidth, bigNumHeight));
const QPoint bigNum2Offset = bigNum1Offset + QPoint(bigNumWidth + 1, 0);
p.drawPixmap(bigNum2Offset, bigNum2);
if (!m_24HourFormat)
{
// draw small num 1
const QString smallNum1Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[2]);
const QPixmap smallNum1 = loadSvg(smallNum1Path, QSize(smallNumWidth, smallNumHeight));
const QPoint smallNum1Offset = bigNum2Offset + QPoint(bigNumWidth + 2, 1);
p.drawPixmap(smallNum1Offset, smallNum1);
// draw small num 2
const QString smallNum2Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[3]);
const QPixmap smallNum2 = loadSvg(smallNum2Path, QSize(smallNumWidth, smallNumHeight));
const QPoint smallNum2Offset = smallNum1Offset + QPoint(smallNumWidth + 1, 0);
p.drawPixmap(smallNum2Offset, smallNum2);
// draw am/pm tips
const int tips_width = (smallNumWidth * 2 + 2) & ~0x1;
const int tips_height = tips_width / 2;
QPixmap tips;
if (current.time().hour() > 11)
tips = loadSvg(":/icons/resources/icons/tips-pm.svg", QSize(tips_width, tips_height));
else
tips = loadSvg(":/icons/resources/icons/tips-am.svg", QSize(tips_width, tips_height));
const QPoint tipsOffset = bigNum2Offset + QPoint(bigNumWidth + 2, bigNumHeight - tips_height);
p.drawPixmap(tipsOffset, tips);
} else {
// draw small num 1
const QString smallNum1Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[2]);
const QPixmap smallNum1 = loadSvg(smallNum1Path, QSize(smallNumWidth, smallNumHeight));
const QPoint smallNum1Offset = bigNum2Offset + QPoint(bigNumWidth + 2, smallNumHeight);
p.drawPixmap(smallNum1Offset, smallNum1);
// draw small num 2
const QString smallNum2Path = QString(":/icons/resources/icons/small%1.svg").arg(currentTimeString[3]);
const QPixmap smallNum2 = loadSvg(smallNum2Path, QSize(smallNumWidth, smallNumHeight));
const QPoint smallNum2Offset = smallNum1Offset + QPoint(smallNumWidth + 1, 0);
p.drawPixmap(smallNum2Offset, smallNum2);
}
}
// draw cached fashion mode time item
painter.drawPixmap(rect().center() - m_cachedIcon.rect().center() / ratio, m_cachedIcon);
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, current.toString(format));
}
const QPixmap DatetimeWidget::loadSvg(const QString &fileName, const QSize size)

View File

@ -47,8 +47,6 @@ private:
const QPixmap loadSvg(const QString &fileName, const QSize size);
private:
QPixmap m_cachedIcon;
QString m_cachedTime;
bool m_24HourFormat;
};