From 1899aac9ae3fd9cfe51ec58b29f99e2cf75eb4dc Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 21 Oct 2022 22:41:38 +0100 Subject: [PATCH] Implement carts --- app/models.py | 14 --- app/musicmuster.py | 178 ++++++++++++++++++--------------- app/ui/dlg_Cart.ui | 206 ++++++++++++++++++++++----------------- app/ui/dlg_cart_ui.py | 63 +++++++----- app/ui/main_window.ui | 6 -- app/ui/main_window_ui.py | 4 - 6 files changed, 256 insertions(+), 215 deletions(-) diff --git a/app/models.py b/app/models.py index 067d78b..4540c3b 100644 --- a/app/models.py +++ b/app/models.py @@ -81,20 +81,6 @@ class Carts(Base): session.add(self) session.commit() - @classmethod - def all_as_dict(cls, session: Session) -> dict: - """Return a dictionary of all carts keyed by cart number""" - - result = ( - session.execute( - select(Carts) - .order_by(Carts.cart_number) - ) - .scalars() - .all() - ) - return {a.id: a for a in result} - @classmethod def get_or_create(cls, session: Session, cart_number: int) -> "Carts": """ diff --git a/app/musicmuster.py b/app/musicmuster.py index bd050cd..24a6201 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -48,6 +48,42 @@ from ui.main_window_ui import Ui_MainWindow # type: ignore from utilities import check_db, update_bitrates +class CartButton(QPushButton): + """Button for playing carts""" + + def __init__(self, parent: QMainWindow, cart: Carts): + """Create a cart pushbutton and set it disabled""" + + super().__init__(parent) + self.parent = parent + self.cart_id = cart.id + self.path = cart.path + self.player = None + self.is_playing = False + + self.clicked.connect(self.parent.cart_click) + self.setEnabled(True) + self.setMinimumSize(QSize(147, 61)) + font = QFont() + font.setPointSize(14) + self.setFont(font) + self.setObjectName("cart_" + str(cart.cart_number)) + + def __repr__(self) -> str: + return ( + f"" + ) + + def event(self, event): + if event.type() == QEvent.MouseButtonRelease: + if event.button() == Qt.RightButton: + self.parent.cart_edit(self, event) + return True + + return super().event(event) + + class TrackData: def __init__(self, track): self.id = track.id @@ -104,50 +140,28 @@ class Window(QMainWindow, Ui_MainWindow): self.timer.start(Config.TIMER_MS) self.connect_signals_slots() - def cart_add(self) -> None: - """Add a cart""" - - pass - - def cart_button(self, cart: Carts) -> QPushButton: - """Create a cart pushbutton and set it disabled""" - - btn = QPushButton(self) - btn.setEnabled(True) - btn.setMinimumSize(QSize(147, 61)) - font = QFont() - font.setPointSize(14) - btn.setFont(font) - btn.setObjectName("cart_" + str(cart.cart_number)) - btn.setText(cart.name) - btn.clicked.connect(self.cart_click) - btn.setEnabled(False) - # Insert button on left of cart space after existing buttons - self.horizontalLayout_Carts.insertWidget(len(self.carts), btn) - - return btn - def cart_click(self) -> None: """Handle cart click""" - # print(f"cart_click({self.sender()=})") - # print(f"{self.carts[self.sender()]['name']}") btn = self.sender() - cart = self.carts[btn] - if helpers.file_is_readable(cart['path']): - cart['player'].play() - self.carts[btn]['playing'] = True + if helpers.file_is_readable(btn.path): + btn.player.play() + btn.is_playing = True colour = Config.COLOUR_CART_PLAYING else: colour = Config.COLOUR_CART_ERROR btn.setStyleSheet("background-color: " + colour + ";\n") - def cart_edit(self, cart_number: int) -> None: - """Edit carts""" + def cart_edit(self, btn: CartButton, event: QEvent): + """Handle context menu for cart button""" with Session() as session: - cart = Carts.get_or_create(session, cart_number) - dlg = CartDialog(self, cart, cart_number) + cart = session.query(Carts).get(btn.cart_id) + if cart is None: + log.ERROR("cart_edit: cart not found") + return + + dlg = CartDialog(parent=self, session=session, cart=cart) if dlg.exec(): name = dlg.ui.lineEditName.text() if not name: @@ -157,58 +171,71 @@ class Window(QMainWindow, Ui_MainWindow): if not path: QMessageBox.warning(self, "Error", "Filename required") return - cart.name = name - cart.path = path - if cart.path: + if cart.path and helpers.file_is_readable(cart.path): tags = helpers.get_tags(cart.path) cart.duration = tags['duration'] + cart.enabled = dlg.ui.chkEnabled.isChecked() + cart.name = name + cart.path = path + session.add(cart) session.commit() + self.cart_configure(cart, btn) + def carts_init(self) -> None: """Initialse carts data structures""" with Session() as session: - carts_in_db = Carts.all_as_dict(session) # Number carts from 1 for humanity for cart_number in range(1, Config.CARTS_COUNT + 1): - if cart_number in carts_in_db: - cart = carts_in_db[cart_number] - else: + cart = session.query(Carts).get(cart_number) + if cart is None: cart = Carts(session, cart_number, name=f"Cart #{cart_number}") - btn = self.cart_button(cart) + btn = CartButton(self, cart) + # Insert button on left of cart space starting at + # location zero + self.horizontalLayout_Carts.insertWidget(cart.id - 1, btn) + # Configure button + self.cart_configure(cart, btn) - self.carts[btn] = {} - self.carts[btn]['name'] = cart.name - self.carts[btn]['duration'] = cart.duration - self.carts[btn]['path'] = cart.path - self.carts[btn]['playing'] = False - player = self.music.VLC.media_player_new(cart.path) - player.audio_set_volume(Config.VOLUME_VLC_DEFAULT) - self.carts[btn]['player'] = player - if cart.path: - if helpers.file_is_readable(cart.path): - colour = Config.COLOUR_CART_READY - btn.setEnabled(True) - else: - colour = Config.COLOUR_CART_ERROR - else: - colour = Config.COLOUR_CART_UNCONFIGURED + def cart_configure(self, cart: Carts, btn: CartButton) -> None: + """Configure button with cart data""" - btn.setStyleSheet("background-color: " + colour + ";\n") + btn.setEnabled(False) + if cart.path: + if helpers.file_is_readable(cart.path): + colour = Config.COLOUR_CART_READY + btn.path = cart.path + btn.player = self.music.VLC.media_player_new(cart.path) + btn.player.audio_set_volume(Config.VOLUME_VLC_DEFAULT) + if cart.enabled: + btn.setEnabled(True) + else: + colour = Config.COLOUR_CART_ERROR + else: + colour = Config.COLOUR_CART_UNCONFIGURED + + btn.setStyleSheet("background-color: " + colour + ";\n") + btn.setText(cart.name) def cart_tick(self) -> None: """Cart clock actions""" - for btn, cart in self.carts.items(): - if cart['playing']: - if not cart['player'].is_playing(): + for i in range(self.horizontalLayout_Carts.count()): + btn = self.horizontalLayout_Carts.itemAt(i).widget() + if not btn: + continue + if btn.is_playing: + if not btn.player.is_playing(): # Cart has finished playing - cart['playing'] = False - cart['player'].set_position(0) + btn.is_playing = False + # Setting to position 0 doesn't seem to work + btn.player = self.music.VLC.media_player_new(btn.path) + btn.player.audio_set_volume(Config.VOLUME_VLC_DEFAULT) colour = Config.COLOUR_CART_READY btn.setStyleSheet("background-color: " + colour + ";\n") @@ -296,10 +323,8 @@ class Window(QMainWindow, Ui_MainWindow): def connect_signals_slots(self) -> None: self.action_About.triggered.connect(self.about) - self.actionAdd_cart.triggered.connect(self.cart_add) self.action_Clear_selection.triggered.connect(self.clear_selection) self.actionDebug.triggered.connect(self.debug) - self.actionAdd_cart.triggered.connect(self.cart_add) self.actionClosePlaylist.triggered.connect(self.close_playlist_tab) self.actionDownload_CSV_of_played_tracks.triggered.connect( self.download_played_tracks) @@ -1199,26 +1224,25 @@ class Window(QMainWindow, Ui_MainWindow): class CartDialog(QDialog): """Edit cart details""" - def __init__(self, parent: QMainWindow, - cart: Optional[Carts], - cart_number: int) -> None: + def __init__(self, parent: QMainWindow, session: Session, + cart: Carts) -> None: """ Manage carts """ super().__init__(parent) + self.parent = parent + self.session = session + self.ui = Ui_DialogCartEdit() self.ui.setupUi(self) - self.ui.chkEnabled.setChecked(True) - self.path = "" - - self.ui.windowTitle = "Edit Cart " + str(cart_number) - - if cart: - self.ui.lineEditName.setText(cart.name) - self.path = cart.path - + self.path = cart.path self.ui.lblPath.setText(self.path) + self.ui.lineEditName.setText(cart.name) + self.ui.chkEnabled.setChecked(cart.enabled) + + self.ui.windowTitle = "Edit Cart " + str(cart.id) + self.ui.btnFile.clicked.connect(self.choose_file) def choose_file(self) -> None: diff --git a/app/ui/dlg_Cart.ui b/app/ui/dlg_Cart.ui index 4eff2bd..41be25d 100644 --- a/app/ui/dlg_Cart.ui +++ b/app/ui/dlg_Cart.ui @@ -6,103 +6,125 @@ 0 0 - 400 - 171 + 564 + 148 Carts - - - - 200 - 140 - 171 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - - - - - 10 - 60 - 76 - 19 - - - - &Name: - - - lineEditName - - - - - - 90 - 60 - 291 - 27 - - - - - - - - - - 10 - 10 - 371 - 41 - - - - xxx - - - Qt::PlainText - - - true - - - - - - 10 - 100 - 100 - 27 - - - - &File - - - - - - 280 - 100 - 104 - 25 - - - - &Enabled - - + + + + + + 56 + 16777215 + + + + &Name: + + + lineEditName + + + + + + + + + + + + + + &Enabled + + + + + + + + 56 + 16777215 + + + + File: + + + lblPath + + + + + + + + 0 + 0 + + + + + 301 + 41 + + + + + + + Qt::PlainText + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + true + + + + + + + + 31 + 16777215 + + + + ... + + + + + + + Qt::Horizontal + + + + 116 + 20 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + diff --git a/app/ui/dlg_cart_ui.py b/app/ui/dlg_cart_ui.py index 9a97170..919d7ec 100644 --- a/app/ui/dlg_cart_ui.py +++ b/app/ui/dlg_cart_ui.py @@ -14,31 +14,50 @@ from PyQt5 import QtCore, QtGui, QtWidgets class Ui_DialogCartEdit(object): def setupUi(self, DialogCartEdit): DialogCartEdit.setObjectName("DialogCartEdit") - DialogCartEdit.resize(400, 171) + DialogCartEdit.resize(564, 148) + self.gridLayout = QtWidgets.QGridLayout(DialogCartEdit) + self.gridLayout.setObjectName("gridLayout") + self.label = QtWidgets.QLabel(DialogCartEdit) + self.label.setMaximumSize(QtCore.QSize(56, 16777215)) + self.label.setObjectName("label") + self.gridLayout.addWidget(self.label, 0, 0, 1, 1) + self.lineEditName = QtWidgets.QLineEdit(DialogCartEdit) + self.lineEditName.setInputMask("") + self.lineEditName.setObjectName("lineEditName") + self.gridLayout.addWidget(self.lineEditName, 0, 1, 1, 2) + self.chkEnabled = QtWidgets.QCheckBox(DialogCartEdit) + self.chkEnabled.setObjectName("chkEnabled") + self.gridLayout.addWidget(self.chkEnabled, 0, 3, 1, 1) + self.label_2 = QtWidgets.QLabel(DialogCartEdit) + self.label_2.setMaximumSize(QtCore.QSize(56, 16777215)) + self.label_2.setObjectName("label_2") + self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1) + self.lblPath = QtWidgets.QLabel(DialogCartEdit) + sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred) + sizePolicy.setHorizontalStretch(0) + sizePolicy.setVerticalStretch(0) + sizePolicy.setHeightForWidth(self.lblPath.sizePolicy().hasHeightForWidth()) + self.lblPath.setSizePolicy(sizePolicy) + self.lblPath.setMinimumSize(QtCore.QSize(301, 41)) + self.lblPath.setText("") + self.lblPath.setTextFormat(QtCore.Qt.PlainText) + self.lblPath.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) + self.lblPath.setWordWrap(True) + self.lblPath.setObjectName("lblPath") + self.gridLayout.addWidget(self.lblPath, 1, 1, 1, 1) + self.btnFile = QtWidgets.QPushButton(DialogCartEdit) + self.btnFile.setMaximumSize(QtCore.QSize(31, 16777215)) + self.btnFile.setObjectName("btnFile") + self.gridLayout.addWidget(self.btnFile, 1, 3, 1, 1) + spacerItem = QtWidgets.QSpacerItem(116, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + self.gridLayout.addItem(spacerItem, 2, 1, 1, 1) self.buttonBox = QtWidgets.QDialogButtonBox(DialogCartEdit) - self.buttonBox.setGeometry(QtCore.QRect(200, 140, 171, 32)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok) self.buttonBox.setObjectName("buttonBox") - self.label = QtWidgets.QLabel(DialogCartEdit) - self.label.setGeometry(QtCore.QRect(10, 60, 76, 19)) - self.label.setObjectName("label") - self.lineEditName = QtWidgets.QLineEdit(DialogCartEdit) - self.lineEditName.setGeometry(QtCore.QRect(90, 60, 291, 27)) - self.lineEditName.setInputMask("") - self.lineEditName.setObjectName("lineEditName") - self.lblPath = QtWidgets.QLabel(DialogCartEdit) - self.lblPath.setGeometry(QtCore.QRect(10, 10, 371, 41)) - self.lblPath.setTextFormat(QtCore.Qt.PlainText) - self.lblPath.setWordWrap(True) - self.lblPath.setObjectName("lblPath") - self.btnFile = QtWidgets.QPushButton(DialogCartEdit) - self.btnFile.setGeometry(QtCore.QRect(10, 100, 100, 27)) - self.btnFile.setObjectName("btnFile") - self.chkEnabled = QtWidgets.QCheckBox(DialogCartEdit) - self.chkEnabled.setGeometry(QtCore.QRect(280, 100, 104, 25)) - self.chkEnabled.setObjectName("chkEnabled") + self.gridLayout.addWidget(self.buttonBox, 2, 2, 1, 2) self.label.setBuddy(self.lineEditName) + self.label_2.setBuddy(self.lblPath) self.retranslateUi(DialogCartEdit) self.buttonBox.accepted.connect(DialogCartEdit.accept) # type: ignore @@ -49,6 +68,6 @@ class Ui_DialogCartEdit(object): _translate = QtCore.QCoreApplication.translate DialogCartEdit.setWindowTitle(_translate("DialogCartEdit", "Carts")) self.label.setText(_translate("DialogCartEdit", "&Name:")) - self.lblPath.setText(_translate("DialogCartEdit", "xxx")) - self.btnFile.setText(_translate("DialogCartEdit", "&File")) self.chkEnabled.setText(_translate("DialogCartEdit", "&Enabled")) + self.label_2.setText(_translate("DialogCartEdit", "File:")) + self.btnFile.setText(_translate("DialogCartEdit", "...")) diff --git a/app/ui/main_window.ui b/app/ui/main_window.ui index 9d1c073..7257ceb 100644 --- a/app/ui/main_window.ui +++ b/app/ui/main_window.ui @@ -861,16 +861,10 @@ padding-left: 8px; - - - &Carts - - - diff --git a/app/ui/main_window_ui.py b/app/ui/main_window_ui.py index b05f9e3..f9a5378 100644 --- a/app/ui/main_window_ui.py +++ b/app/ui/main_window_ui.py @@ -381,8 +381,6 @@ class Ui_MainWindow(object): self.menuSearc_h.setObjectName("menuSearc_h") self.menuHelp = QtWidgets.QMenu(self.menubar) self.menuHelp.setObjectName("menuHelp") - self.menu_Carts = QtWidgets.QMenu(self.menubar) - self.menu_Carts.setObjectName("menu_Carts") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setEnabled(True) @@ -534,7 +532,6 @@ class Ui_MainWindow(object): self.menubar.addAction(self.menuPlaylist.menuAction()) self.menubar.addAction(self.menuSearc_h.menuAction()) self.menubar.addAction(self.menuHelp.menuAction()) - self.menubar.addAction(self.menu_Carts.menuAction()) self.retranslateUi(MainWindow) self.tabPlaylist.setCurrentIndex(-1) @@ -573,7 +570,6 @@ class Ui_MainWindow(object): self.menuPlaylist.setTitle(_translate("MainWindow", "Sho&wtime")) self.menuSearc_h.setTitle(_translate("MainWindow", "&Search")) self.menuHelp.setTitle(_translate("MainWindow", "&Help")) - self.menu_Carts.setTitle(_translate("MainWindow", "&Carts")) self.actionPlay_next.setText(_translate("MainWindow", "&Play next")) self.actionPlay_next.setShortcut(_translate("MainWindow", "Return")) self.actionSkipToNext.setText(_translate("MainWindow", "Skip to &next"))