scan archive UI improvements, phase 3
[zxing.git] / iphone / Classes / ParsedResult.m
1 //
2 //  ParsedResult.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 22/05/2008.
6 /*
7  * Copyright 2008 ZXing authors
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #import "ParsedResult.h"
23
24 #import "TextParsedResult.h"
25 #import "TelParsedResult.h"
26 #import "EmailDoCoMoParsedResult.h"
27 #import "AddressBookDoCoMoParsedResult.h"
28 #import "URIParsedResult.h"
29 #import "URLTOParsedResult.h"
30 #import "BookmarkDoCoMoParsedResult.h"
31 #import "GeoParsedResult.h"
32
33 #import "UIKit/UIStringDrawing.h"
34 #import <math.h>
35
36 @implementation ParsedResult
37
38 static NSArray *parsedResultTypes = nil;
39 static NSMutableDictionary *iconsByClass = nil;
40
41 + (NSArray *)parsedResultTypes {
42   if (parsedResultTypes == nil) {
43     parsedResultTypes = 
44     [[NSArray alloc] initWithObjects:
45      [AddressBookDoCoMoParsedResult class],
46      [EmailDoCoMoParsedResult class],
47      [BookmarkDoCoMoParsedResult class],
48      [URLTOParsedResult class],
49      [TelParsedResult class],
50      [GeoParsedResult class],
51      [URIParsedResult class],
52      [TextParsedResult class], 
53      nil];
54   }
55   return parsedResultTypes;
56 }
57
58 + parsedResultForString:(NSString *)s {
59 #ifdef DEBUG
60   NSLog(@"parsing result:\n<<<\n%@\n>>>\n", s);
61 #endif
62   for (Class c in [self parsedResultTypes]) {
63     ParsedResult *result = [c parsedResultForString:s];
64     if (result != nil) {
65       return result;
66     }
67   }
68   return nil;
69 }
70
71 + (NSString *)typeName {
72   return NSStringFromClass(self);
73 }
74
75 - (NSString *)stringForDisplay {
76   return @"{none}";
77 }
78
79 #define ICON_SIZE 40
80 #define ICON_INSIDE 36
81
82 + (UIImage *)icon {
83   if (iconsByClass == nil) {
84     iconsByClass = [[NSMutableDictionary alloc] initWithCapacity:16];
85   }
86   UIImage *icon = [iconsByClass objectForKey:[self class]];
87   if (icon == nil) {
88     UIGraphicsBeginImageContext(CGSizeMake(ICON_SIZE, ICON_SIZE));
89     CGContextRef ctx = UIGraphicsGetCurrentContext();
90     
91     [[UIColor lightGrayColor] set];
92     UIRectFill(CGRectMake(0, 0, ICON_SIZE, ICON_SIZE));
93     
94     [[UIColor blackColor] set];
95     NSString *s = [[self class] typeName];
96     UIFont *font = [UIFont systemFontOfSize:16];
97     CGSize stringSize = [s sizeWithFont:font];
98     float xScale = fminf(1.0, ICON_INSIDE / stringSize.width);
99     float yScale = fminf(1.0, ICON_INSIDE / stringSize.height);
100     
101     CGContextTranslateCTM(ctx, (ICON_SIZE / 2), (ICON_SIZE / 2));
102     CGContextRotateCTM(ctx, -M_PI / 6.0);
103     CGContextScaleCTM(ctx, xScale, yScale);
104     CGContextTranslateCTM(ctx, 
105                           -(stringSize.width)/2.0, 
106                           -(stringSize.height)/2.0);
107     
108     [s drawAtPoint:CGPointMake(0, 0) withFont:font];
109     
110     icon = [UIGraphicsGetImageFromCurrentImageContext() retain];
111     [iconsByClass setObject:icon forKey:[self class]];
112     UIGraphicsEndImageContext();
113   }
114   return icon;
115 }
116
117 - (UIImage *)icon {
118   return [[self class] icon];
119 }
120
121 - (NSArray *)actions {
122   if (!actions) {
123     actions = [[NSMutableArray alloc] init];
124     [self populateActions];
125   }
126   return actions;
127 }
128
129 - (void) populateActions {
130 }
131
132 - (void) dealloc {
133   [actions release];
134   [super dealloc];
135 }
136
137 @end