summaryrefslogtreecommitdiff
path: root/ptgui.py
blob: d5356d7aa75f8a768cef5cadb5db5bc8c386d791 (plain)
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
#!/usr/bin/env python
# vim: set expandtab ts=4:

from gi.repository import GLib, Gtk
import cairo, pango, pangocairo
ml = GLib.MainLoop()
import pttarget, ptlayout
from ptlayout import PTLText
import time
import usb.core

class MainWindow(object):
    def __init__(self):
        builder = Gtk.Builder()
        builder.add_from_file('ptgui.glade')
        builder.connect_signals(self)

        self.tape = None
        self.tgtwidth = 192

        self.wnd = builder.get_object('mainwnd')
        self.wnd.connect("delete-event", Gtk.main_quit)
        for name in ['statuslabel', 'tabviewport', 'img', 'printbtn',
                'color_fg', 'color_bg', 'numcopies', 'cuttoggle', 'devbox']:
            self.__dict__[name] = builder.get_object(name)
        self.ctlgrid = Gtk.Table()
        self.tabviewport.add(self.ctlgrid)

        self.layout = ptlayout.PTLHSeq()
        self.layout.spacing = 0
        # vs1 = ptlayout.PTLVStack()
        # vs1.add(PTLText())
        # vs1.add(PTLText())
        # vs2 = ptlayout.PTLVStack()
        # vs2.add(PTLText())
        # vs2.add(PTLText())
        t = PTLText()
        t.font = 'FrutigerNextLT Medium 20'
        t.mode = 'left'
        self.layout.add(t)
        t = PTLText()
        t.font = 'FrutigerNextLT Heavy 60'
        t.mode = 'left'
        self.layout.add(t)

        self.layout.add(PTLText())

        t = PTLText()
        t.font = 'FrutigerNextLT Heavy 60'
        t.mode = 'right'
        self.layout.add(t)
        t = PTLText()
        t.font = 'FrutigerNextLT Medium 20'
        t.mode = 'right'
        self.layout.add(t)
        self.layout.add(PTLText())
        self.layout_put_controls()

        self.sizing_apply()
        self.img.connect('draw', self.draw) 
        # help(self.img)

        self.devlist = Gtk.ListStore(str, object)
        self.devbox.set_model(self.devlist)
        self.scan()
        self.dev_select()
        self.display_refresh()
        self.wnd.show_all()
        
        GLib.timeout_add(100, self.status_update)

    def scan(self):
        pttarget.PTUSB.scan()
        for dev in pttarget.PTUSB.printers:
            print str(dev)
            self.devlist.append([str(dev), dev])

        if len(self.devlist) == 0:
            for dummy_width in [9,12,24]:
                dummy_printer = pttarget.PTPrinter(dummy_width)
                self.devlist.append([str(dummy_printer), dummy_printer])

        itr = self.devlist.get_iter((0, ))
        self.devbox.set_active_iter(itr)

    def dev_select(self):
        itr = self.devbox.get_active_iter()
        if itr is None:
            return
        dev = self.devlist[itr][1]
        dev.setup()
        self.status_update()
        
    def status_update(self):
        itr = self.devbox.get_active_iter()
        if itr is None:
            return
        dev = self.devlist[itr][1]

        try:
            if dev.last_status is None or time.time() - dev.last_status >= 1:
                dev.refresh_status()
            else:
                dev.check_status()
        except usb.core.USBError, e:
            print 'problem while status refresh:', str(e)

        if dev.err1 == 0 and dev.err2 == 0:
            self.printbtn.set_sensitive(True)
            if dev.find_tape() != self.tape:
                self.tape = dev.find_tape()
                self.sizing_apply()
                self.layout_update()
        else:
            self.printbtn.set_sensitive(False)

        mtype = pttarget.media_types.get(dev.media_type, 'unknown (%02x)' % (dev.media_type))
        msize =  '%dx%dmm' % (dev.media_width, dev.media_length) if dev.media_length != 0 else \
                 '%dmm continuous tape' % (dev.media_width)
        self.statuslabel.set_text('Status: %s, %s %s' % (dev.get_errstr(), mtype, msize))
        return True

    def sizing_apply(self):
        if self.tape is None:
            self.label_surface = None
            tapeh = 16
        else:
            self.label_surface = cairo.ImageSurface(cairo.FORMAT_A1, self.tgtwidth, self.tape.pixels)
            tapeh = self.tape.pixels

        self.display_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
            self.tgtwidth + 40, tapeh + 40)
        self.img.set_size_request(self.display_surface.get_width(),
            self.display_surface.get_height())

        #ctx = cairo.Context(self.label_surface)
        # for i in range(0, 128 / 16):
        #     x = 16 * (i & 1)
        #     ctx.rectangle(x, i * 16, 12, 12)
        #     ctx.fill()
        # for i in range(0, 128 / 16):
        #     x = 32 + 16 * (i & 1)
        #     ctx.rectangle(x, i * 16, 12, 12)
        #     ctx.fill()

        # pctx = pangocairo.CairoContext(ctx)
        # font = pango.FontDescription('Delicious 24')
        # layout = pctx.create_layout()
        # layout.set_font_description(font)
        # layout.set_text(u'Hello')
        # pctx.update_layout(layout)
        # pctx.show_layout(layout)

        # self.display_refresh()

    def display_refresh(self, *args):
        ctx = cairo.Context(self.display_surface)
        w = self.display_surface.get_width()
        h = self.display_surface.get_height()
        for x in range(0, w, 16):
            for y in range(0, h, 16):
                col = 0.3 + 0.1 * (((x + y) / 16) & 1)
                ctx.rectangle(x, y, 16, 16)
                ctx.set_source_rgb(col, col, col)
                ctx.fill()

        if self.label_surface is None:
            return

        pw = self.label_surface.get_width()
        ph = self.label_surface.get_height()
        px, py = (w - pw) / 2, (h - ph) / 2
        ctx.rectangle(px, py, pw, ph)
        bg = self.color_bg.get_rgba()
        ctx.set_source_rgb(bg.red, bg.green, bg.blue)
        ctx.fill()

        fg = self.color_fg.get_rgba()
        ctx.set_source_rgb(fg.red, fg.green, fg.blue)
        ctx.mask_surface(self.label_surface, px, py)

        self.img.queue_draw()
                
    def draw(self, widget, ctx):
        ctx.set_source_surface(self.display_surface)
        w = self.display_surface.get_width()
        h = self.display_surface.get_height()
        ctx.rectangle(0, 0, w, h)
        ctx.fill()

    def layout_update(self):
        if self.label_surface is None:
            return

        w, h = self.layout.prep_size(self.label_surface.get_height())
        if w < 32:
            w = 32
        if w != self.tgtwidth:
            self.tgtwidth = w
            self.sizing_apply()
            
        if self.label_surface is None:
            return

        self.label_surface = cairo.ImageSurface(cairo.FORMAT_A1, self.tgtwidth, self.tape.pixels)
        ctx = cairo.Context(self.label_surface)
        ctx.set_operator(cairo.OPERATOR_SOURCE)
        finw = self.layout.render(ctx, self.label_surface.get_height())
        self.display_refresh()

    def layout_control(self, node, name, spec):
        elem = Gtk.Entry()
        buf = elem.get_buffer()
        def update(buf, *args):
            node.__dict__[name] = buf.get_text()
            self.layout_update()

        buf.set_text(node.__dict__[name], -1)
        buf.connect('deleted-text', update)
        buf.connect('inserted-text', update)
        return elem

    def layout_put_controls(self):
        nodes = [self.layout]
        rows = -1
        while len(nodes) > 0:
            rows += 1 # separator
            node = nodes.pop(0)
            nodes.extend(node.children())
            for p in node.properties():
                rows += 1

        self.ctlgrid.resize(rows, 2)

        nodes = [self.layout]
        row = 0
        first = True
        while len(nodes) > 0:
            if not first:
                hs = Gtk.HSeparator()
                self.ctlgrid.attach(hs, 0, 2, row, row + 1, ypadding = 3.0,
                    xoptions = Gtk.AttachOptions.FILL | Gtk.AttachOptions.EXPAND, yoptions = 0)
                row += 1
            first = False

            node = nodes.pop(0)
            nodes.extend(node.children())
            for p in node.properties():
                elem = self.layout_control(node, p[0], p[1])
                lbl = Gtk.Label(p[0])
                lbl.set_alignment(0.0, 0.5)
                self.ctlgrid.attach(lbl, 0, 1, row, row + 1,
                    xoptions = Gtk.AttachOptions.FILL, yoptions = 0)
                self.ctlgrid.attach(elem, 1, 2, row, row + 1,
                    xoptions = Gtk.AttachOptions.FILL | Gtk.AttachOptions.EXPAND, yoptions = 0)
                row += 1

    def on_print(self, *args):
        if self.label_surface is None:
            return


        copies = self.numcopies.get_buffer().get_text()
        if copies.strip() == '':
            copies = 1
        else:
            copies = int(copies)
        feedlast = self.cuttoggle.get_active()

        data = pttarget.PTLabelData()
        data.addcairo(self.label_surface, offset = self.tape.offset)
        itr = self.devbox.get_active_iter()
        dev = self.devlist[itr][1]
        for i in range(0, copies):
            dev.send(data.get(feedlast and i == copies - 1))

mw = MainWindow()
Gtk.main()
# ml.run()