-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
loyaltyCards.py
executable file
·385 lines (338 loc) · 15.8 KB
/
loyaltyCards.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/python3
import os.path
import gi, sqlite3 as sqlite
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, GLib, Gdk
try:
from barcode import Code128, EAN13, EAN14, EAN8
from barcode.writer import ImageWriter
except ImportError:
print("Error: The barcode dependency was not found. You can install it using 'pip3 install python-barcode'")
global path
global showPreview
home = os.path.expanduser("~")
path=home+"/.local/share/loyaltyCardsOpen/"
print(path+"loyaltyCardsDb.db")
con = sqlite.connect(path+"loyaltyCardsDb.db")
class ListBoxRowWithData(Gtk.ListBoxRow):
def __init__(self, data):
super(Gtk.ListBoxRow, self).__init__()
self.data = data
self.add(Gtk.Label(label=data))
class Handler:
global comeFromEdit
comeFromEdit = 0
showPreview = 1
screen = Gdk.Screen.get_default()
provider = Gtk.CssProvider()
style_context = Gtk.StyleContext()
style_context.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def onDestroy(self, *args):
Gtk.main_quit()
def on_file_selected(self, *args):
print("File selected!")
img = builder.get_object("img")
frontImage = builder.get_object("frontImage")
pathFront = frontImage.get_filename()
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=pathFront, width=100, height=80, preserve_aspect_ratio=True)
img.set_from_pixbuf(pixbuf)
img.show_all()
def on_file_selected_back(self, *args):
print("Back File selected!")
imgBack = builder.get_object("imgBack")
backImage = builder.get_object("backImage")
pathBack = backImage.get_filename()
pixbuf1 = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=pathBack, width=100, height=80, preserve_aspect_ratio=True)
imgBack.set_from_pixbuf(pixbuf1)
imgBack.show_all()
def entered_tab(self, cur, *args):
print ('Accessed SearchTab:')
searchEntry = builder.get_object("searchEntry")
listbox = builder.get_object("listbox")
searchParam = searchEntry.get_text()
searchParam = "%"+searchParam+"%"
print("searchParam:"+searchParam)
#Clean listbox rows
for row in listbox:
listbox.remove(row)
with con:
cur = con.cursor()
cur.execute("SELECT * FROM CARD where CARD_NAME LIKE ?" , (searchParam,))
rows = cur.fetchall()
for row in rows:
print (row[0], row[1])
rowData = " \n"+str(row[1])+"\n"+str(row[2])+""
listbox.add(ListBoxRowWithData(rowData))
listbox.show_all()
def codebarImg(self, codebar):
if(len(codebar) == 13):
print("Predicted to have a EAN13 barcode")
try:
codebarImg = EAN13(codebar, writer=ImageWriter())
except: ""
elif(len(codebar) == 8):
print("Predicted to have a EAN8 barcode")
try:
codebarImg = EAN8(codebar, writer=ImageWriter())
except: ""
elif(len(codebar) == 14):
print("Predicted to have a EAN14 barcode")
try:
codebarImg = EAN14(codebar, writer=ImageWriter())
except: ""
else:
print("Predicted to have a Code128 barcode")
try:
codebarImg = Code128(codebar, writer=ImageWriter())
except: ""
return codebarImg
def row_selected(self, cur , row):
global stringId
global cardName
global codebar
global photoPath
global photoPathBack
global barcodeImgFile
image = builder.get_object("image")
backImag = builder.get_object("backImag")
barcodeImg = builder.get_object("barcodeImg")
rowData = row.data
rowData = rowData.strip()
cardName, codebar = rowData.split("\n")
print("cardName:"+cardName)
print("codebar:"+codebar)
with con:
cur = con.cursor()
cur.execute("SELECT ID, BARCODE, IMAGE, IMAGEBACK FROM CARD where CARD_NAME = ? LIMIT 1" , (cardName,))
row = cur.fetchone()
id = row[0]
stringId = str(id)+""
print("id0:"+stringId)
codebar = row[1].replace(" ", " ")
print("codebar:"+codebar)
barcodeImgFile= path+"tmp/"+str(id)+"_barcode"
try:
self.codebarImg(codebar).save(barcodeImgFile)
print("Barcode correctly Saved")
pixbufCodeBar = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=barcodeImgFile+".png", width=400, height=100, preserve_aspect_ratio=True)
barcodeImg.set_from_pixbuf(pixbufCodeBar)
barcodeImg.show()
except:
print("Barcode no correctly generated because of the number of digits")
barcodeImg.hide()
if(self.showPreview == 1):
photo = row[2]
photoPath = path+"tmp/"+str(id)+".jpg"
try:
with open(photoPath, 'wb') as file:
file.write(photo)
print("Stored blob data into: ", photoPath, "\n")
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPath, width=80, height=60, preserve_aspect_ratio=True)
image.set_from_pixbuf(pixbuf)
image.show()
except:
print("No image where given")
image.hide()
try:
photoBack = row[3]
photoPathBack = path+"tmp/"+str(id)+"_back.jpg"
with open(photoPathBack, 'wb') as file:
file.write(photoBack)
print("Stored blob data into: ", photoPathBack, "\n")
pixbufBack = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPathBack, width=80, height=60, preserve_aspect_ratio=True)
backImag.set_from_pixbuf(pixbufBack)
backImag.show()
except:
print("No image where given")
backImag.hide()
delete.show()
edit.show()
def edit_clicked_cb(self, notebook):
notebook = builder.get_object("notebook")
notebook.next_page()
global comeFromEdit
comeFromEdit = 1
print ("comeFromEdit:"+str(comeFromEdit))
id = stringId
print("id:"+id)
entry = builder.get_object("cardNameEntry")
img = builder.get_object("img")
barcodeEntry = builder.get_object("barcodeEntry")
frontImage = builder.get_object("frontImage")
backImage = builder.get_object("backImage")
imgBack = builder.get_object("imgBack")
pathFront = frontImage.get_filename()
pathBack = backImage.get_filename()
entry.set_text(cardName)
barcodeEntry.set_text(codebar)
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPath, width=80, height=60, preserve_aspect_ratio=True)
img.set_from_pixbuf(pixbuf)
pixbuf1 = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPathBack, width=80, height=60, preserve_aspect_ratio=True)
imgBack.set_from_pixbuf(pixbuf1)
img.show_all()
imgBack.show_all()
def imgBig_clicked_cb(self, cur, button):
print("Clicked image")
image = builder.get_object("image")
backImag = builder.get_object("backImag")
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPath, width=150, height=150, preserve_aspect_ratio=True)
image.set_from_pixbuf(pixbuf)
image.show_all()
pixbufBack = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPathBack, width=15, height=15, preserve_aspect_ratio=True)
backImag.set_from_pixbuf(pixbufBack)
backImag.show_all()
def on_button_clicked(self, button):
global comeFromEdit
entry = builder.get_object("cardNameEntry")
img = builder.get_object("img")
imgBack = builder.get_object("imgBack")
barcodeEntry = builder.get_object("barcodeEntry")
frontImage = builder.get_object("frontImage")
backImage = builder.get_object("backImage")
savedImageInfo = builder.get_object("savedImageInfo")
pathFront = frontImage.get_filename()
pathBack = backImage.get_filename()
try:
with open(pathFront, 'rb') as input_file:
pathFrontBlob = input_file.read()
except:
pathFrontBlob = 0
try:
with open(pathBack, 'rb') as input_file1:
pathBackBlob = input_file1.read()
except:
pathBackBlob = 0
cardName =str(entry.get_text())
barcodeEntryStr =str(barcodeEntry.get_text())
print('Card Name: %s' % cardName + ' '+ 'barcodeEntry: %s' % barcodeEntryStr)
print("comeFromEditSave:"+str(comeFromEdit))
with con:
cur = con.cursor()
if comeFromEdit == 0:
print ("We will do an INSERT instead of update because it is not an edit")
if (pathFrontBlob != 0 and pathBackBlob == 0):
print("FrontImage entered but not back")
cur.execute('INSERT INTO CARD(CARD_NAME, BARCODE, IMAGE) VALUES (?,?,?)', (cardName, barcodeEntryStr, sqlite.Binary(pathFrontBlob)))
if (pathFrontBlob == 0 and pathBackBlob != 0):
print("BackImage entered but not front")
cur.execute('INSERT INTO CARD(CARD_NAME, BARCODE, IMAGEBACK) VALUES (?,?,?)', (cardName, barcodeEntryStr, sqlite.Binary(pathBackBlob)))
if (pathFrontBlob == 0 and pathBackBlob == 0):
print("No Back or front image entered")
cur.execute('INSERT INTO CARD(CARD_NAME, BARCODE) VALUES (?,?)', (cardName, barcodeEntryStr))
if (pathFrontBlob != 0 and pathBackBlob != 0):
cur.execute('INSERT INTO CARD(CARD_NAME, BARCODE, IMAGE, IMAGEBACK) VALUES (?,?,?,?)', (cardName, barcodeEntryStr, sqlite.Binary(pathFrontBlob), sqlite.Binary(pathBackBlob)))
print(cur)
else:
print("Else => UPDATE")
if (pathFrontBlob == 0 and pathBackBlob == 0):
sql =cur.execute("UPDATE CARD SET CARD_NAME = ?, BARCODE = ? WHERE ID = ?" , (cardName, barcodeEntryStr, stringId))
if (pathFrontBlob != 0 and pathBackBlob == 0):
sql =cur.execute("UPDATE CARD SET CARD_NAME = ?, BARCODE = ?, IMAGE = ? WHERE ID = ?" , (cardName, barcodeEntryStr, sqlite.Binary(pathFrontBlob), stringId))
if (pathFrontBlob == 0 and pathBackBlob != 0):
sql =cur.execute("UPDATE CARD SET CARD_NAME = ?, BARCODE = ?, IMAGEBACK = ? WHERE ID = ?" , (cardName, barcodeEntryStr, sqlite.Binary(pathBackBlob), stringId))
if (pathFrontBlob != 0 and pathBackBlob != 0):
sql =cur.execute("UPDATE CARD SET CARD_NAME = ?, BARCODE = ?, IMAGE = ?, IMAGEBACK = ? WHERE ID = ?" , (cardName, barcodeEntryStr, sqlite.Binary(pathFrontBlob), sqlite.Binary(pathBackBlob), stringId))
print(sql)
comeFromEdit = 0
cardName = entry.set_text('')
barcodeEntry = barcodeEntry.set_text('')
if (pathFrontBlob != 0 and pathBackBlob != 0):
pathFront = frontImage.unselect_all()
pathBack = backImage.unselect_all()
img.hide()
imgBack.hide()
GLib.timeout_add_seconds(1, 3, self.show_saved_image_seconds())
savedImageInfo.hide()
def show_saved_image_seconds(self):
print ('Accessed show_saved_image_seconds:')
savedImageInfo.show()
def del_clicked_cb(self, cur):
popDelConfirm = builder.get_object("popDelConfirm")
popDelConfirm.show_all()
def del_yes_clicked_cb(self, cur):
popDelConfirm = builder.get_object("popDelConfirm")
id = stringId
print("id:"+id)
with con:
cur = con.cursor()
cur.execute('DELETE FROM CARD WHERE ID = ? ', (id,))
print(cur)
popDelConfirm.hide()
self.entered_tab(stringId, cur)
def del_no_clicked_cb(self, cur):
popDelConfirm = builder.get_object("popDelConfirm")
popDelConfirm.hide()
def newImageBig_clicked_cb(self, cur, button):
imageBigNewWindow = builder.get_object("imageBigNewWindow")
imageBigNewWindow.show_all()
print("Front image Clicked")
imageBigger = builder.get_object("imageBigger")
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPath,width=300, height=300, preserve_aspect_ratio=True)
imageBigger.set_from_pixbuf(pixbuf)
imageBigger.show_all()
imageBigNewWindow.show_all()
def imgBigBack_clicked_cb(self, cur, button):
imageBigNewWindow = builder.get_object("imageBigNewWindow")
imageBigNewWindow.show_all()
print("Back image Clicked")
imageBigger = builder.get_object("imageBigger")
pixbufBack = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=photoPathBack,width=300, height=300, preserve_aspect_ratio=True)
imageBigger.set_from_pixbuf(pixbufBack)
imageBigger.show_all()
imageBigNewWindow.show_all()
def barcodeImgFile_clicked_cb(self, cur, button):
imageBigNewWindow = builder.get_object("imageBigNewWindow")
imageBigNewWindow.show_all()
print("Barcode image Clicked")
imageBigger = builder.get_object("imageBigger")
pixbufBack = GdkPixbuf.Pixbuf.new_from_file_at_scale(filename=barcodeImgFile+".png",width=300, height=300, preserve_aspect_ratio=True)
imageBigger.set_from_pixbuf(pixbufBack)
imageBigger.show_all()
imageBigNewWindow.show_all()
def hide_clicked_cb(self, cur, button):
print("Hide Clicked")
imageBigNewWindow = builder.get_object("imageBigNewWindow")
imageBigNewWindow.hide()
def entered_settings(self):
print("Entered settings")
def on_theme_activated(self, cur, provider):
css_path = path+"css/"
if cur.get_active() == False:
print("Theme on!")
css_path_class = os.path.join(css_path, "main.css")
self.provider.load_from_path(css_path_class)
f=open('savedConf.conf','w+')
f.write('main.css')
else:
print("Theme off!")
css_path_plain = os.path.join(css_path, "plain.css")
self.provider.load_from_path(css_path_plain)
f=open('savedConf.conf','w+')
f.write('plain.css')
def destroy_clicked_cb(self):
print("Asked to close window popup")
def quitButtonClicked(self, *args):
print("Close and quit loyaltyCardsOpen... Good luck! See you soon!")
Gtk.main_quit()
f=open(path+"savedConf.conf",'r')
fContent=f.read()
print("Loading default theme")
path = path+"css/"
css_path = os.path.join(path, fContent)
provider.load_from_path(css_path)
builder = Gtk.Builder()
builder.add_from_file(path+"gladeWindowDesign.glade")
builder.connect_signals(Handler())
window = builder.get_object("window1")
delete = builder.get_object("del")
edit = builder.get_object("edit")
savedImageInfo = builder.get_object("savedImageInfo")
#TODO Set window title
#window.set_title="Loyalty Cards Open"
window.show_all()
delete.hide()
edit.hide()
savedImageInfo.hide()
imageBigNewWindow = builder.get_object("imageBigNewWindow")
imageBigNewWindow.hide()
Gtk.main()