mapmanager.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from pathlib import Path
  2. import yaml
  3. from PyQt5.QtCore import QSize, Qt
  4. from PyQt5.QtGui import QFont
  5. from PyQt5.QtWidgets import QPushButton, QTreeWidget, QTreeWidgetItem
  6. from ...utils import load_yaml, PluginConfig, got
  7. class MapManager(QTreeWidget):
  8. """
  9. 地图管理
  10. """
  11. def __init__(
  12. self,
  13. map_folder: Path,
  14. parent=None,
  15. ):
  16. super().__init__(parent)
  17. self.map_folder = map_folder
  18. self.font = QFont()
  19. self.font.setFamily("微软雅黑")
  20. self.font.setPointSize(8)
  21. self.setFont(self.font)
  22. self.update_host = "https://maps.liuxs.pro/dist/"
  23. self.update_url = f"{self.update_host}summary.yml"
  24. self.conf = PluginConfig()
  25. # self.check_update()
  26. self.setupUI()
  27. def setupUI(self):
  28. self.clear()
  29. self.setColumnCount(3) # 设置列
  30. self.setHeaderLabels(["名称", "Local", "LastUpdated", "操作"])
  31. self.header().setDefaultAlignment(Qt.AlignCenter)
  32. self.setUniformRowHeights(True)
  33. # 设置宽度
  34. self.setColumnWidth(0, 190)
  35. self.setColumnWidth(1, 125)
  36. self.setColumnWidth(2, 125)
  37. self.setColumnWidth(3, 90)
  38. self.load_map_summary()
  39. self.expandAll()
  40. def load_map_detail(self, map_id):
  41. mapfile_path = self.map_folder.joinpath(f"{map_id}.yml")
  42. data = load_yaml(mapfile_path)
  43. return data
  44. def get_summary(self):
  45. summary_path = self.map_folder.joinpath("summary.yml")
  46. return load_yaml(summary_path)
  47. def get_map_id_by_name(self, name):
  48. """通过地图名称获取 id"""
  49. summary = self.get_summary()
  50. for item in summary.values():
  51. if item["name"] == name:
  52. return item["id"]
  53. return None
  54. def load_map_summary(self):
  55. summary = self.get_summary()
  56. for value in summary.values():
  57. update_btn = QPushButton("更新")
  58. update_btn.setStyleSheet("QPushButton{margin:2px 20px;}")
  59. update_btn.clicked.connect(self.update_btn_clicked)
  60. update_btn.setEnabled(False)
  61. item = QTreeWidgetItem(self, [value["name"], value["lastUpdated"], "/"])
  62. item.setSizeHint(0, QSize(160, 28))
  63. item.setTextAlignment(1, Qt.AlignCenter)
  64. item.setTextAlignment(2, Qt.AlignCenter)
  65. self.setItemWidget(item, 3, update_btn)
  66. extra_maps_status = self.conf.get_extra_maps_status()
  67. map_detail = self.load_map_detail(value["id"])["maps"]
  68. section_maps_status = extra_maps_status[value["id"]]
  69. # 添加地图item
  70. for map_name in map_detail.keys():
  71. child_item = QTreeWidgetItem(item)
  72. child_item.setText(0, map_name)
  73. # 是否启用
  74. if map_name in section_maps_status:
  75. child_item.setCheckState(0, Qt.Checked)
  76. else:
  77. child_item.setCheckState(0, Qt.Unchecked)
  78. self.addTopLevelItem(item)
  79. def update_btn_clicked(self):
  80. """
  81. 更新地图配置文件
  82. """
  83. sender_btn = self.sender() # 获取发出信号的按钮
  84. if sender_btn:
  85. item = self.itemFromIndex(self.indexAt(sender_btn.pos())) # 获取包含按钮的项
  86. if item:
  87. map_id = self.get_map_id_by_name(item.text(0))
  88. self.download_map_conf(map_id)
  89. # 重新禁用按钮
  90. update_btn = self.itemWidget(item, 3)
  91. update_btn.setEnabled(False)
  92. # 重绘UI
  93. self.setupUI()
  94. def download_map_conf(self, map_id):
  95. download_url = f"{self.update_host}{map_id}.yml"
  96. mapfile_path = self.map_folder.joinpath(f"{map_id}.yml")
  97. # 更新summary
  98. summary_data = got(self.update_url)
  99. if summary_data.ok:
  100. with open(
  101. self.map_folder.joinpath("summary.yml"), "w", encoding="utf-8"
  102. ) as f:
  103. f.write(summary_data.text)
  104. conf_data = got(download_url)
  105. if conf_data.ok:
  106. with open(mapfile_path, "w", encoding="utf-8") as f:
  107. f.write(conf_data.text)
  108. def check_update(self):
  109. r = got(self.update_url)
  110. if r is None:
  111. print("检查更新失败,请稍后重试")
  112. return
  113. update_summary = yaml.safe_load(r.text)
  114. for _, map_sum in update_summary.items():
  115. name = map_sum["name"]
  116. item = self.findItems(name, Qt.MatchExactly)[0]
  117. item.setText(2, map_sum["lastUpdated"])
  118. if item.text(1) != item.text(2):
  119. # 将按钮设置为启用状态
  120. update_btn = self.itemWidget(item, 3)
  121. update_btn.setEnabled(True)
  122. def update_map_enable_state(self):
  123. top_level_item_count = self.topLevelItemCount()
  124. current_status = {}
  125. for i in range(top_level_item_count):
  126. top_level_item = self.topLevelItem(i)
  127. map_name = top_level_item.text(0)
  128. map_id = self.get_map_id_by_name(map_name)
  129. # 获取子项的数量
  130. child_count = top_level_item.childCount()
  131. # 遍历子项
  132. checked_item = []
  133. for j in range(child_count):
  134. child_item = top_level_item.child(j)
  135. if child_item.checkState(0) == 2:
  136. checked_item.append(child_item.text(0))
  137. current_status[map_id] = checked_item
  138. # 保存状态
  139. self.conf.set_extra_maps_status(current_status)