diff --git a/__init__.py b/__init__.py index 5bc951c..303711a 100644 --- a/__init__.py +++ b/__init__.py @@ -25,20 +25,32 @@ """ try: - import fontTools -except ImportError: - print("no fonttool.....") import sys import os sys.path.append(os.path.dirname(__file__)) + import fontTools +except ImportError: + print("no fonttool.....") + + # 确保在插件运行时能够访问依赖项 + def install_and_import(package): + try: + __import__(package) + except ImportError: + try: + # 尝试使用 QGIS 的 Python 环境中的 pip 安装包 + import pip # type: ignore + pip.main(['install', package]) + except AttributeError: + # 适用于较新版本的 pip + import subprocess + subprocess.check_call([sys.executable, '-m', 'pip', 'install', package]) + __import__(package) + install_and_import("fontTools") + + # noinspection PyPep8Naming def classFactory(iface): # pylint: disable=invalid-name - """Load FontOutline class from file FontOutline. - - :param iface: A QGIS interface instance. - :type iface: QgsInterface - """ - # from .font_outline import FontOutline return FontOutline(iface) diff --git a/font_outline.py b/font_outline.py index 361b570..5a1290a 100644 --- a/font_outline.py +++ b/font_outline.py @@ -169,31 +169,21 @@ def run(self): self.dlg.show() # show the dialog result = self.dlg.exec_() # Run the dialog event loop - # See if OK was pressed + if result: input_str = self.dlg.lineEdit.text() - location_text = self.dlg.location_text.text() - saveFileWidget = self.dlg.saveFileWidget - - # self.dlg.saveFileWidget.setStorageMode(3) - # self.dlg.saveFileWidget.setFilter('GeoJSON Files (*.geojson);;All Files (*)') - # saveFileWidget.setFilter("geojson;;json") - - print("saveFileWidget:", self.dlg.saveFileWidget) + if input_str is "" or input_str is None: + return self.iface.messageBar().pushMessage("warning", f"输入字符串为空。执行失败。", level=Qgis.Warning, duration=5) + location_text = self.dlg.location_text.text() save_path = self.dlg.saveFileWidget.filePath() - - print("input_str:", input_str) - print("location_text: ", location_text) - print("save_path:", save_path) + if not (save_path.endswith(".geojson") and save_path.endswith(".json")) : + save_path += ".geojson" self._save2file(input_str, location_text, save_path) self._result2project(save_path) - # self._result2project(r"C:\Users\Raino\Desktop\raino1.geojson") - # 显示结果 - # QMessageBox.information(self.iface.mainWindow(), "finish!!!", f"fontoutline plugin finished, the save file to {save_path}") def _save2file(self, input_str, location_text, save_path): fontparse = FontParser(os.path.join(self.plugin_dir,"assets", "msyahei.ttf")) @@ -208,7 +198,7 @@ def _result2project(self, geojson_path): # 检查图层是否有效 if not layer.isValid(): - print("GeoJSON 图层加载失败") + self.iface.messageBar().pushMessage("error", f" geojson 文件:{geojson_path} 加载失败。。", level=Qgis.Error, duration=5) return # 将图层添加到当前项目中 @@ -216,5 +206,5 @@ def _result2project(self, geojson_path): # 反馈信息 self.iface.messageBar().pushMessage("success", f"成功加载 geojson 文件:{geojson_path}", level=Qgis.Info, duration=5) - + diff --git a/font_outline_dialog.py b/font_outline_dialog.py index 531f71d..69cece3 100644 --- a/font_outline_dialog.py +++ b/font_outline_dialog.py @@ -45,3 +45,10 @@ def __init__(self, parent=None): # http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html # #widgets-and-dialogs-with-auto-connect self.setupUi(self) + + self.dlg.lineEdit.textChanged.connect(self.on_input_changed) + + def on_input_changed(self, text): + if text is "" or text is None: + # TODO: 输入字符空值判断 + print("输入字符串不能为空。")