234 lines
7.0 KiB
Python
Executable File
234 lines
7.0 KiB
Python
Executable File
#!venv/bin/python3
|
|
import datetime
|
|
import re
|
|
import sys
|
|
from typing import Any, cast
|
|
|
|
from PySide6.QtCore import (
|
|
QCoreApplication,
|
|
QModelIndex,
|
|
QPersistentModelIndex,
|
|
QRect,
|
|
QSize,
|
|
Signal,
|
|
Slot,
|
|
)
|
|
from PySide6.QtGui import QColor, QPainter, QTextDocument
|
|
from PySide6.QtSql import (
|
|
QSqlDatabase,
|
|
QSqlQuery,
|
|
QSqlTableModel,
|
|
)
|
|
from PySide6.QtWidgets import (
|
|
QAbstractItemView,
|
|
QApplication,
|
|
QHeaderView,
|
|
QMainWindow,
|
|
QStyle,
|
|
QStyledItemDelegate,
|
|
QStyleOptionViewItem,
|
|
QWidget,
|
|
)
|
|
|
|
from docketModel import docketModel
|
|
from lib.utils import query_error
|
|
from ui.MainWindow import Ui_MainWindow
|
|
from workers import loadCases, updateThread
|
|
|
|
translate = QCoreApplication.translate
|
|
|
|
|
|
class dateDelegate(QStyledItemDelegate):
|
|
def displayText(self, value: int, _: Any) -> str:
|
|
date = datetime.date.fromtimestamp(value)
|
|
return date.strftime("%B %-d, %Y")
|
|
|
|
|
|
class QStyleOptionViewItemInit(QStyleOptionViewItem):
|
|
backgroundBrush: QColor
|
|
rect: QRect
|
|
widget: QWidget
|
|
text: str
|
|
|
|
|
|
class activeDelegate(QStyledItemDelegate):
|
|
def initStyleOption(
|
|
self,
|
|
option: QStyleOptionViewItem,
|
|
index: QModelIndex | QPersistentModelIndex,
|
|
/,
|
|
) -> None:
|
|
options = cast(QStyleOptionViewItemInit, option)
|
|
super().initStyleOption(options, index)
|
|
assert isinstance(index, QModelIndex)
|
|
if index.siblingAtColumn(6).data() == 0:
|
|
options.backgroundBrush = QColor(0x444444)
|
|
return
|
|
|
|
|
|
class documentDelegate(QStyledItemDelegate):
|
|
def initStyleOption(
|
|
self,
|
|
option: QStyleOptionViewItem,
|
|
index: QModelIndex | QPersistentModelIndex,
|
|
/,
|
|
) -> None:
|
|
options = cast(QStyleOptionViewItemInit, option)
|
|
super().initStyleOption(options, index)
|
|
assert index.isValid() and isinstance(index, QModelIndex)
|
|
if index.siblingAtColumn(6).data() == 0:
|
|
options.backgroundBrush = QColor(0x444444)
|
|
return
|
|
|
|
def paint(
|
|
self,
|
|
painter: QPainter,
|
|
option: QStyleOptionViewItem,
|
|
index: QModelIndex | QPersistentModelIndex,
|
|
) -> None:
|
|
options = cast(QStyleOptionViewItemInit, option)
|
|
self.initStyleOption(options, index)
|
|
painter.save()
|
|
doc = QTextDocument()
|
|
doc.setTextWidth(options.rect.width())
|
|
doc.setHtml(options.text)
|
|
options.text = ""
|
|
options.widget.style().drawControl(
|
|
QStyle.ControlElement.CE_ItemViewItem,
|
|
options,
|
|
painter,
|
|
)
|
|
painter.translate(options.rect.left(), options.rect.top())
|
|
clip = QRect(0, 0, options.rect.width(), options.rect.height())
|
|
doc.drawContents(painter, clip)
|
|
painter.restore()
|
|
return
|
|
|
|
def sizeHint(
|
|
self,
|
|
option: QStyleOptionViewItem,
|
|
index: QModelIndex | QPersistentModelIndex,
|
|
) -> QSize:
|
|
options = cast(QStyleOptionViewItemInit, option)
|
|
self.initStyleOption(options, index)
|
|
doc = QTextDocument()
|
|
doc.setTextWidth(options.rect.width())
|
|
doc.setHtml(options.text)
|
|
doc.setTextWidth(options.rect.width())
|
|
return QSize(int(doc.idealWidth()), int(doc.size().height()))
|
|
|
|
|
|
class MainWindow(QMainWindow, Ui_MainWindow):
|
|
show_entries = Signal(int)
|
|
|
|
loadThread = None
|
|
|
|
def __init__(self) -> None:
|
|
super(MainWindow, self).__init__()
|
|
self.setupUi(self)
|
|
self.loadThread = loadCases()
|
|
self.loadThread.finished.connect(self.updateDone)
|
|
self.loadThread.start()
|
|
|
|
# model = QSqlQueryModel()
|
|
model = QSqlTableModel()
|
|
query = QSqlQuery(
|
|
"SELECT * FROM cases ORDER BY SUBSTR(docket_id,1,3), "
|
|
"CAST(SUBSTR(docket_id,4) AS INT)"
|
|
)
|
|
if not query.exec():
|
|
query_error(query)
|
|
model.setQuery(query)
|
|
self.casesView.setModel(model)
|
|
|
|
self.casesView.setSelectionMode(
|
|
QAbstractItemView.SelectionMode.SingleSelection
|
|
)
|
|
self.casesView.setSelectionBehavior(
|
|
QAbstractItemView.SelectionBehavior.SelectRows
|
|
)
|
|
self.casesView.hideColumn(0)
|
|
self.casesView.hideColumn(2)
|
|
self.casesView.setItemDelegateForColumn(5, dateDelegate())
|
|
self.casesView.setItemDelegate(activeDelegate())
|
|
self.casesView.resizeColumnToContents(1)
|
|
self.casesView.resizeColumnToContents(5)
|
|
header = self.casesView.horizontalHeader()
|
|
header.setSectionResizeMode(3, QHeaderView.ResizeMode.Fixed)
|
|
header.setSectionResizeMode(4, QHeaderView.ResizeMode.Fixed)
|
|
self.show()
|
|
remaining = (
|
|
self.casesView.width()
|
|
- header.sectionSize(1)
|
|
- header.sectionSize(5)
|
|
- 5
|
|
)
|
|
self.casesView.setColumnWidth(3, int(remaining * 0.5))
|
|
self.casesView.setColumnWidth(4, int(remaining * 0.5))
|
|
self.casesView.verticalHeader().hide()
|
|
self.casesView.resizeRowsToContents()
|
|
self.casesView.doubleClicked.connect(self.rowClicked)
|
|
self.casesView.clicked.connect(self.rowClicked)
|
|
|
|
self.docketView.setModel(docketModel())
|
|
self.docketView.horizontalHeader().setSectionResizeMode(
|
|
1, QHeaderView.ResizeMode.Stretch
|
|
)
|
|
self.docketView.resizeRowsToContents()
|
|
self.docketView.setItemDelegateForColumn(1, documentDelegate())
|
|
return
|
|
|
|
@Slot(QModelIndex) # type: ignore
|
|
def rowClicked(self, index: QModelIndex) -> None:
|
|
if not index.isValid():
|
|
raise Exception("Bad index")
|
|
docket = index.siblingAtColumn(1).data()
|
|
self.docketLabel.setText(docket)
|
|
self.show_entries.emit(index.siblingAtColumn(0).data())
|
|
model = docketModel(index.siblingAtColumn(0).data())
|
|
self.docketView.setModel(model)
|
|
self.docketView.resizeColumnToContents(0)
|
|
self.docketView.resizeRowsToContents()
|
|
return
|
|
|
|
updateThread = None
|
|
|
|
@Slot()
|
|
def on_updateButton_clicked(self) -> None:
|
|
text = self.docketInput.toPlainText()
|
|
print(f"on_updateButton_clicked(): {text}")
|
|
if not self.updateThread:
|
|
self.updateThread = updateThread()
|
|
assert isinstance(self.updateThread, updateThread)
|
|
self.updateThread.finished.connect(self.updateDone)
|
|
self.updateThread.setDocketId(text)
|
|
self.updateThread.start()
|
|
return
|
|
|
|
@Slot()
|
|
def updateDone(self) -> None:
|
|
self.updateThread = None
|
|
print("Done updating")
|
|
model: QSqlTableModel = cast(QSqlTableModel, self.casesView.model())
|
|
query = model.query()
|
|
query.exec()
|
|
model.setQuery(query)
|
|
return
|
|
|
|
def main() -> int:
|
|
#
|
|
app = QApplication(sys.argv)
|
|
db = QSqlDatabase.addDatabase("QMYSQL")
|
|
db.setHostName("ceph5")
|
|
db.setDatabaseName("scotus")
|
|
db.setUserName("scotus")
|
|
db.setPassword("lechOtvirf8Om/")
|
|
db.open()
|
|
import lib.dbConfig
|
|
window = MainWindow()
|
|
return app.exec()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|