the very simple test app that calls the ZXingWidget
[zxing.git] / iphone / Classes / ScanViewController.m
1 //
2 //  ScanViewController.m
3 //  ZXing
4 //
5 //  Created by Christian Brunschen on 24/06/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 "ScanViewController.h"
23 #import "ResultAction.h"
24
25
26 #define TEXT_VIEW_TAG 0x17
27 #define DATETIME_VIEW_TAG 0x18
28 #define BUTTON_LABEL_TAG 0x19
29 #define TITLE_HEIGHT 44
30 #define BODY_HEIGHT 88
31
32 @implementation ScanViewController
33
34 @synthesize result;
35 @synthesize scan;
36 @synthesize dateFormatter;
37
38 #define FONT_NAME @"TimesNewRomanPSMT"
39 #define FONT_SIZE 16
40
41 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
42         if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
43     self.result = r;
44     self.scan = s;
45     self.title = NSLocalizedString(@"ScanViewController title", @"Scan");
46     dateFormatter = [[NSDateFormatter alloc] init];
47     [dateFormatter setDateStyle:NSDateFormatterLongStyle];
48     [dateFormatter setTimeStyle:NSDateFormatterLongStyle];
49     bodyFont = [[UIFont fontWithName:FONT_NAME size:FONT_SIZE] retain];
50         }
51         return self;
52 }
53
54
55 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
56         return [[result actions] count] ? 2 : 1;
57 }
58
59
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
61   switch (section) {
62     case 0:
63       return 3;
64     case 1:
65       return [[result actions] count];
66     default:
67       return 0;
68   }
69 }
70
71 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
72         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
73         if (cell == nil) {
74                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
75         }
76   return cell;
77 }
78
79 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
80         static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
81   return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
82 }
83
84 - (UITableViewCell *)datetimeCellInTableView:(UITableView *)tableView {
85         static NSString *DatetimeIdentifier = @"ScanViewDatetimeIdentifier";
86         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DatetimeIdentifier];
87         if (cell == nil) {
88                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 34) reuseIdentifier:DatetimeIdentifier] autorelease];
89     UILabel *label = [cell textLabel];
90     label.font = [UIFont systemFontOfSize:[UIFont systemFontSize] * 2.0 / 3.0];
91     label.textColor = [UIColor grayColor];
92     label.textAlignment = UITextAlignmentCenter;
93         }
94   return cell;
95 }
96
97 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
98         static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
99         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
100         if (cell == nil) {
101                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, BODY_HEIGHT) reuseIdentifier:BodyIdentifier] autorelease];
102     UITextView *textView = [[UITextView alloc] initWithFrame:CGRectInset(cell.contentView.bounds, 6, 6)];
103     textView.font = bodyFont;
104     [textView setTag:TEXT_VIEW_TAG];
105     textView.editable = NO;
106     [textView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
107     [cell.contentView addSubview:textView];
108     [textView release];
109         }
110   return cell;
111 }
112
113 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
114         static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
115         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonIdentifier];
116         if (cell == nil) {
117                 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 44) reuseIdentifier:ButtonIdentifier] autorelease];
118     UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(cell.contentView.bounds, 6, 6)];
119     label.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
120     [label setTag:BUTTON_LABEL_TAG];
121     label.lineBreakMode = UILineBreakModeMiddleTruncation;
122     label.textColor = [UIColor grayColor];
123     label.textAlignment = UITextAlignmentCenter;
124     [label setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
125     [cell.contentView addSubview:label];
126     [label release];
127         }
128   return cell;
129 }
130
131 #define TEXT_VIEW_HEIGHT 330.0
132
133 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
134   if (indexPath.section == 0) {
135     if (indexPath.row == 0) {
136       return TITLE_HEIGHT;
137     } else if (indexPath.row == 1) {
138       CGSize size = [[result stringForDisplay] sizeWithFont:bodyFont constrainedToSize:CGSizeMake(280.0, TEXT_VIEW_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
139 #ifdef DEBUG
140       NSLog(@"text size = %f", size.height);
141 #endif
142       return fminf(TEXT_VIEW_HEIGHT, fmaxf(44, size.height + 24));
143     } else if (indexPath.row == 2) {
144       return 24.0;
145     }
146   }
147   return tableView.rowHeight;
148 }
149
150 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
151   UITableViewCell *cell;
152   
153   if (indexPath.section == 0) {
154     if (indexPath.row == 0) {
155       cell = [self titleCellInTableView:tableView];
156       UIImageView *imageView = cell.imageView;
157       imageView.image = [result icon];
158       UILabel *textLabel = cell.textLabel;
159       textLabel.text = [[result class] typeName];
160     } else if (indexPath.row == 1) {
161       cell = [self bodyCellInTableView:tableView];
162       UITextView *textView = (UITextView *)[cell viewWithTag:TEXT_VIEW_TAG];
163       textView.text = [result stringForDisplay];
164     } else if (indexPath.row == 2) {
165       cell = [self datetimeCellInTableView:tableView];
166       UILabel *textLabel = cell.textLabel;
167       textLabel.text = [dateFormatter stringFromDate:[scan stamp]];
168     }
169   } else if (indexPath.section == 1) {
170     cell = [self buttonCellInTableView:tableView];
171     ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
172     UILabel *label = (UILabel *)[cell viewWithTag:BUTTON_LABEL_TAG];
173     label.text = [action title];
174   }
175         
176         return cell;
177 }
178
179 - (void)performAction:(ResultAction *)action {
180   [action performActionWithController:self shouldConfirm:NO];
181 }
182
183 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
184   if (indexPath.section == 1) {
185     ResultAction *action = [[result actions] objectAtIndex:indexPath.row];
186     [self performSelector:@selector(performAction:) withObject:action afterDelay:0.0];
187   }
188 }
189
190 /*
191 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
192         
193         if (editingStyle == UITableViewCellEditingStyleDelete) {
194         }
195         if (editingStyle == UITableViewCellEditingStyleInsert) {
196         }
197 }
198 */
199
200 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
201         return NO;
202 }
203
204 /*
205 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
206 }
207 */
208
209 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
210         return NO;
211 }
212
213 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
214   if (indexPath.section != 1) {
215     return nil;
216   }
217   return indexPath;
218 }
219
220
221 - (void)dealloc {
222   [result release];
223   [scan release];
224   [bodyFont release];
225   [dateFormatter release];
226         [super dealloc];
227 }
228
229
230 - (void)viewDidLoad {
231         [super viewDidLoad];
232 }
233
234
235 - (void)viewWillAppear:(BOOL)animated {
236         [super viewWillAppear:animated];
237 }
238
239 - (void)viewDidAppear:(BOOL)animated {
240         [super viewDidAppear:animated];
241 }
242
243 - (void)viewWillDisappear:(BOOL)animated {
244 }
245
246 - (void)viewDidDisappear:(BOOL)animated {
247 }
248
249 - (void)didReceiveMemoryWarning {
250         [super didReceiveMemoryWarning];
251 }
252
253
254 @end
255