Add mouse over for defined words
This commit is contained in:
41
lib/read.py
41
lib/read.py
@@ -173,7 +173,10 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
cursor = QTextCursor(self.paraEdit.document())
|
||||
query = QSqlQuery()
|
||||
query.prepare(
|
||||
"SELECT * FROM word_block " "WHERE section_id = :section_id"
|
||||
"SELECT wb.*,w.word,w.definition FROM word_block wb "
|
||||
"LEFT JOIN words w "
|
||||
"ON (w.word_id = wb.word_id) "
|
||||
"WHERE wb.section_id = :section_id"
|
||||
)
|
||||
query.bindValue(":section_id", section_id)
|
||||
if not query.exec():
|
||||
@@ -193,6 +196,15 @@ class EditDialog(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"]
|
||||
except:
|
||||
phonetic = ""
|
||||
def_format.setToolTip(
|
||||
f'<font size="24">{word}:<br/><font family="Tex Gyre Heros">{phonetic}</font></font>'
|
||||
)
|
||||
cursor.setCharFormat(def_format)
|
||||
|
||||
return
|
||||
@@ -201,6 +213,9 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
return
|
||||
|
||||
def paintEvent(self, e: QPaintEvent | None) -> None:
|
||||
idx = self.stackedWidget.currentIndex()
|
||||
if idx > 0:
|
||||
return
|
||||
position = (
|
||||
self.paraEdit.document().findBlockByNumber(self.block).position()
|
||||
)
|
||||
@@ -362,9 +377,15 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
definition = json.loads(query.value("definition"))
|
||||
self.defEdit.document().clear()
|
||||
cursor = self.defEdit.textCursor()
|
||||
|
||||
h1_size = 48
|
||||
h1_weight = QFont.Weight.Bold
|
||||
p_size = 16
|
||||
p_weight = QFont.Weight.Normal
|
||||
|
||||
word_format = QTextCharFormat()
|
||||
word_format.setFontPointSize(48)
|
||||
word_format.setFontWeight(QFont.Weight.Bold)
|
||||
word_format.setFontPointSize(h1_size)
|
||||
word_format.setFontWeight(h1_weight)
|
||||
cursor.insertText(word, word_format)
|
||||
typeFormat = QTextListFormat()
|
||||
typeFormat.setStyle(QTextListFormat.Style.ListDisc)
|
||||
@@ -372,14 +393,17 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
defFormat = QTextListFormat()
|
||||
defFormat.setStyle(QTextListFormat.Style.ListCircle)
|
||||
defFormat.setIndent(2)
|
||||
word_format.setFontWeight(QFont.Weight.Normal)
|
||||
word_format.setFontPointSize(16)
|
||||
cursor.setCharFormat(word_format)
|
||||
word_format.setFontWeight(p_weight)
|
||||
word_format.setFontPointSize(p_size)
|
||||
phonetic_format = QTextCharFormat()
|
||||
phonetic_format.setFontPointSize(32)
|
||||
phonetic_format.setFontFamily("TeX Gyre Heros")
|
||||
try:
|
||||
cursor.insertBlock()
|
||||
cursor.insertText(definition["phonetic"])
|
||||
cursor.insertText(definition["phonetic"], phonetic_format)
|
||||
except Exception:
|
||||
pass
|
||||
cursor.setCharFormat(word_format)
|
||||
for meaning in definition["meanings"]:
|
||||
cursor.insertList(typeFormat)
|
||||
cursor.insertText(meaning["partOfSpeech"])
|
||||
@@ -392,6 +416,8 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
# TODO: synonyms
|
||||
# TODO: antonyms
|
||||
first = False
|
||||
cursor.setPosition(0)
|
||||
self.defEdit.setTextCursor(cursor)
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
@@ -400,6 +426,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
if idx == 0:
|
||||
self.display_definition()
|
||||
self.stackedWidget.setCurrentIndex(1 - idx)
|
||||
self.update()
|
||||
return
|
||||
|
||||
def nextDefinition(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user