2019-02-05 04:59:03 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
2012-12-21 08:51:59 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
2020-01-15 22:41:23 +01:00
|
|
|
from typing import Callable, List, Optional
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-20 10:19:03 +01:00
|
|
|
import aqt.deckchooser
|
|
|
|
import aqt.editor
|
2012-12-21 08:51:59 +01:00
|
|
|
import aqt.forms
|
2019-12-20 10:19:03 +01:00
|
|
|
import aqt.modelchooser
|
2021-01-30 12:51:24 +01:00
|
|
|
from anki.collection import SearchTerm
|
2020-04-07 07:02:53 +02:00
|
|
|
from anki.consts import MODEL_CLOZE
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.notes import Note
|
2019-03-04 08:25:19 +01:00
|
|
|
from anki.utils import htmlToTextLine, isMac
|
2020-01-15 03:46:53 +01:00
|
|
|
from aqt import AnkiQt, gui_hooks
|
2020-08-16 18:49:51 +02:00
|
|
|
from aqt.main import ResetReason
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt.qt import *
|
2020-01-20 11:10:38 +01:00
|
|
|
from aqt.sound import av_player
|
2019-12-23 01:34:10 +01:00
|
|
|
from aqt.utils import (
|
2020-11-17 08:42:43 +01:00
|
|
|
TR,
|
2021-01-25 14:45:47 +01:00
|
|
|
HelpPage,
|
2019-12-23 01:34:10 +01:00
|
|
|
addCloseShortcut,
|
|
|
|
askUser,
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button,
|
2019-12-23 01:34:10 +01:00
|
|
|
downArrow,
|
|
|
|
openHelp,
|
|
|
|
restoreGeom,
|
|
|
|
saveGeom,
|
|
|
|
shortcut,
|
|
|
|
showWarning,
|
|
|
|
tooltip,
|
2020-11-17 08:42:43 +01:00
|
|
|
tr,
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
class AddCards(QDialog):
|
2019-12-20 09:43:52 +01:00
|
|
|
def __init__(self, mw: AnkiQt) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.__init__(self, None, Qt.Window)
|
2016-07-04 05:22:35 +02:00
|
|
|
mw.setupDialogGC(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.mw = mw
|
|
|
|
self.form = aqt.forms.addcards.Ui_Dialog()
|
|
|
|
self.form.setupUi(self)
|
2020-11-17 08:42:43 +01:00
|
|
|
self.setWindowTitle(tr(TR.ACTIONS_ADD))
|
2021-01-07 05:24:49 +01:00
|
|
|
disable_help_button(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.setMinimumHeight(300)
|
|
|
|
self.setMinimumWidth(400)
|
|
|
|
self.setupChoosers()
|
|
|
|
self.setupEditor()
|
|
|
|
self.setupButtons()
|
|
|
|
self.onReset()
|
2019-12-16 10:46:40 +01:00
|
|
|
self.history: List[int] = []
|
2019-11-21 02:18:01 +01:00
|
|
|
self.previousNote = None
|
2012-12-21 08:51:59 +01:00
|
|
|
restoreGeom(self, "add")
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.append(self.onReset)
|
|
|
|
gui_hooks.current_note_type_did_change.append(self.onModelChange)
|
2012-12-22 01:11:29 +01:00
|
|
|
addCloseShortcut(self)
|
2020-04-03 10:54:54 +02:00
|
|
|
gui_hooks.add_cards_did_init(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def setupEditor(self) -> None:
|
2019-12-23 01:34:10 +01:00
|
|
|
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, self, True)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def setupChoosers(self) -> None:
|
2020-10-05 05:33:54 +02:00
|
|
|
self.modelChooser = aqt.modelchooser.ModelChooser(
|
|
|
|
self.mw, self.form.modelArea, on_activated=self.show_notetype_selector
|
|
|
|
)
|
2019-12-23 01:34:10 +01:00
|
|
|
self.deckChooser = aqt.deckchooser.DeckChooser(self.mw, self.form.deckArea)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def helpRequested(self):
|
2021-01-25 14:45:47 +01:00
|
|
|
openHelp(HelpPage.ADDING_CARD_AND_NOTE)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def setupButtons(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
bb = self.form.buttonBox
|
|
|
|
ar = QDialogButtonBox.ActionRole
|
|
|
|
# add
|
2020-11-17 08:42:43 +01:00
|
|
|
self.addButton = bb.addButton(tr(TR.ACTIONS_ADD), ar)
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(self.addButton.clicked, self.addCards)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addButton.setShortcut(QKeySequence("Ctrl+Return"))
|
2020-11-17 08:42:43 +01:00
|
|
|
self.addButton.setToolTip(shortcut(tr(TR.ADDING_ADD_SHORTCUT_CTRLANDENTER)))
|
2012-12-21 08:51:59 +01:00
|
|
|
# close
|
2020-11-17 08:42:43 +01:00
|
|
|
self.closeButton = QPushButton(tr(TR.ACTIONS_CLOSE))
|
2012-12-21 08:51:59 +01:00
|
|
|
self.closeButton.setAutoDefault(False)
|
2016-05-31 10:51:40 +02:00
|
|
|
bb.addButton(self.closeButton, QDialogButtonBox.RejectRole)
|
2012-12-21 08:51:59 +01:00
|
|
|
# help
|
2020-11-17 08:42:43 +01:00
|
|
|
self.helpButton = QPushButton(tr(TR.ACTIONS_HELP), clicked=self.helpRequested) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
self.helpButton.setAutoDefault(False)
|
2019-12-23 01:34:10 +01:00
|
|
|
bb.addButton(self.helpButton, QDialogButtonBox.HelpRole)
|
2012-12-21 08:51:59 +01:00
|
|
|
# history
|
2020-11-17 08:42:43 +01:00
|
|
|
b = bb.addButton(tr(TR.ADDING_HISTORY) + " " + downArrow(), ar)
|
2013-05-23 07:44:00 +02:00
|
|
|
if isMac:
|
|
|
|
sc = "Ctrl+Shift+H"
|
|
|
|
else:
|
|
|
|
sc = "Ctrl+H"
|
|
|
|
b.setShortcut(QKeySequence(sc))
|
2020-11-17 12:47:47 +01:00
|
|
|
b.setToolTip(tr(TR.ADDING_SHORTCUT, val=shortcut(sc)))
|
2020-05-04 05:23:08 +02:00
|
|
|
qconnect(b.clicked, self.onHistory)
|
2012-12-21 08:51:59 +01:00
|
|
|
b.setEnabled(False)
|
|
|
|
self.historyButton = b
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def setAndFocusNote(self, note: Note) -> None:
|
2017-08-05 07:15:19 +02:00
|
|
|
self.editor.setNote(note, focusTo=0)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-10-05 05:33:54 +02:00
|
|
|
def show_notetype_selector(self) -> None:
|
|
|
|
self.editor.saveNow(self.modelChooser.onModelChange)
|
|
|
|
|
2020-01-24 06:46:42 +01:00
|
|
|
def onModelChange(self, unused=None) -> None:
|
2016-04-10 13:11:25 +02:00
|
|
|
oldNote = self.editor.note
|
2017-08-05 07:15:19 +02:00
|
|
|
note = self.mw.col.newNote()
|
2019-11-21 02:18:01 +01:00
|
|
|
self.previousNote = None
|
2016-04-10 13:11:25 +02:00
|
|
|
if oldNote:
|
2016-05-12 06:45:35 +02:00
|
|
|
oldFields = list(oldNote.keys())
|
|
|
|
newFields = list(note.keys())
|
2019-12-23 01:34:10 +01:00
|
|
|
for n, f in enumerate(note.model()["flds"]):
|
|
|
|
fieldName = f["name"]
|
2016-04-10 13:11:25 +02:00
|
|
|
# copy identical fields
|
|
|
|
if fieldName in oldFields:
|
|
|
|
note[fieldName] = oldNote[fieldName]
|
2020-04-26 16:53:23 +02:00
|
|
|
elif n < len(oldNote.model()["flds"]):
|
2020-04-26 16:52:35 +02:00
|
|
|
# set non-identical fields by field index
|
2020-04-26 16:53:23 +02:00
|
|
|
oldFieldName = oldNote.model()["flds"][n]["name"]
|
|
|
|
if oldFieldName not in newFields:
|
2016-04-10 13:11:25 +02:00
|
|
|
note.fields[n] = oldNote.fields[n]
|
2020-04-26 16:09:14 +02:00
|
|
|
self.editor.note = note
|
|
|
|
# When on model change is called, reset is necessarily called.
|
|
|
|
# Reset load note, so it is not required to load it here.
|
2016-04-10 13:11:25 +02:00
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def onReset(self, model: None = None, keep: bool = False) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
oldNote = self.editor.note
|
2017-08-05 07:15:19 +02:00
|
|
|
note = self.mw.col.newNote()
|
2019-12-23 01:34:10 +01:00
|
|
|
flds = note.model()["flds"]
|
2012-12-21 08:51:59 +01:00
|
|
|
# copy fields from old note
|
|
|
|
if oldNote:
|
2020-04-26 14:42:54 +02:00
|
|
|
for n in range(min(len(note.fields), len(oldNote.fields))):
|
|
|
|
if not keep or flds[n]["sticky"]:
|
|
|
|
note.fields[n] = oldNote.fields[n]
|
2017-08-05 07:15:19 +02:00
|
|
|
self.setAndFocusNote(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def removeTempNote(self, note: Note) -> None:
|
2020-05-20 07:01:05 +02:00
|
|
|
print("removeTempNote() will go away")
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def addHistory(self, note):
|
2017-01-08 14:48:58 +01:00
|
|
|
self.history.insert(0, note.id)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.history = self.history[:15]
|
|
|
|
self.historyButton.setEnabled(True)
|
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def onHistory(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
m = QMenu(self)
|
2017-01-08 14:48:58 +01:00
|
|
|
for nid in self.history:
|
2021-01-30 02:23:32 +01:00
|
|
|
if self.mw.col.findNotes(SearchTerm(nid=nid)):
|
2020-06-03 23:29:09 +02:00
|
|
|
note = self.mw.col.getNote(nid)
|
|
|
|
fields = note.fields
|
2017-03-14 07:48:40 +01:00
|
|
|
txt = htmlToTextLine(", ".join(fields))
|
2017-02-05 08:45:08 +01:00
|
|
|
if len(txt) > 30:
|
|
|
|
txt = txt[:30] + "..."
|
2020-11-18 02:53:33 +01:00
|
|
|
line = tr(TR.ADDING_EDIT, val=txt)
|
2020-06-03 23:29:09 +02:00
|
|
|
line = gui_hooks.addcards_will_add_history_entry(line, note)
|
|
|
|
a = m.addAction(line)
|
2020-01-15 22:41:23 +01:00
|
|
|
qconnect(a.triggered, lambda b, nid=nid: self.editHistory(nid))
|
2017-01-09 11:55:30 +01:00
|
|
|
else:
|
2020-11-17 08:42:43 +01:00
|
|
|
a = m.addAction(tr(TR.ADDING_NOTE_DELETED))
|
2017-01-09 11:55:30 +01:00
|
|
|
a.setEnabled(False)
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.add_cards_will_show_history_menu(self, m)
|
2019-12-23 01:34:10 +01:00
|
|
|
m.exec_(self.historyButton.mapToGlobal(QPoint(0, 0)))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def editHistory(self, nid):
|
2021-01-30 02:23:32 +01:00
|
|
|
self.mw.browser_search(SearchTerm(nid=nid))
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2020-01-15 22:41:23 +01:00
|
|
|
def addNote(self, note) -> Optional[Note]:
|
2019-12-23 01:34:10 +01:00
|
|
|
note.model()["did"] = self.deckChooser.selectedId()
|
2012-12-21 08:51:59 +01:00
|
|
|
ret = note.dupeOrEmpty()
|
2020-03-20 11:59:59 +01:00
|
|
|
problem = None
|
2012-12-21 08:51:59 +01:00
|
|
|
if ret == 1:
|
2020-11-17 08:42:43 +01:00
|
|
|
problem = tr(TR.ADDING_THE_FIRST_FIELD_IS_EMPTY)
|
2020-03-20 11:59:59 +01:00
|
|
|
problem = gui_hooks.add_cards_will_add_note(problem, note)
|
|
|
|
if problem is not None:
|
2021-01-25 14:45:47 +01:00
|
|
|
showWarning(problem, help=HelpPage.ADDING_CARD_AND_NOTE)
|
2020-01-15 22:41:23 +01:00
|
|
|
return None
|
2020-04-07 07:02:53 +02:00
|
|
|
if note.model()["type"] == MODEL_CLOZE:
|
2020-05-14 13:56:45 +02:00
|
|
|
if not note.cloze_numbers_in_fields():
|
2020-11-18 02:32:22 +01:00
|
|
|
if not askUser(tr(TR.ADDING_YOU_HAVE_A_CLOZE_DELETION_NOTE)):
|
2020-01-15 22:41:23 +01:00
|
|
|
return None
|
2020-05-14 13:59:42 +02:00
|
|
|
self.mw.col.add_note(note, self.deckChooser.selectedId())
|
2019-12-06 05:28:57 +01:00
|
|
|
self.mw.col.clearUndo()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addHistory(note)
|
2019-11-21 02:18:01 +01:00
|
|
|
self.previousNote = note
|
2020-08-16 18:49:51 +02:00
|
|
|
self.mw.requireReset(reason=ResetReason.AddCardsAddNote, context=self)
|
2020-01-15 08:45:35 +01:00
|
|
|
gui_hooks.add_cards_did_add_note(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
return note
|
|
|
|
|
|
|
|
def addCards(self):
|
2016-07-14 12:23:44 +02:00
|
|
|
self.editor.saveNow(self._addCards)
|
|
|
|
|
|
|
|
def _addCards(self):
|
2012-12-21 08:51:59 +01:00
|
|
|
self.editor.saveAddModeVars()
|
2019-11-21 02:18:52 +01:00
|
|
|
if not self.addNote(self.editor.note):
|
2012-12-21 08:51:59 +01:00
|
|
|
return
|
2020-10-24 10:47:25 +02:00
|
|
|
|
|
|
|
# workaround for PyQt focus bug
|
|
|
|
self.editor.hideCompleters()
|
|
|
|
|
2020-11-17 08:42:43 +01:00
|
|
|
tooltip(tr(TR.ADDING_ADDED), period=500)
|
2020-01-20 11:10:38 +01:00
|
|
|
av_player.stop_and_clear_queue()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.onReset(keep=True)
|
|
|
|
self.mw.col.autosave()
|
|
|
|
|
|
|
|
def keyPressEvent(self, evt):
|
|
|
|
"Show answer on RET or register answer."
|
2019-12-23 01:34:10 +01:00
|
|
|
if evt.key() in (Qt.Key_Enter, Qt.Key_Return) and self.editor.tags.hasFocus():
|
2012-12-21 08:51:59 +01:00
|
|
|
evt.accept()
|
|
|
|
return
|
|
|
|
return QDialog.keyPressEvent(self, evt)
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def reject(self) -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
self.ifCanClose(self._reject)
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def _reject(self) -> None:
|
2020-01-15 07:53:24 +01:00
|
|
|
gui_hooks.state_did_reset.remove(self.onReset)
|
|
|
|
gui_hooks.current_note_type_did_change.remove(self.onModelChange)
|
2020-01-20 11:10:38 +01:00
|
|
|
av_player.stop_and_clear_queue()
|
2017-08-16 04:45:33 +02:00
|
|
|
self.editor.cleanup()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.modelChooser.cleanup()
|
|
|
|
self.deckChooser.cleanup()
|
|
|
|
self.mw.maybeReset()
|
|
|
|
saveGeom(self, "add")
|
2017-08-16 04:45:33 +02:00
|
|
|
aqt.dialogs.markClosed("AddCards")
|
2012-12-21 08:51:59 +01:00
|
|
|
QDialog.reject(self)
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def ifCanClose(self, onOk: Callable) -> None:
|
2017-08-16 04:45:33 +02:00
|
|
|
def afterSave():
|
2019-12-23 01:34:10 +01:00
|
|
|
ok = self.editor.fieldsAreBlank(self.previousNote) or askUser(
|
2020-11-17 08:42:43 +01:00
|
|
|
tr(TR.ADDING_CLOSE_AND_LOSE_CURRENT_INPUT), defaultno=True
|
2019-12-23 01:34:10 +01:00
|
|
|
)
|
2017-08-16 04:45:33 +02:00
|
|
|
if ok:
|
|
|
|
onOk()
|
|
|
|
|
|
|
|
self.editor.saveNow(afterSave)
|
|
|
|
|
|
|
|
def closeWithCallback(self, cb):
|
|
|
|
def doClose():
|
|
|
|
self._reject()
|
|
|
|
cb()
|
2019-12-23 01:34:10 +01:00
|
|
|
|
2017-08-16 04:45:33 +02:00
|
|
|
self.ifCanClose(doClose)
|