-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathReportDialog.py
More file actions
90 lines (74 loc) · 3.99 KB
/
ReportDialog.py
File metadata and controls
90 lines (74 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from PyQt6.QtPrintSupport import QPrinter
from PyQt6 import QtWidgets, QtCore,QtGui
class ReportDialog(QtWidgets.QDialog):
def __init__(self, graph, parent=None):
super().__init__(parent)
self.graph = graph
self.setWindowTitle("Create Report")
self.setMinimumSize(500, 500)
self.layout = QtWidgets.QVBoxLayout(self)
self.textEdit = QtWidgets.QTextEdit()
defaultFont = QtGui.QFont()
defaultFont.setPointSize(14)
defaultFont.setWeight(50)
defaultFont.setFamily("Arial")
self.textEdit.setFont(defaultFont)
self.textEdit.setStyleSheet("QTextEdit { padding: 10px; }")
self.layout.addWidget(self.textEdit)
self.exportButton = QtWidgets.QPushButton("Export")
self.exportButton.clicked.connect(self.export_report)
self.layout.addWidget(self.exportButton)
self.screenshotButton = QtWidgets.QPushButton("Add Screenshot")
self.screenshotButton.clicked.connect(self.add_screenshot_to_report)
self.layout.addWidget(self.screenshotButton)
def show_statistics_table(self):
selectedIndecies = self.graph.parent().signalListWidget.selectedIndexes()
if len(selectedIndecies) > 0:
statistics = self.graph.parent().show_statistics_tooltip(selectedIndecies[0].row())
if statistics:
cursor = self.textEdit.textCursor()
cursor.movePosition(QtGui.QTextCursor.MoveOperation.End)
keyword_format = QtGui.QTextCharFormat()
keyword_format.setFontWeight(QtGui.QFont.Weight.Bold)
keyword_format.setFontPointSize(9)
keyword_format.setForeground(QtGui.QColor("blue"))
value_format = QtGui.QTextCharFormat()
value_format.setFontWeight(QtGui.QFont.Weight.Bold)
value_format.setFontPointSize(9)
value_format.setForeground(QtGui.QColor("black"))
def insert_formatted_text(keyword, value):
cursor.insertText(keyword + " ", keyword_format)
cursor.insertText(str(value) + "\n", value_format)
insert_formatted_text("Max Value", statistics['Max_Value'])
insert_formatted_text("Min Value", statistics['Min_Value'])
insert_formatted_text("Length", statistics['Time_Length'])
insert_formatted_text("Speed", statistics['Speed'])
insert_formatted_text("Color", statistics['Color'])
insert_formatted_text("STD", statistics['Std_Deviation'])
self.textEdit.setTextCursor(cursor)
def add_screenshot_to_report(self):
graph_widget = self.graph
graph_rect = graph_widget.geometry()
screen = QtGui.QGuiApplication.primaryScreen()
screenshot = screen.grabWindow(graph_widget.winId(), graph_rect.x(), graph_rect.y(), graph_rect.width() - 10, graph_rect.height() - 10)
resized_screenshot = screenshot.scaled(600, 400, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.SmoothTransformation)
buffer = QtCore.QBuffer()
buffer.open(QtCore.QIODevice.OpenModeFlag.WriteOnly)
resized_screenshot.save(buffer, "PNG")
base64_image = buffer.data().toBase64().data().decode()
buffer.close()
html_image = f'<img src="data:image/png;base64,{base64_image}" width="600" height="400">'
cursor = self.textEdit.textCursor()
cursor.insertHtml(html_image)
self.textEdit.setTextCursor(cursor)
self.show_statistics_table()
def export_report(self):
fileName, _ = QtWidgets.QFileDialog.getSaveFileName(
self, None, None, "PDF Files (*.pdf);;All Files (*)"
)
if fileName:
printer = QPrinter(QPrinter.PrinterMode.HighResolution)
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
printer.setOutputFileName(fileName)
self.textEdit.document().print(printer)
self.close()