From 9352d52ad4704e58ce480cea20d3239db121ba29 Mon Sep 17 00:00:00 2001 From: zhaoyafan Date: Sat, 14 Dec 2024 19:14:42 +0800 Subject: [PATCH] 20241214191500 --- main.py | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 2360efa..01ec743 100644 --- a/main.py +++ b/main.py @@ -641,6 +641,12 @@ class CustomLabel(QLabel): self.setStyleSheet('font-size: 12px; font-family: \'Microsoft YaHei\'; color: #0C0C0C; border: 1px solid #0C0C0C;') +class CustomImageLabel(QLabel): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.setStyleSheet('font-size: 12px; font-family: \'Microsoft YaHei\'; color: #0C0C0C; border: 2px dashed #999999;') + + class CustomText(QLabel): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -676,7 +682,7 @@ class MainWindow(QMainWindow): def __init__(self, logger: Logger): super().__init__() self.app_name = '标签打印' - self.app_version = ('1.0.8', '20241214', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/') + self.app_version = ('1.0.9', '20241215', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/') self.logger = logger self.toast = ToastNotification() self.setting = Setting( @@ -685,10 +691,14 @@ class MainWindow(QMainWindow): 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) + class_name='LabelPrint', limit_time=7776000, limit_rows=100000 + ) + self.open_file_extension = {'class': 'BarTender 文档 (*.btw)', 'extension': '.btw'} 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_i = None + self.last_fast_print_action_shortcut = 'F10' self.last_fast_print_action = QAction('', self) + self.last_fast_print_action.setShortcut(self.last_fast_print_action_shortcut) self.last_opened_template = ['', ''] self.input_convert_letter = 0 self.input_scan_prohibited_enter = 0 @@ -703,6 +713,7 @@ class MainWindow(QMainWindow): self.capslock_state_timer.timeout.connect(self.update_capslock_state) self.setWindowTitle(self.app_name) self.setGeometry(0, 0, 600, 400) + self.setAcceptDrops(True) self.menubar = self.menuBar() filemenu = self.menubar.addMenu('设置') self.blockRepeatAction = QAction('禁止重复打印', self, checkable=True) @@ -759,7 +770,7 @@ class MainWindow(QMainWindow): # Select file self.butSTP = CustomPushButton('选择文件') self.butSTP.setFixedSize(82, 27) - self.butSTP.clicked.connect(self.on_open_template_file) + self.butSTP.clicked.connect(self.on_open_template) l_layout_1.addWidget(self.butSTP) # Template name self.edtNTP = CustomLineEditNoPopup('') @@ -772,7 +783,7 @@ class MainWindow(QMainWindow): self.butTPT.clicked.connect(self.on_test_print) l_layout_1.addWidget(self.butTPT) # Print preview - self.labPRV = CustomLabel() + self.labPRV = CustomImageLabel() self.labPRV.setAlignment(Qt.AlignCenter) self.labPRV.setFixedSize(455, 256) l_layout_2.addWidget(self.labPRV) @@ -846,6 +857,17 @@ class MainWindow(QMainWindow): self.edtSCN.setFocus() self.show() self.load_setting() + len(sys.argv) == 2 and os.path.isfile(sys.argv[1]) and os.path.basename(sys.argv[1]).lower().endswith(self.open_file_extension['extension']) and self.on_open_template_file(sys.argv[1]) + + def dragEnterEvent(self, event): + if (event.mimeData().hasUrls() and len(event.mimeData().urls()) == 1) == 1: + file = event.mimeData().urls()[0].toLocalFile() + event.accept() if os.path.isfile(file) and os.path.basename(file).lower().endswith(self.open_file_extension['extension']) else event.ignore() + return None + event.ignore() + + def dropEvent(self, event): + self.on_open_template_file(event.mimeData().urls()[0].toLocalFile()) def closeEvent(self, event): self.bt.quit() @@ -896,7 +918,7 @@ class MainWindow(QMainWindow): 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']) + self.last_fast_print_action.setText('%s (%s)' % (self.fast_print_data[action_index]['name'], self.last_fast_print_action_shortcut)) 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('无法找到脚本') @@ -909,7 +931,7 @@ class MainWindow(QMainWindow): self.toast.show_toast('获取数据失败') def on_execute_fast_print_last(self): - self.on_execute_fast_print(self.last_fast_print_i) + self.last_fast_print_i is not None and self.on_execute_fast_print(self.last_fast_print_i) def blockRepeatActionFunction(self, checked): self.setting['blockRepeat'] = bool(checked) @@ -1037,8 +1059,14 @@ class MainWindow(QMainWindow): except Exception as e: QMessageBox.critical(self, '错误', '%s' % e) - def on_open_template_file(self): - filename, _ = QFileDialog.getOpenFileName(self, '打开文件', os.path.dirname(self.last_opened_template[0]), 'BarTender 文档 (*.btw)') + def on_open_template(self): + self.on_open_template_file() + + def on_open_template_file(self, file=''): + if (file != '') == 1: + filename = file + else: + filename, _ = QFileDialog.getOpenFileName(self, '打开文件', os.path.dirname(self.last_opened_template[0]), self.open_file_extension['class']) if (filename and os.path.exists(filename)) == 1: if (self.load_template(filename)) == 1: self.setting['template'] = filename