-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageView.py
More file actions
132 lines (105 loc) · 5.12 KB
/
messageView.py
File metadata and controls
132 lines (105 loc) · 5.12 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
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
# Yahalomit Levi 203956321
# Nisan Fichman 204470199
import os
import sys
import time
from PyQt6.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialog
from PyQt6 import uic
import DB.DataBase
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Spacer
from reportlab.platypus.para import Paragraph
import textwrap
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class messageViewWindow(QMainWindow):
def __init__(self, identifier):
super().__init__()
# loading the ui file with uic module
script_path = os.path.dirname(os.path.realpath(__file__))
UI_File_Path = os.path.join(script_path, resource_path("GUIui/messageView.ui"))
uic.loadUi(UI_File_Path, self)
self.show()
self.identifier = identifier
self.exportBtn.clicked.connect(self.exportBtn_clicked)
self.showLetter(identifier)
def showLetter(self,identifier):
data = DB.DataBase.getMessageData(identifier)
if data==None:
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Icon.Critical)
msgBox.setText("Message was no found")
msgBox.setWindowTitle("Something went wrong")
msgBox.exec()
else:
print(data)
for d in data:
self.textBrowser.append(d + " : " + data[d])
def exportBtn_clicked(self):
filename, _ = QFileDialog.getSaveFileName(self, "Export PDF", "", "PDF files (*.pdf)")
information = DB.DataBase.getMessageData(self.identifier)
if filename:
try:
pdf = SimpleDocTemplate(filename, pagesize=landscape(letter))
styles = getSampleStyleSheet()
title_style = styles['Heading1']
# Create the title paragraph
title = Paragraph(self.identifier, title_style)
# Add the title paragraph and a spacer to the document
pdf_elements = [title, Spacer(1, 24)]
# Convert the records into a table format
# Add column headers
header = [" ", " ", " ", " ", " ", " ", " ", " "]
data = [header]
record_list = DB.DataBase.getMedicationInfo(self.identifier)
data.append(["Identifier : ", information["identifier"]])
data.append(["Fist Name : ", information["first name"]])
data.append(["Last Name : ", information["last name"]])
data.append(["Email : ", information["email"]])
data.append(["Phone Number : ", information["phone number"]])
data.append(["Contact Preference : ", information["contact preference"]])
data.append(["Date : " , information["Date"]])
data.append(["Message : " , information["content"]])
# Create the table with the modified table_data
wrapped_data = []
for row in data:
wrapped_row = []
for cell in row:
wrapped_cell = textwrap.wrap(cell, 450//5)
wrapped_row.append('\n'.join(wrapped_cell))
wrapped_data.append(wrapped_row)
# Calculate the available page width
available_page_width = pdf.width
# Calculate the width for each column based on the available page width
num_columns = len(header)
column_width = available_page_width / num_columns
table = Table(wrapped_data, colWidths=[column_width]*num_columns) # Adjust the width of each column
# Apply table styles
table_style = TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.grey), # Gray background for header row
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke), # White text for header row
('ALIGN', (0, 0), (-1, -1), 'LEFT'), # Left-align all cells
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'), # Bold font for header row
('BOTTOMPADDING', (0, 0), (-1, 0), 12), # Add padding to the header row
('BACKGROUND', (0, 1), (-1, -1), colors.beige), # Background color for data rows
])
table.setStyle(table_style)
pdf_elements.append(table)
pdf.build(pdf_elements)
print("pdf export complete successfully")
except Exception as e:
print("PDF generation failed: ", e)
if __name__ == "__main__":
app = QApplication([])
window = messageViewWindow('acamol')
window.show()
app.exec()