Skip to content

Commit

Permalink
Fully implemented anime search.
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Pigozzi committed Aug 4, 2009
1 parent 92a0d53 commit 0491459
Show file tree
Hide file tree
Showing 12 changed files with 1,627 additions and 371 deletions.
65 changes: 65 additions & 0 deletions NSImage+NiceScaling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* =============================================================================
PROJECT: Filie
FILE: NSImage+NiceScaling.h
COPYRIGHT: (c) 2003 by M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: GNU GPL, Modified BSD
REVISIONS:
2003-12-19 UK Created.
========================================================================== */

// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------

#import <AppKit/AppKit.h>


// -----------------------------------------------------------------------------
// Categories:
// -----------------------------------------------------------------------------

/*
All these calls return a version of this image that is scaled proportionally
to the best size. There are two ways the best size can be determined:
a) "fit" - The image is resized so it completely fits into a rect of the
specified size. Nothing of the image will come to lie outside, but there
may be empty areas in the destination rectangle not covered by the image.
b) "cover" - The image is scaled so drawing it into the specified rectangle
will cause the image to leave no empty spots in the rectangle. For this
to look right, some parts of the image may be cut off.
Defaults:
withInterpolation - NSImageInterpolationHigh
andBox - NO
align - NSImageAlignCenter
*/

@interface NSImage (NiceScaling)

// NSImage -> NSImage (fit):
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn;
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter;
-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox;

// NSImage -> NSImage (cover):
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn;
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter;
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox;
-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter
andBox: (BOOL)doBox align: (NSImageAlignment)align;

// [NSImage size] -> NSSize:
-(NSSize) scaledSizeToFitSize: (NSSize)size;
-(NSSize) scaledSizeToCoverSize: (NSSize)size;

// NSSize -> NSSize:
+(NSSize) scaledSize: (NSSize)imgSize toFitSize: (NSSize)size;
+(NSSize) scaledSize: (NSSize)imgSize toCoverSize: (NSSize)size;

@end
228 changes: 228 additions & 0 deletions NSImage+NiceScaling.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/* =============================================================================
PROJECT: Filie
FILE: NSImage+NiceScaling.h
COPYRIGHT: (c) 2003 by M. Uli Kusterer, all rights reserved.
AUTHORS: M. Uli Kusterer - UK
LICENSES: GNU GPL, Modified BSD
REVISIONS:
2003-12-19 UK Created.
========================================================================== */

// -----------------------------------------------------------------------------
// Headers:
// -----------------------------------------------------------------------------

#import "NSImage+NiceScaling.h"


@implementation NSImage (NiceScaling)

-(NSImage*) scaledImageToFitSize: (NSSize)fitIn
{
return [self scaledImageToFitSize: fitIn withInterpolation: NSImageInterpolationHigh];
}


-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter
{
return [self scaledImageToFitSize: fitIn withInterpolation: inter andBox: NO];
}

-(NSImage*) scaledImageToFitSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox
{
NSSize size = [self scaledSizeToFitSize: fitIn];
NSImage* img = [[[NSImage alloc] initWithSize: fitIn] autorelease];
NSRect srcBox = { { 0, 0 }, { 0, 0 } },
dstBox = { { 0, 0 }, { 0, 0 } };

dstBox.size = size;
srcBox.size = [self size];
dstBox.origin.x += (fitIn.width -size.width) /2;
dstBox.origin.y += (fitIn.height -size.height) /2;

NS_DURING
[img lockFocus];
if( doBox )
{
[[NSColor whiteColor] set];
[NSBezierPath fillRect: dstBox];
[[NSColor blackColor] set];
}
[[NSGraphicsContext currentContext] setImageInterpolation: inter];
[self drawInRect: dstBox fromRect: srcBox operation:NSCompositeSourceOver fraction:1.0];
if( doBox )
[NSBezierPath strokeRect: dstBox];
[img unlockFocus];

NS_VALUERETURN( img, NSImage* );
NS_HANDLER
NSLog(@"Couldn't scale image %@",localException);
NS_ENDHANDLER

return nil;
}


-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn
{
return [self scaledImageToCoverSize: fitIn withInterpolation: NSImageInterpolationHigh];
}


-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter
{
return [self scaledImageToCoverSize: fitIn withInterpolation: inter andBox: NO];
}

-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter andBox: (BOOL)doBox
{
return [self scaledImageToCoverSize: fitIn withInterpolation: inter andBox: doBox align: NSImageAlignCenter];
}

