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
db.set_cover(cid, pmap)
cover_changed = True
elif ext in BOOK_EXTENSIONS:
else:
formats.append((ext, path))
accept = True
if accept and event is not None:

View File

@ -619,7 +619,7 @@ class BookDetails(QWidget): # {{{
def dragEnterEvent(self, event):
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):
event.acceptProposedAction()
@ -642,7 +642,7 @@ class BookDetails(QWidget): # {{{
return
# 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:
# Nothing found
return

View File

@ -158,7 +158,7 @@ def remote_urls_from_qurl(qurls, allowed_exts):
qurl.path())[1][1:].lower() in allowed_exts:
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:
prints('\nDebugging DND event')
for f in md.formats():
@ -177,6 +177,8 @@ def dnd_has_extension(md, extensions):
prints('Paths:', paths)
prints('Extensions:', exts)
if allow_all_extensions:
return bool(exts)
return bool(exts.intersection(frozenset(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
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
the extensions in exts.
@ -249,9 +251,12 @@ def dnd_get_files(md, exts):
urls = urls_from_md(md)
# First look for a local file
local_files = [path_from_qurl(x) for x in urls]
local_files = [p for p in local_files if
posixpath.splitext(urllib.unquote(p))[1][1:].lower() in
exts]
def is_ok(path):
ext = posixpath.splitext(path)[1][1:].lower()
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)]
if local_files:
return local_files, None

View File

@ -177,14 +177,14 @@ class FormatList(QListWidget): # {{{
def dragEnterEvent(self, event):
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()
def dropEvent(self, event):
event.setDropAction(Qt.CopyAction)
md = event.mimeData()
# 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:
# Nothing found
return