Lint picking
This commit is contained in:
120
lib/read.py
120
lib/read.py
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
import re
|
||||
from typing import cast
|
||||
from typing import Any, Dict, List, Optional, cast
|
||||
|
||||
import requests
|
||||
from PyQt6.QtCore import (
|
||||
@@ -30,35 +30,38 @@ from PyQt6.QtGui import (
|
||||
QTextListFormat,
|
||||
)
|
||||
from PyQt6.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
|
||||
from PyQt6.QtWidgets import QDialog, QPushButton
|
||||
from PyQt6.QtWidgets import QDialog, QPushButton, QTextEdit, QWidget
|
||||
|
||||
from lib.preferences import Preferences
|
||||
from lib.session import SessionDialog
|
||||
from lib.sounds import SoundOff
|
||||
from main import query_error
|
||||
from ui.ReadDialog import Ui_Dialog
|
||||
from lib.preferences import Preferences
|
||||
from lib.sounds import SoundOff
|
||||
|
||||
|
||||
class ReadDialog(QDialog, Ui_Dialog):
|
||||
playSound = pyqtSignal(str)
|
||||
playAlert = pyqtSignal()
|
||||
block: int
|
||||
preferences: Dict[str, str | List[str]]
|
||||
paragraphs = True
|
||||
sessionSignal = pyqtSignal()
|
||||
displayedWord = pyqtSignal(int)
|
||||
newParagraph = pyqtSignal(int, int)
|
||||
|
||||
def __init__(self, parent, session, person_id: int) -> None:
|
||||
|
||||
def __init__(
|
||||
self, parent: Optional[QWidget], session: SessionDialog, person_id: int
|
||||
) -> None:
|
||||
self.session = session
|
||||
super(ReadDialog, self).__init__(parent)
|
||||
self.person_id = person_id
|
||||
self.preferences = Preferences().get()
|
||||
self.preferences = cast(Dict[str, str | List[str]], Preferences().get())
|
||||
self.sound = SoundOff()
|
||||
styleSheet = QResource(":/display.css").data().decode("utf-8")
|
||||
styleSheet = styleSheet.replace(
|
||||
'{readerFont}',self.preferences['readerFont']
|
||||
)
|
||||
styleSheet = styleSheet.replace(
|
||||
'{phoneticFont}',self.preferences['phoneticFont']
|
||||
)
|
||||
readerFont = cast(str, self.preferences["readerFont"])
|
||||
phoneticFont = cast(str, self.preferences["phoneticFont"])
|
||||
styleSheet = styleSheet.replace("{readerFont}", readerFont)
|
||||
styleSheet = styleSheet.replace("{phoneticFont}", phoneticFont)
|
||||
self.setupUi(self)
|
||||
#
|
||||
# Override UI
|
||||
@@ -88,7 +91,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
self.prevBtn.clicked.connect(self.prevAction)
|
||||
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
|
||||
@@ -107,16 +110,17 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
# slots
|
||||
#
|
||||
@pyqtSlot()
|
||||
def timerAction(self):
|
||||
if self.session.isActive(): # We are stopping
|
||||
def timerAction(self) -> None:
|
||||
if self.session.isActive(): # We are stopping
|
||||
self.sessionBtn.setText("Start")
|
||||
else:
|
||||
self.sessionBtn.setText("Stop")
|
||||
self.session.timerAction()
|
||||
self.newParagraph.emit(self.section_id, self.block)
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def recursiveDef(self):
|
||||
def recursiveDef(self) -> None:
|
||||
cursor = self.defEdit.textCursor()
|
||||
selection = cursor.selectedText().strip()
|
||||
if len(selection) <= 0:
|
||||
@@ -125,7 +129,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
query.prepare("SELECT * FROM words " "WHERE word = :word")
|
||||
query.bindValue(":word", selection)
|
||||
if not query.exec():
|
||||
query_error()
|
||||
query_error(query)
|
||||
if not query.next():
|
||||
response = requests.get(
|
||||
f"https://api.dictionaryapi.dev/api/v2/entries/en/{selection}"
|
||||
@@ -138,13 +142,13 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
else:
|
||||
definition = query.value("definition")
|
||||
word_id = query.value("word_id")
|
||||
self.setDefEdit(selection, word_id, definition)
|
||||
self.setDefEdit(selection, word_id, definition)
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def sessionAction(self) -> None:
|
||||
self.sessionSignal.emit()
|
||||
self.session.addParagraph(self.section_id, self.block)
|
||||
self.newParagraph.emit(self.section_id, self.block)
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
@@ -248,10 +252,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
|
||||
@pyqtSlot()
|
||||
def nextAction(self) -> None:
|
||||
if self.stackedWidget.currentIndex() == 1:
|
||||
print("Next Definition")
|
||||
self.nextDefinition()
|
||||
elif self.paragraphs:
|
||||
if self.paragraphs:
|
||||
self.nextParagraph()
|
||||
else:
|
||||
self.nextSection()
|
||||
@@ -259,15 +260,15 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
|
||||
@pyqtSlot()
|
||||
def prevAction(self) -> None:
|
||||
if self.stackedWidget.currentIndex() == 1:
|
||||
print("Previous Definition")
|
||||
self.prevDefinition()
|
||||
elif self.paragraphs:
|
||||
if self.paragraphs:
|
||||
self.prevParagraph()
|
||||
else:
|
||||
self.prevSection()
|
||||
return
|
||||
|
||||
#
|
||||
# Called when the "define" button is pressed
|
||||
#
|
||||
@pyqtSlot()
|
||||
def defineAction(self) -> None:
|
||||
editor = self.paraEdit
|
||||
@@ -277,10 +278,10 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
self.showDefinition()
|
||||
return
|
||||
|
||||
def defToHtml(self, word: str, definition) -> str:
|
||||
def defToHtml(self, word: str, definition: Dict[str, Any]) -> str:
|
||||
html = f'<h1 class="def-word">{word}</h1>' + "\n"
|
||||
try:
|
||||
words = []
|
||||
words: List[str] = []
|
||||
for phonetic in definition["phonetics"]:
|
||||
if phonetic["text"] in words:
|
||||
continue
|
||||
@@ -389,7 +390,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
#
|
||||
# Event handlers
|
||||
#
|
||||
def keyReleaseEvent(self, event: QKeyEvent) -> None:
|
||||
def keyReleaseEvent(self, event: Optional[QKeyEvent]) -> None:
|
||||
self.nextBtn.setText("Next Para")
|
||||
self.prevBtn.setText("Prev Para")
|
||||
self.defineBtn.setText("Show Def")
|
||||
@@ -397,7 +398,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
super().keyReleaseEvent(event)
|
||||
return
|
||||
|
||||
def keyPressEvent(self, event: QKeyEvent) -> None:
|
||||
def keyPressEvent(self, event: Optional[QKeyEvent]) -> None:
|
||||
self.nextBtn.setText("Next Sect")
|
||||
self.prevBtn.setText("Prev Sect")
|
||||
self.defineBtn.setText("Add Def")
|
||||
@@ -447,7 +448,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
self.savePosition()
|
||||
return
|
||||
|
||||
def addWord(self,editor) -> None:
|
||||
def addWord(self, editor: QTextEdit) -> None:
|
||||
#
|
||||
# Find the word
|
||||
#
|
||||
@@ -470,7 +471,9 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
#
|
||||
# Find the block
|
||||
#
|
||||
textBlock = editor.document().findBlock(cursor.position())
|
||||
document = editor.document()
|
||||
assert document is not None
|
||||
textBlock = document.findBlock(cursor.position())
|
||||
blockNum = textBlock.blockNumber()
|
||||
start = start - textBlock.position()
|
||||
end = end - textBlock.position()
|
||||
@@ -490,7 +493,7 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
|
||||
)
|
||||
if response.status_code != 200:
|
||||
print(f"{word}: {response.content}")
|
||||
print(f"{word}: {response.content.decode('utf8')}")
|
||||
self.playAlert.emit()
|
||||
return
|
||||
definitions = json.loads(response.content.decode("utf-8"))
|
||||
@@ -551,6 +554,10 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
return
|
||||
|
||||
# XXX - rename
|
||||
#
|
||||
# Create a definition for the word under the cursor on the current
|
||||
# panel.
|
||||
#
|
||||
def display_definition(self, idx: int) -> bool:
|
||||
if idx == 0:
|
||||
editor = self.paraEdit
|
||||
@@ -559,14 +566,11 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
cursor = editor.textCursor()
|
||||
cursor.select(QTextCursor.SelectionType.WordUnderCursor)
|
||||
word = cursor.selectedText()
|
||||
fmt = cursor.charFormat()
|
||||
if not fmt.fontUnderline():
|
||||
self.addWord(editor)
|
||||
# fmt = cursor.charFormat()
|
||||
# if not fmt.fontUnderline():
|
||||
# self.addWord(editor)
|
||||
query = QSqlQuery()
|
||||
query.prepare(
|
||||
"SELECT w.* FROM words w "
|
||||
"WHERE word = :word"
|
||||
)
|
||||
query.prepare("SELECT w.* FROM words w " "WHERE word = :word")
|
||||
query.bindValue(":word", word)
|
||||
if not query.exec():
|
||||
query_error(query)
|
||||
@@ -577,7 +581,9 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
self.setDefEdit(word, query.value("word_id"), definition)
|
||||
return True
|
||||
|
||||
def setDefEdit(self, word, word_id, definition):
|
||||
def setDefEdit(
|
||||
self, word: str, word_id: int, definition: Dict[str, str]
|
||||
) -> None:
|
||||
if "phonetics" in definition:
|
||||
self.phonetics = definition["phonetics"]
|
||||
else:
|
||||
@@ -615,34 +621,6 @@ class ReadDialog(QDialog, Ui_Dialog):
|
||||
self.update()
|
||||
return
|
||||
|
||||
def nextDefinition(self) -> None:
|
||||
cursor = self.paraEdit.textCursor()
|
||||
formats = self.paraEdit.document().allFormats()
|
||||
found = None
|
||||
for f in formats:
|
||||
wc = QTextCursor(cursor)
|
||||
wc.setPosition(f.start)
|
||||
wc.movePosition(
|
||||
QTextCursor.MoveOperation.Right,
|
||||
QTextCursor.MoveMode.KeepAnchor,
|
||||
f.length,
|
||||
)
|
||||
word = wc.selectedText()
|
||||
cf = wc.charFormat()
|
||||
if f.start <= position:
|
||||
continue
|
||||
if not cf.fontUnderline():
|
||||
continue
|
||||
if not found:
|
||||
found = f
|
||||
elif f.start < found.start:
|
||||
found = f
|
||||
if found:
|
||||
cursor.setPosition(found.start)
|
||||
self.paraEdit.setTextCursor(cursor)
|
||||
self.display_definition(0)
|
||||
return
|
||||
|
||||
def scrollTo(self, position: int) -> None:
|
||||
cursor = self.paraEdit.textCursor()
|
||||
cursor.setPosition(position)
|
||||
|
||||
Reference in New Issue
Block a user