]>
Softwares of Agnibho - medscript.git/blob - editpreset.py
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/>.
8 from PyQt6
. QtWidgets
import QWidget
, QMainWindow
, QVBoxLayout
, QHBoxLayout
, QPushButton
, QComboBox
, QTextEdit
, QTableView
, QMessageBox
9 from PyQt6
. QtGui
import QIcon
, QStandardItemModel
, QStandardItem
10 from PyQt6
. QtCore
import pyqtSignal
11 from config
import config
12 import logging
, os
, csv
14 class EditPreset ( QMainWindow
):
16 presetEdited
= pyqtSignal ()
18 model
= QStandardItemModel ()
20 def __init__ ( self
, * args
, ** kwargs
):
21 super () .__ init
__ (* args
, ** kwargs
)
23 self
. setWindowTitle ( "MedScript Preset Editor" )
24 self
. setGeometry ( 200 , 200 , 600 , 400 )
27 layout
= QVBoxLayout ( widget
)
28 self
. table
= QTableView ()
30 self
. input_file
= QComboBox ()
31 self
. input_file
. addItems ([ "note" , "report" , "advice" , "investigation" , "medication" , "additional" , "certificate" ])
32 self
. input_file
. currentIndexChanged
. connect ( self
. cmd_load
)
33 button_save
= QPushButton ( "Save" )
34 button_save
. clicked
. connect ( self
. cmd_save
)
35 button_row
= QPushButton ( "Add Row" )
36 button_row
. clicked
. connect ( self
. cmd_row
)
37 layout2
. addWidget ( self
. input_file
)
38 layout2
. addWidget ( button_save
)
39 layout
. addLayout ( layout2
)
40 layout
. addWidget ( self
. table
)
41 layout
. addWidget ( button_row
)
43 self
. setCentralWidget ( widget
)
44 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
54 item
= self
. input_file
. currentText ()
55 file = os
. path
. join ( config
[ "preset_directory" ], item
+ ".csv" )
56 with
open ( file , "w" , newline
= "" ) as f
:
57 writer
= csv
. writer ( f
, delimiter
= config
[ "preset_delimiter" ])
58 writer
. writerow ([ "KEY" , "VALUE" ])
59 for i
in self
. editors
:
60 row
=[ i
[ 0 ]. toPlainText (), i
[ 1 ]. toPlainText ()]
61 if row
[ 0 ]. strip ()!= "" or row
[ 1 ]. strip ()!= "" :
64 self
. presetEdited
. emit ()
66 except Exception as e
:
71 tablerow
. append ( QStandardItem ( "" ))
72 tablerow
. append ( QStandardItem ( "" ))
73 self
. model
. appendRow ( tablerow
)
74 self
. editors
. append ([ QTextEdit (), QTextEdit ()])
75 self
. table
. setIndexWidget ( self
. model
. index ( self
. model
. rowCount ()- 1 , 0 ), self
. editors
[- 1 ][ 0 ])
76 self
. table
. setIndexWidget ( self
. model
. index ( self
. model
. rowCount ()- 1 , 1 ), self
. editors
[- 1 ][ 1 ])
77 self
. table
. resizeRowsToContents ()
79 def load ( self
, file = None ):
82 item
= self
. input_file
. currentText ()
83 file = os
. path
. join ( config
[ "preset_directory" ], item
+ ".csv" )
85 self
. model
= QStandardItemModel ()
86 self
. model
. setHorizontalHeaderLabels ([ "KEY" , "VALUE" ])
87 self
. table
. setModel ( self
. model
)
89 reader
= csv
. reader ( f
, delimiter
= config
[ "preset_delimiter" ])
91 for idx
, row
in enumerate ( reader
):
101 tablerow
. append ( QStandardItem ( row
[ 0 ]))
102 tablerow
. append ( QStandardItem ( row
[ 1 ]))
103 self
. model
. appendRow ( tablerow
)
104 self
. editors
. append ([ QTextEdit (), QTextEdit ()])
105 self
. editors
[ idx
][ 0 ]. setPlainText ( row
[ 0 ])
106 self
. editors
[ idx
][ 1 ]. setPlainText ( row
[ 1 ])
107 self
. table
. setIndexWidget ( self
. model
. index ( idx
, 0 ), self
. editors
[ idx
][ 0 ])
108 self
. table
. setIndexWidget ( self
. model
. index ( idx
, 1 ), self
. editors
[ idx
][ 1 ])
109 self
. table
. horizontalHeader (). setStretchLastSection ( True )
110 self
. table
. resizeRowsToContents ()
112 except Exception as e
:
116 return QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm action" , "Unsaved changes may be lost. Continue?" )
118 def closeEvent ( self
, event
):