forked from achimrabus/polyscriptor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogo_handler.py
More file actions
208 lines (165 loc) · 6.52 KB
/
logo_handler.py
File metadata and controls
208 lines (165 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Logo Handler for Polyscriptor HTR Application
Manages application logo loading, fallback text logo, and icon generation.
"""
from pathlib import Path
from typing import Optional, Tuple
from PyQt6.QtGui import QPixmap, QIcon, QImage, QPainter, QFont, QColor
from PyQt6.QtCore import Qt, QSize
class LogoHandler:
"""Handles logo loading with fallback to text-based logo."""
# Logo file search paths (in order of preference)
LOGO_PATHS = [
"assets/logo.png",
"assets/polyscriptor_logo.png",
"assets/polyscript_logo.png", # Legacy support
"logo.png",
"polyscriptor_logo.png",
"polyscript_logo.png", # Legacy support
"assets/logo.svg",
"assets/polyscriptor_logo.svg",
"assets/polyscript_logo.svg", # Legacy support
]
# Supported image formats
SUPPORTED_FORMATS = ['.png', '.jpg', '.jpeg', '.svg', '.ico', '.bmp']
def __init__(self, base_dir: Optional[Path] = None):
"""
Initialize logo handler.
Args:
base_dir: Base directory for asset search (defaults to script directory)
"""
self.base_dir = base_dir or Path(__file__).parent
self._logo_pixmap: Optional[QPixmap] = None
self._logo_icon: Optional[QIcon] = None
def find_logo_file(self) -> Optional[Path]:
"""
Search for logo file in predefined paths.
Returns:
Path to logo file, or None if not found
"""
for logo_path in self.LOGO_PATHS:
full_path = self.base_dir / logo_path
if full_path.exists():
return full_path
# Search assets directory for any supported image
assets_dir = self.base_dir / "assets"
if assets_dir.exists():
for ext in self.SUPPORTED_FORMATS:
for file in assets_dir.glob(f"*{ext}"):
if 'logo' in file.stem.lower():
return file
return None
def create_text_logo(self, width: int = 400, height: int = 120) -> QPixmap:
"""
Create a text-based logo as fallback.
Args:
width: Logo width in pixels
height: Logo height in pixels
Returns:
QPixmap with text-based logo
"""
pixmap = QPixmap(width, height)
pixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(pixmap)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
# Gradient background (optional)
# gradient = QLinearGradient(0, 0, width, height)
# gradient.setColorAt(0, QColor(41, 128, 185)) # Blue
# gradient.setColorAt(1, QColor(142, 68, 173)) # Purple
# painter.fillRect(0, 0, width, height, gradient)
# Main text: "Polyscriptor"
font = QFont("Arial", 36, QFont.Weight.Bold)
painter.setFont(font)
painter.setPen(QColor(41, 128, 185)) # Blue
painter.drawText(0, 0, width, height * 0.6,
Qt.AlignmentFlag.AlignCenter, "Polyscriptor")
# Subtitle: "Multi-Engine HTR"
font_small = QFont("Arial", 14)
painter.setFont(font_small)
painter.setPen(QColor(127, 140, 141)) # Gray
painter.drawText(0, height * 0.5, width, height * 0.5,
Qt.AlignmentFlag.AlignCenter, "Multi-Engine HTR")
painter.end()
return pixmap
def create_icon_from_text(self, size: int = 64) -> QIcon:
"""
Create application icon from text.
Args:
size: Icon size in pixels
Returns:
QIcon with text-based icon
"""
pixmap = QPixmap(size, size)
pixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(pixmap)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
# Background circle
painter.setBrush(QColor(41, 128, 185)) # Blue
painter.setPen(Qt.PenStyle.NoPen)
painter.drawEllipse(2, 2, size - 4, size - 4)
# Letter "P"
font = QFont("Arial", int(size * 0.6), QFont.Weight.Bold)
painter.setFont(font)
painter.setPen(QColor(255, 255, 255)) # White
painter.drawText(0, 0, size, size,
Qt.AlignmentFlag.AlignCenter, "П") # Cyrillic P
painter.end()
return QIcon(pixmap)
def get_logo_pixmap(self, width: Optional[int] = None,
height: Optional[int] = None) -> QPixmap:
"""
Get logo pixmap, loading from file or creating fallback.
Args:
width: Desired width (maintains aspect ratio if only one dimension given)
height: Desired height (maintains aspect ratio if only one dimension given)
Returns:
QPixmap with logo
"""
if self._logo_pixmap is None:
logo_file = self.find_logo_file()
if logo_file:
print(f"[Logo] Loading logo from: {logo_file}")
self._logo_pixmap = QPixmap(str(logo_file))
else:
print("[Logo] No logo file found, creating text-based logo")
self._logo_pixmap = self.create_text_logo()
# Scale if dimensions provided
if width or height:
if width and height:
return self._logo_pixmap.scaled(width, height,
Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
elif width:
return self._logo_pixmap.scaledToWidth(width,
Qt.TransformationMode.SmoothTransformation)
else:
return self._logo_pixmap.scaledToHeight(height,
Qt.TransformationMode.SmoothTransformation)
return self._logo_pixmap
def get_icon(self) -> QIcon:
"""
Get application icon.
Returns:
QIcon for window/taskbar
"""
if self._logo_icon is None:
logo_file = self.find_logo_file()
if logo_file:
self._logo_icon = QIcon(str(logo_file))
else:
self._logo_icon = self.create_icon_from_text()
return self._logo_icon
# Global singleton instance
_logo_handler: Optional[LogoHandler] = None
def get_logo_handler(base_dir: Optional[Path] = None) -> LogoHandler:
"""
Get global logo handler instance.
Args:
base_dir: Base directory for asset search
Returns:
Global LogoHandler instance
"""
global _logo_handler
if _logo_handler is None:
_logo_handler = LogoHandler(base_dir)
return _logo_handler