Use QMimeData for clipboard — same as drag and drop
Sets both file URL and image data on clipboard so it works with file managers (paste as file) and image apps (paste as image). No more wl-copy dependency for copying.
This commit is contained in:
parent
a8da23ab1d
commit
7b6a9ab911
@ -1955,34 +1955,15 @@ class BooruApp(QMainWindow):
|
|||||||
return
|
return
|
||||||
log.debug(f"Copying: {path}")
|
log.debug(f"Copying: {path}")
|
||||||
|
|
||||||
import shutil
|
from PySide6.QtCore import QMimeData, QUrl
|
||||||
_IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".gif", ".webp"}
|
mime = QMimeData()
|
||||||
ext = Path(path).suffix.lower()
|
mime.setUrls([QUrl.fromLocalFile(str(Path(path).resolve()))])
|
||||||
|
# Also set image data for apps that prefer it
|
||||||
if shutil.which("wl-copy"):
|
|
||||||
import subprocess
|
|
||||||
try:
|
|
||||||
if ext in _IMAGE_EXTS:
|
|
||||||
_MIMES = {".jpg": "image/jpeg", ".jpeg": "image/jpeg",
|
|
||||||
".png": "image/png", ".gif": "image/gif", ".webp": "image/webp"}
|
|
||||||
with open(path, "rb") as f:
|
|
||||||
subprocess.run(["wl-copy", "--type", _MIMES[ext]], stdin=f, timeout=10)
|
|
||||||
else:
|
|
||||||
# Videos/other: copy as file URI for file manager paste
|
|
||||||
uri = f"file://{Path(path).resolve()}\r\n"
|
|
||||||
subprocess.run(["wl-copy", "--type", "text/uri-list"], input=uri.encode(), timeout=5)
|
|
||||||
self._status.showMessage(f"Copied to clipboard: {Path(path).name}")
|
|
||||||
return
|
|
||||||
except Exception as e:
|
|
||||||
log.warning(f"wl-copy failed: {e}")
|
|
||||||
|
|
||||||
# Qt fallback (images only)
|
|
||||||
pix = QPixmap(path)
|
pix = QPixmap(path)
|
||||||
if not pix.isNull():
|
if not pix.isNull():
|
||||||
QApplication.clipboard().setPixmap(pix)
|
mime.setImageData(pix.toImage())
|
||||||
|
QApplication.clipboard().setMimeData(mime)
|
||||||
self._status.showMessage(f"Copied to clipboard: {Path(path).name}")
|
self._status.showMessage(f"Copied to clipboard: {Path(path).name}")
|
||||||
else:
|
|
||||||
self._status.showMessage("Nothing to copy")
|
|
||||||
|
|
||||||
# -- Bookmarks --
|
# -- Bookmarks --
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user