Package muntjac :: Package addon :: Package colorpicker :: Module color_picker_gradient
[hide private]
[frames] | no frames]

Source Code for Module muntjac.addon.colorpicker.color_picker_gradient

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  from muntjac.ui.abstract_component import AbstractComponent 
 17   
 18  from muntjac.addon.colorpicker.color_change_event import ColorChangeEvent 
 19  from muntjac.addon.colorpicker.color_picker import IColorChangeListener 
 20  from muntjac.addon.colorpicker.color_selector import IColorSelector 
 21   
 22   
 23  _COLOR_CHANGE_METHOD = getattr(IColorChangeListener, 'colorChanged') 
 24   
 25   
26 -class ColorPickerGradient(AbstractComponent, IColorSelector):
27 """The Class ColorPickerGradient. 28 29 @author: John Ahlroos 30 @author: Richard Lincoln 31 """ 32 33 CLIENT_WIDGET = None #ClientWidget(VColorPickerGradient) 34 35 TYPE_MAPPING = 'com.vaadin.addon.colorpicker.ColorPickerGradient' 36
37 - def __init__(self, Id, converter):
38 """Instantiates a new color picker gradient. 39 40 @param id: 41 the id 42 @param converter: 43 the converter 44 """ 45 super(ColorPickerGradient, self).__init__() 46 47 #: The id. 48 self._id = Id 49 50 #: The converter. 51 self._converter = converter 52 53 #: The foreground color. 54 self._color = None 55 56 #: The x-coordinate. 57 self._x = 0 58 59 #: The y-coordinate. 60 self._y = 0 61 62 #: The background color. 63 self._backgroundColor = None 64 65 self.requestRepaint()
66 67
68 - def setColor(self, c):
69 self._color = c 70 coords = self._converter.calculate(c) 71 self._x = coords[0] 72 self._y = coords[1] 73 self.requestRepaint()
74 75
76 - def paintContent(self, target):
77 target.addAttribute('cssid', self._id) 78 79 if self._color is not None: 80 target.addAttribute('cursorX', self._x) 81 target.addAttribute('cursorY', self._y) 82 83 if self._backgroundColor is not None: 84 bgRed = '%.2x' % self._backgroundColor.getRed() 85 # bgRed = '0' + bgRed if len(bgRed) < 2 else bgRed 86 bgGreen = '%.2x' % self._backgroundColor.getGreen() 87 # bgGreen = '0' + bgGreen if len(bgGreen) < 2 else bgGreen 88 bgBlue = '%.2x' % self._backgroundColor.getBlue() 89 # bgBlue = '0' + bgBlue if len(bgBlue) < 2 else bgBlue 90 target.addAttribute('bgColor', '#' + bgRed + bgGreen + bgBlue)
91 92
93 - def changeVariables(self, source, variables):
94 if 'cursorX' in variables and 'cursorY' in variables: 95 self._x = variables['cursorX'] 96 self._y = variables['cursorY'] 97 self._color = self._converter.calculate(self._x, self._y) 98 self.fireColorChanged(self._color)
99 100
101 - def addListener(self, listener, iface=None):
102 103 if (isinstance(listener, IColorChangeListener) and 104 (iface is None or issubclass(iface, IColorChangeListener))): 105 self.registerListener(ColorChangeEvent, listener, 106 _COLOR_CHANGE_METHOD) 107 108 super(ColorPickerGradient, self).addListener(listener, iface)
109 110
111 - def addCallback(self, callback, eventType=None, *args):
112 if eventType is None: 113 eventType = callback._eventType # set by decorator 114 115 if issubclass(eventType, ColorChangeEvent): 116 self.registerCallback(ColorChangeEvent, callback, None, *args) 117 else: 118 super(ColorPickerGradient, self).addCallback(callback, eventType, 119 *args)
120 121
122 - def removeListener(self, listener, iface=None):
123 124 if (isinstance(listener, IColorChangeListener) and 125 (iface is None or issubclass(iface, IColorChangeListener))): 126 self.withdrawListener(ColorChangeEvent, listener) 127 128 super(ColorPickerGradient, self).removeListener(listener, iface)
129 130
131 - def removeCallback(self, callback, eventType=None):
132 if eventType is None: 133 eventType = callback._eventType 134 135 if issubclass(eventType, ColorChangeEvent): 136 self.withdrawCallback(ColorChangeEvent, callback) 137 138 else: 139 super(ColorPickerGradient, self).removeCallback(callback, 140 eventType)
141 142
143 - def setBackgroundColor(self, color):
144 """Sets the background color. 145 146 @param color: 147 the new background color 148 """ 149 self._backgroundColor = color 150 self.requestRepaint()
151 152
153 - def getColor(self):
154 return self._color
155 156
157 - def fireColorChanged(self, color):
158 """Notifies the listeners that the color has changed 159 160 @param color: 161 The color which it changed to 162 """ 163 self.fireEvent(ColorChangeEvent(self, color))
164