From 1955a7963a08386694cc9185bcdeae82cbc114b3 Mon Sep 17 00:00:00 2001 From: 283375 Date: Wed, 6 Sep 2023 00:31:20 +0800 Subject: [PATCH] wip: chore --- ui/designer/tabs/tabInputScore.ui | 6 - ui/designer/tabs/tabInputScore_ui.py | 7 +- ui/extends/shared/delegates/chartDelegate.py | 2 +- ui/extends/shared/layouts/flowLayout.py | 126 ++++++++++++++++++ ui/extends/shared/models/tables/b30.py | 2 +- ui/implements/components/chartSelector.py | 2 +- ui/implements/components/songIdSelector.py | 2 +- .../tabs/tabTools/tabTools_InfoLookup.py | 8 +- 8 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 ui/extends/shared/layouts/flowLayout.py diff --git a/ui/designer/tabs/tabInputScore.ui b/ui/designer/tabs/tabInputScore.ui index 6cf7a43..3c61652 100644 --- a/ui/designer/tabs/tabInputScore.ui +++ b/ui/designer/tabs/tabInputScore.ui @@ -16,12 +16,6 @@ - - - 0 - 0 - - tab.selectChart diff --git a/ui/designer/tabs/tabInputScore_ui.py b/ui/designer/tabs/tabInputScore_ui.py index 68c34a7..3172a4d 100644 --- a/ui/designer/tabs/tabInputScore_ui.py +++ b/ui/designer/tabs/tabInputScore_ui.py @@ -3,7 +3,7 @@ ################################################################################ ## Form generated from reading UI file 'tabInputScore.ui' ## -## Created by: Qt User Interface Compiler version 6.5.0 +## Created by: Qt User Interface Compiler version 6.5.2 ## ## WARNING! All changes made in this file will be lost when recompiling UI file! ################################################################################ @@ -31,11 +31,6 @@ class Ui_TabInputScore(object): self.verticalLayout.setObjectName(u"verticalLayout") self.groupBox = QGroupBox(TabInputScore) self.groupBox.setObjectName(u"groupBox") - sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding) - sizePolicy.setHorizontalStretch(0) - sizePolicy.setVerticalStretch(0) - sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth()) - self.groupBox.setSizePolicy(sizePolicy) self.verticalLayout_2 = QVBoxLayout(self.groupBox) self.verticalLayout_2.setObjectName(u"verticalLayout_2") self.verticalLayout_2.setContentsMargins(0, 0, 0, 0) diff --git a/ui/extends/shared/delegates/chartDelegate.py b/ui/extends/shared/delegates/chartDelegate.py index c4184a9..ebdb674 100644 --- a/ui/extends/shared/delegates/chartDelegate.py +++ b/ui/extends/shared/delegates/chartDelegate.py @@ -165,7 +165,7 @@ class ChartDelegate(TextSegmentDelegate): def updateEditorGeometry(self, editor: QWidget, option, index: QModelIndex) -> None: editor.move(editor.pos() + option.rect.topLeft()) editor.setMaximumWidth(option.rect.width()) - + keepWidgetInScreen(editor) def setEditorData(self, editor: ChartSelectorDelegateWrapper, index: QModelIndex): diff --git a/ui/extends/shared/layouts/flowLayout.py b/ui/extends/shared/layouts/flowLayout.py new file mode 100644 index 0000000..a2d1683 --- /dev/null +++ b/ui/extends/shared/layouts/flowLayout.py @@ -0,0 +1,126 @@ +# Adapted from https://doc.qt.io/qt-6/qtwidgets-layouts-flowlayout-example.html + +from PySide6.QtCore import QPoint, QRect, QSize, Qt +from PySide6.QtWidgets import QLayout, QLayoutItem, QSizePolicy, QStyle, QWidget + + +class FlowLayout(QLayout): + def __init__(self, parent=None, margin=0, hSpacing=0, vSpacing=0): + super().__init__(parent) + self.hSpace = hSpacing + self.vSpace = vSpacing + self.setContentsMargins(margin, margin, margin, margin) + self.itemList: list[QLayoutItem] = [] + + def __del__(self): + item = self.takeAt(0) + while item: + del item + item = self.takeAt(0) + + def addItem(self, item: QLayoutItem): + self.itemList.append(item) + + def horizontalSpacing(self): + return ( + self.hSpace + if self.hSpace >= 0 + else self.smartSpacing(QStyle.PixelMetric.PM_LayoutHorizontalSpacing) + ) + + def verticalSpacing(self): + return ( + self.vSpace + if self.vSpace >= 0 + else self.smartSpacing(QStyle.PixelMetric.PM_LayoutVerticalSpacing) + ) + + def count(self): + return len(self.itemList) + + def itemAt(self, index: int): + return self.itemList[index] if 0 <= index < len(self.itemList) else None + + def takeAt(self, index): + return self.itemList.pop(index) if 0 <= index < len(self.itemList) else None + + # Qt::Orientations FlowLayout::expandingDirections() const + # { + # return { }; + # } + def expandingDirections(self): + return Qt.Orientations(0) + + def hasHeightForWidth(self): + return True + + def heightForWidth(self, width: int): + return self.doLayout(QRect(0, 0, width, 0), True) + + def setGeometry(self, rect: QRect): + super().setGeometry(rect) + self.doLayout(rect, False) + + def sizeHint(self): + return self.minimumSize() + + def minimumSize(self): + size = QSize() + for item in self.itemList: + size = size.expandedTo(item.minimumSize()) + + margins = self.contentsMargins() + size += QSize( + margins.left() + margins.right(), margins.top() + margins.bottom() + ) + return size + + def doLayout(self, rect: QRect, testOnly: bool): + margins = self.contentsMargins() + left = margins.left() + top = margins.top() + right = margins.right() + bottom = margins.bottom() + effectiveRect = rect.adjusted(+left, +top, -right, -bottom) + x = effectiveRect.x() + y = effectiveRect.y() + lineHeight = 0 + for item in self.itemList: + widget = item.widget() + spaceX = self.horizontalSpacing() + if spaceX == -1: + spaceX = widget.style().layoutSpacing( + QSizePolicy.ControlType.PushButton, + QSizePolicy.ControlType.PushButton, + ) + spaceY = self.verticalSpacing() + if spaceY == -1: + spaceY = widget.style().layoutSpacing( + QSizePolicy.ControlType.PushButton, + QSizePolicy.ControlType.PushButton, + ) + + nextX = x + item.sizeHint().width() + spaceX + if nextX - spaceX > effectiveRect.right() and lineHeight > 0: + x = effectiveRect.x() + y = y + lineHeight + spaceY + nextX = x + item.sizeHint().width() + spaceX + lineHeight = 0 + + if not testOnly: + item.setGeometry(QRect(QPoint(x, y), item.sizeHint())) + + x = nextX + lineHeight = max(lineHeight, item.sizeHint().height()) + return y + lineHeight - rect.y() + bottom + + def smartSpacing(self, pm: QStyle.PixelMetric): + parent = self.parent() + if not parent: + return -1 + elif parent.isWidgetType(): + parent: QWidget + return parent.style().pixelMetric(pm, None, parent) + else: + parent: QLayout + return parent.spacing() diff --git a/ui/extends/shared/models/tables/b30.py b/ui/extends/shared/models/tables/b30.py index ea248cb..e35c5ba 100644 --- a/ui/extends/shared/models/tables/b30.py +++ b/ui/extends/shared/models/tables/b30.py @@ -39,7 +39,7 @@ class DbB30TableModel(DbTableModel): ptts = [r.potential for r in results] for scoreId, ptt in zip(songIds, ptts): - score = self._db.get_score_by_id(scoreId) + score = self._db.get_score(scoreId) chart = self._db.get_chart(score.song_id, score.rating_class) self.beginInsertRows(QModelIndex(), self.rowCount(), self.rowCount()) diff --git a/ui/implements/components/chartSelector.py b/ui/implements/components/chartSelector.py index 3f8eb39..b2319bb 100644 --- a/ui/implements/components/chartSelector.py +++ b/ui/implements/components/chartSelector.py @@ -46,7 +46,7 @@ class ChartSelector(Ui_ChartSelector, QWidget): def updateResultLabel(self): chart = self.value() if isinstance(chart, Chart): - pack = self.db.get_pack_by_id(chart.set) + pack = self.db.get_pack(chart.set) texts = [ [ pack.name, diff --git a/ui/implements/components/songIdSelector.py b/ui/implements/components/songIdSelector.py index 2501182..2003d7f 100644 --- a/ui/implements/components/songIdSelector.py +++ b/ui/implements/components/songIdSelector.py @@ -112,7 +112,7 @@ class SongIdSelector(Ui_SongIdSelector, QWidget): isAppendPack = re.search(r"_append_.*$", pack.id) if isAppendPack: basePackId = re.sub(r"_append_.*$", "", pack.id) - basePackName = self.db.get_pack_by_id(basePackId).name + basePackName = self.db.get_pack(basePackId).name packName = f"{basePackName} - {pack.name}" else: packName = pack.name diff --git a/ui/implements/tabs/tabTools/tabTools_InfoLookup.py b/ui/implements/tabs/tabTools/tabTools_InfoLookup.py index 24bf835..68ea256 100644 --- a/ui/implements/tabs/tabTools/tabTools_InfoLookup.py +++ b/ui/implements/tabs/tabTools/tabTools_InfoLookup.py @@ -77,8 +77,8 @@ class TabTools_InfoLookup(Ui_TabTools_InfoLookup, QWidget): self.resetPackLabels() return - pack = self.db.get_pack_by_id(packId) - packLocalized = self.db.get_pack_localized_by_id(packId) + pack = self.db.get_pack(packId) + packLocalized = self.db.get_pack_localized(packId) name = self.getLocalizedItem(pack, packLocalized, "name") description = self.getLocalizedItem(pack, packLocalized, "description") @@ -98,8 +98,8 @@ class TabTools_InfoLookup(Ui_TabTools_InfoLookup, QWidget): self.resetSongLabels() return - song = self.db.get_song_by_id(songId) - songLocalized = self.db.get_song_localized_by_id(songId) + song = self.db.get_song(songId) + songLocalized = self.db.get_song_localized(songId) title = self.getLocalizedItem(song, songLocalized, "title") bgSideTexts = []