data_process_ui/app/widget_interact.py

131 lines
3.3 KiB
Python
Raw Permalink Normal View History

2026-01-12 09:21:42 +08:00
# -*- encoding:utf-8 -*-
import os
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtWidgets import (
QWidget, QDockWidget, QTabWidget, QVBoxLayout, QHBoxLayout,
QGridLayout, QPushButton, QLabel, QLineEdit, QGroupBox,
)
from PyQt6.QtGui import (
QFont, QIcon, QColor, QIntValidator, QDoubleValidator,
)
import color
from config import config, script_path
font = QFont("consolas", 14)
font.setFamilies(["consolas", "黑体"])
class edit_input(QLineEdit):
def __init__(self, window, id, *arg, **kwarg):
super().__init__(*arg, **kwarg)
self.window = window
self.id = id
self.textChanged.connect(self.on_text_changed)
def on_text_changed(self):
if self.id:
self.window.widget_interact.widget_grid.map_input[self.id] = self.text()
class button_input(QPushButton):
def __init__(self, window, script, *arg, **kwarg):
super().__init__(*arg, **kwarg)
self.window = window
self.script = script
self.clicked.connect(self.on_click)
def on_click(self):
if not self.script:
return
filename = os.path.join(script_path, self.script)
if not os.path.isfile(filename):
raise RuntimeError(f"the path is not a valid file: \"{filename}\"")
filename = filename.replace("\\", "\\\\")
filename = filename.replace("\"", "\\\"")
self.window.process(f"window.load_script(\"{filename}\")")
class widget_grid(QWidget):
def __init__(self, window, *arg, **kwarg):
super().__init__(*arg, **kwarg)
self.window = window
layout_base = QVBoxLayout()
self.setLayout(layout_base)
group_grid = QGroupBox()
layout_base.addWidget(group_grid)
self.layout_grid = QGridLayout()
group_grid.setLayout(self.layout_grid)
self.list_widget = []
self.map_input = {}
def load_interact(self, table_id):
for widget in self.list_widget:
self.layout_grid.removeWidget(widget)
widget.deleteLater()
self.list_widget.clear()
if not ("interact" in config):
return
config_interact = config["interact"]
if not (table_id in config_interact):
return
config_interact = config_interact[table_id]
row = 0
col = 0
for cfg in config_interact:
tp = cfg["type"]
label = cfg["label"]
if tp == "input":
if col > 0:
row += 1
col = 0
id = cfg.get("id")
if id in self.map_input:
text = self.map_input[id]
else:
text = cfg.get("text", "")
self.map_input[id] = text
label = QLabel(label)
label.setFont(font)
label.setToolTip(f"id:{id}")
edit = edit_input(self.window, id, text)
self.list_widget.append(label)
self.list_widget.append(edit)
self.layout_grid.addWidget(label, row, 0)
self.layout_grid.addWidget(edit, row, 1)
row += 1
elif tp == "button":
if "script" in cfg:
script = cfg["script"]
else:
script = None
button = button_input(self.window, script, label)
self.list_widget.append(button)
self.layout_grid.addWidget(button, row, col)
if col == 0:
col += 1
else:
row += 1
col = 0
class widget_interact(QDockWidget):
def __init__(self, window, *arg, **kwarg):
super().__init__(*arg, **kwarg)
self.window = window
self.setAllowedAreas(Qt.DockWidgetArea.LeftDockWidgetArea | Qt.DockWidgetArea.RightDockWidgetArea)
self.setFont(font)
self.widget_grid = widget_grid(window)
self.setWidget(self.widget_grid)
def load_interact(self, table_id):
self.widget_grid.load_interact(table_id)