Skip to content

Commit

Permalink
Added Ability to Drag out of window
Browse files Browse the repository at this point in the history
  • Loading branch information
Creepler13 committed May 22, 2024
1 parent aeb119e commit dbbe1b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
36 changes: 33 additions & 3 deletions tagstudio/src/qt/modals/drop_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,55 @@
import shutil
import typing

from PySide6.QtCore import QThreadPool
from PySide6.QtGui import QDropEvent, QDragEnterEvent, QDragMoveEvent
from PySide6.QtCore import QThreadPool,Qt,QMimeData,QUrl
from PySide6.QtGui import QDropEvent, QDragEnterEvent,QImage, QDragMoveEvent,QMouseEvent,QDrag,QDragLeaveEvent
from PySide6.QtWidgets import QMessageBox
from src.qt.widgets import ProgressWidget
from src.qt.helpers import FunctionIterator, CustomRunnable

from ctypes import wintypes,windll

if typing.TYPE_CHECKING:
from src.qt.ts_qt import QtDriver

import logging

class DropImport:
def __init__(self, driver: "QtDriver"):
self.driver = driver


def mouseMoveEvent(self,event:QMouseEvent):
if event.buttons() is not Qt.MouseButton.LeftButton: return
if len(self.driver.selected) == 0: return

drag = QDrag(self.driver)
paths = []
mimedata = QMimeData()
for selected in self.driver.selected:
entry =self.driver.lib.get_entry(selected[1])
url = QUrl.fromLocalFile(self.driver.lib.library_dir+"/"+entry.path+"/"+entry.filename)
paths.append(url)

mimedata.setUrls(paths)
drag.setMimeData(mimedata)
drag.exec(Qt.DropAction.CopyAction)

def dropEvent(self, event: QDropEvent):
if event.source() is self.driver: # change that if you want to drop something originating from tagstudio, for moving or so
return

if not event.mimeData().hasUrls():
return

self.urls = event.mimeData().urls()
self.import_files()

def dragLeaveEvent(self,event:QDragLeaveEvent):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()

def dragEnterEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
event.accept()
Expand All @@ -34,6 +61,7 @@ def dragMoveEvent(self, event: QDragMoveEvent):
if event.mimeData().hasUrls():
event.accept()
else:
logging.info(self.driver.selected)
event.ignore()

def import_files(self):
Expand Down Expand Up @@ -216,3 +244,5 @@ def get_renamed_duplicate_filename_in_lib(self, filePath: Path) -> str:
)
index += 1
return filePath.name


17 changes: 11 additions & 6 deletions tagstudio/src/qt/ts_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ def start(self):
# f'QScrollBar::{{background:red;}}'
# )

self.drop_import = DropImport(self)
self.main_window.setAcceptDrops(True)
self.main_window.dragEnterEvent = self.drop_import.dragEnterEvent
self.main_window.dropEvent = self.drop_import.dropEvent
self.main_window.dragMoveEvent = self.drop_import.dragMoveEvent


# # self.main_window.windowFlags() &
# # self.main_window.setWindowFlag(Qt.WindowType.FramelessWindowHint, True)
# self.main_window.setWindowFlag(Qt.WindowType.NoDropShadowWindowHint, True)
Expand Down Expand Up @@ -511,12 +518,6 @@ def start(self):
# self.render_times: list = []
# self.main_window.setWindowFlag(Qt.FramelessWindowHint)

drop_import = DropImport(self)
self.main_window.setAcceptDrops(True)
self.main_window.dragEnterEvent = drop_import.dragEnterEvent
self.main_window.dropEvent = drop_import.dropEvent
self.main_window.dragMoveEvent = drop_import.dragMoveEvent

# NOTE: Putting this early will result in a white non-responsive
# window until everything is loaded. Consider adding a splash screen
# or implementing some clever loading tricks.
Expand Down Expand Up @@ -1015,6 +1016,10 @@ def _init_thumb_grid(self):
item_thumb = ItemThumb(
None, self.lib, self.preview_panel, (self.thumb_size, self.thumb_size)
)

item_thumb.setMouseTracking(True)
item_thumb.mouseMoveEvent = self.drop_import.mouseMoveEvent

layout.addWidget(item_thumb)
self.item_thumbs.append(item_thumb)

Expand Down

0 comments on commit dbbe1b0

Please sign in to comment.