]> Softwares of Agnibho - medscript.git/blob - renderbox.py
Added display without rendering
[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
14 from config import config
15
16 class RenderBox(QMainWindow):
17
18 file=""
19
20 def cmd_pdf(self):
21 file=QFileDialog.getSaveFileName(self, "Save PDF", os.path.abspath(os.path.join(config["document_directory"], ".pdf")), "PDF (*.pdf);; All Files (*)")
22 page=QPageSize(QPageSize.PageSizeId[self.input_size.currentText()])
23 self.webview.printToPdf(file[0], QPageLayout(page, QPageLayout.Orientation.Portrait, QMarginsF()))
24
25 def cmd_print(self):
26 dialog=QPrintDialog(self.printer)
27 if(dialog.exec()):
28 self.webview.print(self.printer)
29
30 def __init__(self, *args, **kwargs):
31 super().__init__(*args, **kwargs)
32
33 self.setWindowTitle("MedScript")
34 self.setGeometry(100, 100, 600, 400)
35
36 self.printer=QPrinter(QPrinter.PrinterMode.HighResolution)
37
38 button_pdf=QPushButton("Save PDF", self)
39 button_pdf.setShortcut("Ctrl+S")
40 button_pdf.clicked.connect(self.cmd_pdf)
41 button_print=QPushButton("Print Document", self)
42 button_print.setShortcut("Ctrl+P")
43 button_print.clicked.connect(self.cmd_print)
44 button_close=QPushButton("Close Window", self)
45 button_close.setShortcut("Ctrl+Q")
46 button_close.clicked.connect(self.hide)
47
48 page_size=[]
49 for size in QPageSize.PageSizeId:
50 page_size.append(size.name)
51 self.input_size=QComboBox(self)
52 self.input_size.addItems(page_size)
53
54 toolbar=QToolBar("View Toolbar", floatable=False, movable=False)
55 toolbar.addWidget(self.input_size)
56 toolbar.addWidget(button_pdf)
57 spacer=QWidget(self)
58 spacer.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
59 toolbar.addWidget(spacer)
60 toolbar.addWidget(button_print)
61 toolbar.addWidget(button_close)
62 self.addToolBar(toolbar)
63
64 self.webview=QWebEngineView()
65
66 self.setCentralWidget(self.webview)
67 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
68
69 def update(self, file):
70 try:
71 self.file=file
72 self.webview.load(QUrl("file:///"+self.file.replace(os.sep, "/")))
73 except Exception as e:
74 QMessageBox.warning(self,"Display failed", "Failed to display file.")
75 self.hide()
76 logging.warning(e)
77
78
79 class UnrenderBox(QDialog):
80
81 def __init__(self, *args, **kwargs):
82 super().__init__(*args, **kwargs)
83
84 self.setWindowTitle("MedScript")
85 self.setMinimumSize(600, 400)
86 self.setSizeGripEnabled(True)
87
88 layout=QVBoxLayout(self)
89 heading=QLabel("<strong>Prescription</strong>")
90 self.display=QTextBrowser()
91 layout.addWidget(heading)
92 layout.addWidget(self.display)
93
94 self.setWindowIcon(QIcon(os.path.join("resource", "icon_medscript.ico")))
95
96 def show(self, prescription):
97 text=""
98 for attr, value in prescription.prescriber.__dict__.items():
99 if(attr not in ["properties"] and len(value)>0):
100 text=text+"<strong>"+value.upper()+"</strong><br>"
101 text=text+"<hr>"
102 for attr, value in prescription.__dict__.items():
103 if(attr not in ["prescriber", "custom", "properties", "file"] and len(str(value))>0):
104 text=text+"<strong>"+attr.upper()+"</strong><br>"
105 text=text+str(value)+"<br>"
106 self.display.setText(text)