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 QMapIterator<QString, QString> i(m_dataValueMap);
53 while (i.hasNext()) {
54 i.next();
55 if (i.key().isEmpty())
56 {
57 LOG(VB_GENERAL, LOG_DEBUG, i.key());
58 ok = false;
59 }
60 }
61
62 return ok;
63}
64
65
66void WeatherScreen::setValue(const QString &key, const QString &value)
67{
68 if (m_dataValueMap.contains(key))
69 m_dataValueMap[key] = prepareDataItem(key, value);
70}
71
72void WeatherScreen::newData(const QString& /*loc*/, units_t /*units*/, DataMap data)
73{
74 DataMap::iterator itr = data.begin();
75 while (itr != data.end())
76 {
77 setValue(itr.key(), *itr);
78 ++itr;
79 }
80
81 // This may seem like overkill, but it is necessary to actually update the
82 // static and animated maps when they are redownloaded on an update
83 if (!prepareScreen())
84 LOG(VB_GENERAL, LOG_ERR, "Theme is missing a required widget!");
85
86 emit screenReady(this);
87}
88
90{
91 if (m_units == ENG_UNITS)
92 return QString::fromUtf8("°") + "F";
93 return QString::fromUtf8("°") + "C";
94}
95
96bool WeatherScreen::prepareScreen(bool checkOnly)
97{
98 QMap<QString, QString>::iterator itr = m_dataValueMap.begin();
99 while (itr != m_dataValueMap.end())
100 {
101 QString name = itr.key();
102 MythUIType *widget = GetChild(name);
103
104 if (!widget)
105 {
106 LOG(VB_GENERAL, LOG_ERR, "Widget not found " + itr.key());
107
108 if (name == "copyright")
109 {
110 LOG(VB_GENERAL, LOG_WARNING,
111 QString("No copyright widget found, skipping screen %1.")
112 .arg(m_name));
113 return false;
114 }
115 if (name == "copyrightlogo")
116 {
117 LOG(VB_GENERAL, LOG_WARNING,
118 QString("No copyrightlogo widget found, skipping screen %1.")
119 .arg(m_name));
120 return false;
121 }
122 }
123
124 if( !widget || checkOnly )
125 {
126 ++itr;
127 continue;
128 }
129
130 if (auto *w2 = dynamic_cast<MythUIText *>(widget))
131 {
132 w2->SetText(itr.value());
133 }
134 else if (auto *w3 = dynamic_cast<MythUIImage *>(widget))
135 {
136 w3->SetFilename(itr.value());
137 w3->Load();
138 }
139
140 prepareWidget(widget);
141 ++itr;
142 }
143
144 m_prepared = true;
145 return true;
146}
147
149{
150 /*
151 * Basically so we don't do it twice since some screens (Static Map) mess
152 * with image dimensions
153 */
154 auto *img = dynamic_cast<MythUIImage *>(widget);
155 if (img != nullptr)
156 {
157 img->Load();
158 }
159}
160
161QString WeatherScreen::formatDataItem(const QString &key, const QString &value)
162{
163 if (key.startsWith("relative_humidity") || key.startsWith("pop"))
164 return value + " %";
165
166 if (key == "pressure")
167 return value + (m_units == ENG_UNITS ? " in" : " mb");
168
169 if (key == "visibility")
170 return value + (m_units == ENG_UNITS ? " mi" : " km");
171
172 if (key.startsWith("temp") ||
173 key.startsWith("appt") ||
174 key.startsWith("low") ||
175 key.startsWith("high"))
176 {
177 if ((value == "NA") || (value == "N/A"))
178 return {};
179 return value + getTemperatureUnit();
180 }
181
182 if (key.startsWith("wind_gust") ||
183 key.startsWith("wind_spdgst") ||
184 key.startsWith("wind_speed"))
185 return value + (m_units == ENG_UNITS ? " mph" : " km/h");
186
187 /*The days of the week will be translated if the script sends elements from
188 the enum DaysOfWeek.*/
189 if (key.startsWith("date"))
190 {
191 bool isNumber = false;
192 (void)value.toInt( &isNumber);
193
194 if (isNumber)
195 {
196 int dayOfWeek = value.toInt();
197
198 switch (dayOfWeek)
199 {
200 case DAY_SUNDAY :
201 return tr("Sunday");
202 break;
203 case DAY_MONDAY :
204 return tr("Monday");
205 break;
206 case DAY_TUESDAY :
207 return tr("Tuesday");
208 break;
209 case DAY_WENDESDAY :
210 return tr("Wednesday");
211 break;
212 case DAY_THURSDAY :
213 return tr("Thursday");
214 break;
215 case DAY_FRIDAY :
216 return tr("Friday");
217 break;
218 case DAY_SATURDAY :
219 return tr("Saturday");
220 break;
221 }
222 }
223 }
224
225 if (key == "copyrightlogo" && value == "none")
226 return {};
227
228 return value;
229}
230
231QString WeatherScreen::prepareDataItem(const QString &key, const QString &value)
232{
233 return formatDataItem(key, value);
234}
235
236bool WeatherScreen::keyPressEvent(QKeyEvent *event)
237{
238 return GetFocusWidget() && GetFocusWidget()->keyPressEvent(event);
239}
240
241/*
242 * vim:ts=4:sw=4:ai:et:si:sts=4
243 */
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:86
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:138
virtual bool keyPressEvent(QKeyEvent *event)
Key event handler.
Definition: mythuitype.cpp:989
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