-
Notifications
You must be signed in to change notification settings - Fork 2
/
PyQTReportDialog.py
226 lines (194 loc) · 9 KB
/
PyQTReportDialog.py
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from PyQt5.QtWidgets import QDialog, QVBoxLayout, QLabel, QFileDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QDateTime, QStandardPaths
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from PyQTCustomItems import CustomCheckBoxLineEdit, CustomPushButton
from typing import Tuple, List, Optional
class ReportWindow(QDialog):
"""
A dialog window for generating and saving a PDF report.
Args:
app: The parent application.
Attributes:
author_input (CustomCheckBoxLineEdit): Input field for the author's name.
watch_input (CustomCheckBoxLineEdit): Input field for the watch information.
movement_input (CustomCheckBoxLineEdit): Input field for the movement information.
serial_input (CustomCheckBoxLineEdit): Input field for the serial number.
liftangle_input (CustomCheckBoxLineEdit): Input field for the lift angle.
movement_bph_input (CustomCheckBoxLineEdit): Input field for beats per hour.
rate_input (CustomCheckBoxLineEdit): Input field for rate.
beat_error_input (CustomCheckBoxLineEdit): Input field for beat error.
amplitude_input (CustomCheckBoxLineEdit): Input field for amplitude.
generate_button (CustomPushButton): Button to generate and save the report.
file_dialog (QFileDialog): File dialog for selecting the save location.
"""
def __init__(self, app):
super().__init__()
self.app = app
self.setFixedWidth(800)
self.setWindowIcon(QIcon('data/icons/audio-waves.png'))
self.setStyleSheet(open("style.css").read())
self.min_label_width = 300
self.initUI()
def initUI(self):
self.setWindowTitle("Report Window")
layout = QVBoxLayout()
# Create line edits for report information
self.author_input = CustomCheckBoxLineEdit("Author:", "")
self.load_user_name()
self.author_input.line_edit.textChanged.connect(self.save_user_name)
self.watch_input = CustomCheckBoxLineEdit("Watch:", "")
self.watch_input.line_edit.textChanged.connect(self.watch_input.setText)
self.movement_input = CustomCheckBoxLineEdit("Movement:", "")
self.movement_input.line_edit.textChanged.connect(self.movement_input.setText)
self.serial_input = CustomCheckBoxLineEdit("Serial Number:", "")
self.serial_input.line_edit.textChanged.connect(self.serial_input.setText)
self.liftangle_input = CustomCheckBoxLineEdit("Lift Angle:", self.app.liftangle)
self.movement_bph_input = CustomCheckBoxLineEdit("BPH:", self.app.movement_bph)
self.rate_input = CustomCheckBoxLineEdit("Rate:", self.app.rate)
self.beat_error_input = CustomCheckBoxLineEdit("Beat Error:", self.app.beat_error)
self.amplitude_input = CustomCheckBoxLineEdit("Amplitude:", self.app.amplitude)
layout.addWidget(QLabel("Report Information"))
layout.addWidget(self.author_input)
layout.addWidget(self.watch_input)
layout.addWidget(self.movement_input)
layout.addWidget(self.serial_input)
layout.addWidget(QLabel("Measurement Information"))
layout.addWidget(self.liftangle_input)
layout.addWidget(self.movement_bph_input)
layout.addWidget(self.rate_input)
layout.addWidget(self.beat_error_input)
layout.addWidget(self.amplitude_input)
# Create the save button
self.generate_button = CustomPushButton("../Data/icons/save-report.png", self.generate_report, "Save report", size=35)
self.generate_button.setFixedWidth(780)
layout.addWidget(self.generate_button)
# Initialize file dialog
self.file_dialog = QFileDialog()
# Set layout
self.setLayout(layout)
def generate_report(self):
"""
Generate and save the PDF report.
"""
self.generate_button.setEnabled(False)
default_file_name = QStandardPaths.standardLocations(QStandardPaths.DownloadLocation)[0] + '\\untitled'
file_path, _ = self.file_dialog.getSaveFileName(self, "Save Report", default_file_name, "PDF Files (*.pdf)")
if not file_path:
self.generate_button.setEnabled(True)
return
c = canvas.Canvas(file_path, pagesize=letter)
y = self.create_pdf_header(c)
self.create_pdf_measurements(c, y_position=y)
self.create_pdf_statement(c)
self.create_pdf_image(c)
c.save()
self.generate_button.setEnabled(True)
def create_pdf_header(self, c: canvas.Canvas, y_position: int = 750, x_position: int = 50) -> int:
"""
Create the header section of the PDF report.
Args:
c (canvas.Canvas): The PDF canvas.
y_position (int): The starting y-position for drawing.
Returns:
int: The updated y-position after drawing the header.
"""
c.setFont("Courier-Bold", 20)
c.drawString(x_position, y_position, "Accuracy Report")
y_position -= 25
c.setFont("Courier-Bold", 14)
c.drawString(x_position, y_position, "Report information")
y_position -= 18
c.setFont("Courier", 12)
c.drawString(x_position + 150, y_position, QDateTime.currentDateTime().toString("yyyy-MM-dd") + " " + QDateTime.currentDateTime().toString("hh:mm:ss"))
y_position -= 15
info = [(_input.label, _input.text) for _input in [self.author_input,
self.watch_input,
self.movement_input,
self.serial_input]
if _input.checkbox.isChecked()]
for label, value in info:
c.setFont("Courier-Bold", 12)
c.drawString(x_position, y_position, label)
c.setFont("Courier", 12)
c.drawString(x_position + 150, y_position, value)
y_position -= 15
c.line(x_position, y_position, 600 - (min(x_position, 50)), y_position) # Replace coordinates as needed
y_position -= 15
return y_position
def create_pdf_measurements(self, c: canvas.Canvas, y_position: int, x_position: int = 50):
"""
Create the measurement information section of the PDF report.
Args:
c (canvas.Canvas): The PDF canvas.
y_position (int): The starting y-position for drawing.
x_position (int): The x-position for drawing.
Returns:
int: The updated y-position after drawing the measurement information.
"""
c.setFont("Courier-Bold", 14, leading=0)
c.drawString(x_position, y_position, "Measurement information")
y_position -= 18
info = [(_input.label, _input.text) for _input in
[
self.movement_bph_input,
self.rate_input,
self.beat_error_input,
self.amplitude_input,
self.liftangle_input
]
if _input.checkbox.isChecked()
]
for label, value in info:
c.setFont("Courier-Bold", 12)
c.drawString(x_position, y_position, label)
c.setFont("Courier", 12)
c.drawString(x_position + 300, y_position, str(value))
y_position -= 15
def create_pdf_statement(self, c: canvas.Canvas):
"""
Create the statement section of the PDF report.
Args:
c (canvas.Canvas): The PDF canvas.
"""
c.setFont("Courier", 5)
statement = [
"This report was generated with TimeTrace.",
"The information in this report is provided as is, without warranty of any kind.",
"TimeTrace is not responsible for any inaccuracies or errors in this report.",
]
y_position = 23
for line in statement:
c.drawString(67, y_position, line)
y_position -= 4
def create_pdf_image(self, c: canvas.Canvas):
"""
Create and add an image to the PDF report.
Args:
c (canvas.Canvas): The PDF canvas.
"""
image_path = 'data/icons/audio-waves.png'
c.drawImage(image_path, 50, 15, width=11, height=11) # Adjust the coordinates and size as needed
def save_user_name(self):
"""
Save the user's name to a text file.
"""
user_name = self.author_input.text
if user_name:
with open("user_name.txt", "w") as file:
file.write(user_name)
def load_user_name(self) -> Optional[str]:
"""
Load the user's name from a text file.
Returns:
Optional[str]: The user's name if found, or None if the file does not exist.
"""
try:
with open("user_name.txt", "r") as file:
user_name = file.read()
if user_name:
self.author_input.setText(user_name)
return user_name
except FileNotFoundError:
pass