MythTV  master
zmevents.cpp
Go to the documentation of this file.
1 /* ============================================================
2  * This program is free software; you can redistribute it
3  * and/or modify it under the terms of the GNU General
4  * Public License as published by the Free Software Foundation;
5  * either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * ============================================================ */
14 
15 #include <iostream>
16 #include <cstdlib>
17 
18 // qt
19 #include <QKeyEvent>
20 
21 // myth
22 #include <mythcontext.h>
23 #include <mythdbcon.h>
24 #include <mythmainwindow.h>
25 #include <mythdate.h>
26 
27 // zoneminder
28 #include "zmevents.h"
29 #include "zmplayer.h"
30 #include "zmclient.h"
31 
33 
35 {
36  delete m_eventList;
37 
38  // remember how the user wants to display the event list
39  gCoreContext->SaveSetting("ZoneMinderOldestFirst", (m_oldestFirst ? "1" : "0"));
40  gCoreContext->SaveSetting("ZoneMinderShowContinuous", (m_showContinuous ? "1" : "0"));
41  gCoreContext->SaveSetting("ZoneMinderGridLayout", m_layout);
42 }
43 
44 bool ZMEvents::Create(void)
45 {
46  // Load the theme for this screen
47  bool foundtheme = LoadWindowFromXML("zoneminder-ui.xml", "zmevents", this);
48  if (!foundtheme)
49  return false;
50 
51  bool err = false;
52  UIUtilE::Assign(this, m_eventNoText, "eventno_text", &err);
53  UIUtilE::Assign(this, m_playButton, "play_button", &err);
54  UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
55  UIUtilE::Assign(this, m_cameraSelector, "camera_selector", &err);
56  UIUtilE::Assign(this, m_dateSelector, "date_selector", &err);
57 
58  if (err)
59  {
60  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'zmevents'");
61  return false;
62  }
63 
65 
66  getCameraList();
67  getDateList();
68 
69  connect(m_cameraSelector, SIGNAL(itemSelected(MythUIButtonListItem*)),
70  this, SLOT(cameraChanged()));
71  connect(m_dateSelector, SIGNAL(itemSelected(MythUIButtonListItem*)),
72  this, SLOT(dateChanged()));
73 
74  // play button
75  if (m_playButton)
76  {
77  m_playButton->SetText(tr("Play"));
78  connect(m_playButton, SIGNAL(Clicked()), this, SLOT(playPressed()));
79  }
80 
81  // delete button
82  if (m_deleteButton)
83  {
84  m_deleteButton->SetText(tr("Delete"));
85  connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deletePressed()));
86  }
87 
88  m_oldestFirst = (gCoreContext->GetNumSetting("ZoneMinderOldestFirst", 1) == 1);
89  m_showContinuous = (gCoreContext->GetNumSetting("ZoneMinderShowContinuous", 0) == 1);
90 
91  getEventList();
92 
93  setGridLayout(gCoreContext->GetNumSetting("ZoneMinderGridLayout", 1));
94 
95  return true;
96 }
97 
98 bool ZMEvents::keyPressEvent(QKeyEvent *event)
99 {
100  // if there is a pending jump point pass the key press to the default handler
101  if (GetMythMainWindow()->IsExitingToMain())
102  return MythScreenType::keyPressEvent(event);
103 
104  if (GetFocusWidget()->keyPressEvent(event))
105  return true;
106 
107  QStringList actions;
108  bool handled = GetMythMainWindow()->TranslateKeyPress("TV Playback", event, actions);
109 
110  for (int i = 0; i < actions.size() && !handled; i++)
111  {
112  QString action = actions[i];
113  handled = true;
114 
115  if (action == "MENU")
116  {
117  ShowMenu();
118  }
119  else if (action == "ESCAPE")
120  {
121  if (GetFocusWidget() == m_eventGrid)
123  else
124  handled = false;
125  }
126 
127  else if (action == "DELETE")
128  {
129  if (m_deleteButton)
130  m_deleteButton->Push();
131  }
132  else if (action == "PAUSE")
133  {
134  if (m_playButton)
135  m_playButton->Push();
136  }
137  else if (action == "INFO")
138  {
140  getEventList();
141  }
142  else if (action == "1")
143  setGridLayout(1);
144  else if (action == "2")
145  setGridLayout(2);
146  else if (action == "3")
147  setGridLayout(3);
148  else
149  handled = false;
150  }
151 
152  if (!handled && MythScreenType::keyPressEvent(event))
153  handled = true;
154 
155  return handled;
156 }
157 
159 {
160  if (ZMClient *zm = ZMClient::get())
161  {
162  QString monitorName = "<ANY>";
163  QString date = "<ANY>";
164 
165  if (m_cameraSelector->GetValue() != tr("All Cameras"))
166  monitorName = m_cameraSelector->GetValue();
167 
168  if (m_dateSelector->GetValue() != tr("All Dates"))
169  date = m_dateList[m_dateSelector->GetCurrentPos() - 1];
170 
171  zm->getEventList(monitorName, m_oldestFirst, date, m_showContinuous, m_eventList);
172 
173  updateUIList();
174  }
175 }
176 
178 {
179  if (!m_eventGrid)
180  return;
181 
182  m_eventGrid->Reset();
183 
184  for (size_t i = 0; i < m_eventList->size(); i++)
185  {
186  Event *event = m_eventList->at(i);
187 
188  MythUIButtonListItem *item = new MythUIButtonListItem(m_eventGrid, "", qVariantFromValue(event));
189 
190  item->SetText(event->eventName());
191  item->SetText(event->monitorName(), "camera" );
192  item->SetText(
194  event->startTime(),
196  item->SetText(event->length(), "length");
197  }
198 
201 }
202 
204 {
206  return;
207 
209 
210  getEventList();
211 }
212 
214 {
216  return;
217 
219 
220  getEventList();
221 }
222 
224 {
225  (void) item;
226 
227  if (m_eventNoText)
228  {
229  if (m_eventGrid->GetCount() > 0)
230  m_eventNoText->SetText(QString("%1/%2")
231  .arg(m_eventGrid->GetCurrentPos() + 1).arg(m_eventGrid->GetCount()));
232  else
233  m_eventNoText->SetText("0/0");
234  }
235 }
236 
238 {
239  if (!item)
240  return;
241 
242  if (item->HasImage())
243  return;
244 
245  Event *event = item->GetData().value<Event*>();
246 
247  if (event)
248  {
249  QImage image;
250  if (ZMClient *zm = ZMClient::get())
251  {
252  zm->getAnalyseFrame(event, 0, image);
253  if (!image.isNull())
254  {
255  MythImage *mimage = GetMythPainter()->GetFormatImage();
256  mimage->Assign(image);
257  item->SetImage(mimage);
258  mimage->SetChanged();
259  mimage->DecrRef();
260  }
261  }
262  }
263 }
264 
266 {
267  if (!m_eventList || m_eventList->empty())
268  return;
269 
271  Event *event = m_eventList->at(m_savedPosition);
272  if (event)
273  {
275 
276  ZMPlayer *player = new ZMPlayer(mainStack, "ZMPlayer",
278 
279  connect(player, SIGNAL(Exiting()), this, SLOT(playerExited()));
280 
281  if (player->Create())
282  mainStack->AddScreen(player);
283  }
284 }
285 
287 {
288  // refresh the grid and restore the saved position
289 
290  if (m_savedPosition > (int)m_eventList->size() - 1)
291  m_savedPosition = m_eventList->size() - 1;
292 
293  updateUIList();
295 }
296 
298 {
299  if (!m_eventList || m_eventList->empty())
300  return;
301 
303  Event *event = m_eventList->at(m_savedPosition);
304  if (event)
305  {
306  if (ZMClient *zm = ZMClient::get())
307  zm->deleteEvent(event->eventID());
308 
310  delete item;
311 
312  vector<Event*>::iterator it;
313  for (it = m_eventList->begin(); it != m_eventList->end(); ++it)
314  {
315  if (*it == event)
316  {
317  m_eventList->erase(it);
318  break;
319  }
320  }
321  }
322 }
323 
325 {
326  if (ZMClient *zm = ZMClient::get())
327  {
328  QStringList cameraList;
329  zm->getCameraList(cameraList);
330  if (!m_cameraSelector)
331  return;
332 
333  new MythUIButtonListItem(m_cameraSelector, tr("All Cameras"));
334 
335  for (int x = 1; x <= cameraList.count(); x++)
336  {
337  new MythUIButtonListItem(m_cameraSelector, cameraList[x-1]);
338  }
339  }
340 }
341 
343 {
344  if (ZMClient *zm = ZMClient::get())
345  {
346  QString monitorName = "<ANY>";
347 
348  if (m_cameraSelector->GetValue() != tr("All Cameras"))
349  {
350  monitorName = m_cameraSelector->GetValue();
351  }
352 
353  zm->getEventDates(monitorName, m_oldestFirst, m_dateList);
354 
355  QString dateFormat = gCoreContext->GetSetting("ZoneMinderDateFormat", "ddd - dd/MM");
356 
357  new MythUIButtonListItem(m_dateSelector, tr("All Dates"));
358 
359  for (int x = 0; x < m_dateList.count(); x++)
360  {
361  QDate date = QDate::fromString(m_dateList[x], Qt::ISODate);
362  new MythUIButtonListItem(m_dateSelector, date.toString(dateFormat));
363  }
364  }
365 }
366 
367 void ZMEvents::setGridLayout(int layout)
368 {
369  if (layout < 1 || layout > 3)
370  layout = 1;
371 
372  if (layout == m_layout)
373  return;
374 
375  if (m_eventGrid)
376  m_eventGrid->Reset();
377 
378  m_layout = layout;
379 
380  // iterate though the children showing/hiding them as appropriate
381  QString name;
382  QString layoutName = QString("layout%1").arg(layout);
383  QList<MythUIType *> *children = GetAllChildren();
384 
385  for (int x = 0; x < children->size(); x++)
386  {
387  MythUIType *type = children->at(x);
388  name = type->objectName();
389  if (name.startsWith("layout"))
390  {
391  if (name.startsWith(layoutName))
392  type->SetVisible(true);
393  else
394  type->SetVisible(false);
395  }
396  }
397 
398  // get the correct grid
399  m_eventGrid = dynamic_cast<MythUIButtonList *> (GetChild(layoutName + "_eventlist"));
400 
401  if (m_eventGrid)
402  {
403  connect(m_eventGrid, SIGNAL(itemSelected( MythUIButtonListItem*)),
404  this, SLOT(eventChanged(MythUIButtonListItem*)));
405  connect(m_eventGrid, SIGNAL(itemClicked( MythUIButtonListItem*)),
406  this, SLOT(playPressed()));
407  connect(m_eventGrid, SIGNAL(itemVisible(MythUIButtonListItem*)),
408  this, SLOT(eventVisible(MythUIButtonListItem*)));
409 
410  updateUIList();
411 
412  BuildFocusList();
413 
415  }
416  else
417  {
418  LOG(VB_GENERAL, LOG_ERR, QString("Theme is missing grid layout (%1).")
419  .arg(layoutName + "_eventlist"));
420  Close();
421  }
422 }
423 
425 {
426  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
427 
428  m_menuPopup = new MythDialogBox("Menu", popupStack, "actionmenu");
429 
430  if (m_menuPopup->Create())
431  popupStack->AddScreen(m_menuPopup);
432 
433  m_menuPopup->SetReturnEvent(this, "action");
434 
435  m_menuPopup->AddButton(tr("Refresh"), SLOT(getEventList()));
436 
437  if (m_showContinuous)
438  m_menuPopup->AddButton(tr("Hide Continuous Events"), SLOT(toggleShowContinuous()));
439  else
440  m_menuPopup->AddButton(tr("Show Continuous Events"), SLOT(toggleShowContinuous()));
441 
442  m_menuPopup->AddButton(tr("Change View"), SLOT(changeView()));
443  m_menuPopup->AddButton(tr("Delete All"), SLOT(deleteAll()));
444 }
445 
447 {
448  setGridLayout(m_layout + 1);
449 }
450 
452 {
454  getEventList();
455 }
456 
458 {
459  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
460 
461  QString title = tr("Delete All Events?");
462  QString msg = tr("Deleting %1 events in this view.").arg(m_eventGrid->GetCount());
463 
465  popupStack, title + '\n' + msg, true);
466 
467  if (dialog->Create())
468  popupStack->AddScreen(dialog);
469 
470  connect(dialog, SIGNAL(haveResult(bool)),
471  SLOT(doDeleteAll(bool)), Qt::QueuedConnection);
472 }
473 
474 void ZMEvents::doDeleteAll(bool doDelete)
475 {
476  if (!doDelete)
477  return;
478 
479  //delete all events
480  if (ZMClient *zm = ZMClient::get())
481  {
482  zm->deleteEventList(m_eventList);
483 
484  getEventList();
485  }
486 }
QList< MythUIType * > * GetAllChildren(void)
Return a list of all child widgets.
Definition: mythuitype.cpp:196
void playerExited(void)
Definition: zmevents.cpp:286
bool HasImage(const QString &name="")
Returns true when the image exists.
MythUIButtonList * m_eventGrid
Definition: zmevents.h:76
bool m_oldestFirst
Definition: zmevents.h:64
Dialog asking for user confirmation.
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
void getCameraList(void)
Definition: zmevents.cpp:324
void cameraChanged(void)
Definition: zmevents.cpp:203
MythDialogBox * m_menuPopup
Definition: zmevents.h:84
virtual void SetChanged(bool change=true)
Definition: mythimage.h:45
void SaveSetting(const QString &key, int newValue)
void setGridLayout(int layout)
Definition: zmevents.cpp:367
void ShowMenu(void) override
Definition: zmevents.cpp:424
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:55
Basic menu dialog, message and a list of options.
void changeView(void)
Definition: zmevents.cpp:446
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:136
MythPainter * GetMythPainter(void)
MythUIButton * m_deleteButton
Definition: zmevents.h:79
MythScreenStack * GetStack(const QString &stackname)
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
MythScreenStack * GetMainStack()
void playPressed(void)
Definition: zmevents.cpp:265
Q_DECLARE_METATYPE(Event *)
The base class on which all widgets and screens are based.
Definition: mythuitype.h:63
void BuildFocusList(void)
bool keyPressEvent(QKeyEvent *) override
Key event handler.
Definition: zmevents.cpp:98
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
virtual void Close()
void AddButton(const QString &title, QVariant data=0, bool newMenu=false, bool setCurrent=false)
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
void toggleShowContinuous(void)
Definition: zmevents.cpp:451
void eventChanged(MythUIButtonListItem *item)
Definition: zmevents.cpp:223
QString GetSetting(const QString &key, const QString &defaultval="")
MythUIButtonList * m_dateSelector
Definition: zmevents.h:82
static ZMClient * get(void)
Definition: zmclient.cpp:38
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void SetText(const QString &text, const QString &name="", const QString &state="")
Do Today/Yesterday/Tomorrow transform.
Definition: mythdate.h:23
const char * name
Definition: ParseText.cpp:328
bool m_showContinuous
Definition: zmevents.h:65
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:101
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
MythUIText * m_eventNoText
Definition: zmevents.h:74
MythUIType * GetFocusWidget(void) const
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
void doDeleteAll(bool doDelete)
Definition: zmevents.cpp:474
MythMainWindow * GetMythMainWindow(void)
virtual QString GetValue() const
int GetNumSetting(const QString &key, int defaultval=0)
int m_layout
Definition: zmevents.h:66
bool keyPressEvent(QKeyEvent *) override
Key event handler.
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
int m_savedPosition
Definition: zmevents.h:70
void dateChanged(void)
Definition: zmevents.cpp:213
bool Create(void) override
QStringList m_dateList
Definition: zmevents.h:69
void getDateList(void)
Definition: zmevents.cpp:342
void deletePressed(void)
Definition: zmevents.cpp:297
void SetReturnEvent(QObject *retobject, const QString &resultid)
Event details.
Definition: zmdefines.h:25
Default local time.
Definition: mythdate.h:20
std::vector< Event * > * m_eventList
Definition: zmevents.h:68
MythUIButton * m_playButton
Definition: zmevents.h:78
void SetItemCurrent(MythUIButtonListItem *item)
int m_currentDate
Definition: zmevents.h:72
bool SetFocusWidget(MythUIType *widget=nullptr)
void updateUIList()
Definition: zmevents.cpp:177
~ZMEvents()
Definition: zmevents.cpp:34
MythUIButtonListItem * GetItemFirst() const
void Assign(const QImage &img)
Definition: mythimage.cpp:80
int GetCurrentPos() const
void deleteAll(void)
Definition: zmevents.cpp:457
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
bool Create(void) override
Definition: zmplayer.cpp:62
void SetText(const QString &msg)
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:132
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
void Push(bool lock=false)
int m_currentCamera
Definition: zmevents.h:71
bool Create(void) override
Definition: zmevents.cpp:44
Default UTC.
Definition: mythdate.h:14
MythUIButtonList * m_cameraSelector
Definition: zmevents.h:81
void eventVisible(MythUIButtonListItem *item)
Definition: zmevents.cpp:237
MythUIButtonListItem * GetItemCurrent() const
bool Create(void) override
void getEventList(void)
Definition: zmevents.cpp:158