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
|
||||
|
||||
@@ -45,23 +45,17 @@ class Ui_Dialog(object):
|
||||
self.widget.setObjectName("widget")
|
||||
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
|
||||
self.verticalLayout.setObjectName("verticalLayout")
|
||||
self.addBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.addBtn.setObjectName("addBtn")
|
||||
self.verticalLayout.addWidget(self.addBtn)
|
||||
self.showBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.showBtn.setObjectName("showBtn")
|
||||
self.verticalLayout.addWidget(self.showBtn)
|
||||
self.defineBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.defineBtn.setObjectName("defineBtn")
|
||||
self.verticalLayout.addWidget(self.defineBtn)
|
||||
self.printBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.printBtn.setObjectName("printBtn")
|
||||
self.verticalLayout.addWidget(self.printBtn)
|
||||
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
|
||||
self.verticalLayout.addItem(spacerItem)
|
||||
self.scrollBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.scrollBtn.setObjectName("scrollBtn")
|
||||
self.verticalLayout.addWidget(self.scrollBtn)
|
||||
self.nextParaBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.nextParaBtn.setObjectName("nextParaBtn")
|
||||
self.verticalLayout.addWidget(self.nextParaBtn)
|
||||
self.prevParaBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.prevParaBtn.setObjectName("prevParaBtn")
|
||||
self.verticalLayout.addWidget(self.prevParaBtn)
|
||||
self.nextBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||
self.nextBtn.setObjectName("nextBtn")
|
||||
self.verticalLayout.addWidget(self.nextBtn)
|
||||
@@ -76,11 +70,9 @@ class Ui_Dialog(object):
|
||||
|
||||
def retranslateUi(self, Dialog):
|
||||
_translate = QtCore.QCoreApplication.translate
|
||||
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
|
||||
self.addBtn.setText(_translate("Dialog", "Add Word"))
|
||||
self.showBtn.setText(_translate("Dialog", "Definition"))
|
||||
Dialog.setWindowTitle(_translate("Dialog", "Reader"))
|
||||
self.defineBtn.setText(_translate("Dialog", "Show Def"))
|
||||
self.printBtn.setText(_translate("Dialog", "Print"))
|
||||
self.scrollBtn.setText(_translate("Dialog", "Scroll"))
|
||||
self.nextParaBtn.setText(_translate("Dialog", "Next Para"))
|
||||
self.prevParaBtn.setText(_translate("Dialog", "Prev Para"))
|
||||
self.nextBtn.setText(_translate("Dialog", "Next Sect"))
|
||||
self.prevBtn.setText(_translate("Dialog", "Prev. Sect"))
|
||||
self.nextBtn.setText(_translate("Dialog", "Next Para"))
|
||||
self.prevBtn.setText(_translate("Dialog", "Prev Para"))
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Dialog</string>
|
||||
<string>Reader</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
@@ -52,16 +52,16 @@
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="addBtn">
|
||||
<widget class="QPushButton" name="defineBtn">
|
||||
<property name="text">
|
||||
<string>Add Word</string>
|
||||
<string>Show Def</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="showBtn">
|
||||
<widget class="QPushButton" name="printBtn">
|
||||
<property name="text">
|
||||
<string>Definition</string>
|
||||
<string>Print</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
@@ -86,30 +86,16 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="nextParaBtn">
|
||||
<widget class="QPushButton" name="nextBtn">
|
||||
<property name="text">
|
||||
<string>Next Para</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="prevParaBtn">
|
||||
<property name="text">
|
||||
<string>Prev Para</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="nextBtn">
|
||||
<property name="text">
|
||||
<string>Next Sect</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="prevBtn">
|
||||
<property name="text">
|
||||
<string>Prev. Sect</string>
|
||||
<string>Prev Para</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
||||
Reference in New Issue
Block a user