]> Softwares of Agnibho - medscript.git/blob - customform.py
Bugfix: Windows uninstall package permission error
[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 logging, 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 logging.exception(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 KeyError as e:
44 logging.warning(e)
45 except Exception as e:
46 logging.exception(e)
47
48 def getData(self):
49 try:
50 for index, item in enumerate(self.custom):
51 if(isinstance(self.inputs[index][1], QLineEdit)):
52 self.custom[index][list(item)[0]]=self.inputs[index][1].text()
53 elif(isinstance(self.inputs[index][1], QTextEdit)):
54 self.custom[index][list(item)[0]]=self.inputs[index][1].toPlainText()
55 elif(isinstance(self.inputs[index][1], QCheckBox)):
56 self.custom[index][list(item)[0]]=self.inputs[index][1].isChecked()
57 elif(isinstance(self.inputs[index][1], QDateTimeEdit)):
58 self.custom[index][list(item)[0]]=self.inputs[index][1].text()
59 except Exception as e:
60 logging.exception(e)
61 return(self.custom)
62
63 def setData(self, custom=False):
64 try:
65 if(custom):
66 self.custom=custom
67 for index, item in enumerate(self.custom):
68 if(isinstance(self.inputs[index][1], QLineEdit)):
69 self.inputs[index][1].setText(self.custom[index][list(item)[0]])
70 elif(isinstance(self.inputs[index][1], QTextEdit)):
71 self.inputs[index][1].setText(self.custom[index][list(item)[0]])
72 elif(isinstance(self.inputs[index][1], QCheckBox)):
73 self.inputs[index][1].setChecked(bool(self.custom[index][list(item)[0]]))
74 elif(isinstance(self.inputs[index][1], QDateTimeEdit)):
75 pdate=dateutil.parser.parse(self.custom[index][list(item)[0]])
76 d=QDateTime.fromString(pdate.strftime("%Y-%m-%d %H:%M:%S"), "yyyy-MM-dd hh:mm:ss")
77 self.inputs[index][1].setDateTime(d)
78 except Exception as e:
79 logging.exception(e)
80
81
82 def __init__(self, *args, **kwargs):
83 super().__init__(*args, **kwargs)
84
85 if(config["enable_form"]):
86 self.load()
87
88 layout=QFormLayout(self)
89 for i in self.inputs:
90 layout.addRow(i[0], i[1])