AddFile.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import os
  2. import sys
  3. from decimal import *
  4. import pathlib
  5. from PySide2.QtWidgets import *
  6. from PySide2.QtCore import *
  7. from PySide2.QtGui import *
  8. from ui.ui_AddPath import Ui_AddPathWindow
  9. from ui.addressBar.addressbar import BreadcrumbsAddressBar
  10. from ui.file_browser import file_list
  11. from ui.file_browser.PathObject import PathObject
  12. from ui.file_browser.filewidget import FileWidget
  13. from ui.file_browser.FileObject import FileObject
  14. class Category:
  15. def __init__(self, text, simple='') -> None:
  16. self.text = text
  17. self.simple = simple
  18. if not self.simple:
  19. self.simple = text
  20. class FolderCategory:
  21. Search_Path = Category("Search Path", "SearchPath")
  22. class FileCategory:
  23. Filelist = Category('Filelist', 'Filelist')
  24. Netlist = Category('Netlist', 'Netlist')
  25. Verilog_2001 = Category('Verilog 2001', 'V2001')
  26. Verilog_2005 = Category("Verilog 2005", 'V2005')
  27. SystemVerilog_2005 = Category("SystemVerilog 2005", "SV2005")
  28. SystemVerilog_2009 = Category("SystemVerilog 2009", "SV2009")
  29. SystemVerilog_2012 = Category("SystemVerilog 2012", "SV2012")
  30. all_category = [Filelist, Netlist, Verilog_2001, Verilog_2005, SystemVerilog_2005, SystemVerilog_2009, SystemVerilog_2012]
  31. def get_category(text):
  32. for category in FileCategory.all_category:
  33. if category.text == text:
  34. return category
  35. def get_all_text()->list:
  36. return [category.text for category in FileCategory.all_category]
  37. class AddPathWindow(Ui_AddPathWindow, QDialog):
  38. add_file_sig = Signal(str,str)
  39. def __init__(self, parent, path) -> None:
  40. super().__init__(parent)
  41. self.setupUi(self)
  42. self.file_suffix = ["All Directories(*.*)"]
  43. self.init(parent, path)
  44. def init(self, parent, path):
  45. self.setWindowTitle("Add Path")
  46. self.Combobox_FileType.addItems(self.file_suffix)
  47. self.Combobox_FileType.currentIndexChanged.connect(lambda index:self.show_file_list())
  48. self.path_obj = PathObject(path)
  49. self.bnt_Apply.clicked.connect(self.apply)
  50. self.bnt_OK.clicked.connect(self.click_ok)
  51. self.init_file_browser(path)
  52. self.fileTable.clearSelection()
  53. self.fileTable.setCurrentIndex(QModelIndex())
  54. self.move(parent.x()+(parent.width()-self.width())/2 + self.width()*0.8,parent.y()+(parent.height()-self.height())/2)
  55. self.show()
  56. def init_file_browser(self, path):
  57. # 表格随窗口适应大小
  58. self.fileTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
  59. self.fileTable.cellDoubleClicked.connect(self.double_click)
  60. self.fileTable.mousePressEvent = self.click_table_widget
  61. self.address_bar = BreadcrumbsAddressBar(self)
  62. self.address_bar.path_selected.connect(self.open_folder)
  63. self.address_bar.path_error.connect(lambda path: self.address_error_connect("No such file or directory", path.name))
  64. self.address_bar.listdir_error.connect(lambda path: self.address_error_connect("Permission denied", path.name))
  65. self.address_bar.set_path(path)
  66. self.verticalLayout_3.insertWidget(0, self.address_bar)
  67. def open_folder(self, dir):
  68. self.path_obj.set(dir)
  69. self.show_file_list()
  70. def show_file_list(self):
  71. _, self.dirs = file_list.get_files_dirs(self.path_obj)
  72. self.fileTable.setRowCount(0)
  73. height = self.fileTable.verticalHeader().defaultSectionSize()
  74. for dir in self.dirs:
  75. row = self.fileTable.rowCount()
  76. self.fileTable.insertRow(row)
  77. fwidget = FileWidget(dir, "Folder", height)
  78. self.fileTable.setCellWidget(row, 0, fwidget)
  79. def address_error_connect(self, error, path):
  80. self.path_error_popup(error, path)
  81. self.address_bar._show_address_field(True)
  82. self.address_bar.line_address.deselect()
  83. self.address_bar.line_address.insert('/')
  84. def path_error_popup(self, error='', path=''):
  85. msg = QMessageBox(self)
  86. msg.setWindowTitle("Error")
  87. msg.setText(error)
  88. msg.setIcon(QMessageBox.Critical)
  89. msg.move(self.x()+(self.width()-msg.width())/2,self.y()+(self.height()-msg.height())/2)
  90. msg.exec_()
  91. # 点击空白处取消选择
  92. def click_table_widget(self, event:QMouseEvent):
  93. p = event.pos()
  94. index = self.fileTable.indexAt(p)
  95. if index.row() == -1:
  96. self.fileTable.clearSelection()
  97. self.fileTable.setCurrentIndex(QModelIndex())
  98. else:
  99. QTableWidget.mousePressEvent(self.fileTable, event)
  100. def double_click(self, row, column):
  101. if column != 0:
  102. return
  103. widg:FileWidget = self.fileTable.cellWidget(row, column)
  104. name = widg.get_text()
  105. abs_path = os.path.join(self.path_obj.pathtext, name)
  106. if os.path.isfile(abs_path):
  107. self.open_file(abs_path)
  108. # 这里不能用 elif os.path.isdir(abs_path) ,因为假如目录没有权限访问,即便它是个目录也会返回 False
  109. else:
  110. self.address_bar.set_path(abs_path)
  111. def current_select_path(self):
  112. row = self.fileTable.currentRow()
  113. if row < 0:
  114. return str(self.address_bar.path_)
  115. widg:FileWidget = self.fileTable.cellWidget(row, 0)
  116. filename = widg.get_text()
  117. abs_path = os.path.join(self.path_obj.pathtext, filename)
  118. return abs_path
  119. def apply(self):
  120. abs_path = self.current_select_path()
  121. if not abs_path:
  122. return
  123. if not os.path.isdir(abs_path):
  124. return
  125. self.add_file_sig.emit(FolderCategory.Search_Path.text, abs_path)
  126. self.fileTable.clearSelection()
  127. self.fileTable.setCurrentIndex(QModelIndex())
  128. def click_ok(self):
  129. self.apply()
  130. self.close()
  131. class AddFileWindow(AddPathWindow):
  132. def __init__(self, parent, path) -> None:
  133. super().__init__(parent, path)
  134. def init(self,parent, path):
  135. self.setWindowTitle("Add File")
  136. self.file_type = [".v", ".sv", ".f", ".lst", ".list"]
  137. self.file_suffix = ["Supported Files(%s)" % " ".join(['*'+str(s) for s in self.file_type]), "All Files(*.*)"]
  138. super().init(parent, path)
  139. self.verticalLayout_2 = QVBoxLayout()
  140. self.verticalLayout_2.setObjectName(u"verticalLayout_2")
  141. self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
  142. self.verticalSpacer_2 = QSpacerItem(20, 31, QSizePolicy.Minimum, QSizePolicy.Fixed)
  143. self.verticalLayout_2.addItem(self.verticalSpacer_2)
  144. file_type = FileCategory.get_all_text()
  145. for t in file_type:
  146. radio = QRadioButton(self)
  147. radio.setObjectName(t)
  148. radio.setText(t)
  149. self.verticalLayout_2.addWidget(radio)
  150. if t == FileCategory.Verilog_2005.text:
  151. radio.setChecked(True)
  152. self.verticalSpacer = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
  153. self.verticalLayout_2.addItem(self.verticalSpacer)
  154. self.horizontalLayout.insertLayout(0, self.verticalLayout_2)
  155. def path_select(self, path):
  156. self.path_obj.set(path)
  157. self.open_folder(path)
  158. def files_filter(self, files):
  159. if self.Combobox_FileType.currentText() == "All Files(*.*)":
  160. return files
  161. filt_res = []
  162. for i in range(len(files)):
  163. _,type =os.path.splitext(files[i])
  164. if type in self.file_type:
  165. filt_res.append(files[i])
  166. return filt_res
  167. def show_file_list(self):
  168. all_files, self.dirs = file_list.get_files_dirs(self.path_obj)
  169. self.files = self.files_filter(all_files)
  170. self.fileTable.setRowCount(0)
  171. height = self.fileTable.verticalHeader().defaultSectionSize()
  172. for dir in self.dirs:
  173. row = self.fileTable.rowCount()
  174. self.fileTable.insertRow(row)
  175. fwidget = FileWidget(dir, "Folder", height)
  176. self.fileTable.setCellWidget(row, 0, fwidget)
  177. for f in self.files:
  178. row = self.fileTable.rowCount()
  179. self.fileTable.insertRow(row)
  180. fwidget = FileWidget(f, "File", height)
  181. self.fileTable.setCellWidget(row, 0, fwidget)
  182. def current_radio_text(self):
  183. file_type = None
  184. for children in self.findChildren(QRadioButton):
  185. children:QRadioButton
  186. if children.isChecked():
  187. file_type = children.text()
  188. return file_type
  189. def apply(self):
  190. abs_path = self.current_select_path()
  191. if not abs_path:
  192. return
  193. if not os.path.isfile(abs_path):
  194. return
  195. text = self.current_radio_text()
  196. self.add_file_sig.emit(text, abs_path)
  197. self.fileTable.clearSelection()
  198. self.fileTable.setCurrentIndex(QModelIndex())
  199. def click_ok(self):
  200. self.apply()
  201. self.close()
  202. def open_file(self, file):
  203. self.apply()
  204. self.close()