00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <iostream>
00016 #include <cstdlib>
00017
00018
00019 #include <QKeyEvent>
00020
00021
00022 #include <mythcontext.h>
00023 #include <mythdbcon.h>
00024 #include <mythmainwindow.h>
00025 #include <mythmiscutil.h>
00026
00027
00028 #include "zmevents.h"
00029 #include "zmplayer.h"
00030 #include "zmclient.h"
00031
00032 ZMEvents::ZMEvents(MythScreenStack *parent) :
00033 MythScreenType(parent, "zmevents"),
00034 m_oldestFirst(false),
00035 m_layout(-1),
00036 m_eventList(new vector<Event*>),
00037 m_savedPosition(0),
00038 m_currentCamera(-1),
00039 m_currentDate(-1),
00040 m_eventNoText(NULL),
00041 m_eventGrid(NULL),
00042 m_playButton(NULL),
00043 m_deleteButton(NULL),
00044 m_cameraSelector(NULL),
00045 m_dateSelector(NULL),
00046 m_menuPopup(NULL)
00047 {
00048 }
00049
00050 ZMEvents::~ZMEvents()
00051 {
00052 if (m_eventList)
00053 delete m_eventList;
00054
00055
00056 gCoreContext->SaveSetting("ZoneMinderOldestFirst", (m_oldestFirst ? "1" : "0"));
00057 gCoreContext->SaveSetting("ZoneMinderGridLayout", m_layout);
00058 }
00059
00060 bool ZMEvents::Create(void)
00061 {
00062 bool foundtheme = false;
00063
00064
00065 foundtheme = LoadWindowFromXML("zoneminder-ui.xml", "zmevents", this);
00066
00067 if (!foundtheme)
00068 return false;
00069
00070 bool err = false;
00071 UIUtilE::Assign(this, m_eventNoText, "eventno_text", &err);
00072 UIUtilE::Assign(this, m_playButton, "play_button", &err);
00073 UIUtilE::Assign(this, m_deleteButton, "delete_button", &err);
00074 UIUtilE::Assign(this, m_cameraSelector, "camera_selector", &err);
00075 UIUtilE::Assign(this, m_dateSelector, "date_selector", &err);
00076
00077 if (err)
00078 {
00079 LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'zmevents'");
00080 return false;
00081 }
00082
00083 BuildFocusList();
00084
00085 getCameraList();
00086 getDateList();
00087
00088 connect(m_cameraSelector, SIGNAL(itemSelected(MythUIButtonListItem*)),
00089 this, SLOT(cameraChanged()));
00090 connect(m_dateSelector, SIGNAL(itemSelected(MythUIButtonListItem*)),
00091 this, SLOT(dateChanged()));
00092
00093
00094 if (m_playButton)
00095 {
00096 m_playButton->SetText(tr("Play"));
00097 connect(m_playButton, SIGNAL(Clicked()), this, SLOT(playPressed()));
00098 }
00099
00100
00101 if (m_deleteButton)
00102 {
00103 m_deleteButton->SetText(tr("Delete"));
00104 connect(m_deleteButton, SIGNAL(Clicked()), this, SLOT(deletePressed()));
00105 }
00106
00107 getEventList();
00108
00109 m_oldestFirst = (gCoreContext->GetNumSetting("ZoneMinderOldestFirst", 1) == 1);
00110 setGridLayout(gCoreContext->GetNumSetting("ZoneMinderGridLayout", 1));
00111
00112 return true;
00113 }
00114
00115 bool ZMEvents::keyPressEvent(QKeyEvent *event)
00116 {
00117 if (GetFocusWidget()->keyPressEvent(event))
00118 return true;
00119
00120 bool handled = false;
00121 QStringList actions;
00122 handled = GetMythMainWindow()->TranslateKeyPress("TV Playback", event, actions);
00123
00124 for (int i = 0; i < actions.size() && !handled; i++)
00125 {
00126 QString action = actions[i];
00127 handled = true;
00128
00129 if (action == "MENU")
00130 {
00131 showMenu();
00132 }
00133 else if (action == "ESCAPE")
00134 {
00135 if (GetFocusWidget() == m_eventGrid)
00136 SetFocusWidget(m_cameraSelector);
00137 else
00138 handled = false;
00139 }
00140
00141 else if (action == "DELETE")
00142 {
00143 if (m_deleteButton)
00144 m_deleteButton->Push();
00145 }
00146 else if (action == "PAUSE")
00147 {
00148 if (m_playButton)
00149 m_playButton->Push();
00150 }
00151 else if (action == "INFO")
00152 {
00153 m_oldestFirst = !m_oldestFirst;
00154 getEventList();
00155 }
00156 else if (action == "1")
00157 setGridLayout(1);
00158 else if (action == "2")
00159 setGridLayout(2);
00160 else if (action == "3")
00161 setGridLayout(3);
00162 else
00163 handled = false;
00164 }
00165
00166 if (!handled && MythScreenType::keyPressEvent(event))
00167 handled = true;
00168
00169 return handled;
00170 }
00171
00172 void ZMEvents::getEventList(void)
00173 {
00174 if (class ZMClient *zm = ZMClient::get())
00175 {
00176 QString monitorName = "<ANY>";
00177 QString date = "<ANY>";
00178
00179 if (m_cameraSelector->GetValue() != tr("All Cameras"))
00180 monitorName = m_cameraSelector->GetValue();
00181
00182 if (m_dateSelector->GetValue() != tr("All Dates"))
00183 date = m_dateList[m_dateSelector->GetCurrentPos() - 1];
00184
00185 zm->getEventList(monitorName, m_oldestFirst, date, m_eventList);
00186
00187 updateUIList();
00188 }
00189 }
00190
00191 void ZMEvents::updateUIList()
00192 {
00193 if (!m_eventGrid)
00194 return;
00195
00196 m_eventGrid->Reset();
00197
00198 for (uint i = 0; i < m_eventList->size(); i++)
00199 {
00200 Event *event = m_eventList->at(i);
00201
00202 MythUIButtonListItem *item = new MythUIButtonListItem(m_eventGrid,
00203 "", NULL, true, MythUIButtonListItem::NotChecked);
00204
00205 item->SetText(event->eventName);
00206 item->SetText(event->monitorName, "camera" );
00207 item->SetText(MythDateTimeToString(event->startTime, kDateTimeFull | kSimplify), "time");
00208 item->SetText(event->length, "length");
00209 }
00210
00211 m_eventGrid->SetItemCurrent(m_eventGrid->GetItemFirst());
00212 eventChanged(m_eventGrid->GetItemCurrent());
00213 }
00214
00215 void ZMEvents::cameraChanged()
00216 {
00217 if (m_currentCamera == m_cameraSelector->GetCurrentPos())
00218 return;
00219
00220 m_currentCamera = m_cameraSelector->GetCurrentPos();
00221
00222 getEventList();
00223 }
00224
00225 void ZMEvents::dateChanged()
00226 {
00227 if (m_currentDate == m_dateSelector->GetCurrentPos())
00228 return;
00229
00230 m_currentDate = m_dateSelector->GetCurrentPos();
00231
00232 getEventList();
00233 }
00234
00235 void ZMEvents::eventChanged(MythUIButtonListItem *item)
00236 {
00237 (void) item;
00238
00239 if (m_eventNoText)
00240 {
00241 if (m_eventGrid->GetCount() > 0)
00242 m_eventNoText->SetText(QString("%1/%2")
00243 .arg(m_eventGrid->GetCurrentPos() + 1).arg(m_eventGrid->GetCount()));
00244 else
00245 m_eventNoText->SetText("0/0");
00246 }
00247
00248
00249 for (int x = m_eventGrid->GetCurrentPos() - m_eventGrid->GetVisibleCount();
00250 x < m_eventGrid->GetCurrentPos() + (int)m_eventGrid->GetVisibleCount(); x++)
00251 {
00252 if (x < 0 || x > (int)m_eventGrid->GetCount() - 1)
00253 continue;
00254
00255 MythUIButtonListItem *gridItem = m_eventGrid->GetItemAt(x);
00256 if (gridItem && !gridItem->getImage())
00257 {
00258 if (x < 0 || x > (int)m_eventList->size() - 1)
00259 continue;
00260
00261 Event *event = m_eventList->at(x);
00262 if (event)
00263 {
00264 QImage image;
00265 if (class ZMClient *zm = ZMClient::get())
00266 {
00267 zm->getAnalyseFrame(event, 0, image);
00268 if (!image.isNull())
00269 {
00270 MythImage *mimage = GetMythPainter()->GetFormatImage();
00271 mimage->Assign(image);
00272 gridItem->setImage(mimage);
00273 mimage->SetChanged();
00274 }
00275 }
00276 }
00277 }
00278 }
00279 }
00280
00281 void ZMEvents::playPressed(void)
00282 {
00283 if (!m_eventList || m_eventList->empty())
00284 return;
00285
00286 m_savedPosition = m_eventGrid->GetCurrentPos();
00287 Event *event = m_eventList->at(m_savedPosition);
00288 if (event)
00289 {
00290 MythScreenStack *mainStack = GetMythMainWindow()->GetMainStack();
00291
00292 ZMPlayer *player = new ZMPlayer(mainStack, "ZMPlayer",
00293 m_eventList, &m_savedPosition);
00294
00295 connect(player, SIGNAL(Exiting()), this, SLOT(playerExited()));
00296
00297 if (player->Create())
00298 mainStack->AddScreen(player);
00299 }
00300 }
00301
00302 void ZMEvents::playerExited(void)
00303 {
00304
00305
00306 if (m_savedPosition > (int)m_eventList->size() - 1)
00307 m_savedPosition = m_eventList->size() - 1;
00308
00309 updateUIList();
00310 m_eventGrid->SetItemCurrent(m_savedPosition);
00311 }
00312
00313 void ZMEvents::deletePressed(void)
00314 {
00315 if (!m_eventList || m_eventList->empty())
00316 return;
00317
00318 m_savedPosition = m_eventGrid->GetCurrentPos();
00319 Event *event = m_eventList->at(m_savedPosition);
00320 if (event)
00321 {
00322 if (class ZMClient *zm = ZMClient::get())
00323 zm->deleteEvent(event->eventID);
00324
00325 MythUIButtonListItem *item = m_eventGrid->GetItemCurrent();
00326 if (item)
00327 delete item;
00328
00329 vector<Event*>::iterator it;
00330 for (it = m_eventList->begin(); it != m_eventList->end(); ++it)
00331 {
00332 if (*it == event)
00333 {
00334 m_eventList->erase(it);
00335 break;
00336 }
00337 }
00338 }
00339 }
00340
00341 void ZMEvents::getCameraList(void)
00342 {
00343 if (class ZMClient *zm = ZMClient::get())
00344 {
00345 QStringList cameraList;
00346 zm->getCameraList(cameraList);
00347 if (!m_cameraSelector)
00348 return;
00349
00350 new MythUIButtonListItem(m_cameraSelector, tr("All Cameras"));
00351
00352 for (int x = 1; x <= cameraList.count(); x++)
00353 {
00354 new MythUIButtonListItem(m_cameraSelector, cameraList[x-1]);
00355 }
00356 }
00357 }
00358
00359 void ZMEvents::getDateList(void)
00360 {
00361 if (class ZMClient *zm = ZMClient::get())
00362 {
00363 QString monitorName = "<ANY>";
00364
00365 if (m_cameraSelector->GetValue() != tr("All Cameras"))
00366 {
00367 monitorName = m_cameraSelector->GetValue();
00368 }
00369
00370 zm->getEventDates(monitorName, m_oldestFirst, m_dateList);
00371
00372 QString dateFormat = gCoreContext->GetSetting("ZoneMinderDateFormat", "ddd - dd/MM");
00373
00374 new MythUIButtonListItem(m_dateSelector, tr("All Dates"));
00375
00376 for (int x = 0; x < m_dateList.count(); x++)
00377 {
00378 QDate date = QDate::fromString(m_dateList[x], Qt::ISODate);
00379 new MythUIButtonListItem(m_dateSelector, date.toString(dateFormat));
00380 }
00381 }
00382 }
00383
00384 void ZMEvents::setGridLayout(int layout)
00385 {
00386 if (layout < 1 || layout > 3)
00387 layout = 1;
00388
00389 if (layout == m_layout)
00390 return;
00391
00392 if (m_eventGrid)
00393 m_eventGrid->Reset();
00394
00395 m_layout = layout;
00396
00397
00398 QString name;
00399 QString layoutName = QString("layout%1").arg(layout);
00400 QList<MythUIType *> *children = GetAllChildren();
00401
00402 for (int x = 0; x < children->size(); x++)
00403 {
00404 MythUIType *type = children->at(x);
00405 name = type->objectName();
00406 if (name.startsWith("layout"))
00407 {
00408 if (name.startsWith(layoutName))
00409 type->SetVisible(true);
00410 else
00411 type->SetVisible(false);
00412 }
00413 }
00414
00415
00416 m_eventGrid = dynamic_cast<MythUIButtonList *> (GetChild(layoutName + "_eventlist"));
00417
00418 if (m_eventGrid)
00419 {
00420 connect(m_eventGrid, SIGNAL(itemSelected( MythUIButtonListItem*)),
00421 this, SLOT(eventChanged(MythUIButtonListItem*)));
00422 connect(m_eventGrid, SIGNAL(itemClicked( MythUIButtonListItem*)),
00423 this, SLOT(playPressed()));
00424
00425 updateUIList();
00426
00427 BuildFocusList();
00428
00429 SetFocusWidget(m_eventGrid);
00430 }
00431 else
00432 {
00433 LOG(VB_GENERAL, LOG_ERR, QString("Theme is missing grid layout (%1).")
00434 .arg(layoutName + "_eventlist"));
00435 Close();
00436 }
00437 }
00438
00439 void ZMEvents::showMenu()
00440 {
00441 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00442
00443 m_menuPopup = new MythDialogBox("Menu", popupStack, "actionmenu");
00444
00445 if (m_menuPopup->Create())
00446 popupStack->AddScreen(m_menuPopup);
00447
00448 m_menuPopup->SetReturnEvent(this, "action");
00449
00450 m_menuPopup->AddButton(tr("Refresh"), SLOT(getEventList()));
00451 m_menuPopup->AddButton(tr("Change View"), SLOT(changeView()));
00452 m_menuPopup->AddButton(tr("Delete All"), SLOT(deleteAll()));
00453 }
00454
00455 void ZMEvents::changeView(void)
00456 {
00457 setGridLayout(m_layout + 1);
00458 }
00459
00460 void ZMEvents::deleteAll(void)
00461 {
00462 MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
00463
00464 QString title = tr("Delete All Events?");
00465 QString msg = tr("Deleting %1 events in this view.").arg(m_eventGrid->GetCount());
00466
00467 MythConfirmationDialog *dialog = new MythConfirmationDialog(
00468 popupStack, title + '\n' + msg, true);
00469
00470 if (dialog->Create())
00471 popupStack->AddScreen(dialog);
00472
00473 connect(dialog, SIGNAL(haveResult(bool)),
00474 SLOT(doDeleteAll(bool)), Qt::QueuedConnection);
00475 }
00476
00477 void ZMEvents::doDeleteAll(bool doDelete)
00478 {
00479 if (!doDelete)
00480 return;
00481
00482
00483 if (class ZMClient *zm = ZMClient::get())
00484 {
00485 zm->deleteEventList(m_eventList);
00486
00487 getEventList();
00488 }
00489 }