20241204151200
This commit is contained in:
parent
43a1424d18
commit
9d25c964d4
130
main.py
130
main.py
|
@ -17,7 +17,7 @@ import hashlib
|
||||||
import win32com.client
|
import win32com.client
|
||||||
import win32print
|
import win32print
|
||||||
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QComboBox, QCheckBox, QLineEdit, QAction, QMenu, \
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QLabel, QComboBox, QCheckBox, QLineEdit, QAction, QMenu, \
|
||||||
QMessageBox, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, QDialog
|
QMessageBox, QPushButton, QVBoxLayout, QHBoxLayout, QFileDialog, QDialog, QTableWidget, QTableWidgetItem, QSizePolicy
|
||||||
from PyQt5.QtCore import Qt, QCoreApplication, QTimer, QPropertyAnimation
|
from PyQt5.QtCore import Qt, QCoreApplication, QTimer, QPropertyAnimation
|
||||||
from PyQt5.QtGui import QPixmap, QIntValidator, QFocusEvent
|
from PyQt5.QtGui import QPixmap, QIntValidator, QFocusEvent
|
||||||
from threading import RLock
|
from threading import RLock
|
||||||
|
@ -440,7 +440,10 @@ class BarTenderPrint:
|
||||||
self.bt_format = None
|
self.bt_format = None
|
||||||
self.printer_list = [printer[2] for printer in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)][::-1]
|
self.printer_list = [printer[2] for printer in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)][::-1]
|
||||||
self.printer_current_index = 0
|
self.printer_current_index = 0
|
||||||
self.template_source_primary = {}
|
self.data_sources_cache = None
|
||||||
|
self.primary_ds_dict = {}
|
||||||
|
self.default_ds_name = 'A'
|
||||||
|
self.current_ds_name = self.default_ds_name
|
||||||
self.logger.i('%s%s' % ('Init', ''))
|
self.logger.i('%s%s' % ('Init', ''))
|
||||||
|
|
||||||
def set_label(self, data: str):
|
def set_label(self, data: str):
|
||||||
|
@ -450,7 +453,9 @@ class BarTenderPrint:
|
||||||
if (self.bt_format is not None) == 1:
|
if (self.bt_format is not None) == 1:
|
||||||
self.bt_format.Close(BarTender.BtSaveOptions.btDoNotSaveChanges)
|
self.bt_format.Close(BarTender.BtSaveOptions.btDoNotSaveChanges)
|
||||||
self.bt_format = self.bt_app.Formats.Open(self.label, False, '')
|
self.bt_format = self.bt_app.Formats.Open(self.label, False, '')
|
||||||
self.template_source_primary = {}
|
self.data_sources_cache = None
|
||||||
|
self.primary_ds_dict = {}
|
||||||
|
self.current_ds_name = self.default_ds_name
|
||||||
self.set_print(self.printer_current_index or 0)
|
self.set_print(self.printer_current_index or 0)
|
||||||
self.set_sheet(self.sheet or 1)
|
self.set_sheet(self.sheet or 1)
|
||||||
|
|
||||||
|
@ -467,21 +472,40 @@ class BarTenderPrint:
|
||||||
if (self.bt_format is not None) == 1:
|
if (self.bt_format is not None) == 1:
|
||||||
self.bt_format.PrintSetup.IdenticalCopiesOfLabel = self.sheet
|
self.bt_format.PrintSetup.IdenticalCopiesOfLabel = self.sheet
|
||||||
|
|
||||||
|
def set_current_ds_name(self, name: str):
|
||||||
|
self.current_ds_name = str(name)
|
||||||
|
self.logger.i('%s%s' % ('Set current data source name ', name))
|
||||||
|
|
||||||
|
def get_current_ds_name(self):
|
||||||
|
return self.current_ds_name
|
||||||
|
|
||||||
def set_data_source(self, name, value):
|
def set_data_source(self, name, value):
|
||||||
if name not in self.template_source_primary.keys():
|
if name not in self.primary_ds_dict.keys():
|
||||||
self.template_source_primary[name] = self.bt_format.GetNamedSubStringValue(name)
|
self.primary_ds_dict[name] = self.bt_format.GetNamedSubStringValue(name)
|
||||||
self.logger.i('%s%s' % ('Set data source ', '%s=%s' % (name, value)))
|
self.logger.i('%s%s' % ('Set data source ', '%s=%s' % (name, value)))
|
||||||
self.bt_format.SetNamedSubStringValue(name, value)
|
self.bt_format.SetNamedSubStringValue(name, value)
|
||||||
|
|
||||||
|
def get_data_source(self):
|
||||||
|
if self.data_sources_cache:
|
||||||
|
return self.data_sources_cache
|
||||||
|
else:
|
||||||
|
data = {}
|
||||||
|
delimiter = ['=>', '::']
|
||||||
|
for ds in [i for i in self.bt_format.NamedSubStrings.GetAll(delimiter[0], delimiter[1]).split(delimiter[1]) if i != '']:
|
||||||
|
ds_list = ds.split(delimiter[0])
|
||||||
|
data[ds_list[0]] = ds_list[1]
|
||||||
|
self.data_sources_cache = data
|
||||||
|
return data
|
||||||
|
|
||||||
def start_printing(self, content: str):
|
def start_printing(self, content: str):
|
||||||
for k, v in {'A': content}.items():
|
for k, v in {self.current_ds_name: content}.items():
|
||||||
self.set_data_source(k, v)
|
self.set_data_source(k, v)
|
||||||
if (self.bt_format is not None) == 1:
|
if (self.bt_format is not None) == 1:
|
||||||
self.logger.i('%s%s' % ('Printing ', 'normal'))
|
self.logger.i('%s%s' % ('Printing ', 'normal'))
|
||||||
self.bt_format.PrintOut(False, False)
|
self.bt_format.PrintOut(False, False)
|
||||||
|
|
||||||
def start_printing_template(self):
|
def start_printing_template(self):
|
||||||
for k, v in self.template_source_primary.items():
|
for k, v in self.primary_ds_dict.items():
|
||||||
self.set_data_source(k, v)
|
self.set_data_source(k, v)
|
||||||
if (self.bt_format is not None) == 1:
|
if (self.bt_format is not None) == 1:
|
||||||
self.logger.i('%s%s' % ('Printing ', 'template'))
|
self.logger.i('%s%s' % ('Printing ', 'template'))
|
||||||
|
@ -566,8 +590,7 @@ class CustomPushButton(QPushButton):
|
||||||
class CustomLabel(QLabel):
|
class CustomLabel(QLabel):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.setStyleSheet(
|
self.setStyleSheet('font-size: 12px; font-family: \'Microsoft YaHei\'; color: #0C0C0C; border: 1px solid #0C0C0C;')
|
||||||
'font-size: 12px; font-family: \'Microsoft YaHei\'; color: #0C0C0C; border: 1px solid #0C0C0C;')
|
|
||||||
|
|
||||||
|
|
||||||
class CustomText(QLabel):
|
class CustomText(QLabel):
|
||||||
|
@ -605,7 +628,7 @@ class MainWindow(QMainWindow):
|
||||||
def __init__(self, logger: Logger):
|
def __init__(self, logger: Logger):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.app_name = '标签打印'
|
self.app_name = '标签打印'
|
||||||
self.app_version = ('1.0.6', '20241128', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/')
|
self.app_version = ('1.0.7', '20241204', 'zhaoyafan', 'zhaoyafan@foxmail.com', 'https://www.fanscloud.net/')
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.toast = ToastNotification()
|
self.toast = ToastNotification()
|
||||||
self.setting = Setting(
|
self.setting = Setting(
|
||||||
|
@ -799,10 +822,7 @@ class MainWindow(QMainWindow):
|
||||||
self.edtNQT.setText(str(self.setting['printCopies'] or 1))
|
self.edtNQT.setText(str(self.setting['printCopies'] or 1))
|
||||||
# load template
|
# load template
|
||||||
tempfile_last = self.setting['template']
|
tempfile_last = self.setting['template']
|
||||||
if (tempfile_last and os.path.exists(tempfile_last)) == 1:
|
tempfile_last and os.path.exists(tempfile_last) and self.load_template(tempfile_last)
|
||||||
self.load_template(tempfile_last)
|
|
||||||
else:
|
|
||||||
self.setting['template'] = ''
|
|
||||||
|
|
||||||
def blockRepeatActionFunction(self, checked):
|
def blockRepeatActionFunction(self, checked):
|
||||||
self.setting['blockRepeat'] = bool(checked)
|
self.setting['blockRepeat'] = bool(checked)
|
||||||
|
@ -899,15 +919,25 @@ class MainWindow(QMainWindow):
|
||||||
if (isinstance(name, str)) == 1:
|
if (isinstance(name, str)) == 1:
|
||||||
self.edtNTP.setText(name)
|
self.edtNTP.setText(name)
|
||||||
|
|
||||||
|
def set_current_data_source_name(self, name):
|
||||||
|
name = name or self.bt.default_ds_name
|
||||||
|
self.bt.set_current_ds_name(name)
|
||||||
|
self.setting['current_data_source_name'] = name
|
||||||
|
|
||||||
|
def get_current_data_source_name(self):
|
||||||
|
return self.setting['current_data_source_name'] or self.bt.default_ds_name
|
||||||
|
|
||||||
def load_template(self, file):
|
def load_template(self, file):
|
||||||
try:
|
try:
|
||||||
if (file != '' and os.path.exists(file)) == 1:
|
if (file != '' and os.path.exists(file)) == 1:
|
||||||
self.bt.set_label(file)
|
self.bt.set_label(file)
|
||||||
self.set_template_name(os.path.splitext(os.path.basename(file))[0])
|
self.set_template_name(os.path.splitext(os.path.basename(file))[0])
|
||||||
self.set_preview(self.bt.generate_preview())
|
self.set_preview(self.bt.generate_preview())
|
||||||
|
self.set_current_data_source_name(self.get_current_data_source_name())
|
||||||
self.last_opened_template[0] = file
|
self.last_opened_template[0] = file
|
||||||
self.last_opened_template[1] = self.md5(open(file, 'rb'))
|
self.last_opened_template[1] = self.md5(open(file, 'rb'))
|
||||||
self.toast.show_toast('模板加载成功')
|
self.toast.show_toast('模板加载成功')
|
||||||
|
return True
|
||||||
except pywintypes.com_error as e:
|
except pywintypes.com_error as e:
|
||||||
t = str(e.args[1])
|
t = str(e.args[1])
|
||||||
m = str(e.args[2][2] if isinstance(e.args[2], tuple) else e.args[2])
|
m = str(e.args[2][2] if isinstance(e.args[2], tuple) else e.args[2])
|
||||||
|
@ -921,11 +951,13 @@ class MainWindow(QMainWindow):
|
||||||
QMessageBox.critical(self, '错误', '%s' % e)
|
QMessageBox.critical(self, '错误', '%s' % e)
|
||||||
|
|
||||||
def on_open_template_file(self):
|
def on_open_template_file(self):
|
||||||
filename, _ = QFileDialog.getOpenFileName(self, '打开文件', os.path.dirname(self.last_opened_template[0]),
|
filename, _ = QFileDialog.getOpenFileName(self, '打开文件', os.path.dirname(self.last_opened_template[0]), 'BarTender 文档 (*.btw)')
|
||||||
'BarTender 文档 (*.btw)')
|
|
||||||
if (filename and os.path.exists(filename)) == 1:
|
if (filename and os.path.exists(filename)) == 1:
|
||||||
self.load_template(filename)
|
if (self.load_template(filename)) == 1:
|
||||||
self.setting['template'] = filename
|
self.setting['template'] = filename
|
||||||
|
self.set_current_data_source_name('')
|
||||||
|
if self.bt.default_ds_name not in self.bt.get_data_source().keys():
|
||||||
|
DataWindow(self).exec()
|
||||||
|
|
||||||
def on_test_print(self):
|
def on_test_print(self):
|
||||||
try:
|
try:
|
||||||
|
@ -1067,6 +1099,70 @@ class MainWindow(QMainWindow):
|
||||||
QMessageBox.critical(self, '错误', '%s' % e)
|
QMessageBox.critical(self, '错误', '%s' % e)
|
||||||
|
|
||||||
|
|
||||||
|
class DataWindow(QDialog):
|
||||||
|
def __init__(self, mainwindow):
|
||||||
|
super().__init__()
|
||||||
|
self.ds_list = []
|
||||||
|
self.mainwindow = mainwindow
|
||||||
|
self.setGeometry(0, 0, 205, 170)
|
||||||
|
self.setWindowTitle('数据源名选择')
|
||||||
|
self.tableWidget = QTableWidget(self)
|
||||||
|
self.tableWidget.setFixedSize(205, 170)
|
||||||
|
self.tableWidget.setRowCount(0)
|
||||||
|
self.tableWidget.setColumnCount(2)
|
||||||
|
self.tableWidget.setHorizontalHeaderLabels(['名称', '选择'])
|
||||||
|
layout = QVBoxLayout()
|
||||||
|
layout.addWidget(self.tableWidget)
|
||||||
|
self.setLayout(layout)
|
||||||
|
self.tableWidget.verticalHeader().hide()
|
||||||
|
self.tableWidget.setColumnWidth(0, 120)
|
||||||
|
self.tableWidget.setColumnWidth(1, 60)
|
||||||
|
|
||||||
|
# 显示界面
|
||||||
|
screen_rect = QApplication.desktop().availableGeometry()
|
||||||
|
window_rect = self.geometry()
|
||||||
|
self.setFixedSize(self.minimumSizeHint())
|
||||||
|
self.move(int((screen_rect.width() - window_rect.width()) * 0.5), int((screen_rect.height() - window_rect.height()) * 0.5))
|
||||||
|
self.show()
|
||||||
|
self.list_update()
|
||||||
|
|
||||||
|
def list_update(self):
|
||||||
|
try:
|
||||||
|
ds_list = self.mainwindow.bt.get_data_source().keys()
|
||||||
|
except AttributeError:
|
||||||
|
self.close()
|
||||||
|
return None
|
||||||
|
if (self.ds_list == ds_list) == 1:
|
||||||
|
return None
|
||||||
|
self.ds_list = ds_list
|
||||||
|
self.tableWidget.setRowCount(0)
|
||||||
|
for row, ds in enumerate(ds_list):
|
||||||
|
self.tableWidget.insertRow(row)
|
||||||
|
self.tableWidget.setItem(row, 0, QTableWidgetItem('%s' % ds))
|
||||||
|
selectButton = QPushButton('选择', self)
|
||||||
|
selectButton.clicked.connect(lambda _, ds=ds: self.set_ds(ds))
|
||||||
|
cell_widget = QWidget()
|
||||||
|
cell_layout = QHBoxLayout(cell_widget)
|
||||||
|
cell_layout.addWidget(selectButton)
|
||||||
|
cell_layout.setAlignment(selectButton, Qt.AlignHCenter)
|
||||||
|
cell_layout.setContentsMargins(0, 0, 0, 0)
|
||||||
|
self.tableWidget.setCellWidget(row, 1, cell_widget)
|
||||||
|
for row in range(self.tableWidget.rowCount()):
|
||||||
|
for col in range(self.tableWidget.columnCount()):
|
||||||
|
item = self.tableWidget.item(row, col)
|
||||||
|
item and item.setFlags(item.flags() & ~Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled)
|
||||||
|
|
||||||
|
def set_ds(self, ds):
|
||||||
|
self.mainwindow.set_current_data_source_name(ds)
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def closeEvent(self, event):
|
||||||
|
event.accept()
|
||||||
|
|
||||||
|
def keyPressEvent(self, event):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ToastNotification(QDialog):
|
class ToastNotification(QDialog):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
Loading…
Reference in New Issue