forked from CareF/deepin-dock-plugin-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforcastwidget.cpp
148 lines (141 loc) · 7 KB
/
forcastwidget.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "forcastwidget.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonParseError>
#include <QFile>
#include <QStandardPaths>
#include <QTimeZone>
ForcastWidget::ForcastWidget(QString thm, QWidget *parent)
: QWidget(parent),
m_settings("deepin", "dde-dock-HTYWeather"),
themeName(thm)
{
setFixedWidth(300);
QGridLayout *layout = new QGridLayout;
for (int i=0; i<6; i++) {
labelWImg[i] = new QLabel;
labelWImg[i]->setPixmap(QPixmap(QString(":/%1/icon/%1/na.png").arg(themeName)).scaled(50,50,Qt::KeepAspectRatio,Qt::SmoothTransformation));
labelWImg[i]->setAlignment(Qt::AlignCenter);
layout->addWidget(labelWImg[i],i,0);
labelTemp[i] = new QLabel("25°C");
labelTemp[i]->setAlignment(Qt::AlignCenter);
layout->addWidget(labelTemp[i],i,1);
if (i==0) {
labelTemp[i]->setStyleSheet("color:white;font-size:20px;");
labelDate[i] = new QLabel("City");
labelDate[i]->setStyleSheet("color:white;font-size:20px;");
} else {
labelTemp[i]->setStyleSheet("color:white;font-size:12px;");
labelDate[i] = new QLabel("01-01 Mon");
labelDate[i]->setStyleSheet("color:white;font-size:12px;");
}
labelDate[i]->setAlignment(Qt::AlignCenter);
layout->addWidget(labelDate[i],i,2);
}
setLayout(layout);
}
void ForcastWidget::updateWeather()
{
QDateTime currentDateTime = QDateTime::currentDateTime();
QString sw = "", stemp = "", stip = "", surl="";
QString log = currentDateTime.toString("yyyy/MM/dd HH:mm:ss") + "\n";
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkReply *reply;
QString city = m_settings.value("city","").toString();
QString country = m_settings.value("country","").toString();
if(city != "" && country != ""){
emit weatherNow("Weather", "Temp", currentDateTime.toString("yyyy/MM/dd HH:mm:ss") + "\nGetting weather of " + city + "," + country, QPixmap(QString(":/%1/icon/%1/na.png").arg(themeName)));
QString appid = "8f3c852b69f0417fac76cd52c894ba63";
surl = "https://api.openweathermap.org/data/2.5/forecast?q=" + city + "," + country + "&appid=" + appid;
reply = manager.get(QNetworkRequest(QUrl(surl)));
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray BA = reply->readAll();
log += surl + "\n";
log += BA + "\n";
QJsonParseError JPE;
QJsonDocument JD = QJsonDocument::fromJson(BA, &JPE);
if (JPE.error == QJsonParseError::NoError) {
QString cod = JD.object().value("cod").toString();
if(cod == "200"){
QJsonObject JO_city = JD.object().value("city").toObject();
QJsonObject coord = JO_city.value("coord").toObject();
double lat = coord.value("lat").toDouble();
double lon = coord.value("lon").toDouble();
m_settings.setValue("lat", lat);
m_settings.setValue("lon", lon);
QJsonArray list = JD.object().value("list").toArray();
int r = 0;
for (int i=0; i<list.size(); i++) {
QDateTime date = QDateTime::fromSecsSinceEpoch(list[i].toObject().value("dt").toInt(), QTimeZone::utc());
QString sdate = date.toString("MM-dd ddd");
QString dt_txt = list[i].toObject().value("dt_txt").toString();
double temp = list[i].toObject().value("main").toObject().value("temp").toDouble() - 273.15;
stemp = QString::number(qRound(temp)) + "°C";
QString humidity = "RH: " + QString::number(list[i].toObject().value("main").toObject().value("humidity").toInt()) + "%";
QString weather = list[i].toObject().value("weather").toArray().at(0).toObject().value("main").toString();
QString sicon = QString(":/%1/icon/%1/").arg(themeName) + list[i].toObject().value("weather").toArray().at(0).toObject().value("icon").toString() + ".png";\
QString wind = "Wind: " + QString::number(list[i].toObject().value("wind").toObject().value("speed").toDouble()) + "m/s, " + QString::number(qRound(list[i].toObject().value("wind").toObject().value("deg").toDouble())) + "°";
log += dt_txt + ", " + date.toString("yyyy-MM-dd HH:mm:ss ddd") + ", " + stemp + ", " + humidity + ","+ weather + ", " + sicon + ", " + wind + "\n";
if(date.time() == QTime(12,0,0)){
if (r == 0) {
QPixmap pixmap(sicon);
labelWImg[0]->setPixmap(pixmap.scaled(80,80,Qt::KeepAspectRatio,Qt::SmoothTransformation));
labelTemp[0]->setText(stemp);
labelDate[0]->setText(JO_city.value("name").toString());
labelWImg[1]->setPixmap(QPixmap(sicon).scaled(50,50,Qt::KeepAspectRatio,Qt::SmoothTransformation));
labelTemp[1]->setText(weather + " " + stemp);
labelDate[1]->setText(sdate);
stip = city + ", " + country + "\n" + weather + "\n" + stemp + "\n" + humidity + "\n" + wind +"\nRefresh:" + currentDateTime.toString("HH:mm:ss");
emit weatherNow(weather, stemp, stip, pixmap);
r++;
} else {
labelWImg[r]->setPixmap(QPixmap(sicon).scaled(50,50,Qt::KeepAspectRatio,Qt::SmoothTransformation));
labelTemp[r]->setText(weather + " " + stemp);
labelDate[r]->setText(sdate);
}
r++;
}
}
} else {
emit weatherNow("Weather", "Temp", cod + "\n" + JD.object().value("message").toString(), QPixmap(QString(":/%1/icon/%1/na.png").arg(themeName)));
}
}else{
emit weatherNow("Weather", "Temp", QString(BA), QPixmap(QString(":/%1/icon/%1/na.png").arg(themeName)));
}
// log
QString path = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).first() + "/HTYWeather.log";
QFile file(path);
if (file.open(QFile::WriteOnly)) {
file.write(log.toUtf8());
file.close();
}
}
}
QString ForcastWidget::translateWeather(QString s)
{
QString sc = "";
if(s == "Atmosphere"){
return "霾";
} else if (s == "Clear") {
return "晴";
} else if (s == "Clouds") {
return "多云";
} else if (s == "Drizzle") {
return "毛毛雨";
} else if (s == "Rain") {
return "雨";
} else if (s == "Snow") {
return "雪";
} else if (s == "Thunderstorm") {
return "雷雨";
}
return sc;
}
void ForcastWidget::setTheme(const QString &theme) {
themeName = theme;
}