Reworking the UI to be a bit cleaner
This commit is contained in:
114
lib/read.py
114
lib/read.py
@@ -38,6 +38,7 @@ from ui.EditDialog import Ui_Dialog
|
|||||||
|
|
||||||
class EditDialog(QDialog, Ui_Dialog):
|
class EditDialog(QDialog, Ui_Dialog):
|
||||||
block: int
|
block: int
|
||||||
|
paragraphs = True
|
||||||
|
|
||||||
def __init__(self, person_id: int) -> None:
|
def __init__(self, person_id: int) -> None:
|
||||||
super(EditDialog, self).__init__()
|
super(EditDialog, self).__init__()
|
||||||
@@ -51,10 +52,6 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
#
|
#
|
||||||
# Override UI
|
# Override UI
|
||||||
#
|
#
|
||||||
self.printBtn = QPushButton()
|
|
||||||
self.printBtn.setText("Print")
|
|
||||||
self.printBtn.setObjectName("printBtn")
|
|
||||||
self.verticalLayout.addWidget(self.printBtn)
|
|
||||||
#
|
#
|
||||||
# End overrides
|
# End overrides
|
||||||
#
|
#
|
||||||
@@ -68,15 +65,12 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
self.block = blockNumber
|
self.block = blockNumber
|
||||||
self.savePosition()
|
self.savePosition()
|
||||||
self.stackedWidget.setCurrentIndex(0)
|
self.stackedWidget.setCurrentIndex(0)
|
||||||
self.addBtn.clicked.connect(self.defineAction)
|
self.defineBtn.clicked.connect(self.defineAction)
|
||||||
self.showBtn.clicked.connect(self.showAction)
|
self.printBtn.clicked.connect(self.printAction)
|
||||||
|
self.scrollBtn.clicked.connect(self.scrollAction)
|
||||||
self.nextBtn.clicked.connect(self.nextAction)
|
self.nextBtn.clicked.connect(self.nextAction)
|
||||||
self.prevBtn.clicked.connect(self.prevAction)
|
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.paraEdit.verticalScrollBar().valueChanged.connect(self.scrollSlot)
|
||||||
self.scrollBtn.clicked.connect(self.scrollAction)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def defToHtml(self, word: str, definition) -> str:
|
def defToHtml(self, word: str, definition) -> str:
|
||||||
@@ -116,7 +110,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
"LEFT JOIN words w "
|
"LEFT JOIN words w "
|
||||||
"ON (w.word_id = wb.word_id) "
|
"ON (w.word_id = wb.word_id) "
|
||||||
"WHERE wb.section_id = :section_id "
|
"WHERE wb.section_id = :section_id "
|
||||||
"ORDER BY w.word"
|
"ORDER BY w.word COLLATE NOCASE"
|
||||||
)
|
)
|
||||||
query.bindValue(":section_id", self.section_id)
|
query.bindValue(":section_id", self.section_id)
|
||||||
if not query.exec():
|
if not query.exec():
|
||||||
@@ -235,7 +229,6 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
|
|
||||||
def show_section(self, section_id: int, start: bool = True) -> None:
|
def show_section(self, section_id: int, start: bool = True) -> None:
|
||||||
sequence = self.section_map[section_id]
|
sequence = self.section_map[section_id]
|
||||||
self.setWindowTitle(f"Section {sequence}")
|
|
||||||
self.paraEdit.clear()
|
self.paraEdit.clear()
|
||||||
cursor = self.paraEdit.textCursor()
|
cursor = self.paraEdit.textCursor()
|
||||||
cursor.insertHtml(self.sections[sequence])
|
cursor.insertHtml(self.sections[sequence])
|
||||||
@@ -294,11 +287,19 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
#
|
#
|
||||||
# Event handlers
|
# 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
|
return
|
||||||
|
|
||||||
def keyPressEvent(self, event: QKeyEvent) -> None:
|
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)
|
super().keyPressEvent(event)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -329,25 +330,53 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
@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
|
self.block += 1
|
||||||
if self.block >= self.paraEdit.document().blockCount():
|
if self.block >= self.paraEdit.document().blockCount():
|
||||||
self.nextAction()
|
self.nextSection()
|
||||||
return
|
return
|
||||||
self.savePosition()
|
self.savePosition()
|
||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
def prevParagraph(self) -> None:
|
||||||
def prevParaAction(self) -> None:
|
|
||||||
self.block -= 1
|
self.block -= 1
|
||||||
if self.block < 0:
|
if self.block < 0:
|
||||||
self.prevAction()
|
self.prevSection()
|
||||||
return
|
return
|
||||||
self.savePosition()
|
self.savePosition()
|
||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
@pyqtSlot()
|
||||||
def defineAction(self) -> None:
|
def defineAction(self) -> None:
|
||||||
|
print("defineAction")
|
||||||
|
if self.paragraphs:
|
||||||
|
self.showDefinition()
|
||||||
|
else:
|
||||||
|
self.addWord()
|
||||||
|
return
|
||||||
|
|
||||||
|
def addWord(self) -> None:
|
||||||
#
|
#
|
||||||
# Find the word
|
# Find the word
|
||||||
#
|
#
|
||||||
@@ -358,7 +387,11 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
if start != end:
|
if start != end:
|
||||||
word = word.strip()
|
word = word.strip()
|
||||||
if len(word) == 0 or word.find(" ") >= 0:
|
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:
|
if start > end:
|
||||||
tmp = start
|
tmp = start
|
||||||
start = end
|
start = end
|
||||||
@@ -386,7 +419,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
|
f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
|
||||||
)
|
)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
print(response.content)
|
print(f"{word}: {response.content}")
|
||||||
return
|
return
|
||||||
definitions = json.loads(response.content.decode("utf-8"))
|
definitions = json.loads(response.content.decode("utf-8"))
|
||||||
definition = definitions[0]
|
definition = definitions[0]
|
||||||
@@ -445,8 +478,13 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
cursor.mergeCharFormat(def_format)
|
cursor.mergeCharFormat(def_format)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# XXX - rename
|
||||||
def display_definition(self) -> None:
|
def display_definition(self) -> None:
|
||||||
cursor = self.paraEdit.textCursor()
|
cursor = self.paraEdit.textCursor()
|
||||||
|
fmt = cursor.charFormat()
|
||||||
|
if not fmt.fontUnderline():
|
||||||
|
self.addWord()
|
||||||
|
cursor = self.paraEdit.textCursor()
|
||||||
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
textBlock = self.paraEdit.document().findBlock(cursor.position())
|
||||||
blockNum = textBlock.blockNumber()
|
blockNum = textBlock.blockNumber()
|
||||||
query = QSqlQuery()
|
query = QSqlQuery()
|
||||||
@@ -468,28 +506,34 @@ 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()
|
||||||
cursor.insertHtml(self.defToHtml(word,definition))
|
cursor.insertHtml(self.defToHtml(word, definition))
|
||||||
cursor.setPosition(0)
|
cursor.setPosition(0)
|
||||||
self.defEdit.setTextCursor(cursor)
|
self.defEdit.setTextCursor(cursor)
|
||||||
print(self.defEdit.document().toHtml())
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
def showDefinition(self) -> None:
|
||||||
def showAction(self) -> None:
|
|
||||||
idx = self.stackedWidget.currentIndex()
|
idx = self.stackedWidget.currentIndex()
|
||||||
if idx == 0:
|
if idx == 0:
|
||||||
|
self.defineBtn.setText("Read")
|
||||||
|
self.nextBtn.setText("Next Def")
|
||||||
|
self.prevBtn.setText("Prev Def")
|
||||||
self.display_definition()
|
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.stackedWidget.setCurrentIndex(1 - idx)
|
||||||
self.update()
|
self.update()
|
||||||
return
|
return
|
||||||
|
|
||||||
def nextDefinition(self) -> None:
|
def nextDefinition(self) -> None:
|
||||||
cursor = self.paraEdit.textCursor()
|
cursor = self.paraEdit.textCursor()
|
||||||
position = cursor.position()
|
formats = self.paraEdit.document().allFormats()
|
||||||
block = cursor.block()
|
|
||||||
found = None
|
found = None
|
||||||
while block and not found:
|
|
||||||
formats = block.textFormats()
|
|
||||||
for f in formats:
|
for f in formats:
|
||||||
wc = QTextCursor(cursor)
|
wc = QTextCursor(cursor)
|
||||||
wc.setPosition(f.start)
|
wc.setPosition(f.start)
|
||||||
@@ -511,7 +555,6 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
if found:
|
if found:
|
||||||
cursor.setPosition(found.start)
|
cursor.setPosition(found.start)
|
||||||
self.paraEdit.setTextCursor(cursor)
|
self.paraEdit.setTextCursor(cursor)
|
||||||
block = block.next()
|
|
||||||
self.display_definition()
|
self.display_definition()
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -545,11 +588,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
query_error(query)
|
query_error(query)
|
||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
def nextSection(self) -> None:
|
||||||
def nextAction(self) -> None:
|
|
||||||
if self.stackedWidget.currentIndex() == 1:
|
|
||||||
self.nextDefinition()
|
|
||||||
return
|
|
||||||
sequence = self.section_map[self.section_id]
|
sequence = self.section_map[self.section_id]
|
||||||
sequence += 1
|
sequence += 1
|
||||||
self.section_id = self.sequence_map[sequence]
|
self.section_id = self.sequence_map[sequence]
|
||||||
@@ -557,10 +596,7 @@ class EditDialog(QDialog, Ui_Dialog):
|
|||||||
self.savePosition()
|
self.savePosition()
|
||||||
return
|
return
|
||||||
|
|
||||||
@pyqtSlot()
|
def prevSection(self) -> None:
|
||||||
def prevAction(self) -> None:
|
|
||||||
if self.stackedWidget.currentIndex() == 1:
|
|
||||||
return
|
|
||||||
sequence = self.section_map[self.section_id]
|
sequence = self.section_map[self.section_id]
|
||||||
if sequence < 1:
|
if sequence < 1:
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -45,23 +45,17 @@ class Ui_Dialog(object):
|
|||||||
self.widget.setObjectName("widget")
|
self.widget.setObjectName("widget")
|
||||||
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
|
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
|
||||||
self.verticalLayout.setObjectName("verticalLayout")
|
self.verticalLayout.setObjectName("verticalLayout")
|
||||||
self.addBtn = QtWidgets.QPushButton(parent=self.widget)
|
self.defineBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||||
self.addBtn.setObjectName("addBtn")
|
self.defineBtn.setObjectName("defineBtn")
|
||||||
self.verticalLayout.addWidget(self.addBtn)
|
self.verticalLayout.addWidget(self.defineBtn)
|
||||||
self.showBtn = QtWidgets.QPushButton(parent=self.widget)
|
self.printBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||||
self.showBtn.setObjectName("showBtn")
|
self.printBtn.setObjectName("printBtn")
|
||||||
self.verticalLayout.addWidget(self.showBtn)
|
self.verticalLayout.addWidget(self.printBtn)
|
||||||
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
|
spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Expanding)
|
||||||
self.verticalLayout.addItem(spacerItem)
|
self.verticalLayout.addItem(spacerItem)
|
||||||
self.scrollBtn = QtWidgets.QPushButton(parent=self.widget)
|
self.scrollBtn = QtWidgets.QPushButton(parent=self.widget)
|
||||||
self.scrollBtn.setObjectName("scrollBtn")
|
self.scrollBtn.setObjectName("scrollBtn")
|
||||||
self.verticalLayout.addWidget(self.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 = QtWidgets.QPushButton(parent=self.widget)
|
||||||
self.nextBtn.setObjectName("nextBtn")
|
self.nextBtn.setObjectName("nextBtn")
|
||||||
self.verticalLayout.addWidget(self.nextBtn)
|
self.verticalLayout.addWidget(self.nextBtn)
|
||||||
@@ -76,11 +70,9 @@ class Ui_Dialog(object):
|
|||||||
|
|
||||||
def retranslateUi(self, Dialog):
|
def retranslateUi(self, Dialog):
|
||||||
_translate = QtCore.QCoreApplication.translate
|
_translate = QtCore.QCoreApplication.translate
|
||||||
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
|
Dialog.setWindowTitle(_translate("Dialog", "Reader"))
|
||||||
self.addBtn.setText(_translate("Dialog", "Add Word"))
|
self.defineBtn.setText(_translate("Dialog", "Show Def"))
|
||||||
self.showBtn.setText(_translate("Dialog", "Definition"))
|
self.printBtn.setText(_translate("Dialog", "Print"))
|
||||||
self.scrollBtn.setText(_translate("Dialog", "Scroll"))
|
self.scrollBtn.setText(_translate("Dialog", "Scroll"))
|
||||||
self.nextParaBtn.setText(_translate("Dialog", "Next Para"))
|
self.nextBtn.setText(_translate("Dialog", "Next Para"))
|
||||||
self.prevParaBtn.setText(_translate("Dialog", "Prev Para"))
|
self.prevBtn.setText(_translate("Dialog", "Prev Para"))
|
||||||
self.nextBtn.setText(_translate("Dialog", "Next Sect"))
|
|
||||||
self.prevBtn.setText(_translate("Dialog", "Prev. Sect"))
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>Dialog</string>
|
<string>Reader</string>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
<item>
|
<item>
|
||||||
@@ -52,16 +52,16 @@
|
|||||||
<widget class="QWidget" name="widget" native="true">
|
<widget class="QWidget" name="widget" native="true">
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="addBtn">
|
<widget class="QPushButton" name="defineBtn">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Add Word</string>
|
<string>Show Def</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="showBtn">
|
<widget class="QPushButton" name="printBtn">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Definition</string>
|
<string>Print</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@@ -86,30 +86,16 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="nextParaBtn">
|
<widget class="QPushButton" name="nextBtn">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Next Para</string>
|
<string>Next Para</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</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>
|
<item>
|
||||||
<widget class="QPushButton" name="prevBtn">
|
<widget class="QPushButton" name="prevBtn">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Prev. Sect</string>
|
<string>Prev Para</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user