import os # from .data import DataModeGenerateDetail from .base_deal import BaseDealImage from .run_main import RunMain from utils.utils_func import check_path from .upload_pic import UploadPic # 手动移动图片等操作 from .manual_image_matching.m_image_matching_cotrol import ( MainMatchingWindow, ) # 每个详情的模板 from .detail_temp import TempItem from .load_plugins import LoadAllPlugins from functools import partial from threading import Lock # from module.view_control.MineQWidget import MineQWidget, DialogShow # from module.base_mode.base import * import threading import settings def queren_get_config(): config_data = {} # config_data["cutout_is_enable"] = self.ui.cutout_is_enable.isChecked() # config_data["cutout_is_pass"] = self.ui.cutout_is_pass.isChecked() # config_data["detail_is_enable"] = self.ui.detail_is_enable.isChecked() # config_data["detail_is_pass"] = self.ui.detail_is_pass.isChecked() # config_data["upload_is_enable"] = self.ui.upload_is_enable.isChecked() # config_data["upload_is_pass"] = self.ui.upload_is_pass.isChecked() # # 保存配置 # settings.GOODS_DETAIL_CUTOUT_IS_ENABLE = self.ui.cutout_is_enable.isChecked() # settings.GOODS_DETAIL_CUTOUT_IS_PASS = self.ui.cutout_is_pass.isChecked() # settings.GOODS_DETAIL_DETAIL_IS_ENABLE = self.ui.detail_is_enable.isChecked() # settings.GOODS_DETAIL_DETAIL_IS_PASS = self.ui.detail_is_pass.isChecked() # settings.GOODS_DETAIL_UPLOAD_IS_ENABLE = self.ui.upload_is_enable.isChecked() # settings.GOODS_DETAIL_UPLOAD_IS_PASS = self.ui.upload_is_pass.isChecked() data_dict = { # "goods_detail_cutout_is_enable": ( # "是" if settings.GOODS_DETAIL_CUTOUT_IS_ENABLE else "否" # ), # "goods_detail_cutout_is_pass": ( # "是" if settings.GOODS_DETAIL_CUTOUT_IS_PASS else "否" # ), # "goods_detail_detail_is_enable": ( # "是" if settings.GOODS_DETAIL_DETAIL_IS_ENABLE else "否" # ), # "goods_detail_detail_is_pass": ( # "是" if settings.GOODS_DETAIL_DETAIL_IS_PASS else "否" # ), # "goods_detail_upload_is_enable": ( # "是" if settings.GOODS_DETAIL_UPLOAD_IS_ENABLE else "否" # ), # "goods_detail_upload_is_pass": ( # "是" if settings.GOODS_DETAIL_UPLOAD_IS_PASS else "否" # ), "goods_detail_cutout_is_enable": "是", "goods_detail_cutout_is_pass": "是", "goods_detail_detail_is_enable": "是", "goods_detail_detail_is_pass": "是", "goods_detail_upload_is_enable": "是", "goods_detail_upload_is_pass": "是", } settings.set_config(data_dict=data_dict, section="goods_detail") return config_data # 抠图与详情生成 class AutoDealPics: # progress_sign = Signal(dict) # text_show = Signal(str) # progress_show_sign = Signal(bool) # progress_end_sign = Signal() # send_dialog_sign = Signal() # change_state_sign = Signal(int) # run_end_sign = Signal(dict) def __init__(self, windows=None, top_windows=None): super().__init__() self.windows = windows self.top_windows = top_windows # self.ui = generate_goods_no_detail_pic_Ui_Form() # self.ui.setupUi(self) # self.setWindowFlags(self.windowFlags() & ~Qt.WindowStaysOnTopHint) self.run_main = RunMain(windows=self) # self.run_main.run_end_sign.connect(self.deal_run_end_sign) # self.run_main.show_dialog_sign.connect(self.show_dialog) # self.run_main.show_progress_detail_sign.connect(self.show_progress_detail) # 程序执行信号 # self.run_end_sign.connect(self.deal_run_end_sign) # # 改变状态 # self.change_state_sign.connect(self.set_state) # self.setWindowTitle("详情生成(new)") # 线程锁 self.lock = Lock() self.image_dir = "" # 0禁用 1进行中 2已结束 99等待结束 self.state = 2 self.data_mode_generate_detail = DataModeGenerateDetail() self.init_cutout() self.init_detail() # self.init_schedule() self.set_state(0) # self.show() # 延时检查是否存在匹配图片 # QTimer.singleShot(1000, self.check_and_matching_pics) # 添加进度页面 # def init_schedule(self, *args): # self.ui.back.clicked.connect(self.back) # self.ui.stop.clicked.connect(self.to_stop) # self.ui.progressBar.hide() def back(self, *args): self.set_state(0) def to_stop(self, *args): self.state = 99 # 自动检查匹配图片 def check_and_matching_pics(self, *args, **kwargs): func = partial(self.run_matching) self.do_thread_run(func=func, call_back=self.run_matching_call, time_out=30) # 运行自动匹配 def run_matching(self, *args): image_dir = "{}/data".format(os.getcwd()) check_path(image_dir) baseDealImage = BaseDealImage(image_dir=image_dir) result = baseDealImage.dealMoveImage(image_dir=image_dir, callback_func=None) # return {'code': 0, 'msg': '处理完成', 'target_path': output_path, 'data': {}} return result # 自动匹配的回调函数 def run_matching_call(self, return_data): print(return_data) code = return_data["code"] if code == 0: output_path = return_data["target_path"] self.image_dir = "{}/{}".format(os.getcwd(), output_path) self.show_img_dir() def show_progress_detail(self, text): self.ui.textBrowser_4.append(text) # todo ===========主方法入口=========== def deal_run_end_sign(self, config_data: dict): if config_data["sign_text"] == "开始抠图": # 先做整体校验 func = partial(self.run_main.check_before_cutout, config_data=config_data) self.do_thread_run( func=func, call_back=self.run_main.check_for_cutout_image_first_call_back, time_out=30, is_show_mask=False, ) print(config_data) if config_data["sign_text"] == "已结束抠图处理": if config_data["detail_is_enable"]: # 先做整体校验 # temp_name=self.last_temp, temp_name_list=self.temp_list func = partial( self.run_main.check_before_detail, config_data=config_data ) self.do_thread_run( func=func, call_back=self.run_main.check_for_detail_first_call_back, time_out=30, is_show_mask=False, ) else: self.set_state(state_value=2) if config_data["sign_text"] == "已结束详情处理": if config_data["upload_is_enable"]: to_deal_dir = "{}/软件-详情图生成".format(config_data["image_dir"]) print("to_deal_dir", to_deal_dir) if os.path.exists(to_deal_dir): self.upload_pic = UploadPic( windows=self, to_deal_dir=to_deal_dir, config_data=config_data ) self.upload_pic.run_end_sign.connect(self.deal_run_end_sign) self.upload_pic.show_progress_detail_sign.connect( self.show_progress_detail ) self.upload_pic.run() # threading.Thread(target=self.upload_pic.run_by_thread, args=()).start() else: self.set_state(state_value=2) else: self.set_state(state_value=2) if config_data["sign_text"] == "结束": self.set_state(state_value=2) def change_temp_mode(self, *args, **kwargs): if self.ui.check_use_excel.isChecked(): self.ui.temp_list_get.hide() else: self.ui.temp_list_get.show() # 重新载入模块 def reload_temp_mode(self, *args, **kwargs): a = QMessageBox.question( self, "确认", "是否重启加载模板", QMessageBox.Yes | QMessageBox.No ) if a != QMessageBox.Yes: return # 检查资源包 if settings.env == "prod": if self.top_windows: self.top_windows.check_resources_download() settings.PLUGINS_DICT = LoadAllPlugins(windows=None).load() # 重新载入模板 self.load_temp_list() # 获取页面配置 def get_config(self, *args): if not settings.IsLogin: QMessageBox.question( self, "确认", "请先登录", QMessageBox.Yes | QMessageBox.No ) return logo_name = self.ui.logo_select_ui.currentText() if logo_name == "请选择": QMessageBox.question( self, "确认", "请先选择logo模板", QMessageBox.Yes | QMessageBox.No ) return if self.ui.cutout_exquisite.isChecked() and not bool(settings.IsExquisiteMode): QMessageBox.question( self, "确认", "暂无 精细化抠图权限", QMessageBox.Yes | QMessageBox.No ) return if "失败" in self.image_dir: self.WaringMessage("文件夹路径不能含有 失败 字样") return image_dir = self.ui.path_name.text() image_order = self.ui.line_image_pos.text() is_check_number = self.ui.check_image_total.isChecked() resize_image_view = self.ui.need_resize.text() logo_name = self.ui.logo_select_ui.currentText() settings.MATCHING_LAST_LOGO_TEMP = logo_name logo_path = self.logo_path_dict[logo_name] cutout_mode = "1" if self.ui.cutout_exquisite.isChecked(): cutout_mode = "2" configData = { "imageorder": image_order, "resize_image_view": resize_image_view, "is_check_number": str(is_check_number), "cutout_mode": str(cutout_mode), "matching_mode_last": settings.MATCHING_MODE_LAST, "matching_last_temp": self.last_temp, "matching_last_logo_temp": logo_name, } settings.cutimage_dict["imageorder"] = image_order settings.cutimage_dict["resize_image_view"] = resize_image_view settings.cutimage_dict["is_check_number"] = str(is_check_number) settings.cutimage_dict["cutout_mode"] = str(cutout_mode) settings.set_config(data_dict=configData, section="cutimage") config_data = {} config_data["image_dir"] = image_dir config_data["image_order"] = image_order config_data["is_check_number"] = is_check_number config_data["resize_image_view"] = resize_image_view config_data["cutout_mode"] = cutout_mode config_data["logo_path"] = logo_path config_data["special_goods_art_no_folder_line"] = "" # ======================================= settings.GOODS_DETAIL_IS_USE_EXCEL = self.is_use_excel settings.GOODS_DETAIL_EXCEL_PATH = self.excel_path settings.GOODS_DETAIL_LAST_IMAGE_PATH = self.image_dir settings.GOODS_DETAIL_IS_CHECK_COLOR_IS_ALL = self.is_check_color_is_all settings.GOODS_DETAIL_IS_PASS = self.is_check_is_pass settings.GOODS_DETAIL_LAST_TEMP = self.last_temp data_dict = { "is_use_excel": "是" if self.is_use_excel else "否", "excel_path": self.excel_path if self.excel_path else "", "last_image_path": self.image_dir if self.image_dir else "", "is_check_color_is_all": "是" if self.is_check_color_is_all else "否", "last_temp": self.last_temp, "goods_detail_temp_mode_last": settings.GOODS_DETAIL_TEMP_MODE_LAST, } settings.set_config(data_dict=data_dict, section="goods_detail") # 红蜻蜓/惠利玛,且为非表格数据必须需要登录处理 assigned_page = self.ui.assigned_page.text() if assigned_page: assigned_page_dict = self.check_assigned_page(assigned_page) if not assigned_page_dict: self.WaringMessage("指定模板填写格式不合规") else: assigned_page_dict = {} config_data["is_use_excel"] = self.is_use_excel config_data["excel_path"] = self.excel_path config_data["is_check_color_is_all"] = self.is_check_color_is_all config_data["assigned_page_dict"] = assigned_page_dict config_data["temp_class"] = self.temp_class config_data["temp_name"] = self.last_temp config_data["temp_name_list"] = self.temp_list config_data["target_error_folder"] = self.target_error_folder print("=============config_data=============") print(config_data) return config_data # 抠图相关处理 def init_cutout(self): # 指定单独处理的颜色文件夹 self.special_goods_art_no_folder = [] self.show_img_dir() self.ui.select_path.mousePressEvent = self.change_img_dir self.ui.line_image_pos.setText(settings.cutimage_dict["imageorder"]) self.ui.need_resize.setText(settings.cutimage_dict["resize_image_view"]) self.ui.check_image_total.setChecked( bool(settings.cutimage_dict["is_check_number"]) ) if settings.DEFAULT_CUTOUT_MODE == "普通抠图": self.ui.cutout_nomal.setChecked(True) elif settings.DEFAULT_CUTOUT_MODE == " 精细化抠图": self.ui.cutout_exquisite.setChecked(True) if not bool(settings.IsExquisiteMode): self.ui.cutout_exquisite.setEnabled(False) self.progress_sign.connect(self.show_progress) self.ui.run.clicked.connect(self.run) # 抠图与主图加工 self.set_logo_selected() # 手动匹配图片 self.ui.manual_matching_pic.mousePressEvent = ( self.manual_matching_pic_mousePressEvent ) def init_detail(self): # 使用使用表格数据 self.is_use_excel = settings.GOODS_DETAIL_IS_USE_EXCEL self.excel_path = settings.GOODS_DETAIL_EXCEL_PATH # 目标文件夹 self.image_dir = settings.GOODS_DETAIL_LAST_IMAGE_PATH # 对应的错误文件夹目录 if self.image_dir and os.path.exists(self.image_dir): self.target_error_folder = "{}/软件-生成详情错误".format(self.image_dir) else: self.target_error_folder = "" # 检查颜色是否齐全 self.is_check_color_is_all = settings.GOODS_DETAIL_IS_CHECK_COLOR_IS_ALL # 已生成主图的自动过滤 self.is_check_is_pass = settings.GOODS_DETAIL_IS_PASS # 上次使用的详情模板 self.last_temp = settings.GOODS_DETAIL_LAST_TEMP # 重新载入模块 self.ui.reload_func.mousePressEvent = self.reload_temp_mode # 手工整理数据 self.ui.manual_matching_pic.mousePressEvent = self.manual_matching_pic # -------初始化页面-------- if self.is_use_excel: self.ui.check_use_excel.setChecked(True) else: self.ui.check_use_excel.setChecked(False) if self.excel_path: self.ui.excel_path_show.setText(self.excel_path) if self.image_dir: self.ui.path_name.setText(self.image_dir) if self.is_check_color_is_all: self.ui.check_image_total_2.setChecked(True) else: self.ui.check_image_total_2.setChecked(False) # 是否开启使用外部数据 self.ui.check_use_excel.toggled.connect(self.check_box_change) self.ui.check_image_total_2.toggled.connect(self.check_box_change) self.check_box_change() self.ui.select_path.mousePressEvent = self.change_img_dir self.ui.select_excel.mousePressEvent = self.select_excel_path # ==================================== self.progress_sign.connect(self.show_progress) # 复制Excel模板 self.ui.get_excel_temp.mousePressEvent = self.copy_excel self.change_temp_mode() # 加载模板 QTimer.singleShot(1000, self.load_temp_list) def manual_matching_pic_mousePressEvent(self, event): if not self.image_dir: a = QMessageBox.question( self, "确认", "请先选择目标文件夹", QMessageBox.Yes, ) return self.manual = MainMatchingWindow(root_path=self.image_dir) # 手工整理数据 def manual_matching_pic(self, *args, **kwargs): if not self.image_dir: a = QMessageBox.question( self, "确认", "请先选择目标文件夹", QMessageBox.Yes, ) return self.manual = MainMatchingWindow(root_path=self.image_dir) def load_temp_list(self): # 注册模板类 self.temp_class = {} self.temp_list = [] plugins_company_name = "企业名称" if settings.PROJECT == "红蜻蜓": self.temp_class = {} plugins_company_name = "红蜻蜓" elif settings.PROJECT == "惠利玛": if "惠利玛" in settings.Company: self.temp_class = {} plugins_company_name = "惠利玛" if "小苏" in settings.Company: self.temp_class = {} plugins_company_name = "小苏" # 通过动态进行加载获取 if settings.PLUGINS_DICT: print("554 settings.PLUGINS_DICT") for _key, _value in settings.PLUGINS_DICT["detail_template"].items(): print(_key, _value) try: if "全部" in settings.PLUGINS_DICT["detail_template"]: for i, v in settings.PLUGINS_DICT["detail_template"][ "全部" ].items(): self.temp_class[i] = v if plugins_company_name in settings.PLUGINS_DICT["detail_template"]: for i, v in settings.PLUGINS_DICT["detail_template"][ plugins_company_name ].items(): self.temp_class[i] = v except BaseException as e: print("209 通过动态进行加载详情获取error", e) pass # 模板名称列表 self.temp_list = [x for x in self.temp_class] print("570 temp_list", self.temp_list) # 删除已渲染的模板 for i in self.ui.show_temp_list.findChildren(TempItem): i.deleteLater() # 模板列表 x, y = 10, 10 self.ui.show_temp_list.setMinimumSize(self.ui.widget_10.width(), 100) self.ui.show_temp_list.resize(self.ui.widget_10.width(), 100) temp_high = 100 for temp_name in self.temp_list: _class = self.temp_class[temp_name] _d = _class.get_temp_pic_info(_class.root) temp_image_path = _d["temp_pic_path"] # print("224 temp_image_path",temp_image_path) # temp_item = TempItem(self.ui.scrollArea, temp_name, temp_image_path) # temp_item.select_sign.connect(self.change_temp_select) # self.ui.horizontalLayout_18.insertWidget( # self.ui.horizontalLayout_18.count() - 1, temp_item # ) temp_item = TempItem(self.ui.show_temp_list, temp_name, temp_image_path) temp_high = temp_item.height() temp_item.select_sign.connect(self.change_temp_select) temp_item.move(x, y) x += temp_item.width() x += 10 if x > self.ui.show_temp_list.width() - temp_item.width(): y += temp_item.height() y += 10 x = 10 y += 100 y += temp_high print(y) self.ui.scrollAreaWidgetContents_3.setMinimumSize(x, self.height() + y) self.ui.show_temp_list.setMinimumSize(x, y) self.ui.show_temp_list.resize(x, y) print(self.ui.show_temp_list.height()) # 设置默认值 if self.last_temp not in self.temp_list: if self.temp_list: self.last_temp = self.temp_list[0] # 设置值 for temp_item in self.ui.show_temp_list.findChildren(TempItem): temp_item.select() break def change_temp_select(self, _id): self.last_temp = _id print("已设置模板的值为:{}".format(self.last_temp)) for temp_item in self.ui.show_temp_list.findChildren(TempItem): if temp_item.id != _id: temp_item.cancel_select() def copy_excel(self, *args, **kwargs): excel_path = r"{}\resources\init\goods_excel_temp.xlsx".format(os.getcwd()) if not os.path.exists(excel_path): return self.check_path(r"{}\temp".format(os.getcwd())) self.check_path(r"{}\temp\excel_temp".format(os.getcwd())) new_excel_path = r"{}\temp\excel_temp\goods_excel_temp.xlsx".format(os.getcwd()) if not os.path.exists(new_excel_path): shutil.copy(excel_path, new_excel_path) # 打开文件夹 os.startfile(r"{}\temp\excel_temp".format(os.getcwd())) def check_box_change(self, *args): if self.ui.check_use_excel.isChecked(): self.is_use_excel = True self.ui.widget.show() self.ui.temp_list_get.hide() else: self.is_use_excel = False self.ui.widget.hide() self.ui.temp_list_get.show() if self.ui.check_image_total_2.isChecked(): self.is_check_color_is_all = True else: self.is_check_color_is_all = False # 设置选择logo模板 def set_logo_selected(self): logo_root_path = "" if settings.PROJECT == "红蜻蜓": logo_root_path = r"{}\resources\LOGO\HQT".format(os.getcwd()) elif settings.PROJECT == "惠利玛": if "小苏" in settings.Company: logo_root_path = r"{}\resources\LOGO\xiaosushuoxie".format(os.getcwd()) if "惠利玛" in settings.Company: logo_root_path = r"{}\resources\LOGO\HLM".format(os.getcwd()) self.logo_path_dict = {"请选择": "", "无logo": ""} if logo_root_path: if os.path.exists(logo_root_path): image_list = get_images(logo_root_path) for image_data in image_list: self.logo_path_dict[image_data["file_name"]] = image_data[ "file_path" ] self.ui.logo_select_ui.addItems([x for x in self.logo_path_dict]) self.ui.logo_select_ui.setCurrentText("请选择") if settings.MATCHING_LAST_LOGO_TEMP: if settings.MATCHING_LAST_LOGO_TEMP in [x for x in self.logo_path_dict]: self.ui.logo_select_ui.setCurrentText(settings.MATCHING_LAST_LOGO_TEMP) def select_excel_path(self, *args, **kwargs): folder = QFileDialog.getOpenFileName(self, "选取excel", "./")[0] if "xlsx" in folder: # if len(folder) > 20: # text = folder[-20:] # else: # text = folder self.ui.excel_path_show.setText(folder) self.excel_path = folder print("folder", folder) def change_img_dir(self, *args): folder = QFileDialog.getExistingDirectory( self, "选取文件夹", settings.GOODS_DETAIL_LAST_IMAGE_PATH ) if folder: # todo 增加检测是否有正常文件文件夹 folder = check_goods_folder(folder) if folder is None: QMessageBox.question( self, "确认", "您已选的文件夹下不存在任何有效数据", QMessageBox.Yes, ) return else: self.image_dir = folder self.target_error_folder = "{}/软件-生成详情错误".format(self.image_dir) self.show_img_dir() def change_img_dir_by_text(self, folder): self.image_dir = folder self.target_error_folder = "{}/软件-生成详情错误".format(self.image_dir) self.show_img_dir() def show_img_dir(self): self.ui.path_name.setText(self.image_dir) def set_state(self, state_value: int): # 0禁用 1进行中 2已结束 if state_value not in [0, 1, 2, 99]: return self.state = state_value # if self.state == 0: # self.ui.stackedWidget.setCurrentIndex(0) # if self.state == 1: # self.ui.stackedWidget.setCurrentIndex(1) # self.ui.progressBar.setValue(0) # self.ui.textBrowser_4.clear() # self.ui.back.hide() # self.ui.stop.show() # if self.state == 2: # self.ui.back.show() # self.ui.stop.hide() # pass def check_path(self, _path): if not os.path.exists(_path): os.mkdir(_path) return True def show_progress(self, *args, **kwargs): pass # todo 主程序处理 def run(self): config_data = self.get_config() if not config_data: return # self.queren = QueRen(self) # ret = self.queren.exec() # if ret == QDialog.Accepted: # print("对话框以接受状态关闭") for key, value in queren_get_config().items(): config_data[key] = value config_data["sign_text"] = "开始抠图" for key, value in config_data.items(): print("key:{}, value:{}".format(key, value)) self.set_state(state_value=1) self.run_end_sign.emit(config_data) # 解析指定模板页面数据 def check_assigned_page(self, text: str): try: # temp_dict 示例{“4”:“xiaosushuoxie-4”} temp_dict = {} for i in self.temp_list: temp_dict[i[-1]] = i text = text.replace(" ", "") text = text.replace(",", ",") text = text.replace(":", ":") text = text.replace(";", ",") assigned_page_list = text.split(",") assigned_page_dict = {} for i in assigned_page_list: if not i: continue _k, _v = i.split(":") _k = temp_dict[_k] if _k not in assigned_page_dict: assigned_page_dict[_k] = [] if _v not in assigned_page_dict[_k]: assigned_page_dict[_k].append(_v) # assigned_page_dict 数据结构,一个模板中存在多个页面数值。 return assigned_page_dict except: return {} # 弹出对话框 def show_dialog(self, data): windows = data["windows"] windows.dialog_result = "" my_dialog = DialogShow( self, text=data["text"], button_1=data["button_1"] if "button_1" in data else None, button_2=data["button_2"] if "button_2" in data else None, button_3=data["button_3"] if "button_3" in data else None, ) ret = my_dialog.exec() print("641 my_dialog.flag_name", my_dialog.flag_name) # 根据用户的选择发出相应的信号 windows.dialog_result_signal.emit(my_dialog.flag_name)