]> Softwares of Agnibho - medscript.git/blob - editpreset.py
Option to preserve template
[medscript.git] / editpreset.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, QVBoxLayout, QHBoxLayout, QPushButton, QComboBox, QTextEdit, QTableView, QMessageBox
9 from PyQt6.QtGui import QIcon, QStandardItemModel, QStandardItem
10 from config import config
11 import os, csv
12
13 class EditPreset(QMainWindow):
14
15 editors=[]
16 model=QStandardItemModel()
17
18 def __init__(self, *args, **kwargs):
19 super().__init__(*args, **kwargs)
20
21 self.setWindowTitle("MedScript")
22 self.setGeometry(200, 200, 600, 400)
23
24 widget=QWidget(self)
25 layout=QVBoxLayout(widget)
26 self.table=QTableView()
27 layout2=QHBoxLayout()
28 self.input_file=QComboBox()
29 self.input_file.addItems(["note", "report", "advice", "investigation", "medication", "additional", "certify"])
30 self.input_file.currentIndexChanged.connect(self.cmd_load)
31 button_save=QPushButton("Save")
32 button_save.clicked.connect(self.cmd_save)
33 button_row=QPushButton("Add Row")
34 button_row.clicked.connect(self.cmd_row)
35 layout2.addWidget(self.input_file)
36 layout2.addWidget(button_save)
37 layout.addLayout(layout2)
38 layout.addWidget(self.table)
39 layout.addWidget(button_row)
40
41 self.setCentralWidget(widget)
42 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
43
44 self.load()
45
46 def cmd_load(self):
47 if self.confirm():
48 self.load()
49
50 def cmd_save(self):
51 try:
52 item=self.input_file.currentText()
53 file=os.path.join(config["preset_directory"], item+".csv")
54 with open(file, "w") as f:
55 writer=csv.writer(f, delimiter=config["preset_delimiter"])
56 writer.writerow(["KEY", "VALUE"])
57 for i in self.editors:
58 row=[i[0].toPlainText(), i[1].toPlainText()]
59 if row[0].strip()!="" or row[1].strip()!="":
60 writer.writerow(row)
61 self.load(file)
62 QMessageBox.information(self,"File saved", "Changes saved. Please restart the program.")
63 except Exception as e:
64 print(e)
65
66 def cmd_row(self):
67 tablerow=[]
68 tablerow.append(QStandardItem(""))
69 tablerow.append(QStandardItem(""))
70 self.model.appendRow(tablerow)
71 self.editors.append([QTextEdit(), QTextEdit()])
72 self.table.setIndexWidget(self.model.index(self.model.rowCount()-1,0), self.editors[-1][0])
73 self.table.setIndexWidget(self.model.index(self.model.rowCount()-1,1), self.editors[-1][1])
74 self.table.resizeRowsToContents()
75
76 def load(self, file=None):
77 try:
78 if file is None:
79 item=self.input_file.currentText()
80 file=os.path.join(config["preset_directory"], item+".csv")
81 self.editors=[]
82 self.model=QStandardItemModel()
83 self.model.setHorizontalHeaderLabels(["KEY", "VALUE"])
84 self.table.setModel(self.model)
85 with open(file) as f:
86 reader=csv.reader(f, delimiter=config["preset_delimiter"])
87 next(reader)
88 for idx,row in enumerate(reader):
89 tablerow=[]
90 tablerow.append(QStandardItem(row[0]))
91 tablerow.append(QStandardItem(row[1]))
92 self.model.appendRow(tablerow)
93 self.editors.append([QTextEdit(), QTextEdit()])
94 self.editors[idx][0].setPlainText(row[0])
95 self.editors[idx][1].setPlainText(row[1])
96 self.table.setIndexWidget(self.model.index(idx,0), self.editors[idx][0])
97 self.table.setIndexWidget(self.model.index(idx,1), self.editors[idx][1])
98 self.table.horizontalHeader().setStretchLastSection(True)
99 self.table.resizeRowsToContents()
100 textedit=QTextEdit()
101 except Exception as e:
102 print(e)
103
104 def confirm(self):
105 return QMessageBox.StandardButton.Yes==QMessageBox.question(self,"Confirm action", "Unsaved changes may be lost. Continue?")
106
107 def closeEvent(self, event):
108 if self.confirm():
109 event.accept()
110 else:
111 event.ignore()