From 8ad93e73b675befc152dc435ce3b42f10af3f5c9 Mon Sep 17 00:00:00 2001 From: zhaoyafan Date: Sat, 14 Dec 2024 10:23:26 +0800 Subject: [PATCH] 20241214102400 --- fast-print.json | 1 + main.py | 93 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 fast-print.json diff --git a/fast-print.json b/fast-print.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/fast-print.json @@ -0,0 +1 @@ +[] diff --git a/main.py b/main.py index b4804a8..2360efa 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,7 @@ import shutil import sqlite3 import logging import tempfile +import subprocess import pywintypes import hashlib import win32com.client @@ -213,6 +214,53 @@ def putJson(file, data=None): return res +class ShellResult(dict): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.code = self.stdout = self.stderr = None + + def __setattr__(self, key, value): + pass + + def __getitem__(self, item): + try: + return super().__getitem__(item) + except Exception: + return None + + def __getattr__(self, item): + try: + return super().__getitem__(item) + except Exception: + return None + + +def exec_shell(command='', stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=0) -> ShellResult: + stdout = stdout or subprocess.DEVNULL + stderr = stderr or subprocess.DEVNULL + try: + result = subprocess.run(command, shell=True, stdout=stdout, stderr=stderr, timeout=timeout or None) + if result.stdout: + stdout_text = result.stdout.decode('utf-8') + else: + stdout_text = '' + if result.stderr: + stderr_text = result.stderr.decode('utf-8') + else: + stderr_text = '' + return ShellResult({ + 'code': result.returncode, + 'stdout': stdout_text, + 'stderr': stderr_text + }) + except subprocess.TimeoutExpired: + return ShellResult({ + 'code': 1, + 'stdout': '', + 'stderr': 'Execution timeout.' + }) + + class SQLite3: __conn__ = None __curr__ = None @@ -628,17 +676,19 @@ class MainWindow(QMainWindow): def __init__(self, logger: Logger): super().__init__() self.app_name = '标签打印' - self.app_version = ('1.0.7', '20241204', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/') + self.app_version = ('1.0.8', '20241214', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/') self.logger = logger self.toast = ToastNotification() self.setting = Setting( os.path.abspath(os.path.join(tempfile.gettempdir(), '%s.config' % self.md5(__file__)[:16])), {}) self.bt = BarTenderPrint(logger=self.logger) - self.sv = ScanVerify(logger=self.logger, - rule_file=os.path.abspath(os.path.join(os.path.dirname(__file__), 'rules.json'))) + self.sv = ScanVerify(logger=self.logger, rule_file=os.path.abspath(os.path.join(os.path.dirname(__file__), 'rules.json'))) self.ditto = Ditto( db_file=os.path.abspath(os.path.join(tempfile.gettempdir(), '%s.db' % self.md5(__file__)[:16])), class_name='LabelPrint', limit_time=7776000, limit_rows=100000) + self.fast_print_data = getJson(os.path.abspath(os.path.join(os.path.dirname(__file__), 'fast-print.json')), []) + self.last_fast_print_i = 0 + self.last_fast_print_action = QAction('', self) self.last_opened_template = ['', ''] self.input_convert_letter = 0 self.input_scan_prohibited_enter = 0 @@ -646,6 +696,9 @@ class MainWindow(QMainWindow): self.task_timer = QTimer(self) self.task_timer.timeout.connect(self.task_execute) self.task_timer.start(100) + self.ui_timer = QTimer(self) + self.ui_timer.timeout.connect(self.update_ui) + self.ui_timer.start(1500) self.capslock_state_timer = QTimer(self) self.capslock_state_timer.timeout.connect(self.update_capslock_state) self.setWindowTitle(self.app_name) @@ -676,6 +729,16 @@ class MainWindow(QMainWindow): self.aboutWindowAction ] ) + fastmenu = None + for i in range(len(self.fast_print_data)): + if (fastmenu is None) == 1: + fastmenu = self.menubar.addMenu('快捷打印') + self.menubar.addAction(self.last_fast_print_action) + self.last_fast_print_action.triggered.connect(self.on_execute_fast_print_last) + data = self.fast_print_data[i] + act = QAction(str(data['name']), self) + act.triggered.connect(lambda checked, index=i: self.on_execute_fast_print(index)) + fastmenu.addAction(act) # 定义布局 m_layout_0 = CustomVBoxLayout() l_layout_0 = CustomVBoxLayout() @@ -810,6 +873,13 @@ class MainWindow(QMainWindow): else: self.TxtCAP.setText('') + def update_ui(self): + try: + self.bt.bt_app.IsPrinting and print('') + self.bt.bt_format and self.bt.bt_format.PrintSetup.Printer or print('') + except Exception: + self.close() + def load_setting(self): # load blockRepeat self.blockRepeatAction.setChecked(bool(self.setting['blockRepeat'])) @@ -824,6 +894,23 @@ class MainWindow(QMainWindow): tempfile_last = self.setting['template'] tempfile_last and os.path.exists(tempfile_last) and self.load_template(tempfile_last) + def on_execute_fast_print(self, action_index): + self.last_fast_print_i = action_index + self.last_fast_print_action.setText(self.fast_print_data[action_index]['name']) + execute_file = os.path.join(os.path.dirname(__file__), self.fast_print_data[action_index]['executable']) + if (os.path.exists(execute_file)) == 0: + self.toast.show_toast('无法找到脚本') + return None + result = exec_shell(command=execute_file, timeout=3).stdout.strip() + if (result != '') == 1: + self.toast.show_toast('获取数据成功') + self.on_start_printing(content=result) + else: + self.toast.show_toast('获取数据失败') + + def on_execute_fast_print_last(self): + self.on_execute_fast_print(self.last_fast_print_i) + def blockRepeatActionFunction(self, checked): self.setting['blockRepeat'] = bool(checked)