AddFile.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_AddFile import Ui_AddFileWindow
  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 AddPathWindow(Ui_AddFileWindow, QDialog):
  15. add_file_sig = Signal(str,str)
  16. def __init__(self, parent, path) -> None:
  17. super().__init__()
  18. self.setupUi(self)
  19. self.setWindowTitle("Add Path")
  20. self.file_suffix = ["All Directories(*.*)"]
  21. self.init(parent, path)
  22. # self.show_file_list()
  23. def init(self, parent, path):
  24. self.path_obj = PathObject(path)
  25. self.Combobox_FileType.addItems(self.file_suffix)
  26. self.Combobox_FileType.currentIndexChanged.connect(lambda index:self.show_file_list())
  27. self.bnt_Apply.clicked.connect(self.apply)
  28. self.bnt_OK.clicked.connect(self.click_ok)
  29. self.init_file_browser(path)
  30. # 用于路径前进后退,目前用不上
  31. self.move(parent.x()+(parent.width()-self.width())/2 + self.width()*0.8,parent.y()+(parent.height()-self.height())/2)
  32. self.show()
  33. def init_file_browser(self, path):
  34. # 表格随窗口适应大小
  35. self.fileTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
  36. self.fileTable.cellDoubleClicked.connect(self.double_click)
  37. self.address_bar = BreadcrumbsAddressBar(self)
  38. self.address_bar.path_selected.connect(self.open_folder)
  39. self.address_bar.path_error.connect(lambda path: self.address_error_connect("No such file or directory", path.name))
  40. self.address_bar.listdir_error.connect(lambda path: self.address_error_connect("Permission denied", path.name))
  41. self.address_bar.set_path(path)
  42. self.verticalLayout_3.insertWidget(0, self.address_bar)
  43. def open_folder(self, dir):
  44. self.path_obj.set(dir)
  45. self.show_file_list()
  46. def show_file_list(self):
  47. _, self.dirs = file_list.get_files_dirs(self.path_obj)
  48. self.fileTable.setRowCount(0)
  49. height = self.fileTable.verticalHeader().defaultSectionSize()
  50. for dir in self.dirs:
  51. row = self.fileTable.rowCount()
  52. self.fileTable.insertRow(row)
  53. fwidget = FileWidget(dir, "Folder", height)
  54. self.fileTable.setCellWidget(row, 0, fwidget)
  55. def address_error_connect(self, error, path):
  56. self.path_error_popup(error, path)
  57. self.address_bar._show_address_field(True)
  58. self.address_bar.line_address.deselect()
  59. self.address_bar.line_address.insert('/')
  60. def path_error_popup(self, error='', path=''):
  61. msg = QMessageBox(self)
  62. msg.setWindowTitle("Error")
  63. msg.setText(error)
  64. msg.setIcon(QMessageBox.Critical)
  65. msg.move(self.x()+(self.width()-msg.width())/2,self.y()+(self.height()-msg.height())/2)
  66. msg.exec_()
  67. def double_click(self, row, column):
  68. if column != 0:
  69. return
  70. widg:FileWidget = self.fileTable.cellWidget(row, column)
  71. name = widg.get_text()
  72. abs_path = os.path.join(self.path_obj.pathtext, name)
  73. if os.path.isfile(abs_path):
  74. self.open_file(abs_path)
  75. # 这里不能用 elif os.path.isdir(abs_path) ,因为假如目录没有权限访问,即便它是个目录也会返回 False
  76. else:
  77. self.address_bar.set_path(abs_path)
  78. def current_select_path(self):
  79. row = self.fileTable.currentRow()
  80. if row < 0:
  81. return
  82. widg:FileWidget = self.fileTable.cellWidget(row, 0)
  83. filename = widg.get_text()
  84. abs_path = os.path.join(self.path_obj.pathtext, filename)
  85. return abs_path
  86. def current_radio_file_type(self):
  87. file_type = None
  88. for children in self.findChildren(QRadioButton):
  89. children:QRadioButton
  90. if children.isChecked():
  91. file_type = children.text()
  92. return file_type
  93. def apply(self):
  94. abs_path = self.current_select_path()
  95. if not abs_path:
  96. return
  97. if not os.path.isdir(abs_path):
  98. return
  99. file_type = self.current_radio_file_type()
  100. self.add_file_sig.emit(file_type, abs_path)
  101. self.fileTable.clearSelection()
  102. def click_ok(self):
  103. self.apply()
  104. self.close()
  105. class AddFileWindow(AddPathWindow):
  106. def __init__(self, parent, path) -> None:
  107. self.file_type = [".v", ".sv", ".f", ".lst", ".list"]
  108. self.file_suffix = ["Supported Files(%s)" % " ".join(['*'+str(s) for s in self.file_type]), "All Files(*.*)"]
  109. super().__init__(parent, path)
  110. self.setWindowTitle("Add File")
  111. def init(self,parent, path):
  112. return super().init(parent, path)
  113. def path_select(self, path):
  114. self.path_obj.set(path)
  115. self.open_folder(path)
  116. # def update_path(self):
  117. # path_text = self.pathBar.text()
  118. # if os.path.isdir(path_text):
  119. # self.path_obj.set(path_text)
  120. # self.show_file_list()
  121. def files_filter(self, files):
  122. if self.Combobox_FileType.currentIndex() == self.Combobox_FileType.count()-1:
  123. return files
  124. filt_res = []
  125. for i in range(len(files)):
  126. _,type =os.path.splitext(files[i])
  127. if type in self.file_type:
  128. filt_res.append(files[i])
  129. return filt_res
  130. def show_file_list(self):
  131. all_files, self.dirs = file_list.get_files_dirs(self.path_obj)
  132. self.files = self.files_filter(all_files)
  133. self.fileTable.setRowCount(0)
  134. height = self.fileTable.verticalHeader().defaultSectionSize()
  135. for dir in self.dirs:
  136. row = self.fileTable.rowCount()
  137. self.fileTable.insertRow(row)
  138. fwidget = FileWidget(dir, "Folder", height)
  139. self.fileTable.setCellWidget(row, 0, fwidget)
  140. for f in self.files:
  141. row = self.fileTable.rowCount()
  142. self.fileTable.insertRow(row)
  143. fwidget = FileWidget(f, "File", height)
  144. self.fileTable.setCellWidget(row, 0, fwidget)
  145. def apply(self):
  146. abs_path = self.current_select_path()
  147. if not abs_path:
  148. return
  149. if not os.path.isfile(abs_path):
  150. return
  151. file_type = self.current_radio_file_type()
  152. self.add_file_sig.emit(file_type, abs_path)
  153. self.fileTable.clearSelection()
  154. def click_ok(self):
  155. self.apply()
  156. self.close()
  157. def open_file(self, file):
  158. self.apply()
  159. self.close()