def execute(self, action): mw.checkpoint(action) for cmd in self: try: self._parse(cmd, course=self.course, datasource=self.datasource) except Exception as x: msg = 'Undoing {0} due to error "{1}"'.format(action, x) if hasattr(x, 'cmd'): msg = msg + '\n\nWas processing command:\n{0}'.format( x.cmd) msg = msg + ''' ------------------------------------------------------------------ Please include the information below if reporting an error ------------------------------------------------------------------ {0}'''.format(traceback.format_exc()) showWarning(msg, title=self.course.name) mw.onUndo() return False if self._warnings: showWarning('\n'.join(self._warnings)) # refresh main view so changes are visible mw.reset() return True
def linkHandler_wrap(reviewer, url): if url == "card_info": Card_Info._cs.toggle() elif url == "skip": reviewer.nextCard() elif url == "undo": mw.onUndo() else: Review_linkHandelr_Original(reviewer, url)
def shortcutKeys(self, _old): return [ ("j", lambda: self._answerCard(1)), # fail ("h", lambda: self._answerCard(2)), # hard ("k", lambda: self._answerCard(3)), # normal ("l", lambda: self._answerCard(4)), # easy ("z", lambda: mw.onUndo()), # undo (a bit hacky but works for me) ] + _old(self)
def handle_wheel_event(event): if mouse_action.isChecked(): step = event.angleDelta().y() if step > 0: if mw.state == "review": if mw.reviewer.state == 'answer' or mw.reviewer.state == 'question': if mw.form.actionUndo.isEnabled(): mw.onUndo() else: if mw.state == "review": if mw.reviewer.state == 'question': mw.reviewer._showAnswer() # ._showAnswerHack() elif mw.reviewer.state == 'answer': mw.reviewer._onAnswerButton("2") # good else: original_mw_web_wheelEvent(event)
def shortcutKeys(self, _old): return [ ("j", lambda: self._answerCard(1)), # fail ("h", lambda: self._answerCard(2)), # hard # to guarantee that 'k' is always normal, it needs to be the button # to the left of the rightmost one (i.e. 4-1 for review, 3-1 for # new/learning cards). ("k", lambda: self._answerCard( self.mw.col.sched.answerButtons(self.card) - 1)), # normal ("l", lambda: self._answerCard(4)), # easy ("z", lambda: mw.onUndo()), # undo (a bit hacky but works for me) ] + _old(self)
def bridgeReroute(self, cmd): if checkProfile(): if cmd.startswith('textToJReading'): splitList = cmd.split(':||:||:') if self.note.id == int(splitList[3]): field = getFieldName(splitList[2], self.note) mw.Exporter.finalizeGroupExport(self, splitList[1], field, self.note) return elif cmd.startswith('individualJExport'): splitList = cmd.split(':||:||:') if self.note.id == int(splitList[3]): field = getFieldName(splitList[2], self.note) mw.Exporter.finalizeIndividualExport(self, splitList[1], field, self.note) return elif cmd.startswith('attemptUndo'): name = mw.col.undoName() if name == 'Sentence Accent Generation' or name == 'Word Accent Generation' or name == 'Accent Removal': mw.onUndo() return ogReroute(self, cmd)
def keyHandler(self, evt, _old): key = unicode(evt.text()) if key == "z": try: # throws an error on undo -> do -> undo pattern, otherwise works fine mw.onUndo() except: pass elif key in [ "q", "e", ]: # allow answering with a and d keys, to keep fingers on WASD cnt = mw.col.sched.answerButtons(mw.reviewer.card) # Get button count isq = self.state == "question" if isq: return self._showAnswer() if key == "q": return self._answerCard(1) elif key == "e": return self._answerCard(cnt) else: return _old(self, evt) else: return _old(self, evt)
def keyHandler(self, evt, _old): key = unicode(evt.text()) if key == "z": try:# throws an error on undo -> do -> undo pattern, otherwise works fine mw.onUndo() except: pass elif key in ["j", "k", "l", ";",]: isq = self.state == "question" if isq: self._showAnswerHack() if key == "j": self._answerCard(1) elif key == "k": self._answerCard(2) elif key == "l": self._answerCard(3) elif key == ";": self._answerCard(4) else: return _old(self, evt) else: return _old(self, evt)
def leftHandKeys(self, evt): key = unicode(evt.text()) if key == "u": try:# throws an error on undo -> do -> undo pattern, otherwise works fine mw.onUndo() except: pass if key == "i": try: mw.onEditCurrent() except: pass if key == "h": if self.state == "question": self._showAnswerHack() elif self.state == "answer": self._answerCard(1) elif key == "j": if self.state == "question": self._showAnswerHack() elif self.state == "answer": self._answerCard(2) elif key == "k": if self.state == "question": self._showAnswerHack() elif self.state == "answer": self._answerCard(3) elif key == "l": if self.state == "question": self._showAnswerHack() elif self.state == "answer": self._answerCard(4) else: origKeyHandler(self, evt)
def _shortcutKeys_wrap(self, _old): original = _old(self) original.extend([(info_shortcut, lambda: Card_Info._cs.toggle()), (skip_shortcut, lambda: self.nextCard()), (undo_shortcut, lambda: mw.onUndo())]) return original
def myUndo(): try: mw.onUndo() except TypeError: # no more steps back pass
def handleMessage(msg): global caffeinated, caffprocess, cafftimer print('Recd: ' + msg) if msg == "connected" or msg == "disconnected": mw.toolbar.draw() elif msg == 'Hi': removeQRandBlur() mw.toolbar.draw() else: if 'darwin' in sys.platform: if not caffeinated: caffprocess = subprocess.Popen("caffeinate -d", shell=True) caffeinated = True cafftimer = threading.Timer(120, killCaffProcess) cafftimer.start() else: cafftimer.cancel() cafftimer = threading.Timer(120, killCaffProcess) cafftimer.start() elif 'win32' in sys.platform: ctypes.windll.kernel32.SetThreadExecutionState(0x00000002) if msg.startswith('setprefs'): split = msg.split('-') filepath = USERFILES + '/' + split[1] with open(filepath, 'w') as writer: writer.write(split[2]) elif msg.startswith('setdeck'): split = msg.split('~#$#~') mw.col.decks.select(mw.col.decks.id(split[1])) mw.moveToState("review") elif msg.startswith('hook'): for i in range(1, 4): if msg.endswith(str(i)): hookname = 'Ankimote.hook' + str(i) print('runHook(' + hookname + ')') runHook(hookname) elif msg.startswith('cmd'): config = mw.addonManager.getConfig(__name__) for i in range(1, 6): if msg.endswith(str(i)): cmdval = config[('cmd' + str(i))] print('running CMD' + str(i) + ': ' + cmdval) exec(cmdval) elif mw.state == 'review' and msg != 'none': if msg == 'good' and mw.reviewer.state == 'question': mw.reviewer._showAnswer() elif msg == 'undo': mw.onUndo() elif msg == 'scrollup': mw.deckBrowser.web.evalWithCallback('window.pageYOffset', scrollUp) elif msg == 'scrolldown': mw.deckBrowser.web.evalWithCallback('window.pageYOffset', scrollDown) elif msg == 'pageup': mw.deckBrowser.web.evalWithCallback('window.pageYOffset', pageUp) elif msg == 'pagedown': mw.deckBrowser.web.evalWithCallback('window.pageYOffset', pageDown) elif msg == 'ambossnext': mw.web.eval("ambossTooltips.rotateTooltips();") elif msg == 'ambossprev': mw.web.eval("ambossTooltips.rotateTooltips(true);") elif msg == 'ambossclose': mw.web.eval("ambossTooltips.hideAll();") elif msg == 'showhints': showHintsJS = ''' var x=document.getElementsByClassName('hint'); for(i=0;i<x.length;i++) { if(x[i].tagName=='A') { x[i].onclick(); } }; if(!(document.getElementById("io-revl-btn")==null)) { document.getElementById("io-revl-btn").onclick() }; ''' mw.web.eval(showHintsJS) elif mw.reviewer.card and mw.reviewer.state == 'answer': for ease, label in mw.reviewer._answerButtonList(): if msg == label.lower(): mw.reviewer._answerCard(ease) colordict = { "again": "#ec1d24", "hard": "#ecb51d", "good": "#5BAE7E", "easy": "#4BA2CB" } feedbackJSp1 = """ if(document.getElementById('snackbar')) { var snackbar = document.getElementById('snackbar'); snackbar.parentNode.removeChild(snackbar) } var sheet = window.document.styleSheets[0]; sheet.insertRule('#snackbar { visibility: hidden; min-width: 250px; margin-left: -125px; background-color: """ feedbackJSp2 = """; color: #fff; text-align: center; border-radius: 48px; padding: 8px; position: fixed; z-index: 1; left: 50%; bottom: 30px; font-size: 24px; font-weight: bold; }', sheet.cssRules.length); sheet.insertRule('#snackbar.show { visibility: visible; -webkit-animation: fadein 0.05s, fadeout 0.25s 0.75s; animation: fadein 0.05s, fadeout 0.25s 0.75s;}', sheet.cssRules.length); sheet.insertRule('@-webkit-keyframes fadein { from {bottom: 0; opacity: 0;} to {bottom: 30px; opacity: 1;}}', sheet.cssRules.length); sheet.insertRule('@keyframes fadein { from {bottom: 0; opacity: 0;} to {bottom: 30px; opacity: 1;}}', sheet.cssRules.length); sheet.insertRule('@-webkit-keyframes fadeout { from {bottom: 30px; opacity: 1;} to {bottom: 0; opacity: 0;}}', sheet.cssRules.length); sheet.insertRule('@keyframes fadeout { from {bottom: 30px; opacity: 1;} to {bottom: 0; opacity: 0;}}', sheet.cssRules.length); var mytoast = document.createElement("div"); mytoast.innerHTML = '""" feedbackJSp3 = """' mytoast.id="snackbar" document.body.appendChild(mytoast) var toastelement = document.getElementById("snackbar"); toastelement.className = "show"; setTimeout(function(){ toastelement.className = toastelement.className.replace("show", ""); for(i = 0;i<6;i++) { sheet.deleteRule(sheet.cssRules.length-1) } toastelement.parentNode.removeChild(toastelement); }, 1000); """ feedbackJS = feedbackJSp1 + colordict[ msg] + feedbackJSp2 + label + feedbackJSp3 config = mw.addonManager.getConfig(__name__) if config['feedback'] == True: mw.web.eval(feedbackJS)