tuikit/common.py
changeset 64 03f591f5fe5c
parent 63 2a0e04091898
child 74 23767a33a781
equal deleted inserted replaced
63:2a0e04091898 64:03f591f5fe5c
    32 def make_select(*args):
    32 def make_select(*args):
    33     name = ''.join([x.capitalize() for x in args]) + 'Select'
    33     name = ''.join([x.capitalize() for x in args]) + 'Select'
    34     return type(name, (Select,), {'_options': args})
    34     return type(name, (Select,), {'_options': args})
    35 
    35 
    36 
    36 
    37 class Coords:
    37 class Coords(Emitter):
    38 
    38 
    39     '''2D coordinates.'''
    39     '''2D coordinates.'''
    40 
    40 
    41     def __init__(self, x=0, y=0):
    41     def __init__(self, x=0, y=0):
    42         self.x = x
    42         self._x = x
    43         self.y = y
    43         self._y = y
       
    44         self.add_events('change', Event)
       
    45 
       
    46     @property
       
    47     def x(self):
       
    48         return self._x
       
    49 
       
    50     @x.setter
       
    51     def x(self, value):
       
    52         self._x = value
       
    53         self.emit('change')
       
    54 
       
    55     @property
       
    56     def y(self):
       
    57         return self._y
       
    58 
       
    59     @y.setter
       
    60     def y(self, value):
       
    61         self._y = value
       
    62         self.emit('change')
    44 
    63 
    45     def __getitem__(self, key):
    64     def __getitem__(self, key):
    46         try:
    65         try:
    47             tupl = (self.x, self.y)
    66             tupl = (self.x, self.y)
    48             return tupl[key]
    67             return tupl[key]
    58 
    77 
    59     def __repr__(self):
    78     def __repr__(self):
    60         return 'Coords(x={0.x},y={0.y})'.format(self)
    79         return 'Coords(x={0.x},y={0.y})'.format(self)
    61 
    80 
    62     def update(self, x=None, y=None):
    81     def update(self, x=None, y=None):
       
    82         old_x, old_y = self._x, self._y
    63         if isinstance(x, Coords) and y is None:
    83         if isinstance(x, Coords) and y is None:
    64             self.x, self.y = x
    84             self.x, self.y = x
    65         else:
    85         else:
    66             if isinstance(x, int):
    86             if isinstance(x, int):
    67                 self.x = x
    87                 self.x = x
    69                 raise ValueError('Coords.update(): first parameter must be int or Coords')
    89                 raise ValueError('Coords.update(): first parameter must be int or Coords')
    70             if isinstance(y, int):
    90             if isinstance(y, int):
    71                 self.y = y
    91                 self.y = y
    72             elif y is not None:
    92             elif y is not None:
    73                 raise ValueError('Coords.update(): second parameter must be int')
    93                 raise ValueError('Coords.update(): second parameter must be int')
       
    94         if self._x != old_x or self._y != old_y:
       
    95             self.emit('change')
    74 
    96 
    75 
    97 
    76 class Size(Emitter):
    98 class Size(Emitter):
    77 
    99 
    78     '''Size class.
   100     '''Size class.