1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29 """The Class ColorPickerGrid.
30
31 @author: John Ahlroos
32 @author: Richard Lincoln
33 """
34
35 CLIENT_WIDGET = None
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
50 self._x = 0
51
52
53 self._y = 0
54
55
56 self._rows = 1
57
58
59 self._columns = 1
60
61
62 self._colorGrid = [[None]]
63
64
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
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
118
119
120 - def addCallback(self, callback, eventType=None, *args):
129
130
142
143
153
154
156 return self._colorGrid[self._x][self._y]
157
158
160 self._colorGrid[self._x][self._y] = color
161 self._changedColors[(self._x, self._y)] = color
162 self.requestRepaint()
163
164
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
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
201
202 green = '%.2x' % c.getGreen()
203
204
205 blue = '%.2x' % c.getBlue()
206
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
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
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