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

Source Code for Module muntjac.addon.colorpicker.color_picker_grid

  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.addon.colorpicker.color import Color 
 17   
 18  from muntjac.ui.abstract_component import AbstractComponent 
 19   
 20  from muntjac.addon.colorpicker.color_change_event import ColorChangeEvent 
 21  from muntjac.addon.colorpicker.color_picker import IColorChangeListener 
 22  from muntjac.addon.colorpicker.color_selector import IColorSelector 
 23   
 24   
 25  _COLOR_CHANGE_METHOD = getattr(IColorChangeListener, 'colorChanged') 
 26   
 27   
28 -class ColorPickerGrid(AbstractComponent, IColorSelector):
29 """The Class ColorPickerGrid. 30 31 @author: John Ahlroos 32 @author: Richard Lincoln 33 """ 34 35 CLIENT_WIDGET = None #ClientWidget(VColorPickerGrid) 36 37 TYPE_MAPPING = 'com.vaadin.addon.colorpicker.ColorPickerGrid' 38
39 - def __init__(self, colors_or_rows=None, cols=None):
40 """Instantiates a new color picker grid. 41 42 @param colors_or_rows: 43 the colors or the rows 44 @param columns 45 the columns 46 """ 47 super(ColorPickerGrid, self).__init__() 48 49 # The x-coordinate. 50 self._x = 0 51 52 # The y-coordinate. 53 self._y = 0 54 55 # The rows. 56 self._rows = 1 57 58 # The columns. 59 self._columns = 1 60 61 # The color grid. 62 self._colorGrid = [[None]] 63 64 # The changed colors. 65 self._changedColors = dict() 66 67 if colors_or_rows is None: 68 self._colorGrid[0][0] = Color.WHITE 69 elif cols is None: 70 colors = colors_or_rows 71 self._rows = len(colors) 72 self._columns = len(colors[0]) 73 self._colorGrid = colors 74 75 for row in range(self._rows): 76 for col in range(self._columns): 77 self._changedColors[(row, col)] = self._colorGrid[row][col] 78 79 self.requestRepaint() 80 else: 81 rows, columns = colors_or_rows, cols 82 self.removeStyleName('v-customcomponent') 83 self._rows = rows 84 self._columns = columns 85 self._colorGrid = [([None] * rows) for _ in range(columns)] 86 self._colorGrid[0][0] = Color.WHITE
87 88
89 - def setColorGrid(self, colors):
90 """Sets the color grid. 91 92 @param colors 93 the new color grid 94 """ 95 self._rows = len(colors) 96 self._columns = len(colors[0]) 97 self._colorGrid = colors 98 99 for row in range(self._rows): 100 for col in range(self._columns): 101 self._changedColors[(row, col)] = self._colorGrid[row][col] 102 103 self.requestRepaint()
104 105
106 - def addListener(self, listener, iface=None):
107 """Adds a color change listener 108 109 @param listener: 110 The color change listener 111 """ 112 if (isinstance(listener, IColorChangeListener) and 113 (iface is None or issubclass(iface, IColorChangeListener))): 114 self.registerListener(ColorChangeEvent, listener, 115 _COLOR_CHANGE_METHOD) 116 117 super(ColorPickerGrid, self).addListener(listener, iface)
118 119
120 - def addCallback(self, callback, eventType=None, *args):
121 if eventType is None: 122 eventType = callback._eventType # set by decorator 123 124 if issubclass(eventType, ColorChangeEvent): 125 self.registerCallback(ColorChangeEvent, callback, None, *args) 126 else: 127 super(ColorPickerGrid, self).addCallback(callback, eventType, 128 *args)
129 130
131 - def removeListener(self, listener, iface=None):
132 """Removes a color change listener 133 134 @param listener: 135 The listener 136 """ 137 if (isinstance(listener, IColorChangeListener) and 138 (iface is None or issubclass(iface, IColorChangeListener))): 139 self.withdrawListener(ColorChangeEvent, listener) 140 141 super(ColorPickerGrid, self).removeListener(listener, iface)
142 143
144 - def removeCallback(self, callback, eventType=None):
145 if eventType is None: 146 eventType = callback._eventType 147 148 if issubclass(eventType, ColorChangeEvent): 149 self.withdrawCallback(ColorChangeEvent, callback) 150 151 else: 152 super(ColorPickerGrid, self).removeCallback(callback, eventType)
153 154
155 - def getColor(self):
156 return self._colorGrid[self._x][self._y]
157 158
159 - def setColor(self, color):
160 self._colorGrid[self._x][self._y] = color 161 self._changedColors[(self._x, self._y)] = color 162 self.requestRepaint()
163 164
165 - def setPosition(self, x, y):
166 """Sets the position. 167 168 @param x: 169 the x-coordinate 170 @param y: 171 the y-coordinate 172 """ 173 if x >= 0 and x < self._columns and y >= 0 and y < self._rows: 174 self._x = x 175 self._y = y
176 177
178 - def getPosition(self):
179 """Gets the position. 180 181 @return: the position 182 """ 183 return (self._x, self._y)
184 185
186 - def paintContent(self, target):
187 target.addAttribute('rows', self._rows) 188 target.addAttribute('columns', self._columns) 189 190 if len(self._changedColors) > 0: 191 colors = [None] * len(self._changedColors) 192 XCoords = [None] * len(self._changedColors) 193 YCoords = [None] * len(self._changedColors) 194 counter = 0 195 for p, c in self._changedColors.iteritems(): 196 if c is None: 197 continue 198 199 red = '%.2x' % c.getRed() 200 # red = '0' + red if len(red) < 2 else red 201 202 green = '%.2x' % c.getGreen() 203 # green = '0' + green if len(green) < 2 else green 204 205 blue = '%.2x' % c.getBlue() 206 # blue = '0' + blue if len(blue) < 2 else blue 207 208 color = '#' + red + green + blue 209 210 colors[counter] = color 211 XCoords[counter] = str(p[0]) 212 YCoords[counter] = str(p[1]) 213 counter += 1 214 215 target.addVariable(self, 'changedColors', colors) 216 target.addVariable(self, 'changedX', XCoords) 217 target.addVariable(self, 'changedY', YCoords) 218 219 self._changedColors.clear()
220 221
222 - def changeVariables(self, source, variables):
223 if 'selectedX' in variables and 'selectedY' in variables: 224 self._x = int(str(variables['selectedX'])) 225 self._y = int(str(variables['selectedY'])) 226 227 self.fireColorChanged(self._colorGrid[self._y][self._x]) 228 229 if 'refresh' in variables and variables['refresh'] == False: 230 for row in range(self._rows): 231 for col in range(self._cols): 232 self._changedColors[(row, col)] = self._colorGrid[row][col] 233 234 self.requestRepaint()
235 236
237 - def fireColorChanged(self, color):
238 """Notifies the listeners that a color change has occurred 239 240 @param color: 241 The color which it changed to 242 """ 243 self.fireEvent(ColorChangeEvent(self, color))
244