MythTV  master
searchview.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstdlib>
3 
4 // qt
5 #include <QKeyEvent>
6 
7 // MythTV
8 #include <libmythbase/mythdb.h>
11 #include <libmythui/mythuitext.h>
13 
14 // mythmusic
15 #include "musiccommon.h"
16 #include "musicdata.h"
17 #include "searchview.h"
18 
20  :MusicCommon(parent, parentScreen,"searchview")
21 {
23 }
24 
26 {
27  // Load the theme for this screen
28  bool err = LoadWindowFromXML("music-ui.xml", "searchview", this);
29 
30  if (!err)
31  return false;
32 
33  // find common widgets available on any view
34  err = CreateCommon();
35 
36  // find widgets specific to this view
37  UIUtilE::Assign(this, m_fieldList, "field_list", &err);
38  UIUtilE::Assign(this, m_criteriaEdit, "criteria_edit", &err);
39  UIUtilW::Assign(this, m_matchesText, "matches_text", &err);
40  UIUtilE::Assign(this, m_tracksList, "tracks_list", &err);
41 
42  if (err)
43  {
44  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'searchview'");
45  return false;
46  }
47 
49 
51 
52  new MythUIButtonListItem(m_fieldList, tr("All Fields"),
53  QVariant::fromValue(0));
54  new MythUIButtonListItem(m_fieldList, tr("Artist"),
55  QVariant::fromValue(1));
56  new MythUIButtonListItem(m_fieldList, tr("Album"),
57  QVariant::fromValue(2));
58  new MythUIButtonListItem(m_fieldList, tr("Title"),
59  QVariant::fromValue(3));
60  new MythUIButtonListItem(m_fieldList, tr("Genre"),
61  QVariant::fromValue(4));
62  //new MythUIButtonListItem(m_fieldList, tr("Tags"),
63  // QVariant::fromValue(5));
64 
67 
70 
73 
75 
77 
78  return true;
79 }
80 
81 void SearchView::customEvent(QEvent *event)
82 {
83  bool handled = false;
84 
85  if (event->type() == MusicPlayerEvent::kTrackRemovedEvent ||
86  event->type() == MusicPlayerEvent::kTrackAddedEvent)
87  {
88  auto *mpe = dynamic_cast<MusicPlayerEvent *>(event);
89 
90  if (!mpe)
91  return;
92 
93  int trackID = mpe->m_trackID;
94 
95  for (int x = 0; x < m_tracksList->GetCount(); x++)
96  {
98  auto *mdata = item->GetData().value<MusicMetadata*>();
99  if (mdata && (mdata->ID() == (MusicMetadata::IdType) trackID || trackID == -1))
100  {
102  item->DisplayState("on", "selectedstate");
103  else
104  item->DisplayState("off", "selectedstate");
105  }
106  }
107 
108  // call the default handler in MusicCommon so the playlist and UI is updated
110  handled = true;
111 
112  if (m_playTrack == 1 || (m_playTrack == -1 && MusicPlayer::getPlayNow()))
113  {
114  if (event->type() == MusicPlayerEvent::kTrackAddedEvent)
115  {
116  // make the added track current and play it
119  }
120  }
121  m_playTrack = -1; // use PlayNow preference or menu option
122  }
123  else if (event->type() == MusicPlayerEvent::kAllTracksRemovedEvent)
124  {
125  for (int x = 0; x < m_tracksList->GetCount(); x++)
126  {
128  if (item)
129  item->DisplayState("off", "selectedstate");
130  }
131  }
132  else if (event->type() == MusicPlayerEvent::kMetadataChangedEvent)
133  {
134  auto *mpe = dynamic_cast<MusicPlayerEvent *>(event);
135 
136  if (!mpe)
137  return;
138 
139  uint trackID = mpe->m_trackID;
140 
141  for (int x = 0; x < m_tracksList->GetCount(); x++)
142  {
144  auto *mdata = item->GetData().value<MusicMetadata*>();
145  if (mdata && mdata->ID() == trackID)
146  {
147  InfoMap metadataMap;
148  mdata->toMap(metadataMap);
149  item->SetTextFromMap(metadataMap);
150  }
151  }
152 
153 // if (trackID == gPlayer->getCurrentMetadata()->ID())
154 // updateTrackInfo(gPlayer->getCurrentMetadata());
155  }
156  else if (event->type() == DialogCompletionEvent::kEventType)
157  {
158  auto *dce = dynamic_cast<DialogCompletionEvent *>(event);
159 
160  // make sure the user didn't ESCAPE out of the menu
161  if ((dce == nullptr) || (dce->GetResult() < 0))
162  return;
163 
164  QString resultid = dce->GetId();
165  QString resulttext = dce->GetResultText();
166  if (resultid == "searchviewmenu")
167  {
168  if (resulttext == tr("Add To Playlist") || resulttext == tr("Remove From Playlist"))
169  {
170  if (GetFocusWidget() == m_tracksList)
171  {
173  if (item)
174  {
175  m_playTrack = 0;
176  trackClicked(item);
177  }
178  }
179  }
180  else if (resulttext == tr("Play Now"))
181  {
182  if (GetFocusWidget() == m_tracksList)
183  {
185  if (item)
186  {
187  m_playTrack = 1;
188  trackClicked(item);
189  }
190  }
191  }
192  else if (resulttext == tr("Prefer Play Now"))
193  {
195  }
196  else if (resulttext == tr("Prefer Add Tracks"))
197  {
199  }
200  else if (resulttext == tr("Search List..."))
201  {
203  }
204  }
205  }
206 
207  if (!handled)
209 }
210 
211 bool SearchView::keyPressEvent(QKeyEvent *event)
212 {
214  return true;
215 
216  QStringList actions;
217  bool handled = GetMythMainWindow()->TranslateKeyPress("Music", event, actions);
218 
219  for (int i = 0; i < actions.size() && !handled; i++)
220  {
221  const QString& action = actions[i];
222  handled = true;
223 
224  if (action == "INFO" || action == "EDIT")
225  {
226  if (GetFocusWidget() == m_tracksList)
227  {
229  {
230  auto *mdata = m_tracksList->GetItemCurrent()->GetData().value<MusicMetadata*>();
231  if (mdata)
232  {
233  if (action == "INFO")
234  showTrackInfo(mdata);
235  else
236  editTrackInfo(mdata);
237  }
238  }
239  }
240  else
241  {
242  handled = false;
243  }
244  }
245  else if (action == "PLAY")
246  {
247  if (GetFocusWidget() == m_tracksList)
248  {
250  if (item)
251  {
252  m_playTrack = 1;
253  trackClicked(item);
254  }
255  }
256  else
257  {
258  handled = false;
259  }
260  }
261  else
262  {
263  handled = false;
264  }
265  }
266 
267  if (!handled && MusicCommon::keyPressEvent(event))
268  handled = true;
269 
270  return handled;
271 }
272 
274 {
275  if (GetFocusWidget() == m_tracksList)
276  {
277  QString label = tr("Search Actions");
278 
279  auto *menu = new MythMenu(label, this, "searchviewmenu");
280 
282  if (item)
283  {
284  auto *mdata = item->GetData().value<MusicMetadata*>();
285  if (mdata)
286  {
288  menu->AddItem(tr("Remove From Playlist"));
289  else
290  {
292  {
293  menu->AddItem(tr("Play Now"));
294  menu->AddItem(tr("Add To Playlist"));
295  menu->AddItem(tr("Prefer Add To Playlist"));
296  }
297  else
298  {
299  menu->AddItem(tr("Add To Playlist"));
300  menu->AddItem(tr("Play Now"));
301  menu->AddItem(tr("Prefer Play Now"));
302  }
303  }
304  }
305  }
306 
308  menu->AddItem(tr("Search List..."));
309 
310  menu->AddItem(tr("More Options"), nullptr, createSubMenu());
311 
312  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
313 
314  auto *menuPopup = new MythDialogBox(menu, popupStack, "actionmenu");
315 
316  if (menuPopup->Create())
317  popupStack->AddScreen(menuPopup);
318  else
319  delete menu;
320  }
321  else
322  {
324  }
325 }
326 
328 {
330 }
331 
333 {
335 }
336 
338 {
339  m_tracksList->Reset();
340 
342 
343  if (!item)
344  return;
345 
346  QString searchStr = m_criteriaEdit->GetText();
347  int field = item->GetData().toInt();
348 
349  QString sql;
350  MSqlQuery query(MSqlQuery::InitCon());
351 
352  if (searchStr.isEmpty())
353  {
354  sql = "SELECT song_id "
355  "FROM music_songs ";
356 
357  query.prepare(sql);
358  }
359  else
360  {
361  switch(field)
362  {
363  case 1: // artist
364  {
365  sql = "SELECT song_id "
366  "FROM music_songs "
367  "LEFT JOIN music_artists ON "
368  " music_songs.artist_id=music_artists.artist_id "
369  "WHERE music_artists.artist_name LIKE '%" + searchStr + "%' ";
370  query.prepare(sql);
371  break;
372  }
373  case 2: // album
374  {
375  sql = "SELECT song_id "
376  "FROM music_songs "
377  "LEFT JOIN music_albums ON music_songs.album_id=music_albums.album_id "
378  "WHERE music_albums.album_name LIKE '%" + searchStr + "%' ";
379  query.prepare(sql);
380  break;
381  }
382  case 3: // title
383  {
384  sql = "SELECT song_id "
385  "FROM music_songs "
386  "WHERE music_songs.name LIKE '%" + searchStr + "%' ";
387  query.prepare(sql);
388  break;
389  }
390  case 4: // genre
391  {
392  sql = "SELECT song_id "
393  "FROM music_songs "
394  "LEFT JOIN music_genres ON music_songs.genre_id=music_genres.genre_id "
395  "WHERE music_genres.genre LIKE '%" + searchStr + "%' ";
396  query.prepare(sql);
397  break;
398  }
399  case 5: // tags
400  {
401  //TODO add tag query. Remove fallthrough once added.
402  [[fallthrough]];
403  }
404  case 0: // all fields
405  default:
406  {
407  sql = "SELECT song_id "
408  "FROM music_songs "
409  "LEFT JOIN music_artists ON "
410  " music_songs.artist_id=music_artists.artist_id "
411  "LEFT JOIN music_albums ON music_songs.album_id=music_albums.album_id "
412  "LEFT JOIN music_artists AS music_comp_artists ON "
413  " music_albums.artist_id=music_comp_artists.artist_id "
414  "LEFT JOIN music_genres ON music_songs.genre_id=music_genres.genre_id "
415  "WHERE music_songs.name LIKE '%" + searchStr + "%' "
416  "OR music_artists.artist_name LIKE '%" + searchStr + "%' "
417  "OR music_albums.album_name LIKE '%" + searchStr + "%' "
418  "OR music_genres.genre LIKE '%" + searchStr + "%' ";
419 
420  query.prepare(sql);
421  }
422  }
423  }
424 
425  if (!query.exec() || !query.isActive())
426  {
427  MythDB::DBError("Search music database", query);
428  return;
429  }
430 
431  while (query.next())
432  {
433  int trackid = query.value(0).toInt();
434 
435  MusicMetadata *mdata = gMusicData->m_all_music->getMetadata(trackid);
436  if (mdata)
437  {
438  auto *newitem = new MythUIButtonListItem(m_tracksList, "");
439  newitem->SetData(QVariant::fromValue(mdata));
440  InfoMap metadataMap;
441  mdata->toMap(metadataMap);
442  newitem->SetTextFromMap(metadataMap);
443 
445  newitem->DisplayState("on", "selectedstate");
446  else
447  newitem->DisplayState("off", "selectedstate");
448 
449  // TODO rating state etc
450  }
451  }
452 
454 
455  if (m_matchesText)
456  m_matchesText->SetText(QString("%1").arg(m_tracksList->GetCount()));
457 }
458 
460 {
461  if (!gPlayer->getCurrentPlaylist())
462  return;
463 
464  auto *mdata = item->GetData().value<MusicMetadata*>();
465  if (mdata)
466  {
467  if (gPlayer->getCurrentPlaylist()->checkTrack(mdata->ID()))
468  gPlayer->getCurrentPlaylist()->removeTrack(mdata->ID());
469  else
470  gPlayer->getCurrentPlaylist()->addTrack(mdata->ID(), true);
471  }
472 }
473 
475 {
476 
477  if (!item)
478  return;
479 
480  if (item->GetImageFilename().isEmpty())
481  {
482  auto *mdata = item->GetData().value<MusicMetadata*>();
483  if (mdata)
484  {
485  QString artFile = mdata->getAlbumArtFile();
486  if (artFile.isEmpty())
487  item->SetImage("");
488  else
489  item->SetImage(mdata->getAlbumArtFile());
490  }
491  else
492  {
493  item->SetImage("");
494  }
495  }
496 }
MusicCommon::m_currentView
MusicView m_currentView
Definition: musiccommon.h:141
SearchView::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: searchview.cpp:211
MythUIButtonList::GetItemAt
MythUIButtonListItem * GetItemAt(int pos) const
Definition: mythuibuttonlist.cpp:1700
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:215
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:812
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
MusicCommon::editTrackInfo
static void editTrackInfo(MusicMetadata *mdata)
Definition: musiccommon.cpp:1945
gPlayer
MusicPlayer * gPlayer
Definition: musicplayer.cpp:37
SearchView::fieldSelected
void fieldSelected(MythUIButtonListItem *item)
Definition: searchview.cpp:327
MythUIButtonList::GetItemCurrent
MythUIButtonListItem * GetItemCurrent() const
Definition: mythuibuttonlist.cpp:1614
mythuitext.h
DialogCompletionEvent::GetId
QString GetId()
Definition: mythdialogbox.h:52
SearchView::criteriaChanged
void criteriaChanged(void)
Definition: searchview.cpp:332
mythdb.h
MythUIButtonListItem::DisplayState
void DisplayState(const QString &state, const QString &name)
Definition: mythuibuttonlist.cpp:3613
MusicCommon::CreateCommon
bool CreateCommon(void)
Definition: musiccommon.cpp:76
MusicPlayerEvent
Definition: musicplayer.h:20
MusicCommon::keyPressEvent
bool keyPressEvent(QKeyEvent *e) override
Key event handler.
Definition: musiccommon.cpp:585
MusicCommon::playlistItemClicked
void playlistItemClicked(MythUIButtonListItem *item)
Definition: musiccommon.cpp:2045
MusicCommon::createSubMenu
MythMenu * createSubMenu(void)
Definition: musiccommon.cpp:2318
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
MusicPlayer::getPlayNow
static bool getPlayNow(void)
Definition: musicplayer.cpp:229
mythdialogbox.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:204
MythScreenStack
Definition: mythscreenstack.h:16
MusicMetadata::ID
IdType ID() const
Definition: musicmetadata.h:218
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:618
MusicCommon::showTrackInfo
static void showTrackInfo(MusicMetadata *mdata)
Definition: musiccommon.cpp:2027
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
MusicMetadata
Definition: musicmetadata.h:81
MusicPlayerEvent::kMetadataChangedEvent
static const Type kMetadataChangedEvent
Definition: musicplayer.h:45
MythUITextEdit::GetText
QString GetText(void) const
Definition: mythuitextedit.h:50
MusicPlayer::getCurrentPlaylist
Playlist * getCurrentPlaylist(void)
Definition: musicplayer.cpp:1636
SearchView::m_playTrack
int m_playTrack
Definition: searchview.h:42
SearchView::m_matchesText
MythUIText * m_matchesText
Definition: searchview.h:45
mythuibuttonlist.h
MythUIButtonList::GetCount
int GetCount() const
Definition: mythuibuttonlist.cpp:1679
MusicPlayerEvent::kAllTracksRemovedEvent
static const Type kAllTracksRemovedEvent
Definition: musicplayer.h:44
SearchView::m_criteriaEdit
MythUITextEdit * m_criteriaEdit
Definition: searchview.h:44
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:110
InfoMap
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
MythUIButtonListItem
Definition: mythuibuttonlist.h:41
MusicCommon::m_moveTrackMode
bool m_moveTrackMode
Definition: musiccommon.h:152
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
MusicMetadata::toMap
void toMap(InfoMap &metadataMap, const QString &prefix="")
Definition: musicmetadata.cpp:1103
Playlist::removeTrack
void removeTrack(MusicMetadata::IdType trackID)
Definition: playlist.cpp:118
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:115
SearchView::m_fieldList
MythUIButtonList * m_fieldList
Definition: searchview.h:43
MythDialogBox
Basic menu dialog, message and a list of options.
Definition: mythdialogbox.h:166
menu
static MythThemedMenu * menu
Definition: mythtv-setup.cpp:58
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:550
MusicMetadata::getAlbumArtFile
QString getAlbumArtFile(void)
Definition: musicmetadata.cpp:1277
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:225
searchview.h
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:203
SearchView::SearchView
SearchView(MythScreenStack *parent, MythScreenType *parentScreen)
Definition: searchview.cpp:19
Playlist::addTrack
void addTrack(MusicMetadata::IdType trackID, bool update_display)
Given a tracks ID, add that track to this playlist.
Definition: playlist.cpp:62
MusicCommon::m_currentPlaylist
MythUIButtonList * m_currentPlaylist
Definition: musiccommon.h:205
SearchView::trackClicked
static void trackClicked(MythUIButtonListItem *item)
Definition: searchview.cpp:459
SearchView::Create
bool Create(void) override
Definition: searchview.cpp:25
MusicPlayerEvent::m_trackID
int m_trackID
Definition: musicplayer.h:33
SearchView::ShowMenu
void ShowMenu(void) override
Definition: searchview.cpp:273
Playlist::checkTrack
bool checkTrack(MusicMetadata::IdType trackID) const
Definition: playlist.cpp:37
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3715
AllMusic::getMetadata
MusicMetadata * getMetadata(int an_id)
Definition: musicmetadata.cpp:1651
MythUIButtonList::itemVisible
void itemVisible(MythUIButtonListItem *item)
musicdata.h
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
MythUIButtonListItem::SetTextFromMap
void SetTextFromMap(const InfoMap &infoMap, const QString &state="")
Definition: mythuibuttonlist.cpp:3338
musiccommon.h
gMusicData
MusicData * gMusicData
Definition: musicdata.cpp:21
MusicMetadata::IdType
uint32_t IdType
Definition: musicmetadata.h:87
MythMenu
Definition: mythdialogbox.h:99
mythuitextedit.h
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:701
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:3479
MusicCommon::searchButtonList
void searchButtonList(void)
Definition: musiccommon.cpp:2250
SearchView::customEvent
void customEvent(QEvent *event) override
Definition: searchview.cpp:81
DialogCompletionEvent
Event dispatched from MythUI modal dialogs to a listening class containing a result of some form.
Definition: mythdialogbox.h:41
SearchView::m_tracksList
MythUIButtonList * m_tracksList
Definition: searchview.h:46
SearchView::trackVisible
static void trackVisible(MythUIButtonListItem *item)
Definition: searchview.cpp:474
MythUIText::SetText
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:115
DialogCompletionEvent::kEventType
static const Type kEventType
Definition: mythdialogbox.h:57
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:1581
build_compdb.action
action
Definition: build_compdb.py:9
MusicPlayerEvent::kTrackRemovedEvent
static const Type kTrackRemovedEvent
Definition: musicplayer.h:42
MythMainWindow::GetStack
MythScreenStack * GetStack(const QString &Stackname)
Definition: mythmainwindow.cpp:322
MythUITextEdit::valueChanged
void valueChanged()
MusicData::m_all_music
AllMusic * m_all_music
Definition: musicdata.h:56
MusicPlayerEvent::kTrackAddedEvent
static const Type kTrackAddedEvent
Definition: musicplayer.h:41
MV_SEARCH
@ MV_SEARCH
Definition: musiccommon.h:38
MythUIButtonListItem::GetImageFilename
QString GetImageFilename(const QString &name="") const
Definition: mythuibuttonlist.cpp:3573
MusicPlayer::setPlayNow
static void setPlayNow(bool PlayNow)
whether we prefer Play Now over Add Tracks
Definition: musicplayer.cpp:225
MythScreenStack::AddScreen
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
Definition: mythscreenstack.cpp:52
MusicCommon::customEvent
void customEvent(QEvent *event) override
Definition: musiccommon.cpp:1230
SearchView::updateTracksList
void updateTracksList(void)
Definition: searchview.cpp:337
MusicCommon
Definition: musiccommon.h:49
MusicCommon::ShowMenu
void ShowMenu(void) override
Definition: musiccommon.cpp:2261
uint
unsigned int uint
Definition: freesurround.h:24
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:837