forked from CareF/deepin-dock-plugin-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherwidget.cpp
81 lines (71 loc) · 2.25 KB
/
weatherwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "dde-dock/constants.h"
#include "weatherwidget.h"
#include <QApplication>
#include <QPainter>
#include <QDebug>
#include <QSvgRenderer>
#include <QMouseEvent>
#define PLUGIN_STATE_KEY "enable"
WeatherWidget::WeatherWidget(const ForcastWidget *wgt, QWidget *parent)
: QWidget(parent),
m_settings("deepin", "dde-dock-HTYWeather"),
forcastwidget(wgt)
{
sw = "Weather";
temp = "Temp";
pixmap = QPixmap(QString(":/%1/icon/%1/na.png").arg(forcastwidget->theme()));
}
bool WeatherWidget::enabled()
{
return m_settings.value(PLUGIN_STATE_KEY, true).toBool();
}
void WeatherWidget::setEnabled(const bool b)
{
m_settings.setValue(PLUGIN_STATE_KEY, b);
}
QSize WeatherWidget::sizeHint() const
{
QFontMetrics FM(qApp->font());
QSize size;
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
if (displayMode == Dock::Efficient) {
if(FM.boundingRect(sw).width() >= FM.boundingRect(temp).width()){
size = FM.boundingRect(sw).size() + QSize(10,FM.boundingRect(sw).height());
}else{
size = FM.boundingRect(temp).size() + QSize(10,FM.boundingRect(temp).height());
}
}else{
size = QPixmap(QString(":/%1/icon/%1/na.png").arg(forcastwidget->theme())).size();
}
return size;
}
void WeatherWidget::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
}
void WeatherWidget::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value<Dock::DisplayMode>();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.setPen(Qt::white);
if (displayMode == Dock::Efficient) {
painter.drawText(rect(), Qt::AlignCenter, sw + "\n" + temp);
} else {
painter.drawPixmap(rect(), pixmap);
}
}
void WeatherWidget::mousePressEvent(QMouseEvent *e)
{
if (e->button() != Qt::RightButton)
return QWidget::mousePressEvent(e);
const QPoint p(e->pos() - rect().center());
if (p.manhattanLength() < std::min(width(), height()) * 0.8 * 0.5)
{
emit requestContextMenu();
return;
}
QWidget::mousePressEvent(e);
}