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 
72  this, &ZMEvents::dateChanged);
73 
74  // play button
75  if (m_playButton)
76  {
77  m_playButton->SetText(tr("Play"));
79  }
80 
81  // delete button
82  if (m_deleteButton)
83  {
84  m_deleteButton->SetText(tr("Delete"));
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 (auto *event : *m_eventList)
185  {
186  auto *item = new MythUIButtonListItem(m_eventGrid, "",
187  QVariant::fromValue(event));
188 
189  item->SetText(event->eventName());
190  item->SetText(event->monitorName(), "camera" );
191  item->SetText(
193  event->startTime(),
195  item->SetText(event->length(), "length");
196  }
197 
200 }
201 
203 {
205  return;
206 
208 
209  getEventList();
210 }
211 
213 {
215  return;
216 
218 
219  getEventList();
220 }
221 
223 {
224  (void) item;
225 
226  if (m_eventNoText)
227  {
228  if (m_eventGrid->GetCount() > 0)
229  {
230  m_eventNoText->SetText(QString("%1/%2")
231  .arg(m_eventGrid->GetCurrentPos() + 1).arg(m_eventGrid->GetCount()));
232  }
233  else
234  {
235  m_eventNoText->SetText("0/0");
236  }
237  }
238 }
239 
241 {
242  if (!item)
243  return;
244 
245  if (item->HasImage())
246  return;
247 
248  auto *event = item->GetData().value<Event*>();
249 
250  if (event)
251  {
252  QImage image;
253  if (ZMClient *zm = ZMClient::get())
254  {
255  zm->getAnalyseFrame(event, 0, image);
256  if (!image.isNull())
257  {
258  MythImage *mimage = GetMythPainter()->GetFormatImage();
259  mimage->Assign(image);
260  item->SetImage(mimage);
261  mimage->SetChanged();
262  mimage->DecrRef();
263  }
264  }
265  }
266 }
267 
269 {
270  if (!m_eventList || m_eventList->empty())
271  return;
272 
274  Event *event = m_eventList->at(m_savedPosition);
275  if (event)
276  {
278 
279  auto *player = new ZMPlayer(mainStack, "ZMPlayer",
281 
282  connect(player, &MythScreenType::Exiting, this, &ZMEvents::playerExited);
283 
284  if (player->Create())
285  mainStack->AddScreen(player);
286  }
287 }
288 
290 {
291  // refresh the grid and restore the saved position
292 
293  if (m_savedPosition > m_eventList->size() - 1)
294  m_savedPosition = m_eventList->size() - 1;
295 
296  updateUIList();
298 }
299 
301 {
302  if (!m_eventList || m_eventList->empty())
303  return;
304 
306  Event *event = m_eventList->at(m_savedPosition);
307  if (event)
308  {
309  if (ZMClient *zm = ZMClient::get())
310  zm->deleteEvent(event->eventID());
311 
313  delete item;
314 
315  std::vector<Event*>::iterator it;
316  for (it = m_eventList->begin(); it != m_eventList->end(); ++it)
317  {
318  if (*it == event)
319  {
320  m_eventList->erase(it);
321  break;
322  }
323  }
324  }
325 }
326 
328 {
329  if (ZMClient *zm = ZMClient::get())
330  {
331  QStringList cameraList;
332  zm->getCameraList(cameraList);
333  if (!m_cameraSelector)
334  return;
335 
336  new MythUIButtonListItem(m_cameraSelector, tr("All Cameras"));
337 
338  for (int x = 1; x <= cameraList.count(); x++)
339  {
340  new MythUIButtonListItem(m_cameraSelector, cameraList[x-1]);
341  }
342  }
343 }
344 
346 {
347  if (ZMClient *zm = ZMClient::get())
348  {
349  QString monitorName = "<ANY>";
350 
351  if (m_cameraSelector->GetValue() != tr("All Cameras"))
352  {
353  monitorName = m_cameraSelector->GetValue();
354  }
355 
356  zm->getEventDates(monitorName, m_oldestFirst, m_dateList);
357 
358  QString dateFormat = gCoreContext->GetSetting("ZoneMinderDateFormat", "ddd - dd/MM");
359 
360  new MythUIButtonListItem(m_dateSelector, tr("All Dates"));
361 
362  for (int x = 0; x < m_dateList.count(); x++)
363  {
364  QDate date = QDate::fromString(m_dateList[x], Qt::ISODate);
365  new MythUIButtonListItem(m_dateSelector, date.toString(dateFormat));
366  }
367  }
368 }
369 
370 void ZMEvents::setGridLayout(int layout)
371 {
372  if (layout < 1 || layout > 3)
373  layout = 1;
374 
375  if (layout == m_layout)
376  return;
377 
378  if (m_eventGrid)
379  m_eventGrid->Reset();
380 
381  m_layout = layout;
382 
383  // iterate though the children showing/hiding them as appropriate
384  QString name;
385  QString layoutName = QString("layout%1").arg(layout);
386  QList<MythUIType *> *children = GetAllChildren();
387 
388  for (auto *type : qAsConst(*children))
389  {
390  name = type->objectName();
391  if (name.startsWith("layout"))
392  {
393  if (name.startsWith(layoutName))
394  type->SetVisible(true);
395  else
396  type->SetVisible(false);
397  }
398  }
399 
400  // get the correct grid
401  m_eventGrid = dynamic_cast<MythUIButtonList *> (GetChild(layoutName + "_eventlist"));
402 
403  if (m_eventGrid)
404  {
406  this, &ZMEvents::eventChanged);
408  this, &ZMEvents::playPressed);
410  this, &ZMEvents::eventVisible);
411 
412  updateUIList();
413 
414  BuildFocusList();
415 
417  }
418  else
419  {
420  LOG(VB_GENERAL, LOG_ERR, QString("Theme is missing grid layout (%1).")
421  .arg(layoutName + "_eventlist"));
422  Close();
423  }
424 }
425 
427 {
428  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
429 
430  m_menuPopup = new MythDialogBox("Menu", popupStack, "actionmenu");
431 
432  if (m_menuPopup->Create())
433  popupStack->AddScreen(m_menuPopup);
434 
435  m_menuPopup->SetReturnEvent(this, "action");
436 
437  m_menuPopup->AddButton(tr("Refresh"), &ZMEvents::getEventList);
438 
439  if (m_showContinuous)
440  m_menuPopup->AddButton(tr("Hide Continuous Events"), &ZMEvents::toggleShowContinuous);
441  else
442  m_menuPopup->AddButton(tr("Show Continuous Events"), &ZMEvents::toggleShowContinuous);
443 
444  m_menuPopup->AddButton(tr("Change View"), &ZMEvents::changeView);
445  m_menuPopup->AddButton(tr("Delete All"), &ZMEvents::deleteAll);
446 }
447 
449 {
450  setGridLayout(m_layout + 1);
451 }
452 
454 {
456  getEventList();
457 }
458 
460 {
461  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
462 
463  QString title = tr("Delete All Events?");
464  QString msg = tr("Deleting %1 events in this view.").arg(m_eventGrid->GetCount());
465 
466  auto *dialog = new MythConfirmationDialog(popupStack, title + '\n' + msg, true);
467 
468  if (dialog->Create())
469  popupStack->AddScreen(dialog);
470 
471  connect(dialog, &MythConfirmationDialog::haveResult,
472  this, &ZMEvents::doDeleteAll, Qt::QueuedConnection);
473 }
474 
476 {
477  if (!doDelete)
478  return;
479 
480  //delete all events
481  if (ZMClient *zm = ZMClient::get())
482  {
483  zm->deleteEventList(m_eventList);
484 
485  getEventList();
486  }
487 }
ZMEvents::Create
bool Create(void) override
Definition: zmevents.cpp:44
MythUIButton::Clicked
void Clicked()
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:324
MythDialogBox::SetReturnEvent
void SetReturnEvent(QObject *retobject, const QString &resultid)
Definition: mythdialogbox.cpp:300
MythDate::toString
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:80
doDelete
bool doDelete
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:638
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1590
ZMEvents::m_menuPopup
MythDialogBox * m_menuPopup
Definition: zmevents.h:84
ZMPlayer
Definition: zmplayer.h:33
MythScreenType::Close
virtual void Close()
Definition: mythscreentype.cpp:402
title
QString title
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:636
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1142
MythUIType::GetChild
MythUIType * GetChild(const QString &name) const
Get a named child of this UIType.
Definition: mythuitype.cpp:131
ZMEvents::changeView
void changeView(void)
Definition: zmevents.cpp:448
MythPainter::GetFormatImage
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
Definition: mythpainter.cpp:540
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
ZMEvents::cameraChanged
void cameraChanged(void)
Definition: zmevents.cpp:202
MythConfirmationDialog::haveResult
void haveResult(bool)
MythScreenStack
Definition: mythscreenstack.h:16
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythUIType::GetAllChildren
QList< MythUIType * > * GetAllChildren(void)
Return a list of all child widgets.
Definition: mythuitype.cpp:195
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:222
ZMEvents::m_dateList
QStringList m_dateList
Definition: zmevents.h:69
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &stackname)
Definition: mythmainwindow.cpp:329
ZMEvents::m_savedPosition
size_t m_savedPosition
Definition: zmevents.h:70
ZMEvents::updateUIList
void updateUIList()
Definition: zmevents.cpp:177
MythUIButtonList::GetCount
int GetCount() const
Definition: mythuibuttonlist.cpp:1655
ZMEvents::eventVisible
static void eventVisible(MythUIButtonListItem *item)
Definition: zmevents.cpp:240
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:112
MythUIButtonListItem::HasImage
bool HasImage(const QString &name="")
Returns true when the image exists.
Definition: mythuibuttonlist.h:79
ZMEvents::playPressed
void playPressed(void)
Definition: zmevents.cpp:268
MythUIButtonListItem
Definition: mythuibuttonlist.h:28
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)
zmevents.h
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:198
ZMEvents::deletePressed
void deletePressed(void)
Definition: zmevents.cpp:300
Event
Event details.
Definition: zmdefines.h:27
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:117
MythDialogBox::AddButton
void AddButton(const QString &title)
Definition: mythdialogbox.h:197
MythDialogBox
Basic menu dialog, message and a list of options.
Definition: mythdialogbox.h:167
MythUIButton::SetText
void SetText(const QString &msg)
Definition: mythuibutton.cpp:229
ZMEvents::~ZMEvents
~ZMEvents() override
Definition: zmevents.cpp:34
MythDialogBox::Create
bool Create(void) override
Definition: mythdialogbox.cpp:127
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
MythImage::DecrRef
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:54
MythUIButton::Push
void Push(bool lock=false)
Definition: mythuibutton.cpp:175
ZMEvents::m_layout
int m_layout
Definition: zmevents.h:66
ZMEvents::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: zmevents.cpp:98
ZMEvents::m_showContinuous
bool m_showContinuous
Definition: zmevents.h:65
ZMEvents::doDeleteAll
void doDeleteAll(bool doDelete)
Definition: zmevents.cpp:475
MythImage::SetChanged
virtual void SetChanged(bool change=true)
Definition: mythimage.h:50
ZMClient::get
static ZMClient * get(void)
Definition: zmclient.cpp:38
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3580
zmplayer.h
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
ZMEvents::m_cameraSelector
MythUIButtonList * m_cameraSelector
Definition: zmevents.h:81
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:933
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:30
ZMEvents::m_playButton
MythUIButton * m_playButton
Definition: zmevents.h:78
MythDate::kSimplify
@ kSimplify
Do Today/Yesterday/Tomorrow transform.
Definition: mythdate.h:23
ZMEvents::getEventList
void getEventList(void)
Definition: zmevents.cpp:158
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:414
ZMEvents::setGridLayout
void setGridLayout(int layout)
Definition: zmevents.cpp:370
ZMEvents::ShowMenu
void ShowMenu(void) override
Definition: zmevents.cpp:426
ZMEvents::getDateList
void getDateList(void)
Definition: zmevents.cpp:345
ZMEvents::m_dateSelector
MythUIButtonList * m_dateSelector
Definition: zmevents.h:82
MythConfirmationDialog
Dialog asking for user confirmation.
Definition: mythdialogbox.h:271
ZMEvents::deleteAll
void deleteAll(void)
Definition: zmevents.cpp:459
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
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:3398
ZMEvents::m_currentCamera
int m_currentCamera
Definition: zmevents.h:71
MythImage
Definition: mythimage.h:37
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:14
MythUIButtonList::GetValue
virtual QString GetValue() const
Definition: mythuibuttonlist.cpp:1609
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:134
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:114
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:108
MythUIButtonList::SetItemCurrent
void SetItemCurrent(MythUIButtonListItem *item)
Definition: mythuibuttonlist.cpp:1557
build_compdb.action
action
Definition: build_compdb.py:9
MythScreenType::Exiting
void Exiting()
ZMEvents::m_oldestFirst
bool m_oldestFirst
Definition: zmevents.h:64
MythDate::kDateTimeFull
@ kDateTimeFull
Default local time.
Definition: mythdate.h:20
MythCoreContext::SaveSetting
void SaveSetting(const QString &key, int newValue)
Definition: mythcorecontext.cpp:902
MythImage::Assign
void Assign(const QImage &img)
Definition: mythimage.cpp:79
GetMythPainter
MythPainter * GetMythPainter(void)
Definition: mythmainwindow.cpp:123
ZMEvents::getCameraList
void getCameraList(void)
Definition: zmevents.cpp:327
MythUIButtonList::GetItemFirst
MythUIButtonListItem * GetItemFirst() const
Definition: mythuibuttonlist.cpp:1636
MythUIButtonList
List widget, displays list items in a variety of themeable arrangements and can trigger signals when ...
Definition: mythuibuttonlist.h:152
mythmainwindow.h
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
ZMEvents::dateChanged
void dateChanged(void)
Definition: zmevents.cpp:212
ZMClient
Definition: zmclient.h:18
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:919
ZMEvents::toggleShowContinuous
void toggleShowContinuous(void)
Definition: zmevents.cpp:453
ZMEvents::playerExited
void playerExited(void)
Definition: zmevents.cpp:289