| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import os
- import sys
- from decimal import *
- import pathlib
- from PySide2.QtWidgets import *
- from PySide2.QtCore import *
- from PySide2.QtGui import *
- from ui.ui_AddFile import Ui_AddFileWindow
- from ui.addressBar.addressbar import BreadcrumbsAddressBar
- from ui.file_browser import file_list
- from ui.file_browser.PathObject import PathObject
- from ui.file_browser.filewidget import FileWidget
- from ui.file_browser.FileObject import FileObject
- class AddPathWindow(Ui_AddFileWindow, QDialog):
- add_file_sig = Signal(str,str)
- def __init__(self, parent, path) -> None:
- super().__init__()
- self.setupUi(self)
- self.setWindowTitle("Add Path")
- self.file_suffix = ["All Directories(*.*)"]
- self.init(parent, path)
- # self.show_file_list()
-
- def init(self, parent, path):
- self.path_obj = PathObject(path)
- self.Combobox_FileType.addItems(self.file_suffix)
- self.Combobox_FileType.currentIndexChanged.connect(lambda index:self.show_file_list())
- self.bnt_Apply.clicked.connect(self.apply)
- self.bnt_OK.clicked.connect(self.click_ok)
- self.init_file_browser(path)
- # 用于路径前进后退,目前用不上
- self.move(parent.x()+(parent.width()-self.width())/2 + self.width()*0.8,parent.y()+(parent.height()-self.height())/2)
- self.show()
- def init_file_browser(self, path):
- # 表格随窗口适应大小
- self.fileTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
- self.fileTable.cellDoubleClicked.connect(self.double_click)
- self.address_bar = BreadcrumbsAddressBar(self)
- self.address_bar.path_selected.connect(self.open_folder)
- self.address_bar.path_error.connect(lambda path: self.address_error_connect("No such file or directory", path.name))
- self.address_bar.listdir_error.connect(lambda path: self.address_error_connect("Permission denied", path.name))
- self.address_bar.set_path(path)
- self.verticalLayout_3.insertWidget(0, self.address_bar)
- def open_folder(self, dir):
- self.path_obj.set(dir)
- self.show_file_list()
- def show_file_list(self):
- _, self.dirs = file_list.get_files_dirs(self.path_obj)
- self.fileTable.setRowCount(0)
- height = self.fileTable.verticalHeader().defaultSectionSize()
- for dir in self.dirs:
- row = self.fileTable.rowCount()
- self.fileTable.insertRow(row)
- fwidget = FileWidget(dir, "Folder", height)
- self.fileTable.setCellWidget(row, 0, fwidget)
- def address_error_connect(self, error, path):
- self.path_error_popup(error, path)
- self.address_bar._show_address_field(True)
- self.address_bar.line_address.deselect()
- self.address_bar.line_address.insert('/')
- def path_error_popup(self, error='', path=''):
- msg = QMessageBox(self)
- msg.setWindowTitle("Error")
- msg.setText(error)
- msg.setIcon(QMessageBox.Critical)
- msg.move(self.x()+(self.width()-msg.width())/2,self.y()+(self.height()-msg.height())/2)
- msg.exec_()
- def double_click(self, row, column):
- if column != 0:
- return
- widg:FileWidget = self.fileTable.cellWidget(row, column)
- name = widg.get_text()
- abs_path = os.path.join(self.path_obj.pathtext, name)
- if os.path.isfile(abs_path):
- self.open_file(abs_path)
- # 这里不能用 elif os.path.isdir(abs_path) ,因为假如目录没有权限访问,即便它是个目录也会返回 False
- else:
- self.address_bar.set_path(abs_path)
- def current_select_path(self):
- row = self.fileTable.currentRow()
- if row < 0:
- return
- widg:FileWidget = self.fileTable.cellWidget(row, 0)
- filename = widg.get_text()
- abs_path = os.path.join(self.path_obj.pathtext, filename)
- return abs_path
-
- def current_radio_file_type(self):
- file_type = None
- for children in self.findChildren(QRadioButton):
- children:QRadioButton
- if children.isChecked():
- file_type = children.text()
- return file_type
-
- def apply(self):
- abs_path = self.current_select_path()
- if not abs_path:
- return
- if not os.path.isdir(abs_path):
- return
- file_type = self.current_radio_file_type()
- self.add_file_sig.emit(file_type, abs_path)
- self.fileTable.clearSelection()
-
- def click_ok(self):
- self.apply()
- self.close()
- class AddFileWindow(AddPathWindow):
- def __init__(self, parent, path) -> None:
- self.file_type = [".v", ".sv", ".f", ".lst", ".list"]
- self.file_suffix = ["Supported Files(%s)" % " ".join(['*'+str(s) for s in self.file_type]), "All Files(*.*)"]
- super().__init__(parent, path)
- self.setWindowTitle("Add File")
-
- def init(self,parent, path):
- return super().init(parent, path)
-
- def path_select(self, path):
- self.path_obj.set(path)
- self.open_folder(path)
-
- # def update_path(self):
- # path_text = self.pathBar.text()
- # if os.path.isdir(path_text):
- # self.path_obj.set(path_text)
- # self.show_file_list()
-
- def files_filter(self, files):
- if self.Combobox_FileType.currentIndex() == self.Combobox_FileType.count()-1:
- return files
- filt_res = []
- for i in range(len(files)):
- _,type =os.path.splitext(files[i])
- if type in self.file_type:
- filt_res.append(files[i])
- return filt_res
-
- def show_file_list(self):
- all_files, self.dirs = file_list.get_files_dirs(self.path_obj)
- self.files = self.files_filter(all_files)
- self.fileTable.setRowCount(0)
- height = self.fileTable.verticalHeader().defaultSectionSize()
- for dir in self.dirs:
- row = self.fileTable.rowCount()
- self.fileTable.insertRow(row)
- fwidget = FileWidget(dir, "Folder", height)
- self.fileTable.setCellWidget(row, 0, fwidget)
- for f in self.files:
- row = self.fileTable.rowCount()
- self.fileTable.insertRow(row)
- fwidget = FileWidget(f, "File", height)
- self.fileTable.setCellWidget(row, 0, fwidget)
- def apply(self):
- abs_path = self.current_select_path()
- if not abs_path:
- return
- if not os.path.isfile(abs_path):
- return
- file_type = self.current_radio_file_type()
- self.add_file_sig.emit(file_type, abs_path)
- self.fileTable.clearSelection()
- def click_ok(self):
- self.apply()
- self.close()
-
- def open_file(self, file):
- self.apply()
- self.close()
|