From ddfba5a1cad209d3c0ad3ac665feb1a31a9a8bf2 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 15 Nov 2013 19:39:56 -0800 Subject: [PATCH] Look up Kanji at the same time as vocab when scanning text Former-commit-id: ec14972d8e7d3119ad76389299dc2609a8f8c095 --- yomi_base/reader.py | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/yomi_base/reader.py b/yomi_base/reader.py index daede83..02c66e9 100644 --- a/yomi_base/reader.py +++ b/yomi_base/reader.py @@ -132,8 +132,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader): def keyPressEvent(self, event): - visible = self.dockVocab.isVisible() or self.dockKanji.isVisible() - if visible and event.key() == QtCore.Qt.Key_Shift: + if event.key() == QtCore.Qt.Key_Shift: self.updateSampleFromPosition() @@ -412,9 +411,7 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader): def updateSampleMouseEvent(self, event): cursor = self.textContent.cursorForPosition(event.pos()) self.state.scanPosition = cursor.position() - requested = event.buttons() & QtCore.Qt.MidButton or event.modifiers() & QtCore.Qt.ShiftModifier - visible = self.dockVocab.isVisible() or self.dockKanji.isVisible() - if visible and requested: + if event.buttons() & QtCore.Qt.MidButton or event.modifiers() & QtCore.Qt.ShiftModifier: self.updateSampleFromPosition() @@ -425,29 +422,37 @@ class MainWindowReader(QtGui.QMainWindow, gen.reader_ui.Ui_MainWindowReader): cursor = self.textContent.textCursor() content = unicode(self.textContent.toPlainText()) contentSample = content[samplePosStart:samplePosEnd] + contentSampleFlat = contentSample.replace(u'\n', unicode()) - if not contentSample or unicode.isspace(contentSample[0]): + if len(contentSampleFlat) == 0: cursor.clearSelection() self.textContent.setTextCursor(cursor) return - contentSampleFlat = contentSample.replace('\n', unicode()) - self.state.vocabDefs, lengthMatched = self.language.findTerm(contentSampleFlat) + lengthMatched = 0 + if self.dockVocab.isVisible(): + self.state.vocabDefs, lengthMatched = self.language.findTerm(contentSampleFlat) + sentence = reader_util.findSentence(content, samplePosStart) + for definition in self.state.vocabDefs: + definition['sentence'] = sentence + self.updateVocabDefs() - sentence = reader_util.findSentence(content, samplePosStart) - for definition in self.state.vocabDefs: - definition['sentence'] = sentence - - self.updateVocabDefs() + if self.dockKanji.isVisible(): + if lengthMatched == 0: + self.state.kanjiDefs = self.language.findCharacters(contentSampleFlat[0]) + if len(self.state.kanjiDefs) > 0: + lengthMatched = 1 + else: + self.state.kanjiDefs = self.language.findCharacters(contentSampleFlat[:lengthMatched]) + self.updateKanjiDefs() lengthSelect = 0 - if lengthMatched: - for c in contentSample: - lengthSelect += 1 - if c != '\n': - lengthMatched -= 1 - if lengthMatched <= 0: - break + for c in contentSample: + if lengthMatched <= 0: + break + lengthSelect += 1 + if c != u'\n': + lengthMatched -= 1 cursor.setPosition(samplePosStart, QtGui.QTextCursor.MoveAnchor) cursor.setPosition(samplePosStart + lengthSelect, QtGui.QTextCursor.KeepAnchor)