103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
from datetime import datetime, timedelta
|
|
|
|
from PyQt6.QtCore import Qt, QTime, QTimer, pyqtSignal, pyqtSlot
|
|
from PyQt6.QtGui import QStandardItem, QStandardItemModel, QTextCursor
|
|
from PyQt6.QtSql import QSqlQuery
|
|
from PyQt6.QtWidgets import QDialog
|
|
|
|
from main import query_error
|
|
from ui.SessionDialog import Ui_Dialog
|
|
|
|
|
|
class SessionDialog(QDialog, Ui_Dialog):
|
|
timer = QTimer()
|
|
startTime = datetime.now()
|
|
totalTime = 0 # seconds
|
|
|
|
def __init__(self) -> None:
|
|
super(SessionDialog, self).__init__()
|
|
self.setupUi(self)
|
|
self.wordView.setModel(QStandardItemModel())
|
|
self.wordView.setWrapping(True)
|
|
#
|
|
# Connections
|
|
#
|
|
self.timer.timeout.connect(self.tickAction)
|
|
self.activeBox.stateChanged.connect(self.activeAction)
|
|
return
|
|
|
|
@pyqtSlot(int)
|
|
def setPerson(self, person_id) -> None:
|
|
query = QSqlQuery()
|
|
query.prepare("SELECT * FROM people " "WHERE person_id = :person_id")
|
|
query.bindValue(":person_id", person_id)
|
|
if not query.exec():
|
|
query_error(query)
|
|
if not query.next():
|
|
raise Exception(f"Bad person_id: {person_id}")
|
|
self.nameLbl.setText(query.value("name"))
|
|
self.totalTime = timedelta()
|
|
return
|
|
|
|
@pyqtSlot(int)
|
|
def activeAction(self, state: int) -> None:
|
|
if state:
|
|
self.startTime = datetime.now()
|
|
self.timer.start()
|
|
else:
|
|
self.totalTime = self.totalTime + (datetime.now() - self.startTime)
|
|
self.timer.stop()
|
|
return
|
|
|
|
@pyqtSlot()
|
|
def timerAction(self) -> None:
|
|
if self.activeBox.isChecked(): # we are stopping
|
|
self.activeBox.setChecked(False)
|
|
self.sender().setText("Start")
|
|
return
|
|
self.timer.setInterval(1000)
|
|
self.sender().setText("Stop")
|
|
self.activeBox.setChecked(True)
|
|
return
|
|
|
|
@pyqtSlot()
|
|
def tickAction(self) -> None:
|
|
delta = self.totalTime + (datetime.now() - self.startTime)
|
|
seconds = delta.seconds % 60
|
|
minutes = int(delta.seconds / 60) % 60
|
|
hours = int(delta.seconds / 3600)
|
|
self.timerLbl.setText(f"{hours:02d}:{minutes:02d}:{seconds:02d}")
|
|
return
|
|
|
|
@pyqtSlot(int)
|
|
def addWord(self, word_id: int) -> None:
|
|
if self.activeBox.isChecked():
|
|
query = QSqlQuery()
|
|
query.prepare("SELECT * FROM words " "WHERE word_id = :word_id")
|
|
query.bindValue(":word_id", word_id)
|
|
if not query.exec():
|
|
query_error(query)
|
|
if not query.next():
|
|
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
|
|
)
|
|
model = self.wordView.model()
|
|
matches = model.match(
|
|
model.createIndex(0, 1),
|
|
Qt.ItemDataRole.UserRole + 1,
|
|
query.value("word_id"),
|
|
1,
|
|
Qt.MatchFlag.MatchExactly,
|
|
)
|
|
if len(matches) > 0:
|
|
return
|
|
self.wordView.model().appendRow([word, word_id])
|
|
self.wordView.model().sort(0)
|
|
else:
|
|
print(f"Not active: {word_id}")
|
|
return
|