-(NSImage*) scaledImageToCoverSize: (NSSize)fitIn withInterpolation: (NSImageInterpolation)inter
andBox: (BOOL)doBox align: (NSImageAlignment)align
{
NSSize size = [self scaledSizeToCoverSize: fitIn];
NSImage* img = [[[NSImage alloc] initWithSize: fitIn] autorelease];
NSRect srcBox = { { 0, 0 }, { 0, 0 } },
dstBox = { { 0, 0 }, { 0, 0 } },
clipBox = { { 0, 0 }, { 0, 0 } };

dstBox.size = size;
srcBox.size = [self size];
clipBox.size = fitIn;

// Center it:
switch( align )
{
case NSImageAlignCenter: // Center h and v.
dstBox.origin.x -= (dstBox.size.width - clipBox.size.width) /2;
dstBox.origin.y -= (dstBox.size.height - clipBox.size.height) /2;
break;

case NSImageAlignTop: // Center h, v at top.
dstBox.origin.x -= (dstBox.size.width - clipBox.size.width) /2;
dstBox.origin.y -= dstBox.size.height - clipBox.size.height;
break;

case NSImageAlignTopLeft: // h at left, v at top.
dstBox.origin.y -= dstBox.size.height - clipBox.size.height;
break;

case NSImageAlignLeft: // h at left, center v.
dstBox.origin.y -= (dstBox.size.height - clipBox.size.height) /2;
break;

case NSImageAlignBottomLeft: // h at left, v at bottom
default:
break;

case NSImageAlignBottom: // center h, v at bottom.
dstBox.origin.x -= (dstBox.size.width - clipBox.size.width) /2;
break;

case NSImageAlignBottomRight: // h at right, v at bottom.
dstBox.origin.x -= dstBox.size.width - clipBox.size.width;
break;

case NSImageAlignRight: // h at right, center v.
dstBox.origin.x -= dstBox.size.width - clipBox.size.width;
dstBox.origin.y -= (dstBox.size.height - clipBox.size.height) /2;
break;

case NSImageAlignTopRight: // h at right, v at top.
dstBox.origin.y -= dstBox.size.height - clipBox.size.height;
dstBox.origin.x -= dstBox.size.width - clipBox.size.width;
break;

}

NS_DURING
[img lockFocus];
[NSBezierPath clipRect: clipBox];
[[NSGraphicsContext currentContext] setImageInterpolation: inter];
[self drawInRect: dstBox fromRect: srcBox operation:NSCompositeSourceOver fraction:1.0];
if( doBox )
{
[[NSColor blackColor] set];
[NSBezierPath strokeRect: clipBox];
}
[img unlockFocus];

NS_VALUERETURN( img, NSImage* );
NS_HANDLER
NSLog(@"Couldn't scale image %@",localException);
NS_ENDHANDLER

return nil;
}


-(NSSize) scaledSizeToFitSize: (NSSize)size
{
return [[self class] scaledSize: [self size] toFitSize: size];
}


+(NSSize) scaledSize: (NSSize)imgSize toFitSize: (NSSize)size
{
NSSize finalSize = imgSize;
float ratio = imgSize.height / imgSize.width;

//if( imgSize.width > size.width || imgSize.height > size.height )
{
finalSize.width = size.width;
if( imgSize.height == imgSize.width )
finalSize.height = finalSize.width;
else
finalSize.height = size.height * ratio;

if( finalSize.height > size.height )
{
ratio = imgSize.width / imgSize.height;
finalSize.height = size.height;
if( imgSize.height == imgSize.width )
finalSize.width = finalSize.height;
else
finalSize.width = size.width * ratio;
}
}

return( finalSize );
}


-(NSSize) scaledSizeToCoverSize: (NSSize)size
{
return [[self class] scaledSize: [self size] toCoverSize: size];
}


+(NSSize) scaledSize: (NSSize)imgSize toCoverSize: (NSSize)size
{
NSSize finalSize = imgSize;
float ratio = imgSize.height / imgSize.width;

/*if( imgSize.width == size.width
&& imgSize.height == size.height )
return imgSize;*/

finalSize.width = size.width;
finalSize.height = finalSize.width * ratio;

if( finalSize.height < size.height )
{
ratio = imgSize.width / imgSize.height;
finalSize.height = size.height;
finalSize.width = finalSize.height * ratio;
}

return( finalSize );
}


@end
16 changes: 16 additions & 0 deletions NSXMLNode+stringForXPath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NSXMLNode+stringForXPath.h
// iMAL
//
// Created by Stefano Pigozzi on 8/4/09.
// Copyright 2009 Stefano Pigozzi. All rights reserved.
//

#import <Cocoa/Cocoa.h>


@interface NSXMLNode (stringForXPath)

-(NSString *)stringForXPath:(NSString *)xp;

@end
30 changes: 30 additions & 0 deletions NSXMLNode+stringForXPath.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// NSXMLNode+stringForXPath.m
// iMAL
//
// Created by Stefano Pigozzi on 8/4/09.
// Copyright 2009 Stefano Pigozzi. All rights reserved.
//

#import "NSXMLNode+stringForXPath.h"


@implementation NSXMLNode (stringForXPath)

-(NSString *)stringForXPath:(NSString *)xp
{
NSError *error;
NSArray *nodes = [self nodesForXPath:xp error:&error];
if (!nodes) {
NSAlert *alert = [NSAlert alertWithError:error];
[alert runModal];
return nil;
}
if ([nodes count] == 0) {
return nil;
} else {
return [[nodes objectAtIndex:0] stringValue];
}
}

@end
25 changes: 23 additions & 2 deletions SearchModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,35 @@

@interface SearchModel : NSObject {
NSString * __title;
NSString * __synonyms;
NSString * __type;
int __score;
NSString * __image_url;
float __score;
int __episodes;

NSString * __status;
NSString * __start_date;
NSString * __end_date;

NSString * __synopsis;

NSImage * __scaled_image;
}

@property (retain) NSString * __title;
@property (retain) NSString * __synonyms;
@property (retain) NSString * __type;
@property (assign) int __score;
@property (retain) NSString * __image_url;
@property (assign) float __score;
@property (assign) int __episodes;
@property (retain) NSString * __status;
@property (retain) NSString * __start_date;
@property (retain) NSString * __end_date;
@property (retain) NSString * __synopsis;

-(NSImage *)__image;

-(NSAttributedString *)__bold_title;
-(NSAttributedString *)__formatted_synopsis;

@end
Loading

0 comments on commit 0491459

Please sign in to comment.