def _on_paint(self, event):
        """ Paint the background using the associated ImageSlice object.
        """
        control = self.control
        dc = BufferDC(control)

        # Draw the slider bar:
        wdx, wdy = control.GetClientSize()
        dx = max(0, min(wdx - 2,
                        int(round(((wdx - 3) * (self.value - self.low)) /
                                  (self.high - self.low)))))

        factory = self.factory
        dc.SetBrush(wx.Brush(factory.slider_color_))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangle(0, 0, dx + 3, wdy)

        # Draw the rest of the background:
        dc.SetBrush(wx.Brush(factory.bg_color_))
        dc.DrawRectangle(dx + 3, 0, wdx - dx - 3, wdy)

        # Draw the slider tip:
        dc.SetBrush(wx.Brush(factory.tip_color_))
        dc.DrawRectangle(dx, 0, 3, wdy)

        # Draw the current text value (if requested):
        if factory.show_value:
            dc.SetBackgroundMode(wx.TRANSPARENT)
            dc.SetTextForeground(factory.text_color_)
            dc.SetFont(control.GetFont())
            tx, ty, tdx, tdy = self._get_text_bounds()
            dc.DrawText(self.text, tx, ty)

        # Copy the buffer to the display:
        dc.copy()
Esempio n. 2
0
    def _on_paint(self, event):
        """ Paint the background using the associated ImageSlice object.
        """
        control = self.control
        dc = BufferDC(control)

        # Draw the background:
        factory = self.factory
        color = factory.color_
        if self._x is not None:
            if factory.active_color_ is not None:
                color = factory.active_color_
        elif self._hover:
            if factory.hover_color_ is not None:
                color = factory.hover_color_

        if color is None:
            paint_parent(dc, control)
            brush = wx.TRANSPARENT_BRUSH
        else:
            brush = wx.Brush(color)

        color = factory.border_color_
        if color is not None:
            pen = wx.Pen(color)
        else:
            pen = wx.TRANSPARENT_PEN

        if (pen != wx.TRANSPARENT_PEN) or (brush != wx.TRANSPARENT_BRUSH):
            wdx, wdy = control.GetClientSizeTuple()
            dc.SetBrush(brush)
            dc.SetPen(pen)
            dc.DrawRectangle(0, 0, wdx, wdy)

        # Draw the current text value:
        dc.SetBackgroundMode(wx.TRANSPARENT)
        dc.SetTextForeground(factory.text_color_)
        dc.SetFont(control.GetFont())
        tx, ty, tdx, tdy = self._get_text_bounds()
        dc.DrawText(self.text, tx, ty)

        # Copy the buffer contents to the display:
        dc.copy()
