• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/pyobjc-42/pyobjc/pyobjc-framework-Cocoa/Examples/AppKit/DragItemAround/
1# Updated by mvl on 2009-02-22 for PyObjC 2
2
3import objc
4from PyObjCTools import AppHelper
5from AppKit import *
6from Foundation import *
7
8
9class DraggableItemView(NSView):
10    """."""
11    _locationDefault = NSMakePoint(0.0, 0.0)
12    _itemColorDefault = NSColor.redColor()
13    _backgroundColorDefault = NSColor.whiteColor()
14
15    def awakeFromNib(self):
16        self.dragging = None
17
18    def initWithFrame_(self, frame):
19        """."""
20        result = super(DraggableItemView, self).initWithFrame_(frame)
21        if result is not None:
22            result._location = self._locationDefault
23            result._itemColor = self._itemColorDefault
24            result._backgroundColor = self._backgroundColorDefault
25        return result
26
27    def drawRect_(self, rect):
28        """."""
29        NSColor.whiteColor().set()
30        NSBezierPath.fillRect_(rect)
31        self.itemColor().set()
32        NSBezierPath.fillRect_(self.calculatedItemBounds())
33
34    def isOpaque(self):
35        """."""
36        return (self.backgroundColor().alphaComponent() >= 1.0)
37
38    def offsetLocationByX_andY_(self, x, y):
39        """."""
40        self.setNeedsDisplayInRect_(self.calculatedItemBounds())
41        if self.isFlipped():
42            invertDeltaY = -1
43        else:
44            invertDeltaY = 1
45        self.location().x = self.location().x + x
46        self.location().y = self.location().y + y * invertDeltaY
47        self.setNeedsDisplayInRect_(self.calculatedItemBounds())
48
49    def mouseDown_(self, event):
50        """."""
51        clickLocation = self.convertPoint_fromView_(event.locationInWindow(),
52                                                    None)
53        itemHit = self.isPointInItem_(clickLocation)
54        if itemHit:
55            self.dragging = True
56            self.lastDragLocation = clickLocation
57            NSCursor.closedHandCursor().push()
58
59    def mouseDragged_(self, event):
60        """."""
61        if self.dragging:
62            newDragLocation = self.convertPoint_fromView_(
63                event.locationInWindow(),
64                None
65            )
66            self.offsetLocationByX_andY_(
67                newDragLocation.x - self.lastDragLocation.x,
68                newDragLocation.y - self.lastDragLocation.y
69            )
70            self.lastDragLocation = newDragLocation
71            self.autoscroll_(event)
72
73    def mouseUp_(self, event):
74        """."""
75        self.dragging = False
76        # NSCursor has both an instance and a class method w/ the name 'pop'
77        NSCursor.pyobjc_classMethods.pop()
78        self.window().invalidateCursorRectsForView_(self)
79
80    def acceptsFirstResponder(self):
81        """."""
82        return True
83
84    def keyDown_(self, event):
85        """."""
86        handled = False
87        characters = event.charactersIgnoringModifiers()
88        if characters.isEqual_('r'):
89            handled = True
90            self.setItemPropertiesToDefault_(self)
91        if handled is False:
92            super(DraggableItemView, self).keyDown_(event)
93
94    @objc.IBAction
95    def changeColor_(self, sender):
96        """."""
97        self.setItemColor_(sender.color())
98
99    def resetCursorRects(self):
100        """."""
101        self.discardCursorRects()
102        self.addCursorRect_cursor_(self.calculatedItemBounds(),
103                                   NSCursor.openHandCursor())
104
105    @objc.IBAction
106    def moveUp_(self, sender):
107        """."""
108        self.offsetLocationByX_andY_(0.0, 10.0)
109        self.window().invalidateCursorRectsForView_(self)
110
111    @objc.IBAction
112    def moveDown_(self, sender):
113        """."""
114        self.offsetLocationByX_andY_(0.0, -10.0)
115        self.window().invalidateCursorRectsForView_(self)
116
117    @objc.IBAction
118    def moveLeft_(self, sender):
119        """."""
120        self.offsetLocationByX_andY_(-10.0, 0.0)
121        self.window().invalidateCursorRectsForView_(self)
122
123    @objc.IBAction
124    def moveRight_(self, sender):
125        """."""
126        self.offsetLocationByX_andY_(10.0, 0.0)
127        self.window().invalidateCursorRectsForView_(self)
128
129    @objc.IBAction
130    def setItemPropertiesToDefault_(self, sender):
131        """."""
132        self.setLocation_(self._locationDefault)
133        self.setItemColor_(self._itemColorDefault)
134        self.setBackgroundColor_(self._backgroundColorDefault)
135
136    def setLocation_(self, point):
137        """."""
138        if not NSEqualPoints(point, self.location()):
139            self.setNeedsDisplayInRect_(self.calculatedItemBounds())
140            self._location = point
141            self.setNeedsDisplayInRect_(self.calculatedItemBounds())
142            self.window().invalidateCursorRectsForView_(self)
143
144    def location(self):
145        """."""
146        return self._location
147
148    def setBackgroundColor_(self, aColor):
149        """."""
150        if not self.backgroundColor().isEqual_(aColor):
151            self._backgroundColor = aColor
152            self.setNeedsDisplayInRect_(self.calculatedItemBounds())
153
154    def backgroundColor(self):
155        """."""
156        return self._backgroundColor
157
158    def setItemColor_(self, aColor):
159        """."""
160        if not self.itemColor().isEqual_(aColor):
161            self._itemColor = aColor
162            self.setNeedsDisplayInRect_(self.calculatedItemBounds())
163
164    def itemColor(self):
165        """."""
166        return self._itemColor
167
168    def calculatedItemBounds(self):
169        """."""
170        return NSMakeRect(self.location().x, self.location().y,
171                          60.0, 20.0)
172
173    def isPointInItem_(self, testPoint):
174        """."""
175        itemHit = NSPointInRect(testPoint, self.calculatedItemBounds())
176        if itemHit:
177            pass
178        return itemHit
179
180if __name__ == "__main__":
181    AppHelper.runEventLoop()
182