]>
Softwares of Agnibho - medscript.git/blob - renderbox.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
, QDialog
, QToolBar
, QFileDialog
, QComboBox
, QPushButton
, QLabel
, QVBoxLayout
, QTextBrowser
, QSizePolicy
9 from PyQt6
. QtWebEngineWidgets
import QWebEngineView
10 from PyQt6
. QtGui
import QIcon
, QPageLayout
, QPageSize
11 from PyQt6
. QtCore
import QUrl
, QMarginsF
12 from PyQt6
. QtPrintSupport
import QPrinter
, QPrintDialog
13 import logging
, os
, copy
14 from config
import config
16 class RenderBox ( QMainWindow
):
22 file = QFileDialog
. getSaveFileName ( self
, "Save PDF" , os
. path
. abspath ( os
. path
. join ( config
[ "document_directory" ], ".pdf" )), "PDF (*.pdf);; All Files (*)" )
23 page
= QPageSize ( QPageSize
. PageSizeId
[ self
. input_size
. currentText ()])
24 self
. webview
. printToPdf ( file [ 0 ], QPageLayout ( page
, QPageLayout
. Orientation
. Portrait
, QMarginsF ()))
25 except Exception as e
:
30 dialog
= QPrintDialog ( self
. printer
)
32 self
. webview
. print ( self
. printer
)
33 except Exception as e
:
36 def __init__ ( self
, * args
, ** kwargs
):
37 super () .__ init
__ (* args
, ** kwargs
)
39 self
. setWindowTitle ( "MedScript Prescription" )
40 self
. setGeometry ( 100 , 100 , 600 , 400 )
42 self
. printer
= QPrinter ( QPrinter
. PrinterMode
. HighResolution
)
44 button_pdf
= QPushButton ( "Save PDF" , self
)
45 button_pdf
. setShortcut ( "Ctrl+S" )
46 button_pdf
. clicked
. connect ( self
. cmd_pdf
)
47 button_print
= QPushButton ( "Print Document" , self
)
48 button_print
. setShortcut ( "Ctrl+P" )
49 button_print
. clicked
. connect ( self
. cmd_print
)
50 button_close
= QPushButton ( "Close Window" , self
)
51 button_close
. setShortcut ( "Ctrl+Q" )
52 button_close
. clicked
. connect ( self
. hide
)
55 for size
in QPageSize
. PageSizeId
:
56 page_size
. append ( size
. name
)
57 self
. input_size
= QComboBox ( self
)
58 self
. input_size
. addItems ( page_size
)
60 toolbar
= QToolBar ( "View Toolbar" , floatable
= False , movable
= False )
61 toolbar
. addWidget ( self
. input_size
)
62 toolbar
. addWidget ( button_pdf
)
64 spacer
. setSizePolicy ( QSizePolicy
. Policy
. Expanding
, QSizePolicy
. Policy
. Expanding
)
65 toolbar
. addWidget ( spacer
)
66 toolbar
. addWidget ( button_print
)
67 toolbar
. addWidget ( button_close
)
68 self
. addToolBar ( toolbar
)
70 self
. webview
= QWebEngineView ()
72 self
. setCentralWidget ( self
. webview
)
73 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
75 def update ( self
, file ):
78 self
. webview
. load ( QUrl ( "file:///" + self
. file . replace ( os
. sep
, "/" )))
79 except Exception as e
:
80 QMessageBox
. warning ( self
, "Display failed" , "Failed to display file." )
85 class UnrenderBox ( QDialog
):
87 def __init__ ( self
, * args
, ** kwargs
):
88 super () .__ init
__ (* args
, ** kwargs
)
90 self
. setWindowTitle ( "MedScript Viewer" )
91 self
. setMinimumSize ( 600 , 400 )
92 self
. setSizeGripEnabled ( True )
94 layout
= QVBoxLayout ( self
)
95 heading
= QLabel ( "<strong>Prescription</strong>" )
96 self
. display
= QTextBrowser ()
97 layout
. addWidget ( heading
)
98 layout
. addWidget ( self
. display
)
100 self
. setWindowIcon ( QIcon ( os
. path
. join ( "resource" , "icon_medscript.ico" )))
102 def show ( self
, prescription
):
103 if ( type ( prescription
) is not dict ):
105 data
= copy
. deepcopy ( prescription
) .__ dict
__
106 if ( type ( data
[ "prescriber" ]) is not dict ):
107 data
[ "prescriber" ]= data
[ "prescriber" ] .__ dict
__
108 except Exception as e
:
111 data
= copy
. deepcopy ( prescription
)
115 def load ( self
, prescription
):
117 for attr
, value
in prescription
[ "prescriber" ]. items ():
118 if ( attr
not in [ "properties" ] and len ( value
)> 0 ):
119 text
= text
+ "<strong>" + value
. upper ()+ "</strong><br>"
120 text
= text
. replace ( " \n " , "<br>" )+ "<hr>"
121 for attr
, value
in prescription
. items ():
122 if ( attr
not in [ "prescriber" , "custom" , "properties" , "file" ] and len ( str ( value
))> 0 and ( attr
not in [ "daw" ] or value
)):
123 text
= text
+ "<strong>" + attr
. upper ()+ "</strong><br>"
124 text
= text
+ str ( value
). strip ()
125 text
= text
. replace ( " \n " , "<br>" )+ "<br>"
126 self
. display
. setText ( text
)