]> Softwares of Agnibho - medscript.git/blob - customform.py
Multiple preset files
[medscript.git] / customform.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, QFormLayout, QLineEdit, QTextEdit, QCheckBox, QDateTimeEdit, QCalendarWidget
9 from PyQt6.QtCore import QDateTime
10 from glob import glob
11 import os, json, sys, dateutil.parser
12 from config import config
13
14 class CustomForm(QWidget):
15
16 forms=[]
17 custom=[]
18 inputs=[]
19
20 def load(self):
21 for i in glob(os.path.join(config["form_directory"], "*")):
22 with open(i) as f:
23 try:
24 self.forms.append(json.loads(f.read()))
25 except Exception as e:
26 print(e)
27 for i in self.forms:
28 try:
29 for j in i["form"]:
30 self.custom.append({j["name"]: ""})
31 if("type" in j and j["type"]=="text"):
32 self.inputs.append([j["description"], QTextEdit()])
33 elif("type" in j and j["type"]=="date"):
34 d=QDateTimeEdit()
35 d.setDisplayFormat("MMMM dd, yyyy hh:mm a")
36 d.setCalendarPopup(True)
37 d.setCalendarWidget(QCalendarWidget())
38 self.inputs.append([j["description"], d])
39 elif("type" in j and j["type"]=="check"):
40 self.inputs.append([j["description"], QCheckBox()])
41 else:
42 self.inputs.append([j["description"], QLineEdit()])
43 except Exception as e:
44 raise(e)
45
46 def getData(self):
47 try:
48 for index, item in enumerate(self.custom):
49 if(isinstance(self.inputs[index][1], QLineEdit)):
50 self.custom[index][list(item)[0]]=self.inputs[index][1].text()
51 elif(isinstance(self.inputs[index][1], QTextEdit)):
52 self.custom[index][list(item)[0]]=self.inputs[index][1].toPlainText()
53 elif(isinstance(self.inputs[index][1], QCheckBox)):
54 self.custom[index][list(item)[0]]=self.inputs[index][1].isChecked()
55 elif(isinstance(self.inputs[index][1], QDateTimeEdit)):
56 self.custom[index][list(item)[0]]=self.inputs[index][1].text()
57 except Exception as e:
58 print(e)
59 return(self.custom)
60
61 def setData(self, custom=False):
62 try:
63 if(custom):
64 self.custom=custom
65 for index, item in enumerate(self.custom):
66 if(isinstance(self.inputs[index][1], QLineEdit)):
67 self.inputs[index][1].setText(self.custom[index][list(item)[0]])
68 elif(isinstance(self.inputs[index][1], QTextEdit)):
69 self.inputs[index][1].setText(self.custom[index][list(item)[0]])
70 elif(isinstance(self.inputs[index][1], QCheckBox)):
71 self.inputs[index][1].setChecked(bool(self.custom[index][list(item)[0]]))
72 elif(isinstance(self.inputs[index][1], QDateTimeEdit)):
73 pdate=dateutil.parser.parse(self.custom[index][list(item)[0]])
74 d=QDateTime.fromString(pdate.strftime("%Y-%m-%d %H:%M:%S"), "yyyy-MM-dd hh:mm:ss")
75 self.inputs[index][1].setDateTime(d)
76 except Exception as e:
77 print(e)
78
79
80 def __init__(self, *args, **kwargs):
81 super().__init__(*args, **kwargs)
82
83 if(config["enable_form"]):
84 self.load()
85
86 layout=QFormLayout(self)
87 for i in self.inputs:
88 layout.addRow(i[0], i[1])