This commit is contained in:
Eduardo 2025-10-23 15:30:55 +02:00
parent 4da1bde084
commit 58f5ee26bb

View File

@ -4,10 +4,10 @@ import peewee
from PyQt5 import Qt
from lib.helpers import timing
from PyQt5.QtCore import Qt, QTimer, QThread
from PyQt5.QtCore import Qt, QTimer, QThread, pyqtSignal
from PyQt5.QtGui import QKeySequence, QPixmap, QPalette, QColor
from PyQt5.QtWidgets import QShortcut, QApplication, QDialog, QVBoxLayout, QLabel, QPushButton, QPlainTextEdit, \
QHBoxLayout, QMessageBox
QHBoxLayout, QMessageBox, QWidget
from ui.test_test import Test_Test
from ui.widget import Widget
@ -16,6 +16,64 @@ from lib.db.models import Recipes , Users
test_scan="xxx\nyyy\nzzz"
class QRCodeScannerDialog(QDialog):
qr_code_scanned = pyqtSignal(str)
def __init__(self, parent=None, candidates=None):
super().__init__(parent)
self.setWindowTitle("Scansione QR Code")
self.setMinimumSize(500, 400)
layout = QVBoxLayout()
# Instructions
instructions = QLabel("SCANSIONARE IL QR CODE CON IL NOME DELLA RICETTA")
instructions.setStyleSheet("font-size: 16pt; font-weight: bold; color: blue;")
instructions.setAlignment(Qt.AlignCenter)
layout.addWidget(instructions)
# QR code input field
self.qr_input = QPlainTextEdit()
self.qr_input.setPlaceholderText("Il QR code apparirà qui...")
self.qr_input.textChanged.connect(self.on_text_changed)
self.qr_input.setStyleSheet("font-size: 14pt;")
layout.addWidget(self.qr_input)
# Candidates list
if candidates:
candidates_label = QLabel("Candidati possibili:")
candidates_label.setStyleSheet("font-size: 14pt; font-weight: bold;")
layout.addWidget(candidates_label)
candidates_layout = QVBoxLayout()
for candidate in candidates:
candidate_label = QLabel(f"- {candidate}")
candidate_label.setStyleSheet("font-size: 12pt;")
candidates_layout.addWidget(candidate_label)
candidates_widget = QWidget()
candidates_widget.setLayout(candidates_layout)
layout.addWidget(candidates_widget)
# Cancel button
cancel_button = QPushButton("Annulla")
cancel_button.setStyleSheet("font-size: 14pt; padding: 10px;")
cancel_button.clicked.connect(self.reject)
layout.addWidget(cancel_button)
self.setLayout(layout)
# Set focus to the input field
self.qr_input.setFocus()
def on_text_changed(self):
# When text is entered, emit the signal with the text
text = self.qr_input.toPlainText().strip()
if text:
self.qr_code_scanned.emit(text)
self.accept() # Close the dialog
class Barcode_Recipe_Selection(Test_Test):
def __init__(self, parent):
@ -81,15 +139,20 @@ class Barcode_Recipe_Selection(Test_Test):
lines = data.splitlines()
#lines = data.split("-")
candidates = [i for i in lines if len(i) in(9,10,11,12,13,17,18)]
if len(candidates)>0:
if len(candidates) > 0:
# RECIPE CODE FOUND
self.recipe=candidates[-1]
self.barcode_input_l.setPalette(self.status_palettes[True])
if "--write-tags" not in sys.argv:
self.ok_timer.start(2000)
if len(candidates) > 1:
# Multiple candidates found, open QR code scanner
self.open_qr_scanner(candidates)
else:
self.parent.components["rfid"].write_tag(data)
# Only one candidate, use it directly
self.recipe = candidates[0]
self.barcode_input_l.setPalette(self.status_palettes[True])
if "--write-tags" not in sys.argv:
self.ok_timer.start(2000)
else:
self.parent.components["rfid"].write_tag(data)
else:
# RECIPE CODE NOT FOUND
@ -117,6 +180,44 @@ class Barcode_Recipe_Selection(Test_Test):
else:
self.parent_assembly_widget().set_text("SCANSIONARE BARCODE SELEZIONE RICETTA")
def open_qr_scanner(self, candidates):
"""
Opens a dialog for scanning a QR code when multiple candidates are found.
Args:
candidates (list): List of candidate recipe codes found in the RFID tag.
"""
dialog = QRCodeScannerDialog(self, candidates)
dialog.qr_code_scanned.connect(self.on_qr_code_scanned)
dialog.exec_()
def on_qr_code_scanned(self, qr_code_text):
"""
Handles the QR code scan result.
Args:
qr_code_text (str): The text scanned from the QR code.
"""
if not qr_code_text:
QMessageBox.warning(self, "Attenzione", "Nessun codice QR rilevato. Riprovare.")
return
# Try to find a recipe with the name from the QR code
try:
recipe = self.recipe_db_model.get(self.recipe_db_model.name == qr_code_text)
# Recipe found, set it
self.recipe = str(recipe.id)
self.barcode_input_l.setPalette(self.status_palettes[True])
self.parent_assembly_widget().set_text(f"RICETTA '{recipe.name}' SELEZIONATA", bg_color="green")
self.ok_timer.start(2000)
except peewee.DoesNotExist:
# Recipe not found, show error
error_msg = f"Ricetta '{qr_code_text}' non trovata nel database. Verificare il codice QR e riprovare."
QMessageBox.warning(self, "Attenzione", error_msg)
self.barcode_input_l.setPalette(self.status_palettes[False])
self.parent_assembly_widget().set_text("RICETTA NON TROVATA", bg_color="red")
self.focus_timer.start(3000)
def tag_write(self, data_to_write=None):
self.parent_assembly_widget().set_text("SCRITTURA TAG NFC ", bg_color=" yellow")
tag_data = data_to_write if data_to_write else self.barcode_input_l.toPlainText().strip()