test_dcDrawLists.py 6.86 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
import unittest
from unittests import wtc
import wx
import random
try:
    import numpy as np
    haveNumpy = True
except ImportError:
    haveNumpy = False

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

w = 600
h = 400
num = 500

colornames = ["BLACK",
         "BLUE",
         "BLUE VIOLET",
         "BROWN",
         "CYAN",
         "DARK GREY",
         "DARK GREEN",
         "GOLD",
         "GREY",
         "GREEN",
         "MAGENTA",
         "NAVY",
         "PINK",
         "RED",
         "SKY BLUE",
         "VIOLET",
         "YELLOW",
         ]
pencache = {}
brushcache = {}

def makeRandomPoints():
    pnts = []

    for i in range(num):
        x = random.randint(0, w)
        y = random.randint(0, h)
        pnts.append( (x,y) )

    return pnts


def makeRandomLines():
    lines = []

    for i in range(num):
        x1 = random.randint(0, w)
        y1 = random.randint(0, h)
        x2 = random.randint(0, w)
        y2 = random.randint(0, h)
        lines.append( (x1,y1, x2,y2) )

    return lines


def makeRandomRectangles():
    rects = []

    for i in range(num):
        W = random.randint(10, w/2)
        H = random.randint(10, h/2)
        x = random.randint(0, w - W)
        y = random.randint(0, h - H)
        rects.append( (x, y, W, H) )

    return rects


def makeRandomPolygons():
    Np = 8 # number of points per polygon
    polys = []

    for i in range(num):
        poly = []

        for i in range(Np):
            x = random.randint(0, w)
            y = random.randint(0, h)
            poly.append( (x,y) )

        polys.append( poly )

    return polys


def makeRandomText():
    Np = 8 # number of characters in text
    text = []

    for i in range(num):
        word = []

        for i in range(Np):
            c = chr( random.randint(48, 122) )
            word.append( c )

        text.append( "".join(word) )

    return text


def makeRandomColors():
    colors = []
    for i in range(num):
        c = random.choice(colornames)
        colors.append(wx.Colour(c))
    return colors


def makeRandomPens():
    pens = []
    for i in range(num):
        c = random.choice(colornames)
        t = random.randint(1, 4)
        if (c,t) not in pencache:
            pencache[(c, t)] = wx.Pen(c, t)
        pens.append( pencache[(c, t)] )
    return pens


def makeRandomBrushes():
    brushes = []
    for i in range(num):
        c = random.choice(colornames)
        if c not in brushcache:
            brushcache[c] = wx.Brush(c)
        brushes.append( brushcache[c] )
    return brushes


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


class dcDrawLists_Tests(wtc.WidgetTestCase):

    def test_dcDrawPointLists(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))

        pens = makeRandomPens()

        dc.DrawPointList(makeRandomPoints())
        dc.DrawPointList(makeRandomPoints(), wx.Pen("RED", 1))
        dc.DrawPointList(makeRandomPoints(), pens)
        del dc


    @unittest.skipIf(not haveNumpy, "Numpy required for this test")
    def test_dcDrawPointArray(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))

        pens = makeRandomPens()

        dc.DrawPointList(np.array(makeRandomPoints()))
        dc.DrawPointList(np.array(makeRandomPoints()), wx.Pen("RED", 1))
        dc.DrawPointList(np.array(makeRandomPoints()), pens)
        del dc


    def test_dcDrawLineLists(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))

        pens = makeRandomPens()

        dc.DrawLineList(makeRandomLines())
        dc.DrawLineList(makeRandomLines(), wx.Pen("RED", 2))
        dc.DrawLineList(makeRandomLines(), pens)
        del dc


    def test_dcDrawRectangleLists(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))
        dc.SetBrush( wx.Brush("RED") )

        pens = makeRandomPens()
        brushes = makeRandomBrushes()

        dc.DrawRectangleList(makeRandomRectangles())
        dc.DrawRectangleList(makeRandomRectangles(),pens)
        dc.DrawRectangleList(makeRandomRectangles(),pens[0],brushes)
        dc.DrawRectangleList(makeRandomRectangles(),pens,brushes[0])
        dc.DrawRectangleList(makeRandomRectangles(),None,brushes)
        del dc


    @unittest.skipIf(not haveNumpy, "Numpy required for this test")
    def test_dcDrawRectangleArray(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))
        dc.SetBrush( wx.Brush("RED") )

        pens = makeRandomPens()
        brushes = makeRandomBrushes()

        dc.DrawRectangleList(np.array(makeRandomRectangles()))
        dc.DrawRectangleList(np.array(makeRandomRectangles()),pens)
        dc.DrawRectangleList(np.array(makeRandomRectangles()),pens[0],brushes)
        dc.DrawRectangleList(np.array(makeRandomRectangles()),pens,brushes[0])
        dc.DrawRectangleList(np.array(makeRandomRectangles()),None,brushes)
        del dc


    def test_dcDrawElipseLists(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))
        dc.SetBrush( wx.Brush("RED") )

        pens = makeRandomPens()
        brushes = makeRandomBrushes()

        dc.DrawEllipseList(makeRandomRectangles())
        dc.DrawEllipseList(makeRandomRectangles(),pens)
        dc.DrawEllipseList(makeRandomRectangles(),pens[0],brushes)
        dc.DrawEllipseList(makeRandomRectangles(),pens,brushes[0])
        dc.DrawEllipseList(makeRandomRectangles(),None,brushes)
        del dc


    def test_dcDrawPloygonLists(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetPen(wx.Pen("BLACK", 1))
        dc.SetBrush( wx.Brush("RED") )

        pens = makeRandomPens()
        brushes = makeRandomBrushes()
        polygons = makeRandomPolygons()

        dc.DrawPolygonList(polygons)
        dc.DrawPolygonList(polygons, pens)
        dc.DrawPolygonList(polygons, pens[0],brushes)
        dc.DrawPolygonList(polygons, pens,brushes[0])
        dc.DrawPolygonList(polygons, None,brushes)
        del dc


    def test_dcDrawTextLists(self):
        pnl = wx.Panel(self.frame)
        self.frame.SetSize((w,h))
        dc = wx.ClientDC(pnl)
        dc.SetBackgroundMode(wx.SOLID)

        points = makeRandomPoints()
        fore = makeRandomColors()
        back = makeRandomColors()
        texts = makeRandomText()

        dc.DrawTextList(texts, points, fore, back)
        del dc



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

if __name__ == '__main__':
    unittest.main()