mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Avoid roundtripping image data when dragging and dropping image files
Fixes #1862440 [Cover disappear when dragging new cover into editor and dropping it on the cover space](https://bugs.launchpad.net/calibre/+bug/1862440)
This commit is contained in:
parent
a7a88de0f8
commit
9c6444e3b8
@ -198,14 +198,7 @@ def dnd_has_extension(md, extensions, allow_all_extensions=False):
|
|||||||
return bool(exts.intersection(frozenset(extensions)))
|
return bool(exts.intersection(frozenset(extensions)))
|
||||||
|
|
||||||
|
|
||||||
def dnd_get_image(md, image_exts=None):
|
def dnd_get_local_image_and_pixmap(md, image_exts=None):
|
||||||
'''
|
|
||||||
Get the image in the QMimeData object md.
|
|
||||||
|
|
||||||
:return: None, None if no image is found
|
|
||||||
QPixmap, None if an image is found, the pixmap is guaranteed not null
|
|
||||||
url, filename if a URL that points to an image is found
|
|
||||||
'''
|
|
||||||
if md.hasImage():
|
if md.hasImage():
|
||||||
for x in md.formats():
|
for x in md.formats():
|
||||||
x = unicode_type(x)
|
x = unicode_type(x)
|
||||||
@ -214,14 +207,13 @@ def dnd_get_image(md, image_exts=None):
|
|||||||
pmap = QPixmap()
|
pmap = QPixmap()
|
||||||
pmap.loadFromData(cdata)
|
pmap.loadFromData(cdata)
|
||||||
if not pmap.isNull():
|
if not pmap.isNull():
|
||||||
return pmap, None
|
return pmap, cdata
|
||||||
break
|
|
||||||
if md.hasFormat('application/octet-stream'):
|
if md.hasFormat('application/octet-stream'):
|
||||||
cdata = bytes(md.data('application/octet-stream'))
|
cdata = bytes(md.data('application/octet-stream'))
|
||||||
pmap = QPixmap()
|
pmap = QPixmap()
|
||||||
pmap.loadFromData(cdata)
|
pmap.loadFromData(cdata)
|
||||||
if not pmap.isNull():
|
if not pmap.isNull():
|
||||||
return pmap, None
|
return pmap, cdata
|
||||||
|
|
||||||
if image_exts is None:
|
if image_exts is None:
|
||||||
image_exts = image_extensions()
|
image_exts = image_extensions()
|
||||||
@ -229,23 +221,40 @@ def dnd_get_image(md, image_exts=None):
|
|||||||
# No image, look for an URL pointing to an image
|
# No image, look for an URL pointing to an image
|
||||||
urls = urls_from_md(md)
|
urls = urls_from_md(md)
|
||||||
paths = [path_from_qurl(u) for u in urls]
|
paths = [path_from_qurl(u) for u in urls]
|
||||||
# First look for a local file
|
# Look for a local file
|
||||||
images = [xi for xi in paths if
|
images = [xi for xi in paths if
|
||||||
posixpath.splitext(unquote(xi))[1][1:].lower() in
|
posixpath.splitext(unquote(xi))[1][1:].lower() in
|
||||||
image_exts]
|
image_exts]
|
||||||
images = [xi for xi in images if os.path.exists(xi)]
|
images = [xi for xi in images if os.path.exists(xi)]
|
||||||
p = QPixmap()
|
|
||||||
for path in images:
|
for path in images:
|
||||||
try:
|
try:
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
p.loadFromData(f.read())
|
cdata = f.read()
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
|
p = QPixmap()
|
||||||
|
p.loadFromData(cdata)
|
||||||
if not p.isNull():
|
if not p.isNull():
|
||||||
return p, None
|
return p, cdata
|
||||||
|
|
||||||
# No local images, look for remote ones
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
|
def dnd_get_image(md, image_exts=None):
|
||||||
|
'''
|
||||||
|
Get the image in the QMimeData object md.
|
||||||
|
|
||||||
|
:return: None, None if no image is found
|
||||||
|
QPixmap, None if an image is found, the pixmap is guaranteed not null
|
||||||
|
url, filename if a URL that points to an image is found
|
||||||
|
'''
|
||||||
|
if image_exts is None:
|
||||||
|
image_exts = image_extensions()
|
||||||
|
pmap, data = dnd_get_local_image_and_pixmap(md, image_exts)
|
||||||
|
if pmap is not None:
|
||||||
|
return pmap, None
|
||||||
|
# Look for a remote image
|
||||||
|
urls = urls_from_md(md)
|
||||||
# First, see if this is from Firefox
|
# First, see if this is from Firefox
|
||||||
rurl, fname = get_firefox_rurl(md, image_exts)
|
rurl, fname = get_firefox_rurl(md, image_exts)
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ from calibre.ebooks import BOOK_EXTENSIONS
|
|||||||
from calibre.utils.config import prefs, XMLConfig
|
from calibre.utils.config import prefs, XMLConfig
|
||||||
from calibre.gui2.progress_indicator import ProgressIndicator as _ProgressIndicator
|
from calibre.gui2.progress_indicator import ProgressIndicator as _ProgressIndicator
|
||||||
from calibre.gui2.dnd import (dnd_has_image, dnd_get_image, dnd_get_files,
|
from calibre.gui2.dnd import (dnd_has_image, dnd_get_image, dnd_get_files,
|
||||||
image_extensions, dnd_has_extension, DownloadDialog)
|
image_extensions, dnd_has_extension, dnd_get_local_image_and_pixmap, DownloadDialog)
|
||||||
from calibre.utils.localization import localize_user_manual_link
|
from calibre.utils.localization import localize_user_manual_link
|
||||||
from polyglot.builtins import native_string_type, unicode_type, range
|
from polyglot.builtins import native_string_type, unicode_type, range
|
||||||
|
|
||||||
@ -238,6 +238,10 @@ class ImageDropMixin(object): # {{{
|
|||||||
def dropEvent(self, event):
|
def dropEvent(self, event):
|
||||||
event.setDropAction(Qt.CopyAction)
|
event.setDropAction(Qt.CopyAction)
|
||||||
md = event.mimeData()
|
md = event.mimeData()
|
||||||
|
pmap, data = dnd_get_local_image_and_pixmap(md)
|
||||||
|
if pmap is not None:
|
||||||
|
self.handle_image_drop(pmap, data)
|
||||||
|
return
|
||||||
|
|
||||||
x, y = dnd_get_image(md)
|
x, y = dnd_get_image(md)
|
||||||
if x is not None:
|
if x is not None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user