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
/
Copy pathIACollectionItem.py
164 lines (132 loc) · 5.6 KB
/
IACollectionItem.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
#
# IACollectionItem.py
from Foundation import *
from objc import *
from AppKit import *
from random import randint
from IAImageView import IAImageView
from IAImageViewInteractive import IAImageViewInteractive
from IABackgroundRenderer import *
class IACollectionViewItem(NSCollectionViewItem):
def setSelected_(self, sel):
NSCollectionViewItem.setSelected_(self,sel)
col = self.collectionView()
if sel and col is not None:
col.updateSelection()
def imageChangedNotification_(self,sender):
view = self.view()
if view is not None:
subview = view.viewWithTag_(1234);
if subview is not None:
subview.setImage_(sender.object().image);
class IACollectionImageView(IAImageView):
collectionItem = None;
drawBorder = False
def initWithFrame_(self, frame):
NSLog("col img view init");
self = super(IACollectionImageView, self).initWithFrame_(frame)
assert self.layer() is not None;
if self:
self.registerTypes()
return self
def registerTypes(self):
# initialization code here
types = [NSFilenamesPboardType]
types.append(NSImage.imagePasteboardTypes())
self.registerForDraggedTypes_(types);
def draggingEntered_(self,sender):
if NSImage.canInitWithPasteboard_(sender.draggingPasteboard()):
self.imageFade = 0.2
image = NSImage.alloc().initWithPasteboard_(sender.draggingPasteboard())
if image is not None:
self.setBackgroundRenderer_(IAImageBackgroundRenderer.alloc().initWithImage_(image))
self.setNeedsDisplay_(YES)
return NSDragOperationCopy | NSDragOperationGeneric | NSDragOperationMove
def draggingExited_(self,sender):
self.imageFade = 1.0
self.setBackgroundRenderer_(self.collectionItem.representedObject())
self.setNeedsDisplay_(YES)
def prepareForDragOperation_(self,sender):
self.imageFade = 1.0
self.setNeedsDisplay_(YES)
if NSImage.canInitWithPasteboard_(sender.draggingPasteboard()):
return YES
else:
self.draggingExited_(sender)
def performDragOperation_(self,sender):
image = NSImage.alloc().initWithPasteboard_(sender.draggingPasteboard())
if image is not None:
self.collectionItem.setSelected_(YES)
self.collectionItem.collectionView().setBackgroundImage_(image);
self.setNeedsDisplay_(YES)
return YES
else:
self.draggingExited_(sender)
def tag(self):
return 1234; # magic for interface builder
def mouseEntered_(self,event):
self.imageFade = 0.75
self.drawBorder = True
def mouseExited_(self,event):
self.imageFade = 1
pass
def mouseUp_(self,event):
self.drawBorder = False
self.setNeedsDisplay_(YES)
if self.collectionItem is not None:
self.collectionItem.setSelected_(YES)
def mouseDown_(self,event):
self.drawBorder = True
self.setNeedsDisplay_(YES)
class IACollectionView(NSCollectionView):
imageView = objc.IBOutlet()
image = None
#def frameChanged_(self,bla):
# self.setMaxItemSize_((300,300))
#for i in self.content():
# i.view().setNeedsDisplay_(YES)
# pass
def awakeFromNib(self):
self.setAllowsMultipleSelection_(NO);
self.sendNotification_(u"SelectionChanged");
#self.setPostsFrameChangedNotifications_(YES)
# NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(self,self.frameChanged_,NSViewFrameDidChangeNotification,self);
def sendNotification_(self,name):
n = NSNotification.notificationWithName_object_(name,self)
NSNotificationQueue.defaultQueue().enqueueNotification_postingStyle_coalesceMask_forModes_(n,NSPostWhenIdle,NSNotificationCoalescingOnName,None)
def selectionChangedNotification_(self,sender):
self.updateSelection();
def updateSelection(self):
idx = self.selectionIndexes().firstIndex()
if idx != NSNotFound:
self.imageView.setBackgroundRenderer_(self.content()[idx]);
def setBackgroundImage_(self,img):
bgr = IAImageBackgroundRenderer.alloc().initWithImage_(img);
content = NSMutableArray.arrayWithArray_(self.content());
idx = self.selectionIndexes().firstIndex()
if idx != NSNotFound:
content[idx] = bgr
self.setContent_(content);
self.sendNotification_(u"SelectionChanged");
def setImage_(self,img):
self.image = img
if img is not None:
size = img.size()
self.setMaxItemSize_((max(100,size.width*2),max(100,size.height*2)))
self.setMinItemSize_((40,40))
self.sendNotification_(u"ImageChanged");
pass
def newItemForRepresentedObject_(self,obj):
colitem = super(IACollectionView,self).newItemForRepresentedObject_(obj);
view = colitem.view().viewWithTag_(1234);
#view = IACollectionImageView.alloc().initWithFrame_(((0,0),image.size()))
view.setImage_(self.image)
view.setBackgroundRenderer_(obj)
view.zoomToFill(0.8);
#colitem = IACollectionViewItem.alloc().init()
#colitem.setRepresentedObject_(obj)
#colitem.setView_(view)
view.collectionItem = colitem
# FIXME: remove from notification queue when deallocing!
NSNotificationCenter.defaultCenter().addObserver_selector_name_object_(colitem,colitem.imageChangedNotification_, u"ImageChanged", self)
return colitem;