Fix moving rows

Also fix associated tests.
Fixes #234
This commit is contained in:
Keith Edmunds 2024-04-28 12:54:32 +01:00
parent 2a55cd9c92
commit 1ce64804fb
2 changed files with 17 additions and 8 deletions

View File

@ -856,15 +856,23 @@ class PlaylistModel(QAbstractTableModel):
# Build a {current_row_number: new_row_number} dictionary # Build a {current_row_number: new_row_number} dictionary
row_map: dict[int, int] = {} row_map: dict[int, int] = {}
# The destination row number will need to be reduced by the
# number of rows being move from above the destination row
# otherwise rows below the destination row will end up above the
# moved rows.
adjusted_to_row = to_row_number - len([a for a in from_rows if a <= to_row_number])
# Put the from_row row numbers into the row_map. Ultimately the # Put the from_row row numbers into the row_map. Ultimately the
# total number of elements in the playlist doesn't change, so # total number of elements in the playlist doesn't change, so
# check that adding the moved rows starting at to_row won't # check that adding the moved rows starting at to_row won't
# overshoot the end of the playlist. # overshoot the end of the playlist.
if to_row_number + len(from_rows) > len(self.playlist_rows): if adjusted_to_row + len(from_rows) > len(self.playlist_rows):
next_to_row = len(self.playlist_rows) - len(from_rows) next_to_row = len(self.playlist_rows) - len(from_rows)
else: else:
next_to_row = to_row_number next_to_row = adjusted_to_row
# zip iterates from_row and to_row simultaneously from the
# respective sequences inside zip()
for from_row, to_row in zip( for from_row, to_row in zip(
from_rows, range(next_to_row, next_to_row + len(from_rows)) from_rows, range(next_to_row, next_to_row + len(from_rows))
): ):
@ -873,7 +881,8 @@ class PlaylistModel(QAbstractTableModel):
# Move the remaining rows to the row_map. We want to fill it # Move the remaining rows to the row_map. We want to fill it
# before (if there are gaps) and after (likewise) the rows that # before (if there are gaps) and after (likewise) the rows that
# are moving. # are moving.
# This iterates old_row and new_row simultaneously. # zip iterates old_row and new_row simultaneously from the
# respective sequences inside zip()
for old_row, new_row in zip( for old_row, new_row in zip(
[x for x in self.playlist_rows.keys() if x not in from_rows], [x for x in self.playlist_rows.keys() if x not in from_rows],
[y for y in range(len(self.playlist_rows)) if y not in row_map.values()], [y for y in range(len(self.playlist_rows)) if y not in row_map.values()],

View File

@ -159,9 +159,9 @@ class TestMMMiscRowMove(unittest.TestCase):
elif row == 3: elif row == 3:
assert self.model.playlist_rows[row].note == str(4) assert self.model.playlist_rows[row].note == str(4)
elif row == 4: elif row == 4:
assert self.model.playlist_rows[row].note == str(5)
elif row == 5:
assert self.model.playlist_rows[row].note == str(3) assert self.model.playlist_rows[row].note == str(3)
elif row == 5:
assert self.model.playlist_rows[row].note == str(5)
def test_move_rows_test3(self): def test_move_rows_test3(self):
# move row 4 to row 3 # move row 4 to row 3
@ -208,7 +208,7 @@ class TestMMMiscRowMove(unittest.TestCase):
assert row in self.model.playlist_rows assert row in self.model.playlist_rows
assert self.model.playlist_rows[row].plr_rownum == row assert self.model.playlist_rows[row].plr_rownum == row
new_order.append(int(self.model.playlist_rows[row].note)) new_order.append(int(self.model.playlist_rows[row].note))
assert new_order == [0, 2, 3, 6, 7, 8, 9, 1, 4, 5, 10] assert new_order == [0, 2, 3, 6, 7, 1, 4, 5, 10, 8, 9]
def test_move_rows_test6(self): def test_move_rows_test6(self):
# move rows [3, 6] → 5 # move rows [3, 6] → 5
@ -221,7 +221,7 @@ class TestMMMiscRowMove(unittest.TestCase):
assert row in self.model.playlist_rows assert row in self.model.playlist_rows
assert self.model.playlist_rows[row].plr_rownum == row assert self.model.playlist_rows[row].plr_rownum == row
new_order.append(int(self.model.playlist_rows[row].note)) new_order.append(int(self.model.playlist_rows[row].note))
assert new_order == [0, 1, 2, 4, 5, 3, 6, 7, 8, 9, 10] assert new_order == [0, 1, 2, 4, 3, 6, 5, 7, 8, 9, 10]
def test_move_rows_test7(self): def test_move_rows_test7(self):
# move rows [3, 5, 6] → 8 # move rows [3, 5, 6] → 8
@ -234,7 +234,7 @@ class TestMMMiscRowMove(unittest.TestCase):
assert row in self.model.playlist_rows assert row in self.model.playlist_rows
assert self.model.playlist_rows[row].plr_rownum == row assert self.model.playlist_rows[row].plr_rownum == row
new_order.append(int(self.model.playlist_rows[row].note)) new_order.append(int(self.model.playlist_rows[row].note))
assert new_order == [0, 1, 2, 4, 7, 8, 9, 10, 3, 5, 6] assert new_order == [0, 1, 2, 4, 7, 3, 5, 6, 8, 9, 10]
def test_move_rows_test8(self): def test_move_rows_test8(self):
# move rows [7, 8, 10] → 5 # move rows [7, 8, 10] → 5