⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 helpers.py

📁 Wxpython Implemented on Windows CE, Source code
💻 PY
📖 第 1 页 / 共 2 页
字号:

    def Draw(self, dc):
        if self.parent.clockStyle & SHOW_SHADOWS:
            self._draw(dc, True)
        self._draw(dc)


    def RecalcCoords(self, clocksize, centre, scale):
        self.centre = centre
        [hand.RecalcCoords(clocksize, centre, scale) for hand in self.hands]


    def SetMaxRadius(self, radius):
        self.radius = radius


    def GetSize(self, target):
        r = []
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                r.append(hand.GetSize())
        return tuple(r)


    def GetFillColour(self, target):
        r = []
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                r.append(hand.GetFillColour())
        return tuple(r)


    def GetBorderColour(self, target):
        r = []
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                r.append(hand.GetBorderColour())
        return tuple(r)


    def GetBorderWidth(self, target):
        r = []
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                r.append(hand.GetBorderWidth())
        return tuple(r)


    def GetShadowColour(self):
        r = []
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                r.append(hand.GetShadowColour())
        return tuple(r)


    def SetSize(self, size, target):
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                hand.SetSize(size)


    def SetFillColour(self, colour, target):
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                hand.SetFillColour(colour)


    def SetBorderColour(self, colour, target):
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                hand.SetBorderColour(colour)


    def SetBorderWidth(self, width, target):
        for i, hand in enumerate(self.hands):
            if _targets[i] & target:
                hand.SetBorderWidth(width)


    def SetShadowColour(self, colour):
        for i, hand in enumerate(self.hands):
            hand.SetShadowColour(colour)

#----------------------------------------------------------------------

class TickSet:
    """Manages a set of tick marks."""

    def __init__(self, parent, **kwargs):
        self.parent = parent
        self.dyer = Dyer()
        self.noe = {"minutes": 60, "hours": 12}[kwargs["kind"]]
        self.font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)

        style = kwargs.pop("style")
        self.kwargs = kwargs
        self.SetStyle(style)


    def _draw(self, dc, shadow=False):
        dc.SetFont(self.font)
        
        a_tick = self.ticks[0]

        if shadow:
            offset = self.parent.shadowOffset * a_tick.GetScale()
        else:
            offset = 0

        clockStyle = self.parent.clockStyle

        for idx, tick in self.ticks.items():
            draw = False

            # Are we a set of hours?
            if self.noe == 12:
                # Should we show all hours ticks?
                if clockStyle & SHOW_HOURS_TICKS:
                    draw = True
                # Or is this tick a quarter and should we show only quarters?
                elif clockStyle & SHOW_QUARTERS_TICKS and not (idx + 1) % 3.:
                    draw = True
            # Are we a set of minutes and minutes should be shown?
            elif self.noe == 60 and clockStyle & SHOW_MINUTES_TICKS:
                # If this tick occupies the same position of an hour/quarter
                # tick, should we still draw it anyway?
                if clockStyle & OVERLAP_TICKS:
                    draw = True
                # Right, sir. I promise I won't overlap any tick.
                else:
                    # Ensure that this tick won't overlap an hour tick.
                    if clockStyle & SHOW_HOURS_TICKS:
                        if (idx + 1) % 5.:
                            draw = True
                    # Ensure that this tick won't overlap a quarter tick.
                    elif clockStyle & SHOW_QUARTERS_TICKS:
                        if (idx + 1) % 15.:
                            draw = True
                    # We're not drawing quarters nor hours, so we can draw all
                    # minutes ticks.
                    else:
                        draw = True

            if draw:
                tick.Draw(dc, offset)


    def Draw(self, dc):
        if self.parent.clockStyle & SHOW_SHADOWS:
            self.dyer.Select(dc, True)
            self._draw(dc, True)
        self.dyer.Select(dc)
        self._draw(dc)


    def RecalcCoords(self, clocksize, centre, scale):
        a_tick = self.ticks[0]

        size = a_tick.GetMaxSize(scale)
        maxsize = size

        # Try to find a 'good' max size for text-based ticks.
        if a_tick.text is not None:
            self.font.SetPointSize(size)
            dc = wx.MemoryDC()
            dc.SelectObject(wx.EmptyBitmap(*clocksize.Get()))
            dc.SetFont(self.font)
            maxsize = size
            for tick in self.ticks.values():
                maxsize = max(*(dc.GetTextExtent(tick.text) + (maxsize,)))

        radius = self.radius = min(clocksize.Get()) / 2. - \
                               self.dyer.width / 2. - \
                               maxsize / 2. - \
                               a_tick.GetOffset() * scale - \
                               self.parent.shadowOffset * scale

        # If we are a set of hours, the number of elements of this tickset is 
        # 12 and ticks are separated by a distance of 30 degrees;
        # if we are a set of minutes, the number of elements of this tickset is
        # 60 and ticks are separated by a distance of 6 degrees.
        angfac = [6, 30][self.noe == 12]

        for i, tick in self.ticks.items():
            tick.SetClockSize(clocksize)
            tick.SetScale(scale)

            deg = 180 - angfac * (i + 1)
            angle = math.radians(deg)

            x = centre.x + radius * math.sin(angle)
            y = centre.y + radius * math.cos(angle)

            tick.SetPosition(wx.Point(x, y))


    def GetSize(self):
        return self.kwargs["size"]


    def GetFillColour(self):
        return self.dyer.GetFillColour()


    def GetBorderColour(self):
        return self.dyer.GetBorderColour()


    def GetBorderWidth(self):
        return self.dyer.GetBorderWidth()


    def GetPolygon(self):
        a_tick = self.ticks.values()[0]
        return a_tick.GetPolygon()


    def GetFont(self):
        return self.font


    def GetOffset(self):
        a_tick = self.ticks[0]
        return a_tick.GetOffset()


    def GetShadowColour(self):
        return self.dyer.GetShadowColour()


    def GetIsRotated(self):
        a_tick = self.ticks[0]
        return a_tick.GetIsRotated()


    def GetStyle(self):
        return self.style


    def SetSize(self, size):
        self.kwargs["size"] = size
        [tick.SetSize(size) for tick in self.ticks.values()]


    def SetFillColour(self, colour):
        self.dyer.SetFillColour(colour)


    def SetBorderColour(self, colour):
        self.dyer.SetBorderColour(colour)


    def SetBorderWidth(self, width):
        self.dyer.SetBorderWidth(width)


    def SetPolygon(self, polygon):
        [tick.SetPolygon(polygon) for tick in self.ticks.values()]


    def SetFont(self, font):
        self.font = font


    def SetOffset(self, offset):
        self.kwargs["offset"] = offset
        [tick.SetOffset(offset) for tick in self.ticks.values()]


    def SetShadowColour(self, colour):
        self.dyer.SetShadowColour(colour)


    def SetIsRotated(self, rotate):
        self.kwargs["rotate"] = rotate
        [tick.SetIsRotated(rotate) for tick in self.ticks.values()]


    def SetStyle(self, style):
        self.style = style
        tickclass = allTickStyles[style]
        self.kwargs["rotate"] = self.parent.clockStyle & ROTATE_TICKS

        self.ticks = {}
        for i in range(self.noe):
            self.kwargs["idx"] = i
            self.ticks[i] = tickclass(**self.kwargs)

