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