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