AddFile.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. import sys
  3. from decimal import *
  4. from PySide2.QtWidgets import *
  5. from PySide2.QtCore import *
  6. from PySide2.QtGui import *
  7. from ui.ui_AddFile import Ui_AddFileWindow
  8. from ui.addressBar.addressbar import BreadcrumbsAddressBar
  9. from ui.file_browser.file_list import get_files_dirs
  10. from ui.file_browser.PathObject import PathObject
  11. from ui.file_browser.filewidget import FileWidget
  12. class AddFileWindow(Ui_AddFileWindow, QDialog):
  13. def __init__(self, parent, path) -> None:
  14. super().__init__()
  15. self.file_suffix = ["Supported Files(*.v *.sv *.f *.lst *.list)", "All files(*.*)"]
  16. self.setupUi(self)
  17. self.init_file_browser(path)
  18. self.move(parent.x()+(parent.width()-self.width())/2,parent.y()+(parent.height()-self.height())/2)
  19. self.show()
  20. self.fileTable.cellDoubleClicked.connect(self.open_item)
  21. self.path_obj = PathObject(path)
  22. self.dir_stack = []
  23. self.show_file_list()
  24. self.comboBox.addItems(self.file_suffix)
  25. def init_file_browser(self, path):
  26. # 表格随窗口适应大小
  27. self.fileTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
  28. self.address_bar = BreadcrumbsAddressBar(self)
  29. self.address_bar.path_selected.connect(self.path_select)
  30. self.verticalLayout_3.insertWidget(0, self.address_bar)
  31. def path_select(self, path):
  32. self.path_obj.set(path)
  33. self.open_folder(path)
  34. def update_path(self):
  35. path_text = self.pathBar.text()
  36. if os.path.isdir(path_text):
  37. self.dir_stack.clear()
  38. self.path_obj.set(path_text)
  39. self.show_file_list()
  40. else:
  41. self.path_error_popup()
  42. self.pathBar.setText(str(self.path_obj))
  43. def path_error_popup(self):
  44. msg = QMessageBox()
  45. msg.setWindowTitle("Error")
  46. msg.setText("Invalid path!")
  47. msg.setIcon(QMessageBox.Critical)
  48. x = msg.exec_()
  49. def show_file_list(self):
  50. self.files, self.dirs = get_files_dirs(self.path_obj)
  51. self.update_counts()
  52. self.fileTable.setRowCount(0)
  53. height = self.fileTable.verticalHeader().defaultSectionSize()
  54. for dir in self.dirs:
  55. row = self.fileTable.rowCount()
  56. self.fileTable.insertRow(row)
  57. fwidget = FileWidget(dir.name, dir.type, height)
  58. self.fileTable.setCellWidget(row, 0, fwidget)
  59. for f in self.files:
  60. row = self.fileTable.rowCount()
  61. self.fileTable.insertRow(row)
  62. fwidget = FileWidget(f.name, f.type, height)
  63. self.fileTable.setCellWidget(row, 0, fwidget)
  64. # self.fileTable.resizeRowsToContents()
  65. # self.fileTable
  66. # self.show_count()
  67. def format_size(self, size):
  68. if size < 999:
  69. return f"{size} B"
  70. if size < 999999:
  71. num = Decimal(size / 1000)
  72. return f"{round(num, 2)} KB"
  73. if size < 999999999:
  74. num = Decimal(size / 1000000)
  75. return f"{round(num, 2)} MB"
  76. else:
  77. num = Decimal(size / 1000000000)
  78. return f"{round(num, 2)} GB"
  79. def update_counts(self):
  80. self.file_count = len(self.files)
  81. self.dir_count = len(self.dirs)
  82. def open_item(self, row, column):
  83. if column != 0:
  84. return
  85. widg = self.fileTable.cellWidget(row, column)
  86. name = widg.get_text()
  87. self.dir_stack.clear()
  88. abs_path = os.path.join(self.path_obj.pathtext, name)
  89. if os.path.isfile(os.path.join(self.path_obj.pathtext, name)):
  90. self.open_file(abs_path)
  91. elif os.path.isdir(abs_path):
  92. self.address_bar.set_path(abs_path)
  93. def open_folder(self, dir):
  94. if self.path_obj.is_root():
  95. self.dir_stack.clear()
  96. self.path_obj.set(dir)
  97. self.show_file_list()
  98. def open_file(self, file):
  99. path = PathObject(str(self.path_obj))
  100. path.set(file)
  101. print(file)
  102. def back(self):
  103. if self.path_obj.is_root():
  104. return
  105. dir = self.path_obj.back()
  106. self.dir_stack.append(dir)
  107. self.show_file_list()
  108. def forward(self):
  109. if len(self.dir_stack) == 0:
  110. return
  111. dir = self.dir_stack[-1]
  112. self.dir_stack.pop()
  113. self.path_obj.add(dir)
  114. self.show_file_list()
  115. def path_error_popup(self):
  116. msg = QMessageBox()
  117. msg.setWindowTitle("Error")
  118. msg.setText("Invalid path!")
  119. msg.setIcon(QMessageBox.Critical)
  120. msg.exec_()
  121. def show_count(self):
  122. count_text = ""
  123. count = self.file_count + self.dir_count
  124. if count == 1:
  125. count_text += "1 item ("
  126. else:
  127. count_text += f"{count} items ("
  128. if self.dir_count == 1:
  129. count_text += "1 folder, "
  130. else:
  131. count_text += f"{self.dir_count} dirs, "
  132. if self.file_count == 1:
  133. count_text += "1 file)"
  134. else:
  135. count_text += f"{self.file_count} files)"
  136. self.status_bar.showMessage(count_text)