Implement carts

This commit is contained in:
Keith Edmunds 2022-10-21 22:41:38 +01:00
parent a649fa8c59
commit 1899aac9ae
6 changed files with 256 additions and 215 deletions

View File

@ -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":
"""

View File

@ -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"<CartButton(cart_id={self.cart_id} "
f"path={self.path}, is_playing={self.is_playing}>"
)
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:

View File

@ -6,103 +6,125 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>171</height>
<width>564</width>
<height>148</height>
</rect>
</property>
<property name="windowTitle">
<string>Carts</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>200</x>
<y>140</y>
<width>171</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>76</width>
<height>19</height>
</rect>
</property>
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="buddy">
<cstring>lineEditName</cstring>
</property>
</widget>
<widget class="QLineEdit" name="lineEditName">
<property name="geometry">
<rect>
<x>90</x>
<y>60</y>
<width>291</width>
<height>27</height>
</rect>
</property>
<property name="inputMask">
<string/>
</property>
</widget>
<widget class="QLabel" name="lblPath">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>371</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>xxx</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="btnFile">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<width>100</width>
<height>27</height>
</rect>
</property>
<property name="text">
<string>&amp;File</string>
</property>
</widget>
<widget class="QCheckBox" name="chkEnabled">
<property name="geometry">
<rect>
<x>280</x>
<y>100</y>
<width>104</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>&amp;Enabled</string>
</property>
</widget>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="maximumSize">
<size>
<width>56</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>&amp;Name:</string>
</property>
<property name="buddy">
<cstring>lineEditName</cstring>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QLineEdit" name="lineEditName">
<property name="inputMask">
<string/>
</property>
</widget>
</item>
<item row="0" column="3">
<widget class="QCheckBox" name="chkEnabled">
<property name="text">
<string>&amp;Enabled</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="maximumSize">
<size>
<width>56</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>File:</string>
</property>
<property name="buddy">
<cstring>lblPath</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="lblPath">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>301</width>
<height>41</height>
</size>
</property>
<property name="text">
<string/>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QPushButton" name="btnFile">
<property name="maximumSize">
<size>
<width>31</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="2" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>116</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="2" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>

View File

@ -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", "..."))

View File

@ -861,16 +861,10 @@ padding-left: 8px;</string>
<addaction name="action_About"/>
<addaction name="actionDebug"/>
</widget>
<widget class="QMenu" name="menu_Carts">
<property name="title">
<string>&amp;Carts</string>
</property>
</widget>
<addaction name="menuFile"/>
<addaction name="menuPlaylist"/>
<addaction name="menuSearc_h"/>
<addaction name="menuHelp"/>
<addaction name="menu_Carts"/>
</widget>
<widget class="QStatusBar" name="statusbar">
<property name="enabled">

View File

@ -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"))