2 // ScanViewController.m
5 // Created by Christian Brunschen on 24/06/2008.
7 * Copyright 2008 ZXing authors
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #import "ScanViewController.h"
23 #import "ResultAction.h"
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
32 @implementation ScanViewController
36 @synthesize dateFormatter;
38 #define FONT_NAME @"TimesNewRomanPSMT"
41 - (id)initWithResult:(ParsedResult *)r forScan:(Scan *)s {
42 if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
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];
55 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
56 return [[result actions] count] ? 2 : 1;
60 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
65 return [[result actions] count];
71 - (UITableViewCell *)cellWithIdentifier:(NSString *)identifier inTableView:(UITableView *)tableView {
72 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
74 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:identifier] autorelease];
79 - (UITableViewCell *)titleCellInTableView:(UITableView *)tableView {
80 static NSString *TitleIdentifier = @"ScanViewTitleIdentifier";
81 return [self cellWithIdentifier:TitleIdentifier inTableView:tableView];
84 - (UITableViewCell *)datetimeCellInTableView:(UITableView *)tableView {
85 static NSString *DatetimeIdentifier = @"ScanViewDatetimeIdentifier";
86 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DatetimeIdentifier];
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;
97 - (UITableViewCell *)bodyCellInTableView:(UITableView *)tableView {
98 static NSString *BodyIdentifier = @"ScanViewBodyIdentifier";
99 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:BodyIdentifier];
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];
113 - (UITableViewCell *)buttonCellInTableView:(UITableView *)tableView {
114 static NSString *ButtonIdentifier = @"ScanViewButtonIdentifier";
115 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ButtonIdentifier];
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];
131 #define TEXT_VIEW_HEIGHT 330.0
133 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
134 if (indexPath.section == 0) {
135 if (indexPath.row == 0) {
137 } else if (indexPath.row == 1) {
138 CGSize size = [[result stringForDisplay] sizeWithFont:bodyFont constrainedToSize:CGSizeMake(280.0, TEXT_VIEW_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
140 NSLog(@"text size = %f", size.height);
142 return fminf(TEXT_VIEW_HEIGHT, fmaxf(44, size.height + 24));
143 } else if (indexPath.row == 2) {
147 return tableView.rowHeight;
150 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
151 UITableViewCell *cell;
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]];
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];
179 - (void)performAction:(ResultAction *)action {
180 [action performActionWithController:self shouldConfirm:NO];
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];
191 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
193 if (editingStyle == UITableViewCellEditingStyleDelete) {
195 if (editingStyle == UITableViewCellEditingStyleInsert) {
200 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
205 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
209 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
213 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
214 if (indexPath.section != 1) {
225 [dateFormatter release];
230 - (void)viewDidLoad {
235 - (void)viewWillAppear:(BOOL)animated {
236 [super viewWillAppear:animated];
239 - (void)viewDidAppear:(BOOL)animated {
240 [super viewDidAppear:animated];
243 - (void)viewWillDisappear:(BOOL)animated {
246 - (void)viewDidDisappear:(BOOL)animated {
249 - (void)didReceiveMemoryWarning {
250 [super didReceiveMemoryWarning];