#----------------------------------------------------------------------

class Box:
    """Gathers info about the clock face and tick sets."""

    def __init__(self, parent, Face, TicksM, TicksH):
        self.parent = parent
        self.Face   = Face
        self.TicksH = TicksH
        self.TicksM = TicksM


    def GetNiceRadiusForHands(self, centre):
        a_tick = self.TicksM.ticks[0]
        scale = a_tick.GetScale()
        bw = max(self.TicksH.dyer.width / 2. * scale,
                 self.TicksM.dyer.width / 2. * scale)

        mgt = self.TicksM.ticks[59]
        my = mgt.pos.y + mgt.GetMaxSize(scale) + bw

        hgt = self.TicksH.ticks[11]
        hy = hgt.pos.y + hgt.GetMaxSize(scale) + bw

        niceradius = centre.y - max(my, hy)
        return niceradius


    def Draw(self, dc):
        [getattr(self, attr).Draw(dc) \
         for attr in ["Face", "TicksM", "TicksH"]]


    def RecalcCoords(self, size, centre, scale):
        [getattr(self, attr).RecalcCoords(size, centre, scale) \
         for attr in ["Face", "TicksH", "TicksM"]]


    def GetTickSize(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetSize())
        return tuple(r)


    def GetTickFillColour(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetFillColour())
        return tuple(r)


    def GetTickBorderColour(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetBorderColour())
        return tuple(r)


    def GetTickBorderWidth(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetBorderWidth())
        return tuple(r)


    def GetTickPolygon(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetPolygon())
        return tuple(r)


    def GetTickFont(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetFont())
        return tuple(r)


    def GetIsRotated(self):
        a_tickset = self.TicksH
        return a_tickset.GetIsRotated()


    def GetTickOffset(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetOffset())
        return tuple(r)


    def GetShadowColour(self):
        a_tickset = self.TicksH
        return a_tickset.GetShadowColour()


    def GetTickStyle(self, target):
        r = []
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                r.append(tick.GetStyle())
        return tuple(r)


    def SetTickSize(self, size, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetSize(size)


    def SetTickFillColour(self, colour, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetFillColour(colour)


    def SetTickBorderColour(self, colour, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetBorderColour(colour)


    def SetTickBorderWidth(self, width, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetBorderWidth(width)


    def SetTickPolygon(self, polygon, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetPolygon(polygon)


    def SetTickFont(self, font, target):
        fs = font.GetNativeFontInfoDesc()
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetFont(wx.FontFromNativeInfoString(fs))


    def SetIsRotated(self, rotate):
        [getattr(self, attr).SetIsRotated(rotate) \
         for attr in ["TicksH", "TicksM"]]


    def SetTickOffset(self, offset, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetOffset(offset)


    def SetShadowColour(self, colour):
        for attr in ["TicksH", "TicksM"]:
            tick = getattr(self, attr)
            tick.SetShadowColour(colour)


    def SetTickStyle(self, style, target):
        for i, attr in enumerate(["TicksH", "TicksM"]):
            if _targets[i] & target:
                tick = getattr(self, attr)
                tick.SetStyle(style)

#----------------------------------------------------------------------

# Relationship between styles and ticks class names.
allTickStyles = {TICKS_BINARY:  TickBinary,
                 TICKS_CIRCLE:  TickCircle,
                 TICKS_DECIMAL: TickDecimal,
                 TICKS_HEX:     TickHex,
                 TICKS_NONE:    TickNone,
                 TICKS_POLY:    TickPoly,
                 TICKS_ROMAN:   TickRoman,
                 TICKS_SQUARE:  TickSquare}


#
##
### eof

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -