add cards dialog & model chooser

This commit is contained in:
Damien Elmes 2011-12-10 21:11:19 +09:00
parent f8d39ca210
commit 60ba70b1b5
10 changed files with 127 additions and 224 deletions

View file

@ -39,7 +39,8 @@ class AddCards(QDialog):
self.setupNewNote()
def setupEditor(self):
self.editor = aqt.editor.Editor(self.mw, self.form.fieldsArea, True)
self.editor = aqt.editor.Editor(
self.mw, self.form.fieldsArea, self, True)
def setupChooser(self):
self.modelChooser = aqt.modelchooser.ModelChooser(
@ -69,7 +70,7 @@ class AddCards(QDialog):
self.connect(self.helpButton, SIGNAL("clicked()"), self.helpRequested)
# history
b = bb.addButton(
_("History")+ u'', ar)
_("History")+ u"", ar)
self.connect(b, SIGNAL("clicked()"), self.onHistory)
b.setEnabled(False)
self.historyButton = b
@ -126,9 +127,9 @@ class AddCards(QDialog):
browser.onSearch()
def addNote(self, note):
if any(note.problems()):
if note.dupeOrEmpty():
showWarning(_(
"Some fields are missing or not unique."),
"The first field is empty or not unique."),
help="AddItems#AddError")
return
cards = self.mw.col.addNote(note)

View file

@ -275,7 +275,7 @@ class Editor(object):
native=True, canDisable=False)
b("layout", self.onCardLayout, "Ctrl+l",
shortcut(_("Customize Card Layout (Ctrl+l)")),
size=False, text=_("Layout..."), native=True, canDisable=False)
size=False, text=_("Cards..."), native=True, canDisable=False)
# align to right
self.iconsBox.addItem(QSpacerItem(20,1, QSizePolicy.Expanding))
b("text_bold", self.toggleBold, "Ctrl+b", _("Bold text (Ctrl+b)"),
@ -298,7 +298,7 @@ class Editor(object):
# fixme: better image names
b("text-speak", self.onAddMedia, "F3", _("Add pictures/audio/video (F3)"))
b("media-record", self.onRecSound, "F5", _("Record audio (F5)"))
b("adv", self.onAdvanced, text=u"")
b("adv", self.onAdvanced, text=u"")
s = QShortcut(QKeySequence("Ctrl+t, t"), self.widget)
s.connect(s, SIGNAL("activated()"), self.insertLatex)
s = QShortcut(QKeySequence("Ctrl+t, e"), self.widget)
@ -442,7 +442,10 @@ class Editor(object):
contents = self.note.fields[0]
browser = aqt.dialogs.open("Browser", self.mw)
browser.form.searchEdit.setText(
"'model:%s' '%s'" % (self.note.model()['name'], contents))
"'model:%s' '%s:%s'" % (
self.note.model()['name'],
self.note.model()['flds'][0]['name'],
contents))
browser.onSearch()
def fieldsAreBlank(self):

View file

@ -18,7 +18,6 @@ class FieldDialog(QDialog):
self.mm = self.mw.col.models
self.model = note.model()
self.mw.checkpoint(_("Fields"))
self.setWindowModality(Qt.WindowModal)
self.form = aqt.forms.fields.Ui_Dialog()
self.form.setupUi(self)
self.form.buttonBox.button(QDialogButtonBox.Help).setAutoDefault(False)

View file

