]> Softwares of Agnibho - medscript.git/blob - index.py
Added new fields in index and tabular
[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 import logging, os, json
15
16 class Index(QMainWindow):
17
18 signal_open=pyqtSignal(str)
19 signal_copy=pyqtSignal(dict)
20 index=[]
21 proxymodel=QSortFilterProxyModel()
22
23 def __init__(self, *args, **kwargs):
24 super().__init__(*args, **kwargs)
25
26 self.setWindowTitle("MedScript")
27 self.setGeometry(200, 200, 600, 400)
28
29 widget=QWidget(self)
30 layout=QVBoxLayout(widget)
31 self.table=QTableView()
32 self.table.setSortingEnabled(True)
33 self.table.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
34 self.table.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
35 self.table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
36 layout2=QFormLayout()
37 self.input_pid=QLineEdit()
38 self.input_pid.returnPressed.connect(self.cmd_filter_pid)
39 self.input_id=QLineEdit()
40 self.input_id.returnPressed.connect(self.cmd_filter_id)
41 self.input_name=QLineEdit()
42 self.input_name.returnPressed.connect(self.cmd_filter_name)
43 layout2.addRow("Filter by PID:", self.input_pid)
44 layout2.addRow("Filter by ID:", self.input_id)
45 layout2.addRow("Filter by Name:", self.input_name)
46 layout3=QHBoxLayout()
47 button_open=QPushButton("Open Original")
48 button_open.clicked.connect(self.cmd_open)
49 button_copy=QPushButton("New Prescription")
50 button_copy.clicked.connect(self.cmd_copy)
51 layout3.addWidget(button_open)
52 layout3.addWidget(button_copy)
53 layout.addLayout(layout2)
54 layout.addLayout(layout3)
55 layout.addWidget(self.table)
56
57 self.setCentralWidget(widget)
58 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
59
60 self.refresh()
61
62 def refresh(self):
63 self.build()
64 self.load()
65
66 def cmd_filter_pid(self):
67 self.input_id.setText("")
68 self.input_name.setText("")
69 self.proxymodel.setFilterKeyColumn(0)
70 self.proxymodel.setFilterFixedString(self.input_pid.text())
71
72 def cmd_filter_id(self):
73 self.input_pid.setText("")
74 self.input_name.setText("")
75 self.proxymodel.setFilterKeyColumn(1)
76 self.proxymodel.setFilterFixedString(self.input_id.text())
77
78 def cmd_filter_name(self):
79 self.input_pid.setText("")
80 self.input_id.setText("")
81 self.proxymodel.setFilterKeyColumn(2)
82 self.proxymodel.setFilterFixedString(self.input_name.text())
83
84 def cmd_open(self):
85 try:
86 self.signal_open.emit(self.getSelectedFile())
87 self.hide()
88 except Exception as e:
89 logging.warning(e)
90
91 def cmd_copy(self):
92 try:
93 with ZipFile(self.getSelectedFile()) as zf:
94 with zf.open("prescription.json") as pf:
95 pres=json.loads(pf.read())
96 self.signal_copy.emit(pres)
97 self.hide()
98 except Exception as e:
99 logging.warning(e)
100
101
102 def getSelectedFile(self):
103 selection=self.table.selectedIndexes()
104 file=selection[-1].data()
105 return file
106
107 def build(self):
108 files=glob(os.path.join(config["document_directory"], "**", "*.mpaz"), recursive=True)
109 for file in files:
110 try:
111 with ZipFile(file) as zf:
112 try:
113 with zf.open("prescription.json") as pf:
114 pres=json.loads(pf.read())
115 self.index.append([pres["pid"], pres["id"], pres["name"], pres["dob"], pres["age"], pres["sex"], pres["date"], pres["diagnosis"], file])
116 except Exception as e:
117 logging.warning(e)
118 except Exception as e:
119 logging.warning(e)
120
121 def load(self):
122 model=QStandardItemModel()
123 model.setHorizontalHeaderLabels(["Patient ID", "Prescription ID", "Name", "Date of Birth", "Age", "Sex", "Date", "Diagnosis", "File"])
124 for item in self.index:
125 row=[]
126 for i in item:
127 row.append(QStandardItem(i))
128 model.appendRow(row)
129 self.proxymodel.setSourceModel(model)
130 self.proxymodel.setFilterCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
131 self.table.setModel(self.proxymodel)
132 self.table.resizeColumnsToContents()