Support drag & drop in main window to import files (#1562)

* Support drag & drop in main window to import files

* Pass drag & drop events to super when not in deck browser

Seems this is required so that JS in the reviewer receives dragover

* Remove unused allowDrops
This commit is contained in:
Abdo 2021-12-20 13:22:21 +03:00 committed by GitHub
parent 0c7a9be359
commit 1120939648
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 9 deletions

View file

@ -121,7 +121,6 @@ class Editor:
def setupWeb(self) -> None:
self.web = EditorWebView(self.widget, self)
self.web.allowDrops = True
self.web.set_bridge_command(self.onBridgeCmd, self)
self.outerLayout.addWidget(self.web, 1)

View file

@ -71,6 +71,7 @@ from aqt.utils import (
tooltip,
tr,
)
from aqt.webview import AnkiWebView
install_pylib_legacy()
@ -82,11 +83,46 @@ MainWindowState = Literal[
T = TypeVar("T")
class MainWebView(AnkiWebView):
def __init__(self, mw: AnkiQt) -> None:
AnkiWebView.__init__(self, title="main webview")
self.mw = mw
self.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
self.setMinimumWidth(400)
self.setAcceptDrops(True)
# Importing files via drag & drop
##########################################################################
def dragEnterEvent(self, event: QDragEnterEvent) -> None:
if self.mw.state != "deckBrowser":
return super().dragEnterEvent(event)
mime = event.mimeData()
if not mime.hasUrls():
return
for url in mime.urls():
path = url.toLocalFile()
if not os.path.exists(path) or os.path.isdir(path):
return
event.accept()
def dropEvent(self, event: QDropEvent) -> None:
import aqt.importing
if self.mw.state != "deckBrowser":
return super().dropEvent(event)
mime = event.mimeData()
paths = [url.toLocalFile() for url in mime.urls()]
deck_paths = filter(lambda p: not p.endswith(".colpkg"), paths)
for path in deck_paths:
aqt.importing.importFile(self.mw, path)
class AnkiQt(QMainWindow):
col: Collection
pm: ProfileManagerType
web: aqt.webview.AnkiWebView
bottomWeb: aqt.webview.AnkiWebView
web: MainWebView
bottomWeb: AnkiWebView
def __init__(
self,
@ -828,15 +864,13 @@ title="{}" {}>{}</button>""".format(
self.form = aqt.forms.main.Ui_MainWindow()
self.form.setupUi(self)
# toolbar
tweb = self.toolbarWeb = aqt.webview.AnkiWebView(title="top toolbar")
tweb = self.toolbarWeb = AnkiWebView(title="top toolbar")
tweb.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
self.toolbar = aqt.toolbar.Toolbar(self, tweb)
# main area
self.web = aqt.webview.AnkiWebView(title="main webview")
self.web.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
self.web.setMinimumWidth(400)
self.web = MainWebView(self)
# bottom area
sweb = self.bottomWeb = aqt.webview.AnkiWebView(title="bottom toolbar")
sweb = self.bottomWeb = AnkiWebView(title="bottom toolbar")
sweb.setFocusPolicy(Qt.FocusPolicy.WheelFocus)
# add in a layout
self.mainLayout = QVBoxLayout()

View file

@ -243,7 +243,6 @@ class AnkiWebView(QWebEngineView):
self.setPage(self._page)
self.resetHandlers()
self.allowDrops = False
self._filterSet = False
QShortcut( # type: ignore
QKeySequence("Esc"),