mirror of
https://github.com/ankitects/anki.git
synced 2025-11-09 14:17:13 -05:00
validity checking
This commit is contained in:
parent
1cdc92d7f5
commit
c55660ef22
1 changed files with 31 additions and 44 deletions
|
|
@ -15,6 +15,14 @@ from aqt.utils import shortcut, showInfo, showWarning, getBase, getFile
|
||||||
import aqt
|
import aqt
|
||||||
import anki.js
|
import anki.js
|
||||||
|
|
||||||
|
# todo:
|
||||||
|
# - tags/groups
|
||||||
|
# if field.fieldModel.features:
|
||||||
|
# w.setLayoutDirection(Qt.RightToLeft)
|
||||||
|
# else:
|
||||||
|
# w.setLayoutDirection(Qt.LeftToRight)
|
||||||
|
|
||||||
|
|
||||||
pics = ("jpg", "jpeg", "png", "tif", "tiff", "gif")
|
pics = ("jpg", "jpeg", "png", "tif", "tiff", "gif")
|
||||||
audio = ("wav", "mp3", "ogg", "flac")
|
audio = ("wav", "mp3", "ogg", "flac")
|
||||||
|
|
||||||
|
|
@ -46,7 +54,7 @@ function onKey() {
|
||||||
clearChangeTimer();
|
clearChangeTimer();
|
||||||
changeTimer = setTimeout(function () {
|
changeTimer = setTimeout(function () {
|
||||||
sendState();
|
sendState();
|
||||||
saveField("key"); }, 200);
|
saveField("key"); }, 600);
|
||||||
};
|
};
|
||||||
|
|
||||||
function sendState() {
|
function sendState() {
|
||||||
|
|
@ -137,6 +145,12 @@ function setFields(fields, focusTo) {
|
||||||
$("#f"+focusTo).focus();
|
$("#f"+focusTo).focus();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function setBackgrounds(cols) {
|
||||||
|
for (var i=0; i<cols.length; i++) {
|
||||||
|
$("#f"+i).css("background", cols[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$(function () {
|
$(function () {
|
||||||
// ignore drops outside the editable area
|
// ignore drops outside the editable area
|
||||||
document.body.ondragover = function () {
|
document.body.ondragover = function () {
|
||||||
|
|
@ -290,7 +304,7 @@ class Editor(object):
|
||||||
# focus lost or key/button pressed?
|
# focus lost or key/button pressed?
|
||||||
if str.startswith("blur") or str.startswith("key"):
|
if str.startswith("blur") or str.startswith("key"):
|
||||||
(type, txt) = str.split(":", 1)
|
(type, txt) = str.split(":", 1)
|
||||||
self.fact._fields[self.currentField] = txt
|
self.fact._fields[self.currentField] = self.mungeHTML(txt)
|
||||||
print "save fact", txt
|
print "save fact", txt
|
||||||
if type == "blur":
|
if type == "blur":
|
||||||
if not self._keepButtons:
|
if not self._keepButtons:
|
||||||
|
|
@ -299,6 +313,7 @@ class Editor(object):
|
||||||
else:
|
else:
|
||||||
runHook("editor.keyPressed", self.fact)
|
runHook("editor.keyPressed", self.fact)
|
||||||
self.fact.flush()
|
self.fact.flush()
|
||||||
|
self.checkValid()
|
||||||
# focused into field?
|
# focused into field?
|
||||||
elif str.startswith("focus"):
|
elif str.startswith("focus"):
|
||||||
(type, num) = str.split(":", 1)
|
(type, num) = str.split(":", 1)
|
||||||
|
|
@ -316,6 +331,11 @@ class Editor(object):
|
||||||
else:
|
else:
|
||||||
print str
|
print str
|
||||||
|
|
||||||
|
def mungeHTML(self, txt):
|
||||||
|
if txt == "<br>":
|
||||||
|
txt = ""
|
||||||
|
return txt
|
||||||
|
|
||||||
# Setting/unsetting the current fact
|
# Setting/unsetting the current fact
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
|
@ -339,9 +359,9 @@ class Editor(object):
|
||||||
if not self._loaded:
|
if not self._loaded:
|
||||||
# will be loaded when page is ready
|
# will be loaded when page is ready
|
||||||
return
|
return
|
||||||
# fixme: focus on first widget
|
|
||||||
self.web.eval("setFields(%s, %d);" % (
|
self.web.eval("setFields(%s, %d);" % (
|
||||||
simplejson.dumps(self.fact.items()), field))
|
simplejson.dumps(self.fact.items()), field))
|
||||||
|
self.checkValid()
|
||||||
self.widget.show()
|
self.widget.show()
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
|
|
@ -353,23 +373,6 @@ class Editor(object):
|
||||||
def deckClosedHook(self):
|
def deckClosedHook(self):
|
||||||
self.setFact(None)
|
self.setFact(None)
|
||||||
|
|
||||||
# if field.fieldModel.features:
|
|
||||||
# w.setLayoutDirection(Qt.RightToLeft)
|
|
||||||
# else:
|
|
||||||
# w.setLayoutDirection(Qt.LeftToRight)
|
|
||||||
|
|
||||||
# catch changes
|
|
||||||
w.connect(w, SIGNAL("lostFocus"),
|
|
||||||
lambda w=w: self.onFocusLost(w))
|
|
||||||
w.connect(w, SIGNAL("textChanged()"),
|
|
||||||
self.onTextChanged)
|
|
||||||
w.connect(w, SIGNAL("currentCharFormatChanged(QTextCharFormat)"),
|
|
||||||
lambda w=w: self.formatChanged(w))
|
|
||||||
return w
|
|
||||||
|
|
||||||
if check:
|
|
||||||
self.checkValid()
|
|
||||||
|
|
||||||
def saveFieldsNow(self):
|
def saveFieldsNow(self):
|
||||||
"Must call this before adding cards, closing dialog, etc."
|
"Must call this before adding cards, closing dialog, etc."
|
||||||
if not self.fact:
|
if not self.fact:
|
||||||
|
|
@ -379,27 +382,17 @@ class Editor(object):
|
||||||
self._keepButtons = False
|
self._keepButtons = False
|
||||||
self.onTagChange()
|
self.onTagChange()
|
||||||
self.onGroupChange()
|
self.onGroupChange()
|
||||||
# ensure valid
|
|
||||||
self.checkValid()
|
|
||||||
|
|
||||||
def checkValid(self):
|
def checkValid(self):
|
||||||
return
|
cols = []
|
||||||
empty = []
|
for p in self.fact.problems():
|
||||||
dupe = []
|
if not p:
|
||||||
for field in self.fact.fields:
|
cols.append("#fff")
|
||||||
p = QPalette()
|
elif p == "unique":
|
||||||
p.setColor(QPalette.Text, QColor("#000000"))
|
cols.append("#fcc")
|
||||||
if not self.fieldValid(field):
|
|
||||||
empty.append(field)
|
|
||||||
p.setColor(QPalette.Base, QColor("#ffffcc"))
|
|
||||||
self.fields[field.name][1].setPalette(p)
|
|
||||||
elif not self.fieldUnique(field):
|
|
||||||
dupe.append(field)
|
|
||||||
p.setColor(QPalette.Base, QColor("#ffcccc"))
|
|
||||||
self.fields[field.name][1].setPalette(p)
|
|
||||||
else:
|
else:
|
||||||
p.setColor(QPalette.Base, QColor("#ffffff"))
|
cols.append("#ffc")
|
||||||
self.fields[field.name][1].setPalette(p)
|
self.web.eval("setBackgrounds(%s);" % simplejson.dumps(cols))
|
||||||
|
|
||||||
# HTML editing
|
# HTML editing
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
@ -480,15 +473,9 @@ class Editor(object):
|
||||||
self.web.eval("setFormat('underline');")
|
self.web.eval("setFormat('underline');")
|
||||||
|
|
||||||
def toggleSuper(self, bool):
|
def toggleSuper(self, bool):
|
||||||
if self._buttons['text_sub'].isChecked():
|
|
||||||
self._buttons['text_sub'].setChecked(False)
|
|
||||||
self.toggleSub(None)
|
|
||||||
self.web.eval("setFormat('superscript');")
|
self.web.eval("setFormat('superscript');")
|
||||||
|
|
||||||
def toggleSub(self, bool):
|
def toggleSub(self, bool):
|
||||||
# if self._buttons['text_super'].isChecked():
|
|
||||||
# self._buttons['text_super'].setChecked(False)
|
|
||||||
# self.toggleSuper(None)
|
|
||||||
self.web.eval("setFormat('subscript');")
|
self.web.eval("setFormat('subscript');")
|
||||||
|
|
||||||
def removeFormat(self):
|
def removeFormat(self):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue