-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
218 lines (165 loc) · 6.64 KB
/
main.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
#!/usr/bin/python
from gi.repository import Gtk
from os import system
from PIL import Image
import StringIO, cairo
from carnet import Carnet
from employee import Employee
# ---- FOR COMMON USE
def execute_cmd(cmd_string):
# system("clear")
a = system(cmd_string)
print ""
if a == 0:
pass # print "Command executed correctly"
else:
print "Command terminated with error"
raw_input("Press enter")
print ""
return a
# ---- FOR COMMON USE
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ID Card Generator - By Erick Birbe")
self.employee = None
self.grid = Gtk.Grid()
self.grid.set_column_spacing(10)
self.grid.set_row_spacing(10)
self.add(self.grid)
self.lbl_search = Gtk.Label("ID")
self.txt_search = Gtk.SearchEntry()
self.txt_search.connect('activate', self.on_txt_search_activate)
self.lbl_name = Gtk.Label("Name:")
self.txt_name = Gtk.Entry()
self.txt_name.set_editable(False)
self.txt_name1 = Gtk.Entry()
self.txt_name1.set_editable(False)
self.lbl_surname = Gtk.Label("Surname:")
self.txt_surname = Gtk.Entry()
self.txt_surname.set_editable(False)
self.txt_surname1 = Gtk.Entry()
self.txt_surname1.set_editable(False)
self.lbl_position = Gtk.Label("Position:")
self.txt_position = Gtk.TextView()
self.txt_position.set_editable(True)
self.lbl_department = Gtk.Label("Department:")
self.txt_department = Gtk.TextView()
self.txt_department.set_editable(True)
self.txt_department.set_wrap_mode(Gtk.WrapMode.WORD)
self.btn_process = Gtk.Button("Process!")
self.btn_process.connect('clicked', self.on_btn_process_clicked)
self.btn_take_picture = Gtk.Button("Take a Picture")
self.btn_take_picture.connect(
'clicked', self.on_btn_take_picture_clicked)
self.btn_print = Gtk.Button("Print")
self.btn_print.connect('clicked', self.on_btn_print_clicked)
self.image = Gtk.Image()
self.grid.add(self.lbl_search)
self.grid.attach(self.txt_search, 1, 0, 1, 1)
self.grid.attach(self.lbl_name, 0, 1, 1, 1)
self.grid.attach(self.txt_name, 1, 1, 1, 1)
self.grid.attach(self.txt_name1, 1, 2, 1, 1)
self.grid.attach(self.lbl_surname, 0, 3, 1, 1)
self.grid.attach(self.txt_surname, 1, 3, 1, 1)
self.grid.attach(self.txt_surname1, 1, 4, 1, 1)
self.grid.attach(self.lbl_department, 0, 5, 1, 1)
self.grid.attach(self.txt_department, 1, 5, 1, 3)
self.grid.attach(self.lbl_position, 0, 8, 1, 1)
self.grid.attach(self.txt_position, 1, 8, 1, 3)
self.grid.attach(self.btn_process, 0, 11, 2, 1)
self.grid.attach(self.btn_take_picture, 0, 12, 2, 1)
self.grid.attach(self.btn_print, 0, 13, 2, 1)
self.clear_form()
def clear_form(self):
widgets = [
self.txt_name,
self.txt_name1,
self.txt_surname,
self.txt_surname1,
self.txt_position.get_buffer(),
self.txt_department.get_buffer(),
]
for w in widgets:
w.set_text('')
self.load_image()
def load_image(self, file_name=None):
print "Filename = %s" % file_name
self.image.destroy()
if file_name is None:
self.image = Gtk.Image.new_from_file('data/front-bg-white.png')
else:
self.image = Gtk.Image.new_from_file(
'output/carnets/%s.png' % file_name)
self.grid.attach(self.image, 3, 0, 1, 20)
self.image.show()
def set_employee(self, pid):
if self.employee is None or self.employee.pid != pid:
self.employee = Employee(pid)
self.txt_name.set_text(self.employee.name)
self.txt_name1.set_text(self.employee.sname)
self.txt_surname.set_text(self.employee.surname)
self.txt_surname1.set_text(self.employee.ssurname)
self.txt_position.get_buffer().set_text(self.employee.position)
self.txt_department.get_buffer().set_text(self.employee.department)
def update_employee(self):
start, end = self.txt_position.get_buffer().get_bounds()
self.employee.position = self.txt_position.get_buffer().get_text(start, end, False)
start, end = self.txt_department.get_buffer().get_bounds()
self.employee.department = self.txt_department.get_buffer().get_text(start, end, False)
carnet = Carnet(self.employee)
carnet.create()
def on_btn_process_clicked(self, widget):
if self.employee is None:
return
self.update_employee()
self.load_image(self.employee.pid)
def on_txt_search_activate(self, widget):
self.clear_form()
try:
self.set_employee(widget.get_text())
self.load_image(self.employee.pid)
except Exception, excp:
print excp
self.clear_form()
def on_btn_take_picture_clicked(self, widget):
status = execute_cmd("python camera.py %s" % self.employee.pid)
if status == 0:
carnet = Carnet(self.employee)
carnet.create()
self.load_image(self.employee.pid)
def on_btn_print_clicked(self, widget):
self.pd = Gtk.PrintOperation()
self.pd.set_n_pages(1)
self.pd.set_use_full_page(True)
self.pd.connect("draw_page", self.draw_page)
result = self.pd.run(
Gtk.PrintOperationAction.PRINT_DIALOG, None)
print result # handle errors etc.
def draw_page(self, operation=None, context=None, page_nr=None):
psettings = self.pd.get_print_settings()
psize = psettings.get_paper_size()
psize.set_size(10, 10, Gtk.Unit.MM)
psettings.set_paper_size(psize)
self.pd.set_print_settings(psettings)
w = int(context.get_width())
h = int(context.get_height())
#page_setup = context.get_page_setup()
#paper_size = page_setup.get_paper_size()
#paper_size.set_size(10, 10, Gtk.Unit.INCH)
#page_setup.set_paper_size(paper_size)
# Getting JPG image
im = Image.open("output/carnets/%s.png" % self.employee.pid)
im = im.resize((w,h), Image.ANTIALIAS)
buf = StringIO.StringIO()
im.save(buf, format="PNG")
buf.seek(0)
surface = cairo.ImageSurface.create_from_png(buf)
ctx = context.get_cairo_context()
print surface.get_width(), surface.get_height()
ctx.set_source_surface(surface, 0.0, 0.0)
ctx.paint()
if __name__ == "__main__":
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()