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
|
2019-12-20 10:19:03 +01:00
|
|
|
from typing import Callable, List
|
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
|
2013-07-16 09:42:50 +02:00
|
|
|
from anki.hooks import addHook, remHook, runHook
|
2019-12-20 10:19:03 +01:00
|
|
|
from anki.lang import _
|
|
|
|
from anki.notes import Note
|
|
|
|
from anki.sound import clearAudioQueue
|
2019-03-04 08:25:19 +01:00
|
|
|
from anki.utils import htmlToTextLine, isMac
|
2019-12-20 10:19:03 +01:00
|
|
|
from aqt import AnkiQt
|
|
|
|
from aqt.qt import *
|
|
|
|
from aqt.utils import (addCloseShortcut, askUser, downArrow, openHelp,
|
|
|
|
restoreGeom, saveGeom, shortcut, showWarning, tooltip)
|
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)
|
|
|
|
self.setWindowTitle(_("Add"))
|
|
|
|
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")
|
|
|
|
addHook('reset', self.onReset)
|
2016-04-10 13:11:25 +02:00
|
|
|
addHook('currentModelChanged', self.onModelChange)
|
2012-12-22 01:11:29 +01:00
|
|
|
addCloseShortcut(self)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.show()
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def setupEditor(self) -> None:
|
2012-12-21 08:51:59 +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:
|
2012-12-21 08:51:59 +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:
|
2012-12-21 08:51:59 +01:00
|
|
|
bb = self.form.buttonBox
|
|
|
|
ar = QDialogButtonBox.ActionRole
|
|
|
|
# add
|
|
|
|
self.addButton = bb.addButton(_("Add"), ar)
|
2016-05-31 10:51:40 +02:00
|
|
|
self.addButton.clicked.connect(self.addCards)
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addButton.setShortcut(QKeySequence("Ctrl+Return"))
|
|
|
|
self.addButton.setToolTip(shortcut(_("Add (shortcut: ctrl+enter)")))
|
|
|
|
# close
|
|
|
|
self.closeButton = QPushButton(_("Close"))
|
|
|
|
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
|
2019-12-20 09:43:52 +01:00
|
|
|
self.helpButton = QPushButton(_("Help"), clicked=self.helpRequested) # type: ignore
|
2012-12-21 08:51:59 +01:00
|
|
|
self.helpButton.setAutoDefault(False)
|
|
|
|
bb.addButton(self.helpButton,
|
|
|
|
QDialogButtonBox.HelpRole)
|
|
|
|
# history
|
|
|
|
b = bb.addButton(
|
2016-05-12 06:45:35 +02:00
|
|
|
_("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))
|
2016-05-31 10:51:40 +02:00
|
|
|
b.clicked.connect(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
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def onModelChange(self) -> 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())
|
2016-04-10 13:11:25 +02:00
|
|
|
for n, f in enumerate(note.model()['flds']):
|
|
|
|
fieldName = f['name']
|
|
|
|
try:
|
|
|
|
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
|
2017-02-16 05:00:49 +01:00
|
|
|
self.removeTempNote(oldNote)
|
2017-08-05 07:15:19 +02:00
|
|
|
self.editor.setNote(note)
|
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()
|
2012-12-21 08:51:59 +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:
|
|
|
|
if not keep or flds[n]['sticky']:
|
|
|
|
note.fields[n] = oldNote.fields[n]
|
|
|
|
else:
|
|
|
|
note.fields[n] = ""
|
|
|
|
except IndexError:
|
|
|
|
break
|
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:
|
2012-12-21 08:51:59 +01:00
|
|
|
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):
|
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)
|
|
|
|
|
|
|
|
def onHistory(self):
|
|
|
|
m = QMenu(self)
|
2017-01-08 14:48:58 +01:00
|
|
|
for nid in self.history:
|
2017-01-09 11:55:30 +01:00
|
|
|
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] + "..."
|
2017-02-05 08:46:02 +01:00
|
|
|
a = m.addAction(_("Edit \"%s\"") % txt)
|
2017-01-09 11:55:30 +01:00
|
|
|
a.triggered.connect(lambda b, nid=nid: self.editHistory(nid))
|
|
|
|
else:
|
|
|
|
a = m.addAction(_("(Note deleted)"))
|
|
|
|
a.setEnabled(False)
|
2013-07-16 09:42:50 +02:00
|
|
|
runHook("AddCards.onHistory", self, m)
|
2012-12-21 08:51:59 +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)
|
2016-07-14 12:23:44 +02:00
|
|
|
browser.onSearchActivated()
|
2012-12-21 08:51:59 +01:00
|
|
|
|
|
|
|
def addNote(self, note):
|
|
|
|
note.model()['did'] = self.deckChooser.selectedId()
|
|
|
|
ret = note.dupeOrEmpty()
|
|
|
|
if ret == 1:
|
|
|
|
showWarning(_(
|
|
|
|
"The first field is empty."),
|
|
|
|
help="AddItems#AddError")
|
|
|
|
return
|
2013-05-17 08:57:22 +02:00
|
|
|
if '{{cloze:' in note.model()['tmpls'][0]['qfmt']:
|
|
|
|
if not self.mw.col.models._availClozeOrds(
|
|
|
|
note.model(), note.joinedFields(), False):
|
|
|
|
if not askUser(_("You have a cloze deletion note type "
|
|
|
|
"but have not made any cloze deletions. Proceed?")):
|
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
cards = self.mw.col.addNote(note)
|
|
|
|
if not cards:
|
|
|
|
showWarning(_("""\
|
|
|
|
The input you have provided would make an empty \
|
|
|
|
question on all cards."""), help="AddItems")
|
|
|
|
return
|
2019-12-06 05:28:57 +01:00
|
|
|
self.mw.col.clearUndo()
|
2012-12-21 08:51:59 +01:00
|
|
|
self.addHistory(note)
|
|
|
|
self.mw.requireReset()
|
2019-11-21 02:18:01 +01:00
|
|
|
self.previousNote = 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
|
|
|
|
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."
|
|
|
|
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:
|
2017-08-16 04:45:33 +02:00
|
|
|
self.ifCanClose(self._reject)
|
|
|
|
|
2019-12-20 09:43:52 +01:00
|
|
|
def _reject(self) -> None:
|
2012-12-21 08:51:59 +01:00
|
|
|
remHook('reset', self.onReset)
|
2016-04-10 13:11:25 +02:00
|
|
|
remHook('currentModelChanged', self.onModelChange)
|
2012-12-21 08:51:59 +01:00
|
|
|
clearAudioQueue()
|
|
|
|
self.removeTempNote(self.editor.note)
|
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-11-21 02:18:52 +01:00
|
|
|
ok = (self.editor.fieldsAreBlank(self.previousNote) or
|
2019-09-01 07:09:03 +02:00
|
|
|
askUser(_("Close and lose current input?"), defaultno=True))
|
2017-08-16 04:45:33 +02:00
|
|
|
if ok:
|
|
|
|
onOk()
|
|
|
|
|
|
|
|
self.editor.saveNow(afterSave)
|
|
|
|
|
|
|
|
def closeWithCallback(self, cb):
|
|
|
|
def doClose():
|
|
|
|
self._reject()
|
|
|
|
cb()
|
|
|
|
self.ifCanClose(doClose)
|