Exemplo n.º 1
0
    def OnPaint(self, event):
        """
		Handles the ``wx.EVT_PAINT`` event for L{RoundButton}.

		:param `event`: a `wx.PaintEvent` event to be processed.
		"""

        dc = wx.BufferedPaintDC(self)

        am = AM.ArtManager()

        gc = wx.GraphicsContext.Create(dc)
        dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour()))
        dc.Clear()

        clientRect = self.GetClientRect()
        boundaryRect = clientRect

        x, y, width, height = clientRect
        colour = self.GetForegroundColour()
        textColour = am.DarkColour(
            wx.WHITE, 3.0) if am.IsDark(colour) else am.LightColour(
                wx.BLACK, 3.0)

        pressed = False
        if wx.Window.GetCapture() != self:
            if self._mouseAction == HOVER:
                colour = am.LightColour(colour, 10.0)
        else:
            colour = am.DarkColour(colour, 10.0)
            textColour = am.DarkColour(textColour, 10.0)
            pressed = True

        r = min(boundaryRect.GetWidth(), boundaryRect.GetHeight()) // 2
        xCenter = x + width // 2
        yCenter = y + height // 2

        gc.SetPen(wx.TRANSPARENT_PEN)

        def drawCircle(x, y, r):
            gc.DrawEllipse(x - r, y - r, r * 2, r * 2)

        # Draw the metal ring
        gc.SetBrush(
            gc.CreateRadialGradientBrush(xCenter, yCenter - r, xCenter,
                                         yCenter - r, r * 2, wx.WHITE,
                                         wx.Colour(33, 33, 33)))
        drawCircle(xCenter, yCenter, r)

        rSmaller = r * 0.80
        gc.SetBrush(
            gc.CreateRadialGradientBrush(xCenter, yCenter + rSmaller, xCenter,
                                         yCenter + rSmaller, rSmaller * 2,
                                         wx.WHITE, wx.Colour(33, 33, 33)))
        drawCircle(xCenter, yCenter, rSmaller)

        # Draw the body of the button.
        rSmaller *= 0.93
        if pressed:
            shrink = 0.025
            yCenter -= r * shrink / 2.0

        dc.SetFont(self.GetFont())
        cRegular = colour
        gc.SetBrush(
            gc.CreateRadialGradientBrush(xCenter, yCenter + rSmaller * 0.9,
                                         xCenter, yCenter + rSmaller,
                                         rSmaller * 2,
                                         am.LightColour(colour,
                                                        75.0), cRegular))
        drawCircle(xCenter, yCenter, rSmaller)
        self._buttonRadius = rSmaller

        # Draw the flare at the top of the button (a shaded ellipse with a linear gradient).
        gc.SetBrush(
            gc.CreateLinearGradientBrush(xCenter - rSmaller,
                                         yCenter - rSmaller,
                                         xCenter - rSmaller, yCenter,
                                         am.LightColour(colour, 40.0),
                                         am.LightColour(colour, 30.0)))

        # Magic constants to get things to look right.
        rWidth = rSmaller * (2.0 * 0.7 * 0.9)
        rHeight = rSmaller * (0.8 * 0.9)
        gc.DrawEllipse(xCenter - rWidth / 2, yCenter - rSmaller, rWidth,
                       rHeight)

        # Draw an outline around the button body.
        # Also covers up the gap between the flare and the top edge of the button.
        gc.SetPen(wx.Pen(wx.Colour(50, 50, 50), r * 0.025))
        gc.SetBrush(wx.TRANSPARENT_BRUSH)
        gc.DrawEllipse(xCenter - rSmaller, yCenter - rSmaller, rSmaller * 2,
                       rSmaller * 2)

        dc.SetTextForeground(textColour)

        label = self.GetLabel().strip()
        if not label:
            return
        lines = label.split('\n')
        textWidth, textHeight = dc.GetTextExtent(label[0])

        yText = yCenter - textHeight * len(lines) / 2.0
        for line in lines:
            dc.DrawText(line, xCenter - dc.GetTextExtent(line)[0] // 2, yText)
            yText += textHeight
Exemplo n.º 2
0
	def OnPaint(self, event):
		"""
		Handles the ``wx.EVT_PAINT`` event for L{CalendarHeatmap}.

		:param `event`: a `wx.PaintEvent` event to be processed.
		"""

		dc = wx.BufferedPaintDC(self)
		
		self._getGridDate()
		self._setDimensions( dc )
		
		am = AM.ArtManager()
		
		gc = wx.GraphicsContext.Create(dc)

		dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour()))
		dc.Clear()
		
		colour = self.GetForegroundColour()
		textColour = am.DarkColour(wx.WHITE, 3.0) if am.IsDark(colour) else am.LightColour(wx.BLACK, 3.0)
		
		valueMax = max( v for v in self.dates.values() )
		
		gc = wx.GraphicsContext.Create(dc)
		
		for r, w in enumerate('MTWTFSS'):
			x, y = self.leftLabelRect.GetX(), self.leftLabelRect.GetY() + self.rowHeight*r
			width = dc.GetTextExtent(w)[0]
			dc.DrawText( w, x + (self.leftLabelRect.GetWidth() - width) // 2, y )
		
		if self.date:
			x, y = self._xyFromDate( self.date )
			dText = '{}: {}'.format( self.date.strftime('%Y-%m-%d'), self.dates.get(self.date, 0) )
			width = dc.GetTextExtent( dText )[0]
			if x+width > self.bodyRect.GetRight():
				x = self.bodyRect.GetRight() - width
			dc.DrawText( dText, x, self.bottomLabelRect.GetY() )
		else:
			dText = '{}'.format( self.year )
			dc.DrawText( dText, self.bottomLabelRect.GetX(), self.bottomLabelRect.GetY() )
		
		backgrounds = [wx.Brush(wx.Colour(200,200,200), wx.SOLID), wx.Brush(wx.Colour(230,230,230), wx.SOLID)]
		monthCur = 0
		gc.SetPen( wx.TRANSPARENT_PEN )
		for d in daterange(datetime.date(self.year, 1, 1), datetime.date(self.year+1, 1, 1)):
			x, y = self._xyFromDate( d )
			gc.SetBrush( backgrounds[d.month&1] if d != self.date else wx.GREEN_BRUSH )			
			gc.DrawRectangle( x, y, self.rowHeight, self.rowHeight )
			
			s = self.dates.get(d, None)
			if s:
				gc.SetBrush( wx.RED_BRUSH )
				size = max(3, self.rowHeight * sqrt((float(s) / valueMax)))
				cc = (self.rowHeight - size) / 2
				gc.DrawEllipse( x + cc, y + cc, size, size )
			
			if d.month != monthCur:
				y = self.topLabelRect.GetY()
				dc.DrawText( d.strftime('%b'), x, y )
				monthCur = d.month
		
		if self.dateSelect:
			x, y = self._xyFromDate( self.dateSelect )
			gc.SetBrush( wx.TRANSPARENT_BRUSH )
			gc.SetPen( wx.Pen(wx.BLACK, 2) )
			gc.DrawRectangle( x-1, y-1, self.rowHeight+1, self.rowHeight+1 )