detect-gui/widget/task_result_embed.py

45 lines
1.7 KiB
Python
Raw Normal View History

2025-02-14 14:22:23 +08:00
import math
2025-02-16 13:06:15 +08:00
from PyQt5.QtGui import QPixmap
2025-02-14 14:22:23 +08:00
from PyQt5.QtWidgets import QFrame, QWidget, QPushButton
import json
from exceptiongroup import catch
2025-02-16 15:32:07 +08:00
class TaskResultEmbedFrame(QFrame):
2025-02-14 14:22:23 +08:00
def __init__(self, task, parent=None):
super(TaskResultEmbedFrame, self).__init__(parent)
self.task = task
self.embed_buttons = []
def showEvent(self, e):
params = json.loads(self.task[3])
try:
max_x = math.ceil(float(max(params, key=lambda p: float(p["x"]))["x"]) / 1000) * 1000
max_y = math.ceil(float(max(params, key=lambda p: float(p["center"]))["center"])) * 1000
w = self.geometry().width()
h = self.geometry().height()
scale = min(max_x / w, max_y / h)
for param in params:
embed_button = QPushButton(param["code"], self)
embed_button.setObjectName("embedButton")
embed_button.setFixedSize(math.ceil(float(param["w"]) / scale), math.ceil(float(param["h"]) / scale))
left = math.ceil((float(param["x"]) - float(param["w"]) / 2) / scale)
top = h - math.ceil(float(param["center"]) * 1000 / scale)
embed_button.move(left, top)
embed_button.setVisible(True)
2025-02-16 13:06:15 +08:00
# p = self.grab()
# p.save("d:\\demo.bmp", "bmp")
2025-02-14 14:22:23 +08:00
except ValueError:
print("任务参数有错误!")
def setChecked(self, text):
for btn in self.embed_buttons:
if btn.text() == text:
btn.setStyleSheet("border: 1px solid #6cff6c;")
def setError(self, text):
for btn in self.embed_buttons:
if btn.text() == text:
btn.setStyleSheet("border: 1px solid #FF4444;")