MythTV master
weatherScreen.cpp
Go to the documentation of this file.
1// C++ headers
2#include <vector>
3
4// MythTV headers
6
7// MythWeather headers
8#include "weather.h"
9#include "weatherScreen.h"
10
12 ScreenListInfo *screenDefn, int id)
13{
14 return new WeatherScreen(parent, screenDefn, id);
15}
16
18 ScreenListInfo *screenDefn, int id) :
19 MythScreenType (parent, screenDefn->m_title),
20 m_screenDefn(screenDefn),
21 m_name(m_screenDefn->m_name),
22 m_id(id)
23{
24
25 QStringList types = m_screenDefn->m_dataTypes;
26
27 for (const QString& type : std::as_const(types))
28 {
29 m_dataValueMap[type] = "";
30 }
31}
32
34{
35 // Load the theme for this screen
36 bool foundtheme = LoadWindowFromXML("weather-ui.xml", m_name, this);
37 if (!foundtheme)
38 return false;
39
40 if (!prepareScreen(true))
41 return false;
42
43 return true;
44}
45
47{
48 if (!inUse())
49 return false;
50
51 bool ok = true;
52 for (auto i = m_dataValueMap.cbegin(); i != m_dataValueMap.cend(); ++i)
53 {
54 if (i.key().isEmpty())
55 {
56 LOG(VB_GENERAL, LOG_DEBUG, i.key());
57 ok = false;
58 }
59 }
60
61 return ok;
62}
63
64
65void WeatherScreen::setValue(const QString &key, const QString &value)
66{
67 if (m_dataValueMap.contains(key))
68 m_dataValueMap[key] = prepareDataItem(key, value);
69}
70
71void WeatherScreen::newData(const QString& /*loc*/, units_t /*units*/, DataMap data)
72{
73 DataMap::iterator itr = data.begin();
74 while (itr != data.end())
75 {
76 setValue(itr.key(), *itr);
77 ++itr;
78 }
79
80 // This may seem like overkill, but it is necessary to actually update the
81 // static and animated maps when they are redownloaded on an update
82 if (!prepareScreen())
83 LOG(VB_GENERAL, LOG_ERR, "Theme is missing a required widget!");
84
85 emit screenReady(this);
86}
87
89{
90 if (m_units == ENG_UNITS)
91 return QString::fromUtf8("°") + "F";
92 return QString::fromUtf8("°") + "C";
93}
94
95bool WeatherScreen::prepareScreen(bool checkOnly)
96{
97 QMap<QString, QString>::iterator itr = m_dataValueMap.begin();
98 while (itr != m_dataValueMap.end())
99 {
100 QString name = itr.key();
101 MythUIType *widget = GetChild(name);
102
103 if (!widget)
104 {
105 LOG(VB_GENERAL, LOG_ERR, "Widget not found " + itr.key());
106
107 if (name == "copyright")
108 {
109 LOG(VB_GENERAL, LOG_WARNING,
110 QString("No copyright widget found, skipping screen %1.")
111 .arg(m_name));
112 return false;
113 }
114 if (name == "copyrightlogo")
115 {
116 LOG(VB_GENERAL, LOG_WARNING,
117 QString("No copyrightlogo widget found, skipping screen %1.")
118 .arg(m_name));
119 return false;
120 }
121 }
122
123 if( !widget || checkOnly )
124 {
125 ++itr;
126 continue;
127 }
128
129 if (auto *w2 = dynamic_cast<MythUIText *>(widget))
130 {
131 w2->SetText(itr.value());
132 }
133 else if (auto *w3 = dynamic_cast<MythUIImage *>(widget))
134 {
135 w3->SetFilename(itr.value());
136 w3->Load();
137 }
138
139 prepareWidget(widget);
140 ++itr;
141 }
142
143 m_prepared = true;
144 return true;
145}
146
148{
149 /*
150 * Basically so we don't do it twice since some screens (Static Map) mess
151 * with image dimensions
152 */
153 auto *img = dynamic_cast<MythUIImage *>(widget);
154 if (img != nullptr)
155 {
156 img->Load();
157 }
158}
159
160QString WeatherScreen::formatDataItem(const QString &key, const QString &value)
161{
162 if (key.startsWith("relative_humidity") || key.startsWith("pop"))
163 return value + " %";
164
165 if (key == "pressure")
166 return value + (m_units == ENG_UNITS ? " in" : " mb");
167
168 if (key == "visibility")
169 return value + (m_units == ENG_UNITS ? " mi" : " km");
170
171 if (key.startsWith("temp") ||
172 key.startsWith("appt") ||
173 key.startsWith("low") ||
174 key.startsWith("high"))
175 {
176 if ((value == "NA") || (value == "N/A"))
177 return {};
178 return value + getTemperatureUnit();
179 }
180
181 if (key.startsWith("wind_gust") ||
182 key.startsWith("wind_spdgst") ||
183 key.startsWith("wind_speed"))
184 return value + (m_units == ENG_UNITS ? " mph" : " km/h");
185
186 /*The days of the week will be translated if the script sends elements from
187 the enum DaysOfWeek.*/
188 if (key.startsWith("date"))
189 {
190 bool isNumber = false;
191 (void)value.toInt( &isNumber);
192
193 if (isNumber)
194 {
195 int dayOfWeek = value.toInt();
196
197 switch (dayOfWeek)
198 {
199 case DAY_SUNDAY :
200 return tr("Sunday");
201 break;
202 case DAY_MONDAY :
203 return tr("Monday");
204 break;
205 case DAY_TUESDAY :
206 return tr("Tuesday");
207 break;
208 case DAY_WENDESDAY :
209 return tr("Wednesday");
210 break;
211 case DAY_THURSDAY :
212 return tr("Thursday");
213 break;
214 case DAY_FRIDAY :
215 return tr("Friday");
216 break;
217 case DAY_SATURDAY :
218 return tr("Saturday");
219 break;
220 }
221 }
222 }
223
224 if (key == "copyrightlogo" && value == "none")
225 return {};
226
227 return value;
228}
229
230QString WeatherScreen::prepareDataItem(const QString &key, const QString &value)
231{
232 return formatDataItem(key, value);
233}
234
235bool WeatherScreen::keyPressEvent(QKeyEvent *event)
236{
237 return GetFocusWidget() && GetFocusWidget()->keyPressEvent(event);
238}
239
240/*
241 * vim:ts=4:sw=4:ai:et:si:sts=4
242 */
Screen in which all other widgets are contained and rendered.
MythUIType * GetFocusWidget(void) const
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:98
bool Load(bool allowLoadInBackground=true, bool forceStat=false)
Load the image(s), wraps ImageLoader::LoadImage()
All purpose text widget, displays a text string.
Definition: mythuitext.h:29
The base class on which all widgets and screens are based.
Definition: mythuitype.h:97
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:130
virtual bool keyPressEvent(QKeyEvent *event)
Key event handler.
Definition: mythuitype.cpp:975
QStringList m_dataTypes
Definition: weatherUtils.h:57
Weather screen.
Definition: weatherScreen.h:27
virtual void prepareWidget(MythUIType *widget)
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
bool inUse() const
Definition: weatherScreen.h:47
ScreenListInfo * m_screenDefn
Definition: weatherScreen.h:59
QMap< QString, QString > m_dataValueMap
Definition: weatherScreen.h:70
units_t m_units
Definition: weatherScreen.h:58
void setValue(const QString &key, const QString &value)
WeatherScreen(MythScreenStack *parent, ScreenListInfo *screenDefn, int id)
virtual QString getTemperatureUnit()
virtual bool canShowScreen()
static WeatherScreen * loadScreen(MythScreenStack *parent, ScreenListInfo *screenDefn, int id)
QString formatDataItem(const QString &key, const QString &value)
virtual bool prepareScreen(bool checkOnly=false)
virtual QString prepareDataItem(const QString &key, const QString &value)
QString m_name
Definition: weatherScreen.h:60
void screenReady(WeatherScreen *)
bool Create(void) override
virtual void newData(const QString &, units_t, DataMap data)
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
static const struct wl_interface * types[]
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
@ DAY_FRIDAY
Definition: weatherScreen.h:20
@ DAY_WENDESDAY
Definition: weatherScreen.h:19
@ DAY_SATURDAY
Definition: weatherScreen.h:20
@ DAY_THURSDAY
Definition: weatherScreen.h:19
@ DAY_MONDAY
Definition: weatherScreen.h:19
@ DAY_SUNDAY
Definition: weatherScreen.h:19
@ DAY_TUESDAY
Definition: weatherScreen.h:19
static constexpr uint8_t ENG_UNITS
Definition: weatherUtils.h:19
QMap< QString, QString > DataMap
Definition: weatherUtils.h:26
unsigned char units_t
Definition: weatherUtils.h:25