anki/qt/aqt/addcards.py

254 lines
8.5 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# -*- coding: utf-8 -*-
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2019-12-20 10:19:03 +01:00
from typing import Callable, List
2019-12-20 10:19:03 +01:00
import aqt.deckchooser
import aqt.editor
import aqt.forms
2019-12-20 10:19:03 +01:00
import aqt.modelchooser
from anki.lang import _
from anki.notes import Note
2019-03-04 08:25:19 +01:00
from anki.utils import htmlToTextLine, isMac
from aqt import AnkiQt, gui_hooks
2019-12-20 10:19:03 +01:00
from aqt.qt import *
from aqt.sound import clearAudioQueue
2019-12-23 01:34:10 +01:00
from aqt.utils import (
addCloseShortcut,
askUser,
downArrow,
openHelp,
restoreGeom,
saveGeom,
shortcut,
showWarning,
tooltip,
)
2019-12-20 09:43:52 +01:00
class AddCards(QDialog):
2019-12-20 09:43:52 +01:00
def __init__(self, mw: AnkiQt) -> None:
QDialog.__init__(self, None, Qt.Window)
mw.setupDialogGC(self)
self.mw = mw
self.form = aqt.forms.addcards.Ui_Dialog()
self.form.setupUi(self)
self.setWindowTitle(_("Add"))
self.setMinimumHeight(300)
self.setMinimumWidth(400)
self.setupChoosers()
self.setupEditor()
self.setupButtons()
self.onReset()
self.history: List[int] = []
2019-11-21 02:18:01 +01:00
self.previousNote = None
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)
addCloseShortcut(self)
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)
2019-12-20 09:43:52 +01:00
def setupChoosers(self) -> None:
2019-12-23 01:34:10 +01:00
self.modelChooser = aqt.modelchooser.ModelChooser(self.mw, self.form.modelArea)
self.deckChooser = aqt.deckchooser.DeckChooser(self.mw, self.form.deckArea)
def helpRequested(self):
openHelp("addingnotes")
2019-12-20 09:43:52 +01:00
def setupButtons(self) -> None:
bb = self.form.buttonBox
ar = QDialogButtonBox.ActionRole
# add
self.addButton = bb.addButton(_("Add"), ar)
self.addButton.clicked.connect(self.addCards)
self.addButton.setShortcut(QKeySequence("Ctrl+Return"))
self.addButton.setToolTip(shortcut(_("Add (shortcut: ctrl+enter)")))
# close
self.closeButton = QPushButton(_("Close"))
self.closeButton.setAutoDefault(False)
bb.addButton(self.closeButton, QDialogButtonBox.RejectRole)
# help
2019-12-23 01:34:10 +01:00
self.helpButton = QPushButton(_("Help"), clicked=self.helpRequested) # type: ignore
self.helpButton.setAutoDefault(False)
2019-12-23 01:34:10 +01:00
bb.addButton(self.helpButton, QDialogButtonBox.HelpRole)
# history
2019-12-23 01:34:10 +01:00
b = bb.addButton(_("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))
b.setToolTip(_("Shortcut: %s") % shortcut(sc))
b.clicked.connect(self.onHistory)
b.setEnabled(False)
self.historyButton = b
2019-12-20 09:43:52 +01:00
def setAndFocusNote(self, note: Note) -> None:
self.editor.setNote(note, focusTo=0)
2020-01-15 04:49:26 +01:00
def onModelChange(self, unused) -> None:
oldNote = self.editor.note
note = self.mw.col.newNote()
2019-11-21 02:18:01 +01:00
self.previousNote = None
if oldNote:
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"]
try:
2019-12-23 01:34:10 +01:00
oldFieldName = oldNote.model()["flds"][n]["name"]
except IndexError:
oldFieldName = None
# copy identical fields
if fieldName in oldFields:
note[fieldName] = oldNote[fieldName]
# set non-identical fields by field index
elif oldFieldName and oldFieldName not in newFields:
try:
note.fields[n] = oldNote.fields[n]
except IndexError:
pass
self.removeTempNote(oldNote)
self.editor.setNote(note)
2019-12-20 09:43:52 +01:00
def onReset(self, model: None = None, keep: bool = False) -> None:
oldNote = self.editor.note
note = self.mw.col.newNote()
2019-12-23 01:34:10 +01:00
flds = note.model()["flds"]
# copy fields from old note
if oldNote:
if not keep:
self.removeTempNote(oldNote)
for n in range(len(note.fields)):
try:
2019-12-23 01:34:10 +01:00
if not keep or flds[n]["sticky"]:
note.fields[n] = oldNote.fields[n]
else:
note.fields[n] = ""
except IndexError:
break
self.setAndFocusNote(note)
2019-12-20 09:43:52 +01:00
def removeTempNote(self, note: Note) -> None:
if not note or not note.id:
return
# we don't have to worry about cards; just the note
self.mw.col._remNotes([note.id])
def addHistory(self, note):
self.history.insert(0, note.id)
self.history = self.history[:15]
self.historyButton.setEnabled(True)
def onHistory(self):
m = QMenu(self)
for nid in self.history:
if self.mw.col.findNotes("nid:%s" % nid):
fields = self.mw.col.getNote(nid).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] + "..."
2019-12-23 01:34:10 +01:00
a = m.addAction(_('Edit "%s"') % txt)
a.triggered.connect(lambda b, nid=nid: self.editHistory(nid))
else:
a = m.addAction(_("(Note deleted)"))
a.setEnabled(False)
2020-01-15 07:53:24 +01:00
gui_hooks.add_cards_history_menu_will_show(self, m)
2019-12-23 01:34:10 +01:00
m.exec_(self.historyButton.mapToGlobal(QPoint(0, 0)))
def editHistory(self, nid):
browser = aqt.dialogs.open("Browser", self.mw)
browser.form.searchEdit.lineEdit().setText("nid:%d" % nid)
browser.onSearchActivated()
def addNote(self, note):
2019-12-23 01:34:10 +01:00
note.model()["did"] = self.deckChooser.selectedId()
ret = note.dupeOrEmpty()
if ret == 1:
2019-12-23 01:34:10 +01:00
showWarning(_("The first field is empty."), help="AddItems#AddError")
return
2019-12-23 01:34:10 +01:00
if "{{cloze:" in note.model()["tmpls"][0]["qfmt"]:
if not self.mw.col.models._availClozeOrds(
2019-12-23 01:34:10 +01:00
note.model(), note.joinedFields(), False
):
if not askUser(
_(
"You have a cloze deletion note type "
"but have not made any cloze deletions. Proceed?"
)
):
return
cards = self.mw.col.addNote(note)
if not cards:
2019-12-23 01:34:10 +01:00
showWarning(
_(
"""\
The input you have provided would make an empty \
2019-12-23 01:34:10 +01:00
question on all cards."""
),
help="AddItems",
)
return
self.mw.col.clearUndo()
self.addHistory(note)
self.mw.requireReset()
2019-11-21 02:18:01 +01:00
self.previousNote = note
2020-01-15 07:53:24 +01:00
gui_hooks.add_cards_note_did_add(note)
return note
def addCards(self):
self.editor.saveNow(self._addCards)
def _addCards(self):
self.editor.saveAddModeVars()
if not self.addNote(self.editor.note):
return
tooltip(_("Added"), period=500)
# stop anything playing
clearAudioQueue()
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():
evt.accept()
return
return QDialog.keyPressEvent(self, evt)
2019-12-20 09:43:52 +01:00
def reject(self) -> None:
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)
clearAudioQueue()
self.removeTempNote(self.editor.note)
self.editor.cleanup()
self.modelChooser.cleanup()
self.deckChooser.cleanup()
self.mw.maybeReset()
saveGeom(self, "add")
aqt.dialogs.markClosed("AddCards")
QDialog.reject(self)
2019-12-20 09:43:52 +01:00
def ifCanClose(self, onOk: Callable) -> None:
def afterSave():
2019-12-23 01:34:10 +01:00
ok = self.editor.fieldsAreBlank(self.previousNote) or askUser(
_("Close and lose current input?"), defaultno=True
)
if ok:
onOk()
self.editor.saveNow(afterSave)
def closeWithCallback(self, cb):
def doClose():
self._reject()
cb()
2019-12-23 01:34:10 +01:00
self.ifCanClose(doClose)