load.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from PyQt5.QtCore import QObject, QEvent, Qt
  2. from PyQt5.QtWidgets import QToolBar, QMessageBox
  3. from qgis.utils import iface
  4. from cryptography.fernet import Fernet
  5. import json
  6. import os
  7. import platform
  8. import winreg
  9. import datetime
  10. import socket
  11. def get_qgis_install_dir():
  12. # 尝试从环境变量获取
  13. qgis_env = os.getenv('QGIS_PREFIX_PATH')
  14. if qgis_env:
  15. return f'{qgis_env.replace("apps/qgis-ltr", "")}bin'
  16. # 尝试从Windows注册表获取
  17. if platform.system() == "Windows":
  18. sub_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\QGIS')
  19. try:
  20. qgis_dir, _ = winreg.QueryValueEx(sub_key, 'QGIS_PREFIX_PATH')
  21. return qgis_dir
  22. except WindowsError:
  23. pass
  24. # 常见的安装路径
  25. default_paths = [
  26. os.path.join(os.path.expanduser('~'), 'AppData\Local\QGIS\QGIS3'),
  27. r'C:\Program Files\QGIS 3.X',
  28. r'C:\Program Files (x86)\QGIS 3.X',
  29. ]
  30. for path in default_paths:
  31. if os.path.exists(path):
  32. return path
  33. return None
  34. def str_to_time(time_str):
  35. return datetime.datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
  36. def str_to_dict(string):
  37. # 去除首尾的花括号
  38. string = string.strip("{}")
  39. # 分割每个键值对
  40. pairs = string.split(", ")
  41. # 创建空字典
  42. result = {}
  43. # 遍历键值对并添加到字典中
  44. for pair in pairs:
  45. key, value = pair.split(": ")
  46. # 去除键和值两边的引号
  47. key = key.strip("'")
  48. value = value.strip("'")
  49. # 添加到字典中
  50. result[key] = value
  51. return result
  52. def on_initialization_completed():
  53. print("QGIS 初始化完成!")
  54. iface.mainWindow().setWindowTitle("无标题工程 - 四维数码")
  55. main_window = iface.mainWindow()
  56. main_window.findChild(QToolBar, 'ProcessingAlgorithms').setVisible(0)
  57. # 获取所有动作
  58. # all_actions = iface.allActions()
  59. #
  60. # # 遍历所有动作并连接触发信号
  61. # for action in all_actions:
  62. # action.triggered.connect(on_action_triggered)
  63. print("注册事件")
  64. # 创建事件过滤器实例
  65. event_filter = ClickEventFilter(main_window)
  66. # 安装事件过滤器到地图画布
  67. main_window.installEventFilter(event_filter)
  68. # 程序许可验证
  69. machine_name = socket.gethostname()
  70. current_time = datetime.datetime.now()
  71. qgis_dir = get_qgis_install_dir()
  72. licensepath = f"{qgis_dir}\\license.txt"
  73. # 打开文件并读取内容
  74. text = []
  75. with open(licensepath, 'r') as file:
  76. for line in file:
  77. text.append(line.replace('\n', ' '))
  78. # 打印文件内容
  79. print(text)
  80. try:
  81. # 尝试执行的代码
  82. fernet = Fernet(text[0].encode("utf-8"))
  83. decrypted_message = fernet.decrypt(text[1].encode("utf-8"))
  84. license = decrypted_message.decode('utf-8')
  85. licenseDict = str_to_dict(license)
  86. host = licenseDict["host"]
  87. date = licenseDict["license"]
  88. time_str = f"{date} 23:59:59"
  89. licensetime = str_to_time(time_str)
  90. if host != machine_name:
  91. licenseError("机器名错误!")
  92. exit(1)
  93. elif current_time > licensetime:
  94. licenseError("许可已过期!")
  95. exit(1)
  96. else:
  97. print("许可正常")
  98. # print(f"解密的消息: {decrypted_message.decode('utf-8')}")
  99. except Exception as e:
  100. # 处理异常的代码
  101. licenseError("许可文件解析问题!")
  102. exit(1)
  103. def licenseError(message):
  104. QMessageBox.critical(None, 'Error', message)
  105. class ClickEventFilter(QObject):
  106. def __init__(self, parent=None):
  107. super().__init__(parent)
  108. def eventFilter(self, obj, event):
  109. print("event")
  110. if event.type() == QEvent.MouseButtonPress:
  111. if event.button() == Qt.LeftButton:
  112. print("左键点击事件")
  113. elif event.button() == Qt.RightButton:
  114. print("右键点击事件")
  115. elif event.button() == Qt.MiddleButton:
  116. print("中键点击事件")
  117. return True # 如果你返回True,则事件被过滤掉,不再传递给其他组件
  118. return super().eventFilter(obj, event)
  119. # 连接信号到槽函数
  120. iface.initializationCompleted.connect(on_initialization_completed)