]>
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 QWidget
, QMainWindow
, QFormLayout
, QHBoxLayout
, QPushButton
, QLabel
, QLineEdit
, QTextEdit
, QComboBox
, QCheckBox
, QStatusBar
, QMessageBox
, QFileDialog
9 from PyQt6
. QtGui
import QIcon
10 from PyQt6
. QtCore
import pyqtSignal
12 from config
import config
, config_file
14 class EditConfiguration ( QMainWindow
):
16 def select_directory ( self
):
17 d
= QFileDialog
. getExistingDirectory ( self
, "Select Directory" , config
[ "data_directory" ])
19 self
. input_directory
. setText ( d
)
20 def select_prescriber ( self
):
21 f
= QFileDialog
. getOpenFileName ( self
, "Select Prescriber" , config
[ "prescriber_directory" ], "JSON (*.json);; All Files (*)" )[ 0 ]
23 self
. input_prescriber
. setText ( f
)
25 f
= QFileDialog
. getOpenFileName ( self
, "Select Private Key" , os
. path
. expanduser ( "~" ), "PEM (*.pem);; All Files (*)" )[ 0 ]
27 self
. input_key
. setText ( f
)
28 def select_certificate ( self
):
29 f
= QFileDialog
. getOpenFileName ( self
, "Select Certificate" , os
. path
. expanduser ( "~" ), "PEM (*.pem);; All Files (*)" )[ 0 ]
31 self
. input_certificate
. setText ( f
)
32 def select_root ( self
):
33 f
= QFileDialog
. getOpenFileName ( self
, "Select Root Bundle" , os
. path
. expanduser ( "~" ), "PEM (*.pem);; All Files (*)" )[ 0 ]
35 self
. input_root
. setText ( f
)
39 self
. statusbar
. showMessage ( config_file
)
40 self
. input_directory
. setText ( self
. config
[ "data_directory" ])
41 self
. input_prescriber
. setText ( self
. config
[ "prescriber" ])
42 self
. input_newline
. setChecked ( bool ( self
. config
[ "preset_newline" ]))
43 self
. input_delimiter
. setCurrentText ( self
. config
[ "preset_delimiter" ])
44 self
. input_markdown
. setChecked ( bool ( self
. config
[ "markdown" ]))
45 self
. input_update
. setChecked ( bool ( self
. config
[ "check_update" ]))
46 self
. input_plugin
. setChecked ( bool ( self
. config
[ "enable_plugin" ]))
47 self
. input_smime
. setChecked ( bool ( self
. config
[ "smime" ]))
48 self
. input_key
. setText ( self
. config
[ "private_key" ])
49 self
. input_certificate
. setText ( self
. config
[ "certificate" ])
50 self
. input_root
. setText ( self
. config
[ "root_bundle" ])
51 except Exception as e
:
52 QMessageBox
. critical ( self
, "Failed to load" , "Failed to load the data into the application." )
56 if ( QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm Save" , "This action will overwrite the previous configuration. Continue?" )):
58 self
. config
[ "data_directory" ]= self
. input_directory
. text ()
59 self
. config
[ "prescriber" ]= self
. input_prescriber
. text ()
60 self
. config
[ "preset_newline" ]= self
. input_newline
. isChecked ()
61 self
. config
[ "preset_delimiter" ]= self
. input_delimiter
. currentText ()
62 self
. config
[ "markdown" ]= self
. input_markdown
. isChecked ()
63 self
. config
[ "check_update" ]= self
. input_update
. isChecked ()
64 self
. config
[ "enable_plugin" ]= self
. input_plugin
. isChecked ()
65 self
. config
[ "smime" ]= self
. input_smime
. isChecked ()
66 self
. config
[ "private_key" ]= self
. input_key
. text ()
67 self
. config
[ "certificate" ]= self
. input_certificate
. text ()
68 self
. config
[ "root_bundle" ]= self
. input_root
. text ()
69 with
open ( config_file
, "w" ) as f
:
70 f
. write ( json
. dumps ( self
. config
, indent
= 4 ))
71 QMessageBox
. information ( self
, "Saved" , "Configuration saved. Please restart MedScript." )
73 except Exception as e
:
74 QMessageBox
. critical ( self
, "Failed to save" , "Failed to save the data to the file." )
77 def __init__ ( self
, * args
, ** kwargs
):
78 super () .__ init
__ (* args
, ** kwargs
)
81 with
open ( config_file
) as f
:
82 self
. config
= json
. loads ( f
. read ()) | config
83 except Exception as e
:
87 self
. setWindowTitle ( "MedScript" )
88 self
. setGeometry ( 200 , 200 , 300 , 200 )
91 layout
= QFormLayout ( widget
)
92 self
. input_directory
= QLineEdit ( self
)
93 btn_directory
= QPushButton ( "Select Directory" , self
)
94 btn_directory
. clicked
. connect ( self
. select_directory
)
95 layout_directory
= QHBoxLayout ()
96 layout_directory
. addWidget ( self
. input_directory
)
97 layout_directory
. addWidget ( btn_directory
)
98 layout
. addRow ( "Data Directory" , layout_directory
)
99 self
. input_prescriber
= QLineEdit ( self
)
100 btn_prescriber
= QPushButton ( "Select File" , self
)
101 btn_prescriber
. clicked
. connect ( self
. select_prescriber
)
102 layout_prescriber
= QHBoxLayout ()
103 layout_prescriber
. addWidget ( self
. input_prescriber
)
104 layout_prescriber
. addWidget ( btn_prescriber
)
105 layout
. addRow ( "Prescriber" , layout_prescriber
)
106 self
. input_newline
= QCheckBox ( "Add newline after preset" , self
)
107 layout
. addRow ( "Preset Newline" , self
. input_newline
)
108 self
. input_delimiter
= QComboBox ( self
)
109 self
. input_delimiter
. addItems ([ "," , ";" ])
110 layout
. addRow ( "Preset Delimiter" , self
. input_delimiter
)
111 self
. input_markdown
= QCheckBox ( "Enable markdown formatting" , self
)
112 layout
. addRow ( "Markdown" , self
. input_markdown
)
113 self
. input_update
= QCheckBox ( "Check update on startup" , self
)
114 layout
. addRow ( "Check Update" , self
. input_update
)
115 self
. input_plugin
= QCheckBox ( "Enable plugin" , self
)
116 layout
. addRow ( "Plugin" , self
. input_plugin
)
117 self
. input_smime
= QCheckBox ( "Enable digital signature (experimental)" , self
)
118 layout
. addRow ( "S/MIME" , self
. input_smime
)
119 self
. input_key
= QLineEdit ( self
)
120 btn_key
= QPushButton ( "Select File" , self
)
121 btn_key
. clicked
. connect ( self
. select_key
)
122 layout_key
= QHBoxLayout ()
123 layout_key
. addWidget ( self
. input_key
)
124 layout_key
. addWidget ( btn_key
)
125 layout
. addRow ( "Private Key" , layout_key
)
126 self
. input_certificate
= QLineEdit ( self
)
127 btn_certificate
= QPushButton ( "Select File" , self
)
128 btn_certificate
. clicked
. connect ( self
. select_certificate
)
129 layout_certificate
= QHBoxLayout ()
130 layout_certificate
. addWidget ( self
. input_certificate
)
131 layout_certificate
. addWidget ( btn_certificate
)
132 layout
. addRow ( "X509 Certificate" , layout_certificate
)
133 self
. input_root
= QLineEdit ( self
)
134 btn_root
= QPushButton ( "Select File" , self
)
135 btn_root
. clicked
. connect ( self
. select_root
)
136 layout_root
= QHBoxLayout ()
137 layout_root
. addWidget ( self
. input_root
)
138 layout_root
. addWidget ( btn_root
)
139 layout
. addRow ( "Root Bundle" , layout_root
)
140 button_save
= QPushButton ( "Save" )
141 button_save
. clicked
. connect ( self
. save
)
142 button_reset
= QPushButton ( "Reset" )
143 button_reset
. clicked
. connect ( self
. load
)
144 layout_btn
= QHBoxLayout ()
145 layout_btn
. addWidget ( button_save
)
146 layout_btn
. addWidget ( button_reset
)
147 layout
. addRow ( "" , layout_btn
)
149 self
. statusbar
= QStatusBar ()
150 self
. setStatusBar ( self
. statusbar
)
152 self
. setCentralWidget ( widget
)
153 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
157 class EditPrescriber ( QMainWindow
):
159 signal_save
= pyqtSignal ( str )
166 self
. file = config
[ "prescriber" ]
167 self
. statusbar
. showMessage ( self
. file )
168 with
open ( self
. file ) as data
:
169 self
. prescriber
= json
. loads ( data
. read ())
170 self
. input_name
. setText ( self
. prescriber
[ "name" ])
171 self
. input_qualification
. setText ( self
. prescriber
[ "qualification" ])
172 self
. input_registration
. setText ( self
. prescriber
[ "registration" ])
173 self
. input_address
. setText ( self
. prescriber
[ "address" ])
174 self
. input_contact
. setText ( self
. prescriber
[ "contact" ])
175 self
. input_extra
. setText ( self
. prescriber
[ "extra" ])
176 except Exception as e
:
177 QMessageBox
. critical ( self
, "Failed to load" , "Failed to load the data into the application." )
180 def save ( self
, file = False ):
181 if ( file is not False or QMessageBox
. StandardButton
. Yes
== QMessageBox
. question ( self
, "Confirm Save" , "This action will overwrite the previous information. Continue?" )):
185 self
. prescriber
[ "name" ]= self
. input_name
. text ()
186 self
. prescriber
[ "qualification" ]= self
. input_qualification
. text ()
187 self
. prescriber
[ "registration" ]= self
. input_registration
. text ()
188 self
. prescriber
[ "address" ]= self
. input_address
. toPlainText ()
189 self
. prescriber
[ "contact" ]= self
. input_contact
. text ()
190 self
. prescriber
[ "extra" ]= self
. input_extra
. toPlainText ()
191 with
open ( file , "w" ) as f
:
192 f
. write ( json
. dumps ( self
. prescriber
, indent
= 4 ))
193 QMessageBox
. information ( self
, "Saved" , "Information saved." )
194 self
. signal_save
. emit ( self
. file )
196 except Exception as e
:
197 QMessageBox
. critical ( self
, "Failed to save" , "Failed to save the data to the file." )
202 self
. save ( QFileDialog
. getSaveFileName ( self
, "Save prescriber" , config
[ "prescriber_directory" ], "JSON (*.json);; All Files (*)" )[ 0 ])
203 except Exception as e
:
207 def __init__ ( self
, * args
, ** kwargs
):
208 super () .__ init
__ (* args
, ** kwargs
)
210 self
. setWindowTitle ( "MedScript" )
211 self
. setGeometry ( 200 , 200 , 300 , 200 )
214 layout
= QFormLayout ( widget
)
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 button_save_as
= QPushButton ( "Save As" )
230 button_save_as
. clicked
. connect ( self
. save_as
)
231 button_reset
= QPushButton ( "Reset" )
232 button_reset
. clicked
. connect ( self
. load
)
233 layout_btn
= QHBoxLayout ()
234 layout_btn
. addWidget ( button_save
)
235 layout_btn
. addWidget ( button_save_as
)
236 layout_btn
. addWidget ( button_reset
)
237 layout
. addRow ( "" , layout_btn
)
239 self
. statusbar
= QStatusBar ()
240 self
. setStatusBar ( self
. statusbar
)
242 self
. setCentralWidget ( widget
)
243 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))