from PyQt6.QtWidgets import QApplication from PyQt6.QtGui import QPixmap, QPainter, QColor, QFont, QTransform from PyQt6.QtCore import Qt import os def create_button_image(first_line, fallback_image_path, second_line=None, existing_icon_path=None): # 1. Determine Base Image if existing_icon_path: # Wrap in str() to convert Path object to string base_image = QPixmap(str(existing_icon_path)) if base_image.isNull(): # Wrap in str() here as well base_image = QPixmap(str(fallback_image_path)) draw_location_text = False else: # Wrap in str() here as well base_image = QPixmap(str(fallback_image_path)) draw_location_text = True if base_image.isNull(): return None painter = QPainter(base_image) try: # 2. Draw Location Name if draw_location_text: font_size = 12 app_font = QApplication.font() app_font.setPointSize(font_size) app_font.setWeight(QFont.Weight.Bold) # Dynamic scaling based on text length text_length = len(first_line) if text_length <= 10: font_stretch, tw_scale, th_scale = 100, 1.1, 1.4 elif text_length <= 15: font_stretch, tw_scale, th_scale = 90, 1.0, 2.0 else: font_stretch, tw_scale, th_scale = 100, 1.1, 4.5 app_font.setStretch(font_stretch) painter.setFont(app_font) # Font size shrink-to-fit logic text_rect = painter.fontMetrics().boundingRect(first_line) while text_rect.width() > 110 and font_size > 5: font_size -= 1 app_font.setPointSize(font_size) painter.setFont(app_font) text_rect = painter.fontMetrics().boundingRect(first_line) # Position calculations x = ((base_image.width() - text_rect.width()) // 2) - 30 y = ((base_image.height() + text_rect.height()) // 2) - 15 transform = QTransform() transform.scale(tw_scale, th_scale) painter.setTransform(transform) x_scaled, y_scaled = int(x / tw_scale), int(y / th_scale) # Shadow and Main Text painter.setPen(QColor(0, 0, 0)) painter.drawText(x_scaled + 1, y_scaled + 1, first_line) painter.setPen(QColor(0, 255, 255)) painter.drawText(x_scaled, y_scaled, first_line) painter.setTransform(QTransform()) # 3. Draw second_line Text if second_line: painter.setPen(QColor(128, 128, 128)) second_line_font = QApplication.font() second_line_font.setPointSize(7) second_line_font.setWeight(QFont.Weight.Normal) painter.setFont(second_line_font) p_rect = painter.fontMetrics().boundingRect(second_line) px = ((base_image.width() - p_rect.width()) // 2) - 30 py = 50 painter.drawText(int(px) + 5, int(py) + 2, second_line) finally: painter.end() return base_image def create_resolution_button_image(btn_path: str, resolution_text: str): """Create the button image for a given text of screen resolution""" template_path = os.path.join( btn_path, "resolution_template_button.png") if os.path.exists(template_path): base_image = QPixmap(template_path) if 'Resolution' in resolution_text: base_image = base_image.scaled(195, 50) font_size = 10 else: base_image = base_image.scaled(187, 75) font_size = 13 else: base_image = QPixmap(185, 75) base_image.fill(QColor(44, 62, 80)) font_size = 13 painter = QPainter(base_image) painter.setRenderHint(QPainter.RenderHint.Antialiasing) font = QFont() font.setPointSize(font_size) font.setBold(True) painter.setFont(font) painter.setPen(QColor(0, 255, 255)) text_rect = base_image.rect() painter.drawText(text_rect, Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignVCenter, resolution_text) painter.end() return base_image