MythTV  master
weather.cpp
Go to the documentation of this file.
1 
2 // C headers
3 #include <cstdlib>
4 #include <unistd.h>
5 
6 // QT headers
7 #include <QApplication>
8 
9 // MythTV headers
10 #include <libmyth/mythcontext.h>
11 #include <libmythbase/mythdate.h>
12 #include <libmythbase/mythdb.h>
13 #include <libmythui/mythuitext.h>
14 
15 // MythWeather headers
16 #include "sourceManager.h"
17 #include "weather.h"
18 #include "weatherScreen.h"
19 #include "weatherSetup.h"
20 
21 Weather::Weather(MythScreenStack *parent, const QString &name, SourceManager *srcMan)
22  : MythScreenType(parent, name),
23  m_weatherStack(new MythScreenStack(GetMythMainWindow(), "weather stack")),
24  m_nextpageInterval(gCoreContext->GetDurSetting<std::chrono::seconds>("weatherTimeout", 10s)),
25  m_nextPageTimer(new QTimer(this))
26 {
27  if (!srcMan)
28  {
29  m_srcMan = new SourceManager();
30  // No point in doing this as the very first thing we are going to do
31  // is destroy the sources and reload them.
32 #if 0
34  m_srcMan->doUpdate();
35 #endif
36  m_createdSrcMan = true;
37  }
38  else
39  {
40  m_srcMan = srcMan;
41  m_createdSrcMan = false;
42  }
43 
45 
47 }
48 
50 {
51  if (m_createdSrcMan)
52  delete m_srcMan;
53 
54  clearScreens();
55 
56  if (m_weatherStack)
58 }
59 
61 {
62  // Load the theme for this screen
63  bool foundtheme = LoadWindowFromXML("weather-ui.xml", "weatherbase", this);
64  if (!foundtheme)
65  {
66  LOG(VB_GENERAL, LOG_ERR, "Missing required window - weatherbase.");
67  return false;
68  }
69 
70  bool err = false;
71 
72  UIUtilE::Assign(this, m_pauseText, "pause_text", &err);
73  UIUtilE::Assign(this, m_headerText, "header", &err);
74  UIUtilE::Assign(this, m_updatedText, "update_text", &err);
75 
76  if (err)
77  {
78  LOG(VB_GENERAL, LOG_ERR,
79  "Window weatherbase is missing required elements.");
80  return false;
81  }
82 
83  if (m_pauseText)
84  {
85  m_pauseText->SetText(tr("Paused"));
86  m_pauseText->Hide();
87  }
88 
89  return true;
90 }
91 
93 {
94  m_currScreen = nullptr;
95 
96  m_curScreenNum = 0;
97  while (!m_screens.empty())
98  {
99  WeatherScreen *screen = m_screens.back();
100  m_weatherStack->PopScreen(screen, false, false);
101  m_screens.pop_back();
102  delete screen;
103  }
104 }
105 
107 {
108  SetupScreens();
109 }
110 
112 {
113  // Delete any existing screens
114  clearScreens();
115 
116  m_paused = false;
117  m_pauseText->Hide();
118 
119  // Refresh sources
123 
125  QString query =
126  "SELECT screen_id, container, units, draworder FROM weatherscreens "
127  " WHERE hostname = :HOST ORDER BY draworder;";
128  db.prepare(query);
129  db.bindValue(":HOST", gCoreContext->GetHostName());
130  if (!db.exec())
131  {
132  MythDB::DBError("Selecting weather screens.", db);
133  return false;
134  }
135 
136  if (!db.size())
137  {
138  if (m_firstSetup)
139  {
140  m_firstSetup = false;
141  // If no screens exist, run the setup
143 
144  auto *ssetup = new ScreenSetup(mainStack, "weatherscreensetup",
145  m_srcMan);
146 
147  connect(ssetup, &MythScreenType::Exiting, this, &Weather::setupScreens);
148 
149  if (ssetup->Create())
150  {
151  mainStack->AddScreen(ssetup);
152  }
153  else
154  {
155  delete ssetup;
156  }
157  }
158  else
159  {
160  Close();
161  }
162  }
163  else
164  {
165  while (db.next())
166  {
167  int id = db.value(0).toInt();
168  QString container = db.value(1).toString();
169  units_t units = db.value(2).toUInt();
170  uint draworder = db.value(3).toUInt();
171 
172  ScreenListInfo &screenListInfo = m_allScreens[container];
173 
174  WeatherScreen *ws = WeatherScreen::loadScreen(m_weatherStack, &screenListInfo, id);
175  if (!ws->Create())
176  {
177  delete ws;
178  continue;
179  }
180 
181  ws->setUnits(units);
182  ws->setInUse(true);
183  m_screens.insert(draworder, ws);
184  connect(ws, &WeatherScreen::screenReady, this,
186  m_srcMan->connectScreen(id, ws);
187  }
188 
189  if( m_screens.empty() )
190  {
191  // We rejected every screen... sit on this and rotate.
192  LOG(VB_GENERAL, LOG_ERR, "No weather screens left, aborting.");
193  m_nextPageTimer->stop();
194  if( m_updatedText )
195  {
196  m_updatedText->SetText(tr("None of the configured screens are complete in this theme (missing copyright information)."));
197  m_updatedText->Show();
198  return true;
199  }
200  return false;
201  }
202 
204  m_srcMan->doUpdate(true);
205  }
206 
207  return true;
208 }
209 
211 {
212  if (m_firstRun && !m_screens.empty() && ws == m_screens[m_curScreenNum])
213  {
214  m_firstRun = false;
215  showScreen(ws);
217  }
218  disconnect(ws, &WeatherScreen::screenReady, this, &Weather::screenReady);
219 }
220 
222 {
223  if (m_screens.empty())
224  return nullptr;
225 
226  m_curScreenNum = (m_curScreenNum + 1) % m_screens.size();
227  return m_screens[m_curScreenNum];
228 }
229 
231 {
232  if (m_screens.empty())
233  return nullptr;
234 
236  m_curScreenNum = (m_curScreenNum + m_screens.size() - 1) % m_screens.size();
237  return m_screens[m_curScreenNum];
238 }
239 
240 bool Weather::keyPressEvent(QKeyEvent *event)
241 {
242  if (GetFocusWidget() && GetFocusWidget()->keyPressEvent(event))
243  return true;
244 
245  QStringList actions;
246  bool handled = GetMythMainWindow()->TranslateKeyPress("Weather", event, actions);
247 
248  for (int i = 0; i < actions.size() && !handled; i++)
249  {
250  QString action = actions[i];
251  handled = true;
252 
253  if (action == "LEFT")
254  cursorLeft();
255  else if (action == "RIGHT")
256  cursorRight();
257  else if (action == "PAUSE")
258  holdPage();
259  else if (action == "MENU")
260  setupPage();
261  else if (action == "UPDATE")
262  {
263  m_srcMan->doUpdate();
264  }
265  else if (action == "ESCAPE")
266  {
267  m_nextPageTimer->stop();
268  hideScreen();
269  Close();
270  }
271  else
272  handled = false;
273  }
274 
275  if (!handled && MythScreenType::keyPressEvent(event))
276  handled = true;
277 
278  return handled;
279 }
280 
282 {
283  if (!ws)
284  return;
285 
286  m_currScreen = ws;
288  m_headerText->SetText(m_currScreen->objectName());
289  m_updatedText->SetText(m_currScreen->getValue("updatetime"));
290 }
291 
293 {
294  if (!m_currScreen)
295  return;
296 
297  m_weatherStack->PopScreen(nullptr, false,false);
298 }
299 
301 {
302  if (!m_nextPageTimer->isActive())
304  else
305  m_nextPageTimer->stop();
306 
307  m_paused = !m_paused;
308 
309  if (m_pauseText)
310  {
311  if (m_paused)
312  m_pauseText->Show();
313  else
314  m_pauseText->Hide();
315  }
316 }
317 
319 {
320  m_srcMan->stopTimers();
321  m_nextPageTimer->stop();
324 
326 
327  auto *ssetup = new ScreenSetup(mainStack, "weatherscreensetup", m_srcMan);
328 
329  connect(ssetup, &MythScreenType::Exiting, this, &Weather::setupScreens);
330 
331  if (ssetup->Create())
332  {
333  clearScreens();
334  mainStack->AddScreen(ssetup);
335  }
336  else
337  {
338  delete ssetup;
339  }
340 
341  m_firstRun = true;
342 }
343 
345 {
346  WeatherScreen *ws = nextScreen();
347  if (ws && ws->canShowScreen())
348  {
349  hideScreen();
350  showScreen(ws);
351  if (!m_paused)
353  }
354 }
355 
357 {
358  WeatherScreen *ws = prevScreen();
359  if (ws && ws->canShowScreen())
360  {
361  hideScreen();
362  showScreen(ws);
363  if (!m_paused)
365  }
366 }
367 
369 {
370  WeatherScreen *nxt = nextScreen();
371 
372  if (nxt && nxt->canShowScreen())
373  {
374  hideScreen();
375  showScreen(nxt);
376  }
377  else
378  LOG(VB_GENERAL, LOG_ERR, "Next screen not ready");
379 
381 }
382 
383 /*
384  * vim:ts=4:sw=4:ai:et:si:sts=4
385  */
Weather::hideScreen
void hideScreen(void)
Definition: weather.cpp:292
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:813
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:318
MSqlQuery::size
int size(void) const
Definition: mythdbcon.h:214
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
sourceManager.h
mythuitext.h
mythdb.h
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:386
Weather::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: weather.cpp:240
WeatherScreen::loadScreen
static WeatherScreen * loadScreen(MythScreenStack *parent, ScreenListInfo *screenDefn, int id)
Definition: weatherScreen.cpp:11
Weather::nextpage_timeout
void nextpage_timeout()
Definition: weather.cpp:368
Weather::m_screens
ScreenList m_screens
Definition: weather.h:65
Weather::Weather
Weather(MythScreenStack *parent, const QString &name, SourceManager *srcMan)
Definition: weather.cpp:21
Weather::nextScreen
WeatherScreen * nextScreen()
Definition: weather.cpp:221
Weather::showScreen
void showScreen(WeatherScreen *ws)
Definition: weather.cpp:281
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:204
MythScreenStack
Definition: mythscreenstack.h:16
Weather::~Weather
~Weather() override
Definition: weather.cpp:49
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:619
Weather::m_weatherStack
MythScreenStack * m_weatherStack
Definition: weather.h:54
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
WeatherScreen::setUnits
void setUnits(units_t units)
Definition: weatherScreen.h:44
weather.h
MythMainWindow::PopScreenStack
void PopScreenStack()
Definition: mythmainwindow.cpp:304
SourceManager::startTimers
void startTimers()
Definition: sourceManager.cpp:260
Weather::m_updatedText
MythUIText * m_updatedText
Definition: weather.h:74
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:113
Weather::m_srcMan
SourceManager * m_srcMan
Definition: weather.h:64
ScreenSetup
Definition: weatherSetup.h:64
Weather::m_curScreenNum
int m_curScreenNum
Definition: weather.h:66
loadScreens
ScreenListMap loadScreens()
Definition: weatherUtils.cpp:41
Weather::holdPage
void holdPage()
Definition: weather.cpp:300
weatherSetup.h
mythdate.h
SourceManager::stopTimers
void stopTimers()
Definition: sourceManager.cpp:266
SourceManager::findScripts
bool findScripts()
Definition: sourceManager.cpp:81
MythUIType::Show
void Show(void)
Definition: mythuitype.cpp:1147
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &Context, QKeyEvent *Event, QStringList &Actions, bool AllowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1112
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:551
Weather::cursorLeft
void cursorLeft()
Definition: weather.cpp:356
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:226
Weather::setupPage
void setupPage()
Definition: weather.cpp:318
Weather::m_firstSetup
bool m_firstSetup
Definition: weather.h:61
SourceManager::doUpdate
void doUpdate(bool forceUpdate=false)
Definition: sourceManager.cpp:272
Weather::m_firstRun
bool m_firstRun
Definition: weather.h:56
Weather::m_paused
bool m_paused
Definition: weather.h:70
Weather::m_currScreen
WeatherScreen * m_currScreen
Definition: weather.h:69
SourceManager::connectScreen
bool connectScreen(uint id, WeatherScreen *screen)
Definition: sourceManager.cpp:299
ScreenListInfo
Definition: weatherUtils.h:48
SourceManager::findScriptsDB
bool findScriptsDB()
Definition: sourceManager.cpp:35
Weather::m_allScreens
ScreenListMap m_allScreens
Definition: weather.h:68
SourceManager::clearSources
void clearSources()
Definition: sourceManager.cpp:154
WeatherScreen
Weather screen.
Definition: weatherScreen.h:26
Weather::setupScreens
void setupScreens()
Definition: weather.cpp:106
uint
unsigned int uint
Definition: compat.h:81
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
SourceManager::setupSources
void setupSources()
Definition: sourceManager.cpp:165
units_t
unsigned char units_t
Definition: weatherUtils.h:25
WeatherScreen::canShowScreen
virtual bool canShowScreen()
Definition: weatherScreen.cpp:46
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
MythUIType::Hide
void Hide(void)
Definition: mythuitype.cpp:1142
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:404
Weather::cursorRight
void cursorRight()
Definition: weather.cpp:344
Weather::m_nextPageTimer
QTimer * m_nextPageTimer
Definition: weather.h:59
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:687
WeatherScreen::setInUse
void setInUse(bool inuse)
Definition: weatherScreen.h:48
WeatherScreen::getValue
QString getValue(const QString &key)
Definition: weatherScreen.h:41
Weather::SetupScreens
bool SetupScreens()
Definition: weather.cpp:111
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:889
std
Definition: mythchrono.h:23
Weather::Create
bool Create(void) override
Definition: weather.cpp:60
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:132
mythcontext.h
MythScreenStack::PopScreen
virtual void PopScreen(MythScreenType *screen=nullptr, bool allowFade=true, bool deleteScreen=true)
Definition: mythscreenstack.cpp:86
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:104
Weather::screenReady
void screenReady(WeatherScreen *ws)
Definition: weather.cpp:210
build_compdb.action
action
Definition: build_compdb.py:9
Weather::m_headerText
MythUIText * m_headerText
Definition: weather.h:73
MythScreenType::Exiting
void Exiting()
WeatherScreen::screenReady
void screenReady(WeatherScreen *)
MythCoreContext::GetHostName
QString GetHostName(void)
Definition: mythcorecontext.cpp:838
weatherScreen.h
WeatherScreen::Create
bool Create(void) override
Definition: weatherScreen.cpp:33
srcMan
SourceManager * srcMan
Definition: mythweather.cpp:20
Weather::m_nextpageInterval
std::chrono::seconds m_nextpageInterval
Definition: weather.h:57
Weather::m_pauseText
MythUIText * m_pauseText
Definition: weather.h:72
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
Weather::m_createdSrcMan
bool m_createdSrcMan
Definition: weather.h:63
SourceManager
Definition: sourceManager.h:18
Weather::clearScreens
void clearScreens()
Definition: weather.cpp:92
Weather::prevScreen
WeatherScreen * prevScreen()
Definition: weather.cpp:230
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:838