]>
Softwares of Agnibho - medscript.git/blob - customform.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
, QFormLayout
, QLineEdit
, QTextEdit
, QCheckBox
, QDateTimeEdit
, QCalendarWidget
9 from PyQt6
. QtCore
import QDateTime
11 import logging
, os
, json
, sys
, dateutil
. parser
12 from config
import config
14 class CustomForm ( QWidget
):
21 for i
in glob ( os
. path
. join ( config
[ "form_directory" ], "*" )):
24 self
. forms
. append ( json
. loads ( f
. read ()))
25 except Exception as e
:
30 self
. custom
. append ({ j
[ "name" ]: "" })
31 if ( "type" in j
and j
[ "type" ]== "text" ):
32 self
. inputs
. append ([ j
[ "description" ], QTextEdit ()])
33 elif ( "type" in j
and j
[ "type" ]== "date" ):
35 d
. setDisplayFormat ( "MMMM dd, yyyy hh:mm a" )
36 d
. setCalendarPopup ( True )
37 d
. setCalendarWidget ( QCalendarWidget ())
38 self
. inputs
. append ([ j
[ "description" ], d
])
39 elif ( "type" in j
and j
[ "type" ]== "check" ):
40 self
. inputs
. append ([ j
[ "description" ], QCheckBox ()])
42 self
. inputs
. append ([ j
[ "description" ], QLineEdit ()])
45 except Exception as e
:
50 for index
, item
in enumerate ( self
. custom
):
51 if ( isinstance ( self
. inputs
[ index
][ 1 ], QLineEdit
)):
52 self
. custom
[ index
][ list ( item
)[ 0 ]]= self
. inputs
[ index
][ 1 ]. text ()
53 elif ( isinstance ( self
. inputs
[ index
][ 1 ], QTextEdit
)):
54 self
. custom
[ index
][ list ( item
)[ 0 ]]= self
. inputs
[ index
][ 1 ]. toPlainText ()
55 elif ( isinstance ( self
. inputs
[ index
][ 1 ], QCheckBox
)):
56 self
. custom
[ index
][ list ( item
)[ 0 ]]= self
. inputs
[ index
][ 1 ]. isChecked ()
57 elif ( isinstance ( self
. inputs
[ index
][ 1 ], QDateTimeEdit
)):
58 self
. custom
[ index
][ list ( item
)[ 0 ]]= self
. inputs
[ index
][ 1 ]. text ()
59 except Exception as e
:
63 def setData ( self
, custom
= False ):
67 for index
, item
in enumerate ( self
. custom
):
68 if ( isinstance ( self
. inputs
[ index
][ 1 ], QLineEdit
)):
69 self
. inputs
[ index
][ 1 ]. setText ( self
. custom
[ index
][ list ( item
)[ 0 ]])
70 elif ( isinstance ( self
. inputs
[ index
][ 1 ], QTextEdit
)):
71 self
. inputs
[ index
][ 1 ]. setText ( self
. custom
[ index
][ list ( item
)[ 0 ]])
72 elif ( isinstance ( self
. inputs
[ index
][ 1 ], QCheckBox
)):
73 self
. inputs
[ index
][ 1 ]. setChecked ( bool ( self
. custom
[ index
][ list ( item
)[ 0 ]]))
74 elif ( isinstance ( self
. inputs
[ index
][ 1 ], QDateTimeEdit
)):
75 pdate
= dateutil
. parser
. parse ( self
. custom
[ index
][ list ( item
)[ 0 ]])
76 d
= QDateTime
. fromString ( pdate
. strftime ( "%Y-%m- %d %H:%M:%S" ), "yyyy-MM-dd hh:mm:ss" )
77 self
. inputs
[ index
][ 1 ]. setDateTime ( d
)
78 except Exception as e
:
82 def __init__ ( self
, * args
, ** kwargs
):
83 super () .__ init
__ (* args
, ** kwargs
)
85 if ( config
[ "enable_form" ]):
88 layout
= QFormLayout ( self
)
90 layout
. addRow ( i
[ 0 ], i
[ 1 ])