Refactor playlist searching

This commit is contained in:
Keith Edmunds 2022-08-15 09:10:26 +01:00
parent 8fedb394a4
commit 49bef912d2
2 changed files with 41 additions and 55 deletions

View File

@ -638,6 +638,9 @@ class Window(QMainWindow, Ui_MainWindow):
self.disable_play_next_controls() self.disable_play_next_controls()
self.txtSearch.setHidden(False) self.txtSearch.setHidden(False)
self.txtSearch.setFocus() self.txtSearch.setFocus()
# Select any text that may already be there
self.txtSearch.selectAll()
def search_playlist_clear(self) -> None: def search_playlist_clear(self) -> None:
"""Tidy up and reset search bar""" """Tidy up and reset search bar"""
@ -651,7 +654,7 @@ class Window(QMainWindow, Ui_MainWindow):
def search_playlist_return(self) -> None: def search_playlist_return(self) -> None:
"""Initiate search when return pressed""" """Initiate search when return pressed"""
self.visible_playlist_tab().search(self.txtSearch.text()) self.visible_playlist_tab().set_search(self.txtSearch.text())
self.enable_play_next_controls() self.enable_play_next_controls()
# def search_playlist_update(self): # def search_playlist_update(self):

View File

@ -776,31 +776,37 @@ class PlaylistTab(QTableWidget):
session.commit() session.commit()
PlaylistRows.delete_higher_rows(session, self.playlist_id, row) PlaylistRows.delete_higher_rows(session, self.playlist_id, row)
def search(self, text: str) -> None: def set_search(self, text: str) -> None:
"""Set search text and find first match""" """Set search text and find first match"""
self.search_text = text self.search_text = text
if not text: if not text:
# Search string has been reset # Search string has been reset
return return
self.search_next() self._search(next=True)
def search_next(self) -> None: def _search(self, next: bool = True) -> None:
""" """
Select next row containg self.search_string. Start from Select next/previous row containg self.search_string. Start from
top selected row if there is one, else from top. top selected row if there is one, else from top.
Wrap at last row. Wrap at last/first row.
""" """
if not self.search_text: if not self.search_text:
return return
selected_row = self._get_selected_row() selected_row = self._get_selected_row()
if selected_row is not None and selected_row < self.rowCount() - 1: if next:
starting_row = selected_row + 1 if selected_row is not None and selected_row < self.rowCount() - 1:
starting_row = selected_row + 1
else:
starting_row = 0
else: else:
starting_row = 0 if selected_row is not None and selected_row > 0:
starting_row = selected_row - 1
else:
starting_row = self.rowCount() - 1
wrapped = False wrapped = False
match_row = None match_row = None
@ -820,60 +826,37 @@ class PlaylistTab(QTableWidget):
if note and needle in note.lower(): if note and needle in note.lower():
match_row = row match_row = row
break break
row += 1 if next:
if wrapped and row >= starting_row: row += 1
break if wrapped and row >= starting_row:
if row >= self.rowCount(): break
row = 0 if row >= self.rowCount():
wrapped = True row = 0
wrapped = True
else:
row -= 1
if wrapped and row <= starting_row:
break
if row < 0:
row = self.rowCount() - 1
wrapped = True
if match_row is not None: if match_row is not None:
self.selectRow(row) self.selectRow(row)
def search_next(self) -> None:
"""
Select next row containg self.search_string.
"""
self._search(next=True)
def search_previous(self) -> None: def search_previous(self) -> None:
""" """
Select previous row containg self.search_string. Start from Select previous row containg self.search_string.
top selected row if there is one, else from top.
Wrap at last row.
""" """
if not self.search_text: self._search(next=False)
return
selected_row = self._get_selected_row()
if selected_row is not None and selected_row > 0:
starting_row = selected_row - 1
else:
starting_row = self.rowCount() - 1
wrapped = False
match_row = None
row = starting_row
needle = self.search_text.lower()
while True:
# Check for match in title, artist or notes
title = self._get_row_title(row)
if title and needle in title.lower():
match_row = row
break
artist = self._get_row_title(row)
if artist and needle in artist.lower():
match_row = row
break
note = self._get_row_note(row)
if note and needle in note.lower():
match_row = row
break
row -= 1
if wrapped and row <= starting_row:
break
if row < 0:
row = self.rowCount() - 1
wrapped = True
if match_row is not None:
self.selectRow(row)
def select_next_row(self) -> None: def select_next_row(self) -> None:
""" """