Web · Wiki · Activities · Blog · Lists · Chat · Meeting · Bugs · Git · Translate · Archive · People · Donate

Commit b7540688a5867e36402b0350b8a70bbe8e52eb5b

Change the key selected with mouse movement
draw_piano.py
(105 / 50)
  
3030 self._add_c = add_c
3131 self._labels = labels
3232 self._pressed_key = None
33 self.font_size = 20
3334 super(PianoKeyboard, self).__init__()
35 # info needed to check keys positions
36 self._white_keys = [0, 2, 4, 5, 7, 9, 11]
37 self._l_keys_areas = [0, 3]
38 self._t_keys_areas = [1, 4, 5]
39 self._j_keys_areas = [2, 6]
3440 self.connect('expose_event', self.expose)
3541 self.connect('button_press_event', self.__button_press_cb)
3642 self.connect('button_release_event', self.__button_release_cb)
43 self.connect('motion_notify_event', self.__motion_notify_cb)
3744 self.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK | \
38 gtk.gdk.BUTTON_RELEASE_MASK)
45 gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.POINTER_MOTION_MASK | \
46 gtk.gdk.POINTER_MOTION_HINT_MASK)
3947
4048 def calculate_sizes(self, width):
4149 self._width = width
5858 def __button_press_cb(self, widget, event):
5959 if event.button == 1:
6060 x, y = event.x, event.y
61 if y > self._height:
61 key_found = self.__get_key_at_position(x, y)
62 if key_found is None:
6263 return True
63 octave_clicked = int(x / self._octave_width)
64 key_area = int((x % self._octave_width) / self._key_width)
65 click_x = int(x % self._key_width)
66 white_keys = [0, 2, 4, 5, 7, 9, 11]
67 l_keys_areas = [0, 3]
68 t_keys_areas = [1, 4, 5]
69 j_keys_areas = [2, 6]
70 if y > self._black_keys_height or \
71 (self._add_c and x > self._width - self._key_width):
72 key_clicked = white_keys[key_area]
7364 else:
74 # check black key at the right
75 key_clicked = -1
76 if key_area in l_keys_areas or \
77 key_area in t_keys_areas:
78 if click_x > self._key_width * 2 / 3:
79 key_clicked = white_keys[key_area] + 1
80 # check black key at the left
81 if key_clicked == -1 and \
82 key_area in j_keys_areas or \
83 key_area in t_keys_areas:
84 if click_x < self._key_width * 1 / 3:
85 key_clicked = white_keys[key_area] - 1
86 if key_clicked == -1:
87 key_clicked = white_keys[key_area]
88 self._pressed_key = (octave_clicked, key_clicked)
89 self.emit('key_pressed', octave_clicked, key_clicked,
90 self._labels[octave_clicked][key_clicked])
91 self.queue_draw()
65 self._pressed_key = key_found
66 octave_clicked = key_found[0]
67 key_clicked = key_found[1]
68 self.emit('key_pressed', octave_clicked, key_clicked,
69 self.get_label(octave_clicked, key_clicked))
70 self.queue_draw()
71 return True
72
73 def __motion_notify_cb(self, widget, event):
74 # only continue if the button is pressed
75 if self._pressed_key is None:
9276 return True
9377
78 # if this is a hint, then let's get all the necessary
79 # information, if not it's all we need.
80 if event.is_hint:
81 x, y, state = event.window.get_pointer()
82 else:
83 x = event.x
84 y = event.y
85 state = event.state
86
87 key_found = self.__get_key_at_position(x, y)
88 if key_found != self._pressed_key:
89 self.emit('key_released', self._pressed_key[0],
90 self._pressed_key[1], self.get_label(*self._pressed_key))
91
92 self._pressed_key = key_found
93 if key_found is not None:
94 octave_clicked = key_found[0]
95 key_clicked = key_found[1]
96 self.emit('key_pressed', octave_clicked, key_clicked,
97 self.get_label(octave_clicked, key_clicked))
98 self.queue_draw()
99
100 def __get_key_at_position(self, x, y):
101 if y > self._height:
102 return None
103 octave_found = int(x / self._octave_width)
104 key_area = int((x % self._octave_width) / self._key_width)
105 click_x = int(x % self._key_width)
106 if y > self._black_keys_height or \
107 (self._add_c and x > self._width - self._key_width):
108 key_found = self._white_keys[key_area]
109 else:
110 # check black key at the right
111 key_found = -1
112 if key_area in self._l_keys_areas or \
113 key_area in self._t_keys_areas:
114 if click_x > self._key_width * 2 / 3:
115 key_found = self._white_keys[key_area] + 1
116 # check black key at the left
117 if key_found == -1 and \
118 key_area in self._j_keys_areas or \
119 key_area in self._t_keys_areas:
120 if click_x < self._key_width * 1 / 3:
121 key_found = self._white_keys[key_area] - 1
122 if key_found == -1:
123 key_found = self._white_keys[key_area]
124 return (octave_found, key_found)
125
94126 def __button_release_cb(self, widget, event):
95 self._pressed_key = None
96 self.emit('key_released', -1, -1, '')
97 self.queue_draw()
127 if self._pressed_key is not None:
128 self.emit('key_released', self._pressed_key[0],
129 self._pressed_key[1], self.get_label(*self._pressed_key))
130 self._pressed_key = None
131 self.queue_draw()
98132
133 def get_label(self, octave, key):
134 if self._labels is None:
135 return ""
136 try:
137 return self._labels[octave][key]
138 except:
139 return ""
140
99141 def expose(self, widget, event):
100142 rect = self.get_allocation()
101143 self.calculate_sizes(rect.width)
152152 # calculate text height
153153 ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
154154 cairo.FONT_WEIGHT_NORMAL)
155 # TODO: configure or calculate
156 ctx.set_font_size(20)
155 ctx.set_font_size(self.font_size)
157156 x_bearing, y_bearing, width, height, x_advance, y_advance = \
158157 ctx.text_extents('M')
159158 self._text_height = height
167167 self.draw_last_C(ctx, n + 1)
168168 if self._pressed_key is not None:
169169 octave, key = self._pressed_key
170 if octave == -1 or (octave == self._octaves and key > 0):
171 return
170172 if key == 0:
171173 if octave < self._octaves:
172174 self.draw_C(ctx, octave, True)
226226 def draw_C(self, ctx, octave_number, highlighted=False):
227227 x = self._key_width * (octave_number * 7)
228228 self.draw_key_L(ctx, x, highlighted)
229 self._draw_label(ctx, x, octave_number, 0, False)
229 self._draw_label(ctx, x, octave_number, 0, False, highlighted)
230230
231231 def draw_CB(self, ctx, octave_number, highlighted=False):
232232 x = self._key_width * (octave_number * 7) + self._key_width * 2 / 3
233233 self.draw_black(ctx, x, highlighted)
234 self._draw_label(ctx, x, octave_number, 1, True)
234 self._draw_label(ctx, x, octave_number, 1, True, highlighted)
235235
236236 def draw_D(self, ctx, octave_number, highlighted=False):
237237 x = self._key_width + self._key_width * (octave_number * 7)
238238 self.draw_key_T(ctx, x, highlighted)
239 self._draw_label(ctx, x, octave_number, 2, False)
239 self._draw_label(ctx, x, octave_number, 2, False, highlighted)
240240
241241 def draw_DB(self, ctx, octave_number, highlighted=False):
242242 x = self._key_width + self._key_width * 2 / 3 + \
243243 self._key_width * (octave_number * 7)
244244 self.draw_black(ctx, x, highlighted)
245 self._draw_label(ctx, x, octave_number, 3, True)
245 self._draw_label(ctx, x, octave_number, 3, True, highlighted)
246246
247247 def draw_E(self, ctx, octave_number, highlighted=False):
248248 x = self._key_width * 2 + self._key_width * (octave_number * 7)
249249 self.draw_key_J(ctx, x, highlighted)
250 self._draw_label(ctx, x, octave_number, 4, False)
250 self._draw_label(ctx, x, octave_number, 4, False, highlighted)
251251
252252 def draw_F(self, ctx, octave_number, highlighted=False):
253253 x = self._key_width * 3 + self._key_width * (octave_number * 7)
254254 self.draw_key_L(ctx, x, highlighted)
255 self._draw_label(ctx, x, octave_number, 5, False)
255 self._draw_label(ctx, x, octave_number, 5, False, highlighted)
256256
257257 def draw_FB(self, ctx, octave_number, highlighted=False):
258258 x = self._key_width * 3 + self._key_width * 2 / 3 + \
259259 self._key_width * (octave_number * 7)
260260 self.draw_black(ctx, x, highlighted)
261 self._draw_label(ctx, x, octave_number, 6, True)
261 self._draw_label(ctx, x, octave_number, 6, True, highlighted)
262262
263263 def draw_G(self, ctx, octave_number, highlighted=False):
264264 x = self._key_width * 4 + self._key_width * (octave_number * 7)
265265 self.draw_key_T(ctx, x, highlighted)
266 self._draw_label(ctx, x, octave_number, 7, False)
266 self._draw_label(ctx, x, octave_number, 7, False, highlighted)
267267
268268 def draw_GB(self, ctx, octave_number, highlighted=False):
269269 x = self._key_width * 4 + self._key_width * 2 / 3 + \
270270 self._key_width * (octave_number * 7)
271271 self.draw_black(ctx, x, highlighted)
272 self._draw_label(ctx, x, octave_number, 8, True)
272 self._draw_label(ctx, x, octave_number, 8, True, highlighted)
273273
274274 def draw_A(self, ctx, octave_number, highlighted=False):
275275 x = self._key_width * 5 + self._key_width * (octave_number * 7)
276276 self.draw_key_T(ctx, x, highlighted)
277 self._draw_label(ctx, x, octave_number, 9, False)
277 self._draw_label(ctx, x, octave_number, 9, False, highlighted)
278278
279279 def draw_AB(self, ctx, octave_number, highlighted=False):
280280 x = self._key_width * 5 + self._key_width * 2 / 3 + \
281281 self._key_width * (octave_number * 7)
282282 self.draw_black(ctx, x, highlighted)
283 self._draw_label(ctx, x, octave_number, 10, True)
283 self._draw_label(ctx, x, octave_number, 10, True, highlighted)
284284
285285 def draw_B(self, ctx, octave_number, highlighted=False):
286286 x = self._key_width * 6 + self._key_width * (octave_number * 7)
287287 self.draw_key_J(ctx, x, highlighted)
288 self._draw_label(ctx, x, octave_number, 11, False)
288 self._draw_label(ctx, x, octave_number, 11, False, highlighted)
289289
290290 def draw_last_C(self, ctx, octave_number, highlighted=False):
291291 x = self._key_width * (octave_number * 7)
292292 self.draw_key_simple(ctx, x, highlighted)
293 self._draw_label(ctx, x, octave_number, 0, False)
293 self._draw_label(ctx, x, octave_number, 0, False, highlighted)
294294
295295 def draw_key_L(self, ctx, x, highlighted):
296296 ctx.save()
380380 ctx.set_source_rgb(*stroke)
381381 ctx.stroke()
382382
383 def _draw_label(self, ctx, x, octave_number, position, black_key):
383 def _draw_label(self, ctx, x, octave_number, position, black_key,
384 highlighted):
384385 #print "Dibujando ",text
385386 if self._labels is not None:
386387 text = self._labels[octave_number][position]
390390 if black_key:
391391 x_text = x + self._key_width * 1 / 3 - (width / 2 + x_bearing)
392392 y_text = self._black_keys_height - (self._text_height * 2)
393 stroke = (1, 1, 1)
393 if highlighted:
394 stroke = (0, 0, 0)
395 else:
396 stroke = (1, 1, 1)
394397 else:
395398 x_text = x + self._key_width / 2 - (width / 2 + x_bearing)
396399 y_text = self._height - (self._text_height * 2)