Windows designer for Avery templates with Avery.com Word download, print, and PyInstaller build. Co-authored-by: Cursor <cursoragent@cursor.com>
250 lines
9.5 KiB
Python
250 lines
9.5 KiB
Python
"""Full US Letter sheet preview showing all label slots (lightweight)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from PySide6.QtCore import QRectF, Qt, Signal
|
|
from PySide6.QtGui import QColor, QFont, QPainter, QPen, QPixmap
|
|
from PySide6.QtWidgets import QWidget
|
|
|
|
from .models import LabelDesign, SheetProject
|
|
from .template_60504 import TEMPLATE
|
|
|
|
# Keep preview cheap — full-res images were a major lag source
|
|
SHEET_DPI = 36
|
|
_THUMB_CACHE: dict[str, QPixmap] = {}
|
|
_THUMB_CACHE_MAX = 64
|
|
|
|
|
|
def _cached_thumb(path: str, w: int, h: int) -> QPixmap:
|
|
key = f"{path}|{w}x{h}"
|
|
hit = _THUMB_CACHE.get(key)
|
|
if hit is not None and not hit.isNull():
|
|
return hit
|
|
src = QPixmap(path)
|
|
if src.isNull():
|
|
return QPixmap()
|
|
# Fill element box (stretch/cover/contain already baked into element size)
|
|
scaled = src.scaled(
|
|
max(1, w),
|
|
max(1, h),
|
|
Qt.AspectRatioMode.IgnoreAspectRatio,
|
|
Qt.TransformationMode.FastTransformation,
|
|
)
|
|
if len(_THUMB_CACHE) >= _THUMB_CACHE_MAX:
|
|
_THUMB_CACHE.clear()
|
|
_THUMB_CACHE[key] = scaled
|
|
return scaled
|
|
|
|
|
|
class SheetPreview(QWidget):
|
|
"""Live preview of the full Letter page with label slots."""
|
|
|
|
slot_clicked = Signal(int)
|
|
|
|
def __init__(self, parent=None) -> None:
|
|
super().__init__(parent)
|
|
self.project: SheetProject | None = None
|
|
self._slot_pixmaps: list[QPixmap] = [QPixmap() for _ in range(4)]
|
|
self.setMinimumWidth(260)
|
|
self.setMinimumHeight(320)
|
|
self.setMouseTracking(True)
|
|
self.setToolTip("Full sheet — click a slot to edit")
|
|
|
|
def set_project(self, project: SheetProject) -> None:
|
|
self.project = project
|
|
self.rebuild_slot_pixmaps()
|
|
|
|
def _header_text(self) -> str:
|
|
if not self.project:
|
|
return f"{TEMPLATE.name} · Letter sheet"
|
|
total = self.project.sheet_count
|
|
current = self.project.preview_sheet + 1
|
|
labels = self.project.label_count
|
|
return f"{TEMPLATE.name} · Sheet {current}/{total} · {labels} label(s)"
|
|
|
|
def rebuild_slot_pixmaps(self) -> None:
|
|
if not self.project:
|
|
self._slot_pixmaps = [QPixmap() for _ in range(4)]
|
|
self.update()
|
|
return
|
|
sheet_labels = self.project.labels_for_sheet(self.project.preview_sheet)
|
|
self._slot_pixmaps = [self._render_design(d) for d in sheet_labels]
|
|
self.update()
|
|
|
|
def _render_design(self, design: LabelDesign) -> QPixmap:
|
|
w = max(1, int(TEMPLATE.label_width_in * SHEET_DPI))
|
|
h = max(1, int(TEMPLATE.label_height_in * SHEET_DPI))
|
|
pix = QPixmap(w, h)
|
|
pix.fill(QColor(design.background))
|
|
painter = QPainter(pix)
|
|
painter.setRenderHint(QPainter.RenderHint.Antialiasing, False)
|
|
sx = w / TEMPLATE.label_width_in
|
|
sy = h / TEMPLATE.label_height_in
|
|
for el in design.elements:
|
|
ex = el.x * sx
|
|
ey = el.y * sy
|
|
ew = max(1.0, el.width * sx)
|
|
eh = max(1.0, el.height * sy)
|
|
if el.type == "rect":
|
|
painter.setBrush(QColor(el.fill))
|
|
painter.setPen(QPen(QColor(el.stroke), max(1.0, el.stroke_width)))
|
|
painter.drawRect(QRectF(ex, ey, ew, eh))
|
|
elif el.type == "image" and el.image_path and Path(el.image_path).is_file():
|
|
img = _cached_thumb(el.image_path, int(ew), int(eh))
|
|
if not img.isNull():
|
|
painter.drawPixmap(int(ex), int(ey), img)
|
|
elif el.type == "text":
|
|
painter.setPen(QColor(el.color))
|
|
font = QFont("Arial", max(6, int(el.font_size * (sx / 96.0) * 0.85)))
|
|
font.setBold(el.font_bold)
|
|
painter.setFont(font)
|
|
align = Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignTop
|
|
if el.align == "center":
|
|
align = Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignTop
|
|
elif el.align == "right":
|
|
align = Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignTop
|
|
painter.drawText(QRectF(ex, ey, ew, eh), align, el.text)
|
|
painter.end()
|
|
return pix
|
|
|
|
def refresh(self) -> None:
|
|
self.rebuild_slot_pixmaps()
|
|
|
|
def _page_rect(self) -> QRectF:
|
|
pad = 12.0
|
|
avail_w = max(1.0, self.width() - pad * 2)
|
|
avail_h = max(1.0, self.height() - pad * 2 - 28)
|
|
page_aspect = TEMPLATE.page_width_in / TEMPLATE.page_height_in
|
|
widget_aspect = avail_w / avail_h
|
|
if widget_aspect > page_aspect:
|
|
h = avail_h
|
|
w = h * page_aspect
|
|
else:
|
|
w = avail_w
|
|
h = w / page_aspect
|
|
x = (self.width() - w) / 2
|
|
y = 20 + (avail_h - h) / 2
|
|
return QRectF(x, y, w, h)
|
|
|
|
def _slot_rect(self, index: int, page: QRectF) -> QRectF:
|
|
left_in, top_in = TEMPLATE.label_origin_in(index)
|
|
if self.project:
|
|
left_in += self.project.offset_x_in
|
|
top_in += self.project.offset_y_in
|
|
sx = page.width() / TEMPLATE.page_width_in
|
|
sy = page.height() / TEMPLATE.page_height_in
|
|
return QRectF(
|
|
page.left() + left_in * sx,
|
|
page.top() + top_in * sy,
|
|
TEMPLATE.label_width_in * sx,
|
|
TEMPLATE.label_height_in * sy,
|
|
)
|
|
|
|
def _slot_at(self, pos) -> int | None:
|
|
page = self._page_rect()
|
|
for i in range(TEMPLATE.labels_per_sheet):
|
|
if self._slot_rect(i, page).contains(pos):
|
|
return i
|
|
return None
|
|
|
|
def mousePressEvent(self, event) -> None:
|
|
if event.button() == Qt.MouseButton.LeftButton:
|
|
slot = self._slot_at(event.position())
|
|
if slot is not None:
|
|
self.slot_clicked.emit(slot)
|
|
event.accept()
|
|
return
|
|
super().mousePressEvent(event)
|
|
|
|
def paintEvent(self, event) -> None:
|
|
painter = QPainter(self)
|
|
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
|
|
painter.fillRect(self.rect(), QColor("#cbd5e1"))
|
|
|
|
page = self._page_rect()
|
|
shadow = page.translated(3, 3)
|
|
painter.setPen(Qt.PenStyle.NoPen)
|
|
painter.setBrush(QColor(0, 0, 0, 40))
|
|
painter.drawRoundedRect(shadow, 2, 2)
|
|
|
|
painter.setBrush(QColor("#f8fafc"))
|
|
painter.setPen(QPen(QColor("#64748b"), 1.2))
|
|
painter.drawRect(page)
|
|
|
|
painter.setPen(QColor("#334155"))
|
|
font = QFont("Segoe UI", 9)
|
|
font.setBold(True)
|
|
painter.setFont(font)
|
|
painter.drawText(
|
|
QRectF(page.left(), max(0.0, page.top() - 18), page.width(), 16),
|
|
Qt.AlignmentFlag.AlignCenter,
|
|
self._header_text(),
|
|
)
|
|
|
|
active = self.project.active_slots if self.project else [True, True, True, True]
|
|
editing_slot = self.project.editing_slot if self.project else 0
|
|
editing_on_this_sheet = (
|
|
self.project is not None
|
|
and (self.project.editing_index // 4) == self.project.preview_sheet
|
|
)
|
|
|
|
sheet_labels = (
|
|
self.project.labels_for_sheet(self.project.preview_sheet)
|
|
if self.project
|
|
else []
|
|
)
|
|
|
|
for i in range(TEMPLATE.labels_per_sheet):
|
|
rect = self._slot_rect(i, page)
|
|
is_on = i < len(active) and active[i]
|
|
pix = self._slot_pixmaps[i] if i < len(self._slot_pixmaps) else QPixmap()
|
|
design = sheet_labels[i] if i < len(sheet_labels) else None
|
|
empty = design is None or design.is_empty
|
|
|
|
if is_on and not pix.isNull() and not empty:
|
|
painter.drawPixmap(rect.toRect(), pix)
|
|
elif is_on and empty:
|
|
painter.fillRect(rect, QColor("#f1f5f9"))
|
|
painter.setPen(QColor("#94a3b8"))
|
|
painter.setFont(QFont("Segoe UI", 8))
|
|
painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, f"Slot {i + 1}\n(empty)")
|
|
elif is_on:
|
|
bg = design.background if design else "#ffffff"
|
|
painter.fillRect(rect, QColor(bg))
|
|
else:
|
|
painter.fillRect(rect, QColor("#e2e8f0"))
|
|
painter.setPen(QPen(QColor("#94a3b8"), 1.0, Qt.PenStyle.DashLine))
|
|
painter.setBrush(Qt.BrushStyle.NoBrush)
|
|
painter.drawRect(rect)
|
|
painter.setPen(QColor("#64748b"))
|
|
painter.setFont(QFont("Segoe UI", 8))
|
|
painter.drawText(rect, Qt.AlignmentFlag.AlignCenter, f"Slot {i + 1}\n(skipped)")
|
|
continue
|
|
|
|
is_editing = editing_on_this_sheet and i == editing_slot
|
|
outline = QColor("#2563eb") if is_editing else QColor("#0f766e")
|
|
width = 2.4 if is_editing else 1.4
|
|
painter.setBrush(Qt.BrushStyle.NoBrush)
|
|
painter.setPen(QPen(outline, width))
|
|
painter.drawRect(rect)
|
|
|
|
badge = QRectF(rect.left() + 4, rect.top() + 4, 18, 18)
|
|
painter.setBrush(outline)
|
|
painter.setPen(Qt.PenStyle.NoPen)
|
|
painter.drawRoundedRect(badge, 3, 3)
|
|
painter.setPen(QColor("#ffffff"))
|
|
badge_font = QFont("Segoe UI", 8)
|
|
badge_font.setBold(True)
|
|
painter.setFont(badge_font)
|
|
painter.drawText(badge, Qt.AlignmentFlag.AlignCenter, str(i + 1))
|
|
|
|
painter.setPen(QColor("#475569"))
|
|
painter.setFont(QFont("Segoe UI", 8))
|
|
painter.drawText(
|
|
QRectF(0, self.height() - 22, self.width(), 18),
|
|
Qt.AlignmentFlag.AlignCenter,
|
|
"Prev/Next sheet · Click a slot to edit",
|
|
)
|