-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProjectController.m
290 lines (238 loc) · 8.39 KB
/
ProjectController.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// ProjectController.m
// Geotagalog
//
// Created by Nathan Vander Wilt on 1/23/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "ProjectController.h"
#import "TLMapView.h"
#import "TLSolidMapLayer.h"
#import "TLGraticuleMapLayer.h"
#import "TLTrackLayer.h"
#import "TLPhotoLayer.h"
#import "TLMapBevel.h"
#import "TLTrack.h"
#import "TLWaypoint.h"
#import "TLLocation.h"
#include "TLGeometry.h"
#import "TLCocoaToolbag.h"
#import "TimeOffsetController.h"
#import "PhotoSourceController.h"
#import "MetadataController.h"
#import "TaskExport.h"
extern NSArray* TLNSArrayShuffle(NSArray* array);
static TLProjectionRef TLProjectionCreateStereographicWithCenter(TLCoordinateDegrees lat,
TLCoordinateDegrees lon);
@interface ProjectController ()
- (void)fitMapViewToTracks;
@end
@implementation ProjectController
@synthesize window;
@synthesize tracks;
@synthesize waypoints;
@synthesize sourceController;
- (void)addTracks:(NSSet*)newTracks {
[tracks unionSet:newTracks];
[self fitMapViewToTracks];
}
- (void)removeTracks:(NSSet*)oldTracks {
// we never remove tracks, this just enables automatic KVO
(void)oldTracks;
[self doesNotRecognizeSelector:_cmd];
}
- (void)addWaypoints:(NSSet*)newWaypoints {
[waypoints unionSet:newWaypoints];
}
- (void)removeWaypoints:(NSSet*)oldWaypoints {
// we never remove tracks, this just enables automatic KVO
(void)oldWaypoints;
[self doesNotRecognizeSelector:_cmd];
}
- (BOOL)isExporting {
return !!exportController;
}
- (id)initWithTracks:(NSArray*)theTracks {
self = [super init];
if (self) {
tracks = [[NSMutableSet setWithArray:theTracks] retain];
waypoints = [NSMutableSet new];
[NSBundle loadNibNamed:@"Project" owner:self];
}
return self;
}
- (void)awakeFromNib {
[window setDelegate:self];
[self fitMapViewToTracks];
TLSolidMapLayer* landcover = [[TLSolidMapLayer new] autorelease];
[mapView addLayer:landcover];
TLGraticuleMapLayer* gridLines = [[TLGraticuleMapLayer new] autorelease];
[mapView addLayer:gridLines];
[mapView addLayer:[metadataController locationLayer]];
[mapView addLayer:[metadataController itemLayer]];
TLMapBevel* bevelLayer = [[TLMapBevel new] autorelease];
[mapView addLayer:bevelLayer];
[metadataController bind:@"itemSource"
toObject:sourceController
withKeyPath:@"source"
options:nil];
// TODO: ensure binding both ways is kosher, then make tl_doubleBind category
[metadataController bind:@"cameraError"
toObject:offsetController
withKeyPath:@"cameraError"
options:nil];
[offsetController bind:@"cameraError"
toObject:metadataController
withKeyPath:@"cameraError"
options:nil];
[metadataController bind:@"cameraTimeZone"
toObject:offsetController
withKeyPath:@"cameraTimeZone"
options:nil];
}
- (void)dealloc {
[metadataController unbind:@"itemSource"];
[metadataController unbind:@"cameraError"];
[offsetController unbind:@"cameraError"];
[metadataController unbind:@"cameraTimeZone"];
[window setDelegate:nil];
[window release];
[sourceController release];
[offsetController release];
[metadataController release];
[tracks release];
[waypoints release];
NSAssert(!exportController, @"Project controller deallocated with outstanding export");
[super dealloc];
}
#pragma mark Data source
- (void)fitMapViewToTracks {
if (![[self tracks] count]) return;
// TODO: find spherical centroid of convex hull around all track points
// find bounds in ECE for now
CGRect trackBounds = CGRectNull;
for (TLTrack* track in [self tracks]) {
for (TLWaypoint* waypoint in [track waypoints]) {
TLCoordinate coord = [[waypoint location] coordinate];
CGPoint ecePoint = CGPointMake((CGFloat)coord.lon, (CGFloat)coord.lat);
trackBounds = TLCGRectExpandToIncludePoint(trackBounds, ecePoint);
}
}
CGPoint eceCenter = TLCGRectGetCenter(trackBounds);
TLProjectionRef proj = TLProjectionCreateStereographicWithCenter(eceCenter.y, eceCenter.x);
if (!proj) {
NSLog(@"Could not zoom in around track (Projection center: %f, %f)", eceCenter.y, eceCenter.x);
return;
}
CGRect mapBounds = CGRectNull;
for (TLTrack* track in [self tracks]) {
for (TLWaypoint* waypoint in [track waypoints]) {
TLCoordinate coord = [[waypoint location] coordinate];
TLProjectionError err = TLProjectionErrorNone;
CGPoint point = TLProjectionProjectCoordinate(proj, coord, &err);
if (err) {
NSLog(@"Error projecting track point.");
continue;
}
mapBounds = TLCGRectExpandToIncludePoint(mapBounds, point);
}
}
if (CGRectGetWidth(mapBounds) < 5.0f) {
mapBounds.size.width = 5.0f;
}
if (CGRectGetHeight(mapBounds) < 5.0f) {
mapBounds.size.height = 5.0f;
}
CGFloat padRatio = 0.1f;
mapBounds = CGRectInset(mapBounds,
-padRatio * CGRectGetWidth(mapBounds),
-padRatio * CGRectGetHeight(mapBounds));
[mapView setDesiredBounds:mapBounds];
[mapView setProjection:proj];
TLProjectionRelease(proj);
}
- (void)windowWillClose:(NSNotification*)theNotification {
NSWindow* theWindow = [theNotification object];
if (theWindow != window) return;
[sourceController setSource:nil];
[metadataController setLocationSource:nil];
}
#pragma mark Export controller handling
- (void)exportDidFinish:(ExportController*)theExportController {
if ([[theExportController warnings] count]) {
NSLog(@"%@", [theExportController warnings]);
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"Some photos were not geotagged. "
@"This may happen with movie files, "
@"files are in an unsupported RAW format, "
@"or files in iPhoto's trash.",
NSLocalizedDescriptionKey, nil];
[NSApp presentError:[NSError errorWithDomain:@"com.calftrail.geotagalog" code:42 userInfo:errInfo]];
}
[exportController release], exportController = nil;
CFNumberRef geolookup = CFPreferencesCopyValue(CFSTR("GeocodeLookupPreference"), CFSTR("com.apple.iPhoto"),
kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (geolookup) {
if ([(id)geolookup intValue] != 2) { // 0 - never lookup, 1 - not set, 2 - automatically
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
@"iPhoto will not show your photos on its map", NSLocalizedDescriptionKey,
@"Before its Places feature will work, you must go into iPhoto's preferences "
@"and, in the Advanced tab, set \"Look up Places\" to \"Automatically\".",
NSLocalizedRecoverySuggestionErrorKey, nil];
NSError* lookupSettingError = [NSError errorWithDomain:@"com.calftrail.tagalog" code:1000 userInfo:errInfo];
[NSApp presentError:lookupSettingError];
}
CFRelease(geolookup);
}
[window close];
}
- (void)exportDidCancel:(ExportController*)theExportController {
(void)theExportController;
[exportController release], exportController = nil;
}
- (void)exportDidFail:(ExportController*)theExportController
withError:(NSError*)err
{
(void)theExportController;
[NSApp presentError:err];
[exportController release], exportController = nil;
}
#pragma mark Action methods
- (IBAction)beginExport:(id)sender {
(void)sender;
NSInteger workflow = [[NSUserDefaults standardUserDefaults] integerForKey:@"ExportWorkflow"];
NSMapTable* exportItemsMetadata = [metadataController exportMetadata];
exportController = [TaskExport new];
[exportController setDelegate:self];
[exportController setProjectWindow:window];
[exportController setItemsWithMetadata:exportItemsMetadata];
[exportController setWorkflow:workflow];
[exportController export];
}
@end
TLProjectionRef TLProjectionCreateStereographicWithCenter(TLCoordinateDegrees lat,
TLCoordinateDegrees lon)
{
TLMutableProjectionParametersRef params = TLProjectionParametersCreateMutable();
TLProjectionParametersSetLongitudeOfOrigin(params, lon);
TLProjectionParametersSetLatitudeOfOrigin(params, lat);
TLProjectionRef proj = TLProjectionCreate(TLProjectionNameStereographic, TLProjectionGeoidWGS84, params, NULL);
TLProjectionParametersRelease(params);
return proj;
}
@interface WorkflowTransformer: NSValueTransformer {} @end
@implementation WorkflowTransformer
+ (Class)transformedValueClass { return [NSString class]; }
+ (BOOL)allowsReverseTransformation { return NO; }
- (id)transformedValue:(id)workflow {
switch ([workflow intValue]) {
case iPhotoDatabase:
return @"iPhoto only";
case iPhotoOriginals:
return @"iPhoto originals";
case justOriginals:
return @"Originals only";
}
return @"Unknown workflow";
}
@end