MythTV  master
weatherScreen.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <vector>
3 
4 // MythTV headers
5 #include <mythcontext.h>
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 : qAsConst(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 
66 void WeatherScreen::setValue(const QString &key, const QString &value)
67 {
68  if (m_dataValueMap.contains(key))
69  m_dataValueMap[key] = prepareDataItem(key, value);
70 }
71 
72 void 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 
96 bool 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 
161 QString 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 QString();
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 QString();
227 
228  return value;
229 }
230 
231 QString WeatherScreen::prepareDataItem(const QString &key, const QString &value)
232 {
233  return formatDataItem(key, value);
234 }
235 
236 bool 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  */
MythUIType::keyPressEvent
virtual bool keyPressEvent(QKeyEvent *event)
Key event handler.
Definition: mythuitype.cpp:993
MythUIImage
Image widget, displays a single image or multiple images in sequence.
Definition: mythuiimage.h:97
WeatherScreen::inUse
bool inUse() const
Definition: weatherScreen.h:47
DAY_TUESDAY
@ DAY_TUESDAY
Definition: weatherScreen.h:19
WeatherScreen::loadScreen
static WeatherScreen * loadScreen(MythScreenStack *parent, ScreenListInfo *screenDefn, int id)
Definition: weatherScreen.cpp:11
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:131
MythUIImage::Load
bool Load(bool allowLoadInBackground=true, bool forceStat=false)
Load the image(s), wraps ImageLoader::LoadImage()
Definition: mythuiimage.cpp:968
WeatherScreen::setValue
void setValue(const QString &key, const QString &value)
Definition: weatherScreen.cpp:66
MythScreenStack
Definition: mythscreenstack.h:16
types
static const struct wl_interface * types[]
Definition: idle_inhibit_unstable_v1.c:39
DAY_WENDESDAY
@ DAY_WENDESDAY
Definition: weatherScreen.h:19
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:44
weather.h
DAY_SUNDAY
@ DAY_SUNDAY
Definition: weatherScreen.h:19
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:112
WeatherScreen::newData
virtual void newData(const QString &, units_t, DataMap data)
Definition: weatherScreen.cpp:72
DAY_FRIDAY
@ DAY_FRIDAY
Definition: weatherScreen.h:20
DAY_THURSDAY
@ DAY_THURSDAY
Definition: weatherScreen.h:19
DataMap
QMap< QString, QString > DataMap
Definition: weatherUtils.h:25
WeatherScreen::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: weatherScreen.cpp:236
WeatherScreen::m_units
units_t m_units
Definition: weatherScreen.h:58
WeatherScreen::WeatherScreen
WeatherScreen(MythScreenStack *parent, ScreenListInfo *screenDefn, int id)
Definition: weatherScreen.cpp:17
ENG_UNITS
#define ENG_UNITS
Definition: weatherUtils.h:18
ScreenListInfo
Definition: weatherUtils.h:47
WeatherScreen
Weather screen.
Definition: weatherScreen.h:26
WeatherScreen::prepareScreen
virtual bool prepareScreen(bool checkOnly=false)
Definition: weatherScreen.cpp:96
units_t
unsigned char units_t
Definition: weatherUtils.h:24
WeatherScreen::canShowScreen
virtual bool canShowScreen()
Definition: weatherScreen.cpp:46
MythUIType
The base class on which all widgets and screens are based.
Definition: mythuitype.h:84
MythUIText
All purpose text widget, displays a text string.
Definition: mythuitext.h:30
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
WeatherScreen::prepareDataItem
virtual QString prepareDataItem(const QString &key, const QString &value)
Definition: weatherScreen.cpp:231
ScreenListInfo::m_dataTypes
QStringList m_dataTypes
Definition: weatherUtils.h:59
WeatherScreen::m_prepared
bool m_prepared
Definition: weatherScreen.h:73
mythcontext.h
WeatherScreen::getTemperatureUnit
virtual QString getTemperatureUnit()
Definition: weatherScreen.cpp:89
WeatherScreen::m_name
QString m_name
Definition: weatherScreen.h:60
WeatherScreen::m_screenDefn
ScreenListInfo * m_screenDefn
Definition: weatherScreen.h:59
WeatherScreen::screenReady
void screenReady(WeatherScreen *)
weatherScreen.h
WeatherScreen::Create
bool Create(void) override
Definition: weatherScreen.cpp:33
WeatherScreen::formatDataItem
QString formatDataItem(const QString &key, const QString &value)
Definition: weatherScreen.cpp:161
WeatherScreen::m_dataValueMap
QMap< QString, QString > m_dataValueMap
Definition: weatherScreen.h:70
DAY_SATURDAY
@ DAY_SATURDAY
Definition: weatherScreen.h:20
DAY_MONDAY
@ DAY_MONDAY
Definition: weatherScreen.h:19
WeatherScreen::prepareWidget
virtual void prepareWidget(MythUIType *widget)
Definition: weatherScreen.cpp:148