This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
IAImageViewInteractive.py
147 lines (116 loc) · 5.19 KB
/
IAImageViewInteractive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#
# IAImageViewInteractive.py
from objc import *
from Foundation import *
from AppKit import *
from IAImageView import IAImageView
from math import floor
class IAImageViewInteractive(IAImageView):
def initWithFrame_(self, frame):
self = super(IAImageViewInteractive, self).initWithFrame_(frame)
if self:
types = [NSFilenamesPboardType]
types.extend(NSImage.imagePasteboardTypes())
self.registerForDraggedTypes_(types)
pass
return self
controller = objc.IBOutlet()
mouseIsDown = False
dragBackground = False
dragStart = (0,0)
def draggingEntered_(self,sender):
if self.controller.canSetDocumentImageFromPasteboard_(sender.draggingPasteboard()):
self.imageFade = 0.15
self.setNeedsDisplay_(YES)
return NSDragOperationCopy | NSDragOperationGeneric | NSDragOperationMove
def draggingExited_(self,sender):
self.imageFade = 1.0
self.setNeedsDisplay_(YES)
def prepareForDragOperation_(self,sender):
self.imageFade = 1.0
self.setNeedsDisplay_(YES)
return self.controller.canSetDocumentImageFromPasteboard_(sender.draggingPasteboard())
def performDragOperation_(self,sender):
return self.controller.setDocumentImageFromPasteboard_(sender.draggingPasteboard())
def resetCursorRects(self):
if self.image() is not None or self.backgroundRenderer is not None:
curs = NSCursor.closedHandCursor() if self.mouseIsDown else NSCursor.openHandCursor()
self.addCursorRect_cursor_(self.visibleRect(), curs)
curs.setOnMouseEntered_(YES)
def scrollWheel_(self,event):
if event.deltaY() > 0:
self.zoomIn_(None)
elif event.deltaY() < 0:
self.zoomOut_(None)
def mouseExited_(self,event):
self.imageFade=1.0
self.setNeedsDisplay_(YES)
def mouseEntered_(self,event):
self.imageFade = 0.5
self.setNeedsDisplay_(YES)
def pointIsInImage_(self,point):
if self.image() is None: return NO
size = self.image().size();
fsize = self.frame().size;
w = max(50,size.width * self.zoom() +15) / 2 # add "border" around image to ease dragging of small ones
h = max(50,size.height * self.zoom() +15) / 2
return point.x >= self.imageOffset[0]+fsize.width/2-w and point.y >= self.imageOffset[1]+fsize.height/2-h and \
point.x <= self.imageOffset[0]+fsize.width/2+w and point.y <= self.imageOffset[1]+fsize.height/2+h
def mouseDragged_(self,event):
point = self.convertPoint_fromView_(event.locationInWindow(), None);
delta = (point.x - self.dragStart[0], point.y - self.dragStart[1])
self.dragStart = (point.x, point.y)
if self.backgroundRenderer is not None and self.dragBackground:
self.backgroundRenderer.moveBy_(delta)
elif self.image() is not None:
size = self.frame().size
self.imageOffset = (self.imageOffset[0] + delta[0],
self.imageOffset[1] + delta[1])
self._limitImageOffset()
self.setNeedsDisplay_(YES)
def mouseChange_(self, isDown):
self.mouseIsDown = isDown
self.window().invalidateCursorRectsForView_(self);
def mouseDown_(self,event):
point = self.convertPoint_fromView_(event.locationInWindow(), None)
if self.backgroundRenderer is None or not self.backgroundRenderer.canMove():
self.dragBackground = False
else:
self.dragBackground = not self.pointIsInImage_(point)
if (event.modifierFlags() & (NSShiftKeyMask | NSAlternateKeyMask | NSCommandKeyMask)):
self.dragBackground = not self.dragBackground
self.dragStart = (point.x,point.y)
if event.clickCount()&3==2:
self.imageOffset = (0,0)
if self.zoomingToFill:
self.setZoom_(1)
else:
self.zoomToFill()
self.setNeedsDisplay_(YES)
else:
self.mouseChange_(YES);
self.mouseDragged_(event)
def magnifyWithEvent_(self, event):
#NSLog("magnified by %f z = %f" % (event.magnification(), self.zoom()));
oldzoom = self.zoom();
# zoom < 1 requires different zooming speed than > 1
if (oldzoom + event.magnification() > 1):
zoom = ((oldzoom / 20) + event.magnification()/4) * 20;
else:
zoom = 1 / (1/oldzoom - event.magnification());
# avoid crossing of the 1.0 boundary at wrong speed
if (zoom > 1.0 and oldzoom < 1.0) or (zoom < 1.0 and oldzoom > 1.0):
zoom = 1.0;
self.setZoom_(max(0.25,zoom));
#def keyDown_(self,event):
#NSLog("key! %s" % event);
def mouseUp_(self,event):
self.mouseChange_(NO);
def updateTouches_(self,event):
touches = event.touchesMatchingPhase_inView_( NSTouchPhaseStationary, self);
#NSLog("touches %s" % touches.allObjects());
self.setDrawAlternateImage_((touches.count() >= 3))
def otherMouseDown_(self,event):
self.setDrawAlternateImage_(YES)
def otherMouseUp_(self,event):
self.setDrawAlternateImage_(NO)