示例#1
0
    def gotoLocalFile(self, rootHtmlPath):
        rootHtmlPath = getResourcePath(rootHtmlPath)

        # Code from AnkiWebView::_setHtml
        app = QApplication.instance()

        # work around webengine stealing focus on setHtml()
        oldFocus = app.focusWidget()
        self.web._page.setUrl(QUrl.fromLocalFile(rootHtmlPath))
        if oldFocus:
            oldFocus.setFocus()
示例#2
0
    def showAnswerGrid(self, card, rev, baseURL):
        dialog = self
        view = dialog.ui.gridView

        # set commands on the custom page to recognise special link handler
        view.page().setSpecialLinks(
            [GridDlg._closepopupCommand, GridDlg._replayaudioCommand])

        cardFrontHtml = renderOneQA(rev, card, "question")

        klass = "card card{}".format(card.ord + 1)
        width = int(0.96 * self.size().width())
        height = int(0.96 * self.size().height())
        buffer = 60  # e.g. for the window's title bar, and a bottom 'margin'
        html = gridHtml('', klass, width, height - buffer)

        htmlFinal = html.replace("##fgCardFront##", cardFrontHtml)

        gridw = GridDlg._gridSize
        size = gridw**2  # still assuming a square grid (for now anyway)

        dummy = '\nnot found\n'
        cards = [dummy for i in range(size)]  # i.e. 4 or 9 dummies
        cards[dialog.correct - 1] = renderOneQA(
            rev, card, "answer")  # put in the real answer

        deckName = mw.col.decks.current()['name']
        search = '-is:suspended deck:"{}"'.format(deckName)
        cardsFound = mw.col.findCards(
            search
        )  # e.g.  '-is:suspended deck:indonesian-lift-dictionary-Orig'
        cardsFound = cardsFound or [
        ]  # using object or evaluation to ensure list
        random.shuffle(cardsFound)

        i = 0
        for c in cardsFound:  # at most; but usually we'll quit after gridw*gridh
            if c is None:  # If c is empty, do no more
                continue
            id = i + 1  # these are offset by one since grid's id-numbering is 1-based but array is 0-based
            if id > size:
                break
            if c == card.id or id == dialog.correct:  # don't use current card nor steal its cell
                i += 1
                continue
            else:
                cellCard = mw.col.getCard(c)
                if cellCard is None or (cellCard.template() !=
                                        card.template()):
                    # do NOT increment i
                    continue  # something went wrong finding that card (throw exception?)
            thecard = renderOneQA(rev, cellCard, "answer")
            if thecard is None:
                continue  # Did not get a valid card, so get the next one
            cards[i] = thecard

            i += 1
        counter = 0  # We use a counter so we can handle missing cells
        for i in range(size):
            drop = True
            try:
                drop = cards[i] == '\nnot found\n'
            except IndexError:
                continue  # Do nothing
            if drop:
                cards[i] = ""  # empty cell?
            else:
                counter += 1  # increment
                id = i + 1
                cards[i] = '<td class="{}">{}</td>'.format(
                    klass, gridHtmlCell(id, cards[i]))
            if counter % gridw == 0 and id < size:  # use modulus to identify/create end of row
                cards[i] += gridHtmlBetweenRows()

        toInsert = '\n'.join(cards)

        toInsert = '''<table class="{}"><tbody><tr>
        {}
        </tr></tbody></table>'''.format(klass, toInsert)

        htmlFinal = htmlFinal.replace("##fgCardGridArea##", toInsert)
        # enable for debug saving:
        #  htmlRender(htmlFinal)
        view.page().setHtml(htmlFinal, QUrl.fromLocalFile(baseURL))