From e6db07b74a3cb7056337c64f8fadd54478036307 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 23 Nov 2008 07:49:43 +0900 Subject: [PATCH] missing files --- ankiqt/ui/facteditor.py | 72 +++++++++++++++++++++++++++++++---------- ankiqt/ui/main.py | 3 +- ankiqt/ui/utils.py | 19 ++++++++++- ankiqt/ui/view.py | 20 ++---------- icons.qrc | 2 ++ 5 files changed, 80 insertions(+), 36 deletions(-) diff --git a/ankiqt/ui/facteditor.py b/ankiqt/ui/facteditor.py index d28473416..84d23846a 100644 --- a/ankiqt/ui/facteditor.py +++ b/ankiqt/ui/facteditor.py @@ -8,6 +8,8 @@ import re, os, sys from anki.utils import parseTags, stripHTML, tidyHTML import anki.sound from ankiqt import ui +import ankiqt +from ankiqt.ui.utils import mungeQA, saveGeom, restoreGeom class FactEditor(object): """An editor for new/existing facts. @@ -120,26 +122,37 @@ class FactEditor(object): self.foreground.setLayout(hbox) self.iconsBox.addWidget(self.foreground) self.foreground.setStyle(self.plastiqueStyle) + # preview + self.preview = QPushButton(self.widget) + self.preview.connect(self.preview, SIGNAL("clicked()"), + self.onPreview) + self.preview.setToolTip(_("Preview (F2)")) + self.preview.setShortcut(_("F2")) + self.preview.setIcon(QIcon(":/icons/document-preview.png")) + self.preview.setFocusPolicy(Qt.NoFocus) + self.preview.setEnabled(False) + self.iconsBox.addWidget(self.preview) + self.preview.setStyle(self.plastiqueStyle) # pictures spc = QSpacerItem(10,10) self.iconsBox.addItem(spc) self.addPicture = QPushButton(self.widget) self.addPicture.connect(self.addPicture, SIGNAL("clicked()"), self.onAddPicture) self.addPicture.setFocusPolicy(Qt.NoFocus) - self.addPicture.setShortcut(_("F2")) + self.addPicture.setShortcut(_("F3")) self.addPicture.setIcon(QIcon(":/icons/colors.png")) self.addPicture.setEnabled(False) - self.addPicture.setToolTip(_("Add a picture (F2)")) + self.addPicture.setToolTip(_("Add a picture (F3)")) self.iconsBox.addWidget(self.addPicture) self.addPicture.setStyle(self.plastiqueStyle) # sounds self.addSound = QPushButton(self.widget) self.addSound.connect(self.addSound, SIGNAL("clicked()"), self.onAddSound) self.addSound.setFocusPolicy(Qt.NoFocus) - self.addSound.setShortcut(_("F3")) + self.addSound.setShortcut(_("F4")) self.addSound.setEnabled(False) self.addSound.setIcon(QIcon(":/icons/text-speak.png")) - self.addSound.setToolTip(_("Add audio (F3)")) + self.addSound.setToolTip(_("Add audio (F4)")) self.iconsBox.addWidget(self.addSound) self.addSound.setStyle(self.plastiqueStyle) # latex @@ -175,17 +188,6 @@ class FactEditor(object): self.latexMathEnv.setEnabled(False) self.iconsBox.addWidget(self.latexMathEnv) self.latexMathEnv.setStyle(self.plastiqueStyle) - # preview - self.preview = QPushButton(self.widget) - self.preview.connect(self.preview, SIGNAL("clicked()"), - self.onPreview) - self.preview.setToolTip(_("Preview (F5)")) - self.preview.setShortcut(_("F5")) - #self.preview.setIcon(QIcon(":/icons/math_matrix.png")) - self.preview.setFocusPolicy(Qt.NoFocus) - self.preview.setEnabled(False) - self.iconsBox.addWidget(self.preview) - self.preview.setStyle(self.plastiqueStyle) self.fieldsFrame = None self.widget.setLayout(self.fieldsBox) @@ -454,8 +456,7 @@ class FactEditor(object): w.moveCursor(QTextCursor.PreviousCharacter) def onPreview(self): - print self.deck.previewFact(self.fact) - print "preview" + PreviewDialog(self.parent, self.deck, self.fact).exec_() def fieldsAreBlank(self): for (field, widget) in self.fields.values(): @@ -568,3 +569,40 @@ class FactEdit(QTextEdit): QTextEdit.focusInEvent(self, evt) self.parent.formatChanged(None) self.parent.enableButtons() + +class PreviewDialog(QDialog): + + def __init__(self, parent, deck, fact, *args): + QDialog.__init__(self, parent, *args) + self.deck = deck + self.fact = fact + cards = self.deck.previewFact(self.fact) + if not cards: + ui.utils.showInfo(_("No cards to preview."), + parent=self.parent) + return + self.cards = cards + self.currentCard = 0 + self.dialog = ankiqt.forms.previewcards.Ui_Dialog() + self.dialog.setupUi(self) + self.dialog.comboBox.addItems(QStringList( + [c.cardModel.name for c in self.cards])) + self.connect(self.dialog.comboBox, SIGNAL("activated(int)"), + self.onChange) + self.updateCard() + restoreGeom(self, "preview") + + def updateCard(self): + c = self.cards[self.currentCard] + self.dialog.webView.setHtml( + "" + + mungeQA(self.deck, c.htmlQuestion()) + + mungeQA(self.deck, c.htmlAnswer())) + + def onChange(self, idx): + self.currentCard = idx + self.updateCard() + + def reject(self): + saveGeom(self, "preview") + QDialog.reject(self) diff --git a/ankiqt/ui/main.py b/ankiqt/ui/main.py index 78b9bcf3a..5d05115c5 100644 --- a/ankiqt/ui/main.py +++ b/ankiqt/ui/main.py @@ -52,6 +52,7 @@ class AnkiQt(QMainWindow): self.restoreGeometry(self.config['mainWindowGeom']) self.bodyView = ui.view.View(self, self.mainWin.mainText, self.mainWin.mainTextFrame) + self.mainWin.mainText.pageAction(QWebPage.Reload).setVisible(False) self.addView(self.bodyView) self.statusView = ui.status.StatusView(self) self.addView(self.statusView) @@ -812,7 +813,7 @@ To upgrade an old deck, download Anki 0.9.8.7.""")) def showSaveEditorButton(self): if self.lastState == self.state: return - self.editFactButton = QPushButton(_("Return")) + self.editFactButton = QPushButton(_("Close")) self.editFactButton.setToolTip("Hit Esc to return to review.") self.editFactButton.setFixedHeight(self.easeButtonHeight) self.editFactButton.setShortcut(_("Esc")) diff --git a/ankiqt/ui/utils.py b/ankiqt/ui/utils.py index ab6859ba4..9c8f8e808 100644 --- a/ankiqt/ui/utils.py +++ b/ankiqt/ui/utils.py @@ -4,7 +4,10 @@ from PyQt4.QtGui import * from PyQt4.QtCore import * -import re, os +from anki.sound import playFromText, stripSounds +from anki.latex import renderLatex, stripLatex + +import re, os, sys import ankiqt def openLink(link): @@ -88,3 +91,17 @@ def restoreGeom(widget, key): key += "Geom" if ankiqt.mw.config.get(key): widget.restoreGeometry(ankiqt.mw.config[key]) + +def mungeQA(deck, txt): + txt = renderLatex(deck, txt) + txt = stripSounds(txt) + def quote(match): + if sys.platform.startswith("win32"): + prefix = "file:///" + else: + prefix = "file://" + return 'img src="%s%s"' % ( + prefix, os.path.join(deck.mediaDir(), + unicode(match.group(1)))) + txt = re.sub('img src="(.*?)"', quote, txt) + return txt diff --git a/ankiqt/ui/view.py b/ankiqt/ui/view.py index a58122129..703b0a1e3 100644 --- a/ankiqt/ui/view.py +++ b/ankiqt/ui/view.py @@ -10,6 +10,7 @@ from anki.latex import renderLatex, stripLatex from anki.utils import stripHTML import types, time, re, os, urllib, sys from ankiqt import ui +from ankiqt.ui.utils import mungeQA # Views - define the way a user is prompted for questions, etc ########################################################################## @@ -68,7 +69,6 @@ class View(object): def addStyles(self): # card styles s = "