-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproject.py
196 lines (157 loc) · 5.19 KB
/
project.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
import os
import sys
import qrcode
import itertools
from PIL import Image as mm
import dearpygui.dearpygui as dpg
from dearpygui_ext.themes import create_theme_imgui_light
# vCard format
vCard = {
"BEGIN": "VCARD",
"VERSION": "4.0",
"KIND": "INDIVIDUAL",
"EMAIL;TYPE=WORK": "Email",
"TITLE": "Title",
"ROLE": "Role",
"FN": "Full Name",
"TEL;TYPE=CELL": "Telephone",
"TEL;TYPE=WORK": "Mobile",
"URL": "URL",
"ORG": "Organization",
"END": "VCARD",
}
def main():
dpg.create_context()
# # Set the theme and styling
light_theme = create_theme_imgui_light()
dpg.bind_theme(light_theme)
# Build the menu bar
with dpg.viewport_menu_bar():
with dpg.menu(label="File"):
dpg.add_menu_item(label="Generate", tag="generate_menu", callback=generate)
dpg.add_menu_item(label="Reset", tag="reset_menu", callback=reset)
dpg.add_menu_item(label="Save", tag="save_menu", callback=generate)
dpg.add_menu_item(label="Quit", tag="quit_menu", callback=end)
# Build the primary window
with dpg.window(tag="QR-vCard - V0.1.0"):
# Spacing guide: menu bar
dpg.add_spacer(width=240, height=25)
# Generate input fields as defined by vCard dictionary
display_fields = dict(itertools.islice(vCard.items(), 3, 11))
for key, value in display_fields.items():
dpg.add_input_text(width=240, hint=value, tag=value, indent=10)
# Spacing guide: prompt
dpg.add_spacer(width=5, height=5)
# Prompt user to perform generation
dpg.add_text("Generate a QR vCard", indent=10)
# Present the action button
dpg.add_button(
tag="generate_button",
label="Generate",
width=240,
height=20,
indent=10,
callback=generate,
)
# Import a placeholder image
width, height, channels, data = dpg.load_image("images/placeholder.png")
with dpg.texture_registry(show=False):
dpg.add_dynamic_texture(330, 330, data, tag="texture_tag")
# Present a window and populate it with the resulting image
dpg.add_image(
texture_tag="texture_tag",
width=240,
height=240,
pos=(18, 290),
)
dpg.create_viewport(
title="QR-vCard - V0.1.0",
width=280,
height=550,
x_pos=660,
y_pos=180,
resizable=False,
small_icon=r"images/icon.png",
large_icon=r"images/icon.png",
)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("QR-vCard - V0.1.0", True)
dpg.start_dearpygui()
dpg.destroy_context()
cleanup()
def end():
"""Removes temporary files and exits the program cleanly."""
cleanup()
sys.exit(1)
def update_preview():
"""Refresh placeholder image with generated qr-code.
Note all images must have the same dimensions or update_preview
will fail with segmentation fault."""
try:
width, height, channels, data = dpg.load_image("output/preview.png")
dpg.set_value("texture_tag", data)
except OSError:
pass
def cleanup():
"""Remove preview image from output folder."""
try:
os.remove("output/preview.png")
except OSError:
pass
def reset():
"""Clear all contact fields."""
try:
display_fields = dict(itertools.islice(vCard.items(), 3, 11))
for key, value in display_fields.items():
dpg.set_value(value, "")
except IndexError:
pass
def generate(sender):
"""Fetch field data and build vCard."""
user_vcard = ""
user_input = []
for key, value in vCard.items():
if dpg.get_value(value):
user_input.append(f"{key}:{dpg.get_value(value)}")
elif dpg.get_value(value) == None:
user_input.append(f"{key}:{value}")
for field in user_input:
user_vcard += f"{field}\n"
if len(user_vcard) > 50:
if "generate_button" in sender or "generate_menu" in sender:
build(vcard=user_vcard.strip())
update_preview()
elif "save_menu" in sender:
if os.path.isfile("output/preview.png"):
build(dest="output/vcard.png", vcard=user_vcard.strip())
else:
pass
else:
build()
update_preview()
def build(dest="output/preview.png", vcard="https://youtu.be/dQw4w9WgXcQ"):
"""Generates a QR code from collected field data."""
# Check if output/ exists, else create it
if not os.path.exists("output/"):
os.makedirs("output/")
# Clean up previously generated preview image
cleanup()
# Set up the qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
# Add the data and create the qrcode image
qr.add_data(vcard)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(dest)
# Resize the image - there should be a way to do this directly from qrcode??
img = mm.open(dest)
img2 = img.resize((330, 330))
img2.save(dest)
if __name__ == "__main__":
main()