header resizing working - sort of

This commit is contained in:
billypom on debian 2024-09-17 21:42:41 -04:00
parent e8c27c782e
commit ec9f93add3
3 changed files with 40 additions and 69 deletions

View File

@ -95,9 +95,9 @@ class MusicTable(QTableView):
"TDRC", "TDRC",
None, None,
] ]
# Header stuff... #
header = ResizableHeaderView(Qt.Horizontal, self) # header = ResizableHeaderView(Qt.Horizontal, self)
self.setHorizontalHeader(header) # self.setHorizontalHeader(header)
# hide the id column # hide the id column
self.hideColumn(0) self.hideColumn(0)
# db names of headers # db names of headers
@ -106,11 +106,12 @@ class MusicTable(QTableView):
self.songChanged = None self.songChanged = None
self.selected_song_filepath = "" self.selected_song_filepath = ""
self.current_song_filepath = "" self.current_song_filepath = ""
# table_view_column_widths = str(self.config["table"]["column_widths"]).split(",")
# for i in range(self.model.columnCount() - 1): table_view_column_widths = str(self.config["table"]["column_widths"]).split(",")
# self.setColumnWidth(i, int(table_view_column_widths[i])) for i in range(self.model.columnCount() - 1):
# self.horizontalHeader().setStretchLastSection(True) self.setColumnWidth(i, int(table_view_column_widths[i]))
# self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive) self.horizontalHeader().setStretchLastSection(True)
self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
# self.horizontalHeader().setCascadingSectionResizes(True) # self.horizontalHeader().setCascadingSectionResizes(True)
# CONNECTIONS # CONNECTIONS
self.clicked.connect(self.set_selected_song_filepath) self.clicked.connect(self.set_selected_song_filepath)
@ -119,35 +120,41 @@ class MusicTable(QTableView):
self.deleteKey.connect(self.delete_songs) self.deleteKey.connect(self.delete_songs)
self.model.dataChanged.connect(self.on_cell_data_changed) # editing cells self.model.dataChanged.connect(self.on_cell_data_changed) # editing cells
self.model.layoutChanged.connect(self.restore_scroll_position) self.model.layoutChanged.connect(self.restore_scroll_position)
# self.horizontalHeader().sectionResized.connect(self.header_was_resized) self.horizontalHeader().sectionResized.connect(self.header_was_resized)
# Final actions # Final actions
self.load_music_table() self.load_music_table()
self.setup_keyboard_shortcuts() self.setup_keyboard_shortcuts()
def resizeEvent(self, e: typing.Optional[QResizeEvent]) -> None: def resizeEvent(self, e: typing.Optional[QResizeEvent]) -> None:
print(f"QTableView width: {self.size().width()}") """Do something when the QTableView is resized"""
if e is None: if e is None:
raise Exception raise Exception
super().resizeEvent(e) super().resizeEvent(e)
def header_was_resized(self, logicalIndex, oldSize, newSize): def header_was_resized(self, logicalIndex, oldSize, newSize):
self.adjust_section_sizes(logicalIndex, oldSize, newSize) """Handles keeping headers inside the viewport"""
# https://stackoverflow.com/questions/46775438/how-to-limit-qheaderview-size-when-resizing-sections
col_count = self.model.columnCount()
qtableview_width = self.size().width()
sum_of_cols = self.horizontalHeader().length()
def adjust_section_sizes(self, logicalIndex, oldSize, newSize): if sum != qtableview_width:
header_width = 0 # if not the last header
current_width = self.size().width() if logicalIndex < col_count:
next_header_size = self.horizontalHeader().sectionSize(logicalIndex + 1)
for i in range(self.model.columnCount()): # If it should shrink
header_width += self.columnWidth(i) if next_header_size > (sum_of_cols - qtableview_width):
if header_width > current_width: # shrink it
# we pushing headers too far self.horizontalHeader().resizeSection(
newSize = oldSize logicalIndex + 1,
print("NOOO") next_header_size - (sum_of_cols - qtableview_width),
print(f"total_width = {header_width}") )
print(f"logical index = {logicalIndex}") else:
print(f"old size = {oldSize}") # block the resize
print(f"new size = {newSize}") self.horizontalHeader().resizeSection(logicalIndex, oldSize)
# super().sectionResized(logicalIndex, oldSize, newSize) # else:
# if newSize > self.horizontalHeader().minimumSectionSize():
# self.horizontalHeader().resizeSection(logicalIndex, oldSize)
def contextMenuEvent(self, a0): def contextMenuEvent(self, a0):
"""Right-click context menu for rows in Music Table""" """Right-click context menu for rows in Music Table"""

