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 
224 {
225  (void) item;
226 
227  if (m_eventNoText)
228  {
229  if (m_eventGrid->GetCount() > 0)
230  {
231  m_eventNoText->SetText(QString("%1/%2")
232  .arg(m_eventGrid->GetCurrentPos() + 1).arg(m_eventGrid->GetCount()));
233  }
234  else
235  {
236  m_eventNoText->SetText("0/0");
237  }
238  }
239 }
240 
242 {
243  if (!item)
244  return;
245 
246  if (item->HasImage())
247  return;
248 
249  auto *event = item->GetData().value<Event*>();
250 
251  if (event)
252  {
253  QImage image;
254  if (ZMClient *zm = ZMClient::get())
255  {
256  zm->getAnalyseFrame(event, 0, image);
257  if (!image.isNull())
258  {
259  MythImage *mimage = GetMythPainter()->GetFormatImage();
260  mimage->Assign(image);
261  item->SetImage(mimage);
262  mimage->SetChanged();
263  mimage->DecrRef();
264  }
265  }
266  }
267 }
268 
270 {
271  if (!m_eventList || m_eventList->empty())
272  return;
273 
275  Event *event = m_eventList->at(m_savedPosition);
276  if (event)
277  {
279 
280  auto *player = new ZMPlayer(mainStack, "ZMPlayer",
282 
283  connect(player, &MythScreenType::Exiting, this, &ZMEvents::playerExited);
284 
285  if (player->Create())
286  mainStack->AddScreen(player);
287  }
288 }
289 
291 {
292  // refresh the grid and restore the saved position
293 
294  if (m_savedPosition > m_eventList->size() - 1)
295  m_savedPosition = m_eventList->size() - 1;
296 
297  updateUIList();
299 }
300 
302 {
303  if (!m_eventList || m_eventList->empty())
304  return;
305 
307  Event *event = m_eventList->at(m_savedPosition);
308  if (event)
309  {
310  if (ZMClient *zm = ZMClient::get())
311  zm->deleteEvent(event->eventID());
312 
314  delete item;
315 
316  std::vector<Event*>::iterator it;
317  for (it = m_eventList->begin(); it != m_eventList->end(); ++it)
318  {
319  if (*it == event)
320  {
321  m_eventList->erase(it);
322  break;
323  }
324  }
325  }
326 }
327 
329 {
330  if (ZMClient *zm = ZMClient::get())
331  {
332  QStringList cameraList;
333  zm->getCameraList(cameraList);
334  if (!m_cameraSelector)
335  return;
336 
337  new MythUIButtonListItem(m_cameraSelector, tr("All Cameras"));
338 
339  for (int x = 1; x <= cameraList.count(); x++)
340  {
341  new MythUIButtonListItem(m_cameraSelector, cameraList[x-1]);
342  }
343  }
344 }
345 
347 {
348  if (ZMClient *zm = ZMClient::get())
349  {
350  QString monitorName = "<ANY>";
351 
352  if (m_cameraSelector->GetValue() != tr("All Cameras"))
353  {
354  monitorName = m_cameraSelector->GetValue();
355  }
356 
357  zm->getEventDates(monitorName, m_oldestFirst, m_dateList);
358 
359  QString dateFormat = gCoreContext->GetSetting("ZoneMinderDateFormat", "ddd - dd/MM");
360 
361  new MythUIButtonListItem(m_dateSelector, tr("All Dates"));
362 
363  for (int x = 0; x < m_dateList.count(); x++)
364  {
365  QDate date = QDate::fromString(m_dateList[x], Qt::ISODate);
366  new MythUIButtonListItem(m_dateSelector, date.toString(dateFormat));
367  }
368  }
369 }
370 
371 void ZMEvents::setGridLayout(int layout)
372 {
373  if (layout < 1 || layout > 3)
374  layout = 1;
375 
376  if (layout == m_layout)
377  return;
378 
379  if (m_eventGrid)
380  m_eventGrid->Reset();
381 
382  m_layout = layout;
383 
384  // iterate though the children showing/hiding them as appropriate
385  QString name;
386  QString layoutName = QString("layout%1").arg(layout);
387  QList<MythUIType *> *children = GetAllChildren();
388 
389  for (auto *type : qAsConst(*children))
390  {
391  name = type->objectName();
392  if (name.startsWith("layout"))
393  {
394  if (name.startsWith(layoutName))
395  type->SetVisible(true);
396  else
397  type->SetVisible(false);
398  }
399  }
400 
401  // get the correct grid
402  m_eventGrid = dynamic_cast<MythUIButtonList *> (GetChild(layoutName + "_eventlist"));
403 
404  if (m_eventGrid)
405  {
407  this, &ZMEvents::eventChanged);
409  this, &ZMEvents::playPressed);
411  this, &ZMEvents::eventVisible);
412 
413  updateUIList();
414 
415  BuildFocusList();
416 
418  }
419  else
420  {
421  LOG(VB_GENERAL, LOG_ERR, QString("Theme is missing grid layout (%1).")
422  .arg(layoutName + "_eventlist"));
423  Close();
424  }
425 }
426 
428 {
429  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
430 
431  m_menuPopup = new MythDialogBox("Menu", popupStack, "actionmenu");
432 
433  if (m_menuPopup->Create())
434  popupStack->AddScreen(m_menuPopup);
435 
436  m_menuPopup->SetReturnEvent(this, "action");
437 
438  m_menuPopup->AddButton(tr("Refresh"), &ZMEvents::getEventList);
439 
440  if (m_showContinuous)
441  m_menuPopup->AddButton(tr("Hide Continuous Events"), &ZMEvents::toggleShowContinuous);
442  else
443  m_menuPopup->AddButton(tr("Show Continuous Events"), &ZMEvents::toggleShowContinuous);
444 
445  m_menuPopup->AddButton(tr("Change View"), &ZMEvents::changeView);
446  m_menuPopup->AddButton(tr("Delete All"), &ZMEvents::deleteAll);
447 }
448 
450 {
451  setGridLayout(m_layout + 1);
452 }
453 
455 {
457  getEventList();
458 }
459 
461 {
462  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
463 
464  QString title = tr("Delete All Events?");
465  QString msg = tr("Deleting %1 events in this view.").arg(m_eventGrid->GetCount());
466 
467  auto *dialog = new MythConfirmationDialog(popupStack, title + '\n' + msg, true);
468 
469  if (dialog->Create())
470  popupStack->AddScreen(dialog);
471 
472  connect(dialog, &MythConfirmationDialog::haveResult,
473  this, &ZMEvents::doDeleteAll, Qt::QueuedConnection);
474 }
475 
476 void ZMEvents::doDeleteAll(bool doDelete)
477 {
478  if (!doDelete)
479  return;
480 
481  //delete all events
482  if (ZMClient *zm = ZMClient::get())
483  {
484  zm->deleteEventList(m_eventList);
485 
486  getEventList();
487  }
488 }
ZMEvents::Create
bool Create(void) override
Definition: zmevents.cpp:45
MythUIButton::Clicked
void Clicked()
MythMainWindow::GetMainStack
MythScreenStack * GetMainStack()
Definition: mythmainwindow.cpp:315
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:449
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: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:241
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:269
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:1104
zmevents.h
MythUIButtonList::GetCurrentPos
int GetCurrentPos() const
Definition: mythuibuttonlist.h:238
ZMEvents::deletePressed
void deletePressed(void)
Definition: zmevents.cpp:301
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:476
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:54
ZMEvents::m_cameraSelector
MythUIButtonList * m_cameraSelector
Definition: zmevents.h:81
MythCoreContext::GetNumSetting
int GetNumSetting(const QString &key, int defaultval=0)
Definition: mythcorecontext.cpp:910
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:371
ZMEvents::ShowMenu
void ShowMenu(void) override
Definition: zmevents.cpp:427
ZMEvents::getDateList
void getDateList(void)
Definition: zmevents.cpp:346
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:460
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:102
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:320
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:879
MythImage::Assign
void Assign(const QImage &img)
Definition: mythimage.cpp:77
GetMythPainter
MythPainter * GetMythPainter(void)
Definition: mythmainwindow.cpp:117
ZMEvents::getCameraList
void getCameraList(void)
Definition: zmevents.cpp:328
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:896
ZMEvents::toggleShowContinuous
void toggleShowContinuous(void)
Definition: zmevents.cpp:454
ZMEvents::playerExited
void playerExited(void)
Definition: zmevents.cpp:290