zaz-barcode/source/ZAZBarCode.py

250 lines
6.2 KiB
Python
Raw Normal View History

2019-09-14 15:19:45 -05:00
import gettext
2019-09-09 22:42:38 -05:00
import uno
import unohelper
from com.sun.star.task import XJobExecutor, XJob
2019-09-14 15:19:45 -05:00
import easymacro as app
2019-09-09 22:42:38 -05:00
import qrcode
import qrcode.image.svg as svg
from barcode import generate
ID_EXTENSION = 'net.elmau.zaz.BarCode'
SERVICE = ('com.sun.star.task.Job',)
TITLE = 'ZAZ BarCode'
QR = 'qrcode'
2019-11-10 15:25:24 -06:00
_ = app.install_locales(__file__)
2019-09-14 15:19:45 -05:00
2019-09-09 22:42:38 -05:00
class Controllers(object):
def __init__(self, dlg):
self.d = dlg
2019-11-10 15:25:24 -06:00
def listbox_item_changed(self, event):
self.d.text.set_focus()
return
2019-09-09 22:42:38 -05:00
def button_action(self, event):
if not self.d.listbox.value:
self.d.listbox.set_focus()
2019-09-14 15:19:45 -05:00
msg = _('Select barcode type')
2019-09-09 22:42:38 -05:00
app.warning(msg, TITLE)
return
if not self.d.text.value.strip():
self.d.text.set_focus()
2019-09-14 15:19:45 -05:00
msg = _('Data field is mandatory')
2019-09-09 22:42:38 -05:00
app.warning(msg, TITLE)
return
self.d.close(1)
return
class ZAZBarCode(unohelper.Base, XJob, XJobExecutor):
2019-09-09 22:42:38 -05:00
def __init__(self, ctx):
self.ctx = ctx
self._data = ''
self._type = ''
2019-11-10 15:25:24 -06:00
self._ask = False
2019-11-21 21:21:22 -06:00
self._path = ''
2019-09-09 22:42:38 -05:00
def execute(self, args):
data = app.property_to_dict(args)
2019-11-21 21:21:22 -06:00
self._type = data['Type'].lower()
self._data = data['Data']
2019-11-21 21:21:22 -06:00
if 'Path' in data:
if not self._type == QR:
p, _, n, _ = app.get_info_path(data['Path'])
data['Path'] = app.join(p, n)
else:
2019-11-21 21:21:22 -06:00
data['Path'] = ''
result = self._create_code(data['Path'])
if result:
app.error(result)
self._path = ''
return self._path
2019-09-09 22:42:38 -05:00
def trigger(self, args):
self._type = args
if args == 'ask' and not self._get_values():
return
doc = app.get_document()
getattr(self, '_insert_in_{}'.format(doc.type))(doc)
2019-11-21 21:21:22 -06:00
app.kill(self._path)
2019-09-09 22:42:38 -05:00
return
2019-11-21 21:21:22 -06:00
def _create_code(self, path=''):
if not path:
path = app.get_temp_file(True)
2019-09-09 22:42:38 -05:00
if self._type == QR:
factory = svg.SvgImage
2019-11-21 21:21:22 -06:00
img = qrcode.make(self._data, border=2, image_factory=factory)
img.save(path)
2019-09-09 22:42:38 -05:00
else:
try:
2019-11-21 21:21:22 -06:00
path = generate(self._type, self._data, output=path)
2019-09-09 22:42:38 -05:00
except Exception as e:
2019-11-10 15:25:24 -06:00
app.error(e)
2019-09-09 22:42:38 -05:00
return str(e)
2019-11-10 15:25:24 -06:00
2019-11-21 21:21:22 -06:00
if app.is_created(path):
self._path = path
2019-11-10 15:25:24 -06:00
return ''
return _('Not generated')
2019-09-09 22:42:38 -05:00
def _get_values(self):
self._type = ''
dlg = self._create_dialog()
if dlg.open():
self._data = dlg.text.value.strip()
self._type = dlg.listbox.value
2019-11-10 15:25:24 -06:00
self._ask = True
2019-09-09 22:42:38 -05:00
return True
return False
def _insert_in_calc(self, doc):
cell = doc.get_cell()
if not self._data:
self._data = cell.value
if not self._data:
2019-09-14 15:19:45 -05:00
msg = _('Select data')
2019-09-09 22:42:38 -05:00
self._show_error(msg)
return
2019-11-21 21:21:22 -06:00
result = self._create_code()
2019-09-09 22:42:38 -05:00
if result:
self._show_error(result)
return
2019-11-10 15:25:24 -06:00
if not self._ask:
cell = cell.offset(0, 1)
2019-11-21 21:21:22 -06:00
cell.insert_image(self._path)
2019-09-09 22:42:38 -05:00
return
def _insert_in_writer(self, doc):
doc = app.get_document()
if not self._data:
2019-11-21 21:21:22 -06:00
sel = app.get_selection()
2019-09-09 22:42:38 -05:00
self._data = sel.string
if not self._data:
2019-09-14 15:19:45 -05:00
msg = _('Select data')
2019-09-09 22:42:38 -05:00
self._show_error(msg)
return
2019-11-21 21:21:22 -06:00
result = self._create_code()
2019-09-09 22:42:38 -05:00
if result:
self._show_error(result)
return
2019-11-21 21:21:22 -06:00
doc.insert_image(self._path)
2019-09-09 22:42:38 -05:00
return
def _insert_in_draw(self, doc):
2019-11-21 21:21:22 -06:00
result = self._create_code()
2019-09-09 22:42:38 -05:00
if result:
self._show_error(result)
return
2019-11-21 21:21:22 -06:00
doc.insert_image(self._path)
2019-09-09 22:42:38 -05:00
return
def _insert_in_impress(self, doc):
self._insert_in_draw(doc)
return
def _show_error(self, error):
2019-09-14 15:19:45 -05:00
msg = _('Error in: {}\n\n{}').format(self._type, error)
2019-09-09 22:42:38 -05:00
app.error(error)
app.errorbox(msg, TITLE)
return
def _create_dialog(self):
args = {
'Name': 'dialog',
'Title': TITLE,
'Width': 160,
'Height': 160,
}
dlg = app.create_dialog(args)
dlg.events = Controllers(dlg)
2019-11-21 21:21:22 -06:00
dlg.id_extension = ID_EXTENSION
2019-09-09 22:42:38 -05:00
args = {
'Type': 'Label',
'Name': 'lbl_barcode',
2019-09-14 15:19:45 -05:00
'Label': _('~Select barcode type'),
'Width': 70,
2019-09-09 22:42:38 -05:00
'Height': 15,
'X': 10,
'Y': 10,
'VerticalAlign': 1,
}
dlg.add_control(args)
args = {
'Type': 'Label',
'Name': 'lbl_data',
2019-09-14 15:19:45 -05:00
'Label': _('~Capture data for barcode'),
'Width': 100,
2019-09-09 22:42:38 -05:00
'Height': 15,
'VerticalAlign': 1,
}
dlg.add_control(args)
args = {
'Type': 'ListBox',
'Name': 'listbox',
2019-09-14 15:19:45 -05:00
'Width': 65,
2019-09-09 22:42:38 -05:00
'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',
2019-09-14 15:19:45 -05:00
'Label': _('~Insert Barcode'),
2019-09-27 14:49:44 -05:00
'Width': 70,
2019-09-09 22:42:38 -05:00
'Height': 15,
2019-11-21 21:21:22 -06:00
'ImageURL': 'qr.png',
2019-09-27 14:49:44 -05:00
'ImagePosition': 1,
2019-09-09 22:42:38 -05:00
}
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
g_ImplementationHelper = unohelper.ImplementationHelper()
g_ImplementationHelper.addImplementation(ZAZBarCode, ID_EXTENSION, SERVICE)