0
|
1 #!/usr/bin/env python3
|
|
2 # Postage Labels - aplikacja do aranżacji wydruków etykiet pocztowych
|
|
3 # autor: Przemysław R. Pietraszczyk
|
|
4 # licencja: GPL
|
|
5 # data 20-IX-2021
|
|
6 # edytor: Geany
|
|
7
|
|
8 import sys
|
|
9 import cairo
|
|
10 import gi, os
|
|
11 gi.require_version("Gtk", "3.0")
|
|
12 from gi.repository import Gtk, GdkPixbuf, Gdk
|
|
13 from gi.repository.GdkPixbuf import Pixbuf
|
|
14
|
|
15
|
|
16 UI_INFO = """
|
|
17 <ui>
|
|
18 <menubar name='MenuBar'>
|
|
19 <menu action='FileMenu'>
|
|
20 <menu action='FileLoad'>
|
|
21 <menuitem action='NewTemplate' />
|
|
22 <menuitem action='LoadImg' />
|
|
23 <menuitem action='Composite' />
|
|
24 </menu>
|
|
25 <menuitem action='FilePrint' />
|
|
26 <separator />
|
|
27 <menuitem action='FileQuit' />
|
|
28 </menu>
|
|
29 <menu action='InfoMenu'>
|
|
30 <menuitem action='Help'/>
|
|
31 <menuitem action='About'/>
|
|
32 </menu>
|
|
33 </menubar>
|
|
34
|
|
35 </ui>
|
|
36 """
|
|
37
|
|
38 grid = Gtk.Grid()
|
|
39 file_img_selected=""
|
|
40 file_label_save=""
|
|
41 new_pixbuf=""
|
|
42 list_labels = []
|
|
43 label_limits = []
|
|
44 print_limits = []
|
|
45 #label_selected = ""
|
|
46 BLANK_W = 1134
|
|
47 BLANK_H = 496
|
|
48
|
|
49
|
|
50 def get_resource_path(rel_path):
|
|
51 dir_of_py_file = os.path.dirname(__file__)
|
|
52 rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
|
|
53 abs_path_to_resource = os.path.abspath(rel_path_to_resource)
|
|
54 return abs_path_to_resource
|
|
55
|
|
56 class DialogCompositor(Gtk.Dialog):
|
|
57 def __init__(self, parent):
|
|
58 global new_pixbuf
|
|
59 Gtk.Dialog.__init__(self, title="Kompozytor", transient_for=parent, flags=0)
|
|
60 self.props.border_width = 20
|
|
61 self.add_buttons(
|
|
62 Gtk.STOCK_CANCEL,
|
|
63 Gtk.ResponseType.CANCEL,
|
|
64 Gtk.STOCK_SAVE,
|
|
65 Gtk.ResponseType.OK,
|
|
66 )
|
|
67 self.compos = Gtk.Grid()
|
|
68 file_img_composite=get_resource_path("/usr/share/postagelabels/blank_small.png")
|
|
69 new_pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
70 filename=file_img_composite,
|
|
71 width=500,
|
|
72 height=250,
|
|
73 preserve_aspect_ratio=True)
|
|
74
|
|
75 self.add(self.compos)
|
|
76
|
|
77 self.compos.set_row_spacing(10)
|
|
78 self.compos.set_column_spacing(10)
|
|
79 #self.compos.set_column_homogeneous(True) # rozszerza kontrolke na resztę okna
|
|
80
|
|
81 # potrzebne do wyśwetlenia tekstu
|
|
82 self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, new_pixbuf.get_width(), new_pixbuf.get_height())
|
|
83 self.context = cairo.Context(self.surface)
|
|
84
|
|
85 Gdk.cairo_set_source_pixbuf(self.context, new_pixbuf, 0, 0)
|
|
86 self.context.paint() #paint the pixbuf
|
|
87
|
|
88 self.fontsize= 25
|
|
89
|
|
90 box = self.get_content_area()
|
|
91
|
|
92 self.com_first = Gtk.Image.new_from_pixbuf(new_pixbuf)
|
|
93 self.compos.attach(self.com_first,0,0,1,3)
|
|
94
|
|
95
|
|
96 self.entry1 = Gtk.Entry()
|
|
97 self.entry1.set_text("Hello World")
|
|
98 self.compos.attach(self.entry1,1,0,1,1)
|
|
99
|
|
100
|
|
101 self.entry2 = Gtk.Entry()
|
|
102 self.entry2.set_text("Hello World")
|
|
103 self.compos.attach(self.entry2,1,1,1,1)
|
|
104
|
|
105 self.entry3 = Gtk.Entry()
|
|
106 self.entry3.set_text("Hello World")
|
|
107 self.compos.attach(self.entry3,1,2,1,1)
|
|
108
|
|
109 self.entry4 = Gtk.Entry()
|
|
110 self.entry4.set_text("Hello World")
|
|
111 self.compos.attach(self.entry4,1,3,1,1)
|
|
112
|
|
113 button1 = Gtk.Button.new_with_label("Aktualizuj")
|
|
114 button1.connect("clicked", self.actualization1)
|
|
115 self.compos.attach(button1,2,0,1,1)
|
|
116
|
|
117 button2 = Gtk.Button.new_with_label("Aktualizuj")
|
|
118 button2.connect("clicked", self.actualization2)
|
|
119 self.compos.attach(button2,2,1,1,1)
|
|
120
|
|
121 button3 = Gtk.Button.new_with_label("Aktualizuj")
|
|
122 button3.connect("clicked", self.actualization3)
|
|
123 self.compos.attach(button3,2,2,1,1)
|
|
124
|
|
125 button4 = Gtk.Button.new_with_label("Aktualizuj")
|
|
126 button4.connect("clicked", self.actualization4)
|
|
127 self.compos.attach(button4,2,3,1,1)
|
|
128
|
|
129
|
|
130 button_clear = Gtk.Button.new_with_label("Wyczyść")
|
|
131 button_clear.connect("clicked", self.entry_clear)
|
|
132 self.compos.attach(button_clear,3,0,1,4)
|
|
133 box.add(self.compos)
|
|
134
|
|
135 self.show_all()
|
|
136
|
|
137 def entry_clear(self, button):
|
|
138 global new_pixbuf
|
|
139 file_img_selected=get_resource_path("/usr/share/postagelabels/blank_small.png")
|
|
140 new_pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
141 filename=file_img_selected,
|
|
142 width=500,
|
|
143 height=250,
|
|
144 preserve_aspect_ratio=True)
|
|
145
|
|
146
|
|
147 if self.com_first != None:
|
|
148 self.compos.remove(self.com_first)
|
|
149 self.com_first=None
|
|
150 else:
|
|
151 self.compos.remove(self.com_new)
|
|
152
|
|
153 # czyści etykiete
|
|
154 self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, new_pixbuf.get_width(), new_pixbuf.get_height())
|
|
155 self.context = cairo.Context(self.surface)
|
|
156 Gdk.cairo_set_source_pixbuf(self.context, new_pixbuf, 0, 0)
|
|
157 self.context.paint() #paint the pixbuf
|
|
158
|
|
159
|
|
160 self.com_new = Gtk.Image.new_from_pixbuf(new_pixbuf)
|
|
161 self.compos.attach(self.com_new,0,0,1,3)
|
|
162
|
|
163 self.show_all()
|
|
164
|
|
165
|
|
166 def actualization1(self, button):
|
|
167 global new_pixbuf
|
|
168 self.context.move_to(15, 20+self.fontsize)
|
|
169 self.context.set_font_size(self.fontsize)
|
|
170 self.context.set_source_rgba(0,0,0,1)
|
|
171 self.context.show_text(self.entry1.get_text())
|
|
172
|
|
173 #get the resulting pixbuf
|
|
174 self.surface= self.context.get_target()
|
|
175 new_pixbuf= Gdk.pixbuf_get_from_surface(self.surface, 0, 0, self.surface.get_width(), self.surface.get_height())
|
|
176
|
|
177 if self.com_first != None:
|
|
178 self.compos.remove(self.com_first)
|
|
179 self.com_first=None
|
|
180 else:
|
|
181 self.compos.remove(self.com_new)
|
|
182
|
|
183
|
|
184 self.com_new = Gtk.Image.new_from_pixbuf(new_pixbuf)
|
|
185 self.compos.attach(self.com_new,0,0,1,3)
|
|
186
|
|
187 self.show_all()
|
|
188
|
|
189 def actualization2(self, button):
|
|
190 global new_pixbuf
|
|
191 self.context.move_to(15, 20+self.fontsize+self.fontsize+15)
|
|
192 self.context.set_font_size(self.fontsize)
|
|
193 self.context.set_source_rgba(0,0,0,1)
|
|
194 self.context.show_text(self.entry2.get_text())
|
|
195
|
|
196 #get the resulting pixbuf
|
|
197 self.surface= self.context.get_target()
|
|
198 new_pixbuf= Gdk.pixbuf_get_from_surface(self.surface, 0, 0, self.surface.get_width(), self.surface.get_height())
|
|
199
|
|
200 if self.com_first != None:
|
|
201 self.compos.remove(self.com_first)
|
|
202 self.com_first=None
|
|
203 else:
|
|
204 self.compos.remove(self.com_new)
|
|
205
|
|
206
|
|
207 self.com_new = Gtk.Image.new_from_pixbuf(new_pixbuf)
|
|
208 self.compos.attach(self.com_new,0,0,1,3)
|
|
209
|
|
210 self.show_all()
|
|
211 def actualization3(self, button):
|
|
212 global new_pixbuf
|
|
213 self.context.move_to(15, 20+self.fontsize+self.fontsize+self.fontsize+30)
|
|
214 self.context.set_font_size(self.fontsize)
|
|
215 self.context.set_source_rgba(0,0,0,1)
|
|
216 self.context.show_text(self.entry3.get_text())
|
|
217
|
|
218 #get the resulting pixbuf
|
|
219 self.surface= self.context.get_target()
|
|
220 new_pixbuf= Gdk.pixbuf_get_from_surface(self.surface, 0, 0, self.surface.get_width(), self.surface.get_height())
|
|
221
|
|
222 if self.com_first != None:
|
|
223 self.compos.remove(self.com_first)
|
|
224 self.com_first=None
|
|
225 else:
|
|
226 self.compos.remove(self.com_new)
|
|
227
|
|
228
|
|
229 self.com_new = Gtk.Image.new_from_pixbuf(new_pixbuf)
|
|
230 self.compos.attach(self.com_new,0,0,1,3)
|
|
231
|
|
232 self.show_all()
|
|
233
|
|
234 def actualization4(self, button):
|
|
235 global new_pixbuf
|
|
236 self.context.move_to(15, 20+self.fontsize+self.fontsize+self.fontsize+self.fontsize+50)
|
|
237 self.context.set_font_size(self.fontsize)
|
|
238 self.context.set_source_rgba(0,0,0,1)
|
|
239 self.context.show_text(self.entry4.get_text())
|
|
240
|
|
241 #get the resulting pixbuf
|
|
242 self.surface= self.context.get_target()
|
|
243 new_pixbuf= Gdk.pixbuf_get_from_surface(self.surface, 0, 0, self.surface.get_width(), self.surface.get_height())
|
|
244
|
|
245 if self.com_first != None:
|
|
246 self.compos.remove(self.com_first)
|
|
247 self.com_first=None
|
|
248 else:
|
|
249 self.compos.remove(self.com_new)
|
|
250
|
|
251
|
|
252 self.com_new = Gtk.Image.new_from_pixbuf(new_pixbuf)
|
|
253 self.compos.attach(self.com_new,0,0,1,3)
|
|
254
|
|
255 self.show_all()
|
|
256
|
|
257
|
|
258 class DialogHelp(Gtk.Dialog):
|
|
259 def __init__(self, parent):
|
|
260 Gtk.Dialog.__init__(self, title="Pomoc", transient_for=parent, flags=0)
|
|
261 self.props.border_width = 20
|
|
262 self.add_buttons(
|
|
263 Gtk.STOCK_OK, Gtk.ResponseType.OK
|
|
264 )
|
|
265
|
|
266 self.set_default_size(150, 100)
|
|
267 label1 = Gtk.Label(label="Aplikacja służąca do aranżacji wydruku etykiet pocztowych.\nMożna zrówno tworzyć jak i wczytywać gotowe etykiety,\n a następnie umieszczać jena siatce.\nAplikacja bierze poprawkę dla drukarki Epson serii L,\nz ramką wokół wydruku.\n")
|
|
268 label2 = Gtk.Label(label="Pojedyńcza etykieta powinna mieć wymiary: 1134x496 px.\nKomptabilny papier pochodzi z zasobów \'Galerii Papieru\'\n\nNa siatce etykiete dodajemy \'lpm\', a usuwamy \'ppm\'.\n\nAplikacja na ekranach HD Ready będzie zachowywać się\nniestabilnie! Zalecany ekran to Full HD.")
|
|
269 box = self.get_content_area()
|
|
270 box.add(label1)
|
|
271 box.add(label2)
|
|
272 self.show_all()
|
|
273
|
|
274 class DialogAbout(Gtk.Dialog):
|
|
275 def __init__(self, parent):
|
|
276 Gtk.Dialog.__init__(self, title="O Programie", transient_for=parent, flags=0)
|
|
277 self.props.border_width = 20
|
|
278 self.add_buttons(
|
|
279 Gtk.STOCK_OK, Gtk.ResponseType.OK
|
|
280 )
|
|
281
|
|
282 self.set_default_size(150, 100)
|
|
283
|
|
284 label = Gtk.Label(label="\tAplikacja napisana na podstawie:\n\nhttps://python-gtk-3-tutorial.readthedocs.io/\n\n\t\tWersja: 0.220120-2\n\n\t\tPrzemysław R. Pietraszczyk\n\n\t\t\tWrzesień 2021\n\t\t\tLicencja: GPL\n\n")
|
|
285
|
|
286 box = self.get_content_area()
|
|
287 box.add(label)
|
|
288
|
|
289 button = Gtk.LinkButton("http://www.prp.xlx.pl", label="Strona Domowa")
|
|
290 box.add(button)
|
|
291
|
|
292 self.show_all()
|
|
293
|
|
294
|
|
295
|
|
296
|
|
297 class DialogMessage(Gtk.Dialog):
|
|
298 def __init__(self, parent):
|
|
299 super().__init__(title="Błąd zaznaczenia", transient_for=parent, flags=0)
|
|
300 self.add_buttons(
|
|
301 Gtk.STOCK_OK, Gtk.ResponseType.OK
|
|
302
|
|
303 )
|
|
304
|
|
305 self.set_default_size(150, 100)
|
|
306
|
|
307 label = Gtk.Label(label="Powinieneś najperw zaznaczyć etykiete,\nz listy dostępnych etykiet.")
|
|
308
|
|
309 box = self.get_content_area()
|
|
310 box.add(label)
|
|
311 self.show_all()
|
|
312
|
|
313
|
|
314
|
|
315
|
|
316 class FileChooserIMG(Gtk.Window):
|
|
317 def __init__(self):
|
|
318 Gtk.Window.__init__(self, title="Wybór plików graficznych")
|
|
319 global file_img_selected
|
|
320
|
|
321 dialog = Gtk.FileChooserDialog(title="Please choose a file", parent=self, action=Gtk.FileChooserAction.OPEN)
|
|
322 dialog.add_buttons(
|
|
323 Gtk.STOCK_CANCEL,
|
|
324 Gtk.ResponseType.CANCEL,
|
|
325 Gtk.STOCK_OPEN,
|
|
326 Gtk.ResponseType.OK,
|
|
327 )
|
|
328
|
|
329 self.add_filters(dialog)
|
|
330
|
|
331 response = dialog.run()
|
|
332 if response == Gtk.ResponseType.OK:
|
|
333 print("Open clicked")
|
|
334 print("File selected: " + dialog.get_filename())
|
|
335 file_img_selected=dialog.get_filename()
|
|
336 list_labels.append(file_img_selected)
|
|
337 elif response == Gtk.ResponseType.CANCEL:
|
|
338 print("Cancel clicked")
|
|
339
|
|
340 dialog.destroy()
|
|
341
|
|
342 def add_filters(self, dialog):
|
|
343 filter_png = Gtk.FileFilter()
|
|
344 filter_png.set_name("Pliki PNG")
|
|
345 filter_png.add_mime_type("image/png")
|
|
346 dialog.add_filter(filter_png)
|
|
347
|
|
348 filter_jpeg = Gtk.FileFilter()
|
|
349 filter_jpeg.set_name("Pliki JPEG")
|
|
350 filter_jpeg.add_mime_type("image/jpeg")
|
|
351 dialog.add_filter(filter_jpeg)
|
|
352
|
|
353
|
|
354
|
|
355
|
|
356 class FileChooserLabel(Gtk.Window):
|
|
357 def __init__(self, this):
|
|
358 global glob_liststore
|
|
359 Gtk.Window.__init__(self, title="Zapis etykiety")
|
|
360 global file_label_save
|
|
361
|
|
362 dialog = Gtk.FileChooserDialog(title="Please choose a file", parent=self, action=Gtk.FileChooserAction.SAVE)
|
|
363 dialog.add_buttons(
|
|
364 Gtk.STOCK_CANCEL,
|
|
365 Gtk.ResponseType.CANCEL,
|
|
366 Gtk.STOCK_SAVE,
|
|
367 Gtk.ResponseType.OK,
|
|
368 )
|
|
369
|
|
370 self.add_filters(dialog)
|
|
371
|
|
372 response = dialog.run()
|
|
373 if response == Gtk.ResponseType.OK:
|
|
374 print("Open clicked")
|
|
375 print("File selected: " + dialog.get_filename())
|
|
376 file_label_save=dialog.get_filename()
|
|
377 pixbuf2 = new_pixbuf.scale_simple(1134, 496, GdkPixbuf.InterpType.BILINEAR)
|
|
378 pixbuf2.savev(file_label_save,"png", ["quality"], ["100"])
|
|
379 this.available_labels(file_label_save)
|
|
380
|
|
381
|
|
382 elif response == Gtk.ResponseType.CANCEL:
|
|
383 print("Cancel clicked")
|
|
384
|
|
385 dialog.destroy()
|
|
386
|
|
387 def add_filters(self, dialog):
|
|
388 filter_png = Gtk.FileFilter()
|
|
389 filter_png.set_name("Pliki PNG")
|
|
390 filter_png.add_mime_type("image/png")
|
|
391 dialog.add_filter(filter_png)
|
|
392 """
|
|
393 filter_jpeg = Gtk.FileFilter()
|
|
394 filter_jpeg.set_name("Pliki JPEG")
|
|
395 filter_jpeg.add_mime_type("image/jpeg")
|
|
396 dialog.add_filter(filter_jpeg)
|
|
397 """
|
|
398
|
|
399
|
|
400
|
|
401
|
|
402
|
|
403
|
|
404
|
|
405 class MyWindow(Gtk.Window):
|
|
406
|
|
407 def __init__(self):
|
|
408 super().__init__()
|
|
409
|
|
410 self.init_ui()
|
|
411
|
|
412
|
|
413 def refresh_label_view(self):
|
|
414
|
|
415 if self.image2 != None:
|
|
416 grid.remove(self.image2)
|
|
417 self.image2=None
|
|
418 else:
|
|
419 grid.remove(self.image_new)
|
|
420
|
|
421
|
|
422 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
423 filename=self.label_selected,
|
|
424 width=500,
|
|
425 height=250,
|
|
426 preserve_aspect_ratio=True)
|
|
427
|
|
428 self.image_new = Gtk.Image.new_from_pixbuf(pixbuf)
|
|
429 grid.attach(self.image_new,0,1,1,1)
|
|
430
|
|
431 self.show_all()
|
|
432
|
|
433 def on_menu_file_new_template(self, widget):
|
|
434
|
|
435 n = 0
|
|
436 file_img_selected=get_resource_path("/usr/share/postagelabels/blank_small.png")
|
|
437 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
438 filename=file_img_selected,
|
|
439 width=250,
|
|
440 height=117, #125,
|
|
441 preserve_aspect_ratio=True)
|
|
442
|
|
443
|
|
444 for l in label_limits:
|
|
445
|
|
446 print ("IMAGE2: "+str(pixbuf))
|
|
447 if pixbuf is not None:
|
|
448 self.image_template[n].set_from_pixbuf(pixbuf)
|
|
449 print_limits[n][2] = None
|
|
450
|
|
451
|
|
452
|
|
453 n += 1
|
|
454 self.show_all() # najprawdopodobnie wymagane po pakietowaniu
|
|
455
|
|
456 def on_menu_file_load_img_generic(self, widget):
|
|
457 global grid
|
|
458 filename=FileChooserIMG()
|
|
459
|
|
460 if len(file_img_selected) != 0:
|
|
461
|
|
462 if self.image2 != None:
|
|
463 grid.remove(self.image2)
|
|
464 self.image2=None
|
|
465 else:
|
|
466 grid.remove(self.image_new)
|
|
467
|
|
468
|
|
469 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
470 filename=file_img_selected,
|
|
471 width=500,
|
|
472 height=250,
|
|
473 preserve_aspect_ratio=True)
|
|
474
|
|
475 self.image_new = Gtk.Image.new_from_pixbuf(pixbuf)
|
|
476
|
|
477 grid.attach(self.image_new,0,1,1,1)
|
|
478
|
|
479
|
|
480 self.scrolled_window.remove(self.treeview)
|
|
481 grid.remove(self.scrolled_window)
|
|
482 self.liststore = Gtk.ListStore(str, str)
|
|
483
|
|
484 self.treeview.remove_column(self.column_text)
|
|
485
|
|
486 for o in list_labels:
|
|
487 self.liststore.append([o,""])
|
|
488
|
|
489 self.scrolled_window = Gtk.ScrolledWindow ()
|
|
490 self.scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
|
|
491
|
|
492 self.treeview = Gtk.TreeView(model=self.liststore)
|
|
493
|
|
494 self.renderer_text = Gtk.CellRendererText()
|
|
495 self.column_text = Gtk.TreeViewColumn("Dostępne etykiety", self.renderer_text, text=0)
|
|
496 self.treeview.append_column(self.column_text)
|
|
497
|
|
498 self.treeview.connect("button_release_event", self.mouse_selected)
|
|
499
|
|
500 self.scrolled_window.add (self.treeview)
|
|
501 grid.attach(self.scrolled_window,0,2,1,6)
|
|
502
|
|
503 self.show_all()
|
|
504
|
|
505 def on_menu_composite_generic(self, widget):
|
|
506 dialog = DialogCompositor(self)
|
|
507 response = dialog.run()
|
|
508
|
|
509 if response == Gtk.ResponseType.OK:
|
|
510 print("Save clicked")
|
|
511 dialog_file_label=FileChooserLabel(self)
|
|
512 elif response == Gtk.ResponseType.CANCEL:
|
|
513 print("Cancel clicked")
|
|
514
|
|
515 dialog.destroy()
|
|
516
|
|
517 def on_menu_file_quit(self, widget):
|
|
518 Gtk.main_quit()
|
|
519
|
|
520 def on_menu_help(self, widget):
|
|
521 dialog = DialogHelp(self)
|
|
522 response = dialog.run()
|
|
523
|
|
524 dialog.destroy()
|
|
525
|
|
526 def on_menu_about(self, widget):
|
|
527 dialog = DialogAbout(self)
|
|
528 response = dialog.run()
|
|
529
|
|
530 dialog.destroy()
|
|
531
|
|
532
|
|
533 def add_info_menu_actions(self, action_group):
|
|
534 action_info_menu = Gtk.Action(name="InfoMenu", label="Info")
|
|
535 action_group.add_action(action_info_menu)
|
|
536
|
|
537 action_new = Gtk.Action(
|
|
538 name="Help",
|
|
539 label="Pomoc",
|
|
540 tooltip="Create a new file",
|
|
541 )
|
|
542 action_new.connect("activate", self.on_menu_help)
|
|
543 action_group.add_action_with_accel(action_new, None)
|
|
544
|
|
545 action_new = Gtk.Action(
|
|
546 name="About",
|
|
547 label="O Programie",
|
|
548 tooltip="Create a new file",
|
|
549 )
|
|
550 action_new.connect("activate", self.on_menu_about)
|
|
551 action_group.add_action_with_accel(action_new, None)
|
|
552
|
|
553 def add_file_menu_actions(self, action_group):
|
|
554 action_filemenu = Gtk.Action(name="FileMenu", label="Plik")
|
|
555 action_group.add_action(action_filemenu)
|
|
556
|
|
557 action_fileloadmenu = Gtk.Action(name="FileLoad", stock_id=Gtk.STOCK_NEW)
|
|
558 action_group.add_action(action_fileloadmenu)
|
|
559
|
|
560 action_new = Gtk.Action(
|
|
561 name="LoadImg",
|
|
562 label="Załaduj etykiete",
|
|
563 tooltip="Wczytuje etykiete",
|
|
564 )
|
|
565 action_new.connect("activate", self.on_menu_file_load_img_generic)
|
|
566 action_group.add_action_with_accel(action_new, None)
|
|
567
|
|
568
|
|
569 action_new = Gtk.Action(
|
|
570 name="NewTemplate",
|
|
571 label="Nowy szablon",
|
|
572 tooltip="Tworzy szablon na nowo",
|
|
573 )
|
|
574 action_new.connect("activate", self.on_menu_file_new_template)
|
|
575 action_group.add_action_with_accel(action_new, None)
|
|
576
|
|
577
|
|
578 action_new = Gtk.Action(
|
|
579 name="Composite",
|
|
580 label="Kompozytor",
|
|
581 tooltip="Tworzy nową etykiete",
|
|
582 )
|
|
583 action_new.connect("activate", self.on_menu_composite_generic)
|
|
584 action_group.add_action_with_accel(action_new, None)
|
|
585
|
|
586
|
|
587 action_print = Gtk.Action(name="FilePrint", stock_id=Gtk.STOCK_PRINT)
|
|
588 action_print.connect("activate", self.print_image)
|
|
589 action_group.add_action(action_print)
|
|
590
|
|
591
|
|
592 action_filequit = Gtk.Action(name="FileQuit", stock_id=Gtk.STOCK_QUIT)
|
|
593 action_filequit.connect("activate", self.on_menu_file_quit)
|
|
594 action_group.add_action(action_filequit)
|
|
595
|
|
596
|
|
597
|
|
598 # tworzy menu bar
|
|
599 def create_ui_manager(self):
|
|
600 uimanager = Gtk.UIManager()
|
|
601
|
|
602 # Throws exception if something went wrong
|
|
603 uimanager.add_ui_from_string(UI_INFO)
|
|
604
|
|
605 # Add the accelerator group to the toplevel window
|
|
606 accelgroup = uimanager.get_accel_group()
|
|
607 self.add_accel_group(accelgroup)
|
|
608 return uimanager
|
|
609
|
|
610
|
|
611
|
|
612 def mouse_selected(self, tv, event):
|
|
613
|
|
614 if event.button == 1:
|
|
615
|
|
616 # Begin added code
|
|
617 pthinfo = self.treeview.get_path_at_pos(event.x, event.y)
|
|
618 if pthinfo != None:
|
|
619 path,col,cellx,celly = pthinfo
|
|
620 self.treeview.grab_focus()
|
|
621 self.treeview.set_cursor(path,col,0)
|
|
622 # End added code
|
|
623
|
|
624 selection = self.treeview.get_selection()
|
|
625 (model, iter) = selection.get_selected()
|
|
626 print(model[iter][0])
|
|
627 self.label_selected = model[iter][0]
|
|
628
|
|
629 self.refresh_label_view()
|
|
630
|
|
631
|
|
632 def onclick_in_pixbuf (self, box, event):
|
|
633 if event.button == 1:
|
|
634 try:
|
|
635 print ("\nKliknieto na pozycji")
|
|
636 print (event.x, event.y)
|
|
637 print ("\n")
|
|
638 e = [int(event.x), int(event.y)]
|
|
639 n = 0
|
|
640
|
|
641 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
642 filename=self.label_selected,
|
|
643 width=250,
|
|
644 height=117, #125,
|
|
645 preserve_aspect_ratio=True)
|
|
646
|
|
647
|
|
648 for l in label_limits:
|
|
649
|
|
650 x, y = l[:]
|
|
651 e_x, e_y = e[:]
|
|
652
|
|
653 if e_x >= x and e_y >= y and e_x <= x+250 and e_y <= y+117:
|
|
654 if pixbuf is not None:
|
|
655 self.image_template[n].set_from_pixbuf(pixbuf)
|
|
656 print_limits[n][2] = self.label_selected
|
|
657
|
|
658 break
|
|
659 n += 1
|
|
660 except AttributeError:
|
|
661
|
|
662 dialog = DialogMessage(self)
|
|
663 response = dialog.run()
|
|
664
|
|
665
|
|
666 dialog.destroy()
|
|
667 if event.button == 3:
|
|
668
|
|
669
|
|
670 e = [int(event.x), int(event.y)]
|
|
671 n = 0
|
|
672
|
|
673 file_img_selected=get_resource_path("/usr/share/postagelabels/blank_small.png")
|
|
674 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
675 filename=file_img_selected,
|
|
676 width=250,
|
|
677 height=117, #125,
|
|
678 preserve_aspect_ratio=True)
|
|
679
|
|
680
|
|
681 for l in label_limits:
|
|
682
|
|
683 x, y = l[:]
|
|
684 e_x, e_y = e[:]
|
|
685
|
|
686 if e_x >= x and e_y >= y and e_x <= x+250 and e_y <= y+117:
|
|
687 print ("IMAGE2: "+str(pixbuf))
|
|
688 if pixbuf is not None:
|
|
689 self.image_template[n].set_from_pixbuf(pixbuf)
|
|
690 print_limits[n][2] = None
|
|
691
|
|
692
|
|
693 break
|
|
694 n += 1
|
|
695 self.show_all() # najprawdopodobnie wymagane po pakietowaniu
|
|
696
|
|
697 def on_mouse_move_in_pixbuf (self, box, event):
|
|
698 e = [int(event.x), int(event.y)]
|
|
699 n = 0
|
|
700 for l in label_limits:
|
|
701
|
|
702 x, y = l[:]
|
|
703 e_x, e_y = e[:]
|
|
704
|
|
705 if e_x >= x and e_y >= y and e_x <= x+200 and e_y <= y+92:
|
|
706 pass
|
|
707
|
|
708 # dnae dla wydruku
|
|
709 def set_print_limits(self):
|
|
710 start_x = 0
|
|
711 start_y = 0
|
|
712 end_x = BLANK_W * 2 #1234*2
|
|
713 end_y = BLANK_H * 6 #3508
|
|
714 cols = 2
|
|
715 rows = 6
|
|
716 width = BLANK_W #1234
|
|
717 height = BLANK_H #585
|
|
718
|
|
719 for j in range (start_y, end_y, height):
|
|
720 for i in range (start_x, end_x, width):
|
|
721 print_limits.append([i, j, None])
|
|
722
|
|
723 print ("PRINT LIMIST")
|
|
724 print (print_limits)
|
|
725
|
|
726
|
|
727 # dane dla siatki
|
|
728 def set_label_limits(self):
|
|
729 start_x = 0
|
|
730 start_y = 0
|
|
731 end_x = 500
|
|
732 end_y = 104 * 6 #117 * 6 #700
|
|
733 cols = 2
|
|
734 rows = 6
|
|
735 width = 250
|
|
736 height = 104 #117
|
|
737
|
|
738 for j in range (start_y, end_y, height):
|
|
739 for i in range (start_x, end_x, width):
|
|
740 label_limits.append([i, j])
|
|
741
|
|
742
|
|
743 def print_page(self, operation=None, context=None, page_nr=None):
|
|
744
|
|
745 ctx = context.get_cairo_context()
|
|
746
|
|
747 # make cairo ImageSurface from the png file
|
|
748 surface = cairo.ImageSurface.create_from_png('/tmp/postagelabels.png')
|
|
749
|
|
750 #ctx.set_operator(cairo.OPERATOR_SOURCE)
|
|
751 ctx.set_source_surface(surface)
|
|
752 ctx.paint ()
|
|
753 #ctx.set_operator(cairo.OPERATOR_OVER)
|
|
754 os.remove("/tmp/postagelabels.png");
|
|
755
|
|
756
|
|
757
|
|
758 #page = self.doc.get_page(page_nr)
|
|
759 #page.render(ctx)
|
|
760
|
|
761
|
|
762
|
|
763
|
|
764
|
|
765 def print_image(self, widget):
|
|
766 print ("PRINT LIMITS")
|
|
767 print (print_limits)
|
|
768
|
|
769 page_width = 210
|
|
770 page_height = 297
|
|
771 #page_margin_top = 20
|
|
772 dpi = 600
|
|
773 #_mm_dpi = 72 / 25.4
|
|
774
|
|
775
|
|
776
|
|
777
|
|
778 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size('/usr/share/postagelabels/szablon.png', 2480, 3508)
|
|
779
|
|
780 for i in range (12):
|
|
781 if print_limits[i][2] != None:
|
|
782
|
|
783 if i%2 == 1:
|
|
784 center_beam = 24
|
|
785 else:
|
|
786 center_beam = 0
|
|
787
|
|
788 label = GdkPixbuf.Pixbuf.new_from_file_at_size(print_limits[i][2], BLANK_W, BLANK_H)
|
|
789 #self.freeze_child_notify()
|
|
790 label.composite(pixbuf, print_limits[i][0]+89+center_beam, print_limits[i][1]+237, label.get_width(), label.get_height(), print_limits[i][0]+89+center_beam, print_limits[i][1]+237, 1, 1, GdkPixbuf.InterpType.NEAREST, 255)
|
|
791 #self.thaw_child_notify()
|
|
792
|
|
793 # z orginalnymi wielkościami nie chce drukowac
|
|
794 pixbuf2 = pixbuf.scale_simple(600, 855, GdkPixbuf.InterpType.BILINEAR)
|
|
795
|
|
796 pixbuf2.savev("/tmp/postagelabels.png","png", ["quality"], ["100"])
|
|
797 """
|
|
798 paper_size = Gtk.PaperSize.new_custom("1.0x", "2480 x 3508", 2480, 3508, Gtk.Unit.POINTS)
|
|
799
|
|
800 page_setup = Gtk.PageSetup()
|
|
801 page_setup.set_paper_size(paper_size)
|
|
802 pd = Gtk.PrintOperation()
|
|
803 #pd.set_embed_page_setup(True)
|
|
804 pd.set_default_page_setup(page_setup)
|
|
805 #pd.set_unit(Gtk.Unit.POINTS)
|
|
806 pd.set_n_pages(1)
|
|
807 pd.connect("draw_page", self.print_page)
|
|
808 result = pd.run(Gtk.PrintOperationAction.PRINT_DIALOG, None) # window zamisat None
|
|
809 print(result) # handle errors etc.
|
|
810 """
|
|
811
|
|
812
|
|
813 #ps= Gtk.PaperSize.new_custom("1.0x", "2480 x 3508", 2480, 3508, Gtk.Unit.POINTS)
|
|
814
|
|
815 ps = Gtk.PaperSize.new_custom("1.0x", "cc", page_width, page_height, Gtk.Unit.MM)
|
|
816 print_settings = Gtk.PrintSettings()
|
|
817 print_settings.set_resolution(dpi)
|
|
818
|
|
819 page_setup = Gtk.PageSetup()
|
|
820 page_setup.set_paper_size(ps)
|
|
821 page_setup.set_bottom_margin(0.0, Gtk.Unit.MM)
|
|
822 page_setup.set_left_margin(0.0, Gtk.Unit.MM)
|
|
823 page_setup.set_right_margin(0.0, Gtk.Unit.MM)
|
|
824 page_setup.set_top_margin(0.0, Gtk.Unit.MM)
|
|
825 page_setup.set_orientation(Gtk.PageOrientation.PORTRAIT)
|
|
826
|
|
827
|
|
828 print_operation = Gtk.PrintOperation()
|
|
829 print_operation.set_n_pages(1)
|
|
830 print_operation.set_default_page_setup(page_setup)
|
|
831 print_operation.set_print_settings(print_settings)
|
|
832 print_operation.connect("draw_page", self.print_page)
|
|
833 #print_operation.set_export_filename("example.pdf")
|
|
834
|
|
835 result = print_operation.run(Gtk.PrintOperationAction.PRINT_DIALOG, None) # window zamisat None
|
|
836
|
|
837 #result = print_operation.run(Gtk.PrintOperationAction.PREVIEW, None)
|
|
838 print(result)
|
|
839
|
|
840 def available_labels(self, name):
|
|
841
|
|
842 self.scrolled_window.remove(self.treeview)
|
|
843 grid.remove(self.scrolled_window)
|
|
844 self.liststore = Gtk.ListStore(str, str)
|
|
845
|
|
846 self.treeview.remove_column(self.column_text)
|
|
847
|
|
848 list_labels.append(name)
|
|
849 for o in list_labels:
|
|
850 self.liststore.append([o,""])
|
|
851
|
|
852 self.scrolled_window = Gtk.ScrolledWindow ()
|
|
853 self.scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
|
|
854
|
|
855 self.treeview = Gtk.TreeView(model=self.liststore)
|
|
856
|
|
857 self.renderer_text = Gtk.CellRendererText()
|
|
858 self.column_text = Gtk.TreeViewColumn("Dostępne etykiety", self.renderer_text, text=0)
|
|
859 self.treeview.append_column(self.column_text)
|
|
860
|
|
861 self.treeview.connect("button_release_event", self.mouse_selected)
|
|
862
|
|
863 self.scrolled_window.add (self.treeview)
|
|
864 grid.attach(self.scrolled_window,0,2,1,6)
|
|
865
|
|
866 self.show_all()
|
|
867
|
|
868
|
|
869 def init_ui(self):
|
|
870 global grid
|
|
871
|
|
872
|
|
873
|
|
874 self.image_new=None
|
|
875 self.props.border_width = 20
|
|
876
|
|
877 self.add(grid)
|
|
878
|
|
879 grid.set_row_spacing(10)
|
|
880 grid.set_column_spacing(10)
|
|
881 grid.set_column_homogeneous(True) # rozszerza kontrolke z utworami na resztę okna
|
|
882
|
|
883 action_group = Gtk.ActionGroup(name="my_actions")
|
|
884
|
|
885 self.add_file_menu_actions(action_group)
|
|
886 self.add_info_menu_actions(action_group)
|
|
887
|
|
888 uimanager = self.create_ui_manager()
|
|
889 uimanager.insert_action_group(action_group)
|
|
890
|
|
891 menubar = uimanager.get_widget("/MenuBar")
|
|
892 box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
893 box.pack_start(menubar, False, False, 0)
|
|
894
|
|
895
|
|
896 grid.attach(box, 0, 0, 2, 1)
|
|
897
|
|
898
|
|
899
|
|
900 event_box = Gtk.EventBox ()
|
|
901 event_box.connect ('button-press-event', self.onclick_in_pixbuf)
|
|
902 event_box.connect("motion-notify-event", self.on_mouse_move_in_pixbuf)
|
|
903 event_box.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
|
|
904
|
|
905 self.set_label_limits()
|
|
906 self.set_print_limits()
|
|
907 frame = Gtk.Frame()
|
|
908
|
|
909
|
|
910 file_img_selected=get_resource_path("/usr/share/postagelabels/blank_small.png")
|
|
911 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
912 filename=file_img_selected,
|
|
913 width=250,
|
|
914 height=117, #125
|
|
915 preserve_aspect_ratio=True)
|
|
916
|
|
917
|
|
918 self.all_img_template = Gtk.Grid()
|
|
919
|
|
920 self.image_template = []
|
|
921 self.image_template= [0 for i in range(12)]
|
|
922
|
|
923
|
|
924 left = 0
|
|
925 top = 1
|
|
926 top_next=False
|
|
927 multiplier = 0
|
|
928 n_multi = 1;
|
|
929 first = False
|
|
930 n = 2
|
|
931 for i in range(12):
|
|
932 print ("left: "+str(left)+" top: "+str(top))
|
|
933
|
|
934 if left == 2:
|
|
935 left = 0;
|
|
936 n_multi += 1
|
|
937 print ("multi: " +str(n_multi)+" %"+ str(n_multi%4))
|
|
938 if n_multi%2== 0:
|
|
939 if first == False:
|
|
940 multiplier = 1
|
|
941 first = True
|
|
942 multiplier += n
|
|
943 n *= 2
|
|
944 self.image_template[i] = Gtk.Image.new_from_pixbuf(pixbuf)
|
|
945 self.all_img_template.attach(self.image_template[i], left, top+multiplier, 1 , top+multiplier)
|
|
946 left += 1
|
|
947 if top_next == False:
|
|
948 top_next = True
|
|
949 pass
|
|
950 else:
|
|
951 top_next = False
|
|
952 top +=1
|
|
953
|
|
954 event_box.add(self.all_img_template)
|
|
955
|
|
956
|
|
957 frame.add(event_box)
|
|
958 grid.attach(frame,1,1,1,6)
|
|
959
|
|
960
|
|
961
|
|
962 # powiększenie etykiety po lewej stronie
|
|
963 file_img_selected=get_resource_path("/usr/share/postagelabels/blank_small.png")
|
|
964 pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
|
|
965 filename=file_img_selected,
|
|
966 width=500,
|
|
967 height=250,
|
|
968 preserve_aspect_ratio=True)
|
|
969
|
|
970 self.image2 = Gtk.Image.new_from_pixbuf(pixbuf)
|
|
971
|
|
972 grid.attach(self.image2,0,1,1,1)
|
|
973
|
|
974
|
|
975
|
|
976 self.liststore = Gtk.ListStore(str, str)
|
|
977 self.treeview = Gtk.TreeView(model=self.liststore)
|
|
978 self.treeview.connect("button_release_event", self.mouse_selected)
|
|
979
|
|
980
|
|
981
|
|
982 self.renderer_text = Gtk.CellRendererText()
|
|
983 self.column_text = Gtk.TreeViewColumn("Dostępne etykiety", self.renderer_text, text=0)
|
|
984 self.treeview.append_column(self.column_text)
|
|
985
|
|
986 self.scrolled_window = Gtk.ScrolledWindow ()
|
|
987 self.scrolled_window.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
|
|
988
|
|
989 self.scrolled_window.add (self.treeview)
|
|
990 grid.attach(self.scrolled_window,0,2,1,6)
|
|
991
|
|
992
|
|
993
|
|
994
|
|
995 self.set_border_width(10)
|
|
996 self.set_title("Naklejki Pocztowe")
|
|
997 self.set_default_size(1000, 655)
|
|
998 self.connect("destroy", Gtk.main_quit)
|
|
999
|
|
1000
|
|
1001
|
|
1002 win = MyWindow()
|
|
1003 win.show_all()
|
|
1004 Gtk.main()
|
|
1005
|
|
1006
|
|
1007
|
|
1008
|
|
1009
|
|
1010
|
|
1011
|
|
1012
|