Make getting current row safer

This commit is contained in:
Keith Edmunds 2024-12-30 08:39:01 +00:00
parent 68e524594d
commit e23f6e2cc8

View File

@ -666,6 +666,17 @@ class Window(QMainWindow, Ui_MainWindow):
return idx
def current_row_or_end(self) -> int:
"""
If a row or rows are selected, return the row number of the first
selected row otherwise return the row number for a new row at the
of the playlist.
"""
if self.current.selected_rows:
return self.current.selected_rows[0]
return self.current.base_model.rowCount()
def debug(self):
"""Invoke debugger"""
@ -848,7 +859,7 @@ class Window(QMainWindow, Ui_MainWindow):
# garbage collected while import threads are still running
self.importer = FileImporter(
self.current.base_model,
self.current.selected_rows[0],
self.current_row_or_end()
)
self.importer.do_import()
@ -862,27 +873,19 @@ class Window(QMainWindow, Ui_MainWindow):
dlg.resize(500, 100)
ok = dlg.exec()
if ok:
if self.current.selected_rows:
new_row_number = self.current.selected_rows[0]
else:
new_row_number = self.current.base_model.rowCount()
self.current.base_model.insert_row(
proposed_row_number=new_row_number,
proposed_row_number=self.current_row_or_end(),
note=dlg.textValue(),
)
def insert_track(self) -> None:
"""Show dialog box to select and add track from database"""
if self.current.selected_rows:
new_row_number = self.current.selected_rows[0]
else:
new_row_number = self.current.base_model.rowCount()
with db.Session() as session:
dlg = TrackSelectDialog(
parent=self,
session=session,
new_row_number=new_row_number,
new_row_number=self.current_row_or_end(),
base_model=self.current.base_model,
)
dlg.exec()
@ -1147,11 +1150,7 @@ class Window(QMainWindow, Ui_MainWindow):
return
to_playlist_model = self.current.base_model
selected_rows = self.current.selected_rows
if selected_rows:
destination_row = selected_rows[0]
else:
destination_row = self.current.base_model.rowCount()
destination_row = self.current_row_or_end()
# If we move a row to immediately under the current track, make
# that moved row the next track
@ -1516,7 +1515,10 @@ class Window(QMainWindow, Ui_MainWindow):
next track. If no next track, return None.
"""
row_number = self.current.selected_rows[0]
row_number: Optional[int] = None
if self.current.selected_rows:
row_number = self.current.selected_rows[0]
if row_number is None:
if track_sequence.next:
if track_sequence.next.track_id: