Basic JMdict searches ("starts_with" index) should now work.
[jben2_gui.git] / widget_hwpad.py
blobc06fc4d2b2cc43463ea2a0f6c66abb6584e14ea9
2 #!/usr/bin/env python
3 # -*- coding: utf-8 -*-
5 # Project: J-Ben, Python front-end
6 # File: widget_hwpad.py
7 # Author: Paul Goins
8 # Created on: 25 Nov 2008
10 import gtk, cairo
11 import os
12 from subprocess import Popen, PIPE
14 class Point(object):
15 def __init__(self, init_x, init_y):
16 self.x = init_x
17 self.y = init_y
18 def __str__(self):
19 """kpengine expects a very simple format."""
20 return "%s %s" % (self.x, self.y)
22 class WidgetHWPad(gtk.DrawingArea):
23 def __init__(self):
24 gtk.DrawingArea.__init__(self)
26 self.set_events(
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
39 self.lines = []
40 self.results = []
42 def on_expose(self, widget, event):
43 cairo_context = self.window.cairo_create()
45 # Clip update region
46 cairo_context.rectangle(
47 event.area.x, event.area.y, event.area.width, event.area.height)
48 cairo_context.clip()
50 # Set drawing style
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)
57 cairo_context.paint()
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
64 line_started = False
66 for point in line:
67 if not line_started:
68 cairo_context.move_to(point.x, point.y)
69 line_started = True
70 else:
71 cairo_context.line_to(point.x, point.y)
72 if self.current_line: self.lines.pop()
74 # Draw lines
75 cairo_context.set_source_rgb(0, 0, 0)
76 cairo_context.stroke()
77 return True
79 def on_motion(self, widget, event):
80 if event.state | gtk.gdk.BUTTON1_MASK:
81 if self.current_line:
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)
89 return True
91 def on_press(self, widget, event):
92 # On left mouse click, create a new line
93 if event.button == 1:
94 self.current_line = []
95 self.current_line.append(Point(int(event.x), int(event.y)))
97 return True
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()
107 self.look_up_chars()
108 return False
109 # If no line was written, return true (no change in state)
110 return True
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
117 return True
118 elif (self.lines):
119 self.lines.pop()
120 self.update_drawing_area()
121 self.look_up_chars()
122 # The search -has- changed, return False so we can capture it.
123 return False
124 else:
125 return True
127 return True
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,)
134 return self.results
136 def clear(self):
137 self.current_line = None
138 self.lines = []
139 self.results = []
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()"
147 #return
149 if os.name == "nt":
150 exe_name = "../deploy_jben/bin/jben_kpengine.exe"
151 data_dir = "../deploy_jben/kpengine_data"
152 else:
153 exe_name = "/home/vultaire/tmp/jben/bin/linux/release/kpengine/jben_kpengine"
154 data_dir = "/home/vultaire/code/projects/jben/src/kpengine"
157 # Line format:
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)
160 # \n (end of kanji)
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()
169 self.results = []
170 if klist:
171 for kanji in klist:
172 self.results.append(unichr(int(kanji)))