def browseInsertInto(self): multiSelect = QFileDialog() multiSelect.setOption(multiSelect.DontUseNativeDialog, True) multiSelect.setOption(multiSelect.HideNameFilterDetails, True) multiSelect.setOption(multiSelect.ShowDirsOnly, False) multiSelect.findChildren(QListView)[0].setSelectionMode( QAbstractItemView.ExtendedSelection) multiSelect.findChildren(QTreeView)[0].setSelectionMode( QAbstractItemView.ExtendedSelection) if multiSelect.exec_() == QDialog.Accepted: self.insertIntoFiles = multiSelect.selectedFiles() # clear out any old selections self.ui.FileNamesInsertInto.clear() # loop through all files selected and add to screen for file in range(len(self.insertIntoFiles)): self.ui.FileNamesInsertInto.append(self.insertIntoFiles[file])
def directories_dialog(path=None,name='Select Directories'): ok = True def _pressed_cancel(): nonlocal ok Fi.close() app.quit() ok &= False path = path or CURDIR app = QCoreApplication.instance() or QApplication([]) Fi = QFileDialog() Fi.setWindowTitle(name) Fi.setOption(Fi.DontUseNativeDialog, True) qr = Fi.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) Fi.move(qr.topLeft()) Fi.setFileMode(Fi.DirectoryOnly) Fi.setDirectory(path) for view in Fi.findChildren((QListView,QTreeView)): if isinstance(view.model(), QFileSystemModel): view.setSelectionMode(QAbstractItemView.MultiSelection) for view in Fi.findChildren(QPushButton): if view.text().lower().startswith('&cancel'): view.clicked.connect(_pressed_cancel) Fi.show() app.exec_() # The folder selection is also selecting its parent: sel_files = Fi.selectedFiles() sel_files2 = set(sel_files) for fi1 in sel_files: for fi2 in sel_files: if fi2 != fi1 and fi1 in fi2: sel_files2 -= {fi1} break return ok, list(sel_files2)
def getExistingDirectories(parent, *args, **kwargs): """ Workaround for selecting multiple directories adopted from http://www.qtcentre.org/threads/34226-QFileDialog-select-multiple-directories?p=158482#post158482 This also give control about hidden folders """ dlg = QFileDialog(parent, *args, **kwargs) dlg.setOption(dlg.DontUseNativeDialog, True) dlg.setOption(dlg.HideNameFilterDetails, True) dlg.setFileMode(dlg.Directory) dlg.setOption(dlg.ShowDirsOnly, True) if hiddenFiles(parent): dlg.setFilter(dlg.filter() | QDir.Hidden) dlg.findChildren(QListView)[0].setSelectionMode( QAbstractItemView.ExtendedSelection) dlg.findChildren(QTreeView)[0].setSelectionMode( QAbstractItemView.ExtendedSelection) if dlg.exec_() == QDialog.Accepted: return dlg.selectedFiles() return [ str(), ]
class MenuUI(QMenuBar): addDirectory = pyqtSignal(list) def __init__(self, parent): super().__init__() self.parent = parent self.file_menu = QMenu("&File") self.edit_menu = QMenu() self.help_menu = QMenu() self._init_menus() def _init_menus(self): self._file_menu() def _file_menu(self): self.add_library = QAction("&Add Library...", self) self.exit_player = QAction("E&xit", self) self.file_menu.addAction(self.add_library) self.file_menu.addAction(self.exit_player) self._connect_widgets() self.addMenu(self.file_menu) def _connect_widgets(self): self.add_library.triggered.connect(self._add_library) self.exit_player.triggered.connect(self.parent.close) def _add_library(self): self.dialog = QFileDialog() self.dialog.setFileMode(QFileDialog.DirectoryOnly) self.dialog.setOption(QFileDialog.DontUseNativeDialog, True) for view in self.dialog.findChildren((QListView, QTreeView)): if isinstance(view.model(), QFileSystemModel): view.setSelectionMode(QAbstractItemView.MultiSelection) if self.dialog.exec(): dirs = self.dialog.selectedFiles() self.addDirectory.emit(dirs)
class MainUI(QWidget): """ Components of Main UI displayed when app opens up """ def __init__(self, parent=None): try: super(MainUI, self).__init__(parent) # self.setMinimumSize(800, 600) self.selected_folders = [] self.folder_table_contents = [] self.empty_folders = [] self.empty = Empty() self.widgets = Widgets() self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored) self._initialize_components() self.setWindowTitle('Delete Empty') except Exception as e: print(e) def _initialize_components(self): layout = QVBoxLayout() splitter = QSplitter(Qt.Horizontal) left_frame = self._initialize_left_frame() right_frame = self._initialize_right_frame() splitter.addWidget(left_frame) splitter.addWidget(right_frame) layout.addWidget(splitter) self.setLayout(layout) def new_qframe(self): frame = QFrame() frame.setStyleSheet(styles.FRAME_PAGE_STYLE) return frame def _initialize_left_frame(self): frame = self.new_qframe() frame.setMaximumWidth(200) splitter = QSplitter(Qt.Vertical) left_top_frame = self._initialize_left_top_frame() left_bottom_frame = self._initialize_left_bottom_frame() splitter.addWidget(left_top_frame) splitter.addWidget(left_bottom_frame) layout = QVBoxLayout() layout.addWidget(splitter) frame.setLayout(layout) return frame def _initialize_right_frame(self): frame = self.new_qframe() layout = QVBoxLayout() splitter = QSplitter(Qt.Vertical) right_top_frame = self._initialize_right_top_frame() right_middle_frame = self._initialize_right_middle_frame() right_bottom_frame = self._initialize_right_bottom_frame() splitter.addWidget(right_top_frame) splitter.addWidget(right_middle_frame) splitter.addWidget(right_bottom_frame) layout.addWidget(splitter) frame.setLayout(layout) return frame def _initialize_left_top_frame(self): frame = self.new_qframe() frame.setMaximumHeight(50) layout = QHBoxLayout() layout.setAlignment(Qt.AlignCenter) select_folder_button = QPushButton("Select Folder") select_folder_button.setStyleSheet(styles.DIALOG_BUTTON) select_folder_button.setMaximumWidth(160) select_folder_button.clicked.connect(self.select_button_clicked) layout.addWidget(select_folder_button) frame.setLayout(layout) return frame def _initialize_left_bottom_frame(self): frame = self.new_qframe() layout = QVBoxLayout() frame.setLayout(layout) return frame def _initialize_right_top_frame(self): frame = self.new_qframe() frame.setMaximumHeight(50) layout = QHBoxLayout() layout.setAlignment(Qt.AlignCenter) header = QLabel('Empty Folders') header.setStyleSheet(styles.LABEL_HEADER) layout.addWidget(header) frame.setLayout(layout) return frame def _initialize_right_middle_frame(self): frame = self.new_qframe() layout = QHBoxLayout() self._initialize_folder_table_view() layout.addWidget(self.folder_list_view) frame.setLayout(layout) return frame def _initialize_right_bottom_frame(self): frame = self.new_qframe() layout = QHBoxLayout() layout.setAlignment(Qt.AlignTop) layout.setSpacing(50) self.delete_all_button = QPushButton('Delete All') self.delete_selected_button = QPushButton('Delete Selected') self.delete_all_button.setMaximumWidth(160) self.delete_selected_button.setMaximumWidth(160) self.delete_selected_button.setEnabled(False) self.delete_all_button.setEnabled(False) self.delete_all_button.setStyleSheet(styles.DIALOG_BUTTON) self.delete_selected_button.setStyleSheet(styles.DIALOG_BUTTON) self.delete_all_button.clicked.connect(self.delete_all_button_clicked) layout.addWidget(self.delete_all_button) layout.addWidget(self.delete_selected_button) frame.setLayout(layout) return frame def _initialize_folder_table_view(self): self.folder_list_view = self._get_table_widget() self.folder_list_view.setColumnCount(4) self.folder_list_view.setHorizontalHeaderLabels( ['Folder Name', 'Path', 'Created time', 'Deleted time']) self.folder_list_view.setSelectionBehavior( QAbstractItemView.SelectRows) # self.folder_list_view.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeToContents) self.folder_list_view.horizontalHeader().setSectionResizeMode( QHeaderView.Stretch) self.folder_list_view.itemSelectionChanged.connect( self.delete_select_button_toggle) def delete_select_button_toggle(self): self.selected_empty_folders = self.folder_list_view.selectedIndexes() if self.selected_empty_folders: self.delete_selected_button.setEnabled(True) print(dir(self.selected_empty_folders[0])) print(self.selected_empty_folders[0].row) print(self.selected_empty_folders[0].column) print(self.selected_empty_folders[0].child) print(self.selected_empty_folders[0].flags) print(self.selected_empty_folders[0].data) print(self.selected_empty_folders[0].internalId) print(self.selected_empty_folders[0].internalPointer) print(self.selected_empty_folders[0].model) print(self.selected_empty_folders[0].parent) print(self.selected_empty_folders[0].sibling) else: self.delete_selected_button.setEnabled(False) print(self.selected_empty_folders) def _get_table_widget(self): table_widget = QTableWidget() table_widget.setStyleSheet(styles.TABLE_WIDGET_STYLE) table_widget.verticalHeader().setVisible(False) table_widget.horizontalHeader().setDefaultAlignment(Qt.AlignCenter) table_widget.verticalScrollBar().setStyleSheet(styles.SCROLL_AREA) table_widget.horizontalHeader().setHighlightSections(False) table_widget.setEditTriggers(QAbstractItemView.NoEditTriggers) table_widget.horizontalHeader().setStretchLastSection(True) return table_widget def delete_all_button_clicked(self): print(self.folder_table_contents) if self.empty.delete_folders(self.empty_folders): self.widgets.get_message_box("Deleted Successfully").exec() def delete_selected_button_clicked(self): pass def select_button_clicked(self): try: print('Folder') self._get_file_dialog() if self.selected_folders: print(self.selected_folders) self.folder_table_contents, self.empty_folders = self.empty.get_empty_folders_list( self.selected_folders) print(self.folder_table_contents) if self.folder_table_contents: self.delete_all_button.setEnabled(True) self.generate_folder_list_view() else: message_box = self.widgets.get_message_box("No Empty folders") message_box.exec() except Exception as e: print(e) def generate_folder_list_view(self): self.folder_list_view.setRowCount(len(self.folder_table_contents)) index = 0 for folder in self.folder_table_contents: self.folder_list_view.setItem(index, 0, QTableWidgetItem(folder[0])) self.folder_list_view.setItem(index, 1, QTableWidgetItem(folder[1])) self.folder_list_view.setItem(index, 2, QTableWidgetItem(folder[2])) self.folder_list_view.setItem(index, 3, QTableWidgetItem(folder[3])) index += 1 self.folder_list_view.horizontalHeader().setSectionResizeMode( 0, QHeaderView.Interactive) self.folder_list_view.horizontalHeader().setSectionResizeMode( 1, QHeaderView.Interactive) self.folder_list_view.horizontalHeader().setSectionResizeMode( 2, QHeaderView.Interactive) self.folder_list_view.horizontalHeader().setSectionResizeMode( 3, QHeaderView.Interactive) def _get_file_dialog(self): self.file_dialog = QFileDialog() options = QFileDialog.Options() options |= QFileDialog.ShowDirsOnly options |= QFileDialog.DontUseNativeDialog self.file_dialog.setFileMode(QFileDialog.DirectoryOnly) self.file_dialog.setOptions(options) self.file_dialog.setWindowTitle("Select Folder") self.file_dialog.setDirectory(QDir.home()) self.file_dialog.setWindowFlag(Qt.WindowContextHelpButtonHint, False) self.file_dialog.findChildren(QListView)[0].setSelectionMode( QAbstractItemView.ExtendedSelection) self.file_dialog.findChildren(QTreeView)[0].setSelectionMode( QAbstractItemView.ExtendedSelection) # self.file_dialog.setFixedSize(800, 600) if self.file_dialog.exec(): self.selected_folders = self.file_dialog.selectedFiles()