Official 22827 Word guides already match the printed layout. Co-authored-by: Cursor <cursoragent@cursor.com>
170 lines
6.0 KiB
Python
170 lines
6.0 KiB
Python
"""Dialog to import PDF/Word and auto-fill Avery 60504 labels (multi-sheet)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from PySide6.QtWidgets import (
|
|
QComboBox,
|
|
QDialog,
|
|
QDialogButtonBox,
|
|
QDoubleSpinBox,
|
|
QFileDialog,
|
|
QFormLayout,
|
|
QGroupBox,
|
|
QHBoxLayout,
|
|
QLabel,
|
|
QLineEdit,
|
|
QMessageBox,
|
|
QPushButton,
|
|
QSpinBox,
|
|
QVBoxLayout,
|
|
)
|
|
|
|
from .import_settings import ImportSettings
|
|
|
|
|
|
class ImportDocumentDialog(QDialog):
|
|
def __init__(self, settings: ImportSettings | None = None, parent=None) -> None:
|
|
super().__init__(parent)
|
|
self.setWindowTitle("Import PDF / Word → Auto-fill 60504 labels")
|
|
self.setMinimumWidth(520)
|
|
self.file_path: Path | None = None
|
|
self.settings = settings or ImportSettings()
|
|
|
|
root = QVBoxLayout(self)
|
|
|
|
file_row = QHBoxLayout()
|
|
self.txt_path = QLineEdit()
|
|
self.txt_path.setPlaceholderText("Choose a PDF or Word document…")
|
|
self.txt_path.setReadOnly(True)
|
|
btn_browse = QPushButton("Browse…")
|
|
btn_browse.clicked.connect(self._browse)
|
|
file_row.addWidget(self.txt_path, stretch=1)
|
|
file_row.addWidget(btn_browse)
|
|
root.addLayout(file_row)
|
|
|
|
fill = QGroupBox("Auto-fill mode (uses ALL pages)")
|
|
fill_l = QFormLayout(fill)
|
|
self.cmb_fill = QComboBox()
|
|
self.cmb_fill.addItem(
|
|
"One PDF page → one label (auto-add Letter sheets, 4/sheet)",
|
|
"one_per_label",
|
|
)
|
|
self.cmb_fill.addItem(
|
|
"Each PDF page → full sheet of 4 copies",
|
|
"all_slots_per_page",
|
|
)
|
|
# Map legacy saved modes
|
|
mode = self.settings.normalized_fill_mode()
|
|
idx = self.cmb_fill.findData(mode)
|
|
self.cmb_fill.setCurrentIndex(max(0, idx))
|
|
fill_l.addRow("Fill", self.cmb_fill)
|
|
|
|
self.spin_start = QSpinBox()
|
|
self.spin_start.setRange(1, 9999)
|
|
self.spin_start.setValue(self.settings.start_page)
|
|
fill_l.addRow("Start page", self.spin_start)
|
|
|
|
self.spin_end = QSpinBox()
|
|
self.spin_end.setRange(0, 9999)
|
|
self.spin_end.setSpecialValueText("End of file")
|
|
self.spin_end.setValue(self.settings.end_page)
|
|
fill_l.addRow("End page (0 = all)", self.spin_end)
|
|
root.addWidget(fill)
|
|
|
|
place = QGroupBox("Placement on each label")
|
|
place_l = QFormLayout(place)
|
|
self.cmb_fit = QComboBox()
|
|
self.cmb_fit.addItem("Contain (fit inside, keep aspect)", "contain")
|
|
self.cmb_fit.addItem("Cover (fill label, may crop)", "cover")
|
|
self.cmb_fit.addItem("Stretch (fill label, may distort)", "stretch")
|
|
idx = self.cmb_fit.findData(self.settings.fit_mode)
|
|
self.cmb_fit.setCurrentIndex(max(0, idx))
|
|
place_l.addRow("Fit", self.cmb_fit)
|
|
|
|
self.spin_scale = QDoubleSpinBox()
|
|
self.spin_scale.setRange(0.25, 3.0)
|
|
self.spin_scale.setSingleStep(0.05)
|
|
self.spin_scale.setDecimals(2)
|
|
self.spin_scale.setValue(self.settings.scale)
|
|
place_l.addRow("Scale", self.spin_scale)
|
|
|
|
self.spin_inset = QDoubleSpinBox()
|
|
self.spin_inset.setRange(0.0, 0.5)
|
|
self.spin_inset.setSingleStep(0.01)
|
|
self.spin_inset.setDecimals(3)
|
|
self.spin_inset.setSuffix(" in")
|
|
self.spin_inset.setValue(self.settings.inset_in)
|
|
place_l.addRow("Inset", self.spin_inset)
|
|
|
|
self.spin_cx = QDoubleSpinBox()
|
|
self.spin_cy = QDoubleSpinBox()
|
|
for spin, val in (
|
|
(self.spin_cx, self.settings.content_offset_x_in),
|
|
(self.spin_cy, self.settings.content_offset_y_in),
|
|
):
|
|
spin.setRange(-1.0, 1.0)
|
|
spin.setSingleStep(0.01)
|
|
spin.setDecimals(3)
|
|
spin.setSuffix(" in")
|
|
spin.setValue(val)
|
|
place_l.addRow("Content offset X", self.spin_cx)
|
|
place_l.addRow("Content offset Y", self.spin_cy)
|
|
|
|
self.spin_dpi = QSpinBox()
|
|
self.spin_dpi.setRange(150, 600)
|
|
self.spin_dpi.setSingleStep(50)
|
|
self.spin_dpi.setValue(self.settings.render_dpi)
|
|
place_l.addRow("Render DPI", self.spin_dpi)
|
|
root.addWidget(place)
|
|
|
|
note = QLabel(
|
|
"Example: a 10-page PDF → 10 labels → 3 Letter sheets (4+4+2).\n"
|
|
"Export creates one print PDF with every sheet included.\n"
|
|
"Word needs Microsoft Word installed (or export to PDF first)."
|
|
)
|
|
note.setWordWrap(True)
|
|
note.setStyleSheet("color: #64748b;")
|
|
root.addWidget(note)
|
|
|
|
buttons = QDialogButtonBox(
|
|
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel
|
|
)
|
|
buttons.accepted.connect(self._accept)
|
|
buttons.rejected.connect(self.reject)
|
|
root.addWidget(buttons)
|
|
|
|
def _browse(self) -> None:
|
|
path, _ = QFileDialog.getOpenFileName(
|
|
self,
|
|
"Import PDF or Word",
|
|
"",
|
|
"Documents (*.pdf *.docx *.doc);;PDF (*.pdf);;Word (*.docx *.doc)",
|
|
)
|
|
if path:
|
|
self.file_path = Path(path)
|
|
self.txt_path.setText(path)
|
|
|
|
def result_settings(self) -> ImportSettings:
|
|
return ImportSettings(
|
|
fill_mode=self.cmb_fill.currentData(),
|
|
fit_mode=self.cmb_fit.currentData(),
|
|
scale=float(self.spin_scale.value()),
|
|
content_offset_x_in=float(self.spin_cx.value()),
|
|
content_offset_y_in=float(self.spin_cy.value()),
|
|
inset_in=float(self.spin_inset.value()),
|
|
start_page=int(self.spin_start.value()),
|
|
end_page=int(self.spin_end.value()),
|
|
render_dpi=int(self.spin_dpi.value()),
|
|
)
|
|
|
|
def _accept(self) -> None:
|
|
if not self.file_path or not self.file_path.is_file():
|
|
QMessageBox.warning(self, "Import", "Please choose a PDF or Word file.")
|
|
return
|
|
if self.spin_end.value() and self.spin_end.value() < self.spin_start.value():
|
|
QMessageBox.warning(self, "Import", "End page must be ≥ start page (or 0 for all).")
|
|
return
|
|
self.accept()
|