2022-06-01 12:26:16 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-09-09 01:12:31 +02:00
|
|
|
import json
|
2023-09-14 01:06:15 +02:00
|
|
|
from dataclasses import dataclass
|
2023-09-09 01:12:31 +02:00
|
|
|
|
2022-06-01 12:26:16 +02:00
|
|
|
import aqt
|
|
|
|
import aqt.deckconf
|
|
|
|
import aqt.main
|
|
|
|
import aqt.operations
|
|
|
|
from aqt.qt import *
|
|
|
|
from aqt.utils import addCloseShortcut, disable_help_button, restoreGeom, saveGeom, tr
|
2023-02-10 05:53:11 +01:00
|
|
|
from aqt.webview import AnkiWebView, AnkiWebViewKind
|
2022-06-01 12:26:16 +02:00
|
|
|
|
|
|
|
|
2023-09-14 01:06:15 +02:00
|
|
|
@dataclass
|
|
|
|
class ImportArgs:
|
|
|
|
path: str
|
|
|
|
title = "importLog"
|
|
|
|
kind = AnkiWebViewKind.IMPORT_LOG
|
|
|
|
ts_page = "import-page"
|
|
|
|
setup_function_name = "setupImportPage"
|
|
|
|
|
|
|
|
def args_json(self) -> str:
|
|
|
|
return json.dumps(self.path)
|
|
|
|
|
|
|
|
|
|
|
|
class JsonFileArgs(ImportArgs):
|
|
|
|
def args_json(self) -> str:
|
|
|
|
return json.dumps(dict(type="json_file", path=self.path))
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class JsonStringArgs(ImportArgs):
|
|
|
|
json: str
|
|
|
|
|
|
|
|
def args_json(self) -> str:
|
|
|
|
return json.dumps(dict(type="json_string", path=self.path, json=self.json))
|
|
|
|
|
|
|
|
|
|
|
|
class CsvArgs(ImportArgs):
|
|
|
|
title = "csv import"
|
|
|
|
kind = AnkiWebViewKind.IMPORT_CSV
|
|
|
|
ts_page = "import-csv"
|
|
|
|
setup_function_name = "setupImportCsvPage"
|
|
|
|
|
|
|
|
|
|
|
|
class AnkiPackageArgs(ImportArgs):
|
|
|
|
title = "anki package import"
|
|
|
|
kind = AnkiWebViewKind.IMPORT_ANKI_PACKAGE
|
|
|
|
ts_page = "import-anki-package"
|
|
|
|
setup_function_name = "setupImportAnkiPackagePage"
|
|
|
|
|
|
|
|
|
2023-09-09 01:00:55 +02:00
|
|
|
class ImportDialog(QDialog):
|
|
|
|
DEFAULT_SIZE = (800, 800)
|
|
|
|
MIN_SIZE = (400, 300)
|
2022-06-01 12:26:16 +02:00
|
|
|
silentlyClose = True
|
|
|
|
|
2023-09-14 01:06:15 +02:00
|
|
|
def __init__(self, mw: aqt.main.AnkiQt, args: ImportArgs) -> None:
|
2023-08-02 12:29:44 +02:00
|
|
|
QDialog.__init__(self, mw, Qt.WindowType.Window)
|
2022-06-01 12:26:16 +02:00
|
|
|
self.mw = mw
|
2023-09-14 01:06:15 +02:00
|
|
|
self.args = args
|
|
|
|
self._setup_ui()
|
2022-06-01 12:26:16 +02:00
|
|
|
self.show()
|
|
|
|
|
2023-09-14 01:06:15 +02:00
|
|
|
def _setup_ui(self) -> None:
|
2022-06-01 12:26:16 +02:00
|
|
|
self.setWindowModality(Qt.WindowModality.ApplicationModal)
|
|
|
|
self.mw.garbage_collect_on_dialog_finish(self)
|
2023-09-09 01:00:55 +02:00
|
|
|
self.setMinimumSize(*self.MIN_SIZE)
|
2022-06-01 12:26:16 +02:00
|
|
|
disable_help_button(self)
|
2023-09-14 01:06:15 +02:00
|
|
|
restoreGeom(self, self.args.title, default_size=self.DEFAULT_SIZE)
|
2022-06-01 12:26:16 +02:00
|
|
|
addCloseShortcut(self)
|
|
|
|
|
2023-09-14 01:06:15 +02:00
|
|
|
self.web = AnkiWebView(kind=self.args.kind)
|
2022-06-01 12:26:16 +02:00
|
|
|
self.web.setVisible(False)
|
2023-09-14 01:06:15 +02:00
|
|
|
self.web.load_ts_page(self.args.ts_page)
|
2022-06-01 12:26:16 +02:00
|
|
|
layout = QVBoxLayout()
|
|
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
layout.addWidget(self.web)
|
|
|
|
self.setLayout(layout)
|
2023-09-14 01:06:15 +02:00
|
|
|
restoreGeom(self, self.args.title, default_size=(800, 800))
|
2022-06-01 12:26:16 +02:00
|
|
|
|
2023-01-18 06:41:47 +01:00
|
|
|
self.web.evalWithCallback(
|
2023-09-14 01:06:15 +02:00
|
|
|
f"anki.{self.args.setup_function_name}({self.args.args_json()});",
|
2023-09-09 01:00:55 +02:00
|
|
|
lambda _: self.web.setFocus(),
|
2023-01-18 06:41:47 +01:00
|
|
|
)
|
2022-06-01 12:26:16 +02:00
|
|
|
self.setWindowTitle(tr.decks_import_file())
|
|
|
|
|
|
|
|
def reject(self) -> None:
|
2023-08-02 12:29:44 +02:00
|
|
|
if self.mw.col and self.windowModality() == Qt.WindowModality.ApplicationModal:
|
|
|
|
self.mw.col.set_wants_abort()
|
2022-06-01 12:26:16 +02:00
|
|
|
self.web.cleanup()
|
|
|
|
self.web = None
|
2023-09-14 01:06:15 +02:00
|
|
|
saveGeom(self, self.args.title)
|
2022-06-01 12:26:16 +02:00
|
|
|
QDialog.reject(self)
|