Allow drag and drop of all file types onto the book details panel and the format list in the edit metadata dialog. See #1515462 (unnecessary file copy during import)

This commit is contained in:
Kovid Goyal 2015-11-12 14:21:35 +05:30
parent 191381f7da
commit 608bb13cae
4 changed files with 15 additions and 10 deletions

View File

@ -370,7 +370,7 @@ class AddAction(InterfaceAction):
accept = True accept = True
db.set_cover(cid, pmap) db.set_cover(cid, pmap)
cover_changed = True cover_changed = True
elif ext in BOOK_EXTENSIONS: else:
formats.append((ext, path)) formats.append((ext, path))
accept = True accept = True
if accept and event is not None: if accept and event is not None:

View File

@ -619,7 +619,7 @@ class BookDetails(QWidget): # {{{
def dragEnterEvent(self, event): def dragEnterEvent(self, event):
md = event.mimeData() md = event.mimeData()
if dnd_has_extension(md, self.DROPABBLE_EXTENSIONS) or \ if dnd_has_extension(md, self.DROPABBLE_EXTENSIONS, allow_all_extensions=True) or \
dnd_has_image(md): dnd_has_image(md):
event.acceptProposedAction() event.acceptProposedAction()
@ -642,7 +642,7 @@ class BookDetails(QWidget): # {{{
return return
# Now look for ebook files # Now look for ebook files
urls, filenames = dnd_get_files(md, BOOK_EXTENSIONS) urls, filenames = dnd_get_files(md, BOOK_EXTENSIONS, allow_all_extensions=True)
if not urls: if not urls:
# Nothing found # Nothing found
return return

View File

@ -158,7 +158,7 @@ def remote_urls_from_qurl(qurls, allowed_exts):
qurl.path())[1][1:].lower() in allowed_exts: qurl.path())[1][1:].lower() in allowed_exts:
yield bytes(qurl.toEncoded()), posixpath.basename(qurl.path()) yield bytes(qurl.toEncoded()), posixpath.basename(qurl.path())
def dnd_has_extension(md, extensions): def dnd_has_extension(md, extensions, allow_all_extensions=False):
if DEBUG: if DEBUG:
prints('\nDebugging DND event') prints('\nDebugging DND event')
for f in md.formats(): for f in md.formats():
@ -177,6 +177,8 @@ def dnd_has_extension(md, extensions):
prints('Paths:', paths) prints('Paths:', paths)
prints('Extensions:', exts) prints('Extensions:', exts)
if allow_all_extensions:
return bool(exts)
return bool(exts.intersection(frozenset(extensions))) return bool(exts.intersection(frozenset(extensions)))
def dnd_get_image(md, image_exts=IMAGE_EXTENSIONS): def dnd_get_image(md, image_exts=IMAGE_EXTENSIONS):
@ -236,7 +238,7 @@ def dnd_get_image(md, image_exts=IMAGE_EXTENSIONS):
return None, None return None, None
def dnd_get_files(md, exts): def dnd_get_files(md, exts, allow_all_extensions=False):
''' '''
Get the file in the QMimeData object md with an extension that is one of Get the file in the QMimeData object md with an extension that is one of
the extensions in exts. the extensions in exts.
@ -249,9 +251,12 @@ def dnd_get_files(md, exts):
urls = urls_from_md(md) urls = urls_from_md(md)
# First look for a local file # First look for a local file
local_files = [path_from_qurl(x) for x in urls] local_files = [path_from_qurl(x) for x in urls]
local_files = [p for p in local_files if def is_ok(path):
posixpath.splitext(urllib.unquote(p))[1][1:].lower() in ext = posixpath.splitext(path)[1][1:].lower()
exts] if allow_all_extensions and ext:
return True
return ext in exts
local_files = [p for p in local_files if is_ok(urllib.unquote(p))]
local_files = [x for x in local_files if os.path.exists(x)] local_files = [x for x in local_files if os.path.exists(x)]
if local_files: if local_files:
return local_files, None return local_files, None

View File

@ -177,14 +177,14 @@ class FormatList(QListWidget): # {{{
def dragEnterEvent(self, event): def dragEnterEvent(self, event):
md = event.mimeData() md = event.mimeData()
if dnd_has_extension(md, self.DROPABBLE_EXTENSIONS): if dnd_has_extension(md, self.DROPABBLE_EXTENSIONS, allow_all_extensions=True):
event.acceptProposedAction() event.acceptProposedAction()
def dropEvent(self, event): def dropEvent(self, event):
event.setDropAction(Qt.CopyAction) event.setDropAction(Qt.CopyAction)
md = event.mimeData() md = event.mimeData()
# Now look for ebook files # Now look for ebook files
urls, filenames = dnd_get_files(md, self.DROPABBLE_EXTENSIONS) urls, filenames = dnd_get_files(md, self.DROPABBLE_EXTENSIONS, allow_all_extensions=True)
if not urls: if not urls:
# Nothing found # Nothing found
return return