Skip to content

Commit

Permalink
init调整
Browse files Browse the repository at this point in the history
  • Loading branch information
raino_tx committed Sep 5, 2024
1 parent 8865f8f commit cf6ecfa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
30 changes: 21 additions & 9 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 8 additions & 18 deletions font_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -208,13 +198,13 @@ 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

# 将图层添加到当前项目中
QgsProject.instance().addMapLayer(layer)

# 反馈信息
self.iface.messageBar().pushMessage("success", f"成功加载 geojson 文件:{geojson_path}", level=Qgis.Info, duration=5)


7 changes: 7 additions & 0 deletions font_outline_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("输入字符串不能为空。")

0 comments on commit cf6ecfa

Please sign in to comment.