View File

@ -1,36 +0,0 @@
from PyQt5.QtWidgets import QHeaderView
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot
import configparser
class ResizableHeaderView(QHeaderView):
def __init__(self, orientation, parent=None):
super().__init__(orientation, parent)
self.config = configparser.ConfigParser()
self.config.read("config.ini")
self.parent = parent
# FIXME: last column needs to not leave the screen when other columns become big...
# howwww
# table_view_column_widths = str(self.config["table"]["column_widths"]).split(",")
# for i in range(self.parent.model.columnCount() - 1):
# self.setColumnWidth(i, int(table_view_column_widths[i]))
# self.setStretchLastSection(True)
self.setSectionResizeMode(QHeaderView.Interactive)
self.setCascadingSectionResizes(True)
def resizeEvent(self, e):
super().resizeEvent(e)
self.adjust_section_sizes()
def sectionResized(self, logicalIndex, oldSize, newSize):
super().sectionResized(logicalIndex, oldSize, newSize)
self.adjust_section_sizes()
def adjust_section_sizes(self):
column_count = self.count()
total_width = 0
for i in range(column_count):
total_width += self.sectionSize(i)
print(f"total_width = {total_width}")

14
main.py
View File

@ -157,7 +157,6 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.probe: QAudioProbe = QAudioProbe() # Gets audio data self.probe: QAudioProbe = QAudioProbe() # Gets audio data
self.audio_visualizer: AudioVisualizer = AudioVisualizer(self.player) self.audio_visualizer: AudioVisualizer = AudioVisualizer(self.player)
self.current_volume: int = 50 self.current_volume: int = 50
# self.qapp = qapp
self.tableView.load_qapp(self) self.tableView.load_qapp(self)
self.albumGraphicsView.load_qapp(self) self.albumGraphicsView.load_qapp(self)
self.config.read("config.ini") self.config.read("config.ini")
@ -214,6 +213,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.actionDeleteLibrary.triggered.connect(self.clear_database) self.actionDeleteLibrary.triggered.connect(self.clear_database)
self.actionDeleteDatabase.triggered.connect(self.delete_database) self.actionDeleteDatabase.triggered.connect(self.delete_database)
# QTableView
self.tableView.viewport().installEventFilter(
self
) # for drag & drop functionality
## CONNECTIONS ## CONNECTIONS
# tableView # tableView
self.tableView.doubleClicked.connect( self.tableView.doubleClicked.connect(
@ -225,6 +229,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.tableView.playPauseSignal.connect( self.tableView.playPauseSignal.connect(
self.on_play_clicked self.on_play_clicked
) # Spacebar toggle play/pause signal ) # Spacebar toggle play/pause signal
self.tableView.handleProgressSignal.connect(self.handle_progress)
# playlistTreeView # playlistTreeView
self.playlistTreeView.playlistChoiceSignal.connect( self.playlistTreeView.playlistChoiceSignal.connect(
@ -237,16 +242,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
self.set_album_art_for_selected_songs self.set_album_art_for_selected_songs
) )
# FIXME: this should delete the album art for the current song - not all selected songs # FIXME: this should delete the album art for the current song - not all selected songs
# move functionality to remove album for selected songs to the batch metadata editor # Move functionality to remove album for selected songs to the batch metadata editor
# self.albumGraphicsView.albumArtDeleted.connect( # self.albumGraphicsView.albumArtDeleted.connect(
# self.delete_album_art_for_selected_songs # self.delete_album_art_for_selected_songs
# ) # )
self.tableView.viewport().installEventFilter(
self
) # for drag & drop functionality
self.tableView.handleProgressSignal.connect(self.handle_progress)
def reload_config(self) -> None: def reload_config(self) -> None:
"""does what it says""" """does what it says"""
self.config.read("config.ini") self.config.read("config.ini")