Skip to content

Commit

Permalink
Replace class MultiDirsSelectDialog with new FileDialog.getMultipleDi…
Browse files Browse the repository at this point in the history
…rectories()
  • Loading branch information
zas committed May 29, 2024
1 parent d060c3e commit 13dfe58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
6 changes: 1 addition & 5 deletions picard/ui/mainwindow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
from picard.ui.tagsfromfilenames import TagsFromFileNamesDialog
from picard.ui.util import (
FileDialog,
MultiDirsSelectDialog,
find_starting_directory,
menu_builder,
)
Expand Down Expand Up @@ -872,13 +871,10 @@ def add_directory(self):
if directory:
dir_list.append(directory)
else:
file_dialog = MultiDirsSelectDialog(
dir_list = FileDialog.getMultipleDirectories(
parent=self,
caption=_("Select one or more directories"),
directory=current_directory,
)
if file_dialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
dir_list = file_dialog.selectedFiles()

dir_count = len(dir_list)
if dir_count:
Expand Down
35 changes: 18 additions & 17 deletions picard/ui/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,23 @@ def getExistingDirectory(**kwargs):
convert_filedialog_params(kwargs, _("Select a directory"))
return QtWidgets.QFileDialog.getExistingDirectory(**kwargs)


class MultiDirsSelectDialog(FileDialog):

"""Custom file selection dialog which allows the selection
of multiple directories.
Depending on the platform, dialog may fallback on non-native.
"""

def __init__(self, parent=None, caption="", directory="", filter=""):
super().__init__(parent=parent, caption=caption, directory=directory, filter=filter)
self.setFileMode(QtWidgets.QFileDialog.FileMode.Directory)
self.setOption(QtWidgets.QFileDialog.Option.ShowDirsOnly)
@staticmethod
def getMultipleDirectories(parent=None, caption="", directory="", filter=""):
"""Custom file selection dialog which allows the selection
of multiple directories.
Depending on the platform, dialog may fallback on non-native.
"""
if not caption:
caption = _("Select one or more directories")
file_dialog = FileDialog(parent=parent, caption=caption, directory=directory, filter=filter)
file_dialog.setFileMode(QtWidgets.QFileDialog.FileMode.Directory)
file_dialog.setOption(QtWidgets.QFileDialog.Option.ShowDirsOnly)
# The native dialog doesn't allow selecting >1 directory
self.setOption(QtWidgets.QFileDialog.Option.DontUseNativeDialog)
for view in self.findChildren((QtWidgets.QListView, QtWidgets.QTreeView)):
file_dialog.setOption(QtWidgets.QFileDialog.Option.DontUseNativeDialog)
for view in file_dialog.findChildren((QtWidgets.QListView, QtWidgets.QTreeView)):
if isinstance(view.model(), QtGui.QFileSystemModel):
view.setSelectionMode(QtWidgets.QAbstractItemView.SelectionMode.ExtendedSelection)
self.set_sidebar_locations()

def set_sidebar_locations(self):
# Allow access to all mounted drives in the sidebar
root_volume = "/"
volume_paths = []
Expand All @@ -172,7 +169,11 @@ def set_sidebar_locations(self):
QtCore.QDir.homePath(),
QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.StandardLocation.MusicLocation),
]
self.setSidebarUrls(QtCore.QUrl.fromLocalFile(p) for p in paths + sorted(volume_paths) if p)
file_dialog.setSidebarUrls(QtCore.QUrl.fromLocalFile(p) for p in paths + sorted(volume_paths) if p)
dirs = ()
if file_dialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
dirs = file_dialog.selectedFiles()
return tuple(dirs)


def qlistwidget_items(qlistwidget):
Expand Down

0 comments on commit 13dfe58

Please sign in to comment.