]>
Softwares of Agnibho - medscript.git/blob - setting.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 QDialog
, QFormLayout
, QHBoxLayout
, QVBoxLayout
, QPushButton
, QLabel
, QLineEdit
, QTextEdit
, QComboBox
, QCheckBox
, QMessageBox
, QFileDialog
, QListWidget
9 from PyQt6
. QtGui
import QIcon
10 from PyQt6
. QtCore
import Qt
, pyqtSignal
12 import logging
, os
, json
13 from prescription
import Prescriber
14 from config
import config
, config_orig
, config_file
16 class EditConfiguration ( QDialog
):
18 def select_directory ( self
):
19 d
= QFileDialog
. getExistingDirectory ( self
, "Select Directory" , config
[ "data_directory" ])
21 self
. input_directory
. setText ( d
)
22 def select_prescriber ( self
):
23 f
= QFileDialog
. getOpenFileName ( self
, "Select Prescriber" , config
[ "prescriber_directory" ], "JSON (*.json);; All Files (*)" )[ 0 ]
25 self
. input_prescriber
. setText ( f
)
27 f
= QFileDialog
. getOpenFileName ( self
, "Select Private Key" , os
. path
. expanduser ( "~" ), "PEM (*.pem);; All Files (*)" )[ 0 ]
29 self
. input_key
. setText ( f
)
30 def select_certificate ( self
):
31 f
= QFileDialog
. getOpenFileName ( self
, "Select Certificate" , os
. path
. expanduser ( "~" ), "PEM (*.pem);; All Files (*)" )[ 0 ]
33 self
. input_certificate
. setText ( f
)
34 def select_root ( self
):
35 f
= QFileDialog
. getOpenFileName ( self
, "Select Root Bundle" , os
. path
. expanduser ( "~" ), "PEM (*.pem);; All Files (*)" )[ 0 ]
37 self
. input_root
. setText ( f
)
41 self
. input_directory
. setText ( self
. config
[ "data_directory" ])
42 self
. input_prescriber
. setText ( self
. config
[ "prescriber" ])
43 self
. input_newline
. setChecked ( bool ( self
. config
[ "preset_newline" ]))
44 self
. input_delimiter
. setCurrentText ( self
. config
[ "preset_delimiter" ])
45 self
. input_agedob
. setChecked ( bool ( self
. config
[ "age_default" ]))
46 self
. input_markdown
. setChecked ( bool ( self
. config
[ "markdown" ]))
47 self
. input_update
. setChecked ( bool ( self
. config
[ "check_update" ]))
48 self
. input_form
. setChecked ( bool ( self
. config
[ "enable_form" ]))
49 self
. input_plugin
. setChecked ( bool ( self
. config
[ "enable_plugin" ]))
50 self
. input_smime
. setChecked ( bool ( self
. config
[ "smime" ]))
51 self
. input_key
. setText ( self
. config
[ "private_key" ])
52 self
. input_certificate
. setText ( self
. config
[ "certificate" ])
53 self
. input_root
. setText ( self
. config
[ "root_bundle" ])
54 except Exception as e
:
55 QMessageBox
. critical ( self
, "Failed to load" , "Failed to load the data into the application." )
59 if ( QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm Save" , "This action will overwrite the previous configuration. Continue?" )):
61 self
. config
[ "data_directory" ]= self
. input_directory
. text ()
62 self
. config
[ "prescriber" ]= self
. input_prescriber
. text ()
63 self
. config
[ "preset_newline" ]= self
. input_newline
. isChecked ()
64 self
. config
[ "preset_delimiter" ]= self
. input_delimiter
. currentText ()
65 self
. config
[ "age_default" ]= self
. input_agedob
. isChecked ()
66 self
. config
[ "markdown" ]= self
. input_markdown
. isChecked ()
67 self
. config
[ "check_update" ]= self
. input_update
. isChecked ()
68 self
. config
[ "enable_form" ]= self
. input_form
. isChecked ()
69 self
. config
[ "enable_plugin" ]= self
. input_plugin
. isChecked ()
70 self
. config
[ "smime" ]= self
. input_smime
. isChecked ()
71 self
. config
[ "private_key" ]= self
. input_key
. text ()
72 self
. config
[ "certificate" ]= self
. input_certificate
. text ()
73 self
. config
[ "root_bundle" ]= self
. input_root
. text ()
74 with
open ( config_file
, "w" ) as f
:
75 f
. write ( json
. dumps ( self
. config
, indent
= 4 ))
76 QMessageBox
. information ( self
, "Saved" , "Configuration saved. Please restart MedScript." )
78 except Exception as e
:
79 QMessageBox
. critical ( self
, "Failed to save" , "Failed to save the data to the file." )
82 def __init__ ( self
, * args
, ** kwargs
):
83 super () .__ init
__ (* args
, ** kwargs
)
86 with
open ( config_file
) as f
:
87 read
= json
. loads ( f
. read ())
88 self
. config
= config_orig | read
89 except FileNotFoundError
as e
:
91 self
. config
= config_orig
92 except Exception as e
:
94 self
. config
= config_orig
96 self
. setWindowTitle ( "Configuration Editor" )
98 layout
= QFormLayout ( self
)
99 self
. input_directory
= QLineEdit ( self
)
100 btn_directory
= QPushButton ( "Select Directory" , self
)
101 btn_directory
. clicked
. connect ( self
. select_directory
)
102 layout_directory
= QHBoxLayout ()
103 layout_directory
. addWidget ( self
. input_directory
)
104 layout_directory
. addWidget ( btn_directory
)
105 layout
. addRow ( "Data Directory" , layout_directory
)
106 self
. input_prescriber
= QLineEdit ( self
)
107 btn_prescriber
= QPushButton ( "Select File" , self
)
108 btn_prescriber
. clicked
. connect ( self
. select_prescriber
)
109 layout_prescriber
= QHBoxLayout ()
110 layout_prescriber
. addWidget ( self
. input_prescriber
)
111 layout_prescriber
. addWidget ( btn_prescriber
)
112 layout
. addRow ( "Prescriber" , layout_prescriber
)
113 self
. input_newline
= QCheckBox ( "Add newline after preset" , self
)
114 layout
. addRow ( "Preset Newline" , self
. input_newline
)
115 self
. input_delimiter
= QComboBox ( self
)
116 self
. input_delimiter
. addItems ([ "," , ";" ])
117 layout
. addRow ( "Preset Delimiter" , self
. input_delimiter
)
118 self
. input_agedob
= QCheckBox ( "Age default in new prescriptions" , self
)
119 layout
. addRow ( "Age/DOB" , self
. input_agedob
)
120 self
. input_markdown
= QCheckBox ( "Enable markdown formatting" , self
)
121 layout
. addRow ( "Markdown" , self
. input_markdown
)
122 self
. input_update
= QCheckBox ( "Check update on startup" , self
)
123 layout
. addRow ( "Check Update" , self
. input_update
)
124 self
. input_form
= QCheckBox ( "Enable custom input form" , self
)
125 layout
. addRow ( "Form" , self
. input_form
)
126 self
. input_plugin
= QCheckBox ( "Enable plugin" , self
)
127 layout
. addRow ( "Plugin" , self
. input_plugin
)
128 self
. input_smime
= QCheckBox ( "Enable digital signature (experimental)" , self
)
129 layout
. addRow ( "S/MIME" , self
. input_smime
)
130 self
. input_key
= QLineEdit ( self
)
131 btn_key
= QPushButton ( "Select File" , self
)
132 btn_key
. clicked
. connect ( self
. select_key
)
133 layout_key
= QHBoxLayout ()
134 layout_key
. addWidget ( self
. input_key
)
135 layout_key
. addWidget ( btn_key
)
136 layout
. addRow ( "Private Key" , layout_key
)
137 self
. input_certificate
= QLineEdit ( self
)
138 btn_certificate
= QPushButton ( "Select File" , self
)
139 btn_certificate
. clicked
. connect ( self
. select_certificate
)
140 layout_certificate
= QHBoxLayout ()
141 layout_certificate
. addWidget ( self
. input_certificate
)
142 layout_certificate
. addWidget ( btn_certificate
)
143 layout
. addRow ( "X509 Certificate" , layout_certificate
)
144 self
. input_root
= QLineEdit ( self
)
145 btn_root
= QPushButton ( "Select File" , self
)
146 btn_root
. clicked
. connect ( self
. select_root
)
147 layout_root
= QHBoxLayout ()
148 layout_root
. addWidget ( self
. input_root
)
149 layout_root
. addWidget ( btn_root
)
150 layout
. addRow ( "Root Bundle" , layout_root
)
151 button_save
= QPushButton ( "Save" )
152 button_save
. clicked
. connect ( self
. save
)
153 button_reset
= QPushButton ( "Reset" )
154 button_reset
. clicked
. connect ( self
. load
)
155 layout_btn
= QHBoxLayout ()
156 layout_btn
. addWidget ( button_save
)
157 layout_btn
. addWidget ( button_reset
)
158 layout
. addRow ( "" , layout_btn
)
160 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
164 class EditPrescriber ( QDialog
):
166 signal_save
= pyqtSignal ( str )
171 def load ( self
, file = None ):
176 self
. file = config
[ "prescriber" ]
177 with
open ( self
. file ) as data
:
178 self
. prescriber
= json
. loads ( data
. read ())
179 self
. input_name
. setText ( self
. prescriber
[ "name" ])
180 self
. input_qualification
. setText ( self
. prescriber
[ "qualification" ])
181 self
. input_registration
. setText ( self
. prescriber
[ "registration" ])
182 self
. input_address
. setText ( self
. prescriber
[ "address" ])
183 self
. input_contact
. setText ( self
. prescriber
[ "contact" ])
184 self
. input_extra
. setText ( self
. prescriber
[ "extra" ])
185 except Exception as e
:
186 QMessageBox
. critical ( self
, "Failed to load" , "Failed to load the data into the application." )
189 def save ( self
, file = False ):
190 if ( file is not False or QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm Save" , "This action will overwrite the previous information. Continue?" )):
194 self
. prescriber
[ "name" ]= self
. input_name
. text ()
195 self
. prescriber
[ "qualification" ]= self
. input_qualification
. text ()
196 self
. prescriber
[ "registration" ]= self
. input_registration
. text ()
197 self
. prescriber
[ "address" ]= self
. input_address
. toPlainText ()
198 self
. prescriber
[ "contact" ]= self
. input_contact
. text ()
199 self
. prescriber
[ "extra" ]= self
. input_extra
. toPlainText ()
200 with
open ( file , "w" ) as f
:
201 f
. write ( json
. dumps ( self
. prescriber
, indent
= 4 ))
202 QMessageBox
. information ( self
, "Saved" , "Information saved." )
203 self
. signal_save
. emit ( self
. file )
205 except Exception as e
:
206 QMessageBox
. critical ( self
, "Failed to save" , "Failed to save the data to the file." )
209 def __init__ ( self
, * args
, ** kwargs
):
210 super () .__ init
__ (* args
, ** kwargs
)
212 self
. setWindowTitle ( "Prescriber Editor" )
214 layout
= QFormLayout ( self
)
215 self
. input_name
= QLineEdit ( self
)
216 layout
. addRow ( "Name" , self
. input_name
)
217 self
. input_qualification
= QLineEdit ( self
)
218 layout
. addRow ( "Qualification" , self
. input_qualification
)
219 self
. input_registration
= QLineEdit ( self
)
220 layout
. addRow ( "Registration" , self
. input_registration
)
221 self
. input_address
= QTextEdit ( self
)
222 layout
. addRow ( "Adress" , self
. input_address
)
223 self
. input_contact
= QLineEdit ( self
)
224 layout
. addRow ( "Contact" , self
. input_contact
)
225 self
. input_extra
= QTextEdit ( self
)
226 layout
. addRow ( "Extra" , self
. input_extra
)
227 button_save
= QPushButton ( "Save" )
228 button_save
. clicked
. connect ( self
. save
)
229 layout
. addRow ( "" , button_save
)
231 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
235 class SelectPrescriber ( QDialog
):
237 signal_select
= pyqtSignal ( str )
238 signal_edit
= pyqtSignal ( str )
240 listAll
={ "obj" : [], "name" : [], "file" : []}
243 self
. listAll
={ "obj" : [], "name" : [], "file" : []}
245 for i
in glob ( os
. path
. join ( config
[ "prescriber_directory" ], "*.json" )):
246 self
. listAll
[ "obj" ]. append ( Prescriber ( i
))
248 self
. listAll
[ "name" ]. append ( self
. listAll
[ "obj" ][- 1 ]. name
)
249 except AttributeError :
250 del self
. listAll
[ "obj" ][- 1 ]
251 self
. listAll
[ "file" ]. append ( i
)
252 for i
in self
. listAll
[ "name" ]:
253 self
. showAll
. addItem ( i
)
256 txt
= self
. showAll
. currentItem (). text ()
257 idx
= self
. listAll
[ "name" ]. index ( txt
)
258 self
. signal_select
. emit ( self
. listAll
[ "file" ][ idx
])
263 file = os
. path
. join ( config
[ "prescriber_directory" ], "prescriber" + str ( num
)+ ".json" )
264 while ( os
. path
. exists ( file )):
266 file = os
. path
. join ( config
[ "prescriber_directory" ], "prescriber" + str ( num
)+ ".json" )
267 self
. signal_edit
. emit ( file )
271 txt
= self
. showAll
. currentItem (). text ()
272 idx
= self
. listAll
[ "name" ]. index ( txt
)
273 self
. signal_edit
. emit ( self
. listAll
[ "file" ][ idx
])
277 txt
= self
. showAll
. currentItem (). text ()
278 idx
= self
. listAll
[ "name" ]. index ( txt
)
279 file = self
. listAll
[ "file" ][ idx
]
280 if ( file == config
[ "prescriber" ]):
281 QMessageBox
. warning ( self
, "Unable to Delete" , "Default prescriber file cannot be deleted." )
283 if ( QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm Delete" , "Are you sure you want to delete the prescriber? This action cannot be undone." )):
284 os
. unlink ( self
. listAll
[ "file" ][ idx
])
285 self
. showAll
. takeItem ( self
. showAll
. currentRow ())
287 def __init__ ( self
, * args
, ** kwargs
):
288 super () .__ init
__ (* args
, ** kwargs
)
290 self
. setWindowTitle ( "Prescriber Selector" )
292 layout
= QVBoxLayout ( self
)
293 self
. showAll
= QListWidget ()
294 self
. showAll
. doubleClicked
. connect ( self
. select
)
295 layout
. addWidget ( self
. showAll
)
296 layout2
= QHBoxLayout ()
297 btn_select
= QPushButton ( "Select" )
298 btn_select
. clicked
. connect ( self
. select
)
299 layout2
. addWidget ( btn_select
)
300 btn_new
= QPushButton ( "New" )
301 btn_new
. clicked
. connect ( self
. new
)
302 layout2
. addWidget ( btn_new
)
303 btn_edit
= QPushButton ( "Edit" )
304 btn_edit
. clicked
. connect ( self
. edit
)
305 layout2
. addWidget ( btn_edit
)
306 btn_del
= QPushButton ( "Delete" )
307 btn_del
. clicked
. connect ( self
. delete
)
308 layout2
. addWidget ( btn_del
)
309 layout
. addLayout ( layout2
)
311 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))