def _draw_gradient(self, dc, rect): """ Draw a vertical gradient background, using the background colour as a starting point """ if rect.height < 1 or rect.width < 1: return dc.SetPen(wx.TRANSPARENT_PEN) # calculate gradient coefficients bck_col = wxcol_to_frgb(self.Parent.GetBackgroundColour()) if self._mouse_hovering: col1 = change_brightness(bck_col, 0.15) col2 = change_brightness(bck_col, 0.10) else: col1 = change_brightness(bck_col, 0.10) col2 = bck_col r1, g1, b1 = col1 r2, g2, b2 = col2 rstep = (r2 - r1) / rect.height gstep = (g2 - g1) / rect.height bstep = (b2 - b1) / rect.height rf, gf, bf = col1 for y in range(rect.y, rect.y + rect.height): cur_col = (rf * 255, gf * 255, bf * 255) dc.SetBrush(wx.Brush(cur_col, wx.BRUSHSTYLE_SOLID)) dc.DrawRectangle(rect.x, rect.y + (y - rect.y), rect.width, rect.height) rf = rf + rstep gf = gf + gstep bf = bf + bstep
def __init__(self, cnvs, sequence_va, colour=gui.SELECTION_COLOUR): """ :param sequence_va: (ListVA) VA to store the sequence in """ super(DichotomyOverlay, self).__init__(cnvs) self.colour = conversion.hex_to_frgba(colour) # Color for quadrant that will expand the sequence self.hover_forw = conversion.hex_to_frgba(colour, 0.5) # Color for quadrant that will cut the sequence self.hover_back = conversion.change_brightness(self.hover_forw, -0.2) self.sequence_va = sequence_va self.sequence_rect = [] # This attribute is used to track the position of the mouse cursor. # The first value denotes the smallest quadrant (in size) in the # sequence and the second one the quadrant index number that will # be added if the mouse is clicked. # This value should be set to (None, None) if the mouse is outside the # canvas or when we are not interested in updating the sequence. self.hover_pos = (None, None) # maximum number of sub-quadrants (6->2**6 smaller than the whole area) self.max_len = 6 self.sequence_va.subscribe(self.on_change, init=True) # Disabling the overlay will allow the event handlers to ignore events self.enabled = False
def test_change_brightness(self): # no change col = (0.2, 0.5, 1.0, 0.8) ncol = conversion.change_brightness(col, 0) self.assertEqual(col, ncol) # brighten col = (0.2, 0.5, 1.0, 0.8) ncol = conversion.change_brightness(col, 0.3) self.assertTrue(all(n >= o for o, n in zip(col, ncol))) # darken col = (0.2, 0.5, 1.0) ncol = conversion.change_brightness(col, -0.6) self.assertTrue(all(n < o for o, n in zip(col, ncol))) # full black col = (0.2, 0.5, 1.0, 1.0) ncol = conversion.change_brightness(col, -1) self.assertTrue(ncol, (0, 0, 0, 1))
def OnPaint(self, event=None): """ This paint event handler draws the actual control """ dc = wx.BufferedPaintDC(self) width, height = self.GetWidth(), self.GetHeight() half_height = height // 2 bgc = self.Parent.GetBackgroundColour() dc.SetBackground(wx.Brush(bgc, wx.SOLID)) dc.Clear() fgc = self.Parent.GetForegroundColour() if not self.Enabled: fgc = change_brightness(fgc, -0.5) dc.SetPen(wx.Pen(fgc, 1)) # Main line dc.DrawLine(self.half_h_width, half_height, width - self.half_h_width, half_height) # ticks steps = [v / 10 for v in range(1, 10)] range_span = self.max_value - self.min_value for s in steps: v = (range_span * s) + self.min_value pix_x = self._val_to_pixel(v) + self.half_h_width dc.DrawLine(pix_x, half_height - 1, pix_x, half_height + 2) if self.Enabled: dc.DrawBitmap(self.bitmap, self.handlePos, half_height - self.half_h_height, True) else: dc.DrawBitmap(self.bitmap_dis, self.handlePos, half_height - self.half_h_height, True) event.Skip()
def draw(self, ctx): ctx.set_line_width(self.line_width) ctx.set_dash([3]) ctx.set_line_join(cairo.LINE_JOIN_MITER) ctx.set_source_rgba(*self.colour) if self.val.value is not None: val = self.val.value if self.map_y_from_x: # Maps Y and also snap X to the closest X value in the data val = self.cnvs.val_x_to_val(val[0]) v_pos = self.cnvs.val_to_pos(val) self.x_label = units.readable_str(val[0], self.cnvs.unit_x, 3) self.y_label = units.readable_str(val[1], self.cnvs.unit_y, 3) # v_posx, v_posy = self.v_pos.value if self.orientation & self.VERTICAL: ctx.move_to(v_pos[0], 0) ctx.line_to(v_pos[0], self.cnvs.ClientSize.y) ctx.stroke() if self.orientation & self.HORIZONTAL: ctx.move_to(0, v_pos[1]) ctx.line_to(self.cnvs.ClientSize.x, v_pos[1]) ctx.stroke() if self.x_label.text: self.x_label.pos = (v_pos[0] + 5, self.cnvs.ClientSize.y) self._write_label(ctx, self.x_label) if self.y_label.text: yp = max(0, v_pos[1] - 5) # Padding from line # Increase bottom margin if x label is close label_padding = 30 if v_pos[0] < 50 else 0 yn = min(self.view_height - label_padding, yp) self.y_label.pos = (2, yn) self._write_label(ctx, self.y_label) r, g, b, a = conversion.change_brightness(self.colour, -0.2) ctx.set_source_rgba(r, g, b, 0.5) ctx.arc(v_pos[0], v_pos[1], 5.5, 0, 2 * math.pi) ctx.fill()
def Draw(self, ctx): ctx.set_line_width(self.line_width) ctx.set_dash([ 3, ]) ctx.set_line_join(cairo.LINE_JOIN_MITER) ctx.set_source_rgba(*self.colour) if (self.v_posx.value is not None and self.orientation & HORIZONTAL == HORIZONTAL): ctx.move_to(self.v_posx.value, 0) ctx.line_to(self.v_posx.value, self.cnvs.ClientSize[1]) ctx.stroke() if (self.v_posy.value is not None and self.orientation & VERTICAL == VERTICAL): ctx.move_to(0, self.v_posy.value) ctx.line_to(self.cnvs.ClientSize[0], self.v_posy.value) ctx.stroke() if None not in (self.v_posy.value, self.v_posx.value): if self.x_label.text: self.x_label.pos = (self.v_posx.value + 5, self.cnvs.ClientSize.y) self._write_label(ctx, self.x_label) if self.y_label.text: yp = max(0, self.v_posy.value - 5) # Padding from line # Increase bottom margin if x label is close label_padding = 30 if self.v_posx.value < 50 else 0 yn = min(self.cnvs.ClientSize.y - label_padding, yp) self.y_label.pos = (2, yn) self._write_label(ctx, self.y_label) r, g, b, a = conversion.change_brightness(self.colour, -0.2) a = 0.5 ctx.set_source_rgba(r, g, b, a) ctx.arc(self.v_posx.value, self.v_posy.value, 5.5, 0, 2 * math.pi) ctx.fill()
def Draw(self, ctx): ctx.set_line_width(self.line_width) ctx.set_dash([3,]) ctx.set_line_join(cairo.LINE_JOIN_MITER) ctx.set_source_rgba(*self.colour) if (self.v_posx.value is not None and self.orientation & HORIZONTAL == HORIZONTAL): ctx.move_to(self.v_posx.value, 0) ctx.line_to(self.v_posx.value, self.cnvs.ClientSize[1]) ctx.stroke() if (self.v_posy.value is not None and self.orientation & VERTICAL == VERTICAL): ctx.move_to(0, self.v_posy.value) ctx.line_to(self.cnvs.ClientSize[0], self.v_posy.value) ctx.stroke() if None not in (self.v_posy.value, self.v_posx.value): if self.x_label.text: self.x_label.pos = (self.v_posx.value + 5, self.cnvs.ClientSize.y) self._write_label(ctx, self.x_label) if self.y_label.text: yp = max(0, self.v_posy.value - 5) # Padding from line # Increase bottom margin if x label is close label_padding = 30 if self.v_posx.value < 50 else 0 yn = min(self.cnvs.ClientSize.y - label_padding, yp) self.y_label.pos = (2, yn) self._write_label(ctx, self.y_label) r, g, b, a = conversion.change_brightness(self.colour, -0.2) a = 0.5 ctx.set_source_rgba(r, g, b, a) ctx.arc(self.v_posx.value, self.v_posy.value, 5.5, 0, 2*math.pi) ctx.fill()
def Draw(self, ctx): ctx.set_line_width(self.line_width) ctx.set_dash([3]) ctx.set_line_join(cairo.LINE_JOIN_MITER) ctx.set_source_rgba(*self.colour) if self.v_pos.value is not None: v_posx, v_posy = self.v_pos.value if self.orientation & self.VERTICAL == self.VERTICAL: ctx.move_to(v_posx, 0) ctx.line_to(v_posx, self.cnvs.ClientSize.y) ctx.stroke() if self.orientation & self.HORIZONTAL == self.HORIZONTAL: ctx.move_to(0, v_posy) ctx.line_to(self.cnvs.ClientSize.x, v_posy) ctx.stroke() if self.x_label.text: self.x_label.pos = (v_posx + 5, self.cnvs.ClientSize.y) self._write_label(ctx, self.x_label) if self.y_label.text: yp = max(0, v_posy - 5) # Padding from line # Increase bottom margin if x label is close label_padding = 30 if v_posx < 50 else 0 yn = min(self.view_height - label_padding, yp) self.y_label.pos = (2, yn) self._write_label(ctx, self.y_label) r, g, b, a = conversion.change_brightness(self.colour, -0.2) a = 0.5 ctx.set_source_rgba(r, g, b, a) ctx.arc(v_posx, v_posy, 5.5, 0, 2*math.pi) ctx.fill()
def Draw(self, ctx): ctx.set_line_width(self.line_width) ctx.set_dash([3]) ctx.set_line_join(cairo.LINE_JOIN_MITER) ctx.set_source_rgba(*self.colour) if self.v_pos.value is not None: v_posx, v_posy = self.v_pos.value if self.orientation & self.VERTICAL == self.VERTICAL: ctx.move_to(v_posx, 0) ctx.line_to(v_posx, self.cnvs.ClientSize.y) ctx.stroke() if self.orientation & self.HORIZONTAL == self.HORIZONTAL: ctx.move_to(0, v_posy) ctx.line_to(self.cnvs.ClientSize.x, v_posy) ctx.stroke() if self.x_label.text: self.x_label.pos = (v_posx + 5, self.cnvs.ClientSize.y) self._write_label(ctx, self.x_label) if self.y_label.text: yp = max(0, v_posy - 5) # Padding from line # Increase bottom margin if x label is close label_padding = 30 if v_posx < 50 else 0 yn = min(self.view_height - label_padding, yp) self.y_label.pos = (2, yn) self._write_label(ctx, self.y_label) r, g, b, a = conversion.change_brightness(self.colour, -0.2) a = 0.5 ctx.set_source_rgba(r, g, b, a) ctx.arc(v_posx, v_posy, 5.5, 0, 2 * math.pi) ctx.fill()