63fcbf75435cd0ce2ffd40928fad95b083e274b4
1 #include "forecastdatawidget.h"
3 #include "forecastdata.h"
4 #include "utilities.h"
5 #include <QHBoxLayout>
6 #include <QLabel>
7 #include <QResizeEvent>
8 #include <QTimer>
10 #include <QtDebug>
12 #define FORECAST_DAYS 3
14 /***********************************************************************************************************
15 * ForecastDataWidget
16 * Widget that groups together the rows of forecast data for easy display in a grid layout. Handles display
17 * of a ForecastData list in a QGridLayout
18 ************************************************************************************************************/
20 ForecastDataWidget::ForecastDataWidget(QWidget *parent) :
21 QWidget(parent)
22 {
23 m_globalSettings = GlobalSettings::getInstance();
24 widgetLayout = new QGridLayout ;
26 for(int a = 0;a<FORECAST_DAYS;a++)
27 {
28 m_forecastDataWidgetGroup[a].highTemp = new QLabel("");
29 m_forecastDataWidgetGroup[a].lowTemp = new QLabel("");
30 m_forecastDataWidgetGroup[a].day = new QLabel("");
31 m_forecastDataWidgetGroup[a].iconHolderLabel= new QLabel();
32 m_forecastDataWidgetGroup[a].iconPixmap = new QPixmap();
33 //m_forecastDataWidgetGroup[a].iconHolderLabel->setPixmap(*m_forecastDataWidgetGroup[a].iconPixmap);
35 m_forecastDataWidgetGroup[a].day->setMargin(0);
37 m_forecastDataWidgetGroup[a].highTemp->setMargin(0);
38 m_forecastDataWidgetGroup[a].lowTemp->setMargin(0);
39 m_forecastDataWidgetGroup[a].iconHolderLabel->setMargin(0);
41 widgetLayout->addWidget(m_forecastDataWidgetGroup[a].day, a, 0);
43 widgetLayout->addWidget(m_forecastDataWidgetGroup[a].iconHolderLabel, a, 1);
44 widgetLayout->addWidget(m_forecastDataWidgetGroup[a].lowTemp, a, 2);
46 widgetLayout->addWidget(m_forecastDataWidgetGroup[a].highTemp, a, 3);
48 }
50 widgetLayout->setHorizontalSpacing(0);
51 widgetLayout->setVerticalSpacing(0);
52 widgetLayout->setMargin(0);
55 widgetLayout->setContentsMargins(0,0,0,0);
57 QHBoxLayout* l = new QHBoxLayout;
58 l->setSpacing(0);
59 l->setContentsMargins(0,0,0,0);
60 l->addLayout(widgetLayout);
62 setStyleSheet("color:#ffffff;background-color: none;padding:0px;spacing:0px;");
63 setLayout(l);
64 QTimer::singleShot(0, this, SLOT(scaleContents()));
66 }
68 //FUNCTION: setData
69 //
70 // A setter function that gives the widget the data list obtained from the web
71 //
73 void ForecastDataWidget::setData(QList<ForecastData *> data)
74 {
75 m_data = data;
76 }
78 //FUNCTION::updateData()
79 //
80 // Slot that updates all displayed values. Useful for unit changes, and invokes scale contents afterwards
81 void ForecastDataWidget::updateData()
82 {
83 for(int a = 0;a<FORECAST_DAYS;a++)
84 {
85 m_forecastDataWidgetGroup[a].highTemp->setText(formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat()));
86 m_forecastDataWidgetGroup[a].lowTemp->setText(formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat()));
87 m_forecastDataWidgetGroup[a].day->setText(m_data[a]->day());
88 m_forecastDataWidgetGroup[a].iconPixmap->load(iconNameToPixmap(m_data[a]->icon()));
89 }
90 QTimer::singleShot(0, this, SLOT(scaleContents()));
91 }
93 //FUNCTION: scaleContents
94 //
95 // Slot that scales the size of every widget contained within the layout to a satisfactory size based on available space.
96 //
98 void ForecastDataWidget::scaleContents()
99 {
100 //scale factor is calculated by inverting the number of rows times a scale factor (for some breathing room) and then multiplying by
101 //the total height of the widget (giving each row that proportion of the total space)
102 int scaleFactor =(1/(FORECAST_DAYS*1.1))*this->height();
104 for(int a = 0;a<FORECAST_DAYS;a++)
105 {
106 m_forecastDataWidgetGroup[a].highTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;");
107 m_forecastDataWidgetGroup[a].lowTemp->setStyleSheet("font-size:"+QString::number((int)(0.50*scaleFactor))+"px;");
108 m_forecastDataWidgetGroup[a].day->setStyleSheet("font-size:"+QString::number((int)(.50*scaleFactor))+"px;");
109 m_forecastDataWidgetGroup[a].iconHolderLabel->setPixmap(m_forecastDataWidgetGroup[a].iconPixmap->scaledToHeight(.9*scaleFactor));
110 }
112 }
114 //FUNCTION: iconNameToPixmap
115 //
116 // Helper function that converts the weather related icon name given from the web to a relevant resource
117 //
119 QString ForecastDataWidget::iconNameToPixmap(QString icon)
120 {
122 if (icon == "partlysunny" || icon == "mostlycloudy" ) {
123 return ":Images/weather-few-clouds.png";
124 }
125 else if (icon == "fog") {
126 return ":Images/weather-fog.png";
127 }
128 else if (icon == "hazy") {
129 return ":Images/weather-haze.png";
130 }
131 else if (icon == "cloudy") {
132 return ":Images/weather-overcast.png";
133 }
134 else if (icon == "rain" || icon == "chancerain") {
135 return ":Images/weather-showers.png";
136 }
137 else if (icon == "sleet" || icon == "chancesleet") {
138 return ":Images/weather-sleet.png";
139 }
140 else if (icon == "flurries" || icon == "snow" ||
141 icon == "chanceflurries" || icon == "chancesnow") {
142 return ":Images/weather-snow.png";
143 }
144 else if (icon == "clear" || icon == "sunny") {
145 return ":Images/weather-sunny.png";
146 }
147 else if (icon == "mostlysunny" || icon == "partlycloudy" ||
148 icon == "unknown") {
149 return ":Images/weather-sunny-very-few-clouds.png";
150 }
151 else if (icon == "tstorms" || icon == "chancetstorms") {
152 return ":Images/weather-thundershower.png";
153 }
154 else
155 return":Images/weather-sunny-very-few-clouds.png";
157 }
159 QVariantHash ForecastDataWidget::getCurrentData()
160 {
161 QVariantHash data;
162 if(m_data.size()>0)
163 {
164 for(int a = 0;a<m_data.size();a++)
165 {
166 ForecastData* forecastData = m_data.at(a);
167 QVariantHash forecastDataHash;
168 forecastDataHash.insert("day", forecastData->day());
169 forecastDataHash.insert("high", formatTemperatureString(m_data[a]->highTemp(), m_globalSettings->temperatureFormat()));
170 forecastDataHash.insert("low", formatTemperatureString(m_data[a]->lowTemp(), m_globalSettings->temperatureFormat()));
171 forecastDataHash.insert("image", iconNameToPixmap(forecastData->icon()).mid(1));
172 data.insert("day"+QString::number(a), forecastDataHash);
173 }
174 }
175 return data;
176 }