]> Softwares of Agnibho - medscript.git/blob - renderbox.py
Bugfix: Windows uninstall package permission error
[medscript.git] / renderbox.py
1 # MedScript
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/>.
7
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
15
16 class RenderBox(QMainWindow):
17
18 file=""
19
20 def cmd_pdf(self):
21 try:
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:
26 logging.exception(e)
27
28 def cmd_print(self):
29 try:
30 dialog=QPrintDialog(self.printer)
31 if(dialog.exec()):
32 self.webview.print(self.printer)
33 except Exception as e:
34 logging.exception(e)
35
36 def __init__(self, *args, **kwargs):
37 super().__init__(*args, **kwargs)
38
39 self.setWindowTitle("MedScript Prescription")
40 self.setGeometry(100, 100, 600, 400)
41
42 self.printer=QPrinter(QPrinter.PrinterMode.HighResolution)
43
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)
53
54 page_size=[]
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)
59
60 toolbar=QToolBar("View Toolbar", floatable=False, movable=False)
61 toolbar.addWidget(self.input_size)
62 toolbar.addWidget(button_pdf)
63 spacer=QWidget(self)
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)
69
70 self.webview=QWebEngineView()
71
72 self.setCentralWidget(self.webview)
73 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
74
75 def update(self, file):
76 try:
77 self.file=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.")
81 self.hide()
82 logging.exception(e)
83
84
85 class UnrenderBox(QDialog):
86
87 def __init__(self, *args, **kwargs):
88 super().__init__(*args, **kwargs)
89
90 self.setWindowTitle("MedScript Viewer")
91 self.setMinimumSize(600, 400)
92 self.setSizeGripEnabled(True)
93
94 layout=QVBoxLayout(self)
95 heading=QLabel("<strong>Prescription</strong>")
96 self.display=QTextBrowser()
97 layout.addWidget(heading)
98 layout.addWidget(self.display)
99
100 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
101
102 def show(self, prescription):
103 if(type(prescription) is not dict):
104 try:
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:
109 logging.critical(e)
110 else:
111 data=copy.deepcopy(prescription)
112 self.load(data)
113 return(self)
114
115 def load(self, prescription):
116 text=""
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)