diff options
author | Bibiko <bibiko@eva.mpg.de> | 2010-10-09 11:04:01 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2010-10-09 11:04:01 +0000 |
commit | 6b60ddf1029b912586f80b6b7727f8083fb122ea (patch) | |
tree | f140424ab4acd2cda8fa1adf52cec74740b68a04 /Source/SPGeometryDataView.m | |
parent | 869e355e7f4856dee3171520f549e32855f68dc5 (diff) | |
download | sequelpro-6b60ddf1029b912586f80b6b7727f8083fb122ea.tar.gz sequelpro-6b60ddf1029b912586f80b6b7727f8083fb122ea.tar.bz2 sequelpro-6b60ddf1029b912586f80b6b7727f8083fb122ea.zip |
• enabled thumbnail images of spatial geometry fields as tooltips
- note: geometrycollection support will follow soon
Diffstat (limited to 'Source/SPGeometryDataView.m')
-rw-r--r-- | Source/SPGeometryDataView.m | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/Source/SPGeometryDataView.m b/Source/SPGeometryDataView.m index 0a5a0294..ee27f15f 100644 --- a/Source/SPGeometryDataView.m +++ b/Source/SPGeometryDataView.m @@ -33,51 +33,63 @@ - (id)initWithCoordinates:(NSDictionary*)coord { + CGFloat maxDim; + CGFloat targetDim = 400.0; + margin_offset = 10.0; type = [coord objectForKey:@"type"]; coordinates = [coord objectForKey:@"coordinates"]; + x_min = (CGFloat)[[[coord objectForKey:@"bbox"] objectAtIndex:0] doubleValue]; x_max = (CGFloat)[[[coord objectForKey:@"bbox"] objectAtIndex:1] doubleValue]; y_min = (CGFloat)[[[coord objectForKey:@"bbox"] objectAtIndex:2] doubleValue]; y_max = (CGFloat)[[[coord objectForKey:@"bbox"] objectAtIndex:3] doubleValue]; - zoom_factor = 1.0; width = x_max - x_min; height = y_max - y_min; - // make it a square due to aspect ratio - if(width>height) - height = width; + maxDim = (width > height) ? width : height; + if(maxDim != 0) + zoom_factor = targetDim/maxDim; else - width = height; + zoom_factor = 1.0; + + width*=zoom_factor; + height*=zoom_factor; + x_min*=zoom_factor; + y_min*=zoom_factor; if ( self = [super initWithFrame:NSMakeRect(0,0,width+margin_offset*2,height+margin_offset*2)] ) { ; } + return self; } - (NSPoint)normalizePoint:(NSPoint)aPoint { + aPoint.x*=zoom_factor; + aPoint.y*=zoom_factor; aPoint.x-=x_min; aPoint.y-=y_min; aPoint.x+=margin_offset; aPoint.y+=margin_offset; - return aPoint; } - (void)drawPoint:(NSPoint)aPoint { + NSBezierPath *circlePath = [NSBezierPath bezierPath]; - [circlePath appendBezierPathWithOvalInRect:NSMakeRect(aPoint.x-2,aPoint.y-2,4,4)]; + [circlePath appendBezierPathWithOvalInRect:NSMakeRect(aPoint.x-5,aPoint.y-5,10,10)]; [[NSColor grayColor] setStroke]; [[NSColor redColor] setFill]; [circlePath stroke]; [circlePath fill]; + } - (void)drawRect:(NSRect)dirtyRect @@ -86,14 +98,17 @@ if(!type || ![type length] || !coordinates || ![coordinates count]) return; NSBezierPath *path; - NSColor *polyFillColor = [NSColor colorWithCalibratedRed:.5 green:.5 blue:0.5 alpha:0.05]; + NSColor *polyFillColor1 = [NSColor colorWithCalibratedRed:0.0 green:1.0 blue:0.0 alpha:0.1]; + NSColor *polyFillColor2 = [NSColor colorWithCalibratedRed:0.0 green:1.0 blue:1.0 alpha:0.1]; + NSColor *polyFillColor3 = [NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.1]; BOOL isFirst = YES; NSPoint aPoint; + // Draw a rect as border path = [NSBezierPath bezierPathWithRect:[self bounds]]; [path setLineWidth:0.1]; - [[NSColor whiteColor] set]; + [[NSColor colorWithCalibratedRed:1 green:1 blue:1 alpha:0.96] set]; [path fill]; [[NSColor grayColor] set]; [path stroke]; @@ -109,7 +124,6 @@ else if([type hasSuffix:@"LINESTRING"]) { for(NSArray* lines in coordinates) { - path = [NSBezierPath bezierPath]; isFirst = YES; for(NSString* coord in lines) { aPoint = [self normalizePoint:NSPointFromString(coord)]; @@ -126,6 +140,7 @@ } } else if([type hasSuffix:@"POLYGON"]) { + NSUInteger i = 0; for(NSArray* polygons in coordinates) { isFirst = YES; for(NSString* coord in polygons) { @@ -139,21 +154,37 @@ [self drawPoint:aPoint]; } [[NSColor blackColor] setStroke]; - [polyFillColor setFill]; + switch(i) { + case 0: [polyFillColor1 setFill]; + break; + case 1: [polyFillColor2 setFill]; + break; + case 2: [polyFillColor3 setFill]; + break; + } [path fill]; [path stroke]; + i++; + if(i>2) i=0; } } } -- (NSImage*)image +- (NSImage*)thumbnailImage { - [self drawRect:[self bounds]]; + if(!type || ![type length] || !coordinates || ![coordinates count]) return nil; + + NSSize mySize = self.bounds.size; + NSSize imgSize = NSMakeSize( mySize.width, mySize.height ); + NSRect myBounds = [self bounds]; + + NSBitmapImageRep *bitmap = [self bitmapImageRepForCachingDisplayInRect:myBounds]; + [bitmap setSize:imgSize]; + [self cacheDisplayInRect:myBounds toBitmapImageRep:bitmap]; - NSImage *image = [[[NSImage alloc] initWithSize:[self bounds].size] autorelease]; - NSBitmapImageRep *bitmap = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]] autorelease]; + NSImage* image = [[[NSImage alloc]initWithSize:imgSize] autorelease]; [image addRepresentation:bitmap]; return image; |