3 # -*- coding: utf-8 -*-
5 # Project: J-Ben, Python front-end
6 # File: widget_hwpad.py
8 # Created on: 25 Nov 2008
12 from subprocess
import Popen
, PIPE
15 def __init__(self
, init_x
, init_y
):
19 """kpengine expects a very simple format."""
20 return "%s %s" % (self
.x
, self
.y
)
22 class WidgetHWPad(gtk
.DrawingArea
):
24 gtk
.DrawingArea
.__init
__(self
)
27 gtk
.gdk
.EXPOSURE_MASK |
28 gtk
.gdk
.BUTTON1_MOTION_MASK |
29 gtk
.gdk
.BUTTON_PRESS_MASK |
30 gtk
.gdk
.BUTTON_RELEASE_MASK |
31 gtk
.gdk
.POINTER_MOTION_HINT_MASK
33 self
.connect("expose-event", self
.on_expose
)
34 self
.connect("motion-notify-event", self
.on_motion
)
35 self
.connect("button-press-event", self
.on_press
)
36 self
.connect("button-release-event", self
.on_release
)
38 self
.current_line
= None
42 def on_expose(self
, widget
, event
):
43 cairo_context
= self
.window
.cairo_create()
46 cairo_context
.rectangle(
47 event
.area
.x
, event
.area
.y
, event
.area
.width
, event
.area
.height
)
51 cairo_context
.set_line_width(5)
52 cairo_context
.set_line_join(cairo
.LINE_JOIN_ROUND
)
53 cairo_context
.set_line_cap(cairo
.LINE_CAP_ROUND
)
55 # Fill background to white
56 cairo_context
.set_source_rgb(1, 1, 1)
59 # Create path from stored data
60 # Also, if a line is being drawn, append the partial line temporarily.
61 if self
.current_line
: self
.lines
.append(self
.current_line
)
62 for line
in self
.lines
:
63 if len(line
) < 2: continue
68 cairo_context
.move_to(point
.x
, point
.y
)
71 cairo_context
.line_to(point
.x
, point
.y
)
72 if self
.current_line
: self
.lines
.pop()
75 cairo_context
.set_source_rgb(0, 0, 0)
76 cairo_context
.stroke()
79 def on_motion(self
, widget
, event
):
80 if event
.state | gtk
.gdk
.BUTTON1_MASK
:
82 self
.current_line
.append(Point(int(event
.x
), int(event
.y
)))
83 self
.update_drawing_area()
85 # This was marked as non-Win32 in the C++ version. Unsure whether
86 # it's okay as it is here.
87 gtk
.gdk
.event_request_motions(event
)
91 def on_press(self
, widget
, event
):
92 # On left mouse click, create a new line
94 self
.current_line
= []
95 self
.current_line
.append(Point(int(event
.x
), int(event
.y
)))
99 def on_release(self
, widget
, event
):
100 """Returns False when the control's state has been changed by a button release, True otherwise."""
102 if event
.button
== 1:
103 if self
.current_line
:
104 self
.lines
.append(self
.current_line
)
105 self
.current_line
= None
106 self
.update_drawing_area()
109 # If no line was written, return true (no change in state)
112 elif event
.button
== 3:
113 if self
.current_line
:
114 self
.current_line
= None
115 self
.update_drawing_area()
116 # No change in search results has occurred, so return true
120 self
.update_drawing_area()
122 # The search -has- changed, return False so we can capture it.
129 def get_results(self
):
130 """Returns an array of 5 UTF8-encoded kanji characters."""
132 print "WidgetHWPad.get_results() \n\t= %s" % (self
.results
,)
137 self
.current_line
= None
140 self
.update_drawing_area()
142 def update_drawing_area(self
):
143 self
.window
.invalidate_rect(None, False)
145 def look_up_chars(self
):
146 print "WidgetHWPad.look_up_chars()"
150 exe_name
= "../deploy_jben/bin/jben_kpengine.exe"
151 data_dir
= "../deploy_jben/kpengine_data"
153 exe_name
= "/home/vultaire/tmp/jben/bin/linux/release/kpengine/jben_kpengine"
154 data_dir
= "/home/vultaire/code/projects/jben/src/kpengine"
158 # x y x y x y x y x y\n (line 1)
159 # x y x y x y x y\n (line 2)
161 pipe_data
= "\n".join(
162 [" ".join([str(point
) for point
in line
])
163 for line
in self
.lines
]) + "\n\n"
165 p
= Popen([exe_name
, "-d", data_dir
], stdin
=PIPE
, stdout
=PIPE
)
166 stdout
, stderr
= p
.communicate(pipe_data
)
168 klist
= stdout
[1:].strip().split()
172 self
.results
.append(unichr(int(kanji
)))