AddFile.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.fileTable.mousePressEvent = self.click_table_widget
  38. self.address_bar = BreadcrumbsAddressBar(self)
  39. self.address_bar.path_selected.connect(self.open_folder)
  40. self.address_bar.path_error.connect(lambda path: self.address_error_connect("No such file or directory", path.name))
  41. self.address_bar.listdir_error.connect(lambda path: self.address_error_connect("Permission denied", path.name))
  42. self.address_bar.set_path(path)
  43. self.verticalLayout_3.insertWidget(0, self.address_bar)
  44. def open_folder(self, dir):
  45. self.path_obj.set(dir)
  46. self.show_file_list()
  47. def show_file_list(self):
  48. _, self.dirs = file_list.get_files_dirs(self.path_obj)
  49. self.fileTable.setRowCount(0)
  50. height = self.fileTable.verticalHeader().defaultSectionSize()
  51. for dir in self.dirs:
  52. row = self.fileTable.rowCount()
  53. self.fileTable.insertRow(row)
  54. fwidget = FileWidget(dir, "Folder", height)
  55. self.fileTable.setCellWidget(row, 0, fwidget)
  56. def address_error_connect(self, error, path):
  57. self.path_error_popup(error, path)
  58. self.address_bar._show_address_field(True)
  59. self.address_bar.line_address.deselect()
  60. self.address_bar.line_address.insert('/')
  61. def path_error_popup(self, error='', path=''):
  62. msg = QMessageBox(self)
  63. msg.setWindowTitle("Error")
  64. msg.setText(error)
  65. msg.setIcon(QMessageBox.Critical)
  66. msg.move(self.x()+(self.width()-msg.width())/2,self.y()+(self.height()-msg.height())/2)
  67. msg.exec_()
  68. # 点击空白处取消选择
  69. def click_table_widget(self, event:QMouseEvent):
  70. p = event.pos()
  71. index = self.fileTable.indexAt(p)
  72. if index.row() == -1:
  73. self.fileTable.clearSelection()
  74. self.fileTable.setCurrentIndex(QModelIndex())
  75. else:
  76. QTableWidget.mousePressEvent(self.fileTable, event)
  77. def double_click(self, row, column):
  78. if column != 0:
  79. return
  80. widg:FileWidget = self.fileTable.cellWidget(row, column)
  81. name = widg.get_text()
  82. abs_path = os.path.join(self.path_obj.pathtext, name)
  83. if os.path.isfile(abs_path):
  84. self.open_file(abs_path)
  85. # 这里不能用 elif os.path.isdir(abs_path) ,因为假如目录没有权限访问,即便它是个目录也会返回 False
  86. else:
  87. self.address_bar.set_path(abs_path)
  88. def current_select_path(self):
  89. row = self.fileTable.currentRow()
  90. if row < 0:
  91. return str(self.address_bar.path_)
  92. widg:FileWidget = self.fileTable.cellWidget(row, 0)
  93. filename = widg.get_text()
  94. abs_path = os.path.join(self.path_obj.pathtext, filename)
  95. return abs_path
  96. def current_radio_file_type(self):
  97. file_type = None
  98. for children in self.findChildren(QRadioButton):
  99. children:QRadioButton
  100. if children.isChecked():
  101. file_type = children.text()
  102. return file_type
  103. def apply(self):
  104. abs_path = self.current_select_path()
  105. if not abs_path:
  106. return
  107. if not os.path.isdir(abs_path):
  108. return
  109. file_type = self.current_radio_file_type()
  110. self.add_file_sig.emit(file_type, abs_path)
  111. self.fileTable.clearSelection()
  112. def click_ok(self):
  113. self.apply()
  114. self.close()
  115. class AddFileWindow(AddPathWindow):
  116. def __init__(self, parent, path) -> None:
  117. self.file_type = [".v", ".sv", ".f", ".lst", ".list"]
  118. self.file_suffix = ["Supported Files(%s)" % " ".join(['*'+str(s) for s in self.file_type]), "All Files(*.*)"]
  119. super().__init__(parent, path)
  120. self.setWindowTitle("Add File")
  121. def init(self,parent, path):
  122. return super().init(parent, path)
  123. def path_select(self, path):
  124. self.path_obj.set(path)
  125. self.open_folder(path)
  126. # def update_path(self):
  127. # path_text = self.pathBar.text()
  128. # if os.path.isdir(path_text):
  129. # self.path_obj.set(path_text)
  130. # self.show_file_list()
  131. def files_filter(self, files):
  132. if self.Combobox_FileType.currentIndex() == self.Combobox_FileType.count()-1:
  133. return files
  134. filt_res = []
  135. for i in range(len(files)):
  136. _,type =os.path.splitext(files[i])
  137. if type in self.file_type:
  138. filt_res.append(files[i])
  139. return filt_res
  140. def show_file_list(self):
  141. all_files, self.dirs = file_list.get_files_dirs(self.path_obj)
  142. self.files = self.files_filter(all_files)
  143. self.fileTable.setRowCount(0)
  144. height = self.fileTable.verticalHeader().defaultSectionSize()
  145. for dir in self.dirs:
  146. row = self.fileTable.rowCount()
  147. self.fileTable.insertRow(row)
  148. fwidget = FileWidget(dir, "Folder", height)
  149. self.fileTable.setCellWidget(row, 0, fwidget)
  150. for f in self.files:
  151. row = self.fileTable.rowCount()
  152. self.fileTable.insertRow(row)
  153. fwidget = FileWidget(f, "File", height)
  154. self.fileTable.setCellWidget(row, 0, fwidget)
  155. def apply(self):
  156. abs_path = self.current_select_path()
  157. if not abs_path:
  158. return
  159. if not os.path.isfile(abs_path):
  160. return
  161. file_type = self.current_radio_file_type()
  162. self.add_file_sig.emit(file_type, abs_path)
  163. self.fileTable.clearSelection()
  164. def click_ok(self):
  165. self.apply()
  166. self.close()
  167. def open_file(self, file):
  168. self.apply()
  169. self.close()