-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMyQuizController.m
More file actions
478 lines (405 loc) · 15.8 KB
/
Copy pathMyQuizController.m
File metadata and controls
478 lines (405 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
Genius
Copyright (C) 2003-2006 John R Chang
Copyright (C) 2007-2008 Chris Miner
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
http://www.gnu.org/licenses/gpl.txt
*/
#import "MyQuizController.h"
#import "GeniusWelcomePanel.h"
#import "NSString+Similiarity.h"
#import "GeniusStringDiff.h"
#import "GeniusPreferencesController.h"
#import "GeniusAssociationEnumerator.h"
#import "GeniusPair.h"
#import "GeniusAssociation.h"
@implementation MyQuizController
//! Standard NSWindowController initialization.
- (id) init {
self = [super initWithWindowNibName:@"Quiz"];
if (self != nil) {
_newSound = [[NSSound soundNamed:@"Blow"] retain];
_rightSound = [[NSSound soundNamed:@"Hero"] retain];
_wrongSound = [[NSSound soundNamed:@"Basso"] retain];
_visibleCueItem = nil;
_visibleAnswerItem = nil;
_cueItemFont = nil;
_answerItemFont = nil;
_answerTextColor = nil;
}
return self;
}
//! Release sound and fonts. Deallocate memory.
/*! @see #init */
- (void) dealloc
{
[_newSound release];
[_rightSound release];
[_wrongSound release];
[_cueItemFont release];
[_answerItemFont release];
[_enumerator release];
[_screenWindow release];
[super dealloc];
}
//! _visibleCueItem setter.
/*!
Single line items are large size and centered-justified.
Multiple line items are small size and left-justified.
Nil items are grey color; non-nil items are black color.
*/
- (void) _setVisibleCueItem:(GeniusItem *)item
{
BOOL useLargeSize = YES;
if (item)
{
NSArray * lines = [[item stringValue] componentsSeparatedByString:@"\n"];
useLargeSize = ([lines count] <= 1);
}
float fontSize = (useLargeSize ? 18.0 : 13.0);
NSFont * font = [NSFont boldSystemFontOfSize:fontSize];
[self setValue:font forKey:@"cueItemFont"];
//[self setValue:[NSColor blackColor] forKey:@"visibleAnswerTextColor"];
_visibleCueItem = item;
NSTextAlignment alignment = (useLargeSize ? NSCenterTextAlignment : NSLeftTextAlignment);
[cueTextView setAlignment:alignment];
}
//! _visibleAnswerItem setter.
/*!
Single line items are large size (18 pt) and centered-justified.
Multiple line items are small size (13 pt) and left-justified.
Nil items are grey color; non-nil items are black color.
*/
- (void) _setVisibleAnswerItem:(GeniusItem *)item
{
BOOL useLargeSize = YES;
if (item)
{
NSArray * lines = [[item stringValue] componentsSeparatedByString:@"\n"];
useLargeSize = ([lines count] <= 1);
}
float fontSize = (useLargeSize ? 18.0 : 13.0);
NSFont * font = [NSFont systemFontOfSize:fontSize];
[self setValue:font forKey:@"answerItemFont"];
if (item)
[self setValue:[NSColor blackColor] forKey:@"answerTextColor"];
else
[self setValue:[NSColor grayColor] forKey:@"answerTextColor"];
_visibleAnswerItem = item;
NSTextAlignment alignment = (useLargeSize ? NSCenterTextAlignment : NSLeftTextAlignment);
[answerTextView setAlignment:alignment];
}
//! _screenWindow getter.
- (NSWindow*) screenWindow
{
if( ! _screenWindow)
{
NSRect screenRect = [[NSScreen mainScreen] frame];
_screenWindow = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
[_screenWindow setLevel:(NSModalPanelWindowLevel-1)];
[_screenWindow setBackgroundColor:[NSColor blackColor]];
[_screenWindow setAlphaValue:0.5];
[_screenWindow setOpaque:NO];
[_screenWindow setHasShadow:NO];
[_screenWindow setIgnoresMouseEvents:YES];
[_screenWindow setReleasedWhenClosed:NO];
[_screenWindow setHidesOnDeactivate:YES];
}
return _screenWindow;
}
//! _enumerator setter.
- (void) setEnumerator: (GeniusAssociationEnumerator*) anEnumerator
{
[anEnumerator retain];
[_enumerator release];
_enumerator = anEnumerator;
}
//! _enumerator getter.
- (GeniusAssociationEnumerator*) enumerator
{
return _enumerator;
}
//! Puts up optional screening window and takes down other app windows.
/*! After this is run all app windows are hidden and an optional screening window is faded into place. */
- (void) quizSetup
{
// Hide other document windows
NSEnumerator * documentEnumerator = [[NSApp orderedDocuments] objectEnumerator];
NSDocument * document;
while ((document = [documentEnumerator nextObject]))
{
NSEnumerator * windowControllerEnumerator = [[document windowControllers] objectEnumerator];
NSWindowController * windowController;
while ((windowController = [windowControllerEnumerator nextObject]))
[[windowController window] orderOut:nil];
}
// Fade in screen window with cool effect.
if ([[NSUserDefaults standardUserDefaults] boolForKey:GeniusPreferencesQuizUseFullScreenKey])
{
NSAnimation *animation = [[NSAnimation alloc] initWithDuration:0.3 animationCurve:NSAnimationEaseIn];
[animation setDelegate:self];
[animation addProgressMark:0.025];
[[self screenWindow] setAlphaValue:0.0];
[[self screenWindow] orderFront:self];
[animation startAnimation];
[animation release];
}
// Initialize progress indicator
[progressIndicator setMaxValue:[[self enumerator] remainingCount]];
}
//! Takes down optional screening window and puts up other app windows.
- (void) quizTeardown
{
// Take down backdrop window
if ([self screenWindow])
{
NSAnimation *animation = [[NSAnimation alloc] initWithDuration:0.2 animationCurve:NSAnimationEaseOut];
[animation setDelegate:self];
[animation addProgressMark:0.025];
[animation startAnimation];
[animation release];
[[self screenWindow] close];
}
// Show other document windows
NSEnumerator * documentEnumerator = [[NSApp orderedDocuments] objectEnumerator];
NSDocument * document;
while ((document = [documentEnumerator nextObject]))
{
NSArray * windowControllers = [document windowControllers];
[windowControllers makeObjectsPerformSelector:@selector(showWindow:) withObject:nil];
}
[NSApp stopModal];
}
//! presents a single Genius Item from deck for quiz or review. Skips items with no answer.
- (void) runQuizOnce
{
[progressIndicator setDoubleValue:([progressIndicator maxValue] - [[self enumerator] remainingCount])];
// skip associations without answer values.
do {
_currentAssociation = [[self enumerator] nextAssociation];
} while (_currentAssociation && [[_currentAssociation answerItem] stringValue] == nil);
if(_currentAssociation != nil)
{
[associationController setContent:_currentAssociation];
GeniusItem * cueItem = [_currentAssociation cueItem];
[self _setVisibleCueItem:cueItem];
GeniusItem * answerItem = [_currentAssociation answerItem];
[self _setVisibleAnswerItem:nil];
[cueTextView setNeedsDisplay:YES];
[answerTextView setNeedsDisplay:YES];
// Prepare window for reviewing
if ([_currentAssociation isFirstTime])
{
// Prepare window for reviewing
[self _setVisibleAnswerItem:answerItem]; // show the answer for review
[entryField setEnabled:YES];
[entryField setStringValue:[answerItem stringValue]];
[entryField selectText:self];
[evaluationTabView selectTabViewItemWithIdentifier:@"reviewMode"];
[_newSound stop];
if ([[NSUserDefaults standardUserDefaults] boolForKey:GeniusPreferencesUseSoundEffectsKey])
[_newSound play];
}
// Prepare window for learning
else
{
[self _setVisibleAnswerItem:nil]; // hide the answer for learning
[entryField setStringValue:@""];
[entryField setEnabled:YES];
[entryField selectText:self];
[evaluationTabView selectTabViewItemWithIdentifier:@"quizMode"];
}
}
// No associations left, so time to clean up.
else {
[[self window] performClose:nil];
}
}
//! The UI is available. Initializ the quiz.
- (void)windowDidLoad
{
[self quizSetup];
[self runQuizOnce];
}
//! Runs a quiz session for the provided @a enumerator.
/*!
Optionally presents a user tips panel with advice about how to work on memorization. Depending on user preferences
A screen window is displayed to reduce distractions. Other document views are hidden while running this quiz. New
GeniusAssociation instances which have no GeniusAssociation#scoreNumber are presented in review mode.
*/
- (void) runQuiz:(GeniusAssociationEnumerator *)enumerator
{
[self setEnumerator:enumerator];
// Show "Take a moment to slow down..." panel
BOOL result = [[GeniusWelcomePanel sharedWelcomePanel] runModal];
if (result == NO)
return;
[NSApp runModalForWindow:[self window]];
}
//! #_visibleCueItem getter
- (GeniusItem *) visibleAnswerItem
{
return _visibleAnswerItem;
}
//! The user entered text in #entryField or hit the okay button during review.
- (IBAction)handleEntry:(id)sender
{
// First end editing in-progress (from -[NSWindow endEditingFor:] documentation)
BOOL succeed = [[self window] makeFirstResponder:[self window]];
if (!succeed)
[[self window] endEditingFor:nil];
// Handle OK button for reviewed item.
if ([_currentAssociation isFirstTime])
{
[[self enumerator] associationWrong:_currentAssociation];
[self runQuizOnce];
}
// Handle typed entry for learning item.
else
{
// Now show correct answer for review and / or re-enforcement
GeniusItem * answerItem = [_currentAssociation answerItem];
[self _setVisibleAnswerItem:answerItem];
[entryField setEnabled:NO];
[evaluationTabView selectTabViewItemWithIdentifier:@"checkMode"];
NSString * inputString = [entryField stringValue];
NSString * targetString = [answerItem stringValue];
float correctness = 0.0;
int matchingMode = [[NSUserDefaults standardUserDefaults] integerForKey:GeniusPreferencesQuizMatchingModeKey];
switch (matchingMode)
{
case GeniusPreferencesQuizExactMatchingMode:
correctness = (float)[targetString isEqualToString:inputString];
break;
case GeniusPreferencesQuizCaseInsensitiveMatchingMode:
correctness = (float)([targetString localizedCaseInsensitiveCompare:inputString] == NSOrderedSame);
break;
case GeniusPreferencesQuizSimilarMatchingMode:
correctness = [targetString isSimilarToString:inputString];
break;
default:
NSAssert(NO, @"matchingMode");
}
#if DEBUG
NSLog(@"correctness = %f", correctness);
#endif
if (correctness == 1.0)
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:GeniusPreferencesUseSoundEffectsKey])
[_rightSound play];
[[self enumerator] associationRight:_currentAssociation];
[self runQuizOnce];
}
else
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:GeniusPreferencesQuizUseVisualErrorsKey])
{
// Get annotated diff string
NSAttributedString * attrString = [GeniusStringDiff attributedStringHighlightingDifferencesFromString:inputString toString:targetString];
NSMutableAttributedString * mutAttrString = [attrString mutableCopy];
NSMutableParagraphStyle * parStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[parStyle setAlignment:NSCenterTextAlignment];
[mutAttrString addAttribute:NSParagraphStyleAttributeName value:parStyle range:NSMakeRange(0, [attrString length])];
[parStyle release];
[entryField setAttributedStringValue:mutAttrString];
[mutAttrString release];
}
if (correctness > 0.5)
{
// guess was pretty good, so we set default to yesButton
[yesButton setKeyEquivalent:@"\r"];
[noButton setKeyEquivalent:@""];
if ([[NSUserDefaults standardUserDefaults] boolForKey:GeniusPreferencesUseSoundEffectsKey])
[_rightSound play];
}
else
{
// guess was not so good, so we set default to No button
[yesButton setKeyEquivalent:@""];
[noButton setKeyEquivalent:@"\r"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:GeniusPreferencesUseSoundEffectsKey])
[_wrongSound play];
}
}
}
}
//! Upon user review the answer is correct.
- (IBAction)getRightYes:(id)sender
{
// First end editing in-progress (from -[NSWindow endEditingFor:] documentation)
BOOL succeed = [[self window] makeFirstResponder:[self window]];
if (!succeed)
[[self window] endEditingFor:nil];
[[self enumerator] associationRight:_currentAssociation];
[self runQuizOnce];
}
//! Upon user review the answer is wrong.
- (IBAction)getRightNo:(id)sender
{
// First end editing in-progress (from -[NSWindow endEditingFor:] documentation)
BOOL succeed = [[self window] makeFirstResponder:[self window]];
if (!succeed)
[[self window] endEditingFor:nil];
[[self enumerator] associationWrong:_currentAssociation];
[self runQuizOnce];
}
//! Upon user review the answer is skipped the item.
- (IBAction)getRightSkip:(id)sender
{
// First end editing in-progress (from -[NSWindow endEditingFor:] documentation)
BOOL succeed = [[self window] makeFirstResponder:[self window]];
if (!succeed)
[[self window] endEditingFor:nil];
[[self enumerator] associationSkip:_currentAssociation];
[self runQuizOnce];
}
//! Handle keyboard driven input.
/*!
@todo What about handling ending with a press of esc.
*/
- (void) keyDown: (NSEvent *) theEvent
{
NSString * characters = [theEvent characters];
if ([characters isEqualToString:@"y"])
[self getRightYes:self];
else if ([characters isEqualToString:@"n"])
[self getRightNo:self];
else
[super keyDown:theEvent];
}
//! The user can elect to end quiz by closing the window.
- (BOOL) windowShouldClose: (id) sender
{
// First end editing in-progress (from -[NSWindow endEditingFor:] documentation)
BOOL succeed = [[self window] makeFirstResponder:[self window]];
if (!succeed)
[[self window] endEditingFor:nil];
return YES;
}
//! The user elected to close the quiz window.
- (void) windowWillClose: (NSNotification *) aNotification
{
[self quizTeardown];
}
@end
//! Support for animated fade in and out of quiz backdrop window
@implementation MyQuizController(NSAnimationDelegate)
//! Handles fade in and out of quiz backdrop window.
/*!
We're set up in runQuiz:cumulativeTime: as the delegate of an NSAnimation. We use the progress
of the animation to determine the current alpha transparency of the quiz backdrop window.
*/
- (void)animation:(NSAnimation*)animation didReachProgressMark:(NSAnimationProgress)progress
{
float alpha = [animation currentValue] * 0.5;
[[self screenWindow] setAlphaValue:alpha];
[animation addProgressMark:0.1];
}
@end