-
Notifications
You must be signed in to change notification settings - Fork 24
/
weatherwidget.cpp
67 lines (60 loc) · 2.02 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
#include "dde-dock/constants.h"
#include "weatherwidget.h"
#include <QApplication>
#include <QPainter>
#include <QMouseEvent>
#define PLUGIN_STATE_KEY "enable"
WeatherWidget::WeatherWidget(QWidget *parent)
: QWidget(parent),
m_settings("deepin", "dde-dock-HTYWeather")
{
sw = "Weather";
temp = "Temp";
pixmap = QPixmap(":icon/Default/na.png");
}
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{
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
if (position == Dock::Top || position == Dock::Bottom)
size = QSize(height(), height());
else
size = QSize(width(), width());
}
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);
if (displayMode == Dock::Efficient) {
painter.setPen(Qt::white);
painter.drawText(rect(), Qt::AlignCenter, sw + "\n" + temp);
} else {
QPixmap pixmap1 = pixmap.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
painter.drawPixmap(rect().center() - pixmap1.rect().center(), pixmap1);
}
}