root/Nuku/trunk/Session.m

Revision 4178, 7.1 kB (checked in by morris, 2 years ago)

Add prefix header

  • Property svn:keywords set to author date id revision
Line 
1 /* $Id$ */
2
3 /*
4  * Copyright (c) 2000-2006 Axel Andersson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28  
29 #import "Session.h"
30 #import "Settings.h"
31 #import "Controller.h"
32
33 @implementation Session
34
35 - (id)init {
36         self = [super init];
37        
38         _settings = [[Settings alloc] init];
39        
40         return self;
41 }
42
43
44
45 - (void)dealloc {
46         [_settings release];
47        
48         [super dealloc];
49 }
50
51
52
53 #pragma mark -
54
55 - (void)awakeFromNib {
56         // --- start with a new session
57         [self newSession:self];
58 }
59
60
61
62 /*
63         Called when the user enters text in the comment box. Prevents more than 255
64         characters from passing through.
65 */
66
67 - (void)textDidChange:(NSNotification *)notification {
68         NSString                *string;
69        
70         string = [viewText string];
71        
72         if([string length] > 255)
73                 [viewText setString:[string substringToIndex:255]];
74 }
75
76
77
78 #pragma mark-
79
80 /*
81         Opens a new session window and sets the controls from prefs.
82 */
83
84 - (IBAction)newSession:(id)sender {
85         _done = NO;
86        
87         // --- set our default session
88         switch([[Settings objectForKey:kDefaultsSession] intValue]) {
89                 case kDefaultsSessionFree:
90                         // --- disable these in the free mode
91                         [fieldTime setEnabled:false];
92                         [fieldKana setEnabled:false];
93                        
94                         [stepperTime setEnabled:false];
95                         [stepperKana setEnabled:false];
96
97                         [selectSession selectItemAtIndex:
98                                 [selectSession indexOfItemWithTag:kDefaultsSessionFree]];
99                         break;
100                
101                 case kDefaultsSessionLimited:
102                         [selectSession selectItemAtIndex:
103                                 [selectSession indexOfItemWithTag:kDefaultsSessionLimited]];
104                         break;
105         }
106        
107         // --- set these from prefs
108         if([[Settings objectForKey:kDefaultsMouseDownOnIncorrect] boolValue])
109                 [boxAwait setState:NSOnState];
110         if([[Settings objectForKey:kDefaultsLockSettings] boolValue])
111                 [boxLock setState:NSOnState];
112        
113         [stepperTime setIntValue:[[Settings objectForKey:kDefaultsTimeLimit] intValue]];
114         [self stepTime:stepperTime];
115         [stepperKana setIntValue:[[Settings objectForKey:kDefaultsKanaLimit] intValue]];
116         [self stepKana:stepperKana];
117        
118         // --- show the session window
119         [windowSession center];
120         [windowSession makeKeyAndOrderFront:self];
121 }
122
123
124
125 /*
126         Starts off a new session.
127 */
128
129 - (IBAction)start:(id)sender {
130         // --- save all settings in prefs
131         [Settings setObject:[NSNumber numberWithInt:[[selectSession selectedItem] tag]]
132                           forKey:kDefaultsSession];
133
134         [Settings setObject:[NSNumber numberWithBool:[boxAwait state]]
135                           forKey:kDefaultsMouseDownOnIncorrect];
136
137         [Settings setObject:[NSNumber numberWithBool:[boxLock state]]
138                           forKey:kDefaultsLockSettings];
139
140         [Settings setObject:[NSNumber numberWithInt:[stepperTime intValue]]
141                           forKey:kDefaultsTimeLimit];
142
143         [Settings setObject:[NSNumber numberWithInt:[stepperKana intValue]]
144                           forKey:kDefaultsKanaLimit];
145        
146         // --- close the session window
147         [windowSession close];
148        
149         // --- open the tutor and start off whatever we're doing
150         [controllerOutlet openTutor];
151         [controllerOutlet start];
152 }
153
154
155
156 #pragma mark -
157
158 /*
159         Called when the user switches session type in the popup menu.
160 */
161
162 - (IBAction)switchType:(id)sender {
163         // --- determine selected item and what to do with it
164         switch([[sender selectedItem] tag]) {
165                 case kDefaultsSessionFree:
166                         [fieldTime setEnabled:false];
167                         [fieldKana setEnabled:false];
168                        
169                         [stepperTime setEnabled:false];
170                         [stepperKana setEnabled:false];
171                         break;
172                
173                 case kDefaultsSessionLimited:
174                         [fieldTime setEnabled:YES];
175                         [fieldKana setEnabled:YES];
176                        
177                         [stepperTime setEnabled:YES];
178                         [stepperKana setEnabled:YES];
179
180                         [fieldTime selectText:self];
181                         break;
182         }
183 }
184
185
186
187 /*
188         Called when the user has entered a value in the time box, convert it to seconds
189         and store in the stepper.
190 */
191
192 - (IBAction)enterTime:(id)sender {
193         NSArray         *values;
194         int                     minutes, seconds, result;
195        
196         values          = [[sender stringValue] componentsSeparatedByString:@":"];
197         minutes         = [[values objectAtIndex:0] intValue];
198         seconds         = [[values objectAtIndex:1] intValue];
199         result          = seconds + (minutes * 60);
200        
201         [stepperTime setIntValue:result];
202 }
203
204
205
206 /*
207         Called when the user has entered a value in the kana box, store in the stepper.
208 */
209
210 - (IBAction)enterKana:(id)sender {
211         [stepperKana takeIntValueFrom:sender];
212 }
213
214
215
216 /*
217         Called when the user clicks the time stepper.
218 */
219
220 - (IBAction)stepTime:(id)sender {
221         int             value, minutes, seconds;
222        
223         value   = [sender intValue];
224         minutes = value / 60;
225         seconds = value - (minutes * 60);
226        
227         [fieldTime setStringValue:[NSString stringWithFormat:@"%d:%d", minutes, seconds]];
228 }
229
230
231
232 /*
233         Called when the user clicks the kana stepper.
234 */
235
236 - (IBAction)stepKana:(id)sender {
237         [fieldKana takeIntValueFrom:sender];
238 }
239
240
241
242 #pragma mark -
243
244 /*
245         Show our report card window.
246 */
247
248 - (void)showReport {
249         int                     answered, total, missed, correct, incorrect;
250         int                     time, minutes, seconds;
251         float           percent;
252        
253         // --- get the result values
254         answered        = [controllerOutlet getQuestions];
255         total           = [[Settings objectForKey:kDefaultsKanaLimit] intValue];
256         missed          = total - answered;
257        
258         if(missed < 0)
259                 missed  = 0;
260        
261         correct         = [controllerOutlet getCorrect];
262         incorrect       = answered - correct;
263
264         if(total == 0)
265                 percent = 0;
266         else
267                 percent = ((float) correct / (float) total) * 100;
268        
269         time            = [controllerOutlet getTime];
270         minutes         = time / 60;
271         seconds         = time - (minutes * 60);
272
273         // --- set the result values
274         [reportAnswered setIntValue:answered];
275         [reportMissed setIntValue:missed];
276         [reportCorrect setIntValue:correct];
277         [reportIncorrect setIntValue:incorrect];
278         [reportPercent setStringValue:[NSString stringWithFormat:@"%.1f%%", percent]];
279         [reportTime setStringValue:[NSString stringWithFormat:@"%d:%d", minutes, seconds]];
280
281         // --- show the window
282         [windowReport center];
283         [windowReport makeKeyAndOrderFront:self];
284 }
285
286
287
288
289 /*
290         Called when the user clicks Print or selects Print from the File menu. Prints
291         our special content view.
292 */
293
294 - (IBAction)print:(id)sender {
295         if([windowReport isKeyWindow])
296                 [viewPrint print:self];
297         else
298                 [[NSApp keyWindow] print:self];
299 }
300
301
302
303 #pragma mark -
304
305 /*
306         Return whether the session has finished or not.
307 */
308
309 - (BOOL)isDone {
310         return _done;
311 }
312
313
314
315 /*
316         Mark the session as finished.
317 */
318
319 - (void)done {
320         _done = YES;
321 }
322
323 @end
Note: See TracBrowser for help on using the browser.