Mostly working Reader

This commit is contained in:
Christopher T. Johnson
2023-11-14 10:43:50 -05:00
parent 598201425c
commit 0b02ed2201
5 changed files with 374 additions and 147 deletions

140
main.py
View File

@@ -5,6 +5,7 @@ import sys
from PyQt6.QtCore import Qt, pyqtSlot
from PyQt6.QtGui import (
QAction,
QFont,
QTextCharFormat,
QTextCursor,
@@ -33,8 +34,6 @@ def query_error(query: QSqlQuery) -> None:
class MainWindow(QMainWindow, Ui_MainWindow):
book_id = 0
def __init__(self) -> None:
super(MainWindow, self).__init__()
self.setupUi(self)
@@ -47,64 +46,77 @@ class MainWindow(QMainWindow, Ui_MainWindow):
# self.wordButton.clicked.connect(self.wordAction)
self.ReadButton.clicked.connect(self.readAction)
self.bookBtn.clicked.connect(self.bookAction)
self.createActions()
self.show()
return
def createActions(self):
query = QSqlQuery()
query.prepare("SELECT * FROM books ORDER BY title")
if not query.exec():
query_error(query)
while query.next():
action = QAction(query.value("title"), self)
action.setData(query.value("book_id"))
action.triggered.connect(self.setBookAction)
self.menuBooks.addAction(action)
return
@pyqtSlot()
def setBookAction(self):
action = self.sender()
print(action)
book_id = action.data()
print(book_id)
indexes = self.peopleView.selectedIndexes()
if len(indexes) < 1:
return
person_id = indexes[0].siblingAtColumn(0).data()
print(person_id)
query = QSqlQuery()
query.prepare(
"UPDATE people SET book_id = :book_id " "WHERE person_id = :person_id"
)
query.bindValue(":book_id", book_id)
query.bindValue(":person_id", person_id)
if not query.exec():
query_error(query)
query.prepare(
"SELECT * FROM person_book "
"WHERE person_id = :person_id "
"AND book_id = :book_id"
)
query.bindValue(":person_id", person_id)
query.bindValue(":book_id", book_id)
if not query.exec():
query_error(query)
if query.next():
return
query.prepare(
"SELECT * FROM sections WHERE sequence = 0 " "AND book_id = :book_id"
)
query.bindValue(":book_id", book_id)
if not query.exec():
query_error(query)
if not query.next():
raise Exception(f"book_id: {book_id} has no section 0!")
section_id = query.value("section_id")
query.prepare(
"INSERT INTO person_book " "VALUES (:person_id, :book_id, :section_id, 0)"
)
query.bindValue(":person_id", person_id)
query.bindValue(":book_id", book_id)
query.bindValue(":section_id", section_id)
if not query.exec():
query_error(query)
return
@pyqtSlot()
def bookAction(self) -> None:
directory = QFileDialog.getExistingDirectory()
book = Book(directory)
self.book_id = self.store_book(book)
self.book = Book(directory)
return
def store_book(self, book: Book) -> int:
uuid = book.metadata["identifier"]
query = QSqlQuery()
query.prepare(
"SELECT COUNT(*) AS number, book_id FROM books b " "WHERE b.uuid = :uuid"
)
query.bindValue(":uuid", uuid)
if not query.exec():
query_error(query)
query.next()
if query.value("number") > 0:
book_id: int = query.value("book_id")
return book_id
query.prepare(
"INSERT INTO books (title, author, uuid, level) VALUES ("
":title, :author, :uuid, 0)"
)
query.bindValue(":title", book.metadata["title"])
query.bindValue(":author", book.metadata["creator"])
query.bindValue(":uuid", uuid)
if not query.exec():
query_error(query)
book_id = query.lastInsertId()
section_query = QSqlQuery()
section_query.prepare(
"INSERT INTO sections (title, sequence, book_id) "
"VALUES (:title, :sequence, :book_id)"
)
section_query.bindValue(":book_id", book_id)
para_query = QSqlQuery()
para_query.prepare(
"INSERT INTO paragraphs (section_id, sequence, content) "
"VALUES (:section_id, :sequence, :content)"
)
for seq, section in enumerate(book.sections):
section_query.bindValue(":title", section["title"])
section_query.bindValue(":sequence", seq)
if not section_query.exec():
query_error(section_query)
section_id = query.lastInsertId()
para_query.bindValue(":section_id", section_id)
for ps, paragraph in enumerate(section["paragraphs"]):
para_query.bindValue(":sequence", ps)
para_query.bindValue(":content", paragraph)
if not para_query.exec():
query_error(para_query)
return book_id
def load_definition(self, word: str, definition: dict) -> None:
document = None # self.textEdit.document()
myCursor = QTextCursor(document)
@@ -144,9 +156,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
if len(indexes) < 1:
return
person_id = indexes[0].siblingAtColumn(0).data()
name = indexes[0].data()
print(person_id, name)
dlg = EditDialog(self.book_id, person_id)
dlg = EditDialog(person_id)
dlg.exec()
return
@@ -161,24 +171,20 @@ SQL_CMDS = [
"uuid TEXT, level INTEGER)",
"CREATE TABLE IF NOT EXISTS sections "
"(section_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"title TEXT, sequence INTEGER, "
"sequence INTEGER, content TEXT, "
"book_id INTEGER REFERENCES books ON DELETE CASCADE) ",
"CREATE TABLE IF NOT EXISTS paragraphs "
"(paragraph_id INTEGER PRIMARY KEY AUTOINCREMENT, "
"section_id INTEGER REFERENCES sections ON DELETE CASCADE, "
"sequence INTEGER NOT NULL DEFAULT 0, "
"content TEXT)",
"CREATE TABLE IF NOT EXISTS people "
"(person_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, "
"organization TEXT, "
"paragraph_id INTEGER REFERENCES paragraphs ON DELETE CASCADE)",
"organization TEXT, book_id INTEGER REFERENCES books ON DELETE CASCADE) ",
"CREATE TABLE IF NOT EXISTS person_book "
"(person_id INTEGER REFERENCES people ON DELETE CASCADE, "
"book_id INTEGER REFERENCES books ON DELETE CASCADE)",
"CREATE TABLE IF NOT EXISTS word_paragraph "
"book_id INTEGER REFERENCES books ON DELETE CASCADE, "
"section_id INTEGER REFERENCES sections, "
"block INTEGER)",
"CREATE TABLE IF NOT EXISTS word_block "
"(word_id INTEGER REFERENCES words ON DELETE CASCADE, "
"paragraph_id INTEGER REFERENCES paragraphs ON DELETE CASCADE, "
"start INTEGER NOT NULL, end INTEGER NOT NULL)",
"section_id INTEGER REFERENCES sections ON DELETE CASCADE, "
"block INTEGER NOT NULL, start INTEGER NOT NULL, end INTEGER NOT NULL)",
]