Esempio n. 3
0
    def Draw(self, grid, attr, dc, rect, row, col, is_selected):
        """ Draws the contents of the specified grid cell.
        """
        # Get the model object this cell is being rendered for:
        model = grid.grid.model
        object = model.get_filtered_item(row)

        # Get the draw bounds:
        x0 = rect.x
        y0 = rect.y
        dx = rect.width
        dy = rect.height

        # Do all drawing into an off-screen buffer:
        bdc = BufferDC(dc, dx, dy)

        # Draw the appropriate theme background:
        column = self.column
        if is_selected:
            theme = (column.get_selected_theme(object)
                     or column.get_cell_theme(object))
        else:
            theme = column.get_cell_theme(object)

        # If no column theme is specified, try to get the global theme from the
        # model:
        if theme is None:
            if row & 1:
                theme = model.alt_theme or model.cell_theme
            else:
                theme = model.cell_theme

            if is_selected:
                theme = model.selected_theme or theme

        if theme is not None:
            content = theme.content
            slice = theme.image_slice
            slice.fill(bdc, 0, 0, dx, dy)

            # Set up the correct text color to use:
            bdc.SetTextForeground(theme.content_color)

            # Calculate the margins for the draw area:
            left = slice.xleft + content.left
            top = slice.xtop + content.top
            right = slice.xright + content.right
            bottom = slice.xbottom + content.bottom
            ox, oy = theme.label.left, theme.label.top
        else:
            if is_selected:
                bg_color = grid.GetSelectionBackground()
            else:
                bg_color = attr.GetBackgroundColour()

            bdc.SetBackgroundMode(wx.SOLID)
            bdc.SetBrush(wx.Brush(bg_color, wx.SOLID))
            bdc.SetPen(wx.TRANSPARENT_PEN)
            bdc.DrawRectangle(0, 0, dx, dy)

            # Set up the correct text color to use:
            bdc.SetTextForeground(attr.GetTextColour())

            # Calculate the margins for the draw area:
            left = right = self.column.horizontal_margin
            top = bottom = self.column.vertical_margin
            ox = oy = 0

        # Get the alignment information:
        halign, valign = attr.GetAlignment()

        # Draw the bar graph (if any):
        maximum = column.get_maximum(object)
        if (not is_selected) and (maximum > 0.0):
            if theme is None:
                left = right = top = bottom = 0
            try:
                ratio = max(min(column.get_raw_value(object) / maximum, 1.0),
                            -1.0)
                avail_dx = dx - left - right
                bar_dx = int(round(ratio * avail_dx))
                if halign == wx.ALIGN_CENTRE:
                    bar_dx /= 2
                    bar_x = left + (avail_dx / 2) + min(0, bar_dx)
                else:
                    bar_dx = abs(bar_dx)
                    if halign == wx.ALIGN_LEFT:
                        bar_x = left
                        left += self.column.horizontal_margin
                    else:
                        bar_x = avail_dx - bar_dx
                        right += self.column.horizontal_margin

                if bar_dx > 0:
                    bdc.SetBackgroundMode(wx.SOLID)
                    bdc.SetBrush(
                        wx.Brush(column.get_graph_color(object), wx.SOLID))
                    bdc.SetPen(wx.TRANSPARENT_PEN)
                    bdc.DrawRectangle(bar_x, top, bar_dx, dy - top - bottom)
            except:
                pass

            if theme is None:
                left = right = self.column.horizontal_margin
                top = bottom = self.column.vertical_margin

        # Get the optional image bitmap and text:
        bitmap = convert_bitmap(column.get_image(object))
        text = grid.GetCellValue(row, col)

        # If no text or bitmap to display, then we are done:
        if (bitmap is None) and (text == ''):
            bdc.copy(x0, y0)
            return

        # Get the bitmap size:
        idx = idy = tdx = tdy = 0
        if bitmap is not None:
            idx = bitmap.GetWidth()
            idy = bitmap.GetHeight()

        # Get the text size:
        if text != '':
            bdc.SetFont(attr.GetFont())
            tdx, tdy = bdc.GetTextExtent(text)

            # Get the spacing between text and image:
            if bitmap is not None:
                idx += self.column.horizontal_margin

        # Calculate the x-coordinate of the image/text:
        if halign == wx.ALIGN_LEFT:
            x = left
        elif halign == wx.ALIGN_CENTRE:
            x = (left + ((dx - left - right - tdx - idx) / 2))
        else:
            x = (dx - right - tdx - idx)

        # Calculate the y-coordinate of the image/text:
        max_dy = max(tdy, idy)
        if valign == wx.ALIGN_TOP:
            y = top
        elif valign == wx.ALIGN_CENTRE:
            y = (top + ((dy - top - bottom - max_dy) / 2))
        else:
            y = (dy - bottom - max_dy)

        # Set up the clipping region to prevent drawing outside the margins:
        bdc.SetClippingRegion(left, top, dx - left - right, dy - top - bottom)

        # Draw the image (if left or center aligned):
        if (bitmap is not None) and (halign != wx.ALIGN_RIGHT):
            bdc.DrawBitmap(bitmap, x, y + ((max_dy - idy) / 2), True)
            x += idx

        # Finally, draw the text:
        if text != '':
            bdc.SetBackgroundMode(wx.TRANSPARENT)
            bdc.DrawText(text, x + ox, y + oy)
            x += tdx + self.column.horizontal_margin

        # Draw the image (if right-aligned):
        if (bitmap is not None) and (halign == wx.ALIGN_RIGHT):
            bdc.DrawBitmap(bitmap, x, y + ((max_dy - idy) / 2), True)

        # Discard the clipping region:
        bdc.DestroyClippingRegion()

        # Copy the buffer to the display:
        bdc.copy(x0, y0)