tuikit/common.py
changeset 35 f62c8269ded6
parent 26 37745c5abc49
child 40 5faa38c10b67
equal deleted inserted replaced
34:e3beacd5e536 35:f62c8269ded6
     1 # -*- coding: utf-8 -*-
     1 # -*- coding: utf-8 -*-
     2 
     2 
     3 
     3 
     4 class Coords:
     4 class Coords:
     5     
     5 
     6     '''2D coordinates.'''
     6     '''2D coordinates.'''
     7     
     7 
     8     def __init__(self, x=0, y=0):
     8     def __init__(self, x=0, y=0):
     9         self.x = x
     9         self.x = x
    10         self.y = y
    10         self.y = y
    11 
    11 
    12     def __getitem__(self, key):
    12     def __getitem__(self, key):
    14             tupl = (self.x, self.y)
    14             tupl = (self.x, self.y)
    15             return tupl[key]
    15             return tupl[key]
    16         except TypeError:
    16         except TypeError:
    17             pass
    17             pass
    18         return self.__dict__[key]
    18         return self.__dict__[key]
    19     
    19 
    20     def __setitem__(self, key, value):
    20     def __setitem__(self, key, value):
    21         if key == 0:
    21         if key == 0:
    22             self.x = value
    22             self.x = value
    23         if key == 1:
    23         if key == 1:
    24             self.y = value
    24             self.y = value
    26     def __repr__(self):
    26     def __repr__(self):
    27         return 'Coords(x={0.x},y={0.y})'.format(self)
    27         return 'Coords(x={0.x},y={0.y})'.format(self)
    28 
    28 
    29 
    29 
    30 class Size:
    30 class Size:
    31     
    31 
    32     '''Size class.
    32     '''Size class.
    33     
    33 
    34     Implements attribute access (.w, .h), list-like access([0],[1])
    34     Implements attribute access (.w, .h), list-like access([0],[1])
    35     and dict-like access (['w'],['h']).
    35     and dict-like access (['w'],['h']).
    36     
    36 
    37     '''
    37     '''
    38     
    38 
    39     def __init__(self, w=None, h=None):
    39     def __init__(self, w=None, h=None):
    40         self.w = w
    40         self.w = w
    41         self.h = h
    41         self.h = h
    42     
    42 
    43     def __getitem__(self, key):
    43     def __getitem__(self, key):
    44         try:
    44         try:
    45             tupl = (self.w, self.h)
    45             tupl = (self.w, self.h)
    46             return tupl[key]
    46             return tupl[key]
    47         except TypeError:
    47         except TypeError:
    51     def __setitem__(self, key, value):
    51     def __setitem__(self, key, value):
    52         if key in [0, 'w']:
    52         if key in [0, 'w']:
    53             self.w = value
    53             self.w = value
    54         if key in [1, 'h']:
    54         if key in [1, 'h']:
    55             self.h = value
    55             self.h = value
    56     
    56 
    57     def __repr__(self):
    57     def __repr__(self):
    58         return 'Size(w={0.w},h={0.h})'.format(self)
    58         return 'Size(w={0.w},h={0.h})'.format(self)
    59 
    59 
    60 
    60 
    61 class Rect:
    61 class Rect:
    62     
    62 
    63     '''Rectangle is defined by 2D coordinates and size.'''
    63     '''Rectangle is defined by 2D coordinates and size.'''
    64     
    64 
    65     def __init__(self, x=0, y=0, w=0, h=0):
    65     def __init__(self, x=0, y=0, w=0, h=0):
    66         self.x = x
    66         self.x = x
    67         self.y = y
    67         self.y = y
    68         self.w = w
    68         self.w = w
    69         self.h = h
    69         self.h = h
    71     def __repr__(self):
    71     def __repr__(self):
    72         return 'Rect(x={0.x},y={0.y},w={0.w},h={0.h})'.format(self)
    72         return 'Rect(x={0.x},y={0.y},w={0.w},h={0.h})'.format(self)
    73 
    73 
    74 
    74 
    75 class Borders:
    75 class Borders:
    76     
    76 
    77     '''Borders are defined by left, top, right, bottom border size.
    77     '''Borders are defined by left, top, right, bottom border size.
    78     
    78 
    79     Ordering is clock-wise, starting with left. This may seem weird,
    79     Ordering is clock-wise, starting with left. This may seem weird,
    80     but it corresponds to X/Y or W/H used elsewhere. Left and right are
    80     but it corresponds to X/Y or W/H used elsewhere. Left and right are
    81     on X axis, so they are defined first.
    81     on X axis, so they are defined first.
    82     
    82 
    83     '''
    83     '''
    84     
    84 
    85     def __init__(self, l=0, t=0, r=0, b=0):
    85     def __init__(self, l=0, t=0, r=0, b=0):
    86         self.l = l # left
    86         self.l = l # left
    87         self.t = t # top
    87         self.t = t # top
    88         self.r = r # right
    88         self.r = r # right
    89         self.b = b # bottom
    89         self.b = b # bottom
    93             tupl = (self.l, self.t, self.r, self.b)
    93             tupl = (self.l, self.t, self.r, self.b)
    94             return tupl[key]
    94             return tupl[key]
    95         except TypeError:
    95         except TypeError:
    96             pass
    96             pass
    97         return self.__dict__[key]
    97         return self.__dict__[key]
    98     
    98 
    99     def __repr__(self):
    99     def __repr__(self):
   100         return 'Borders(l={0.l},t={0.t},r={0.r},b={0.b})'.format(self)
   100         return 'Borders(l={0.l},t={0.t},r={0.r},b={0.b})'.format(self)
   101 
   101 
   102 
   102 
   103 class ClipStack:
   103 class ClipStack:
   104     
   104 
   105     '''Stack of clipping regions.'''
   105     '''Stack of clipping regions.'''
   106     
   106 
   107     def __init__(self):
   107     def __init__(self):
   108         self.stack = []
   108         self.stack = []
   109     
   109 
   110     def push(self, x, y, w, h):
   110     def push(self, x, y, w, h):
   111         newclip = Rect(x, y, w, h)
   111         newclip = Rect(x, y, w, h)
   112         if len(self.stack):
   112         if len(self.stack):
   113             oldclip = self.stack[-1]
   113             oldclip = self.stack[-1]
   114             newclip = self.intersect(oldclip, newclip)
   114             newclip = self.intersect(oldclip, newclip)
   145         h = max(r1.y + r1.h, r2.y + r2.h) - y
   145         h = max(r1.y + r1.h, r2.y + r2.h) - y
   146         return Rect(x, y, w, h)
   146         return Rect(x, y, w, h)
   147 
   147 
   148 
   148 
   149 class UnicodeGraphics:
   149 class UnicodeGraphics:
   150     
   150 
   151     '''Unicode graphics bank.
   151     '''Unicode graphics bank.
   152     
   152 
   153     This class can be overriden to change graphics style (round corners etc.).'''
   153     This class can be overriden to change graphics style (round corners etc.).'''
   154     
   154 
   155     # http://en.wikipedia.org/wiki/List_of_Unicode_characters#Geometric_shapes
   155     # http://en.wikipedia.org/wiki/List_of_Unicode_characters#Geometric_shapes
   156     UP_ARROW = '▲' #curses.ACS_UARROW
   156     UP_ARROW = '▲' #curses.ACS_UARROW
   157     DOWN_ARROW = '▼' #curses.ACS_DARROW
   157     DOWN_ARROW = '▼' #curses.ACS_DARROW
   158     
   158 
   159     # http://en.wikipedia.org/wiki/Box-drawing_characters
   159     # http://en.wikipedia.org/wiki/Box-drawing_characters
   160     LIGHT_SHADE = '░' #curses.ACS_BOARD
   160     LIGHT_SHADE = '░' #curses.ACS_BOARD
   161     MEDIUM_SHADE = '▒'
   161     MEDIUM_SHADE = '▒'
   162     DARK_SHADE = '▓'
   162     DARK_SHADE = '▓'
   163     BLOCK = '█'
   163     BLOCK = '█'
   164     
   164 
   165     COLUMN = '▁▂▃▄▅▆▇█'
   165     COLUMN = '▁▂▃▄▅▆▇█'
   166     CORNER_ROUND = '╭╮╰╯'
   166     CORNER_ROUND = '╭╮╰╯'
   167     CORNER = '┌┐└┘'
   167     CORNER = '┌┐└┘'
   168     LINE = '─━│┃┄┅┆┇┈┉┊┋'
   168     LINE = '─━│┃┄┅┆┇┈┉┊┋'
   169 
   169 
   176     LTEE = '├'
   176     LTEE = '├'
   177     RTEE = '┤'
   177     RTEE = '┤'
   178 
   178 
   179 
   179 
   180 class MouseEvent:
   180 class MouseEvent:
   181     def __init__(self, x=0, y=0):
   181     def __init__(self, x=0, y=0, button=0):
   182         self.x = x   # global coordinates
   182         self.x = x   # global coordinates
   183         self.y = y
   183         self.y = y
   184         self.wx = x  # local widget coordinates
   184         self.wx = x  # local widget coordinates
   185         self.wy = y
   185         self.wy = y
   186         self.px = 0  # parent coordinates
   186         self.px = 0  # parent coordinates
   187         self.py = 0
   187         self.py = 0
   188         self.button = 0
   188         self.button = button
   189 
   189 
   190 
   190 
   191     def childevent(self, child):
   191     def childevent(self, child):
   192         ev = MouseEvent(self.x, self.y)
   192         ev = MouseEvent(self.x, self.y, self.button)
   193         # original local coordinates are new parent coordinates
   193         # original local coordinates are new parent coordinates
   194         ev.px = self.wx
   194         ev.px = self.wx
   195         ev.py = self.wy
   195         ev.py = self.wy
   196         # update local coordinates
   196         # update local coordinates
   197         ev.wx = self.wx - child.x
   197         ev.wx = self.wx - child.x
   198         ev.wy = self.wy - child.y
   198         ev.wy = self.wy - child.y
   199 
       
   200         return ev
   199         return ev
   201 
   200 
       
   201     def __repr__(self):
       
   202         return 'MouseEvent(x={0.x},y={0.y},button={0.button})'.format(self)
       
   203