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