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
/
IAImageView.py
249 lines (196 loc) · 8.17 KB
/
IAImageView.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#
# IAImageView.py
from objc import *
from Foundation import *
from AppKit import *
from Quartz.CoreGraphics import *
from Quartz.QuartzCore import *
from math import ceil, floor
class IAImageView(NSView):
_zoom = 2.0
_lastZoom = -1
_image = None
_inWantsLayers = False
_alternateImage = None
_drawAlternateImage = NO
backgroundRenderer = None
_smooth = YES
backgroundOffset = (0,0)
imageOffset = (0,0)
imageFade = 1.0
zoomingToFill = 0
_imageLayer = None
_backgroundLayer = None
def initWithFrame_(self, frame):
self = super(IAImageView, self).initWithFrame_(frame)
if self:
self.myAddLayers()
return self
# existence of initWithCoder_ causes later crashes in a place
# with backtrace I don't recognize, so lazy myAddLayers()
# is scattered all over the place instead
def myAddLayers(self):
if not self._inWantsLayers:
self._inWantsLayers = True;
self.setWantsLayer_(YES);
self._inWantsLayers = False;
if self.layer() is None: self.setLayer_(CALayer.layer());
assert self.layer() is not None;
self._imageLayer = CALayer.layer()
self._backgroundLayer = CALayer.layer();
self._backgroundLayer.setFrame_(self.bounds())
self._backgroundLayer.setAutoresizingMask_(kCALayerWidthSizable | kCALayerHeightSizable);
self._backgroundLayer.setBackgroundColor_(CGColorCreateGenericRGB(0.5,0.5,0.5,1));
self.layer().addSublayer_(self._backgroundLayer);
self.layer().addSublayer_(self._imageLayer);
self.addShadow();
def addShadow(self):
shadowHeight = 10;
shadowWidth = 12;
bounds = self.layer().bounds();
NSLog("bounds %d %d %d %d",bounds.size.width, bounds.size.height, bounds.origin.x, bounds.origin.y);
shadow1 = CAGradientLayer.layer();
stops = [CGColorCreateGenericRGB(0,0,0,x) for x in [0, 0.04, 0.11, 0.3]]
shadow1.setColors_(stops);
shadow1.setAutoresizingMask_(kCALayerWidthSizable | kCALayerMinYMargin);
shadow1.setFrame_(((0,bounds.size.height-shadowHeight), (bounds.size.width, shadowHeight)));
shadow2 = CAGradientLayer.layer();
stops.reverse();
shadow2.setColors_(stops);
shadow2.setStartPoint_((0,0));
shadow2.setEndPoint_((1,0));
shadow2.setAutoresizingMask_(kCALayerHeightSizable | kCALayerMaxXMargin);
shadow2.setFrame_(((0,0),(shadowWidth,bounds.size.height)));
self.layer().addSublayer_(shadow2);
self.layer().addSublayer_(shadow1);
self.shadow1 = shadow1;
def setFrame_(self,rect):
NSView.setFrame_(self,rect)
if self.zoomingToFill: self.zoomToFill(self.zoomingToFill)
@objc.IBAction
def zoomIn_(self, sender):
self.setZoom_(self.zoom() * 2.0);
@objc.IBAction
def zoomOut_(self,sender):
self.setZoom_(self.zoom() / 2.0);
def zoomToFill(self, zoom=1.0):
self.zoomingToFill = zoom
if self.image() is None: return
size = self.image().size()
framesize = self.frame().size
zoom = min(framesize.width/size.width, framesize.height/size.height)*self.zoomingToFill
if zoom > 1.0:
zoom = min(4.0,floor(zoom))
self._setZoom(zoom)
def _limitImageOffset(self):
if self.image() is None: return
size = self.frame().size
imgsize = self.image().size()
w = (size.width + imgsize.width * self.zoom()) /2
h = (size.height + imgsize.height * self.zoom()) /2
self.imageOffset = (max(-w+15, min(w-15, self.imageOffset[0])), \
max(-h+15, min(h-15, self.imageOffset[1])))
def smooth(self):
return self._smooth;
def setSmooth_(self,smooth):
self._smooth = smooth
if self._imageLayer:
self._imageLayer.setMagnificationFilter_(kCAFilterLinear if smooth else kCAFilterNearest)
self._imageLayer.setMinificationFilter_(kCAFilterLinear if smooth else kCAFilterNearest)
self.setNeedsDisplay_(YES)
def zoom(self):
return self._zoom;
def setZoom_(self,zoom):
self.zoomingToFill = 0
self._setZoom(zoom);
def _setZoom(self,zoom):
self._zoom = min(16.0,max(1.0/128.0,zoom))
self._limitImageOffset()
if self._imageLayer:
if 1.0==zoom:
self._imageLayer.setMagnificationFilter_(kCAFilterNearest)
elif self._smooth:
self.setSmooth_(YES)
self.setNeedsDisplay_(YES)
def image(self):
return self._image;
def setImage_(self,aImage):
self._image=aImage
if self._alternateImage and aImage:
self._setScale_ofImage_(self._getScaleOfImage_(self._alternateImage), aImage);
if self.zoomingToFill:
self.zoomToFill(self.zoomingToFill)
self.setDrawAlternateImage_(NO)
def _getScaleOfImage_(self, image):
w,h = image.size()
rep = image.representations();
if not rep or not rep.count(): return (0,0);
return (rep[0].pixelsWide()/w, rep[0].pixelsHigh()/h);
def _setScale_ofImage_(self, scale, image):
rep = image.representations();
if not rep or not rep.count(): return (0,0);
image.setSize_((rep[0].pixelsWide()/scale[0], rep[0].pixelsHigh()/scale[1]));
def alternateImage(self):
return self._alternateImage;
def setAlternateImage_(self,aImage):
if self._image and aImage:
self._setScale_ofImage_(self._getScaleOfImage_(aImage), self._image);
self._alternateImage = aImage
self.imageOffset = (0,0)
if not self.zoomingToFill:
self.setZoom_(1.0)
self.setNeedsDisplay_(YES)
def drawAlternateImage(self):
return self._drawAlternateImage;
def setDrawAlternateImage_(self,tf):
self._drawAlternateImage = tf
if self._imageLayer:
image = self.image() if not tf else self.alternateImage()
CATransaction.begin()
CATransaction.setDisableActions_(True)
self._imageLayer.setContents_(image);
self._updateLayerZoom()
CATransaction.commit()
self.setNeedsDisplay_(YES)
def drawAlternateImage(self):
return self._drawAlternateImage == True
def setBackgroundLayer_(self, layer):
assert layer
if self.layer() is None:
self.myAddLayers()
layer.setFrame_(self.bounds());
layer.setAutoresizingMask_(kCALayerWidthSizable|kCALayerHeightSizable)
self.layer().replaceSublayer_with_(self._backgroundLayer, layer);
self._backgroundLayer = layer;
def setBackgroundRenderer_(self,renderer):
assert renderer is not None;
self.backgroundRenderer = renderer;
if self.layer():
self.setBackgroundLayer_(renderer.getLayer())
self.setNeedsDisplay_(YES)
def isOpaque(self):
return self.backgroundRenderer is not None
def _updateLayerZoom(self):
if self._lastZoom != self._zoom:
image = self.image() if not self.drawAlternateImage() else self.alternateImage();
if image is not None and self._imageLayer is not None:
w,h = image.size()
x,y = self.imageOffset
size = self.bounds()[1]
self._imageLayer.setFrame_(((x+size[0]/2-w/2,y+size[1]/2-h/2),(w*self._zoom, h*self._zoom)));
self._lastZoom = self._zoom
def setNeedsDisplay_(self, tf):
if tf:
if self.layer() is None:
self.myAddLayers()
image = self.image() if not self.drawAlternateImage() else self.alternateImage();
if image is not None and self._imageLayer is not None:
self._updateLayerZoom()
self._imageLayer.setOpacity_(self.imageFade);
x,y = self.imageOffset
size = self.bounds()[1]
CATransaction.begin()
CATransaction.setDisableActions_(True)
self._imageLayer.setPosition_((x+size[0]/2,y+size[1]/2))
CATransaction.commit()
super(IAImageView, self).setNeedsDisplay_(tf);