From a649fa8c592608c5631d874f26dc7eb406b08573 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 15 Oct 2022 20:15:30 +0100 Subject: [PATCH] WIP: Carts --- app/config.py | 2 ++ app/models.py | 12 ++++++------ app/musicmuster.py | 24 +++++++++++++++++++----- app/ui/main_window.ui | 3 +-- app/ui/main_window_ui.py | 3 +-- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/app/config.py b/app/config.py index aa837d4..cdcf179 100644 --- a/app/config.py +++ b/app/config.py @@ -10,12 +10,14 @@ class Config(object): BITRATE_OK_THRESHOLD = 300 CHECK_AUDACITY_AT_STARTUP = True CART_DIRECTORY = "/home/kae/radio/CartTracks" + CARTS_COUNT = 10 COLOUR_BITRATE_LOW = "#ffcdd2" COLOUR_BITRATE_MEDIUM = "#ffeb6f" COLOUR_BITRATE_OK = "#dcedc8" COLOUR_CART_ERROR = "#dc3545" COLOUR_CART_PLAYING = "#248f24" COLOUR_CART_READY = "#ffc107" + COLOUR_CART_UNCONFIGURED = "#f2f2f2" COLOUR_CURRENT_HEADER = "#d4edda" COLOUR_CURRENT_PLAYLIST = "#7eca8f" COLOUR_CURRENT_TAB = "#248f24" diff --git a/app/models.py b/app/models.py index 2ecc0e3..067d78b 100644 --- a/app/models.py +++ b/app/models.py @@ -63,12 +63,12 @@ class Carts(Base): def __repr__(self) -> str: return ( - f"" ) def __init__(self, session: Session, cart_number: int, name: str = None, - duration: int = None, path: str = None, + duration: int = None, path: str = None, enabled: bool = True) -> None: """Create new cart""" @@ -82,18 +82,18 @@ class Carts(Base): session.commit() @classmethod - def enabled_carts(cls, session: Session) -> List["Carts"]: - """Return a list of all active carts""" + def all_as_dict(cls, session: Session) -> dict: + """Return a dictionary of all carts keyed by cart number""" - return ( + result = ( session.execute( select(Carts) - .where(Carts.enabled.is_(True)) .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 94b7719..bd050cd 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -110,7 +110,7 @@ class Window(QMainWindow, Ui_MainWindow): pass def cart_button(self, cart: Carts) -> QPushButton: - """Create a cart pushbutton""" + """Create a cart pushbutton and set it disabled""" btn = QPushButton(self) btn.setEnabled(True) @@ -121,6 +121,7 @@ class Window(QMainWindow, Ui_MainWindow): 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) @@ -169,7 +170,15 @@ class Window(QMainWindow, Ui_MainWindow): """Initialse carts data structures""" with Session() as session: - for cart in Carts.enabled_carts(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 = Carts(session, cart_number, + name=f"Cart #{cart_number}") + btn = self.cart_button(cart) self.carts[btn] = {} @@ -180,10 +189,15 @@ class Window(QMainWindow, Ui_MainWindow): player = self.music.VLC.media_player_new(cart.path) player.audio_set_volume(Config.VOLUME_VLC_DEFAULT) self.carts[btn]['player'] = player - if helpers.file_is_readable(cart.path): - colour = Config.COLOUR_CART_READY + 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_ERROR + colour = Config.COLOUR_CART_UNCONFIGURED + btn.setStyleSheet("background-color: " + colour + ";\n") def cart_tick(self) -> None: diff --git a/app/ui/main_window.ui b/app/ui/main_window.ui index 14d72ee..9d1c073 100644 --- a/app/ui/main_window.ui +++ b/app/ui/main_window.ui @@ -865,7 +865,6 @@ padding-left: 8px; &Carts - @@ -1158,7 +1157,7 @@ padding-left: 8px; - Add &cart... + Edit cart &1... diff --git a/app/ui/main_window_ui.py b/app/ui/main_window_ui.py index 869924a..b05f9e3 100644 --- a/app/ui/main_window_ui.py +++ b/app/ui/main_window_ui.py @@ -530,7 +530,6 @@ class Ui_MainWindow(object): self.menuSearc_h.addAction(self.actionSelect_previous_track) self.menuHelp.addAction(self.action_About) self.menuHelp.addAction(self.actionDebug) - self.menu_Carts.addAction(self.actionAdd_cart) self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuPlaylist.menuAction()) self.menubar.addAction(self.menuSearc_h.menuAction()) @@ -629,6 +628,6 @@ class Ui_MainWindow(object): self.actionSave_as_template.setText(_translate("MainWindow", "Save as template...")) self.actionNew_from_template.setText(_translate("MainWindow", "New from template...")) self.actionDebug.setText(_translate("MainWindow", "Debug")) - self.actionAdd_cart.setText(_translate("MainWindow", "Add &cart...")) + self.actionAdd_cart.setText(_translate("MainWindow", "Edit cart &1...")) from infotabs import InfoTabs import icons_rc