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