Skip to content

Commit

Permalink
Add wrappers to QFileDialog -> picard.util.ui.FileDialog
Browse files Browse the repository at this point in the history
- it will help with PySide move
- it ensures we have a useful caption, and unify them
- it shortens lines
  • Loading branch information
zas committed May 28, 2024
1 parent fbd09e7 commit a5cb56e
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 38 deletions.
10 changes: 6 additions & 4 deletions picard/script/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
)
from picard.util import make_filename_from_title

from picard.ui.util import FileDialog


@unique
class ScriptSerializerType(IntEnum):
Expand Down Expand Up @@ -222,10 +224,10 @@ def export_script(self, parent=None):

dialog_title = _("Export Script File")
dialog_file_types = self._get_dialog_filetypes()
filename, file_type = QtWidgets.QFileDialog.getSaveFileName(
filename, file_type = FileDialog.getSaveFileName(
parent=parent,
caption=dialog_title,
directory=default_path,
dir=default_path,
filter=dialog_file_types,
)
if not filename:
Expand Down Expand Up @@ -264,10 +266,10 @@ def import_script(cls, parent=None):
dialog_title = _("Import Script File")
dialog_file_types = cls._get_dialog_filetypes()
default_script_directory = os.path.normpath(QtCore.QStandardPaths.writableLocation(QtCore.QStandardPaths.StandardLocation.DocumentsLocation))
filename, file_type = QtWidgets.QFileDialog.getOpenFileName(
filename, file_type = FileDialog.getOpenFileName(
parent=parent,
caption=dialog_title,
directory=default_script_directory,
dir=default_script_directory,
filter=dialog_file_types,
)
if not filename:
Expand Down
3 changes: 2 additions & 1 deletion picard/tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
from picard.ui.searchdialog.album import AlbumSearchDialog
from picard.ui.searchdialog.artist import ArtistSearchDialog
from picard.ui.searchdialog.track import TrackSearchDialog
from picard.ui.util import FileDialog


# A "fix" for https://bugs.python.org/issue1438480
Expand Down Expand Up @@ -1201,7 +1202,7 @@ def lookup_cd(self, action):
traceback=self._debug)

def lookup_discid_from_logfile(self):
file_chooser = QtWidgets.QFileDialog(parent=self.window)
file_chooser = FileDialog(parent=self.window)
file_chooser.setNameFilters([
_("All supported log files") + " (*.log *.txt)",
_("EAC / XLD / Whipper / fre:ac log files") + " (*.log)",
Expand Down
3 changes: 2 additions & 1 deletion picard/ui/coverartbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from picard.util.lrucache import LRUCache

from picard.ui.colors import interface_colors
from picard.ui.util import FileDialog
from picard.ui.widgets import ActiveLabel


Expand Down Expand Up @@ -607,7 +608,7 @@ def _try_load_remote_image(self, url, data):
return coverartimage

def choose_local_file(self):
file_chooser = QtWidgets.QFileDialog(parent=self)
file_chooser = FileDialog(parent=self)
extensions = ['*' + ext for ext in imageinfo.get_supported_extensions()]
extensions.sort()
file_chooser.setNameFilters([
Expand Down
3 changes: 2 additions & 1 deletion picard/ui/logview.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
PicardDialog,
)
from picard.ui.colors import interface_colors
from picard.ui.util import FileDialog


class LogViewDialog(PicardDialog):
Expand Down Expand Up @@ -275,7 +276,7 @@ def _format(self, level):
return self.formats[level]

def _save_log_as_do(self):
path, ok = QtWidgets.QFileDialog.getSaveFileName(
path, ok = FileDialog.getSaveFileName(
parent=self,
caption=_("Save Log View to File"),
options=QtWidgets.QFileDialog.Option.DontConfirmOverwrite,
Expand Down
10 changes: 6 additions & 4 deletions picard/ui/mainwindow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
)
from picard.ui.tagsfromfilenames import TagsFromFileNamesDialog
from picard.ui.util import (
FileDialog,
MultiDirsSelectDialog,
find_starting_directory,
menu_builder,
Expand Down Expand Up @@ -847,9 +848,9 @@ def add_files(self):
extensions.sort()
formats.insert(0, _("All supported formats") + " (%s)" % " ".join(extensions))
formats.insert(1, _("All files") + " (*)")
files, _filter = QtWidgets.QFileDialog.getOpenFileNames(
files, _filter = FileDialog.getOpenFileNames(
parent=self,
directory=current_directory,
dir=current_directory,
filter=";;".join(formats),
)
if files:
Expand All @@ -864,15 +865,16 @@ def add_directory(self):
dir_list = []
config = get_config()
if not config.setting['allow_multi_dirs_selection']:
directory = QtWidgets.QFileDialog.getExistingDirectory(
directory = FileDialog.getExistingDirectory(
parent=self,
directory=current_directory,
dir=current_directory,
)
if directory:
dir_list.append(directory)
else:
file_dialog = MultiDirsSelectDialog(
parent=self,
caption=_("Select one or more directories"),
directory=current_directory,
)
if file_dialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
Expand Down
6 changes: 3 additions & 3 deletions picard/ui/options/fingerprinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from PyQt6 import (
QtCore,
QtGui,
QtWidgets,
)

from picard.acoustid import find_fpcalc
Expand All @@ -48,6 +47,7 @@
OptionsCheckError,
OptionsPage,
)
from picard.ui.util import FileDialog


class ApiKeyValidator(QtGui.QValidator):
Expand Down Expand Up @@ -120,9 +120,9 @@ def update_groupboxes(self):
self._acoustid_fpcalc_check()

def acoustid_fpcalc_browse(self):
path, _filter = QtWidgets.QFileDialog.getOpenFileName(
path, _filter = FileDialog.getOpenFileName(
parent=self,
directory=self.ui.acoustid_fpcalc.text(),
dir=self.ui.acoustid_fpcalc.text(),
)
if path:
path = os.path.normpath(path)
Expand Down
9 changes: 6 additions & 3 deletions picard/ui/options/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
OS_SUPPORTS_THEMES,
UiTheme,
)
from picard.ui.util import changes_require_restart_warning
from picard.ui.util import (
FileDialog,
changes_require_restart_warning,
)


class InterfaceOptionsPage(OptionsPage):
Expand Down Expand Up @@ -197,9 +200,9 @@ def save(self):

def starting_directory_browse(self):
item = self.ui.starting_directory_path
path = QtWidgets.QFileDialog.getExistingDirectory(
path = FileDialog.getExistingDirectory(
parent=self,
directory=item.text(),
dir=item.text(),
)
if path:
path = os.path.normpath(path)
Expand Down
9 changes: 6 additions & 3 deletions picard/ui/options/interface_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@
)
from picard.ui.moveable_list_view import MoveableListView
from picard.ui.options import OptionsPage
from picard.ui.util import qlistwidget_items
from picard.ui.util import (
FileDialog,
qlistwidget_items,
)


ToolbarButtonDesc = namedtuple('ToolbarButtonDesc', ('label', 'icon'))
Expand Down Expand Up @@ -162,9 +165,9 @@ def restore_defaults(self):

def starting_directory_browse(self):
item = self.ui.starting_directory_path
path = QtWidgets.QFileDialog.getExistingDirectory(
path = FileDialog.getExistingDirectory(
parent=self,
directory=item.text(),
dir=item.text(),
)
if path:
path = os.path.normpath(path)
Expand Down
25 changes: 13 additions & 12 deletions picard/ui/options/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

from picard.ui.forms.ui_options_maintenance import Ui_MaintenanceOptionsPage
from picard.ui.options import OptionsPage
from picard.ui.util import FileDialog


OPTIONS_NOT_IN_PAGES = {
Expand Down Expand Up @@ -141,9 +142,9 @@ def _dialog_invalid_backup_dir(self, path):
dialog.exec()

def _dialog_autobackup_dir_browse(self):
path = QtWidgets.QFileDialog.getExistingDirectory(
path = FileDialog.getExistingDirectory(
parent=self,
directory=self.get_current_autobackup_dir(),
dir=self.get_current_autobackup_dir(),
)
if path:
self.set_current_autobackup_dir(path)
Expand Down Expand Up @@ -237,11 +238,11 @@ def _dialog_save_backup_error(self, filename):
dialog.exec()

def _dialog_ask_backup_filename(self, default_path, ext):
filename, file_type = QtWidgets.QFileDialog.getSaveFileName(
self,
_("Backup Configuration File"),
default_path,
self._get_dialog_filetypes(ext),
filename, file_type = FileDialog.getSaveFileName(
parent=self,
caption=_("Backup Configuration File"),
dir=default_path,
filter=self._get_dialog_filetypes(ext),
)
return filename

Expand Down Expand Up @@ -316,11 +317,11 @@ def _dialog_load_backup_error(self, filename):
dialog.exec()

def _dialog_load_backup_select_filename(self, directory, ext):
filename, file_type = QtWidgets.QFileDialog.getOpenFileName(
self,
_("Select Configuration File to Load"),
directory,
self._get_dialog_filetypes(ext),
filename, file_type = FileDialog.getOpenFileName(
parent=self,
caption=_("Select Configuration File to Load"),
dir=directory,
filter=self._get_dialog_filetypes(ext),
)
return filename

Expand Down
5 changes: 3 additions & 2 deletions picard/ui/options/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
from picard.ui.forms.ui_options_plugins import Ui_PluginsOptionsPage
from picard.ui.options import OptionsPage
from picard.ui.theme import theme
from picard.ui.util import FileDialog


COLUMN_NAME, COLUMN_VERSION, COLUMN_ACTIONS = range(3)
Expand Down Expand Up @@ -659,9 +660,9 @@ def change_details(self):
self.refresh_details(item)

def open_plugins(self):
files, _filter = QtWidgets.QFileDialog.getOpenFileNames(
files, _filter = FileDialog.getOpenFileNames(
parent=self,
directory=QtCore.QDir.homePath(),
dir=QtCore.QDir.homePath(),
filter="Picard plugin (*.py *.pyc *.zip)",
)
if files:
Expand Down
6 changes: 3 additions & 3 deletions picard/ui/options/renaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import os.path

from PyQt6 import QtWidgets
from PyQt6.QtGui import QPalette

from picard.config import get_config
Expand All @@ -62,6 +61,7 @@
populate_script_selection_combo_box,
synchronize_vertical_scrollbars,
)
from picard.ui.util import FileDialog


class RenamingOptionsPage(OptionsPage):
Expand Down Expand Up @@ -280,9 +280,9 @@ def display_error(self, error):
super().display_error(error)

def move_files_to_browse(self):
path = QtWidgets.QFileDialog.getExistingDirectory(
path = FileDialog.getExistingDirectory(
parent=self,
directory=self.ui.move_files_to.text(),
dir=self.ui.move_files_to.text(),
)
if path:
path = os.path.normpath(path)
Expand Down
58 changes: 57 additions & 1 deletion picard/ui/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,63 @@ def find_starting_directory():
return find_existing_path(path)


class MultiDirsSelectDialog(QtWidgets.QFileDialog):
def picardize_caption(caption):
return _("Picard - %s") % caption


def convert_filedialog_params(kwargs, default_caption):
# We use PySide/Qt syntax in code, so convert to PyQt6
if 'dir' in kwargs:
# TODO: remove with PySide
# pyside & qt use `dir`, pyqt uses `directory`
kwargs['directory'] = kwargs['dir']
del kwargs['dir']

if 'selectedFilter' in kwargs:
# TODO: remove with PySide
# pyside & qt use `selectedFilter`, pyqt uses `initialFilter`
kwargs['initialFilter'] = kwargs['selectedFilter']
del kwargs['selectedFilter']

# Ensure we have a caption in those dialogs
if 'caption' not in kwargs or not kwargs['caption']:
kwargs['caption'] = default_caption

# Add app name to the caption, fancier in window managers
kwargs['caption'] = picardize_caption(kwargs['caption'])


class FileDialog(QtWidgets.QFileDialog):
"""Wrap QFileDialog & its static methods"""

def __init__(self, parent=None, caption="", directory="", filter=""):
if not caption:
caption = _("Select a file or a directory")
caption = picardize_caption(caption)
super().__init__(parent=parent, caption=caption, directory=directory, filter=filter)

@staticmethod
def getSaveFileName(**kwargs):
convert_filedialog_params(kwargs, _("Select a target file"))
return QtWidgets.QFileDialog.getSaveFileName(**kwargs)

@staticmethod
def getOpenFileName(**kwargs):
convert_filedialog_params(kwargs, _("Select a file"))
return QtWidgets.QFileDialog.getOpenFileName(**kwargs)

@staticmethod
def getOpenFileNames(**kwargs):
convert_filedialog_params(kwargs, _("Select one or more files"))
return QtWidgets.QFileDialog.getOpenFileNames(**kwargs)

@staticmethod
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.
Expand Down

0 comments on commit a5cb56e

Please sign in to comment.