]>
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 config
import config
13 class EditPreset ( QMainWindow
):
16 model
= QStandardItemModel ()
18 def __init__ ( self
, * args
, ** kwargs
):
19 super () .__ init
__ (* args
, ** kwargs
)
21 self
. setWindowTitle ( "MedScript" )
22 self
. setGeometry ( 200 , 200 , 600 , 400 )
25 layout
= QVBoxLayout ( widget
)
26 self
. table
= QTableView ()
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
)
41 self
. setCentralWidget ( widget
)
42 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
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 ()!= "" :
62 QMessageBox
. information ( self
, "File saved" , "Changes saved. Please restart the program." )
63 except Exception as e
:
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 ()
76 def load ( self
, file = None ):
79 item
= self
. input_file
. currentText ()
80 file = os
. path
. join ( config
[ "preset_directory" ], item
+ ".csv" )
82 self
. model
= QStandardItemModel ()
83 self
. model
. setHorizontalHeaderLabels ([ "KEY" , "VALUE" ])
84 self
. table
. setModel ( self
. model
)
86 reader
= csv
. reader ( f
, delimiter
= config
[ "preset_delimiter" ])
88 for idx
, row
in enumerate ( reader
):
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 ()
101 except Exception as e
:
105 return QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm action" , "Unsaved changes may be lost. Continue?" )
107 def closeEvent ( self
, event
):