People edit and improved Session

This commit is contained in:
Christopher T. Johnson
2023-12-12 10:09:59 -05:00
parent 6752249680
commit 3aec9cc2c8
9 changed files with 411 additions and 65 deletions

View File

@@ -1,7 +1,13 @@
from datetime import datetime, timedelta
from PyQt6.QtCore import Qt, QTime, QTimer, pyqtSignal, pyqtSlot
from PyQt6.QtGui import QStandardItem, QStandardItemModel, QTextCursor
from PyQt6.QtGui import (
QStandardItem,
QStandardItemModel,
QTextCursor,
QTextDocument,
QTextBlockFormat
)
from PyQt6.QtSql import QSqlQuery
from PyQt6.QtWidgets import QDialog
@@ -10,9 +16,16 @@ from ui.SessionDialog import Ui_Dialog
class SessionDialog(QDialog, Ui_Dialog):
WordIdRole = Qt.ItemDataRole.UserRole
SectionIdRole = Qt.ItemDataRole.UserRole + 1
BlockRole = Qt.ItemDataRole.UserRole + 2
timer = QTimer()
startTime = datetime.now()
totalTime = 0 # seconds
sessionStart = None
sessionEnd = None
blocks = QStandardItemModel()
def __init__(self) -> None:
super(SessionDialog, self).__init__()
@@ -26,8 +39,18 @@ class SessionDialog(QDialog, Ui_Dialog):
self.activeBox.stateChanged.connect(self.activeAction)
return
@pyqtSlot()
def resetForm(self) -> None:
self.nameLbl.setText("")
self.totalTime = timedelta()
self.wordView.model().clear()
self.textBrowser.document().clear()
return
@pyqtSlot(int)
def setPerson(self, person_id) -> None:
self.resetForm()
self.person_id = person_id
query = QSqlQuery()
query.prepare("SELECT * FROM people " "WHERE person_id = :person_id")
query.bindValue(":person_id", person_id)
@@ -37,16 +60,26 @@ class SessionDialog(QDialog, Ui_Dialog):
raise Exception(f"Bad person_id: {person_id}")
self.nameLbl.setText(query.value("name"))
self.totalTime = timedelta()
self.wordView.model().clear()
return
@pyqtSlot()
def accept(self) -> None:
print("Accepted")
super().accept()
return
@pyqtSlot(int)
def activeAction(self, state: int) -> None:
if state:
if not self.sessionStart:
self.sessionStart = datetime.now()
self.startTime = datetime.now()
self.timer.start()
else:
self.totalTime = self.totalTime + (datetime.now() - self.startTime)
self.timer.stop()
self.sessionEnd = datetime.now()
return
@pyqtSlot()
@@ -81,22 +114,56 @@ class SessionDialog(QDialog, Ui_Dialog):
raise Exception(f"Word_id({word_id}) not found in DB")
word = QStandardItem()
word.setData(query.value("word"), Qt.ItemDataRole.DisplayRole)
word_id = QStandardItem()
word_id.setData(
query.value("word_id"), Qt.ItemDataRole.UserRole + 1
)
word.setData(query.value("word_id"), SessionDialog.WordIdRole)
model = self.wordView.model()
matches = model.match(
model.createIndex(0, 1),
Qt.ItemDataRole.UserRole + 1,
model.createIndex(0, 0),
SessionDialog.WordIdRole,
query.value("word_id"),
1,
Qt.MatchFlag.MatchExactly,
)
if len(matches) > 0:
return
self.wordView.model().appendRow([word, word_id])
self.wordView.model().appendRow(word)
self.wordView.model().sort(0)
else:
print(f"Not active: {word_id}")
return
@pyqtSlot(int, int)
def addBlock(self, section_id, block) -> None:
new_block = QStandardItem()
new_block.setData(section_id, SessionDialog.SectionIdRole)
new_block.setData(block, SessionDialog.BlockRole)
matches = self.blocks.match(
self.blocks.createIndex(0, 0),
SessionDialog.BlockRole,
block,
1,
Qt.MatchFlag.MatchExactly,
)
if len(matches) > 0:
return
self.blocks.appendRow(new_block)
query = QSqlQuery()
query.prepare(
"SELECT * FROM sections " "WHERE section_id = :section_id"
)
query.bindValue(":section_id", section_id)
if not query.exec():
query_error(query)
if not query.next():
raise Exception(f"Section not found {section_id}")
document = QTextDocument()
cursor = QTextCursor(document)
cursor.setPosition(0)
cursor.insertHtml(query.value("content"))
textBlock = document.findBlockByNumber(block)
blockFormat = QTextBlockFormat()
blockFormat.setTextIndent(25.0)
self.textBrowser.textCursor().setBlockFormat(blockFormat)
self.textBrowser.textCursor().insertHtml('<p>'+textBlock.text()+'</p>')
self.textBrowser.textCursor().insertBlock()
return