Alert on no def, clean up of finding word to define
This commit is contained in:
92
lib/read.py
92
lib/read.py
@@ -39,6 +39,7 @@ from lib.sounds import SoundOff
|
|||||||
|
|
||||||
class ReadDialog(QDialog, Ui_Dialog):
|
class ReadDialog(QDialog, Ui_Dialog):
|
||||||
playSound = pyqtSignal(str)
|
playSound = pyqtSignal(str)
|
||||||
|
playAlert = pyqtSignal()
|
||||||
block: int
|
block: int
|
||||||
paragraphs = True
|
paragraphs = True
|
||||||
sessionSignal = pyqtSignal()
|
sessionSignal = pyqtSignal()
|
||||||
@@ -52,7 +53,6 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
self.preferences = Preferences().get()
|
self.preferences = Preferences().get()
|
||||||
self.sound = SoundOff()
|
self.sound = SoundOff()
|
||||||
styleSheet = QResource(":/display.css").data().decode("utf-8")
|
styleSheet = QResource(":/display.css").data().decode("utf-8")
|
||||||
print(styleSheet)
|
|
||||||
styleSheet = styleSheet.replace(
|
styleSheet = styleSheet.replace(
|
||||||
'{readerFont}',self.preferences['readerFont']
|
'{readerFont}',self.preferences['readerFont']
|
||||||
)
|
)
|
||||||
@@ -63,6 +63,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
#
|
#
|
||||||
# Override UI
|
# Override UI
|
||||||
#
|
#
|
||||||
|
self.returnBtn.setVisible(False)
|
||||||
#
|
#
|
||||||
# End overrides
|
# End overrides
|
||||||
#
|
#
|
||||||
@@ -85,17 +86,17 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
self.scrollBtn.clicked.connect(self.scrollAction)
|
self.scrollBtn.clicked.connect(self.scrollAction)
|
||||||
self.nextBtn.clicked.connect(self.nextAction)
|
self.nextBtn.clicked.connect(self.nextAction)
|
||||||
self.prevBtn.clicked.connect(self.prevAction)
|
self.prevBtn.clicked.connect(self.prevAction)
|
||||||
#self.sessionBtn.clicked.connect(self.session.timerAction)
|
|
||||||
self.sessionBtn.clicked.connect(self.timerAction)
|
self.sessionBtn.clicked.connect(self.timerAction)
|
||||||
self.paraEdit.verticalScrollBar().valueChanged.connect(self.scrollSlot)
|
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
|
# Connect signals
|
||||||
#
|
#
|
||||||
self.displayedWord.connect(self.session.addWord)
|
self.displayedWord.connect(self.session.addWord)
|
||||||
self.newParagraph.connect(self.session.addBlock)
|
self.newParagraph.connect(self.session.addBlock)
|
||||||
self.playSound.connect(self.sound.playSound)
|
self.playSound.connect(self.sound.playSound)
|
||||||
#self.alert.connect(self.sound.alert)
|
self.playAlert.connect(self.sound.alert)
|
||||||
return
|
return
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -155,7 +156,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
cursor = self.paraEdit.textCursor()
|
cursor = self.paraEdit.textCursor()
|
||||||
fmt = cursor.charFormat()
|
fmt = cursor.charFormat()
|
||||||
if not fmt.fontUnderline():
|
if not fmt.fontUnderline():
|
||||||
self.addWord()
|
self.addWord(self.paraEdit)
|
||||||
cursor = self.paraEdit.textCursor()
|
cursor = self.paraEdit.textCursor()
|
||||||
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
||||||
blockNum = textBlock.blockNumber()
|
blockNum = textBlock.blockNumber()
|
||||||
@@ -269,10 +270,11 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
|
|
||||||
@pyqtSlot()
|
@pyqtSlot()
|
||||||
def defineAction(self) -> None:
|
def defineAction(self) -> None:
|
||||||
if self.paragraphs:
|
editor = self.paraEdit
|
||||||
|
if self.stackedWidget.currentIndex() > 0:
|
||||||
|
editor = self.defEdit
|
||||||
|
self.addWord(editor)
|
||||||
self.showDefinition()
|
self.showDefinition()
|
||||||
else:
|
|
||||||
self.addWord()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def defToHtml(self, word: str, definition) -> str:
|
def defToHtml(self, word: str, definition) -> str:
|
||||||
@@ -381,17 +383,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
cursor.setPosition(
|
cursor.setPosition(
|
||||||
end + textBlock.position(), QTextCursor.MoveMode.KeepAnchor
|
end + textBlock.position(), QTextCursor.MoveMode.KeepAnchor
|
||||||
)
|
)
|
||||||
word = query.value("word")
|
|
||||||
definition = json.loads(query.value("definition"))
|
|
||||||
try:
|
|
||||||
phonetic = definition["phonetic"]
|
|
||||||
def_format.setToolTip(
|
|
||||||
f'<font size="24">{word}:<br/><font family="Tex Gyre Heros">{phonetic}</font></font>'
|
|
||||||
)
|
|
||||||
cursor.mergeCharFormat(def_format)
|
cursor.mergeCharFormat(def_format)
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -455,11 +447,11 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
self.savePosition()
|
self.savePosition()
|
||||||
return
|
return
|
||||||
|
|
||||||
def addWord(self) -> None:
|
def addWord(self,editor) -> None:
|
||||||
#
|
#
|
||||||
# Find the word
|
# Find the word
|
||||||
#
|
#
|
||||||
cursor = self.paraEdit.textCursor()
|
cursor = editor.textCursor()
|
||||||
word = cursor.selectedText()
|
word = cursor.selectedText()
|
||||||
start = cursor.selectionStart()
|
start = cursor.selectionStart()
|
||||||
end = cursor.selectionEnd()
|
end = cursor.selectionEnd()
|
||||||
@@ -478,7 +470,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
#
|
#
|
||||||
# Find the block
|
# Find the block
|
||||||
#
|
#
|
||||||
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
textBlock = editor.document().findBlock(cursor.position())
|
||||||
blockNum = textBlock.blockNumber()
|
blockNum = textBlock.blockNumber()
|
||||||
start = start - textBlock.position()
|
start = start - textBlock.position()
|
||||||
end = end - textBlock.position()
|
end = end - textBlock.position()
|
||||||
@@ -499,6 +491,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
)
|
)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
print(f"{word}: {response.content}")
|
print(f"{word}: {response.content}")
|
||||||
|
self.playAlert.emit()
|
||||||
return
|
return
|
||||||
definitions = json.loads(response.content.decode("utf-8"))
|
definitions = json.loads(response.content.decode("utf-8"))
|
||||||
definition = definitions[0]
|
definition = definitions[0]
|
||||||
@@ -558,33 +551,31 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# XXX - rename
|
# XXX - rename
|
||||||
def display_definition(self) -> None:
|
def display_definition(self, idx: int) -> bool:
|
||||||
cursor = self.paraEdit.textCursor()
|
if idx == 0:
|
||||||
|
editor = self.paraEdit
|
||||||
|
else:
|
||||||
|
editor = self.defEdit
|
||||||
|
cursor = editor.textCursor()
|
||||||
|
cursor.select(QTextCursor.SelectionType.WordUnderCursor)
|
||||||
|
word = cursor.selectedText()
|
||||||
fmt = cursor.charFormat()
|
fmt = cursor.charFormat()
|
||||||
if not fmt.fontUnderline():
|
if not fmt.fontUnderline():
|
||||||
self.addWord()
|
self.addWord(editor)
|
||||||
cursor = self.paraEdit.textCursor()
|
|
||||||
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
|
||||||
blockNum = textBlock.blockNumber()
|
|
||||||
query = QSqlQuery()
|
query = QSqlQuery()
|
||||||
query.prepare(
|
query.prepare(
|
||||||
"SELECT w.* FROM word_block wb "
|
"SELECT w.* FROM words w "
|
||||||
"LEFT JOIN words w "
|
"WHERE word = :word"
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
query.bindValue(":position", cursor.position() - textBlock.position())
|
query.bindValue(":word", word)
|
||||||
query.bindValue(":block", blockNum)
|
|
||||||
query.bindValue(":section_id", self.section_id)
|
|
||||||
if not query.exec():
|
if not query.exec():
|
||||||
query_error(query)
|
query_error(query)
|
||||||
if not query.next():
|
if not query.next():
|
||||||
return
|
return False
|
||||||
word = query.value("word")
|
word = query.value("word")
|
||||||
definition = json.loads(query.value("definition"))
|
definition = json.loads(query.value("definition"))
|
||||||
self.setDefEdit(word, query.value("word_id"), definition)
|
self.setDefEdit(word, query.value("word_id"), definition)
|
||||||
return
|
return True
|
||||||
|
|
||||||
def setDefEdit(self, word, word_id, definition):
|
def setDefEdit(self, word, word_id, definition):
|
||||||
if "phonetics" in definition:
|
if "phonetics" in definition:
|
||||||
@@ -600,22 +591,27 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
self.displayedWord.emit(word_id)
|
self.displayedWord.emit(word_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
def showDefinition(self) -> None:
|
@pyqtSlot()
|
||||||
idx = self.stackedWidget.currentIndex()
|
def returnAction(self) -> None:
|
||||||
if idx == 0:
|
self.returnBtn.setVisible(False)
|
||||||
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:
|
if self.paragraphs:
|
||||||
self.nextBtn.setText("Next Para")
|
self.nextBtn.setText("Next Para")
|
||||||
self.prevBtn.setText("Prev Para")
|
self.prevBtn.setText("Prev Para")
|
||||||
else:
|
else:
|
||||||
self.nextBtn.setText("Next Sect")
|
self.nextBtn.setText("Next Sect")
|
||||||
self.prevBtn.setText("Prev Sect")
|
self.prevBtn.setText("Prev Sect")
|
||||||
self.stackedWidget.setCurrentIndex(1 - idx)
|
self.stackedWidget.setCurrentIndex(0)
|
||||||
|
self.update()
|
||||||
|
return
|
||||||
|
|
||||||
|
def showDefinition(self) -> None:
|
||||||
|
idx = self.stackedWidget.currentIndex()
|
||||||
|
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()
|
self.update()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -644,7 +640,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
|||||||
if found:
|
if found:
|
||||||
cursor.setPosition(found.start)
|
cursor.setPosition(found.start)
|
||||||
self.paraEdit.setTextCursor(cursor)
|
self.paraEdit.setTextCursor(cursor)
|
||||||
self.display_definition()
|
self.display_definition(0)
|
||||||
return
|
return
|
||||||
|
|
||||||
def scrollTo(self, position: int) -> None:
|
def scrollTo(self, position: int) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user