zaz-barcode/source/pythonpath/main.py

175 lines
3.5 KiB
Python

#!/usr/bin/env python3
import easymacro as app
import qrcode
import qrcode.image.svg as svg
from barcode import generate
ID_EXTENSION = ''
_ = None
TITLE = 'ZAZ BarCode'
QR = 'qrcode'
class Controllers(object):
def __init__(self, dlg):
self.d = dlg
def listbox_item_changed(self, event):
self.d.text.set_focus()
return
def button_action(self, event):
if not self.d.listbox.value:
self.d.listbox.set_focus()
msg = _('Select barcode type')
app.warning(msg, TITLE)
return
if not self.d.text.value.strip():
self.d.text.set_focus()
msg = _('Data field is mandatory')
app.warning(msg, TITLE)
return
self.d.close(1)
return
@app.catch_exception
def _use_dialog():
dlg = _create_dialog()
if not dlg.open():
return
data = dlg.text.value.strip()
type_code = dlg.listbox.value
path = _make_code(type_code, data)
app.selection.insert_image(path)
app.paths.kill(path)
return
def _make_code(type_code, data):
path = app.paths.tmp()
if type_code == QR:
factory = svg.SvgImage
img = qrcode.make(data, border=2, image_factory=factory)
img.save(path)
else:
try:
generate(type_code, data, output=open(path, 'wb'))
except Exception as e:
app.errorbox(e)
return ''
return path
def _create_dialog():
args = {
'Name': 'dialog',
'Title': TITLE,
'Width': 160,
'Height': 160,
}
dlg = app.create_dialog(args)
dlg.id = ID_EXTENSION
dlg.events = Controllers
args = {
'Type': 'Label',
'Name': 'lbl_barcode',
'Label': _('~Select barcode type'),
'Width': 70,
'Height': 15,
'X': 10,
'Y': 10,
'VerticalAlign': 1,
}
dlg.add_control(args)
args = {
'Type': 'Label',
'Name': 'lbl_data',
'Label': _('~Capture data for barcode'),
'Width': 100,
'Height': 15,
'VerticalAlign': 1,
}
dlg.add_control(args)
args = {
'Type': 'ListBox',
'Name': 'listbox',
'Width': 65,
'Height': 15,
'Dropdown': True,
}
dlg.add_control(args)
data = ('code39', 'code128', 'ean', 'ean8', 'ean13', 'gs1', 'gtin',
'isbn', 'isbn10', 'isbn13', 'issn', 'jan', 'pzn', 'upc', 'upca',
'qrcode')
dlg.listbox.data = data
args = {
'Type': 'Text',
'Name': 'text',
'Width': 140,
'Height': 75,
'MultiLine': True,
'VScroll': True,
}
dlg.add_control(args)
args = {
'Type': 'Button',
'Name': 'button',
'Label': _('~Insert Barcode'),
'Width': 70,
'Height': 15,
'ImageURL': 'qr.png',
'ImagePosition': 1,
}
dlg.add_control(args)
dlg.listbox.move(dlg.lbl_barcode, 5, 0)
dlg.lbl_data.move(dlg.lbl_barcode)
dlg.text.move(dlg.lbl_data)
dlg.button.move(dlg.text, 0, 10)
dlg.button.center()
return dlg
@app.catch_exception
def _insert_code(type_code):
sel = app.selection
data = sel.value
if not data:
msg = _('Select data')
app.errorbox(msg)
return
path = _make_code(type_code, data)
sel.offset().insert_image(path)
app.paths.kill(path)
return
def run(args, path_locales):
global _
_ = app.install_locales(path_locales)
if args == 'used_dialog':
_use_dialog()
else:
_insert_code(args)
return