diff --git a/ui/designer/components/chartSelector.ui b/ui/designer/components/chartSelector.ui index 61ad9c6..e92db98 100644 --- a/ui/designer/components/chartSelector.ui +++ b/ui/designer/components/chartSelector.ui @@ -138,71 +138,7 @@ 0 - - - - 0 - 0 - - - - PAST - - - false - - - - - - - - 0 - 0 - - - - PRESENT - - - false - - - - - - - - 0 - 0 - - - - FUTURE - - - false - - - - - - - false - - - - 0 - 0 - - - - BEYOND - - - false - - + @@ -238,14 +174,12 @@ - RatingClassRadioButton - QRadioButton -
ui.implements.components.ratingClassRadioButton
+ RatingClassSelector + QWidget +
ui.implements.components.ratingClassSelector
+ 1
- - bydButton - diff --git a/ui/designer/components/chartSelector_ui.py b/ui/designer/components/chartSelector_ui.py index 6410692..b518c22 100644 --- a/ui/designer/components/chartSelector_ui.py +++ b/ui/designer/components/chartSelector_ui.py @@ -3,7 +3,7 @@ ################################################################################ ## Form generated from reading UI file 'chartSelector.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! ################################################################################ @@ -19,7 +19,7 @@ from PySide6.QtWidgets import (QApplication, QComboBox, QGroupBox, QHBoxLayout, QLabel, QLineEdit, QPushButton, QSizePolicy, QSpacerItem, QVBoxLayout, QWidget) -from ui.implements.components.ratingClassRadioButton import RatingClassRadioButton +from ui.implements.components.ratingClassSelector import RatingClassSelector class Ui_ChartSelector(object): def setupUi(self, ChartSelector): @@ -104,45 +104,10 @@ class Ui_ChartSelector(object): self.horizontalLayout_2 = QHBoxLayout(self.ratingClassGroupBox) self.horizontalLayout_2.setSpacing(0) self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") - self.pstButton = RatingClassRadioButton(self.ratingClassGroupBox) - self.pstButton.setObjectName(u"pstButton") - sizePolicy1 = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.MinimumExpanding) - sizePolicy1.setHorizontalStretch(0) - sizePolicy1.setVerticalStretch(0) - sizePolicy1.setHeightForWidth(self.pstButton.sizePolicy().hasHeightForWidth()) - self.pstButton.setSizePolicy(sizePolicy1) - self.pstButton.setText(u"PAST") - self.pstButton.setAutoExclusive(False) + self.ratingClassSelector = RatingClassSelector(self.ratingClassGroupBox) + self.ratingClassSelector.setObjectName(u"ratingClassSelector") - self.horizontalLayout_2.addWidget(self.pstButton) - - self.prsButton = RatingClassRadioButton(self.ratingClassGroupBox) - self.prsButton.setObjectName(u"prsButton") - sizePolicy1.setHeightForWidth(self.prsButton.sizePolicy().hasHeightForWidth()) - self.prsButton.setSizePolicy(sizePolicy1) - self.prsButton.setText(u"PRESENT") - self.prsButton.setAutoExclusive(False) - - self.horizontalLayout_2.addWidget(self.prsButton) - - self.ftrButton = RatingClassRadioButton(self.ratingClassGroupBox) - self.ftrButton.setObjectName(u"ftrButton") - sizePolicy1.setHeightForWidth(self.ftrButton.sizePolicy().hasHeightForWidth()) - self.ftrButton.setSizePolicy(sizePolicy1) - self.ftrButton.setText(u"FUTURE") - self.ftrButton.setAutoExclusive(False) - - self.horizontalLayout_2.addWidget(self.ftrButton) - - self.bydButton = RatingClassRadioButton(self.ratingClassGroupBox) - self.bydButton.setObjectName(u"bydButton") - self.bydButton.setEnabled(False) - sizePolicy1.setHeightForWidth(self.bydButton.sizePolicy().hasHeightForWidth()) - self.bydButton.setSizePolicy(sizePolicy1) - self.bydButton.setText(u"BEYOND") - self.bydButton.setAutoExclusive(False) - - self.horizontalLayout_2.addWidget(self.bydButton) + self.horizontalLayout_2.addWidget(self.ratingClassSelector) self.mainVerticalLayout.addWidget(self.ratingClassGroupBox) @@ -151,11 +116,11 @@ class Ui_ChartSelector(object): self.resultsHorizontalLayout.setObjectName(u"resultsHorizontalLayout") self.resultLabel = QLabel(ChartSelector) self.resultLabel.setObjectName(u"resultLabel") - sizePolicy2 = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) - sizePolicy2.setHorizontalStretch(0) - sizePolicy2.setVerticalStretch(0) - sizePolicy2.setHeightForWidth(self.resultLabel.sizePolicy().hasHeightForWidth()) - self.resultLabel.setSizePolicy(sizePolicy2) + sizePolicy1 = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) + sizePolicy1.setHorizontalStretch(0) + sizePolicy1.setVerticalStretch(0) + sizePolicy1.setHeightForWidth(self.resultLabel.sizePolicy().hasHeightForWidth()) + self.resultLabel.setSizePolicy(sizePolicy1) self.resultLabel.setText(u"...") self.resultLabel.setTextFormat(Qt.RichText) diff --git a/ui/implements/components/chartSelector.py b/ui/implements/components/chartSelector.py index 687c48b..db4bb2e 100644 --- a/ui/implements/components/chartSelector.py +++ b/ui/implements/components/chartSelector.py @@ -22,23 +22,6 @@ class ChartSelector(Ui_ChartSelector, QWidget): self.db = Database() self.setupUi(self) - self.pstButton.setColors(QColor("#399bb2"), QColor("#f0f8fa")) - self.prsButton.setColors(QColor("#809955"), QColor("#f7f9f4")) - self.ftrButton.setColors(QColor("#702d60"), QColor("#f7ebf4")) - self.bydButton.setColors(QColor("#710f25"), QColor("#f9ced8")) - self.__RATING_CLASS_BUTTONS = [ - self.pstButton, - self.prsButton, - self.ftrButton, - self.bydButton, - ] - self.pstButton.clicked.connect(self.selectRatingClass) - self.prsButton.clicked.connect(self.selectRatingClass) - self.ftrButton.clicked.connect(self.selectRatingClass) - self.bydButton.clicked.connect(self.selectRatingClass) - self.deselectAllRatingClassButtons() - self.updateRatingClassButtonsEnabled([]) - self.previousPackageButton.clicked.connect( lambda: self.quickSwitchSelection("previous", "package") ) @@ -71,10 +54,7 @@ class ChartSelector(Ui_ChartSelector, QWidget): self.packageComboBox.setItemDelegate(DescriptionDelegate(self.packageComboBox)) self.songIdComboBox.setItemDelegate(DescriptionDelegate(self.songIdComboBox)) - self.pstButton.toggled.connect(self.valueChanged) - self.prsButton.toggled.connect(self.valueChanged) - self.ftrButton.toggled.connect(self.valueChanged) - self.bydButton.toggled.connect(self.valueChanged) + self.ratingClassSelector.valueChanged.connect(self.valueChanged) self.packageComboBox.currentIndexChanged.connect(self.valueChanged) self.songIdComboBox.currentIndexChanged.connect(self.valueChanged) @@ -104,7 +84,7 @@ class ChartSelector(Ui_ChartSelector, QWidget): def value(self): packageId = self.packageComboBox.currentData() songId = self.songIdComboBox.currentData() - ratingClass = self.selectedRatingClass() + ratingClass = self.ratingClassSelector.value() if packageId and songId and isinstance(ratingClass, int): return self.db.get_chart(songId, ratingClass) @@ -185,11 +165,11 @@ class ChartSelector(Ui_ChartSelector, QWidget): @Slot(int) def on_songIdComboBox_currentIndexChanged(self, index: int): - rating_classes = [] + ratingClasses = [] if index > -1: charts = self.db.get_charts_by_song_id(self.songIdComboBox.currentData()) - rating_classes = [chart.rating_class for chart in charts] - self.updateRatingClassButtonsEnabled(rating_classes) + ratingClasses = [chart.rating_class for chart in charts] + self.ratingClassSelector.setButtonsEnabled(ratingClasses) @Slot() def on_resetButton_clicked(self): @@ -219,7 +199,7 @@ class ChartSelector(Ui_ChartSelector, QWidget): # QMessageBox return - self.selectRatingClass(chart.rating_class) + self.ratingClassSelector.select(chart.rating_class) @Slot(QModelIndex) def fuzzySearchCompleterSetSelection(self, index: QModelIndex): @@ -228,35 +208,3 @@ class ChartSelector(Ui_ChartSelector, QWidget): self.fuzzySearchLineEdit.clear() self.fuzzySearchLineEdit.clearFocus() - - def ratingClassButtons(self): - return self.__RATING_CLASS_BUTTONS - - def selectedRatingClass(self): - for i, button in enumerate(self.__RATING_CLASS_BUTTONS): - if button.isChecked(): - return i - - def updateRatingClassButtonsEnabled(self, rating_classes: list[int]): - for i, button in enumerate(self.__RATING_CLASS_BUTTONS): - if i in rating_classes: - button.setEnabled(True) - else: - button.setChecked(False) - button.setEnabled(False) - - def deselectAllRatingClassButtons(self): - [button.setChecked(False) for button in self.__RATING_CLASS_BUTTONS] - - @Slot() - def selectRatingClass(self, rating_class: int | None = None): - if type(rating_class) == int and rating_class in range(4): - self.deselectAllRatingClassButtons() - button = self.__RATING_CLASS_BUTTONS[rating_class] - if button.isEnabled(): - button.setChecked(True) - else: - button = self.sender() - if isinstance(button, RatingClassRadioButton) and button.isEnabled(): - self.deselectAllRatingClassButtons() - button.setChecked(True) diff --git a/ui/implements/components/ratingClassSelector.py b/ui/implements/components/ratingClassSelector.py new file mode 100644 index 0000000..764b2eb --- /dev/null +++ b/ui/implements/components/ratingClassSelector.py @@ -0,0 +1,100 @@ +from typing import Type + +from PySide6.QtCore import Signal +from PySide6.QtGui import QColor +from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget + +from ui.implements.components.ratingClassRadioButton import RatingClassRadioButton + + +class RatingClassSelector(QWidget): + valueChanged = Signal() + selected = Signal(int) + + def __init__( + self, parent=None, layout: Type[QHBoxLayout] | Type[QVBoxLayout] = QHBoxLayout + ): + super().__init__(parent) + + self.preferredLayout = layout(self) + self.preferredLayout.setSpacing(0) + + self.pstButton = RatingClassRadioButton(self) + self.pstButton.setObjectName("self.pstButton") + self.pstButton.setText("PAST") + self.pstButton.setAutoExclusive(False) + self.preferredLayout.addWidget(self.pstButton) + + self.prsButton = RatingClassRadioButton(self) + self.prsButton.setObjectName("self.prsButton") + self.prsButton.setText("PRESENT") + self.prsButton.setAutoExclusive(False) + self.preferredLayout.addWidget(self.prsButton) + + self.ftrButton = RatingClassRadioButton(self) + self.ftrButton.setObjectName("self.ftrButton") + self.ftrButton.setText("FUTURE") + self.ftrButton.setAutoExclusive(False) + self.preferredLayout.addWidget(self.ftrButton) + + self.bydButton = RatingClassRadioButton(self) + self.bydButton.setObjectName("self.bydButton") + self.bydButton.setText("BEYOND") + self.bydButton.setAutoExclusive(False) + self.preferredLayout.addWidget(self.bydButton) + + self.buttons = [self.pstButton, self.prsButton, self.ftrButton, self.bydButton] + self.pstButton.setColors(QColor("#399bb2"), QColor("#f0f8fa")) + self.prsButton.setColors(QColor("#809955"), QColor("#f7f9f4")) + self.ftrButton.setColors(QColor("#702d60"), QColor("#f7ebf4")) + self.bydButton.setColors(QColor("#710f25"), QColor("#f9ced8")) + + self.pstButton.clicked.connect(self.select) + self.prsButton.clicked.connect(self.select) + self.ftrButton.clicked.connect(self.select) + self.bydButton.clicked.connect(self.select) + self.reset() + self.setButtonsEnabled([]) + + def value(self): + for i, button in enumerate(self.buttons): + if button.isChecked(): + return i + return None + + def reset(self): + for button in self.buttons: + button.setChecked(False) + self.valueChanged.emit() + + def setButtonsEnabled(self, ratingClasses: list[int]): + for i, button in enumerate(self.buttons): + if i in ratingClasses: + button.setEnabled(True) + else: + button.setChecked(False) + button.setEnabled(False) + self.valueChanged.emit() + + def select(self, ratingClass: int | bool | None = None): + if not ( + type(ratingClass) == int + or type(ratingClass) == bool + or ratingClass is not None + ): + return + + if ratingClass is None or isinstance(ratingClass, bool): + button = self.sender() + elif ratingClass in range(4): + button = self.buttons[ratingClass] + else: + return + + if not button.isEnabled(): + return + + self.reset() + button.setChecked(True) + self.valueChanged.emit() + self.selected.emit(self.buttons.index(button))