From 6c4a360f973310d9e479b782418fd52162376ad1 Mon Sep 17 00:00:00 2001 From: "Christopher T. Johnson" Date: Fri, 19 Jan 2024 11:17:42 -0500 Subject: [PATCH] Alert on no def, clean up of finding word to define --- lib/read.py | 104 +++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 54 deletions(-) diff --git a/lib/read.py b/lib/read.py index 562f7b0..469e5eb 100644 --- a/lib/read.py +++ b/lib/read.py @@ -39,6 +39,7 @@ from lib.sounds import SoundOff class ReadDialog(QDialog, Ui_Dialog): playSound = pyqtSignal(str) + playAlert = pyqtSignal() block: int paragraphs = True sessionSignal = pyqtSignal() @@ -52,7 +53,6 @@ class ReadDialog(QDialog, Ui_Dialog): self.preferences = Preferences().get() self.sound = SoundOff() styleSheet = QResource(":/display.css").data().decode("utf-8") - print(styleSheet) styleSheet = styleSheet.replace( '{readerFont}',self.preferences['readerFont'] ) @@ -63,6 +63,7 @@ class ReadDialog(QDialog, Ui_Dialog): # # Override UI # + self.returnBtn.setVisible(False) # # End overrides # @@ -85,17 +86,17 @@ class ReadDialog(QDialog, Ui_Dialog): self.scrollBtn.clicked.connect(self.scrollAction) self.nextBtn.clicked.connect(self.nextAction) self.prevBtn.clicked.connect(self.prevAction) - #self.sessionBtn.clicked.connect(self.session.timerAction) self.sessionBtn.clicked.connect(self.timerAction) self.paraEdit.verticalScrollBar().valueChanged.connect(self.scrollSlot) - self.defEdit.selectionChanged.connect(self.recursiveDef) + #self.defEdit.selectionChanged.connect(self.recursiveDef) + self.returnBtn.clicked.connect(self.returnAction) # # Connect signals # self.displayedWord.connect(self.session.addWord) self.newParagraph.connect(self.session.addBlock) self.playSound.connect(self.sound.playSound) - #self.alert.connect(self.sound.alert) + self.playAlert.connect(self.sound.alert) return # @@ -155,7 +156,7 @@ class ReadDialog(QDialog, Ui_Dialog): cursor = self.paraEdit.textCursor() fmt = cursor.charFormat() if not fmt.fontUnderline(): - self.addWord() + self.addWord(self.paraEdit) cursor = self.paraEdit.textCursor() textBlock = self.paraEdit.document().findBlock(cursor.position()) blockNum = textBlock.blockNumber() @@ -269,10 +270,11 @@ class ReadDialog(QDialog, Ui_Dialog): @pyqtSlot() def defineAction(self) -> None: - if self.paragraphs: - self.showDefinition() - else: - self.addWord() + editor = self.paraEdit + if self.stackedWidget.currentIndex() > 0: + editor = self.defEdit + self.addWord(editor) + self.showDefinition() return def defToHtml(self, word: str, definition) -> str: @@ -381,17 +383,7 @@ class ReadDialog(QDialog, Ui_Dialog): cursor.setPosition( end + textBlock.position(), QTextCursor.MoveMode.KeepAnchor ) - word = query.value("word") - definition = json.loads(query.value("definition")) - try: - phonetic = definition["phonetic"] - def_format.setToolTip( - f'{word}:
{phonetic}
' - ) - cursor.mergeCharFormat(def_format) - except: - pass - + cursor.mergeCharFormat(def_format) return # @@ -455,11 +447,11 @@ class ReadDialog(QDialog, Ui_Dialog): self.savePosition() return - def addWord(self) -> None: + def addWord(self,editor) -> None: # # Find the word # - cursor = self.paraEdit.textCursor() + cursor = editor.textCursor() word = cursor.selectedText() start = cursor.selectionStart() end = cursor.selectionEnd() @@ -478,7 +470,7 @@ class ReadDialog(QDialog, Ui_Dialog): # # Find the block # - textBlock = self.paraEdit.document().findBlock(cursor.position()) + textBlock = editor.document().findBlock(cursor.position()) blockNum = textBlock.blockNumber() start = start - textBlock.position() end = end - textBlock.position() @@ -499,6 +491,7 @@ class ReadDialog(QDialog, Ui_Dialog): ) if response.status_code != 200: print(f"{word}: {response.content}") + self.playAlert.emit() return definitions = json.loads(response.content.decode("utf-8")) definition = definitions[0] @@ -558,33 +551,31 @@ class ReadDialog(QDialog, Ui_Dialog): return # XXX - rename - def display_definition(self) -> None: - cursor = self.paraEdit.textCursor() + def display_definition(self, idx: int) -> bool: + if idx == 0: + editor = self.paraEdit + else: + editor = self.defEdit + cursor = editor.textCursor() + cursor.select(QTextCursor.SelectionType.WordUnderCursor) + word = cursor.selectedText() fmt = cursor.charFormat() if not fmt.fontUnderline(): - self.addWord() - cursor = self.paraEdit.textCursor() - textBlock = self.paraEdit.document().findBlock(cursor.position()) - blockNum = textBlock.blockNumber() + self.addWord(editor) query = QSqlQuery() query.prepare( - "SELECT w.* FROM word_block wb " - "LEFT JOIN words w " - "ON (w.word_id = wb.word_id) " - "WHERE :position BETWEEN wb.start AND wb.end " - "AND wb.block = :block AND wb.section_id = :section_id" + "SELECT w.* FROM words w " + "WHERE word = :word" ) - query.bindValue(":position", cursor.position() - textBlock.position()) - query.bindValue(":block", blockNum) - query.bindValue(":section_id", self.section_id) + query.bindValue(":word", word) if not query.exec(): query_error(query) if not query.next(): - return + return False word = query.value("word") definition = json.loads(query.value("definition")) self.setDefEdit(word, query.value("word_id"), definition) - return + return True def setDefEdit(self, word, word_id, definition): if "phonetics" in definition: @@ -600,22 +591,27 @@ class ReadDialog(QDialog, Ui_Dialog): self.displayedWord.emit(word_id) return + @pyqtSlot() + def returnAction(self) -> None: + self.returnBtn.setVisible(False) + if self.paragraphs: + self.nextBtn.setText("Next Para") + self.prevBtn.setText("Prev Para") + else: + self.nextBtn.setText("Next Sect") + self.prevBtn.setText("Prev Sect") + self.stackedWidget.setCurrentIndex(0) + self.update() + return + def showDefinition(self) -> None: idx = self.stackedWidget.currentIndex() - if idx == 0: - self.defineBtn.setText("Read") - self.nextBtn.setText("Next Def") - self.prevBtn.setText("Prev Def") - self.display_definition() - else: - self.defineBtn.setText("Definition") - if self.paragraphs: - self.nextBtn.setText("Next Para") - self.prevBtn.setText("Prev Para") - else: - self.nextBtn.setText("Next Sect") - self.prevBtn.setText("Prev Sect") - self.stackedWidget.setCurrentIndex(1 - idx) + self.returnBtn.setVisible(True) + self.nextBtn.setText("Next Def") + self.prevBtn.setText("Prev Def") + if not self.display_definition(idx): + return + self.stackedWidget.setCurrentIndex(1) self.update() return @@ -644,7 +640,7 @@ class ReadDialog(QDialog, Ui_Dialog): if found: cursor.setPosition(found.start) self.paraEdit.setTextCursor(cursor) - self.display_definition() + self.display_definition(0) return def scrollTo(self, position: int) -> None: