hydraveil-gui/gui/v2/ui/pages/profile_creation/resolution_widget.py

73 lines
2.6 KiB
Python

from gui.v2.presenters.data.resolution_data import process_custom_resolution, get_host_resolution_limits
from PyQt6.QtWidgets import QDialog, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox
from PyQt6.QtCore import pyqtSignal
class CustomResolutionDialog(QDialog):
"""Get custom resolution input from the user, and check it's validity with external functions"""
# Signal emits the final resolution string (e.g., "1920x1080")
resolution_selected = pyqtSignal(str)
def __init__(self, style_sheet, parent=None):
super().__init__(parent)
self.setWindowTitle("Custom Resolution")
self.setFixedSize(300, 150)
self.setModal(True)
self.setStyleSheet(style_sheet)
self.init_ui()
def init_ui(self):
# Get the max allowed values
limit_w, limit_h = get_host_resolution_limits()
layout = QVBoxLayout(self)
# Input Row
input_layout = QHBoxLayout()
self.width_input = QLineEdit()
self.width_input.setText(str(limit_w)) # Default to max allowed
self.height_input = QLineEdit()
self.height_input.setText(str(limit_h)) # Default to max allowed
input_layout.addWidget(QLabel("Width:"))
input_layout.addWidget(self.width_input)
input_layout.addWidget(QLabel("Height:"))
input_layout.addWidget(self.height_input)
# Button Row
button_layout = QHBoxLayout()
ok_button = QPushButton("OK")
cancel_button = QPushButton("Cancel")
button_layout.addWidget(cancel_button)
button_layout.addWidget(ok_button)
layout.addLayout(input_layout)
layout.addLayout(button_layout)
# Connections
ok_button.clicked.connect(self.validate_and_accept)
cancel_button.clicked.connect(self.reject)
self.width_input.returnPressed.connect(self.validate_and_accept)
self.height_input.returnPressed.connect(self.validate_and_accept)
def validate_and_accept(self):
# 1. Collect input
res_string = f"{self.width_input.text()}x{self.height_input.text()}"
# 2. Validate:
final_res, was_adjusted, is_valid = process_custom_resolution(res_string)
if not is_valid:
QMessageBox.information(self, "Notice", "Invalid format")
return
# 3. notification and the emission
if was_adjusted:
QMessageBox.information(self, "Notice", "Adjusted to fit host size")
self.resolution_selected.emit(final_res)
self.accept()