1# 2# GraphicsArrayController.py 3# GraphicsBindings 4# 5# Converted by u.fiedler on feb 2005 6# with great help from Bob Ippolito - Thank you Bob! 7# 8# The original version was written in Objective-C by Malcolm Crawford 9# http://homepage.mac.com/mmalc/CocoaExamples/controllers.html 10 11from sys import maxint 12from Foundation import * 13from AppKit import * 14from random import random 15from math import fabs 16 17class GraphicsArrayController (NSArrayController): 18 """Allow filtering by color, just for the fun of it""" 19 20 filterColor = objc.IBOutlet() 21 newCircle = objc.IBOutlet() 22 shouldFilter = objc.ivar.BOOL() 23 graphicsView = objc.IBOutlet() 24 25 def arrangeObjects_(self, objects): 26 "Filtering is not yet connected in IB!" 27 # XXX: This doesn't work yet, so disable 28 if self.shouldFilter: 29 self.shouldFilter = False 30 31 if not self.shouldFilter: 32 return super(GraphicsArrayController, self).arrangeObjects_(objects) 33 34 if self.filterColor is None: 35 self.filterColor = NSColor.blackColor().colorUsingColorSpaceName_(NSCalibratedRGBColorSpace) 36 37 filterHue = self.filterColor.hueComponent() 38 filteredObjects = [] 39 for item in objects: 40 hue = item.color.hueComponent() 41 if ((fabs(hue - filterHue) < 0.05) or 42 (fabs(hue - filterHue) > 0.95) or 43 (item is self.newCircle)): 44 filteredObjects.append(item) 45 self.newCircle = None 46 return super(GraphicsArrayController, self).arrangeObjects_(filteredObjects) 47 48 def newObject(self): 49 "Randomize attributes of new circles so we get a pretty display" 50 self.newCircle = super(GraphicsArrayController, self).newObject() 51 radius = 5.0 + 15.0 * random() 52 self.newCircle.radius = radius 53 54 height = self.graphicsView.bounds().size.height 55 width = self.graphicsView.bounds().size.width 56 57 xOffset = 10.0 + (height - 20.0) * random() 58 yOffset = 10.0 + (width - 20.0) * random() 59 60 self.newCircle.xLoc = xOffset 61 self.newCircle.yLoc = height - yOffset 62 63 color = NSColor.colorWithCalibratedHue_saturation_brightness_alpha_( 64 random(), 65 (0.5 + random() / 2.0), 66 (0.333 + random() / 3.0), 67 1.0) 68 69 self.newCircle.color = color 70 return self.newCircle 71