Changed dynamic text on buttons

This commit is contained in:
Your Name 2025-06-25 02:14:51 +01:00
parent 6ecf1d0615
commit 498891d655
3 changed files with 127 additions and 15 deletions

View file

@ -2717,6 +2717,7 @@ class HidetorPage(Page):
def __init__(self, page_stack, main_window, parent=None):
super().__init__("HideTor", page_stack, main_window, parent)
self.selected_location_icon = None
self.connection_manager = main_window.connection_manager
self.update_status = main_window
self.button_next.clicked.connect(self.go_selected)
self.button_reverse.setVisible(True)
@ -2733,11 +2734,17 @@ class HidetorPage(Page):
boton.setGeometry(*geometry)
boton.setIconSize(boton.size())
boton.setCheckable(True)
locations = self.connection_manager.get_location_info(icon_name)
if icon_name == 'my_14':
boton.setVisible(False)
boton.setIcon(QIcon(os.path.join(self.btn_path, f"button_{icon_name}.png")))
if boton.icon().isNull():
boton.setIcon(QIcon(os.path.join(self.btn_path, "default_location_button.png")))
fallback_path = os.path.join(self.btn_path, "default_location_button.png")
if locations and hasattr(locations, 'country_name'):
base_image = LocationPage.create_location_button_image(locations.country_name, fallback_path)
boton.setIcon(QIcon(base_image))
else:
boton.setIcon(QIcon(fallback_path))
self.buttons.append(boton)
self.buttonGroup.addButton(boton, j)
boton.clicked.connect(lambda _, location=icon_name: self.show_location(location))
@ -2999,6 +3006,7 @@ class LocationPage(Page):
self.selected_location_icon = None
self.update_status = main_window
self.button_reverse.setVisible(True)
self.connection_manager = main_window.connection_manager
self.button_reverse.clicked.connect(self.reverse)
self.display.setGeometry(QtCore.QRect(5, 10, 390, 520))
self.title.setGeometry(395, 40, 380, 40); self.title.setText("Pick a location")
@ -3019,16 +3027,23 @@ class LocationPage(Page):
def create_interface_elements(self, available_locations):
self.buttonGroup = QButtonGroup(self)
self.buttons = []
for j, (object_type, icon_name, geometry) in enumerate(available_locations):
boton = object_type(self)
boton.setGeometry(*geometry)
boton.setIconSize(boton.size())
boton.setCheckable(True)
locations = self.connection_manager.get_location_info(icon_name)
boton.setIcon(QIcon(os.path.join(self.btn_path, f"button_{icon_name}.png")))
if boton.icon().isNull():
fallback_path = os.path.join(self.btn_path, "default_location_button.png")
boton.setIcon(QIcon(fallback_path))
if locations and hasattr(locations, 'country_name'):
base_image = LocationPage.create_location_button_image(locations.country_name, fallback_path)
boton.setIcon(QIcon(base_image))
else:
boton.setIcon(QIcon(fallback_path))
self.buttons.append(boton)
self.buttonGroup.addButton(boton, j)
boton.clicked.connect(lambda _, location=icon_name: self.show_location(location))
@ -3061,6 +3076,71 @@ class LocationPage(Page):
else:
self.page_stack.setCurrentIndex(self.page_stack.indexOf(self.page_stack.findChild(BrowserPage)))
@staticmethod
def create_location_button_image(location_name, fallback_image_path):
base_image = QPixmap(fallback_image_path)
if base_image.isNull():
return base_image
painter = QPainter(base_image)
font_size = 12
app_font = QApplication.font()
app_font.setPointSize(font_size)
app_font.setWeight(QFont.Weight.Bold)
yscale = 0
text_length = len(location_name)
if text_length <= 10:
font_stretch = 100
text_width_scale = 1.1
text_height_scale = 1.4
elif text_length <= 15:
font_stretch = 90
text_width_scale = 1.0
text_height_scale = 2
else:
font_stretch = 100
text_width_scale = 1.1
text_height_scale = 4.5
yscale = 5
app_font.setStretch(font_stretch)
painter.setFont(app_font)
painter.setPen(QColor(0, 255, 255))
text_rect = painter.fontMetrics().boundingRect(location_name)
max_width = 110
while text_rect.width() > max_width:
font_size -= 1
app_font.setPointSize(font_size)
app_font.setWeight(QFont.Weight.Bold)
painter.setFont(app_font)
text_rect = painter.fontMetrics().boundingRect(location_name)
x = ((base_image.width() - text_rect.width()) // 2 ) - 20
y = ((base_image.height() + text_rect.height()) // 2 ) + yscale
from PyQt6.QtGui import QTransform
transform = QTransform()
transform.scale(text_width_scale, text_height_scale)
painter.setTransform(transform)
x_scaled = int(x / text_width_scale)
y_scaled = int(y / text_height_scale)
shadow_offset = 1
painter.setPen(QColor(0, 0, 0))
painter.drawText(x_scaled + shadow_offset, y_scaled + shadow_offset, location_name)
painter.setPen(QColor(0, 255, 255))
painter.drawText(x_scaled, y_scaled, location_name)
painter.end()
return base_image
class BrowserPage(Page):
def __init__(self, page_stack, main_window, parent=None):
@ -3156,7 +3236,7 @@ class BrowserPage(Page):
button.setIcon(QIcon(BrowserPage.create_browser_button_image(f"{browser_name} {version}", self.btn_path)))
if button.icon().isNull():
fallback_path = os.path.join(self.btn_path, "default_browser_button.png")
button.setIcon(QIcon(fallback_path))
button.setIcon(QIcon(BrowserPage.create_browser_button_image(f"{browser_name} {version}", fallback_path, True)))
self._apply_button_style(button)
self.buttons.append(button)
@ -3164,18 +3244,37 @@ class BrowserPage(Page):
button.clicked.connect(lambda _, b=icon_name: self.show_browser(b.replace(':', ' ')))
@staticmethod
def create_browser_button_image(browser_text, btn_path):
def create_browser_button_image(browser_text, btn_path, is_fallback=False):
browser_name, version = browser_text.split()[0], ' '.join(browser_text.split()[1:])
base_image = QPixmap(os.path.join(btn_path, f"{browser_name}_button.png"))
if is_fallback:
base_image = QPixmap(btn_path)
else:
base_image = QPixmap(os.path.join(btn_path, f"{browser_name}_button.png"))
painter = QPainter(base_image)
BrowserPage._setup_version_font(painter, version, base_image)
text_rect = painter.fontMetrics().boundingRect(version)
x = (base_image.width() - text_rect.width()) // 2 - 27
y = (base_image.height() + text_rect.height()) // 2 + 10
painter.drawText(x, y, version)
if is_fallback:
BrowserPage._setup_browser_name(painter, browser_name, base_image)
painter.end()
return base_image
@staticmethod
def _setup_browser_name(painter, browser_name, base_image):
font_size = 13
app_font = QApplication.font()
app_font.setPointSize(font_size)
app_font.setWeight(QFont.Weight.Bold)
painter.setFont(app_font)
painter.setPen(QColor(0, 255, 255))
text_rect = painter.fontMetrics().boundingRect(browser_name)
while text_rect.width() > base_image.width() * 0.6 and font_size > 6:
font_size -= 1
x = (base_image.width() - text_rect.width()) // 2 - 30
y = (base_image.height() + text_rect.height()) // 2 - 10
painter.drawText(x, y, browser_name)
@staticmethod
def _setup_version_font(painter, version, base_image):
font_size = 11
@ -3187,6 +3286,10 @@ class BrowserPage(Page):
painter.setFont(QFont('Arial', font_size))
text_rect = painter.fontMetrics().boundingRect(version)
x = (base_image.width() - text_rect.width()) // 2 - 27
y = (base_image.height() + text_rect.height()) // 2 + 10
painter.drawText(x, y, version)
def _apply_button_style(self, button):
button.setStyleSheet("""
QPushButton {
@ -3335,7 +3438,8 @@ class ResumePage(Page):
if os.path.exists(icon_path):
label.setPixmap(QPixmap(icon_path))
if label.pixmap().isNull():
label.setPixmap(QPixmap(os.path.join(self.btn_path, "default_location_button.png")))
locations = self.connection_manager.get_location_info(icon_name)
label.setPixmap(LocationPage.create_location_button_image(locations.country_name, os.path.join(self.btn_path, "default_location_button.png")))
elif object_type == QLineEdit:
self.line_edit = object_type(self) # Define line_edit como un atributo de la clase ResumePage
self.line_edit.setGeometry(*geometry)
@ -3377,7 +3481,9 @@ class ResumePage(Page):
parent_label.setGeometry(*geometry)
parent_label.setPixmap(base_image)
if parent_label.pixmap().isNull():
parent_label.setPixmap(QPixmap(os.path.join(self.btn_path, "default_browser_button.png")))
fallback_path = os.path.join(self.btn_path, "default_browser_button.png")
base_image = BrowserPage.create_browser_button_image(text, fallback_path, True)
parent_label.setPixmap(base_image)
parent_label.show()
self.labels_creados.append(parent_label)
elif item == 'location':
@ -3387,7 +3493,10 @@ class ResumePage(Page):
parent_label.setGeometry(*geometry)
parent_label.setPixmap(QPixmap(icon_path))
if parent_label.pixmap().isNull():
parent_label.setPixmap(QPixmap(os.path.join(self.btn_path, "default_location_button.png")))
locations = self.connection_manager.get_location_info(text)
fallback_path = os.path.join(self.btn_path, "default_location_button.png")
base_image = LocationPage.create_location_button_image(locations.country_name, fallback_path)
parent_label.setPixmap(base_image)
parent_label.show()
self.labels_creados.append(parent_label)
else:
@ -3790,13 +3899,16 @@ class EditorPage(Page):
else:
base_image = BrowserPage.create_browser_button_image(browser_value, self.btn_path)
if base_image.isNull():
base_image = QPixmap(os.path.join(self.btn_path, "default_browser_button.png"))
fallback_path = os.path.join(self.btn_path, "default_browser_button.png")
base_image = BrowserPage.create_browser_button_image(browser_value, fallback_path, True)
elif key == 'location':
current_value = f"{data_profile.get(key, '')}"
image_path = os.path.join(self.btn_path, f"button_{current_value}.png")
base_image = QPixmap(image_path)
if base_image.isNull():
base_image = QPixmap(os.path.join(self.btn_path, "default_location_button.png"))
locations = self.connection_manager.get_location_info(current_value)
fallback_path = os.path.join(self.btn_path, "default_location_button.png")
base_image = LocationPage.create_location_button_image(locations.country_name, fallback_path)
else:
image_path = os.path.join(self.btn_path, f"{data_profile.get(key, '')}_button.png")
current_value = data_profile.get(key, '')

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB