From cb40fd0a5781d438c3846f4f009a8a1e336fea71 Mon Sep 17 00:00:00 2001 From: tsi-billypom Date: Tue, 17 Sep 2024 12:01:43 -0400 Subject: [PATCH] headers --- components/MusicTable.py | 9 +++-- components/ResizableHeaderView.py | 55 +++++++++++++++++++++++++++++++ components/__init__.py | 1 + main.py | 14 ++------ ui.py | 4 +-- ui.ui | 2 +- 6 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 components/ResizableHeaderView.py diff --git a/components/MusicTable.py b/components/MusicTable.py index ddc22ec..e43b809 100644 --- a/components/MusicTable.py +++ b/components/MusicTable.py @@ -32,6 +32,7 @@ from components.LyricsWindow import LyricsWindow from components.AddToPlaylistWindow import AddToPlaylistWindow from components.MetadataWindow import MetadataWindow +from components.ResizableHeaderView import ResizableHeaderView from main import Worker from utils.batch_delete_filepaths_from_database import ( batch_delete_filepaths_from_database, @@ -93,6 +94,9 @@ class MusicTable(QTableView): "TDRC", None, ] + # Header stuff... + header = ResizableHeaderView(Qt.Horizontal, self) + self.setHorizontalHeader(header) # hide the id column self.hideColumn(0) # db names of headers @@ -113,8 +117,9 @@ class MusicTable(QTableView): self.setup_keyboard_shortcuts() def resizeEvent(self, e: typing.Optional[QResizeEvent]) -> None: - assert e is not None - return super().resizeEvent(e) + if e is None: + raise Exception + super().resizeEvent(e) def contextMenuEvent(self, a0): """Right-click context menu for rows in Music Table""" diff --git a/components/ResizableHeaderView.py b/components/ResizableHeaderView.py new file mode 100644 index 0000000..041be95 --- /dev/null +++ b/components/ResizableHeaderView.py @@ -0,0 +1,55 @@ +from PyQt5.QtWidgets import QHeaderView +from PyQt5.QtCore import Qt +import configparser + + +class ResizableHeaderView(QHeaderView): + def __init__(self, orientation, parent=None): + super().__init__(orientation, parent) + self.config = configparser.ConfigParser() + self.config.read("config.ini") + # 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(parent.model.columnCount() - 1): + self.setColumnWidth(i, int(table_view_column_widths[i])) + self.setSectionsMovable(True) + self.setSectionResizeMode(QHeaderView.Interactive) + self.setStretchLastSection(True) + self.min_section_size = 50 + self.default_column_proportions = [1, 1, 1, 1, 1, 1, 1, 1] + + def set_default_column_proportions(self, proportions): + self.default_column_proportions = proportions + + 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): + total_width = self.width() + column_count = self.count() + if not self.default_column_proportions: + self.default_column_proportions = [1] * column_count + + # Calculate the total proportion + total_proportion = sum(self.default_column_proportions) + + # Calculate sizes based on proportions + sizes = [ + max(self.min_section_size, int(total_width * prop / total_proportion)) + for prop in self.default_column_proportions + ] + + # Adjust sizes to fit exactly + extra = total_width - sum(sizes) + for i in range(abs(extra)): + sizes[i % column_count] += 1 if extra > 0 else -1 + + # Apply sizes + for i in range(column_count): + self.resizeSection(i, sizes[i]) diff --git a/components/__init__.py b/components/__init__.py index 57ba057..9ea4c60 100644 --- a/components/__init__.py +++ b/components/__init__.py @@ -9,3 +9,4 @@ from .AddToPlaylistWindow import AddToPlaylistWindow from .CreatePlaylistWindow import CreatePlaylistWindow from .PlaylistsPane import PlaylistsPane from .ExportPlaylistWindow import ExportPlaylistWindow +from .ResizableHeaderView import ResizableHeaderView diff --git a/main.py b/main.py index 0655529..ca5eacc 100644 --- a/main.py +++ b/main.py @@ -46,6 +46,7 @@ from components import ( AudioVisualizer, CreatePlaylistWindow, ExportPlaylistWindow, + ResizableHeaderView, ) # Create ui.py file from Qt Designer @@ -237,7 +238,6 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): ) # 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 - # self.albumGraphicsView.albumArtDeleted.connect( # self.delete_album_art_for_selected_songs # ) @@ -246,15 +246,6 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self ) # for drag & drop functionality self.tableView.handleProgressSignal.connect(self.handle_progress) - # set column widths - # 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.tableView.model.columnCount() - 1): - self.tableView.setColumnWidth(i, int(table_view_column_widths[i])) - # dont extend last column past table view border - self.tableView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) - self.tableView.horizontalHeader().setStretchLastSection(True) def reload_config(self) -> None: """does what it says""" @@ -276,7 +267,8 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): # Save the config with open("config.ini", "w") as configfile: self.config.write(configfile) - super().closeEvent(a0) + if a0 is not None: + super().closeEvent(a0) def show_status_bar_message(self, message: str, timeout: int | None = None) -> None: """ diff --git a/ui.py b/ui.py index a7104cf..17c09c1 100644 --- a/ui.py +++ b/ui.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file 'ui.ui' # -# Created by: PyQt5 UI code generator 5.15.10 +# Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. @@ -160,7 +160,7 @@ class Ui_MainWindow(object): self.verticalLayout_3.setStretch(2, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) - self.menubar.setGeometry(QtCore.QRect(0, 0, 1152, 41)) + self.menubar.setGeometry(QtCore.QRect(0, 0, 1152, 21)) self.menubar.setObjectName("menubar") self.menuFile = QtWidgets.QMenu(self.menubar) self.menuFile.setObjectName("menuFile") diff --git a/ui.ui b/ui.ui index 15a128d..af74898 100644 --- a/ui.ui +++ b/ui.ui @@ -284,7 +284,7 @@ 0 0 1152 - 41 + 21