From: Agnibho Mondal Date: Fri, 27 Oct 2023 21:27:45 +0000 (+0530) Subject: Added preset editor X-Git-Tag: v0.4~11 X-Git-Url: https://code.agnibho.com/repo?a=commitdiff_plain;h=a2ad9226f3c4204ac65b435dff30671fc832a8ff;p=medscript.git Added preset editor --- diff --git a/README b/README index bc8754b..36adf6d 100644 --- a/README +++ b/README @@ -174,13 +174,25 @@ need to type them. To use this feature the text must be entered beforehand to the respective files. These files are in the .csv format and can be edited with any spreadsheet editing software. +There is a preset editor included with this software as well. This editor can +be accessed by going to the setting menu and selecting the preset option. + +The preset editor contains a drop down input at the top which can be used to +select the respective file to edit. There is also a save button at the top +which can be used to save the changes to the selected file. + +The data is presented in an editable table which can be used to edit the data +in the selected file. While editing, the KEY/VALUE format is to be used as +shown in the top row. The add row button at the bottom can be used to add a +new row for entry of new data. + The preset files are kept in the preset directory. The files associated with medical certificate, clinical note, report, advice, investigation, medication and additional are certify.csv, note.csv, report.csv, advice.csv, investigation.csv, medication.csv and additional.csv. Each file contains a top row indicating the variables in the columns. The data is entered in two columns with each row containing a KEY and a TEXT. The TEXT can be entered in -to the edit area by selecting the KEY from the preset input. Blank files with +the edit area by selecting the KEY from the preset input. Blank files with only the top row are included with the program. Configuration diff --git a/editpreset.py b/editpreset.py new file mode 100644 index 0000000..84e0630 --- /dev/null +++ b/editpreset.py @@ -0,0 +1,111 @@ +# MedScript +# Copyright (C) 2023 Dr. Agnibho Mondal +# This file is part of MedScript. +# 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. +# 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. +# You should have received a copy of the GNU General Public License along with MedScript. If not, see . + +from PyQt6.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QHBoxLayout, QPushButton, QComboBox, QTextEdit, QTableView, QMessageBox +from PyQt6.QtGui import QIcon, QStandardItemModel, QStandardItem +from config import config +import os, csv + +class EditPreset(QMainWindow): + + editors=[] + model=QStandardItemModel() + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.setWindowTitle("MedScript") + self.setGeometry(200, 200, 600, 400) + + widget=QWidget(self) + layout=QVBoxLayout(widget) + self.table=QTableView() + layout2=QHBoxLayout() + self.input_file=QComboBox() + self.input_file.addItems(["note", "report", "advice", "investigation", "medication", "additional", "certify"]) + self.input_file.currentIndexChanged.connect(self.cmd_load) + button_save=QPushButton("Save") + button_save.clicked.connect(self.cmd_save) + button_row=QPushButton("Add Row") + button_row.clicked.connect(self.cmd_row) + layout2.addWidget(self.input_file) + layout2.addWidget(button_save) + layout.addLayout(layout2) + layout.addWidget(self.table) + layout.addWidget(button_row) + + self.setCentralWidget(widget) + self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico"))) + + self.load() + + def cmd_load(self): + if self.confirm(): + self.load() + + def cmd_save(self): + try: + item=self.input_file.currentText() + file=os.path.join(config["preset_directory"], item+".csv") + with open(file, "w") as f: + writer=csv.writer(f, delimiter=config["preset_delimiter"]) + writer.writerow(["KEY", "VALUE"]) + for i in self.editors: + row=[i[0].toPlainText(), i[1].toPlainText()] + if row[0].strip()!="" or row[1].strip()!="": + writer.writerow(row) + self.load(file) + QMessageBox.information(self,"File saved", "Changes saved. Please restart the program.") + except Exception as e: + print(e) + + def cmd_row(self): + tablerow=[] + tablerow.append(QStandardItem("")) + tablerow.append(QStandardItem("")) + self.model.appendRow(tablerow) + self.editors.append([QTextEdit(), QTextEdit()]) + self.table.setIndexWidget(self.model.index(self.model.rowCount()-1,0), self.editors[-1][0]) + self.table.setIndexWidget(self.model.index(self.model.rowCount()-1,1), self.editors[-1][1]) + self.table.resizeRowsToContents() + + def load(self, file=None): + try: + if file is None: + item=self.input_file.currentText() + file=os.path.join(config["preset_directory"], item+".csv") + self.editors=[] + self.model=QStandardItemModel() + self.model.setHorizontalHeaderLabels(["KEY", "VALUE"]) + self.table.setModel(self.model) + with open(file) as f: + reader=csv.reader(f, delimiter=config["preset_delimiter"]) + next(reader) + for idx,row in enumerate(reader): + tablerow=[] + tablerow.append(QStandardItem(row[0])) + tablerow.append(QStandardItem(row[1])) + self.model.appendRow(tablerow) + self.editors.append([QTextEdit(), QTextEdit()]) + self.editors[idx][0].setPlainText(row[0]) + self.editors[idx][1].setPlainText(row[1]) + self.table.setIndexWidget(self.model.index(idx,0), self.editors[idx][0]) + self.table.setIndexWidget(self.model.index(idx,1), self.editors[idx][1]) + self.table.horizontalHeader().setStretchLastSection(True) + self.table.resizeRowsToContents() + textedit=QTextEdit() + except Exception as e: + print(e) + + def confirm(self): + return QMessageBox.StandardButton.Yes==QMessageBox.question(self,"Confirm action", "Unsaved changes may be lost. Continue?") + + def closeEvent(self, event): + if self.confirm(): + event.accept() + else: + event.ignore() diff --git a/preset.py b/preset.py index 33fbdc3..13a9088 100644 --- a/preset.py +++ b/preset.py @@ -34,3 +34,7 @@ class Preset(): print(self.file, e) except IndexError as e: print(self.file, e) + except StopIteration as e: + print(self.file, e, ": Check if file is empty") + except Exception as e: + print(self.file, e) diff --git a/window.py b/window.py index 63d6171..d85d9ee 100644 --- a/window.py +++ b/window.py @@ -21,6 +21,7 @@ from renderer import Renderer from filehandler import FileHandler from renderbox import RenderBox from setting import EditConfiguration, EditPrescriber +from editpreset import EditPreset from viewbox import ViewBox from preset import Preset from tabular import Tabular @@ -212,6 +213,9 @@ class MainWindow(QMainWindow): except FileNotFoundError as e: print(e) + def cmd_preset(self): + self.edit_preset.show() + def cmd_about(self): year=datetime.datetime.now().year if(year>2023): @@ -512,6 +516,8 @@ class MainWindow(QMainWindow): action_prescriber.triggered.connect(self.cmd_prescriber) action_switch=QAction("Switch", self) action_switch.triggered.connect(self.cmd_switch) + action_preset=QAction("Preset", self) + action_preset.triggered.connect(self.cmd_preset) action_tabular=QAction("Tabular", self) action_tabular.triggered.connect(self.cmd_tabular) action_index=QAction("Index", self) @@ -542,6 +548,7 @@ class MainWindow(QMainWindow): menu_settings.addAction(action_configuration) menu_settings.addAction(action_prescriber) menu_settings.addAction(action_switch) + menu_settings.addAction(action_preset) menu_data=menubar.addMenu("Data") menu_data.addAction(action_index) menu_data.addAction(action_tabular) @@ -798,6 +805,7 @@ class MainWindow(QMainWindow): self.edit_prescriber.signal_save.connect(self.cmd_prescriber_reload) self.viewbox=ViewBox() self.index=Index() + self.edit_preset=EditPreset() self.index.signal_open.connect(self.cmd_open) self.index.signal_copy.connect(self.cmd_copy) self.signal_update.connect(self.show_update)