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

Source Code for Module muntjac.addon.colorpicker.color_picker_application

  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 StringIO import StringIO 
 17   
 18  from math import pi 
 19   
 20  from datetime import datetime as Date 
 21   
 22  from muntjac.addon.colorpicker.color import Color 
 23   
 24  from muntjac.application import Application 
 25   
 26  from muntjac.ui.check_box import CheckBox 
 27  from muntjac.ui.window import Window 
 28  from muntjac.ui.embedded import Embedded 
 29  from muntjac.ui.panel import Panel 
 30  from muntjac.ui.vertical_layout import VerticalLayout 
 31  from muntjac.ui.horizontal_layout import HorizontalLayout 
 32  from muntjac.ui.grid_layout import GridLayout 
 33  from muntjac.ui.button import IClickListener 
 34  from muntjac.ui.alignment import Alignment 
 35   
 36  from muntjac.terminal.stream_resource import IStreamSource, StreamResource 
 37   
 38  from muntjac.addon.colorpicker.color_picker \ 
 39      import ColorPicker, ButtonStyle, IColorChangeListener 
 40   
 41  from muntjac.addon.canvas.canvas import Canvas 
 42   
 43   
44 -class ColorPickerApplication(Application, IColorChangeListener):
45 """Testing application for the ColorPicker. 46 47 @author: John Ahlroos / ITMill Oy Ltd 2010 48 @author: Richard Lincoln 49 """ 50 51 _VERSION = '1.1.2' 52
53 - def __init__(self):
54 super(ColorPickerApplication, self).__init__() 55 56 # The foreground color. 57 self._foregroundColor = Color.BLACK # The currently selected 58 59 # The background color. 60 self._backgroundColor = Color.WHITE # The currently selected 61 62 # The display box where the image is rendered. 63 self._display = None 64 65 self._mainLayout = None 66 67 self._colorpicker1 = None 68 self._colorpicker2 = None 69 self._colorpicker3 = None 70 self._colorpicker4 = None 71 self._colorpicker5 = None 72 self._colorpicker6 = None 73 74 self._rgbVisible = True 75 self._hsvVisible = True 76 self._swaVisible = True 77 self._historyVisible = True 78 self._txtfieldVisible = True 79 80 self._rgbBox = CheckBox('RGB tab visible') 81 self._hsvBox = CheckBox('HSV tab visible') 82 self._swaBox = CheckBox('Swatches tab visible') 83 self._hisBox = CheckBox('History visible') 84 self._txtBox = CheckBox('CSS field visible')
85 86
87 - def setPopupVisibilities(self):
88 self._rgbBox.setEnabled(not (self._rgbVisible 89 and not self._hsvVisible and not self._swaVisible)) 90 91 self._hsvBox.setEnabled(not (not self._rgbVisible 92 and self._hsvVisible and not self._swaVisible)) 93 94 self._swaBox.setEnabled(not (not self._rgbVisible 95 and not self._hsvVisible and self._swaVisible)) 96 97 self._colorpicker1.setRGBVisibility(self._rgbVisible) 98 self._colorpicker2.setRGBVisibility(self._rgbVisible) 99 self._colorpicker3.setRGBVisibility(self._rgbVisible) 100 self._colorpicker4.setRGBVisibility(self._rgbVisible) 101 self._colorpicker5.setRGBVisibility(self._rgbVisible) 102 self._colorpicker6.setRGBVisibility(self._rgbVisible) 103 104 self._colorpicker1.setHSVVisibility(self._hsvVisible) 105 self._colorpicker2.setHSVVisibility(self._hsvVisible) 106 self._colorpicker3.setHSVVisibility(self._hsvVisible) 107 self._colorpicker4.setHSVVisibility(self._hsvVisible) 108 self._colorpicker5.setHSVVisibility(self._hsvVisible) 109 self._colorpicker6.setHSVVisibility(self._hsvVisible) 110 111 self._colorpicker1.setSwatchesVisibility(self._swaVisible) 112 self._colorpicker2.setSwatchesVisibility(self._swaVisible) 113 self._colorpicker3.setSwatchesVisibility(self._swaVisible) 114 self._colorpicker4.setSwatchesVisibility(self._swaVisible) 115 self._colorpicker5.setSwatchesVisibility(self._swaVisible) 116 self._colorpicker6.setSwatchesVisibility(self._swaVisible) 117 118 self._colorpicker1.setHistoryVisibility(self._historyVisible) 119 self._colorpicker2.setHistoryVisibility(self._historyVisible) 120 self._colorpicker3.setHistoryVisibility(self._historyVisible) 121 self._colorpicker4.setHistoryVisibility(self._historyVisible) 122 self._colorpicker5.setHistoryVisibility(self._historyVisible) 123 self._colorpicker6.setHistoryVisibility(self._historyVisible) 124 125 self._colorpicker1.setTextfieldVisibility(self._txtfieldVisible) 126 self._colorpicker2.setTextfieldVisibility(self._txtfieldVisible) 127 self._colorpicker3.setTextfieldVisibility(self._txtfieldVisible) 128 self._colorpicker4.setTextfieldVisibility(self._txtfieldVisible) 129 self._colorpicker5.setTextfieldVisibility(self._txtfieldVisible) 130 self._colorpicker6.setTextfieldVisibility(self._txtfieldVisible)
131 132
133 - def init(self):
134 # This is called whenever a colorpicker popup is closed 135 main = Window() 136 main.setWidth('1000px') 137 self.setMainWindow(main) 138 139 # Create an instance of the preview and add it to the window 140 # self._display = Embedded('Color preview') 141 self._display = Canvas() 142 self._display.setWidth('270px') 143 self._display.setHeight('270px') 144 145 # Add the foreground and background colorpickers to a layout 146 self._mainLayout = mainLayout = HorizontalLayout() 147 mainLayout.setMargin(True) 148 mainLayout.setSpacing(True) 149 main.setContent(mainLayout) 150 151 layout = VerticalLayout() 152 layout.setWidth('450px') 153 layout.setSpacing(True) 154 155 optPanel = Panel('Customize the color picker popup window', 156 GridLayout(3, 2)) 157 optPanel.getContent().setSizeFull() 158 optPanel.getContent().setMargin(True) 159 optPanel.getContent().setSpacing(True) 160 161 self._rgbBox.addListener(RgbClickListener(self), IClickListener) 162 self._rgbBox.setValue(self._rgbVisible) 163 self._rgbBox.setImmediate(True) 164 optPanel.getContent().addComponent(self._rgbBox) 165 166 self._hsvBox.addListener(HsvClickListener(self), IClickListener) 167 self._hsvBox.setValue(self._hsvVisible) 168 self._hsvBox.setImmediate(True) 169 optPanel.getContent().addComponent(self._hsvBox) 170 171 self._swaBox.addListener(SwaClickListener(self), IClickListener) 172 self._swaBox.setValue(self._swaVisible) 173 self._swaBox.setImmediate(True) 174 optPanel.getContent().addComponent(self._swaBox) 175 176 self._hisBox.addListener(HisClickListener(self), IClickListener) 177 self._hisBox.setValue(self._historyVisible) 178 self._hisBox.setImmediate(True) 179 optPanel.getContent().addComponent(self._hisBox) 180 181 self._txtBox.addListener(TxtClickListener(self), IClickListener) 182 self._txtBox.setValue(self._txtfieldVisible) 183 self._txtBox.setImmediate(True) 184 optPanel.getContent().addComponent(self._txtBox) 185 186 layout.addComponent(optPanel) 187 188 panel1 = Panel( 189 'Button like colorpicker with current color and CSS code', 190 HorizontalLayout()) 191 panel1.getContent().setSizeFull() 192 panel1.getContent().setMargin(True) 193 194 self._colorpicker1 = ColorPicker('Foreground', self._foregroundColor) 195 self._colorpicker1.setWidth('100px') 196 self._colorpicker1.addListener(self) 197 panel1.addComponent(self._colorpicker1) 198 panel1.getContent().setComponentAlignment(self._colorpicker1, 199 Alignment.MIDDLE_CENTER) 200 201 self._colorpicker2 = ColorPicker('Background', self._backgroundColor) 202 self._colorpicker2.addListener(self) 203 self._colorpicker2.setWidth('100px') 204 panel1.addComponent(self._colorpicker2) 205 panel1.getContent().setComponentAlignment(self._colorpicker2, 206 Alignment.MIDDLE_CENTER) 207 layout.addComponent(panel1) 208 209 panel2 = Panel( 210 'Button like colorpicker with current color and custom caption', 211 HorizontalLayout()) 212 panel2.getContent().setSizeFull() 213 panel2.getContent().setMargin(True) 214 self._colorpicker3 = ColorPicker('Foreground', self._foregroundColor) 215 self._colorpicker3.addListener(self) 216 self._colorpicker3.setWidth('120px') 217 self._colorpicker3.setButtonCaption('Foreground') 218 panel2.addComponent(self._colorpicker3) 219 panel2.getContent().setComponentAlignment(self._colorpicker3, 220 Alignment.MIDDLE_CENTER) 221 222 self._colorpicker4 = ColorPicker('Background', self._backgroundColor) 223 self._colorpicker4.addListener(self) 224 self._colorpicker4.setWidth('120px') 225 self._colorpicker4.setButtonCaption('Background') 226 panel2.addComponent(self._colorpicker4) 227 panel2.getContent().setComponentAlignment(self._colorpicker4, 228 Alignment.MIDDLE_CENTER) 229 layout.addComponent(panel2) 230 231 panel3 = Panel( 232 'Color area color picker with caption', 233 HorizontalLayout()) 234 panel3.getContent().setSizeFull() 235 panel3.getContent().setMargin(True) 236 237 self._colorpicker5 = ColorPicker('Foreground', self._foregroundColor) 238 self._colorpicker5.setCaption('Foreground') 239 self._colorpicker5.addListener(self) 240 self._colorpicker5.setButtonStyle(ButtonStyle.BUTTON_AREA) 241 panel3.addComponent(self._colorpicker5) 242 panel3.getContent().setComponentAlignment(self._colorpicker5, 243 Alignment.MIDDLE_CENTER) 244 245 self._colorpicker6 = ColorPicker('Background', self._backgroundColor) 246 self._colorpicker6.setCaption('Background') 247 self._colorpicker6.addListener(self) 248 self._colorpicker6.setButtonStyle(ButtonStyle.BUTTON_AREA) 249 panel3.addComponent(self._colorpicker6) 250 panel3.getContent().setComponentAlignment(self._colorpicker6, 251 Alignment.MIDDLE_CENTER) 252 layout.addComponent(panel3) 253 254 mainLayout.addComponent(layout) 255 mainLayout.addComponent(self._display) 256 257 self.updateDisplay(self._foregroundColor, self._backgroundColor)
258 259
260 - def updateDisplay(self, fg, bg):
261 """Update display. 262 263 @param fg: 264 the foreround color 265 @param bg: 266 the background color 267 """ 268 # imagesource = MyImageSource(fg, bg) 269 # now = Date.now() 270 # frmt = '%H%M%S' 271 # imageresource = StreamResource(imagesource, 272 # 'myimage' + now.strftime(frmt) + '.png', self) 273 # imageresource.setCacheTime(0) 274 # self._display.setSource(imageresource) 275 276 canvas = self._display 277 canvas.saveContext() 278 canvas.clear() 279 canvas.setFillStyle(str(self._backgroundColor)) 280 canvas.fillRect(0, 0, 270, 270) 281 282 canvas.saveContext() 283 canvas.setFillStyle(str(self._foregroundColor)) 284 canvas.arc(135, 135, 100, 0, 2 * pi, True) 285 canvas.fill() 286 canvas.restoreContext() 287 288 canvas.restoreContext()
289 290
291 - def colorChanged(self, event):
292 if ((event.getSource() == self._colorpicker1) 293 or (event.getSource() == self._colorpicker3) 294 or (event.getSource() == self._colorpicker5)): 295 self._foregroundColor = event.getColor() 296 297 if event.getSource() != self._colorpicker1: 298 self._colorpicker1.setColor(event.getColor()) 299 300 if event.getSource() != self._colorpicker3: 301 self._colorpicker3.setColor(event.getColor()) 302 303 if event.getSource() != self._colorpicker5: 304 self._colorpicker5.setColor(event.getColor()) 305 elif ((event.getSource() == self._colorpicker2) 306 or (event.getSource() == self._colorpicker4) 307 or (event.getSource() == self._colorpicker6)): 308 self._backgroundColor = event.getColor() 309 310 if event.getSource() != self._colorpicker2: 311 self._colorpicker2.setColor(event.getColor()) 312 313 if event.getSource() != self._colorpicker4: 314 self._colorpicker4.setColor(event.getColor()) 315 316 if event.getSource() != self._colorpicker6: 317 self._colorpicker6.setColor(event.getColor()) 318 else: 319 return 320 321 oldDisplay = self._display 322 self._display = Canvas() 323 self._display.setWidth('270px') 324 self._display.setHeight('270px') 325 self.updateDisplay(self._foregroundColor, self._backgroundColor) 326 self._mainLayout.replaceComponent(oldDisplay, self._display)
327 328
329 - def getVersion(self):
330 return self._VERSION
331 332
333 -class _ColorClickListener(IClickListener):
334
335 - def __init__(self, app):
336 self._app = app
337 338
339 -class RgbClickListener(_ColorClickListener):
340
341 - def buttonClick(self, event):
342 self._app._rgbVisible = event.getButton().getValue() 343 self._app.setPopupVisibilities()
344 345
346 -class HsvClickListener(_ColorClickListener):
347
348 - def buttonClick(self, event):
349 self._app._hsvVisible = event.getButton().getValue() 350 self._app.setPopupVisibilities()
351 352
353 -class SwaClickListener(_ColorClickListener):
354
355 - def buttonClick(self, event):
356 self._app._swaVisible = event.getButton().getValue() 357 self._app.setPopupVisibilities()
358 359
360 -class HisClickListener(_ColorClickListener):
361
362 - def buttonClick(self, event):
363 self._app._historyVisible = event.getButton().getValue() 364 self._app.setPopupVisibilities()
365 366
367 -class TxtClickListener(_ColorClickListener):
368
369 - def buttonClick(self, event):
370 self._app._txtfieldVisible = event.getButton().getValue() 371 self._app.setPopupVisibilities()
372 373
374 -class MyImageSource(IStreamSource):
375 """This class is used to represent the preview of the color selection.""" 376
377 - def __init__(self, fg, bg):
378 """Instantiates a new my image source. 379 380 @param fg: 381 the foreground color 382 @param bg: 383 the background color 384 """ 385 self._imagebuffer = None 386 387 self._fgColor = fg 388 self._bgColor = bg
389 390
391 - def getStream(self):
392 from PIL import Image, ImageDraw # PIL dependency 393 394 # Create an image and draw something on it. 395 image = Image.new("RGB", (270, 270)) 396 drawable = ImageDraw.Draw(image) 397 drawable.rectangle([0, 0, 270, 270], fill=str(Color.BLUE)) 398 drawable.rectangle([1, 1, 268, 268], fill=str(self._bgColor)) 399 drawable.ellipse([25, 25, 245, 245], fill=str(self._fgColor)) 400 drawable.text((50, 100), 401 'r=' + str(self._fgColor.getRed()) + 402 ',g=' + str(self._fgColor.getGreen()) + 403 ',b=' + str(self._fgColor.getBlue()), fill=str(Color.BLACK)) 404 drawable.text((5, 15), 405 'r=' + str(self._bgColor.getRed()) + 406 ',g=' + str(self._bgColor.getGreen()) + 407 ',b=' + str(self._bgColor.getBlue()), fill=str(Color.BLACK)) 408 409 del drawable 410 411 try: 412 # Write the image to a buffer. 413 self._imagebuffer = StringIO() 414 image.save(self._imagebuffer, 'PNG') 415 return self._imagebuffer 416 except IOError: 417 return None
418 419 420 if __name__ == '__main__': 421 from muntjac.main import muntjac 422 muntjac(ColorPickerApplication, nogui=True, forever=True, debug=True) 423