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

Source Code for Module muntjac.addon.canvas.canvas

  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   
19 -class Canvas(AbstractComponent):
20 """Server side component for the VCanvas widget.""" 21 22 CLIENT_WIDGET = None #ClientWidget(VCanvas) 23 24 TYPE_MAPPING = 'org.vaadin.hezamu.canvas.Canvas' 25 26 BEVEL = 'BEVEL' 27 BUTT = 'BUTT' 28 DESTINATION_OVER = 'DESTINATION_OVER' 29 SOURCE_OVER = 'SOURCE_OVER' 30 MITER = 'MITER' 31 TRANSPARENT = 'TRANSPARENT' 32 ROUND = 'ROUND' 33 SQUARE = 'SQUARE' 34
35 - def __init__(self):
36 super(Canvas, self).__init__() 37 38 self._commands = list() 39 self._downListeners = list() 40 self._upListeners = list()
41 42
43 - def createLinearGradient(self, name, x0, y0, x1, y1):
44 arguments = dict() 45 arguments['command'] = 'createLinearGradient' 46 arguments['name'] = name 47 arguments['x0'] = x0 48 arguments['y0'] = y0 49 arguments['x1'] = x1 50 arguments['y1'] = y1 51 self._commands.append(arguments) 52 self.requestRepaint()
53 54
55 - def createRadialGradient(self, name, x0, y0, r0, x1, y1, r1):
56 arguments = dict() 57 arguments['command'] = 'createRadialGradient' 58 arguments['name'] = name 59 arguments['x0'] = x0 60 arguments['y0'] = y0 61 arguments['r0'] = r0 62 arguments['x1'] = x1 63 arguments['y1'] = y1 64 arguments['r1'] = r1 65 self._commands.append(arguments) 66 self.requestRepaint()
67 68
69 - def cubicCurveTo(self, cp1x, cp1y, cp2x, cp2y, x, y):
70 arguments = dict() 71 arguments['command'] = 'cubicCurveTo' 72 arguments['cp1x'] = cp1x 73 arguments['cp1y'] = cp1y 74 arguments['cp2x'] = cp2x 75 arguments['cp2y'] = cp2y 76 arguments['x'] = x 77 arguments['y'] = y 78 self._commands.append(arguments) 79 self.requestRepaint()
80 81
82 - def drawImage(self, *args):
83 nargs = len(args) 84 if nargs == 3: 85 url, offsetX, offsetY = args 86 arguments = dict() 87 arguments['command'] = 'drawImage1' 88 arguments['url'] = url 89 arguments['offsetX'] = offsetX 90 arguments['offsetY'] = offsetY 91 self._commands.append(arguments) 92 self.requestRepaint() 93 elif nargs == 5: 94 url, offsetX, offsetY, width, height = args 95 arguments = dict() 96 arguments['command'] = 'drawImage2' 97 arguments['url'] = url 98 arguments['offsetX'] = offsetX 99 arguments['offsetY'] = offsetY 100 arguments['width'] = width 101 arguments['height'] = height 102 self._commands.append(arguments) 103 self.requestRepaint() 104 elif nargs == 9: 105 (url, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, 106 destWidth, destHeight) = args 107 arguments = dict() 108 arguments['command'] = 'drawImage3' 109 arguments['url'] = url 110 arguments['sourceX'] = sourceX 111 arguments['sourceY'] = sourceY 112 arguments['sourceWidth'] = sourceWidth 113 arguments['sourceHeight'] = sourceHeight 114 arguments['destX'] = destX 115 arguments['destY'] = destY 116 arguments['destWidth'] = destWidth 117 arguments['destHeight'] = destHeight 118 self._commands.append(arguments) 119 self.requestRepaint() 120 else: 121 raise ValueError
122 123
124 - def fill(self):
125 arguments = dict() 126 arguments['command'] = 'fill' 127 self._commands.append(arguments) 128 self.requestRepaint()
129 130
131 - def fillRect(self, startX, startY, width, height):
132 arguments = dict() 133 arguments['command'] = 'fillRect' 134 arguments['startX'] = startX 135 arguments['startY'] = startY 136 arguments['width'] = width 137 arguments['height'] = height 138 self._commands.append(arguments) 139 self.requestRepaint()
140 141
142 - def lineTo(self, x, y):
143 arguments = dict() 144 arguments['command'] = 'lineTo' 145 arguments['x'] = x 146 arguments['y'] = y 147 self._commands.append(arguments) 148 self.requestRepaint()
149 150
151 - def moveTo(self, x, y):
152 arguments = dict() 153 arguments['command'] = 'moveTo' 154 arguments['x'] = x 155 arguments['y'] = y 156 self._commands.append(arguments) 157 self.requestRepaint()
158 159
160 - def quadraticCurveTo(self, cpx, cpy, x, y):
161 arguments = dict() 162 arguments['command'] = 'quadraticCurveTo' 163 arguments['cpx'] = cpx 164 arguments['cpy'] = cpy 165 arguments['x'] = x 166 arguments['y'] = y 167 self._commands.append(arguments) 168 self.requestRepaint()
169 170
171 - def rect(self, startX, startY, width, height):
172 arguments = dict() 173 arguments['command'] = 'rect' 174 arguments['startX'] = startX 175 arguments['startY'] = startY 176 arguments['width'] = width 177 arguments['height'] = height 178 self._commands.append(arguments) 179 self.requestRepaint()
180 181
182 - def rotate(self, angle):
183 arguments = dict() 184 arguments['command'] = 'rotate' 185 arguments['angle'] = angle 186 self._commands.append(arguments) 187 self.requestRepaint()
188 189
190 - def setGradientFillStyle(self, gradient):
191 arguments = dict() 192 arguments['command'] = 'setGradientFillStyle' 193 arguments['gradient'] = gradient 194 self._commands.append(arguments) 195 self.requestRepaint()
196 197
198 - def setFillStyle(self, *args):
199 nargs = len(args) 200 if nargs == 1: 201 color, = args 202 arguments = dict() 203 arguments['command'] = 'setFillStyle' 204 arguments['color'] = color 205 self._commands.append(arguments) 206 self.requestRepaint() 207 elif nargs == 3: 208 r, g, b = args 209 self.setFillStyle('#%02x%02x%02x' % r, g, b) 210 else: 211 raise ValueError
212 213
214 - def setLineCap(self, lineCap):
215 arguments = dict() 216 arguments['command'] = 'setLineCap' 217 arguments['lineCap'] = lineCap 218 self._commands.append(arguments) 219 self.requestRepaint()
220 221
222 - def setLineJoin(self, lineJoin):
223 arguments = dict() 224 arguments['command'] = 'setLineJoin' 225 arguments['lineJoin'] = lineJoin 226 self._commands.append(arguments) 227 self.requestRepaint()
228 229
230 - def setLineWidth(self, width):
231 arguments = dict() 232 arguments['command'] = 'setLineWidth' 233 arguments['width'] = width 234 self._commands.append(arguments) 235 self.requestRepaint()
236 237
238 - def setMiterLimit(self, miterLimit):
239 arguments = dict() 240 arguments['command'] = 'setMiterLimit' 241 arguments['miterLimit'] = miterLimit 242 self._commands.append(arguments) 243 self.requestRepaint()
244 245
246 - def setGradientStrokeStyle(self, gradient):
247 arguments = dict() 248 arguments['command'] = 'setGradientStrokeStyle' 249 arguments['gradient'] = gradient 250 self._commands.append(arguments) 251 self.requestRepaint()
252 253
254 - def setColorStrokeStyle(self, color):
255 arguments = dict() 256 arguments['command'] = 'setColorStrokeStyle' 257 arguments['color'] = color 258 self._commands.append(arguments) 259 self.requestRepaint()
260 261
262 - def strokeRect(self, startX, startY, width, height):
263 arguments = dict() 264 arguments['command'] = 'strokeRect' 265 arguments['startX'] = startX 266 arguments['startY'] = startY 267 arguments['width'] = width 268 arguments['height'] = height 269 self._commands.append(arguments) 270 self.requestRepaint()
271 272
273 - def transform(self, m11, m12, m21, m22, dx, dy):
274 arguments = dict() 275 arguments['command'] = 'transform' 276 arguments['m11'] = m11 277 arguments['m12'] = m12 278 arguments['m21'] = m21 279 arguments['m22'] = m22 280 arguments['dx'] = dx 281 arguments['dy'] = dy 282 self._commands.append(arguments) 283 self.requestRepaint()
284 285
286 - def arc(self, x, y, radius, startAngle, endAngle, antiClockwise):
287 arguments = dict() 288 arguments['command'] = 'arc' 289 arguments['x'] = x 290 arguments['y'] = y 291 arguments['radius'] = radius 292 arguments['startAngle'] = startAngle 293 arguments['endAngle'] = endAngle 294 self._commands.append(arguments) 295 self.requestRepaint()
296 297
298 - def translate(self, x, y):
299 arguments = dict() 300 arguments['command'] = 'translate' 301 arguments['x'] = x 302 arguments['y'] = y 303 self._commands.append(arguments) 304 self.requestRepaint()
305 306
307 - def scale(self, x, y):
308 arguments = dict() 309 arguments['command'] = 'scale' 310 arguments['x'] = x 311 arguments['y'] = y 312 self._commands.append(arguments) 313 self.requestRepaint()
314 315
316 - def stroke(self):
317 arguments = dict() 318 arguments['command'] = 'stroke' 319 self._commands.append(arguments) 320 self.requestRepaint()
321 322
323 - def saveContext(self):
324 arguments = dict() 325 arguments['command'] = 'saveContext' 326 self._commands.append(arguments) 327 self.requestRepaint()
328 329
330 - def restoreContext(self):
331 arguments = dict() 332 arguments['command'] = 'restoreContext' 333 self._commands.append(arguments) 334 self.requestRepaint()
335 336
337 - def setBackgroundColor(self, rgb):
338 arguments = dict() 339 arguments['command'] = 'setBackgroundColor' 340 arguments['rgb'] = rgb 341 self._commands.append(arguments) 342 self.requestRepaint()
343 344
345 - def setStrokeColor(self, rgb):
346 arguments = dict() 347 arguments['command'] = 'setStrokeColor' 348 arguments['rgb'] = rgb 349 self._commands.append(arguments) 350 self.requestRepaint()
351 352
353 - def beginPath(self):
354 arguments = dict() 355 arguments['command'] = 'beginPath' 356 self._commands.append(arguments) 357 self.requestRepaint()
358 359
360 - def clear(self):
361 arguments = dict() 362 arguments['command'] = 'clear' 363 self._commands.append(arguments) 364 self.requestRepaint()
365 366
367 - def reset(self):
368 self._commands.clear() 369 self.clear()
370 371
372 - def paintContent(self, target):
373 super(Canvas, self).paintContent(target) 374 for command in self._commands: 375 target.startTag(command.get('command')) 376 377 for key in command: 378 if key == 'command': 379 continue # This is already in tag name 380 381 value = command.get(key) 382 target.addAttribute(key, value) 383 384 target.endTag(command.get('command'))
385 386
387 - def changeVariables(self, source, variables):
388 if 'sizeChanged' in variables: 389 print 'Canvass size now ' + str(variables['sizeChanged']) 390 self.requestRepaint() 391 elif 'event' in variables: 392 eventtype = variables['event'] 393 x = variables['mx'] 394 y = variables['my'] 395 if eventtype == 'mousedown': 396 self.fireMouseDown(x, y) 397 elif eventtype == 'mouseup': 398 self.fireMouseUp(x, y) 399 else: 400 print 'Unknown event type: ' + eventtype
401 402
403 - def setStrokeStyle(self, r, g, b):
404 self.setStrokeColor('#%02x%02x%02x' % (r, g, b))
405 406
407 - def setGlobalAlpha(self, alpha):
408 arguments = dict() 409 arguments['command'] = 'setGlobalAlpha' 410 arguments['alpha'] = alpha 411 self._commands.append(arguments) 412 self.requestRepaint()
413 414
415 - def closePath(self):
416 arguments = dict() 417 arguments['command'] = 'closePath' 418 self._commands.append(arguments) 419 self.requestRepaint()
420 421
422 - def setGlobalCompositeOperation(self, mode):
423 arguments = dict() 424 arguments['command'] = 'setGlobalCompositeOperation' 425 arguments['mode'] = mode 426 self._commands.append(arguments) 427 self.requestRepaint()
428 429
430 - def addColorStop(self, gradient, offset, color):
431 arguments = dict() 432 arguments['command'] = 'addColorStop' 433 arguments['gradient'] = gradient 434 arguments['offset'] = offset 435 arguments['color'] = color 436 self._commands.append(arguments) 437 self.requestRepaint()
438 439
440 - def addListener(self, listener, iface=None):
441 if isinstance(listener, ICanvasMouseDownListener): 442 if listener not in self._downListeners: 443 self._downListeners.append(listener) 444 else: 445 if listener not in self._upListeners: 446 self._upListeners.append(listener)
447 448
449 - def removeListener(self, listener, iface=None):
450 if isinstance(listener, ICanvasMouseDownListener): 451 if listener in self._downListeners: 452 self._downListeners.remove(listener) 453 else: 454 if listener in self._upListeners: 455 self._upListeners.remove(listener)
456 457
458 - def fireMouseDown(self, x, y):
459 for listener in self._downListeners: 460 listener.mouseDown(x, y)
461 462
463 - def fireMouseUp(self, x, y):
464 for listener in self._upListeners: 465 listener.mouseDown(x, y)
466 467
468 -class ICanvasMouseDownListener(object):
469
470 - def mouseDown(self, x, y):
471 raise NotImplementedError
472 473
474 -class ICanvasMouseUpListener(object):
475
476 - def mouseDown(self, x, y):
477 raise NotImplementedError
478