Put section headers in row 2

Bug in Qt means automatically setting row height doesn't take into
account row spans, so putting headers in narrow column makes for tall
rows.
This commit is contained in:
Keith Edmunds 2022-08-24 17:33:22 +01:00
parent 7819e863eb
commit f3ccab513b

View File

@ -56,6 +56,7 @@ from models import (
) )
start_time_re = re.compile(r"@\d\d:\d\d:\d\d") start_time_re = re.compile(r"@\d\d:\d\d:\d\d")
HEADER_NOTES_COLUMN = 2
class RowMeta: class RowMeta:
@ -224,7 +225,7 @@ class PlaylistTab(QTableWidget):
# rows. Check and fix: # rows. Check and fix:
for row in range(drop_row, drop_row + len(rows_to_move)): for row in range(drop_row, drop_row + len(rows_to_move)):
if not self._get_row_track_id(row): if not self._get_row_track_id(row):
self.setSpan(row, 1, 1, len(columns)) self.setSpan(row, HEADER_NOTES_COLUMN, 1, len(columns))
# Scroll to drop zone # Scroll to drop zone
self.scrollToItem(self.item(row, 1)) self.scrollToItem(self.item(row, 1))
@ -419,8 +420,8 @@ class PlaylistTab(QTableWidget):
editor: QWidget, editor: QWidget,
hint: QAbstractItemDelegate.EndEditHint) -> None: hint: QAbstractItemDelegate.EndEditHint) -> None:
""" """
Override QAbstractItemView.closeEditor to enable play controls Override PySide2.QAbstractItemView.closeEditor to enable
and update display. play controls and update display.
""" """
# update_display to update start times, such as when a note has # update_display to update start times, such as when a note has
@ -437,7 +438,7 @@ class PlaylistTab(QTableWidget):
trigger: QAbstractItemView.EditTrigger, trigger: QAbstractItemView.EditTrigger,
event: QEvent) -> bool: event: QEvent) -> bool:
""" """
Override QAbstractItemView.edit to catch when editing starts Override PySide2.QAbstractItemView.edit to catch when editing starts
""" """
result = super(PlaylistTab, self).edit(index, trigger, event) result = super(PlaylistTab, self).edit(index, trigger, event)
@ -461,16 +462,16 @@ class PlaylistTab(QTableWidget):
self.edit_cell_type = "row_notes" self.edit_cell_type = "row_notes"
else: else:
# Can't edit other columns # Can't edit other columns
return return False
# Check whether we're editing a notes row for later # Check whether we're editing a notes row for later
if self.edit_cell_type == "row_notes": if self.edit_cell_type == "row_notes":
note_column = columns['row_notes'].idx note_column = columns['row_notes'].idx
else: else:
# This is a section header. Text is always in row 1. # This is a section header.
if column != 1: if column != HEADER_NOTES_COLUMN:
return return False
note_column = 1 note_column = HEADER_NOTES_COLUMN
self.edit_cell_type = "row_notes" self.edit_cell_type = "row_notes"
# Disable play controls so that keyboard input doesn't # Disable play controls so that keyboard input doesn't
@ -629,12 +630,16 @@ class PlaylistTab(QTableWidget):
# Make empty items (row background won't be coloured without # Make empty items (row background won't be coloured without
# items present). Any notes should displayed starting in # items present). Any notes should displayed starting in
# column 0 # column 2 for now - bug in Qt means that when row size is
for i in range(2, len(columns)): # set, spanned columns are ignored, so put notes in col2
# (typically title).
for i in range(1, len(columns)):
if i == 2:
continue
self.setItem(row, i, QTableWidgetItem()) self.setItem(row, i, QTableWidgetItem())
self.setSpan(row, 1, 1, len(columns)) self.setSpan(row, HEADER_NOTES_COLUMN, 1, len(columns) - 1)
notes_item = QTableWidgetItem(row_data.note) notes_item = QTableWidgetItem(row_data.note)
self.setItem(row, 1, notes_item) self.setItem(row, HEADER_NOTES_COLUMN, notes_item)
# Save (no) track_id # Save (no) track_id
userdata_item.setData(self.ROW_TRACK_ID, 0) userdata_item.setData(self.ROW_TRACK_ID, 0)
@ -1383,7 +1388,7 @@ class PlaylistTab(QTableWidget):
if track_id: if track_id:
item_note = self.item(row, columns['row_notes'].idx) item_note = self.item(row, columns['row_notes'].idx)
else: else:
item_note = self.item(row, 1) item_note = self.item(row, HEADER_NOTES_COLUMN)
return item_note.text() return item_note.text()
def _get_row_start_time(self, row: int) -> Optional[datetime]: def _get_row_start_time(self, row: int) -> Optional[datetime]:
@ -1607,7 +1612,7 @@ class PlaylistTab(QTableWidget):
for i in range(2, len(columns)): for i in range(2, len(columns)):
self.item(row, i).setText("") self.item(row, i).setText("")
# Set note text in correct column for section head # Set note text in correct column for section head
self.item(row, 1).setText(plr.note) self.item(row, HEADER_NOTES_COLUMN).setText(plr.note)
# Remove row duration # Remove row duration
self._set_row_duration(row, 0) self._set_row_duration(row, 0)
# Remote track_id from row # Remote track_id from row
@ -1825,12 +1830,12 @@ class PlaylistTab(QTableWidget):
additional_text: str) -> None: additional_text: str) -> None:
"""Append additional_text to row display""" """Append additional_text to row display"""
# Column to update is either 1 for a section header or the # Column to update is either HEADER_NOTES_COLUMN for a section
# appropriate row_notes column for a track row # header or the appropriate row_notes column for a track row
if playlist_row.track_id: if playlist_row.track_id:
column = columns['row_notes'].idx column = columns['row_notes'].idx
else: else:
column = 1 column = HEADER_NOTES_COLUMN
# Update text # Update text
new_text = playlist_row.note + additional_text new_text = playlist_row.note + additional_text