Add interactive label calibration UI #18
27
slips/api.py
27
slips/api.py
@@ -4,7 +4,11 @@ from django.utils import timezone
|
|||||||
from ninja import NinjaAPI, Query
|
from ninja import NinjaAPI, Query
|
||||||
|
|
||||||
from .models import Product, Order
|
from .models import Product, Order
|
||||||
from .schemas import ProductOut, ProductIn, OrderOut, OrderIn, OrderStatusIn
|
from .schemas import (
|
||||||
|
ProductOut, ProductIn, OrderOut, OrderIn, OrderStatusIn,
|
||||||
|
CalibrationIn, CalibrationOut,
|
||||||
|
)
|
||||||
|
from .calibration import load_calibration, save_calibration, DEFAULTS, for_api
|
||||||
|
|
||||||
api = NinjaAPI(title='Lithium Label API', version='1.0.0', urls_namespace='slips_api')
|
api = NinjaAPI(title='Lithium Label API', version='1.0.0', urls_namespace='slips_api')
|
||||||
|
|
||||||
@@ -63,3 +67,24 @@ def delete_order(request, order_id: int):
|
|||||||
order = get_object_or_404(Order, id=order_id)
|
order = get_object_or_404(Order, id=order_id)
|
||||||
order.delete()
|
order.delete()
|
||||||
return 204, None
|
return 204, None
|
||||||
|
|
||||||
|
|
||||||
|
# ─── Calibration ─────────────────────────────────────────
|
||||||
|
|
||||||
|
@api.get('/calibration', response=CalibrationOut, tags=['Calibration'])
|
||||||
|
def get_calibration(request):
|
||||||
|
return for_api(load_calibration())
|
||||||
|
|
||||||
|
|
||||||
|
@api.put('/calibration', response=CalibrationOut, tags=['Calibration'])
|
||||||
|
def put_calibration(request, payload: CalibrationIn):
|
||||||
|
data = payload.dict()
|
||||||
|
# Drop None optional keys so merge keeps defaults only when omitted
|
||||||
|
for field, vals in data.items():
|
||||||
|
data[field] = {k: v for k, v in vals.items() if v is not None}
|
||||||
|
return for_api(save_calibration(data))
|
||||||
|
|
||||||
|
|
||||||
|
@api.post('/calibration/reset', response=CalibrationOut, tags=['Calibration'])
|
||||||
|
def reset_calibration(request):
|
||||||
|
return for_api(save_calibration(DEFAULTS))
|
||||||
|
|||||||
138
slips/calibration.py
Normal file
138
slips/calibration.py
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
"""Label field positions (mm / pt). Stored in data/calibration.json."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from copy import deepcopy
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Matches prior label_print.css defaults (screen + print).
|
||||||
|
DEFAULTS = {
|
||||||
|
'batch': {
|
||||||
|
'top': 63.0,
|
||||||
|
'left': 30.2,
|
||||||
|
'font': 7.0,
|
||||||
|
'bottom_top': 43.3,
|
||||||
|
'right_left': 22.2,
|
||||||
|
},
|
||||||
|
'date': {
|
||||||
|
'top': 63.0,
|
||||||
|
'left': 67.4,
|
||||||
|
'font': 6.0,
|
||||||
|
'bottom_top': 43.3,
|
||||||
|
'bottom_left': 70.4,
|
||||||
|
},
|
||||||
|
'next': {
|
||||||
|
'top': 68.5,
|
||||||
|
'left': 84.2,
|
||||||
|
'font': 7.0,
|
||||||
|
'bottom_top': 48.8,
|
||||||
|
'bottom_left': 87.2,
|
||||||
|
},
|
||||||
|
'weight': {
|
||||||
|
'top': 68.5,
|
||||||
|
'left': 37.3,
|
||||||
|
'font': 7.0,
|
||||||
|
'bottom_top': 48.8,
|
||||||
|
'right_left': 29.3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
FIELDS = ('batch', 'date', 'next', 'weight')
|
||||||
|
|
||||||
|
|
||||||
|
def _data_dir() -> Path:
|
||||||
|
db_env = os.environ.get('DB_PATH')
|
||||||
|
if db_env:
|
||||||
|
return Path(db_env).parent
|
||||||
|
try:
|
||||||
|
from django.conf import settings
|
||||||
|
return Path(settings.BASE_DIR) / 'data'
|
||||||
|
except Exception:
|
||||||
|
return Path(__file__).resolve().parents[1] / 'data'
|
||||||
|
|
||||||
|
|
||||||
|
def calibration_path() -> Path:
|
||||||
|
return _data_dir() / 'calibration.json'
|
||||||
|
|
||||||
|
|
||||||
|
def _merge(base: dict, overlay: dict) -> dict:
|
||||||
|
out = deepcopy(base)
|
||||||
|
for field, vals in (overlay or {}).items():
|
||||||
|
if field not in out or not isinstance(vals, dict):
|
||||||
|
continue
|
||||||
|
for key, val in vals.items():
|
||||||
|
if key in out[field] and isinstance(val, (int, float)):
|
||||||
|
out[field][key] = float(val)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def load_calibration() -> dict:
|
||||||
|
path = calibration_path()
|
||||||
|
if not path.is_file():
|
||||||
|
return deepcopy(DEFAULTS)
|
||||||
|
try:
|
||||||
|
with path.open(encoding='utf-8') as f:
|
||||||
|
data = json.load(f)
|
||||||
|
except (OSError, json.JSONDecodeError):
|
||||||
|
return deepcopy(DEFAULTS)
|
||||||
|
return _merge(DEFAULTS, data)
|
||||||
|
|
||||||
|
|
||||||
|
def for_api(cal: dict) -> dict:
|
||||||
|
"""Ensure optional keys are present (null) for API schema."""
|
||||||
|
out = {}
|
||||||
|
for field in FIELDS:
|
||||||
|
v = cal[field]
|
||||||
|
out[field] = {
|
||||||
|
'top': v['top'],
|
||||||
|
'left': v['left'],
|
||||||
|
'font': v['font'],
|
||||||
|
'bottom_top': v['bottom_top'],
|
||||||
|
'bottom_left': v.get('bottom_left'),
|
||||||
|
'right_left': v.get('right_left'),
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def save_calibration(data: dict) -> dict:
|
||||||
|
merged = _merge(DEFAULTS, data)
|
||||||
|
path = calibration_path()
|
||||||
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
tmp = path.with_suffix('.tmp')
|
||||||
|
with tmp.open('w', encoding='utf-8') as f:
|
||||||
|
json.dump(merged, f, indent=2)
|
||||||
|
f.write('\n')
|
||||||
|
tmp.replace(path)
|
||||||
|
return merged
|
||||||
|
|
||||||
|
|
||||||
|
def css_vars(cal: dict | None = None) -> str:
|
||||||
|
"""Inline :root block for print + calibrate pages."""
|
||||||
|
cal = cal or load_calibration()
|
||||||
|
lines = [':root {']
|
||||||
|
for field in FIELDS:
|
||||||
|
v = cal[field]
|
||||||
|
lines.append(f' --{field}-top: {v["top"]}mm;')
|
||||||
|
lines.append(f' --{field}-left: {v["left"]}mm;')
|
||||||
|
lines.append(f' --{field}-font: {v["font"]}pt;')
|
||||||
|
lines.append(f' --{field}-bottom-top: {v["bottom_top"]}mm;')
|
||||||
|
if 'bottom_left' in v:
|
||||||
|
lines.append(f' --{field}-bottom-left: {v["bottom_left"]}mm;')
|
||||||
|
if 'right_left' in v:
|
||||||
|
lines.append(f' --{field}-right-left: {v["right_left"]}mm;')
|
||||||
|
lines.append('}')
|
||||||
|
return '\n'.join(lines)
|
||||||
|
|
||||||
|
|
||||||
|
# ponytail: assert-based check; upgrade to pytest if suite grows
|
||||||
|
if __name__ == '__main__':
|
||||||
|
css = css_vars(DEFAULTS)
|
||||||
|
assert '--batch-top: 63.0mm;' in css
|
||||||
|
assert '--date-bottom-left: 70.4mm;' in css
|
||||||
|
assert '--weight-right-left: 29.3mm;' in css
|
||||||
|
merged = _merge(DEFAULTS, {'batch': {'top': 64, 'bogus': 1}})
|
||||||
|
assert merged['batch']['top'] == 64.0
|
||||||
|
assert 'bogus' not in merged['batch']
|
||||||
|
assert merged['date']['left'] == DEFAULTS['date']['left']
|
||||||
|
print('ok')
|
||||||
@@ -66,3 +66,23 @@ class OrderOut(Schema):
|
|||||||
|
|
||||||
class OrderStatusIn(Schema):
|
class OrderStatusIn(Schema):
|
||||||
status: str
|
status: str
|
||||||
|
|
||||||
|
|
||||||
|
class FieldCalibration(Schema):
|
||||||
|
top: float
|
||||||
|
left: float
|
||||||
|
font: float
|
||||||
|
bottom_top: float
|
||||||
|
bottom_left: Optional[float] = None
|
||||||
|
right_left: Optional[float] = None
|
||||||
|
|
||||||
|
|
||||||
|
class CalibrationIn(Schema):
|
||||||
|
batch: FieldCalibration
|
||||||
|
date: FieldCalibration
|
||||||
|
next: FieldCalibration
|
||||||
|
weight: FieldCalibration
|
||||||
|
|
||||||
|
|
||||||
|
class CalibrationOut(CalibrationIn):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
/* Shared label field calibration — derived from
|
/* Shared label field layout — positions from CSS vars
|
||||||
* ETICHETTE PER INKS(...).xlsx.pdf (fit-to-page A4 print).
|
* (defaults here; calibrate UI / data/calibration.json override via :root).
|
||||||
* Positions are mm from the top-left of each slip quadrant.
|
|
||||||
* Font sizes are effective printed sizes (Excel 15/13pt × ~0.46 scale).
|
|
||||||
*/
|
*/
|
||||||
.var-field {
|
.var-field {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -11,27 +9,27 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.val-batch {
|
.val-batch {
|
||||||
font-size: 7pt;
|
font-size: var(--batch-font, 7pt);
|
||||||
top: 63mm; /* +2mm vs prior top-row calibration */
|
top: var(--batch-top, 63mm);
|
||||||
left: 30.2mm;
|
left: var(--batch-left, 30.2mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
.val-date {
|
.val-date {
|
||||||
font-size: 6pt;
|
font-size: var(--date-font, 6pt);
|
||||||
top: 63mm; /* +2mm vs prior top-row calibration */
|
top: var(--date-top, 63mm);
|
||||||
left: 67.4mm;
|
left: var(--date-left, 67.4mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
.val-next {
|
.val-next {
|
||||||
font-size: 7pt;
|
font-size: var(--next-font, 7pt);
|
||||||
top: 68.5mm; /* +2mm vs prior top-row calibration */
|
top: var(--next-top, 68.5mm);
|
||||||
left: 84.2mm;
|
left: var(--next-left, 84.2mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
.val-weight {
|
.val-weight {
|
||||||
font-size: 7pt;
|
font-size: var(--weight-font, 7pt);
|
||||||
top: 68.5mm; /* +2mm vs prior top-row calibration */
|
top: var(--weight-top, 68.5mm);
|
||||||
left: 37.3mm;
|
left: var(--weight-left, 37.3mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -39,29 +37,29 @@
|
|||||||
* matching fields is ~118.8mm (not 148.5mm). Shift bottom slips up.
|
* matching fields is ~118.8mm (not 148.5mm). Shift bottom slips up.
|
||||||
* Quadrant order in the 2×2 grid: 1=TL, 2=TR, 3=BL, 4=BR.
|
* Quadrant order in the 2×2 grid: 1=TL, 2=TR, 3=BL, 4=BR.
|
||||||
*/
|
*/
|
||||||
.slip-quadrant:nth-child(n + 3) .val-batch,
|
.slip-quadrant:nth-child(n + 3) .val-batch {
|
||||||
.slip-quadrant:nth-child(n + 3) .val-date {
|
top: var(--batch-bottom-top, 43.3mm);
|
||||||
top: 43.3mm; /* +12mm vs prior bottom-row calibration */
|
|
||||||
}
|
|
||||||
|
|
||||||
.slip-quadrant:nth-child(n + 3) .val-next,
|
|
||||||
.slip-quadrant:nth-child(n + 3) .val-weight {
|
|
||||||
top: 48.8mm; /* +12mm vs prior bottom-row calibration */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.slip-quadrant:nth-child(n + 3) .val-date {
|
.slip-quadrant:nth-child(n + 3) .val-date {
|
||||||
left: 70.4mm; /* +3mm vs top-row date */
|
top: var(--date-bottom-top, 43.3mm);
|
||||||
|
left: var(--date-bottom-left, 70.4mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
.slip-quadrant:nth-child(n + 3) .val-next {
|
.slip-quadrant:nth-child(n + 3) .val-next {
|
||||||
left: 87.2mm; /* +3mm vs top-row next control */
|
top: var(--next-bottom-top, 48.8mm);
|
||||||
|
left: var(--next-bottom-left, 87.2mm);
|
||||||
|
}
|
||||||
|
|
||||||
|
.slip-quadrant:nth-child(n + 3) .val-weight {
|
||||||
|
top: var(--weight-bottom-top, 48.8mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Right column (TR, BR): Batch# and Net weight */
|
/* Right column (TR, BR): Batch# and Net weight */
|
||||||
.slip-quadrant:nth-child(2n) .val-batch {
|
.slip-quadrant:nth-child(2n) .val-batch {
|
||||||
left: 22.2mm; /* -8mm vs left-column batch */
|
left: var(--batch-right-left, 22.2mm);
|
||||||
}
|
}
|
||||||
|
|
||||||
.slip-quadrant:nth-child(2n) .val-weight {
|
.slip-quadrant:nth-child(2n) .val-weight {
|
||||||
left: 29.3mm; /* -8mm vs left-column weight */
|
left: var(--weight-right-left, 29.3mm);
|
||||||
}
|
}
|
||||||
|
|||||||
292
slips/static/slips/js/calibrate.js
Normal file
292
slips/static/slips/js/calibrate.js
Normal file
@@ -0,0 +1,292 @@
|
|||||||
|
(() => {
|
||||||
|
const FIELDS = ['batch', 'date', 'next', 'weight'];
|
||||||
|
const LABELS = { batch: 'Batch', date: 'Date', next: 'Next control', weight: 'Weight' };
|
||||||
|
|
||||||
|
const TOP_KEYS = [
|
||||||
|
{ key: 'top', label: 'top' },
|
||||||
|
{ key: 'left', label: 'left' },
|
||||||
|
{ key: 'font', label: 'font pt' },
|
||||||
|
];
|
||||||
|
const BOTTOM_KEYS = {
|
||||||
|
batch: [{ key: 'bottom_top', label: 'top' }],
|
||||||
|
date: [
|
||||||
|
{ key: 'bottom_top', label: 'top' },
|
||||||
|
{ key: 'bottom_left', label: 'left' },
|
||||||
|
],
|
||||||
|
next: [
|
||||||
|
{ key: 'bottom_top', label: 'top' },
|
||||||
|
{ key: 'bottom_left', label: 'left' },
|
||||||
|
],
|
||||||
|
weight: [{ key: 'bottom_top', label: 'top' }],
|
||||||
|
};
|
||||||
|
const RIGHT_KEYS = {
|
||||||
|
batch: [{ key: 'right_left', label: 'left' }],
|
||||||
|
weight: [{ key: 'right_left', label: 'left' }],
|
||||||
|
};
|
||||||
|
|
||||||
|
const dataEl = document.getElementById('calibration-data');
|
||||||
|
let state = JSON.parse(dataEl.textContent);
|
||||||
|
let saved = structuredClone(state);
|
||||||
|
let selected = { field: 'batch', section: 'top' };
|
||||||
|
|
||||||
|
const statusEl = document.getElementById('status');
|
||||||
|
const stepEl = document.getElementById('step');
|
||||||
|
const varsEl = document.getElementById('calibration-vars');
|
||||||
|
|
||||||
|
function round1(n) {
|
||||||
|
return Math.round(Number(n) * 10) / 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isDirty() {
|
||||||
|
return JSON.stringify(state) !== JSON.stringify(saved);
|
||||||
|
}
|
||||||
|
|
||||||
|
function setStatus(msg, kind) {
|
||||||
|
statusEl.textContent = msg;
|
||||||
|
statusEl.className = 'status' + (kind ? ' ' + kind : '');
|
||||||
|
}
|
||||||
|
|
||||||
|
function cssFromState(cal) {
|
||||||
|
const lines = [':root {'];
|
||||||
|
for (const field of FIELDS) {
|
||||||
|
const v = cal[field];
|
||||||
|
lines.push(` --${field}-top: ${v.top}mm;`);
|
||||||
|
lines.push(` --${field}-left: ${v.left}mm;`);
|
||||||
|
lines.push(` --${field}-font: ${v.font}pt;`);
|
||||||
|
lines.push(` --${field}-bottom-top: ${v.bottom_top}mm;`);
|
||||||
|
if (v.bottom_left != null) lines.push(` --${field}-bottom-left: ${v.bottom_left}mm;`);
|
||||||
|
if (v.right_left != null) lines.push(` --${field}-right-left: ${v.right_left}mm;`);
|
||||||
|
}
|
||||||
|
lines.push('}');
|
||||||
|
return lines.join('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyLive() {
|
||||||
|
varsEl.textContent = cssFromState(state);
|
||||||
|
if (isDirty()) setStatus('Unsaved changes', 'dirty');
|
||||||
|
else setStatus('Saved', 'ok');
|
||||||
|
syncInputs();
|
||||||
|
highlightSelected();
|
||||||
|
}
|
||||||
|
|
||||||
|
function step() {
|
||||||
|
const s = parseFloat(stepEl.value);
|
||||||
|
return Number.isFinite(s) && s > 0 ? s : 0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
function setValue(field, key, value) {
|
||||||
|
if (!(key in state[field])) return;
|
||||||
|
let v = round1(value);
|
||||||
|
if (key === 'font') v = Math.max(4, Math.min(20, v));
|
||||||
|
else v = Math.max(0, Math.min(150, v));
|
||||||
|
state[field][key] = v;
|
||||||
|
applyLive();
|
||||||
|
}
|
||||||
|
|
||||||
|
function nudge(field, key, dir) {
|
||||||
|
setValue(field, key, state[field][key] + dir * step());
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeNudgeRow(field, key, label) {
|
||||||
|
const row = document.createElement('div');
|
||||||
|
row.className = 'nudge';
|
||||||
|
row.dataset.field = field;
|
||||||
|
row.dataset.key = key;
|
||||||
|
|
||||||
|
const lab = document.createElement('label');
|
||||||
|
lab.textContent = label;
|
||||||
|
|
||||||
|
const input = document.createElement('input');
|
||||||
|
input.type = 'number';
|
||||||
|
input.step = '0.1';
|
||||||
|
input.value = state[field][key];
|
||||||
|
input.addEventListener('change', () => setValue(field, key, input.value));
|
||||||
|
|
||||||
|
const minus = document.createElement('button');
|
||||||
|
minus.type = 'button';
|
||||||
|
minus.textContent = '−';
|
||||||
|
minus.addEventListener('click', () => nudge(field, key, -1));
|
||||||
|
|
||||||
|
const plus = document.createElement('button');
|
||||||
|
plus.type = 'button';
|
||||||
|
plus.textContent = '+';
|
||||||
|
plus.addEventListener('click', () => nudge(field, key, 1));
|
||||||
|
|
||||||
|
row.append(lab, input, minus, plus);
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeFieldBlock(field, keys, section) {
|
||||||
|
const block = document.createElement('div');
|
||||||
|
block.className = 'field-block';
|
||||||
|
block.dataset.field = field;
|
||||||
|
block.dataset.section = section;
|
||||||
|
|
||||||
|
const h = document.createElement('h3');
|
||||||
|
h.textContent = LABELS[field];
|
||||||
|
block.appendChild(h);
|
||||||
|
|
||||||
|
for (const { key, label } of keys) {
|
||||||
|
if (state[field][key] == null) continue;
|
||||||
|
block.appendChild(makeNudgeRow(field, key, label));
|
||||||
|
}
|
||||||
|
|
||||||
|
block.addEventListener('click', () => {
|
||||||
|
selected = { field, section };
|
||||||
|
highlightSelected();
|
||||||
|
});
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildPanel() {
|
||||||
|
const top = document.getElementById('sec-top');
|
||||||
|
const bottom = document.getElementById('sec-bottom');
|
||||||
|
const right = document.getElementById('sec-right');
|
||||||
|
|
||||||
|
for (const field of FIELDS) {
|
||||||
|
top.appendChild(makeFieldBlock(field, TOP_KEYS, 'top'));
|
||||||
|
if (BOTTOM_KEYS[field]) {
|
||||||
|
bottom.appendChild(makeFieldBlock(field, BOTTOM_KEYS[field], 'bottom'));
|
||||||
|
}
|
||||||
|
if (RIGHT_KEYS[field]) {
|
||||||
|
right.appendChild(makeFieldBlock(field, RIGHT_KEYS[field], 'right'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncInputs() {
|
||||||
|
document.querySelectorAll('.nudge').forEach((row) => {
|
||||||
|
const field = row.dataset.field;
|
||||||
|
const key = row.dataset.key;
|
||||||
|
const input = row.querySelector('input');
|
||||||
|
if (input && document.activeElement !== input) {
|
||||||
|
input.value = state[field][key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function highlightSelected() {
|
||||||
|
document.querySelectorAll('.field-block').forEach((el) => {
|
||||||
|
el.classList.toggle(
|
||||||
|
'active',
|
||||||
|
el.dataset.field === selected.field && el.dataset.section === selected.section
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.querySelectorAll('.var-field').forEach((el) => {
|
||||||
|
const q = el.dataset.scope;
|
||||||
|
const field = el.dataset.field;
|
||||||
|
let match = field === selected.field;
|
||||||
|
if (selected.section === 'top') {
|
||||||
|
match = match && (q === 'TL' || q === 'TR');
|
||||||
|
} else if (selected.section === 'bottom') {
|
||||||
|
match = match && (q === 'BL' || q === 'BR');
|
||||||
|
} else if (selected.section === 'right') {
|
||||||
|
match = match && (q === 'TR' || q === 'BR');
|
||||||
|
}
|
||||||
|
el.classList.toggle('selected', match);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function payloadFromState() {
|
||||||
|
// API schema expects all keys; optional ones may be null
|
||||||
|
const out = {};
|
||||||
|
for (const field of FIELDS) {
|
||||||
|
out[field] = {
|
||||||
|
top: state[field].top,
|
||||||
|
left: state[field].left,
|
||||||
|
font: state[field].font,
|
||||||
|
bottom_top: state[field].bottom_top,
|
||||||
|
bottom_left: state[field].bottom_left ?? null,
|
||||||
|
right_left: state[field].right_left ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function save() {
|
||||||
|
const btn = document.getElementById('btn-save');
|
||||||
|
btn.disabled = true;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/calibration', {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(payloadFromState()),
|
||||||
|
});
|
||||||
|
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||||||
|
state = await res.json();
|
||||||
|
saved = structuredClone(state);
|
||||||
|
applyLive();
|
||||||
|
setStatus('Saved to server', 'ok');
|
||||||
|
} catch (e) {
|
||||||
|
setStatus('Save failed: ' + e.message, 'err');
|
||||||
|
} finally {
|
||||||
|
btn.disabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function reset() {
|
||||||
|
if (!confirm('Reset all positions to built-in defaults?')) return;
|
||||||
|
try {
|
||||||
|
const res = await fetch('/api/calibration/reset', { method: 'POST' });
|
||||||
|
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||||||
|
state = await res.json();
|
||||||
|
saved = structuredClone(state);
|
||||||
|
applyLive();
|
||||||
|
setStatus('Defaults restored', 'ok');
|
||||||
|
} catch (e) {
|
||||||
|
setStatus('Reset failed: ' + e.message, 'err');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.querySelectorAll('.var-field').forEach((el) => {
|
||||||
|
el.addEventListener('click', () => {
|
||||||
|
const q = el.dataset.scope;
|
||||||
|
const field = el.dataset.field;
|
||||||
|
let section = 'top';
|
||||||
|
if ((q === 'TR' || q === 'BR') && (field === 'batch' || field === 'weight')) {
|
||||||
|
section = 'right';
|
||||||
|
} else if (q === 'BL' || q === 'BR') {
|
||||||
|
section = 'bottom';
|
||||||
|
}
|
||||||
|
selected = { field, section };
|
||||||
|
const block = document.querySelector(
|
||||||
|
`.field-block[data-field="${field}"][data-section="${section}"]`
|
||||||
|
);
|
||||||
|
if (block) block.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
||||||
|
highlightSelected();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
document.addEventListener('keydown', (e) => {
|
||||||
|
if (e.target.matches('input, textarea')) return;
|
||||||
|
const map = {
|
||||||
|
ArrowLeft: ['leftish', -1],
|
||||||
|
ArrowRight: ['leftish', 1],
|
||||||
|
ArrowUp: ['topish', -1],
|
||||||
|
ArrowDown: ['topish', 1],
|
||||||
|
};
|
||||||
|
const hit = map[e.key];
|
||||||
|
if (!hit) return;
|
||||||
|
e.preventDefault();
|
||||||
|
const [axis, dir] = hit;
|
||||||
|
const { field, section } = selected;
|
||||||
|
let key;
|
||||||
|
if (section === 'top') {
|
||||||
|
key = axis === 'topish' ? 'top' : 'left';
|
||||||
|
} else if (section === 'bottom') {
|
||||||
|
key = axis === 'topish' ? 'bottom_top' : 'bottom_left';
|
||||||
|
} else {
|
||||||
|
key = axis === 'topish' ? null : 'right_left';
|
||||||
|
}
|
||||||
|
if (!key || state[field][key] == null) return;
|
||||||
|
nudge(field, key, dir);
|
||||||
|
});
|
||||||
|
|
||||||
|
document.getElementById('btn-save').addEventListener('click', save);
|
||||||
|
document.getElementById('btn-reset').addEventListener('click', reset);
|
||||||
|
document.getElementById('btn-print').addEventListener('click', () => window.print());
|
||||||
|
|
||||||
|
buildPanel();
|
||||||
|
applyLive();
|
||||||
|
})();
|
||||||
@@ -4,76 +4,280 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Calibration Page</title>
|
<title>Calibration — Lithium | Print Label</title>
|
||||||
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
|
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
|
||||||
|
<style id="calibration-vars">
|
||||||
|
{{ calibration_css|safe }}
|
||||||
|
</style>
|
||||||
<style>
|
<style>
|
||||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||||
body { font-family: Arial, sans-serif; background-color: #f0f0f0; }
|
body {
|
||||||
|
font-family: system-ui, sans-serif;
|
||||||
.preview-controls {
|
background: #0f172a;
|
||||||
background: #1e293b; color: #fff; padding: 1rem; text-align: center;
|
color: #e2e8f0;
|
||||||
position: fixed; top: 0; left: 0; right: 0; z-index: 100;
|
min-height: 100vh;
|
||||||
}
|
|
||||||
.preview-controls button {
|
|
||||||
background: #3b82f6; color: white; border: none; padding: 10px 20px;
|
|
||||||
font-size: 16px; border-radius: 6px; cursor: pointer; font-weight: bold;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.page-wrapper { margin-top: 80px; display: flex; justify-content: center; padding-bottom: 40px; }
|
.layout {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: minmax(300px, 360px) 1fr;
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel {
|
||||||
|
background: #1e293b;
|
||||||
|
border-right: 1px solid #334155;
|
||||||
|
padding: 1rem;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
max-height: 100vh;
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel h1 { font-size: 1.1rem; font-weight: 700; }
|
||||||
|
.panel .hint { font-size: 0.8rem; color: #94a3b8; line-height: 1.4; }
|
||||||
|
|
||||||
|
.toolbar {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
.toolbar button, .nudge button {
|
||||||
|
background: #334155;
|
||||||
|
color: #f1f5f9;
|
||||||
|
border: 1px solid #475569;
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 0.45rem 0.7rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
.toolbar button.primary { background: #2563eb; border-color: #2563eb; }
|
||||||
|
.toolbar button.danger { background: #7f1d1d; border-color: #991b1b; }
|
||||||
|
.toolbar button:hover, .nudge button:hover { filter: brightness(1.1); }
|
||||||
|
.toolbar button:disabled { opacity: 0.5; cursor: default; }
|
||||||
|
|
||||||
|
.status {
|
||||||
|
font-size: 0.8rem;
|
||||||
|
min-height: 1.2em;
|
||||||
|
color: #94a3b8;
|
||||||
|
}
|
||||||
|
.status.dirty { color: #fbbf24; }
|
||||||
|
.status.ok { color: #34d399; }
|
||||||
|
.status.err { color: #f87171; }
|
||||||
|
|
||||||
|
.step-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
}
|
||||||
|
.step-row input[type="number"] {
|
||||||
|
width: 4.5rem;
|
||||||
|
background: #0f172a;
|
||||||
|
border: 1px solid #475569;
|
||||||
|
color: #f1f5f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 0.3rem 0.4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section {
|
||||||
|
border: 1px solid #334155;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 0.65rem;
|
||||||
|
background: #0f172a66;
|
||||||
|
}
|
||||||
|
.section h2 {
|
||||||
|
font-size: 0.75rem;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.04em;
|
||||||
|
color: #94a3b8;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-block {
|
||||||
|
border-radius: 6px;
|
||||||
|
padding: 0.4rem;
|
||||||
|
margin-bottom: 0.35rem;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
}
|
||||||
|
.field-block.active {
|
||||||
|
border-color: #3b82f6;
|
||||||
|
background: rgba(59, 130, 246, 0.12);
|
||||||
|
}
|
||||||
|
.field-block h3 {
|
||||||
|
font-size: 0.85rem;
|
||||||
|
margin-bottom: 0.35rem;
|
||||||
|
color: #f8fafc;
|
||||||
|
}
|
||||||
|
|
||||||
|
.nudge {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 4.5rem 1fr auto auto auto;
|
||||||
|
gap: 0.3rem;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 0.25rem;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
}
|
||||||
|
.nudge label { color: #94a3b8; }
|
||||||
|
.nudge input[type="number"] {
|
||||||
|
width: 100%;
|
||||||
|
background: #0f172a;
|
||||||
|
border: 1px solid #475569;
|
||||||
|
color: #f1f5f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 0.25rem 0.35rem;
|
||||||
|
font-variant-numeric: tabular-nums;
|
||||||
|
}
|
||||||
|
.nudge button { padding: 0.2rem 0.55rem; min-width: 2rem; }
|
||||||
|
|
||||||
|
.preview-wrap {
|
||||||
|
padding: 1.25rem;
|
||||||
|
overflow: auto;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
.a4-page {
|
.a4-page {
|
||||||
width: 210mm; height: 297mm; background: white; position: relative;
|
width: 210mm;
|
||||||
box-shadow: 0 10px 25px rgba(0,0,0,0.15); overflow: hidden;
|
height: 297mm;
|
||||||
|
background: white;
|
||||||
|
position: relative;
|
||||||
|
box-shadow: 0 10px 25px rgba(0,0,0,0.35);
|
||||||
|
overflow: hidden;
|
||||||
background-image:
|
background-image:
|
||||||
linear-gradient(to right, #e2e8f0 1px, transparent 1px),
|
linear-gradient(to right, #e2e8f0 1px, transparent 1px),
|
||||||
linear-gradient(to bottom, #e2e8f0 1px, transparent 1px);
|
linear-gradient(to bottom, #e2e8f0 1px, transparent 1px);
|
||||||
background-size: 10mm 10mm;
|
background-size: 10mm 10mm;
|
||||||
|
flex-shrink: 0;
|
||||||
|
transform-origin: top center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.slip-grid {
|
.slip-grid {
|
||||||
display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr;
|
display: grid;
|
||||||
width: 100%; height: 100%;
|
grid-template-columns: 1fr 1fr;
|
||||||
|
grid-template-rows: 1fr 1fr;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.slip-quadrant { position: relative; width: 105mm; height: 148.5mm; border: 1px dashed #cbd5e1; }
|
.slip-quadrant {
|
||||||
|
position: relative;
|
||||||
|
width: 105mm;
|
||||||
|
height: 148.5mm;
|
||||||
|
border: 1px dashed #cbd5e1;
|
||||||
|
}
|
||||||
|
|
||||||
.var-field {
|
.var-field {
|
||||||
color: red; /* Red for visibility during calibration */
|
color: #dc2626;
|
||||||
|
cursor: pointer;
|
||||||
|
outline: 1px solid transparent;
|
||||||
|
padding: 0 1px;
|
||||||
|
}
|
||||||
|
.var-field:hover { outline-color: #3b82f6; }
|
||||||
|
.var-field.selected {
|
||||||
|
outline: 2px solid #2563eb;
|
||||||
|
background: rgba(37, 99, 235, 0.12);
|
||||||
|
}
|
||||||
|
.var-field::before {
|
||||||
|
content: "┼";
|
||||||
|
position: absolute;
|
||||||
|
left: -10px;
|
||||||
|
top: -5px;
|
||||||
|
color: #2563eb;
|
||||||
|
font-size: 20px;
|
||||||
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.crosshair::before {
|
.q-tag {
|
||||||
content: "┼"; position: absolute; left: -10px; top: -5px; color: blue; font-size: 20px;
|
position: absolute;
|
||||||
|
top: 4px;
|
||||||
|
left: 6px;
|
||||||
|
font-size: 10px;
|
||||||
|
color: #64748b;
|
||||||
|
font-family: sans-serif;
|
||||||
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media print {
|
@media print {
|
||||||
@page { size: A4 portrait; margin: 0; }
|
@page { size: A4 portrait; margin: 0; }
|
||||||
body { background: none; margin: 0; }
|
body { background: none; margin: 0; color: #000; }
|
||||||
.preview-controls { display: none; }
|
.panel { display: none !important; }
|
||||||
.page-wrapper { margin: 0; padding: 0; display: block; }
|
.layout { display: block; }
|
||||||
.a4-page { box-shadow: none; width: 100%; height: 100vh; background-image: none; }
|
.preview-wrap { margin: 0; padding: 0; display: block; overflow: visible; }
|
||||||
|
.a4-page {
|
||||||
|
box-shadow: none;
|
||||||
|
width: 100%;
|
||||||
|
height: 100vh;
|
||||||
|
background-image: none;
|
||||||
|
transform: none !important;
|
||||||
|
}
|
||||||
|
.slip-quadrant { border: none; }
|
||||||
|
.q-tag { display: none; }
|
||||||
|
.var-field { color: #000; outline: none !important; background: none !important; }
|
||||||
|
.var-field::before { display: none; }
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 900px) {
|
||||||
|
.layout { grid-template-columns: 1fr; }
|
||||||
|
.panel { max-height: none; border-right: none; border-bottom: 1px solid #334155; }
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="preview-controls">
|
<div class="layout">
|
||||||
<p style="margin-bottom: 10px;">Calibration Sheet. Print this on blank paper and overlay on a pre-printed label against light.</p>
|
<aside class="panel">
|
||||||
<button onclick="window.print()">🖨️ Print Calibration</button>
|
<h1>Label calibration</h1>
|
||||||
<button onclick="window.close()" style="background:#64748b; margin-left: 10px;">Close</button>
|
<p class="hint">
|
||||||
</div>
|
Nudge fields in mm, print on blank A4, overlay on a pre-printed sheet against light.
|
||||||
|
Save writes <code>data/calibration.json</code> (used by real prints too).
|
||||||
|
</p>
|
||||||
|
|
||||||
<div class="page-wrapper">
|
<div class="toolbar">
|
||||||
<div class="a4-page">
|
<button type="button" class="primary" id="btn-save">Save</button>
|
||||||
<div class="slip-grid">
|
<button type="button" id="btn-print">Print</button>
|
||||||
{% for i in "1234" %}
|
<button type="button" class="danger" id="btn-reset">Reset defaults</button>
|
||||||
<div class="slip-quadrant">
|
<a href="{% url 'entry' %}" style="align-self:center;color:#93c5fd;font-size:0.85rem;margin-left:0.25rem;">← Back</a>
|
||||||
<div class="var-field crosshair val-batch">[Batch#]</div>
|
</div>
|
||||||
<div class="var-field crosshair val-date">[Date]</div>
|
<div class="status" id="status">Loaded from server</div>
|
||||||
<div class="var-field crosshair val-next">[Next Control]</div>
|
|
||||||
<div class="var-field crosshair val-weight">[Weight]</div>
|
<div class="step-row">
|
||||||
|
<label for="step">Step</label>
|
||||||
|
<input type="number" id="step" value="0.5" min="0.1" max="5" step="0.1">
|
||||||
|
<span>mm</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section" id="sec-top">
|
||||||
|
<h2>Top row (TL / TR base)</h2>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="sec-bottom">
|
||||||
|
<h2>Bottom row overrides</h2>
|
||||||
|
</div>
|
||||||
|
<div class="section" id="sec-right">
|
||||||
|
<h2>Right column overrides</h2>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
|
||||||
|
<div class="preview-wrap">
|
||||||
|
<div class="a4-page" id="a4-page">
|
||||||
|
<div class="slip-grid">
|
||||||
|
{% for label in quadrants %}
|
||||||
|
<div class="slip-quadrant" data-q="{{ label }}">
|
||||||
|
<span class="q-tag">{{ label }}</span>
|
||||||
|
<div class="var-field val-batch" data-field="batch" data-scope="{{ label }}">[Batch#]</div>
|
||||||
|
<div class="var-field val-date" data-field="date" data-scope="{{ label }}">[Date]</div>
|
||||||
|
<div class="var-field val-next" data-field="next" data-scope="{{ label }}">[Next]</div>
|
||||||
|
<div class="var-field val-weight" data-field="weight" data-scope="{{ label }}">[Weight]</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{{ calibration_json|json_script:"calibration-data" }}
|
||||||
|
<script src="{% static 'slips/js/calibrate.js' %}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Print Order #{{ order.id }}</title>
|
<title>Print Order #{{ order.id }}</title>
|
||||||
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
|
<link rel="stylesheet" href="{% static 'slips/css/label_print.css' %}">
|
||||||
|
<style>
|
||||||
|
{{ calibration_css|safe }}
|
||||||
|
</style>
|
||||||
<style>
|
<style>
|
||||||
/* Base reset and print-specific settings */
|
/* Base reset and print-specific settings */
|
||||||
* {
|
* {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from django.shortcuts import render, redirect, get_object_or_404
|
from django.shortcuts import render, redirect, get_object_or_404
|
||||||
|
from .calibration import css_vars, load_calibration
|
||||||
from .models import Product, Order
|
from .models import Product, Order
|
||||||
from .print_layout import build_print_pages
|
from .print_layout import build_print_pages
|
||||||
|
|
||||||
@@ -29,13 +30,21 @@ def print_page(request, order_id):
|
|||||||
order = get_object_or_404(Order.objects.select_related('product'), id=order_id)
|
order = get_object_or_404(Order.objects.select_related('product'), id=order_id)
|
||||||
pages = build_print_pages(order)
|
pages = build_print_pages(order)
|
||||||
|
|
||||||
|
cal = load_calibration()
|
||||||
return render(request, 'slips/print_page.html', {
|
return render(request, 'slips/print_page.html', {
|
||||||
'order': order,
|
'order': order,
|
||||||
'pages': pages,
|
'pages': pages,
|
||||||
'date_formatted': order.production_date.strftime('%d/%m/%Y'),
|
'date_formatted': order.production_date.strftime('%d/%m/%Y'),
|
||||||
'next_control_formatted': order.next_control_date.strftime('%d/%m/%Y'),
|
'next_control_formatted': order.next_control_date.strftime('%d/%m/%Y'),
|
||||||
|
'calibration_css': css_vars(cal),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def calibrate(request):
|
def calibrate(request):
|
||||||
return render(request, 'slips/calibrate.html')
|
cal = load_calibration()
|
||||||
|
return render(request, 'slips/calibrate.html', {
|
||||||
|
'calibration': cal,
|
||||||
|
'calibration_css': css_vars(cal),
|
||||||
|
'calibration_json': cal,
|
||||||
|
'quadrants': ['TL', 'TR', 'BL', 'BR'],
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user