mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use our file icon provider when using non-native file dialogs
This commit is contained in:
parent
b0d8409afb
commit
1a7a4544f5
@ -4,7 +4,7 @@
|
||||
|
||||
import os
|
||||
|
||||
from qt.core import QFileDialog, QObject
|
||||
from qt.core import QFileDialog, QObject, QDialog
|
||||
|
||||
from calibre.gui2.linux_file_dialogs import dialog_name, image_extensions
|
||||
from polyglot.builtins import string_or_bytes
|
||||
@ -95,6 +95,29 @@ class FileDialog(QObject):
|
||||
initial_dir = select_initial_dir(initial_dir)
|
||||
self.selected_files = []
|
||||
use_native_dialog = 'CALIBRE_NO_NATIVE_FILEDIALOGS' not in os.environ
|
||||
|
||||
def create_dialog(title, ftext='', for_saving=False):
|
||||
from calibre.gui2 import file_icon_provider
|
||||
ans = QFileDialog(parent, title, initial_dir)
|
||||
if ftext:
|
||||
ans.setNameFilter(ftext)
|
||||
ans.setOptions(opts)
|
||||
ans.setFileMode(mode)
|
||||
ans.setSupportedSchemes(('file',))
|
||||
ans.setIconProvider(file_icon_provider())
|
||||
if for_saving:
|
||||
ans.setAcceptMode(QFileDialog.AcceptMode.AcceptSave)
|
||||
ret = ans.exec()
|
||||
ans.setParent(None)
|
||||
if ret != QDialog.DialogCode.Accepted:
|
||||
return ()
|
||||
|
||||
def c(url):
|
||||
if url.isLocalFile() or url.isEmpty():
|
||||
return url.toLocalFile()
|
||||
return url.toString()
|
||||
return tuple(c(url) for url in ans.selectedUrls())
|
||||
|
||||
with sanitize_env_vars(), adapt_menubar:
|
||||
opts = QFileDialog.Option(0)
|
||||
if not use_native_dialog:
|
||||
@ -102,15 +125,24 @@ class FileDialog(QObject):
|
||||
if has_long_filter:
|
||||
opts |= QFileDialog.Option.HideNameFilterDetails
|
||||
if mode == QFileDialog.FileMode.AnyFile:
|
||||
f = QFileDialog.getSaveFileName(parent, title, initial_dir, ftext, "", opts)
|
||||
if use_native_dialog:
|
||||
f = QFileDialog.getSaveFileName(parent, title, initial_dir, ftext, "", opts)
|
||||
else:
|
||||
f = create_dialog(title, ftext, for_saving=True)
|
||||
if f and f[0]:
|
||||
self.selected_files.append(f[0])
|
||||
elif mode == QFileDialog.FileMode.ExistingFile:
|
||||
f = QFileDialog.getOpenFileName(parent, title, initial_dir, ftext, "", opts)
|
||||
if use_native_dialog:
|
||||
f = QFileDialog.getOpenFileName(parent, title, initial_dir, ftext, "", opts)
|
||||
else:
|
||||
f = create_dialog(title, ftext)
|
||||
if f and f[0] and os.path.exists(f[0]):
|
||||
self.selected_files.append(f[0])
|
||||
elif mode == QFileDialog.FileMode.ExistingFiles:
|
||||
fs = QFileDialog.getOpenFileNames(parent, title, initial_dir, ftext, "", opts)
|
||||
if use_native_dialog:
|
||||
fs = QFileDialog.getOpenFileNames(parent, title, initial_dir, ftext, "", opts)
|
||||
else:
|
||||
fs = create_dialog(title, ftext), True
|
||||
if fs and fs[0]:
|
||||
for f in fs[0]:
|
||||
f = str(f)
|
||||
@ -125,8 +157,12 @@ class FileDialog(QObject):
|
||||
else:
|
||||
if mode == QFileDialog.FileMode.Directory:
|
||||
opts |= QFileDialog.Option.ShowDirsOnly
|
||||
f = str(QFileDialog.getExistingDirectory(parent, title, initial_dir, opts))
|
||||
if os.path.exists(f):
|
||||
if use_native_dialog:
|
||||
f = str(QFileDialog.getExistingDirectory(parent, title, initial_dir, opts))
|
||||
else:
|
||||
f = create_dialog(title)
|
||||
f = f[0] if f else ''
|
||||
if f and os.path.exists(f):
|
||||
self.selected_files.append(f)
|
||||
if self.selected_files:
|
||||
self.selected_files = [str(q) for q in self.selected_files]
|
||||
|
Loading…
x
Reference in New Issue
Block a user