hydraveil-gui/gui/v2/ui/pages/currency_selection_page.py

94 lines
3.4 KiB
Python
Executable file

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
from gui.v2.ui.pages.Page import Page
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)
self.billing_data = PaymentData(instance_id=profile_id)
# set the value as an Enum to be used in a different page,
self.billing_data.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):
self.custom_window.navigator.navigate("duration_selection")