From 240f449f0a61f87265f4b502dcf5d55f8c058a2b Mon Sep 17 00:00:00 2001 From: tsi-billypom Date: Tue, 17 Sep 2024 16:52:36 -0400 Subject: [PATCH] working on stopping headers from leaving viewport on resize --- components/MusicTable.py | 32 ++++++++++++++++++++++++----- components/ResizableHeaderView.py | 34 ++++++++----------------------- main.py | 9 ++++++-- ui.py | 1 + ui.ui | 3 +++ 5 files changed, 46 insertions(+), 33 deletions(-) diff --git a/components/MusicTable.py b/components/MusicTable.py index e43b809..dbfe78e 100644 --- a/components/MusicTable.py +++ b/components/MusicTable.py @@ -13,16 +13,17 @@ from PyQt5.QtWidgets import ( QAction, QHeaderView, QMenu, + QSizePolicy, QTableView, QShortcut, QMessageBox, QAbstractItemView, ) from PyQt5.QtCore import ( + Qt, QAbstractItemModel, QModelIndex, QThreadPool, - Qt, pyqtSignal, QTimer, ) @@ -95,8 +96,8 @@ class MusicTable(QTableView): None, ] # Header stuff... - header = ResizableHeaderView(Qt.Horizontal, self) - self.setHorizontalHeader(header) + # header = ResizableHeaderView(Qt.Horizontal, self) + # self.setHorizontalHeader(header) # hide the id column self.hideColumn(0) # db names of headers @@ -105,21 +106,42 @@ class MusicTable(QTableView): self.songChanged = None self.selected_song_filepath = "" self.current_song_filepath = "" - # self.tableView.resizeColumnsToContents() + table_view_column_widths = str(self.config["table"]["column_widths"]).split(",") + for i in range(self.model.columnCount() - 1): + self.setColumnWidth(i, int(table_view_column_widths[i])) + self.horizontalHeader().setStretchLastSection(True) + self.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive) + self.horizontalHeader().setCascadingSectionResizes(True) + # CONNECTIONS self.clicked.connect(self.set_selected_song_filepath) - # doubleClicked is a built in event for QTableView - we listen for this event and run set_current_song_filepath self.doubleClicked.connect(self.set_current_song_filepath) self.enterKey.connect(self.set_current_song_filepath) self.deleteKey.connect(self.delete_songs) self.model.dataChanged.connect(self.on_cell_data_changed) # editing cells self.model.layoutChanged.connect(self.restore_scroll_position) + self.horizontalHeader().sectionResized.connect(self.header_was_resized) + # Final actions self.load_music_table() self.setup_keyboard_shortcuts() def resizeEvent(self, e: typing.Optional[QResizeEvent]) -> None: + print(f"QTableView size: {self.size().width()}") if e is None: raise Exception super().resizeEvent(e) + self.setMaximumSize(self.size().width(), self.size().height()) + + def header_was_resized(self, logicalIndex, oldSize, newSize): + # super().sectionResized(logicalIndex, oldSize, newSize) + self.adjust_section_sizes() + + def adjust_section_sizes(self): + column_count = self.model.columnCount() + total_width = 0 + + for i in range(self.model.columnCount()): + total_width += self.columnWidth(i) + print(f"total_width = {total_width}") def contextMenuEvent(self, a0): """Right-click context menu for rows in Music Table""" diff --git a/components/ResizableHeaderView.py b/components/ResizableHeaderView.py index 041be95..9c3af97 100644 --- a/components/ResizableHeaderView.py +++ b/components/ResizableHeaderView.py @@ -8,19 +8,14 @@ class ResizableHeaderView(QHeaderView): 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(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 + self.setSectionResizeMode(QHeaderView.Interactive) def resizeEvent(self, e): super().resizeEvent(e) @@ -31,25 +26,12 @@ class ResizableHeaderView(QHeaderView): self.adjust_section_sizes() def adjust_section_sizes(self): - total_width = self.width() column_count = self.count() + total_width = 0 + + for i in range(self.parent.model.columnCount()): + total_width += self.parent.model.columnWidth(i) + print(f"total_width = {total_width}") + 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/main.py b/main.py index ca5eacc..658be64 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import sys import logging from subprocess import run import qdarktheme - +import typing from pyqtgraph import mkBrush from mutagen.id3 import ID3 from mutagen.id3._frames import APIC @@ -34,7 +34,7 @@ from PyQt5.QtCore import ( QRunnable, ) from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QAudioProbe -from PyQt5.QtGui import QCloseEvent, QPixmap +from PyQt5.QtGui import QCloseEvent, QPixmap, QResizeEvent from utils import ( scan_for_music, delete_and_create_library_database, @@ -255,6 +255,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): """Returns the threadpool instance""" return self.threadpool + def resizeEvent(self, a0: typing.Optional[QResizeEvent]) -> None: + """Do something when the window resizes""" + if a0 is not None: + return super().resizeEvent(a0) + def closeEvent(self, a0: QCloseEvent | None) -> None: """Save settings when closing the application""" # MusicTable/tableView column widths diff --git a/ui.py b/ui.py index 17c09c1..99ac7d5 100644 --- a/ui.py +++ b/ui.py @@ -100,6 +100,7 @@ class Ui_MainWindow(object): self.hLayoutHead.setStretch(2, 6) self.verticalLayout.addLayout(self.hLayoutHead) self.hLayoutMusicTable = QtWidgets.QHBoxLayout() + self.hLayoutMusicTable.setSizeConstraint(QtWidgets.QLayout.SetMaximumSize) self.hLayoutMusicTable.setContentsMargins(0, -1, 0, -1) self.hLayoutMusicTable.setObjectName("hLayoutMusicTable") self.playlistTreeView = PlaylistsPane(self.centralwidget) diff --git a/ui.ui b/ui.ui index af74898..87c41a2 100644 --- a/ui.ui +++ b/ui.ui @@ -166,6 +166,9 @@ + + QLayout::SetMaximumSize + 0