1 #include "weatherdatawidget.h"
3 #include <QtGui>
4 #include "globalsettings.h"
5 #include "weatherdata.h"
6 #include "utilities.h"
8 WeatherDataWidget::WeatherDataWidget(QWidget *parent) :
9 QWidget(parent)
10 {
11 m_data=NULL;
12 m_globalSettings = GlobalSettings::getInstance();
14 m_currentTemp = new QLabel();
15 m_currentCity = new QLabel();
16 m_currentCity->setWordWrap(true);
17 m_currentIconHolderLabel = new QLabel();
18 m_currentIconHolderLabel->setScaledContents(true);
19 m_currentIconPixmap = new QPixmap();
21 // create current layout
22 QVBoxLayout *currentVLayout = new QVBoxLayout;
23 currentVLayout->addWidget(m_currentTemp);
24 currentVLayout->addWidget(m_currentCity);
26 // add icon to current layout
27 QHBoxLayout *currentHLayout = new QHBoxLayout;
28 currentHLayout->addWidget(m_currentIconHolderLabel);
29 currentHLayout->setAlignment(m_currentIconHolderLabel, Qt::AlignCenter);
30 currentHLayout->setContentsMargins(0,0,0,0);
31 currentHLayout->addLayout(currentVLayout);
33 setStyleSheet("color:#ffffff;background: 0;padding:0px;");
34 setLayout(currentHLayout);
35 QTimer::singleShot(0, this, SLOT(scaleContents()));
36 }
39 void WeatherDataWidget::setData(WeatherData *data)
40 {
41 m_data = data;
42 }
44 void WeatherDataWidget::updateData()
45 {
46 m_currentTemp->setText(formatTemperatureString(m_data->currentTemp(), m_globalSettings->temperatureFormat()));
47 m_currentCity->setText(m_data->currentCity());
48 m_currentIconPixmap->load(iconNameToPixmap(m_data->icon()));
49 QTimer::singleShot(0, this, SLOT(scaleContents()));
50 }
52 void WeatherDataWidget::scaleContents()
53 {
54 int scaleFactor = this->height();
55 m_currentTemp->setStyleSheet("font-size:"+QString::number((int)(.48*scaleFactor))+"px;");
56 m_currentCity->setStyleSheet("font-size:"+QString::number((int)(.16*scaleFactor))+"px;");
57 m_currentIconHolderLabel->setPixmap(m_currentIconPixmap->scaledToHeight(.9*scaleFactor));
58 }
60 QString WeatherDataWidget::iconNameToPixmap(QString icon)
61 {
62 QString pixmap;
63 // set icon for current weather conditions
64 if (icon == "partlysunny" || icon == "mostlycloudy" ) {
65 if(m_data->localTime().time().hour() < 5 || m_data->localTime().time().hour() > 20) {
66 pixmap = (":Images/weather-moony-few-clouds.png");
67 } else {
68 pixmap = (":Images/weather-few-clouds.png");
69 }
70 }
71 else if (icon == "fog") {
72 pixmap = (":Images/weather-fog.png");
73 }
74 else if (icon == "hazy") {
75 if(m_data->localTime().time().hour() < 5 || m_data->localTime().time().hour() > 20) {
76 pixmap = (":Images/weather-moony.png");
77 } else {
78 pixmap = (":Images/weather-haze.png");
79 }
80 }
81 else if (icon == "cloudy") {
82 pixmap = (":Images/weather-overcast.png");
83 }
84 else if (icon == "rain" || icon == "chancerain") {
85 pixmap = (":Images/weather-showers.png");
86 }
87 else if (icon == "sleet" || icon == "chancesleet") {
88 pixmap = (":Images/weather-sleet.png");
89 }
90 else if (icon == "flurries" || icon == "snow" ||
91 icon == "chanceflurries" || icon == "chancesnow") {
92 pixmap = (":Images/weather-snow.png");
93 }
94 else if (icon == "clear" || icon == "sunny") {
95 if(m_data->localTime().time().hour() < 5 || m_data->localTime().time().hour() > 20) {
96 pixmap = (":Images/weather-moony.png");
97 } else {
98 pixmap = (":Images/weather-sunny.png");
99 }
100 }
101 else if (icon == "mostlysunny" || icon == "partlycloudy" ||
102 icon == "unknown") {
103 if(m_data->localTime().time().hour() < 5 || m_data->localTime().time().hour() > 20) {
104 pixmap = (":Images/weather-moony-very-few-clouds");
106 } else {
107 pixmap = (":Images/weather-sunny-very-few-clouds.png");
108 }
109 }
110 else if (icon == "tstorms" || icon == "chancetstorms") {
111 pixmap = (":Images/weather-thundershower.png");
112 }
114 return pixmap;
115 }
117 QVariantHash WeatherDataWidget::getCurrentData()
118 {
119 QVariantHash data;
120 if(m_data)
121 {
122 data.insert("currentTemp", formatTemperatureString(m_data->currentTemp(), m_globalSettings->temperatureFormat()));
123 data.insert("currentCity", m_data->currentCity());
124 data.insert("currentImage", iconNameToPixmap(m_data->icon()).mid(1));
125 }
126 return data;
127 }