@ -671,28 +671,12 @@ Debug info:\n%s""") % traceback.format_exc(), help="DeckErrors")
self.col.reset()
self.moveToState("overview")
def onGroups(self, parent=None):
from aqt.groups import Groups
g = Groups(self, parent)
def onCardStats(self):
self.cardStats.show()
def onStats(self):
aqt.stats.DeckStats(self)
def onCardLayout(self):
from aqt.clayout import CardLayout
CardLayout(self, self.reviewer.card.note(), ord=self.reviewer.card.ord)
def onDeckOpts(self):
import aqt.deckopts
aqt.deckopts.DeckOptions(self)
def onModels(self):
import aqt.models
aqt.models.Models(self)
def onPrefs(self):
import aqt.preferences
aqt.preferences.Preferences(self)

View file

@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
# Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
from aqt.qt import *
from operator import itemgetter
from anki import stdmodels
from anki.lang import ngettext
from anki.hooks import addHook, remHook, runHook
from aqt.utils import isMac
@ -11,24 +11,22 @@ import aqt
class ModelChooser(QHBoxLayout):
def __init__(self, mw, widget, cards=True, label=True):
def __init__(self, mw, widget, label=True):
QHBoxLayout.__init__(self)
self.widget = widget
self.mw = mw
self.deck = mw.col
self.handleCards = cards
self.label = label
self._ignoreReset = False
self.setMargin(0)
self.setSpacing(4)
self.setSpacing(8)
self.setupModels()
self.setupTemplates()
addHook('reset', self.onReset)
self.widget.setLayout(self)
def setupModels(self):
if self.label:
self.modelLabel = QLabel(_("<b>Model</b>:"))
self.modelLabel = QLabel(_("Note Type:"))
self.addWidget(self.modelLabel)
# models dropdown
self.models = QComboBox()
@ -39,9 +37,12 @@ class ModelChooser(QHBoxLayout):
self.connect(self.models, SIGNAL("activated(int)"), self.onModelChange)
# edit button
self.edit = QPushButton()
if not isMac:
if isMac:
self.edit.setFixedWidth(24)
self.edit.setFixedHeight(21)
else:
self.edit.setFixedWidth(32)
self.edit.setIcon(QIcon(":/icons/configure.png"))
self.edit.setIcon(QIcon(":/icons/gears.png"))
self.edit.setShortcut(_("Shift+Alt+e"))
self.edit.setToolTip(_("Customize Models"))
self.edit.setAutoDefault(False)
@ -54,22 +55,12 @@ class ModelChooser(QHBoxLayout):
self.models.setSizePolicy(sizePolicy)
self.updateModels()
def setupTemplates(self):
self.cardShortcuts = []
if self.handleCards:
self.cards = QPushButton()
self.cards.setAutoDefault(False)
self.connect(self.cards, SIGNAL("clicked()"), self.onCardChange)
self.addWidget(self.cards)
self.updateTemplates()
def cleanup(self):
remHook('reset', self.onReset)
def onReset(self):
if not self._ignoreReset:
self.updateModels()
self.updateTemplates()
def show(self):
self.widget.show()
@ -83,8 +74,7 @@ class ModelChooser(QHBoxLayout):
def onModelChange(self, idx):
model = self._models[idx]
self.deck.conf['currentModelId'] = model.id
self.updateTemplates()
self.deck.conf['curModel'] = model.id
self._ignoreReset = True
runHook("currentModelChanged")
self._ignoreReset = False
@ -95,105 +85,6 @@ class ModelChooser(QHBoxLayout):
key=itemgetter("name"))
self.models.addItems([m['name'] for m in self._models])
for c, m in enumerate(self._models):
if m['id'] == str(self.deck.conf['currentModelId']):
if m['id'] == str(self.deck.conf['curModel']):
self.models.setCurrentIndex(c)
break
def updateTemplates(self):
if not self.handleCards:
return
# remove any shortcuts
for s in self.cardShortcuts:
s.setEnabled(False)
self.cardShortcuts = []
m = self.deck.models.current()
ts = m['tmpls']
active = [t['name'] for t in ts if t['actv']]
txt = ngettext("%d card", "%d cards", len(active)) % len(active)
self.cards.setText(txt)
n = 1
for t in ts:
s = QShortcut(QKeySequence("Ctrl+%d" % n), self.widget)
self.widget.connect(s, SIGNAL("activated()"),
lambda t=t: self.toggleTemplate(t))
self.cardShortcuts.append(s)
n += 1
def onCardChange(self):
m = QMenu(self.widget)
model = self.deck.models.current()
for template in model.templates:
action = QAction(self.widget)
action.setCheckable(True)
if template['actv']:
action.setChecked(True)
action.setText(template['name'])
self.connect(action, SIGNAL("triggered()"),
lambda t=template: \
self.toggleTemplate(t))
m.addAction(action)
m.exec_(self.cards.mapToGlobal(QPoint(0,0)))
def canDisableTemplate(self):
return len([t for t in self.deck.models.current()['tmpls']
if t['actv']]) > 1
def toggleTemplate(self, card):
if not card['actv']:
card['actv'] = True
elif self.canDisableTemplate():
card['actv'] = False
self.deck.models.current().flush()
self.updateTemplates()
class AddModel(QDialog):
def __init__(self, mw, parent=None):
self.parent = parent or mw
self.mw = mw
self.deck = mw.col
QDialog.__init__(self, self.parent, Qt.Window)
self.model = None
self.dialog = aqt.forms.addmodel.Ui_Dialog()
self.dialog.setupUi(self)
# standard models
self.models = []
for (name, func) in stdmodels.models:
item = QListWidgetItem(_("Add: %s") % name)
self.dialog.models.addItem(item)
self.models.append((True, func))
# add copies
mids = self.deck.db.list("select id from models order by name")
for m in [self.deck.getModel(mid, False) for mid in mids]:
m.id = None
item = QListWidgetItem(_("Copy: %s") % m['name'])
self.dialog.models.addItem(item)
m['name'] = _("%s copy") % m['name']
self.models.append((False, m))
self.dialog.models.setCurrentRow(0)
# the list widget will swallow the enter key
s = QShortcut(QKeySequence("Return"), self)
self.connect(s, SIGNAL("activated()"), self.accept)
# help
self.connect(self.dialog.buttonBox, SIGNAL("helpRequested()"), self.onHelp)
def get(self):
self.exec_()
return self.model
def reject(self):
self.accept()
def accept(self):
(isStd, model) = self.models[self.dialog.models.currentRow()]
if isStd:
# create
self.model = model(self.deck)
else:
# add copy to deck
self.mw.col.addModel(model)
self.model = model
QDialog.accept(self)
def onHelp(self):
openHelp("AddModel")

View file

@ -3,8 +3,9 @@
from aqt.qt import *
from operator import itemgetter
from aqt.utils import showInfo, askUser, getText, maybeHideClose
from aqt.utils import showInfo, askUser, getText, maybeHideClose, openHelp
import aqt.modelchooser, aqt.clayout
from anki import stdmodels
class Models(QDialog):
def __init__(self, mw, parent=None):
@ -35,7 +36,7 @@ class Models(QDialog):
c(b, s, self.onRename)
b = box.addButton(_("Delete"), t)
c(b, s, self.onDelete)
b = box.addButton(_("Advanced..."), t)
b = box.addButton(_("Options..."), t)
c(b, s, self.onAdvanced)
c(f.modelsList, SIGNAL("currentRowChanged(int)"), self.modelChanged)
c(f.modelsList, SIGNAL("itemDoubleClicked(QListWidgetItem*)"),
@ -45,9 +46,10 @@ class Models(QDialog):
maybeHideClose(box)
def onRename(self):
txt = getText(_("New name?"), default=self.model.name)
txt = getText(_("New name:"), default=self.model.name)
if txt[0]:
self.model.name = txt[0]
self.model['name'] = txt[0]
self.mm.save(self.model)
self.updateModelsList()
def updateModelsList(self):
@ -59,7 +61,7 @@ class Models(QDialog):
self.form.modelsList.clear()
for m in self.models:
item = QListWidgetItem(_("%(name)s [%(notes)d notes]") % dict(
name=m.name, notes=m.useCount()))
name=m['name'], notes=self.mm.useCount(m)))
self.form.modelsList.addItem(item)
self.form.modelsList.setCurrentRow(row)
@ -70,31 +72,10 @@ class Models(QDialog):
self.model = self.models[idx]
def onAdd(self):
m = aqt.modelchooser.AddModel(self.mw, self).get()
m = AddModel(self.mw, self).get()
if m:
self.col.addModel(m)
self.updateModelsList()
def onLayout(self):
# set to current
# # see if there's an available note
dummy = False
id = self.col.db.scalar(
"select id from notes where mid = ?", self.model.id)
if id:
note = self.col.getNote(id)
else:
# generate a dummy one
self.col.conf['currentModelId'] = self.model.id
note = self.col.newNote()
for f in note.keys():
note[f] = f
self.col.addNote(note)
dummy = True
aqt.clayout.CardLayout(self.mw, note, type=2, parent=self)
if dummy:
self.col._delNotes([note.id])
def onDelete(self):
if len(self.models) < 2:
showInfo(_("Please add another model first."),
@ -104,7 +85,7 @@ class Models(QDialog):
_("Delete this model and all its cards?"),
parent=self):
return
self.col.delModel(self.model.id)
self.mm.rem(self.model)
self.model = None
self.updateModelsList()
@ -112,14 +93,20 @@ class Models(QDialog):
d = QDialog(self)
frm = aqt.forms.modelopts.Ui_Dialog()
frm.setupUi(d)
frm.latexHeader.setText(self.model.conf['latexPre'])
frm.latexFooter.setText(self.model.conf['latexPost'])
frm.clozeCtx.setChecked(self.model['clozectx'])
frm.latexHeader.setText(self.model['latexPre'])
frm.latexFooter.setText(self.model['latexPost'])
d.setWindowTitle(_("Options for %s") % self.model['name'])
self.connect(
frm.buttonBox, SIGNAL("helpRequested()"),
lambda: openHelp("NoteOptions"))
d.exec_()
self.model.conf['latexPre'] = unicode(frm.latexHeader.toPlainText())
self.model.conf['latexPost'] = unicode(frm.latexFooter.toPlainText())
self.model['clozectx'] = frm.clozeCtx.isChecked()
self.model['latexPre'] = unicode(frm.latexHeader.toPlainText())
self.model['latexPost'] = unicode(frm.latexFooter.toPlainText())
def saveModel(self):
self.model.flush()
self.mm.save(self.model)
# Cleanup
##########################################################################
@ -130,3 +117,53 @@ class Models(QDialog):
self.saveModel()
self.mw.reset()
QDialog.reject(self)
class AddModel(QDialog):
def __init__(self, mw, parent=None):
self.parent = parent or mw
self.mw = mw
self.col = mw.col
QDialog.__init__(self, self.parent, Qt.Window)
self.model = None
self.dialog = aqt.forms.addmodel.Ui_Dialog()
self.dialog.setupUi(self)
# standard models
self.models = []
for (name, func) in stdmodels.models:
item = QListWidgetItem(_("Add: %s") % name)
self.dialog.models.addItem(item)
self.models.append((True, func))
# add copies
for m in self.col.models.all():
item = QListWidgetItem(_("Clone: %s") % m['name'])
self.dialog.models.addItem(item)
self.models.append((False, m))
self.dialog.models.setCurrentRow(0)
# the list widget will swallow the enter key
s = QShortcut(QKeySequence("Return"), self)
self.connect(s, SIGNAL("activated()"), self.accept)
# help
self.connect(self.dialog.buttonBox, SIGNAL("helpRequested()"), self.onHelp)
def get(self):
self.exec_()
return self.model
def reject(self):
self.accept()
def accept(self):
(isStd, model) = self.models[self.dialog.models.currentRow()]
if isStd:
# create
self.model = model(self.col)
else:
# add copy to deck
self.model = self.mw.col.models.copy(model)
self.mw.col.models.setCurrent(self.model)
QDialog.accept(self)
def onHelp(self):
openHelp("AddModel")

View file

@ -358,7 +358,7 @@ def tooltip(msg, period=3000, parent=None):
evt.accept()
self.hide()
closeTooltip()
aw = parent or aqt.mw.app.activeWindow()
aw = parent or aqt.mw.app.activeWindow() or aqt.mw
lab = CustomLabel("""\
<table cellpadding=10>
<tr>

