MythTV  0.28pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
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->title),
21  m_units(SI_UNITS),
22  m_screenDefn(screenDefn),
23  m_name(m_screenDefn->name),
24  m_inuse(false),
25  m_prepared(false),
26  m_id(id)
27 {
28 
29  QStringList types = m_screenDefn->dataTypes;
30 
31  for (int i = 0; i < types.size(); ++i)
32  {
33  m_dataValueMap[types.at(i)] = "";
34  }
35 }
36 
38 {
39 }
40 
42 {
43 
44  bool foundtheme = false;
45 
46  // Load the theme for this screen
47  foundtheme = LoadWindowFromXML("weather-ui.xml", m_name, this);
48 
49  if (!foundtheme)
50  return false;
51 
52  if (!prepareScreen(true))
53  return false;
54 
55  return true;
56 }
57 
59 {
60  if (!inUse())
61  return false;
62 
63  bool ok = true;
64  QMapIterator<QString, QString> i(m_dataValueMap);
65  while (i.hasNext()) {
66  i.next();
67  if (i.key().isEmpty())
68  {
69  LOG(VB_GENERAL, LOG_DEBUG, i.key());
70  ok = false;
71  }
72  }
73 
74  return ok;
75 }
76 
77 
78 void WeatherScreen::setValue(const QString &key, const QString &value)
79 {
80  if (m_dataValueMap.contains(key))
81  m_dataValueMap[key] = prepareDataItem(key, value);
82 }
83 
84 void WeatherScreen::newData(QString loc, units_t units, DataMap data)
85 {
86  (void) loc;
87  (void) units;
88 
89  DataMap::iterator itr = data.begin();
90  while (itr != data.end())
91  {
92  setValue(itr.key(), *itr);
93  ++itr;
94  }
95 
96  // This may seem like overkill, but it is necessary to actually update the
97  // static and animated maps when they are redownloaded on an update
98  if (!prepareScreen())
99  LOG(VB_GENERAL, LOG_ERR, "Theme is missing a required widget!");
100 
101  emit screenReady(this);
102 }
103 
105 {
106  if (m_units == ENG_UNITS)
107  return QString::fromUtf8("°") + "F";
108  else
109  return QString::fromUtf8("°") + "C";
110 }
111 
112 bool WeatherScreen::prepareScreen(bool checkOnly)
113 {
114  QMap<QString, QString>::iterator itr = m_dataValueMap.begin();
115  while (itr != m_dataValueMap.end())
116  {
117  QString name = itr.key();
118  MythUIType *widget = GetChild(name);
119 
120  if (!widget)
121  {
122  LOG(VB_GENERAL, LOG_ERR, "Widget not found " + itr.key());
123 
124  if (name == "copyright")
125  {
126  LOG(VB_GENERAL, LOG_WARNING,
127  QString("No copyright widget found, skipping screen %1.")
128  .arg(m_name));
129  return false;
130  }
131  else if (name == "copyrightlogo")
132  {
133  LOG(VB_GENERAL, LOG_WARNING,
134  QString("No copyrightlogo widget found, skipping screen %1.")
135  .arg(m_name));
136  return false;
137  }
138  }
139 
140  if( !widget || checkOnly )
141  {
142  ++itr;
143  continue;
144  }
145 
146  if (dynamic_cast<MythUIText *>(widget))
147  {
148  ((MythUIText *) widget)->SetText(itr.value());
149  }
150  else if (dynamic_cast<MythUIImage *>(widget))
151  {
152  ((MythUIImage *) widget)->SetFilename(itr.value());
153  ((MythUIImage *) widget)->Load();
154  }
155 
156  prepareWidget(widget);
157  ++itr;
158  }
159 
160  m_prepared = true;
161  return true;
162 }
163 
165 {
166  MythUIImage *img;
167  /*
168  * Basically so we don't do it twice since some screens (Static Map) mess
169  * with image dimensions
170  */
171  if ((img = dynamic_cast<MythUIImage *>(widget)))
172  {
173  img->Load();
174  }
175 }
176 
177 QString WeatherScreen::formatDataItem(const QString &key, const QString &value)
178 {
179  if (key.startsWith("relative_humidity") || key.startsWith("pop"))
180  return value + " %";
181 
182  if (key == "pressure")
183  return value + (m_units == ENG_UNITS ? " in" : " mb");
184 
185  if (key == "visibility")
186  return value + (m_units == ENG_UNITS ? " mi" : " km");
187 
188  if (key.startsWith("temp") ||
189  key.startsWith("appt") ||
190  key.startsWith("low") ||
191  key.startsWith("high"))
192  {
193  if ((value == "NA") || (value == "N/A"))
194  return QString();
195  else
196  return value + getTemperatureUnit();
197  }
198 
199  if (key.startsWith("wind_gust") ||
200  key.startsWith("wind_spdgst") ||
201  key.startsWith("wind_speed"))
202  return value + (m_units == ENG_UNITS ? " mph" : " km/h");
203 
204  /*The days of the week will be translated if the script sends elements from
205  the enum DaysOfWeek.*/
206  if (key.startsWith("date"))
207  {
208  bool isNumber;
209  value.toInt( &isNumber);
210 
211  if (isNumber)
212  {
213  int dayOfWeek = value.toInt();
214 
215  switch (dayOfWeek)
216  {
217  case DAY_SUNDAY :
218  return tr("Sunday");
219  break;
220  case DAY_MONDAY :
221  return tr("Monday");
222  break;
223  case DAY_TUESDAY :
224  return tr("Tuesday");
225  break;
226  case DAY_WENDESDAY :
227  return tr("Wednesday");
228  break;
229  case DAY_THURSDAY :
230  return tr("Thursday");
231  break;
232  case DAY_FRIDAY :
233  return tr("Friday");
234  break;
235  case DAY_SATURDAY :
236  return tr("Saturday");
237  break;
238  }
239  }
240  }
241 
242  if (key == "copyrightlogo" && value == "none")
243  return QString();
244 
245  return value;
246 }
247 
248 QString WeatherScreen::prepareDataItem(const QString &key, const QString &value)
249 {
250  return formatDataItem(key, value);
251 }
252 
253 bool WeatherScreen::keyPressEvent(QKeyEvent *event)
254 {
255  if (GetFocusWidget() && GetFocusWidget()->keyPressEvent(event))
256  return true;
257 
258  return false;
259 }
260 
261 /*
262  * vim:ts=4:sw=4:ai:et:si:sts=4
263  */
MYTH_GLsizeiptr const GLvoid * data
virtual QString getTemperatureUnit()
typedef void(__LZO_CDECL *lzo_free_func_t)(lzo_callback_p self
my types
Definition: envcan.pl:35
All purpose text widget, displays a text string.
Definition: mythuitext.h:27
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:88
QMap< QString, QString > m_dataValueMap
Definition: weatherScreen.h:70
virtual QString prepareDataItem(const QString &key, const QString &value)
QStringList dataTypes
Definition: weatherUtils.h:94
void setValue(const QString &key, const QString &value)
AllMusic * parent
static WeatherScreen * loadScreen(MythScreenStack *parent, ScreenListInfo *screenDefn, int id)
units_t m_units
Definition: weatherScreen.h:58
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:83
QString formatDataItem(const QString &key, const QString &value)
The base class on which all widgets and screens are based.
Definition: mythuitype.h:70
bool Load(bool allowLoadInBackground=true, bool forceStat=false)
Load the image(s), wraps ImageLoader::LoadImage()
virtual bool prepareScreen(bool checkOnly=false)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:143
bool keyPressEvent(QKeyEvent *)
Key event handler.
const char * name
Definition: ParseText.cpp:338
MythUIType * GetFocusWidget(void) const
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
void screenReady(WeatherScreen *)
virtual void newData(QString loc, units_t units, DataMap data)
unsigned char units_t
Definition: weatherUtils.h:20
virtual bool canShowScreen()
virtual void prepareWidget(MythUIType *widget)
ScreenListInfo * m_screenDefn
Definition: weatherScreen.h:59
QMap< QString, QString > DataMap
Definition: weatherUtils.h:23
bool Create(void)
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