diff options
Diffstat (limited to 'drawwaveform.py')
-rw-r--r-- | drawwaveform.py | 190 |
1 files changed, 110 insertions, 80 deletions
diff --git a/drawwaveform.py b/drawwaveform.py index c42a142..494830c 100644 --- a/drawwaveform.py +++ b/drawwaveform.py @@ -15,7 +15,8 @@ # Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA -import gtk +from gi.repository import Gtk +from gi.repository import Gdk from math import floor, ceil from numpy import array, where, float64, multiply, fft, arange, blackman from ringbuffer import RingBuffer1d @@ -33,7 +34,7 @@ logging.basicConfig() from gettext import gettext as _ -class DrawWaveform(gtk.DrawingArea): +class DrawWaveform(Gtk.DrawingArea): """ Handles all the drawing of waveforms """ __gtype_name__ = "MeasureDrawWaveform" @@ -46,10 +47,10 @@ class DrawWaveform(gtk.DrawingArea): def __init__(self, activity, input_frequency=RATE, channels=1): """ Initialize drawing area and scope parameter """ - gtk.DrawingArea.__init__(self) + Gtk.DrawingArea.__init__(self) - self.add_events(gtk.gdk.BUTTON_PRESS_MASK | \ - gtk.gdk.PROPERTY_CHANGE_MASK) + self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | \ + Gdk.EventMask.PROPERTY_CHANGE_MASK) self.activity = activity self._input_freq = input_frequency @@ -63,7 +64,7 @@ class DrawWaveform(gtk.DrawingArea): self.bias = [] # vertical position fine-tuning from slider self.active = False - self._redraw_atom = gtk.gdk.atom_intern('MeasureRedraw') + self._redraw_atom = Gdk.atom_intern('MeasureRedraw', False) self.buffers = array([]) self.main_buffers = array([]) @@ -105,7 +106,6 @@ class DrawWaveform(gtk.DrawingArea): self.scaleY = "" self._back_surf = None - self.expose_event_id = self.connect('expose_event', self._expose) self.pr_time = 0 self.MAX_GRAPHS = MAX_GRAPHS # Maximum simultaneous graphs @@ -142,7 +142,7 @@ class DrawWaveform(gtk.DrawingArea): self.Ystart.append(50) self.Xend.append(1000) self.Yend.append(500) - self.type .append(0) + self.type.append(0) self.color.append('#FF0000') self.source.append(0) self.visibility.append(True) @@ -194,16 +194,12 @@ class DrawWaveform(gtk.DrawingArea): def set_context_on(self): """ Return to an active state (context on) """ - if not self.context: - self.handler_unblock(self.expose_event_id) self.context = True self._indirect_queue_draw() return def set_context_off(self): """ Return to an inactive state (context off) """ - if self.context: - self.handler_block(self.expose_event_id) self.context = False self._indirect_queue_draw() return @@ -224,17 +220,18 @@ class DrawWaveform(gtk.DrawingArea): def do_size_allocate(self, allocation): """ Allocate a drawing area for the plot """ - gtk.DrawingArea.do_size_allocate(self, allocation) + Gtk.DrawingArea.do_size_allocate(self, allocation) self._update_mode() - if self.window is not None: + if self.get_window() is not None: self._create_background_pixmap() return def _indirect_queue_draw(self): - if self.window is None: + if self.get_window() is None: return - self.window.property_change(self._redraw_atom, self._redraw_atom, - 32, gtk.gdk.PROP_MODE_REPLACE, []) + # FIXME: ¿Como se hace? + #self.get_window().property_change(self._redraw_atom, self._redraw_atom, + # 32, Gdk.PropMode.REPLACE, []) return def do_property_notify_event(self, event): @@ -245,7 +242,8 @@ class DrawWaveform(gtk.DrawingArea): def do_realize(self): """ Called when we are creating all of our window resources """ - + # FIXME: No tengo idea de que hacer aca! + """ gtk.DrawingArea.do_realize(self) # Force a native X window to exist @@ -302,49 +300,47 @@ class DrawWaveform(gtk.DrawingArea): self._HARMONIC_LINE_THICKNESS, gtk.gdk.LINE_SOLID, gtk.gdk.CAP_ROUND, gtk.gdk.JOIN_BEVEL) self._harmonic_gc.set_foreground(clr) - + """ self._create_background_pixmap() return def _create_background_pixmap(self): """ Draw the gridlines for the plot """ - back_surf = gtk.gdk.Pixmap(self.window, self._tick_size, - self._tick_size) - cr = back_surf.cairo_create() - cr.set_source_rgb(0, 0, 0) - cr.paint() - - cr.set_line_width(self._BACKGROUND_LINE_THICKNESS) - cr.set_source_rgb(0.2, 0.2, 0.2) - - x = 0 - y = 0 - - for j in range(0, 2): - cr.move_to(x, y) - cr.rel_line_to(0, self._tick_size) - x = x + self._tick_size - + cr = self.get_window().cairo_create() + Gdk.cairo_set_source_color(cr, Gdk.color_parse('gray')) + allocation = self.get_allocation() x = 0 - y = (self.allocation.height % self._tick_size) / 2 - self._tick_size - - for j in range(0, 3): - cr.move_to(x, y) - cr.rel_line_to(self._tick_size, 0) - y = y + self._tick_size - cr.set_line_width(self._BACKGROUND_LINE_THICKNESS) - cr.stroke() + for j in range(0, allocation.height / (self._FOREGROUND_LINE_THICKNESS * 2)): + line = Gdk.Rectangle() + line.x = x + line.y = 0 + line.width = 1 + line.height = allocation.height + x = x + (self._FOREGROUND_LINE_THICKNESS * 5) + Gdk.cairo_rectangle(cr, line) + cr.fill() + + + y = (allocation.height % self._tick_size) / 2 - self._tick_size + + for j in range(0, allocation.width / (self._FOREGROUND_LINE_THICKNESS * 2)): + line = Gdk.Rectangle() + line.x = 0 + line.y = y + line.width = allocation.width + line.height = 1 + Gdk.cairo_rectangle(cr, line) + cr.fill() + y = y + (self._FOREGROUND_LINE_THICKNESS * 5) - del cr - self.window.set_back_pixmap(back_surf, False) return def do_button_press_event(self, event): """ Set the trigger postion on a button-press event """ - self.trigger_xpos = event.x / float(self.allocation.width) - self.trigger_ypos = event.y / float(self.allocation.height) + self.trigger_xpos = event.x / float(self.get_allocation().width) + self.trigger_ypos = event.y / float(self.get_allocation().height) return True def _calculate_trigger_position(self, samples, y_mag, buf): @@ -359,7 +355,7 @@ class DrawWaveform(gtk.DrawingArea): else: ypos *= -32767.0 / y_mag - x_offset = self.allocation.width * xpos - \ + x_offset = self.get_allocation().width * xpos - \ (samples - samples_to_end) * self.draw_interval position = -1 @@ -388,37 +384,61 @@ class DrawWaveform(gtk.DrawingArea): self.draw_interval + 0.5) return position, samples_to_end - def _expose(self, widget, event): + def do_draw(self, context): """The 'expose' event handler does all the drawing""" - width, height = self.window.get_size() + self._create_background_pixmap() + allocation = self.get_allocation() + width, height = allocation.width, allocation.height + window = self.get_window() + Gdk.cairo_set_source_color(context, Gdk.color_parse('red')) # Real time drawing if self.context and self.active: - # Draw tuning lines # If we are tuning, we want to scale by 10 scale = 10. * self.freq_div / 500. if self.fft_show and self.instrument in INSTRUMENT_DICT: for n, note in enumerate(INSTRUMENT_DICT[self.instrument]): x = int(note / scale) - self.window.draw_line( - self._instrument_gc[n], x, 0, x, height) + line = Gdk.Rectangle() + line.x = x + line.y = 0 + line.width = x + line.height = height + Gdk.cairo_rectangle(context, line) + context.fill() for n, note in enumerate(INSTRUMENT_DICT[self.instrument]): if self.harmonics: x = int(note / scale) for i in range(3): j = i + 2 - self.window.draw_line(self._instrument_h_gc[n], - x * j, 20 * j, x * j, height) + line = Gdk.Rectangle() + line.x = x * j + line.y = 20 * j + line.width = x * j + line.height = height + Gdk.cairo_rectangle(context, line) + context.fill() if self.fft_show and self.tuning_line > 0.0: x = int(self.tuning_line / scale) - self.window.draw_line(self._tuning_line_gc, x, 0, x, height) + line = Gdk.Rectangle() + line.x = x + line.y = 0 + line.width = x + line.height = height + Gdk.cairo_rectangle(context, line) + context.fill() if self.harmonics: for i in range(3): j = i + 2 - self.window.draw_line(self._harmonic_gc, x * j, - 20 * j, x * j, height) + line = Gdk.Rectangle() + line.x = x * j + line.y = 20 * j + line.width = x * j + line.height = height + Gdk.cairo_rectangle(context, line) + context.fill() #Iterate for each graph for graph_id in self.graph_id: @@ -426,7 +446,7 @@ class DrawWaveform(gtk.DrawingArea): continue if self.graph_show_state[graph_id]: buf = self.ringbuffer[graph_id].read(None, self.input_step) - samples = ceil(self.allocation.width / self.draw_interval) + samples = ceil(width / self.draw_interval) if len(buf) == 0: # We don't have enough data to plot. self._indirect_queue_draw() @@ -468,15 +488,15 @@ class DrawWaveform(gtk.DrawingArea): if factor == 0: factor = 0.01 if self.invert[graph_id]: - data *= self.allocation.height / factor + data *= height / factor else: - data *= -self.allocation.height / factor + data *= -height / factor data -= self.bias[graph_id] if self.fft_show: - data += self.allocation.height - 3 + data += height - 3 else: - data += (self.allocation.height / 2.0) + data += (height / 2.0) # The actual drawing of the graph lines = (arange(len(data), dtype='float32')\ @@ -500,20 +520,30 @@ class DrawWaveform(gtk.DrawingArea): freq_note(x, flatsharp=True)) else: if self.triggering != self.TRIGGER_NONE: - x = int(self.trigger_xpos * self.allocation.width) - y = int(self.trigger_ypos * self.allocation.height) + x = int(self.trigger_xpos * width) + y = int(self.trigger_ypos * height) length = int(self._TRIGGER_LINE_THICKNESS * 3.5) - self.window.draw_line(self._trigger_line_gc, - x - length, y, - x + length, y) - self.window.draw_line(self._trigger_line_gc, - x, y - length, - x, y - length) + line = Gdk.Rectangle() + line.x = x - length + line.y = y + line.width = x + length + line.height = y + Gdk.cairo_rectangle(context, line) + context.fill() + line = Gdk.Rectangle() + line.x = x + line.y = y - length + line.width = x + line.height = y - length + Gdk.cairo_rectangle(context, line) + context.fill() if self.type[graph_id] == 0: - self.window.draw_lines(self._line_gc[graph_id], lines) + print graph_id + window.draw_lines(self._line_gc[graph_id], lines) else: - self.window.draw_points(self._line_gc[graph_id], lines) + window.draw_points(self._line_gc[graph_id], lines) + print graph_id self._indirect_queue_draw() return True @@ -538,7 +568,7 @@ class DrawWaveform(gtk.DrawingArea): self.triggering = trigger def get_ticks(self): - return self.allocation.width / float(self._tick_size) + return self.get_allocation().width / float(self._tick_size) def get_fft_mode(self): """Returns if FFT is ON (True) or OFF (False)""" @@ -554,7 +584,7 @@ class DrawWaveform(gtk.DrawingArea): self._freq_range = freq_range def _update_mode(self): - if self.allocation.width <= 0: + if self.get_allocation().width <= 0: return if self.fft_show: @@ -565,12 +595,12 @@ class DrawWaveform(gtk.DrawingArea): self.draw_interval = 5.0 self.set_max_samples( - ceil(self.allocation.width / \ + ceil(self.get_allocation().width / \ float(self.draw_interval) * 2) * self.input_step) # Create the (blackman) window self.fft_window = blackman( - ceil(self.allocation.width / float(self.draw_interval) * 2)) + ceil(self.get_allocation().width / float(self.draw_interval) * 2)) self.draw_interval *= wanted_step / self.input_step else: @@ -582,8 +612,8 @@ class DrawWaveform(gtk.DrawingArea): self.set_max_samples(samples * self.max_samples_fact) self.input_step = max(ceil(samples\ - / (self.allocation.width / 3.0)), 1) - self.draw_interval = self.allocation.width\ + / (self.get_allocation().width / 3.0)), 1) + self.draw_interval = self.get_allocation().width\ / (float(samples) / self.input_step) self.fft_window = None |