View file

@ -104,6 +104,7 @@
<addaction name="actionCheckMediaDatabase"/>
<addaction name="separator"/>
<addaction name="menuPlugins"/>
<addaction name="separator"/>
<addaction name="actionPreferences"/>
</widget>
<addaction name="menuCol"/>
@ -112,10 +113,6 @@
<addaction name="menuHelp"/>
</widget>
<action name="actionExit">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/application-exit.png</normaloff>:/icons/application-exit.png</iconset>
</property>
<property name="text">
<string>E&amp;xit</string>
</property>
@ -124,12 +121,8 @@
</property>
</action>
<action name="actionPreferences">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/configure.png</normaloff>:/icons/configure.png</iconset>
</property>
<property name="text">
<string>&amp;Preferences</string>
<string>&amp;Preferences...</string>
</property>
<property name="statusTip">
<string>Configure interface language and options</string>
@ -142,10 +135,6 @@
</property>
</action>
<action name="actionAbout">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/anki.png</normaloff>:/icons/anki.png</iconset>
</property>
<property name="text">
<string>&amp;About...</string>
</property>
@ -154,10 +143,6 @@
</property>
</action>
<action name="actionCstats">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/package_games_card.png</normaloff>:/icons/package_games_card.png</iconset>
</property>
<property name="text">
<string>&amp;Card Info</string>
</property>
@ -172,10 +157,6 @@
<property name="enabled">
<bool>false</bool>
</property>
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/edit-undo.png</normaloff>:/icons/edit-undo.png</iconset>
</property>
<property name="text">
<string>&amp;Undo</string>
</property>
@ -184,10 +165,6 @@
</property>
</action>
<action name="actionCheckMediaDatabase">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/text-speak.png</normaloff>:/icons/text-speak.png</iconset>
</property>
<property name="text">
<string>&amp;Unused Media...</string>
</property>
@ -211,10 +188,6 @@
</property>
</action>
<action name="actionDonate">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/emblem-favorite.png</normaloff>:/icons/emblem-favorite.png</iconset>
</property>
<property name="text">
<string>&amp;Support Anki...</string>
</property>
@ -228,19 +201,11 @@
</property>
</action>
<action name="actionFullDatabaseCheck">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/sqlitebrowser.png</normaloff>:/icons/sqlitebrowser.png</iconset>
</property>
<property name="text">
<string>&amp;Check DB...</string>
</property>
</action>
<action name="actionDocumentation">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/help-hint.png</normaloff>:/icons/help-hint.png</iconset>
</property>
<property name="text">
<string>&amp;Guide...</string>
</property>
@ -249,10 +214,6 @@
</property>
</action>
<action name="actionSwitchProfile">
<property name="icon">
<iconset resource="icons.qrc">
<normaloff>:/icons/user-identity.png</normaloff>:/icons/user-identity.png</iconset>
</property>
<property name="text">
<string>&amp;Switch Profile...</string>
</property>

View file

@ -9,12 +9,12 @@
<rect>
<x>0</x>
<y>0</y>
<width>353</width>
<height>363</height>
<width>276</width>
<height>323</height>
</rect>
</property>
<property name="windowTitle">
<string>Model Options</string>
<string/>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
@ -22,6 +22,33 @@
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Basic</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QCheckBox" name="clozeCtx">
<property name="text">
<string>Show context in cloze answers</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>184</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>LaTeX</string>

View file

@ -14,7 +14,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Models</string>
<string>Note Types</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5" stretch="100,0">
<property name="spacing">