]> Softwares of Agnibho - medscript.git/blob - index.py
Bugfix: save state on file open
[medscript.git] / index.py
1 # MedScript
2 # Copyright (C) 2023 Dr. Agnibho Mondal
3 # This file is part of MedScript.
4 # MedScript is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # MedScript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6 # You should have received a copy of the GNU General Public License along with MedScript. If not, see <https://www.gnu.org/licenses/>.
7
8 from PyQt6.QtWidgets import QWidget, QMainWindow, QFormLayout, QVBoxLayout, QHBoxLayout, QPushButton, QLineEdit, QTableView, QAbstractItemView
9 from PyQt6.QtGui import QIcon, QStandardItemModel, QStandardItem
10 from PyQt6.QtCore import Qt, pyqtSignal, QSortFilterProxyModel
11 from glob import glob
12 from zipfile import ZipFile
13 from config import config
14 from renderbox import UnrenderBox
15 import logging, os, json
16
17 class Index(QMainWindow):
18
19 signal_open=pyqtSignal(str)
20 signal_copy=pyqtSignal(dict)
21 index=[]
22 proxymodel=QSortFilterProxyModel()
23
24 def __init__(self, *args, **kwargs):
25 super().__init__(*args, **kwargs)
26
27 self.setWindowTitle("MedScript")
28 self.setGeometry(200, 200, 600, 400)
29
30 widget=QWidget(self)
31 layout=QVBoxLayout(widget)
32 self.table=QTableView()
33 self.table.setSortingEnabled(True)
34 self.table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
35 self.table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
36 self.table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
37 self.table.doubleClicked.connect(self.cmd_view)
38 layout2=QFormLayout()
39 self.input_pid=QLineEdit()
40 self.input_pid.returnPressed.connect(self.cmd_filter_pid)
41 self.input_id=QLineEdit()
42 self.input_id.returnPressed.connect(self.cmd_filter_id)
43 self.input_name=QLineEdit()
44 self.input_name.returnPressed.connect(self.cmd_filter_name)
45 layout2.addRow("Filter by PID:", self.input_pid)
46 layout2.addRow("Filter by ID:", self.input_id)
47 layout2.addRow("Filter by Name:", self.input_name)
48 layout3=QHBoxLayout()
49 button_view=QPushButton("View Prescription")
50 button_view.clicked.connect(self.cmd_view)
51 button_open=QPushButton("Open Original")
52 button_open.clicked.connect(self.cmd_open)
53 button_copy=QPushButton("Create Copy")
54 button_copy.clicked.connect(self.cmd_copy)
55 layout3.addWidget(button_view)
56 layout3.addWidget(button_open)
57 layout3.addWidget(button_copy)
58 layout.addLayout(layout2)
59 layout.addLayout(layout3)
60 layout.addWidget(self.table)
61
62 self.unrenderbox=UnrenderBox()
63
64 self.setCentralWidget(widget)
65 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
66
67 self.refresh()
68
69 def refresh(self):
70 self.build()
71 self.load()
72
73 def cmd_filter_pid(self):
74 self.input_id.setText("")
75 self.input_name.setText("")
76 self.proxymodel.setFilterKeyColumn(0)
77 self.proxymodel.setFilterFixedString(self.input_pid.text())
78
79 def cmd_filter_id(self):
80 self.input_pid.setText("")
81 self.input_name.setText("")
82 self.proxymodel.setFilterKeyColumn(1)
83 self.proxymodel.setFilterFixedString(self.input_id.text())
84
85 def cmd_filter_name(self):
86 self.input_pid.setText("")
87 self.input_id.setText("")
88 self.proxymodel.setFilterKeyColumn(2)
89 self.proxymodel.setFilterFixedString(self.input_name.text())
90
91 def cmd_view(self):
92 try:
93 with ZipFile(self.getSelectedFile()) as zf:
94 with zf.open("prescription.json") as pf:
95 prescription=json.loads(pf.read())
96 self.unrenderbox.show(prescription).exec()
97 except Exception as e:
98 logging.warning(e)
99
100 def cmd_open(self):
101 try:
102 self.signal_open.emit(self.getSelectedFile())
103 self.hide()
104 except Exception as e:
105 logging.warning(e)
106
107 def cmd_copy(self):
108 try:
109 with ZipFile(self.getSelectedFile()) as zf:
110 with zf.open("prescription.json") as pf:
111 pres=json.loads(pf.read())
112 self.signal_copy.emit(pres)
113 self.hide()
114 except Exception as e:
115 logging.warning(e)
116
117
118 def getSelectedFile(self):
119 selection=self.table.selectedIndexes()
120 file=selection[-1].data()
121 return file
122
123 def build(self):
124 files=glob(os.path.join(config["document_directory"], "**", "*.mpaz"), recursive=True)
125 self.index=[]
126 for file in files:
127 try:
128 with ZipFile(file) as zf:
129 try:
130 with zf.open("prescription.json") as pf:
131 pres=json.loads(pf.read())
132 self.index.append([pres["pid"], pres["id"], pres["name"], pres["dob"], pres["age"], pres["sex"], pres["date"], pres["diagnosis"], file])
133 except Exception as e:
134 logging.warning(e)
135 except Exception as e:
136 logging.warning(e)
137
138 def load(self):
139 model=QStandardItemModel()
140 model.setHorizontalHeaderLabels(["Patient ID", "Prescription ID", "Name", "Date of Birth", "Age", "Sex", "Date", "Diagnosis", "File"])
141 for item in self.index:
142 row=[]
143 for i in item:
144 row.append(QStandardItem(i))
145 model.appendRow(row)
146 self.proxymodel.setSourceModel(model)
147 self.proxymodel.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
148 self.table.setModel(self.proxymodel)
149 self.table.resizeColumnsToContents()