forked from nst/CocoaSlideShow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagesController.m
191 lines (142 loc) · 5.36 KB
/
ImagesController.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
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
#import "ImagesController.h"
#import "CSSImageInfo.h"
#import "NSFileManager+CSS.h"
#import "AppDelegate.h"
#import "NSImage+CSS.h"
static NSString *const kApplyNaturalSortOrder = @"ApplyNaturalSortOrder";
static NSComparisonResult naturalCompare( CSSImageInfo *img1, CSSImageInfo *img2, void *context ) {
NSString *str1 = [img1 fileName];
NSString *str2 = [img2 fileName];
return [str1 compare:str2 options:NSNumericSearch];
}
@implementation ImagesController
- (void)awakeFromNib {
allowedExtensions = [NSArray arrayWithObjects:@"jpg", @"jpeg", @"jpe", @"tif", @"tiff", @"gif", @"png", @"pct", @"pict", @"pic",
//@"pdf", @"eps", @"epi", @"epsf", @"epsi", @"ps",
@"ico", @"icns", @"bmp", @"bmpf",
@"dng", @"cr2", @"crw", @"fpx", @"fpix", @"raf", @"dcr", @"ptng", @"pnt", @"mac", @"mrw", @"nef",
@"orf", @"exr", @"psd", @"qti", @"qtif", @"hdr", @"sgi", @"srf", @"targa", @"tga", @"cur", @"xbm", nil];
}
- (NSUndoManager *)undoManager {
return [cocoaSlideShow undoManager];
}
- (NSIndexSet *)flaggedIndexes {
NSMutableIndexSet *flaggedIndexes = [[NSMutableIndexSet alloc] init];
for( CSSImageInfo *imageInfo in [self arrangedObjects] ) {
if([imageInfo isFlagged]) {
[flaggedIndexes addIndex:[[self arrangedObjects] indexOfObject:imageInfo]];
}
}
return flaggedIndexes;
}
- (void)flagIndexes:(NSIndexSet *)indexSet {
[[[self arrangedObjects] objectsAtIndexes:indexSet] makeObjectsPerformSelector:@selector(flag)];
}
- (IBAction)flag:(id)sender {
[[[self undoManager] prepareWithInvocationTarget:self] unflag:self];
[[self selectedObjects] makeObjectsPerformSelector:@selector(flag)];
}
- (IBAction)unflag:(id)sender {
[[[self undoManager] prepareWithInvocationTarget:self] flag:self];
[[self selectedObjects] makeObjectsPerformSelector:@selector(unflag)];
}
- (IBAction)toggleFlags:(id)sender {
[[[self undoManager] prepareWithInvocationTarget:self] toggleFlags:self];
[[self selectedObjects] makeObjectsPerformSelector:@selector(toggleFlag)];
}
- (IBAction)selectFlags:(id)sender {
[(NSArrayController *)[[self undoManager] prepareWithInvocationTarget:self] setSelectionIndexes:[self selectionIndexes]];
[self setSelectionIndexes:[self flaggedIndexes]];
}
- (IBAction)removeAllFlags:(id)sender {
[[[self undoManager] prepareWithInvocationTarget:self] flagIndexes:[self flaggedIndexes]];
[[self arrangedObjects] makeObjectsPerformSelector:@selector(removeFlag)];
}
- (IBAction)remove:(id)sender {
[[[self undoManager] prepareWithInvocationTarget:self] performSelector:@selector(addFiles:) withObject:[self valueForKeyPath:@"selectedObjects.path"]];
[super remove:sender];
}
- (void)selectPreviousImage {
if([self canSelectPrevious]) {
[[[self undoManager] prepareWithInvocationTarget:self] selectNext:self];
[self selectPrevious:self];
}
}
- (void)selectNextImage {
if([self canSelectNext]) {
[[[self undoManager] prepareWithInvocationTarget:self] selectPrevious:self];
[self selectNext:self];
}
}
- (void)selectNextImageOrFirstOne {
if([self canSelectNext]) {
[[[self undoManager] prepareWithInvocationTarget:self] selectPrevious:self];
[self selectNext:self];
} else {
if([[self arrangedObjects] count] == 0) return;
[self setSelectionIndex:0];
}
}
- (BOOL)multipleImagesSelected {
return [[self selectedObjects] count] > 1;
}
- (BOOL)containsPath:(NSString *)path {
NSArray *paths = [self valueForKeyPath:@"arrangedObjects.path"];
return [paths containsObject:path];
}
- (BOOL)extensionIsAllowed:(NSString *)path {
return [allowedExtensions containsObject:[[path pathExtension] lowercaseString]];
}
- (void)addFiles:(NSArray *)filePaths {
NSMutableArray *imagesInfoToAdd = [NSMutableArray array];
for(NSString *path in filePaths) {
if([[NSFileManager defaultManager] isDirectory:path]) {
NSArray *dirContent = [[NSFileManager defaultManager] directoryContentFullPaths:path recursive:YES];
[self addFiles:dirContent];
continue;
}
if([path hasPrefix:@"."] || ![self extensionIsAllowed:path]) {
continue;
}
[imagesInfoToAdd addObject:[CSSImageInfo containerWithPath:path]];
}
if([[NSUserDefaults standardUserDefaults] boolForKey:kApplyNaturalSortOrder]) {
[imagesInfoToAdd sortUsingFunction:&naturalCompare context:nil];
}
[self addObjects:imagesInfoToAdd];
}
- (void)addDirFiles:(NSString *)dir {
//NSLog(@"-- addDirFiles: %@", dir);
[self addFiles:[NSArray arrayWithObject:dir]];
ImagesController *ic = [[self undoManager] prepareWithInvocationTarget:self];
[ic remove:self];
}
- (IBAction)moveToTrash:(id)sender {
[[self selectedObjects] makeObjectsPerformSelector:@selector(moveToTrash)];
[self removeObjectsAtArrangedObjectIndexes:[self selectionIndexes]];
}
/*
- (NSArray *)flagged {
NSPredicate *p = [NSPredicate predicateWithFormat:@"flagged == YES"];
NSMutableArray *ma = [[self arrangedObjects] mutableCopy];
[ma filterUsingPredicate:p];
return [ma autorelease];
}
*/
#pragma mark GPS
- (BOOL)atLeastOneImageWithGPSSelected {
NSEnumerator *e = [[self selectedObjects] objectEnumerator];
CSSImageInfo *container = nil;
while((container = [e nextObject])) {
if([container gps] != nil) return YES;
}
return NO;
}
- (IBAction)openMap:(id)sender {
if(![[self selectedObjects] count]) return;
CSSImageInfo *i = [[self selectedObjects] lastObject];
NSURL *url = [i googleMapsURL];
if(!url) return;
[[NSWorkspace sharedWorkspace] openURL:url];
}
@end