-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathQSIconLoader.m
107 lines (90 loc) · 2.71 KB
/
QSIconLoader.m
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
#import "QSIconLoader.h"
#import <QSCore/QSObject.h>
@implementation QSIconLoader
+ (id)loaderWithArray:(NSArray *)newArray {
return [[[self alloc] initWithArray:newArray] autorelease];
}
- (id)initWithArray:(NSArray *)newArray {
if (self = [super init]) {
array = [newArray retain];
loaderValid = YES;
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[delegate release];
[array release];
[loadedIndexes release];
[super dealloc];
}
- (void)invalidate {
loaderValid = NO;
}
- (void)loadIcons {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[self retain] autorelease];
loadThread = [NSThread currentThread];
// [NSThread setThreadPriority:0.0];
NSArray *sourceArray = nil;
BOOL rangeValid = NO;
int i, j, m;
id <NSObject, QSObject> thisObject;
while (!(rangeValid) && loaderValid) {
loadRange = newRange;
rangeValid = YES;
for (i = 0; i <= loadRange.length && loaderValid && rangeValid; i++) {
m = loadRange.location;
j = i;
if (modulation) {
j = loadRange.length/2-j/2+j*(j%2); //Center Modulation
j = loadRange.length/2-j/2+j*(j%2); //Center Modulation
j = loadRange.length/2-j/2+j*(j%2); //Center Modulation
}
m += j;
if (m<0 || m >= [array count]) continue;
thisObject = [array objectAtIndex:m];
if (![thisObject isKindOfClass:[NSNull class]] && ![thisObject iconLoaded]) {
[thisObject loadIcon];
[delegate iconLoader:self loadedIndex:m inArray:sourceArray];
}
rangeValid = NSEqualRanges(loadRange, newRange);
}
}
loadThread = nil;
[pool release];
}
- (void)loadIconsInRange:(NSRange)range {
if (!NSEqualRanges(range, newRange) ) {
newRange = range;
//NSLog(@"%d, %d", range, NSMaxRange(range) );
if (!loadThread) [NSThread detachNewThreadSelector:@selector(loadIcons) toTarget:self withObject:nil];
}
}
- (NSObject *)delegate { return delegate; }
- (void)setDelegate:(NSObject *)aDelegate {
if (delegate != aDelegate) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
if (delegate)
[nc removeObserver:self];
[delegate release];
delegate = [aDelegate retain];
if (aDelegate)
[nc addObserver:self selector:@selector(cancelLoading:) name:QSIconLoaderDelegateCanceled object:aDelegate];
}
}
+ (void)invalidateLoaderForDelegate:(id)delegate {
//NSLog(@"invalidate %@", delegate);
[[NSNotificationCenter defaultCenter] postNotificationName:QSIconLoaderDelegateCanceled object:delegate];
}
- (BOOL)cancelLoading:(NSNotification *)notif {
//NSLog(@"invalidate");
[self invalidate];
return YES;
}
- (BOOL)isLoading { return loadThread != nil; }
- (int) modulation { return modulation; }
- (void)setModulation:(int)newModulation {
modulation = newModulation;
}
@end