]> Softwares of Agnibho - medscript.git/blob - index.py
Allowed custom properties in prescriprion, prescriber
[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 pyqtSignal, QSortFilterProxyModel
11 from glob import glob
12 from zipfile import ZipFile
13 from config import config
14 import 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_id=QLineEdit()
38 self.input_id.returnPressed.connect(self.cmd_filter_id)
39 self.input_name=QLineEdit()
40 self.input_name.returnPressed.connect(self.cmd_filter_name)
41 layout2.addRow("Filter by ID:", self.input_id)
42 layout2.addRow("Filter by Name:", self.input_name)
43 layout3=QHBoxLayout()
44 button_open=QPushButton("Open Original")
45 button_open.clicked.connect(self.cmd_open)
46 button_copy=QPushButton("New Prescription")
47 button_copy.clicked.connect(self.cmd_copy)
48 layout3.addWidget(button_open)
49 layout3.addWidget(button_copy)
50 layout.addLayout(layout2)
51 layout.addLayout(layout3)
52 layout.addWidget(self.table)
53
54 self.setCentralWidget(widget)
55 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
56
57 self.refresh()
58
59 def refresh(self):
60 self.build()
61 self.load()
62
63 def cmd_filter_id(self):
64 self.input_name.setText("")
65 self.proxymodel.setFilterKeyColumn(0)
66 self.proxymodel.setFilterFixedString(self.input_id.text())
67
68 def cmd_filter_name(self):
69 self.input_id.setText("")
70 self.proxymodel.setFilterKeyColumn(1)
71 self.proxymodel.setFilterFixedString(self.input_name.text())
72
73 def cmd_open(self):
74 try:
75 self.signal_open.emit(self.getSelectedFile())
76 self.hide()
77 except Exception as e:
78 print(e)
79
80 def cmd_copy(self):
81 try:
82 with ZipFile(self.getSelectedFile()) as zf:
83 with zf.open("prescription.json") as pf:
84 pres=json.loads(pf.read())
85 self.signal_copy.emit(pres)
86 self.hide()
87 except Exception as e:
88 print(e)
89
90
91 def getSelectedFile(self):
92 selection=self.table.selectedIndexes()
93 file=selection[-1].data()
94 return file
95
96 def build(self):
97 files=glob(os.path.join(config["document_directory"], "**", "*.mpaz"), recursive=True)
98 for file in files:
99 with ZipFile(file) as zf:
100 with zf.open("prescription.json") as pf:
101 pres=json.loads(pf.read())
102 self.index.append([pres["id"], pres["name"], pres["age"], pres["sex"], pres["date"], file])
103
104 def load(self):
105 model=QStandardItemModel()
106 model.setHorizontalHeaderLabels(["ID", "Name", "Age", "Sex", "Date", "File"])
107 for item in self.index:
108 row=[]
109 for i in item:
110 row.append(QStandardItem(i))
111 model.appendRow(row)
112 self.proxymodel.setSourceModel(model)
113 self.table.setModel(self.proxymodel)
114 self.table.resizeColumnsToContents()