# core from core.services.subscriptions.subscriptions_by_protocol import paying_for_tickets, get_current_billing_data from core.controllers.profile_state.data_filters import ALL_CURRENCIES from core.models.enum_models import Currency from core.services.payment_phase.payment_state import PaymentData # gui from gui.v2.ui.pages.Page import Page, DataError import os from PyQt6 import QtCore from PyQt6.QtWidgets import QLabel, QPushButton, QButtonGroup from PyQt6.QtWidgets import QVBoxLayout, QWidget, QSpacerItem, QSizePolicy from PyQt6.QtGui import QIcon from PyQt6.QtCore import QSize class CurrencySelectionPage(Page): def __init__(self, page_stack, main_window=None, parent=None): super().__init__("Select Currency", page_stack, main_window, parent) self.update_status = main_window self.selected_duration = None # Map to link the button object to its currency string value self.button_to_currency = {} self.create_initial_elements() self.button_reverse.setVisible(True) self.button_next.setVisible(False) def create_initial_elements(self): self.title = QLabel("Payment Method", self) self.title.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter) self.button_reverse.clicked.connect(self.reverse) # container widget to hold the 'block' of buttons self.button_container = QWidget(self) # Position of the entire group, not each individual button self.button_container.setGeometry(545, 75, 185, 400) # Vertical Layout self.layout = QVBoxLayout(self.button_container) # margins avoid cutting off image: self.layout.setContentsMargins(0, 0, 0, 0) # Consistent gap between buttons self.layout.setSpacing(20) self.layout.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter) self.buttonGroup = QButtonGroup(self) # loop through enums and get the relevant image, for currency_val in ALL_CURRENCIES: button = QPushButton(self) # size required for the image button.setFixedSize(185, 75) button.setIconSize(QSize(190, 120)) button.setCheckable(True) button.setIcon(QIcon(os.path.join(self.btn_path, f"{currency_val}.png"))) button.clicked.connect(self.on_currency_selected) self.button_to_currency[button] = currency_val self.buttonGroup.addButton(button) self.layout.addWidget(button) def on_currency_selected(self): """Sets the value selected to the singleton used across pages""" profile_id = int(self.update_status.current_profile_id) # get the singleton (this was created on a previous page) print("doing the new method") data_result = get_current_billing_data(profile_id=profile_id) if not data_result.valid: message = data_result.message if not message: message = "Error fetching data" self.show_a_popup(str(message)) # get the singleton (this was created on a previous page) billing_singleton = data_result.data if not billing_singleton: message = "Error, no billing data" self.show_a_popup(str(message)) # prior direct method: # self.billing_data = PaymentData(instance_id=profile_id) # set the value as an Enum to be used in a different page, billing_singleton.currency = self._get_selected_currency() self.update_status.update_status('Loading payment details...') self.custom_window.navigator.navigate("payment_details") def _get_selected_currency(self): selected_button = self.buttonGroup.checkedButton() # Retrieve the value from the map using the button object as the key raw_selection = self.button_to_currency.get(selected_button) # convert to enum: return Currency(raw_selection) def reverse(self): if paying_for_tickets(): self.custom_window.navigator.navigate("plan_picker") else: self.custom_window.navigator.navigate("duration_selection")