-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLMapController.m
424 lines (345 loc) · 12.5 KB
/
TLMapController.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//
// TLMapController.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 12/17/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TLMapController.h"
#import "TLMapView.h"
#import "TLMapFrameLayer.h"
#import "TLGraticuleMapLayer.h"
#import "TLNaturalEarthLayer.h"
#import "TLMapNavigationLayer.h"
#import "TLMapLocationLayer.h"
#import "TLPhotoLayer.h"
#import "TLTrackLayer.h"
#import "TLLibraryHost.h"
#import "TLTrack.h"
#import "TLPhoto.h"
#import "TLLocation.h"
#import "TLTimestamp.h"
#import "TLLocator.h"
#include "TLGeometry.h"
#include "TLMercatalogControllerShared.h"
#import "TLCocoaToolbag.h"
const NSDragOperation TLDragOperationInternal = NSDragOperationPrivate;
@implementation TLMapController
#pragma mark Lifecycle
- (void)awakeFromNib {
TLMapFrameLayer* mapFrameLayer = [[TLMapFrameLayer new] autorelease];
[view addLayer:mapFrameLayer];
TLNaturalEarthLayer* naturalEarthLayer = [[TLNaturalEarthLayer new] autorelease];
[view addLayer:naturalEarthLayer];
TLGraticuleMapLayer* graticuleLayer = [[TLGraticuleMapLayer new] autorelease];
[view addLayer:graticuleLayer];
trackLayer = [[TLTrackLayer new] autorelease];
[trackLayer setDataSource:self];
//[trackLayer setHidden:YES];
[view addLayer:trackLayer];
navigationLayer = [[TLMapNavigationLayer new] autorelease];
[navigationLayer setDelegate:self];
[view addLayer:navigationLayer];
locationLayer = [[TLMapLocationLayer new] autorelease];
[locationLayer setDelegate:self];
[view addLayer:locationLayer];
TLLibraryHost* host = [TLLibraryHost libraryHostForContext:[self modelContext]];
[locationLayer setHomeBase:[host homeBase]];
NSArray* dropTypes = TLMercatalogAcceptedDropTypes();
photoLayer = [[TLPhotoLayer new] autorelease];
[photoLayer setDelegate:self];
[photoLayer setDataSource:self];
[photoLayer setRegisteredDragTypes:dropTypes];
[view addLayer:photoLayer];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[modelContext release];
[navigationLayer setDelegate:nil];
[locationLayer setDelegate:nil];
[photoLayer setDelegate:nil];
[photoLayer setDataSource:nil];
[trackLayer setDataSource:nil];
[view release];
[super dealloc];
}
#pragma mark Basic accessors
@synthesize delegate;
@synthesize modelContext;
- (void)setModelContext:(NSManagedObjectContext*)newModelContext {
if (newModelContext == modelContext) return;
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:modelContext];
[modelContext release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(modelChanged:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:newModelContext];
modelContext = [newModelContext retain];
}
- (void)loadView {
view = [[TLMapView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, 1.0f, 1.0f)];
[view setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)];
[view awakeFromNib];
[self awakeFromNib];
}
- (NSView*)view {
if (!view) {
[self loadView];
}
return view;
}
#pragma mark Glue and external communication
- (void)delayedModelChanged {
[photoLayer reloadData];
[trackLayer reloadData];
}
- (void)modelChanged:(NSNotification*)aNotification {
(void)aNotification;
// delay updates to ensure host object gets its notification first
[self performSelector:@selector(delayedModelChanged) withObject:nil afterDelay:0.0];
}
- (void)setTracksVisible:(BOOL)newTracksVisible {
[trackLayer setHidden:!newTracksVisible];
[locationLayer setHidden:!newTracksVisible];
}
- (BOOL)tracksVisible {
return ![trackLayer isHidden];
}
- (void)setSelectedPhotos:(NSSet*)newSelectedPhotos {
[photoLayer setSelectedPhotos:newSelectedPhotos];
}
- (NSSet*)selectedPhotos {
return [photoLayer selectedPhotos];
}
- (void)setDisplayedPhotos:(NSSet*)newDisplayedPhotos {
[photoLayer setDisplayedPhotos:newDisplayedPhotos];
}
- (NSSet*)displayedPhotos {
return [photoLayer displayedPhotos];
}
- (IBAction)zoomCompletelyOut:(id)sender {
[navigationLayer zoomCompletelyOut:sender];
}
- (void)setPreviewTimestamps:(NSArray*)previewTimestamps {
(void)previewTimestamps;
if ([[self delegate]
respondsToSelector:@selector(mapControllerWantsPreview:forTimestamps:)])
{
[[self delegate] mapControllerWantsPreview:self
forTimestamps:previewTimestamps];
}
}
- (void)setPreviewLocations:(NSArray*)previewLocations {
[photoLayer setPreviewLocations:previewLocations];
}
- (void)setMouseLocation:(TLLocation*)mouseLocation {
[locationLayer setPreviewLocation:mouseLocation];
}
- (void)mapLocationLayerDidSetHomeBase:(TLMapLocationLayer*)theLocationLayer {
TLLibraryHost* host = [TLLibraryHost libraryHostForContext:[self modelContext]];
[host setHomeBase:[theLocationLayer homeBase]];
}
- (void)mapNavigationLayerDidIgnoreClick:(TLMapNavigationLayer*)navLayer {
(void)navLayer;
[photoLayer setSelectedPhotos:nil];
}
- (void)photoMapLayerSelectionDidChange:(NSNotification*)notification {
(void)notification;
NSNotification* newNotification = [NSNotification notificationWithName:TLMercatalogSelectionDidChangeNotification
object:self];
if ([[self delegate] respondsToSelector:@selector(controllerSelectionDidChange:)]) {
[[self delegate] controllerSelectionDidChange:newNotification];
}
[[NSNotificationCenter defaultCenter] postNotification:newNotification];
}
- (TLLocator*)locator {
TLLocator* locator = [[TLLocator new] autorelease];
[locator setModelContext:[self modelContext]];
return locator;
}
- (NSArray*)filenamesForPhotos:(NSArray*)photosDropped
promisedAtDestination:(NSURL*)dropDestination
{
NSArray* filenames = nil;
if ([[self delegate] respondsToSelector:
@selector(controllerNeedsFilenames:forDroppedPhotos:atDestination:)])
{
[[self delegate] controllerNeedsFilenames:self
forDroppedPhotos:photosDropped
atDestination:dropDestination];
}
else {
NSLog(@"Delegate did not fulfill promise drag.");
filenames = [NSArray array];
}
return filenames;
}
- (void)performFileImportInBackground:(NSArray*)filenames {
if ([[self delegate] respondsToSelector:@selector(controllerNeedsImport:forFiles:)]) {
[[self delegate] controllerNeedsImport:self forFiles:filenames];
}
}
#pragma mark Data source
- (NSArray*)trackLayer:(TLTrackLayer*)layer
tracksInBounds:(TLBounds)bounds
underProjection:(TLProjectionRef)proj
{
(void)layer;
(void)bounds;
(void)proj;
TLLibraryHost* host = [TLLibraryHost libraryHostForContext:[self modelContext]];
return [host allTracks];
}
- (NSArray*)photoLayer:(TLPhotoLayer*)layer
photosInBounds:(TLBounds)bounds
underProjection:(TLProjectionRef)proj
{
(void)layer;
(void)bounds;
(void)proj;
TLLibraryHost* host = [TLLibraryHost libraryHostForContext:[self modelContext]];
return [host visiblePhotos];
}
- (BOOL)photoLayer:(TLPhotoLayer*)layer
writePhotos:(NSArray*)photos
toPasteboard:(NSPasteboard*)pasteboard
{
(void)layer;
return TLMercatalogWritePhotosToPasteboard(photos, pasteboard, self);
}
- (NSArray*)photoLayer:(TLPhotoLayer*)layer
filenamesForPhotos:(NSArray*)photosDropped
promisedAtDestination:(NSURL*)dropDestination
{
(void)layer;
return [self filenamesForPhotos:photosDropped promisedAtDestination:dropDestination];
}
#pragma mark Drop destination
- (NSMapTable*)locatePhotos:(NSArray*)photos usingSingleLocation:(TLLocation*)location {
NSMapTable* photoLocations = [NSMapTable mapTableWithStrongToStrongObjects];
for (TLPhoto* photo in photos) {
[photoLocations setObject:[location perturbedLocation] forKey:photo];
}
return photoLocations;
}
- (TLLocation*)locationForMouseInMap:(CGPoint)targetPoint withInfo:(id < TLMapInfo >)mapInfo {
TLProjectionError err = TLProjectionErrorNone;
TLCoordinate mouseCoord = TLProjectionUnprojectPoint([mapInfo projection], targetPoint, &err);
if (err) return nil;
CGSize interactiveSize = [mapInfo significantInteractiveSize];
CGFloat interactiveDistance = TLSizeGetAverageWidth(interactiveSize);
TLCoordinateAccuracy mouseAccuracy = interactiveDistance / 2.0f;
return [TLLocation locationWithCoordinate:mouseCoord
horizontalAccuracy:mouseAccuracy];
}
- (NSSet*)timestampsForMouseInMap:(CGPoint)targetPoint withInfo:(id < TLMapInfo >)mapInfo {
TLLocation* mouseLocation = [self locationForMouseInMap:targetPoint withInfo:mapInfo];
if (!mouseLocation) return nil;
const double trackSnapFactor = 40.0f;
TLCoordinateAccuracy searchDistance = trackSnapFactor * [mouseLocation horizontalAccuracy];
TLLocation* searchLocation = [TLLocation locationWithCoordinate:[mouseLocation coordinate]
horizontalAccuracy:searchDistance];
return [[self locator] trackTimestampsAtLocation:searchLocation];
}
- (TLTimestamp*)timestampNearestPhoto:(TLPhoto*)firstPhoto inTimestamps:(NSSet*)timestamps {
NSDate* targetDate = [[firstPhoto timestamp] time];
TLTimestamp* closestTimestamp = nil;
NSTimeInterval closestInterval = 0.0;
for (TLTimestamp* timestamp in timestamps) {
NSTimeInterval interval = fabs([targetDate timeIntervalSinceDate:[timestamp time]]);
if (!closestTimestamp || interval < closestInterval) {
closestTimestamp = timestamp;
closestInterval = interval;
}
}
return closestTimestamp;
}
- (BOOL)acceptInternalDrop:(id < NSDraggingInfo >)dropInfo
withMapInfo:(id < TLMapInfo >)mapInfo
previewOnly:(BOOL)justPreview
{
NSPasteboard* pasteboard = [dropInfo draggingPasteboard];
NSArray* photos = TLMercatalogPhotosFromPasteboard(pasteboard);
if (![photos count] || TLMercatalogPhotosAreLocked(photos)) return NO;
CGPoint mouseOnMap = [mapInfo convertWindowPointToMap:[dropInfo draggingLocation]];
NSSet* mouseTimestamps = [self timestampsForMouseInMap:mouseOnMap withInfo:mapInfo];
if ([mouseTimestamps count]) {
// use timestamp closest to first photo's timestamp
TLPhoto* mousePhoto = [photos objectAtIndex:0];
TLTimestamp* mouseTimestamp = [self timestampNearestPhoto:mousePhoto
inTimestamps:mouseTimestamps];
NSMapTable* photoTimestamps = TLMercatalogTimestampPhotos(photos, mouseTimestamp);
NSMapTable* photoLocations = [[self locator] locateTimestamps:photoTimestamps];
if (justPreview) {
NSArray* previewTimestamps = TLNSMapTableAllObjects(photoTimestamps);
NSArray* previewLocations = TLNSMapTableAllObjects(photoLocations);
[self setPreviewTimestamps:previewTimestamps];
[photoLayer setPreviewLocations:previewLocations];
}
else {
for (TLPhoto* photo in photos) {
TLTimestamp* newTimestamp = [photoTimestamps objectForKey:photo];
TLLocation* newLocation = [photoLocations objectForKey:photo];
[photo setTimestamp:newTimestamp];
[photo setLocation:newLocation];
}
[self setPreviewTimestamps:nil];
}
}
else {
TLLocation* mouseLocation = [self locationForMouseInMap:mouseOnMap withInfo:mapInfo];
NSMapTable* photoLocations = [self locatePhotos:photos usingSingleLocation:mouseLocation];
if (justPreview) {
NSArray* previewLocations = TLNSMapTableAllObjects(photoLocations);
[photoLayer setPreviewLocations:previewLocations];
}
else {
for (TLPhoto* photo in photos) {
TLLocation* newLocation = [photoLocations objectForKey:photo];
[photo setLocation:newLocation];
}
}
[self setPreviewTimestamps:nil];
}
return YES;
}
- (BOOL)acceptExternalDrop:(id < NSDraggingInfo >)dropInfo
withMapInfo:(id < TLMapInfo >)mapInfo
previewOnly:(BOOL)justPreview
{
(void)mapInfo;
NSPasteboard* pasteboard = [dropInfo draggingPasteboard];
NSArray* filenames = TLMercatalogFilesFromPasteboard(pasteboard);
if (![filenames count]) return NO;
if (!justPreview) {
[self performFileImportInBackground:filenames];
}
return YES;
}
- (NSDragOperation)photoLayer:(TLPhotoLayer*)layer
validateDrop:(id < NSDraggingInfo >)dropInfo
withMapInfo:(id < TLMapInfo >)mapInfo
{
(void)layer;
BOOL internalDrop = [self acceptInternalDrop:dropInfo withMapInfo:mapInfo previewOnly:YES];
if (internalDrop) return TLDragOperationInternal;
BOOL externalDrop = [self acceptExternalDrop:dropInfo withMapInfo:mapInfo previewOnly:YES];
if (externalDrop) return NSDragOperationLink;
return NSDragOperationNone;
}
- (BOOL)photoLayer:(TLPhotoLayer*)layer
acceptDrop:(id < NSDraggingInfo >)dropInfo
withMapInfo:(id < TLMapInfo >)mapInfo
{
(void)layer;
BOOL internalDrop = [self acceptInternalDrop:dropInfo withMapInfo:mapInfo previewOnly:NO];
if (internalDrop) return YES;
BOOL externalDrop = [self acceptExternalDrop:dropInfo withMapInfo:mapInfo previewOnly:NO];
if (externalDrop) return YES;
return NO;
}
- (void)photoLayerDropDidCancel:(TLPhotoLayer*)layer {
(void)layer;
[self setPreviewTimestamps:nil];
}
@end