Reworking the UI to be a bit cleaner
This commit is contained in:
156
lib/read.py
156
lib/read.py
@@ -38,6 +38,7 @@ from ui.EditDialog import Ui_Dialog
|
||||
|
||||
class EditDialog(QDialog, Ui_Dialog):
|
||||
block: int
|
||||
paragraphs = True
|
||||
|
||||
def __init__(self, person_id: int) -> None:
|
||||
super(EditDialog, self).__init__()
|
||||
@@ -51,10 +52,6 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
#
|
||||
# Override UI
|
||||
#
|
||||
self.printBtn = QPushButton()
|
||||
self.printBtn.setText("Print")
|
||||
self.printBtn.setObjectName("printBtn")
|
||||
self.verticalLayout.addWidget(self.printBtn)
|
||||
#
|
||||
# End overrides
|
||||
#
|
||||
@@ -68,15 +65,12 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
self.block = blockNumber
|
||||
self.savePosition()
|
||||
self.stackedWidget.setCurrentIndex(0)
|
||||
self.addBtn.clicked.connect(self.defineAction)
|
||||
self.showBtn.clicked.connect(self.showAction)
|
||||
self.defineBtn.clicked.connect(self.defineAction)
|
||||
self.printBtn.clicked.connect(self.printAction)
|
||||
self.scrollBtn.clicked.connect(self.scrollAction)
|
||||
self.nextBtn.clicked.connect(self.nextAction)
|
||||
self.prevBtn.clicked.connect(self.prevAction)
|
||||
self.nextParaBtn.clicked.connect(self.nextParaAction)
|
||||
self.prevParaBtn.clicked.connect(self.prevParaAction)
|
||||
self.printBtn.clicked.connect(self.printAction)
|
||||
self.paraEdit.verticalScrollBar().valueChanged.connect(self.scrollSlot)
|
||||
self.scrollBtn.clicked.connect(self.scrollAction)
|
||||
return
|
||||
|
||||
def defToHtml(self, word: str, definition) -> str:
|
||||
@@ -116,7 +110,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
"LEFT JOIN words w "
|
||||
"ON (w.word_id = wb.word_id) "
|
||||
"WHERE wb.section_id = :section_id "
|
||||
"ORDER BY w.word"
|
||||
"ORDER BY w.word COLLATE NOCASE"
|
||||
)
|
||||
query.bindValue(":section_id", self.section_id)
|
||||
if not query.exec():
|
||||
@@ -235,7 +229,6 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
|
||||
def show_section(self, section_id: int, start: bool = True) -> None:
|
||||
sequence = self.section_map[section_id]
|
||||
self.setWindowTitle(f"Section {sequence}")
|
||||
self.paraEdit.clear()
|
||||
cursor = self.paraEdit.textCursor()
|
||||
cursor.insertHtml(self.sections[sequence])
|
||||
@@ -294,11 +287,19 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
#
|
||||
# Event handlers
|
||||
#
|
||||
def mousePressEvent(self, event: QMouseEvent | None) -> None:
|
||||
def keyReleaseEvent(self, event: QKeyEvent) -> None:
|
||||
self.nextBtn.setText("Next Para")
|
||||
self.prevBtn.setText("Prev Para")
|
||||
self.defineBtn.setText("Show Def")
|
||||
self.paragraphs = True
|
||||
super().keyReleaseEvent(event)
|
||||
return
|
||||
|
||||
def keyPressEvent(self, event: QKeyEvent) -> None:
|
||||
print(event)
|
||||
self.nextBtn.setText("Next Sect")
|
||||
self.prevBtn.setText("Prev Sect")
|
||||
self.defineBtn.setText("Add Def")
|
||||
self.paragraphs = False
|
||||
super().keyPressEvent(event)
|
||||
return
|
||||
|
||||
@@ -329,25 +330,53 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def nextParaAction(self) -> None:
|
||||
def nextAction(self) -> None:
|
||||
if self.stackedWidget.currentIndex() == 1:
|
||||
print("Next Definition")
|
||||
self.nextDefinition()
|
||||
elif self.paragraphs:
|
||||
self.nextParagraph()
|
||||
else:
|
||||
self.nextSection()
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def prevAction(self) -> None:
|
||||
if self.stackedWidget.currentIndex() == 1:
|
||||
print("Previous Definition")
|
||||
self.prevDefinition()
|
||||
elif self.paragraphs:
|
||||
self.prevParagraph()
|
||||
else:
|
||||
self.prevSection()
|
||||
return
|
||||
|
||||
def nextParagraph(self) -> None:
|
||||
self.block += 1
|
||||
if self.block >= self.paraEdit.document().blockCount():
|
||||
self.nextAction()
|
||||
self.nextSection()
|
||||
return
|
||||
self.savePosition()
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def prevParaAction(self) -> None:
|
||||
def prevParagraph(self) -> None:
|
||||
self.block -= 1
|
||||
if self.block < 0:
|
||||
self.prevAction()
|
||||
self.prevSection()
|
||||
return
|
||||
self.savePosition()
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def defineAction(self) -> None:
|
||||
print("defineAction")
|
||||
if self.paragraphs:
|
||||
self.showDefinition()
|
||||
else:
|
||||
self.addWord()
|
||||
return
|
||||
|
||||
def addWord(self) -> None:
|
||||
#
|
||||
# Find the word
|
||||
#
|
||||
@@ -358,7 +387,11 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
if start != end:
|
||||
word = word.strip()
|
||||
if len(word) == 0 or word.find(" ") >= 0:
|
||||
return
|
||||
cursor.select(QTextCursor.SelectionType.WordUnderCursor)
|
||||
word = cursor.selectedText()
|
||||
word = word.strip()
|
||||
start = cursor.selectionStart()
|
||||
end = cursor.selectionEnd()
|
||||
if start > end:
|
||||
tmp = start
|
||||
start = end
|
||||
@@ -386,7 +419,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
|
||||
)
|
||||
if response.status_code != 200:
|
||||
print(response.content)
|
||||
print(f"{word}: {response.content}")
|
||||
return
|
||||
definitions = json.loads(response.content.decode("utf-8"))
|
||||
definition = definitions[0]
|
||||
@@ -445,8 +478,13 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
cursor.mergeCharFormat(def_format)
|
||||
return
|
||||
|
||||
# XXX - rename
|
||||
def display_definition(self) -> None:
|
||||
cursor = self.paraEdit.textCursor()
|
||||
fmt = cursor.charFormat()
|
||||
if not fmt.fontUnderline():
|
||||
self.addWord()
|
||||
cursor = self.paraEdit.textCursor()
|
||||
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
||||
blockNum = textBlock.blockNumber()
|
||||
query = QSqlQuery()
|
||||
@@ -468,50 +506,55 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
definition = json.loads(query.value("definition"))
|
||||
self.defEdit.document().clear()
|
||||
cursor = self.defEdit.textCursor()
|
||||
cursor.insertHtml(self.defToHtml(word,definition))
|
||||
cursor.insertHtml(self.defToHtml(word, definition))
|
||||
cursor.setPosition(0)
|
||||
self.defEdit.setTextCursor(cursor)
|
||||
print(self.defEdit.document().toHtml())
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def showAction(self) -> None:
|
||||
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.update()
|
||||
return
|
||||
|
||||
def nextDefinition(self) -> None:
|
||||
cursor = self.paraEdit.textCursor()
|
||||
position = cursor.position()
|
||||
block = cursor.block()
|
||||
formats = self.paraEdit.document().allFormats()
|
||||
found = None
|
||||
while block and not found:
|
||||
formats = block.textFormats()
|
||||
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)
|
||||
block = block.next()
|
||||
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()
|
||||
return
|
||||
|
||||
@@ -545,11 +588,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
query_error(query)
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def nextAction(self) -> None:
|
||||
if self.stackedWidget.currentIndex() == 1:
|
||||
self.nextDefinition()
|
||||
return
|
||||
def nextSection(self) -> None:
|
||||
sequence = self.section_map[self.section_id]
|
||||
sequence += 1
|
||||
self.section_id = self.sequence_map[sequence]
|
||||
@@ -557,10 +596,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
self.savePosition()
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
def prevAction(self) -> None:
|
||||
if self.stackedWidget.currentIndex() == 1:
|
||||
return
|
||||
def prevSection(self) -> None:
|
||||
sequence = self.section_map[self.section_id]
|
||||
if sequence < 1:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user