/* CardInspectorController.m - card inspector controller class for Popup.app Copyright (C) 2004 Rob Burns February,28 2004 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 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. You should have received a copy of the GNU General Public License along with this application; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ #include "CardInspectorController.h" #include "CardModel.h" #include "StackModel.h" #include "ResultGraph.h" #include "Constants.h" static CardInspectorController *singleInstance = nil; @implementation CardInspectorController + (id) sharedInspector { if ( !singleInstance ) { singleInstance = [[CardInspectorController alloc] init]; } return singleInstance; } - (void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver: self]; RELEASE(card); RELEASE(passImage); RELEASE(failImage); [super dealloc]; } - (id) init { if( (self = [super initWithWindowNibName: @"CardInspector" owner: self]) ) { [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(cardChanged:) name: @"CardChangedNotification" object: nil]; [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(stackChanged:) name: @"StackChangedNotification" object: nil]; passImage = RETAIN([NSImage imageNamed: @"Pass"]); failImage = RETAIN([NSImage imageNamed: @"Fail"]); return self; } return nil; } - (void) awakeFromNib { NSArray *grammar, *usage; NSImageCell *tc; int i; // localize the interface [[self window] setTitle: _(@"Card Inspector")]; [[[historyView tableColumnWithIdentifier: @"Slot"] headerCell] setTitle: _(@"Slot")]; [[[historyView tableColumnWithIdentifier: @"Time"] headerCell] setTitle: _(@"Time")]; [passLabel setStringValue: _(@"Pass:")]; [failLabel setStringValue: _(@"Fail:")]; [posLabel setStringValue: _(@"Position:")]; [timeLabel setStringValue: _(@"Next quiz:")]; [[tabButton itemAtIndex: 0] setTitle: _(@"General")]; [[tabButton itemAtIndex: 1] setTitle: _(@"History")]; // fill the Popup buttons grammar = [[NSArray alloc] initWithObjects: _(@"None"), _(@"Noun"), _(@"Verb"), _(@"Adjective"), _(@"Adverb"), _(@"Pronoun"), _(@"Conjunction"), nil]; usage = [[NSArray alloc] initWithObjects: _(@"None"), _(@"Casual"), _(@"Formal"), _(@"Very Formal"), _(@"Polite"), _(@"Rude"), nil]; [grammarPopup removeAllItems]; [usagePopup removeAllItems]; for( i=0; i < [grammar count]; ++i ) { [grammarPopup addItemWithTitle: [grammar objectAtIndex: i]]; } for( i=0; i < [usage count]; ++i ) { [usagePopup addItemWithTitle: [usage objectAtIndex: i]]; } // fix the fonts in the history table on osx #ifdef __APPLE__ [[[historyView tableColumnWithIdentifier: @"Time"] dataCell] setFont: [NSFont systemFontOfSize: [NSFont smallSystemFontSize]]]; #endif tc = AUTORELEASE([[NSImageCell alloc] initImageCell: nil]); [[historyView tableColumnWithIdentifier: @"Result"] setDataCell: tc]; // other stuff [[self window] setFrameAutosaveName: @"Inspector"]; [[self window] setFrameUsingName: @"Inspector"]; [(NSPanel*)[self window] setBecomesKeyOnlyIfNeeded: YES]; [historyView setAutoresizesAllColumnsToFit: YES]; } // ************** // Action methods // ************** - (void) cardChanged: (NSNotification *)aNotification { if( [[aNotification object] isKindOfClass: [CardModel class]] ) { card = [aNotification object]; } [frontField setStringValue: [card front]]; [backField setStringValue: [card back]]; [timeField setStringValue: [[card time] descriptionWithCalendarFormat: @"%a, %b %d, %I:%M %p" timeZone: nil locale: nil]]; if( [card slot] >= 0 ) [posField setStringValue: [card slot]==0 ? _(@"Time Zero") : [[[[stack scheduler] schedule] objectAtIndex:[card slot]-1 ] objectForKey: @"Title"]]; else [posField setStringValue: @""]; [self _updateResults]; [historyView reloadData]; } - (void) stackChanged: (NSNotification *)aNotification { if( [[aNotification object] isKindOfClass: [StackModel class]] ) { stack = [aNotification object]; } } - (void) changeTabView: (id)sender { [tabView selectTabViewItemAtIndex: [sender indexOfSelectedItem]]; } // *************** // Private methods // *************** - (void) _updateResults { int pass = 0; int fail = 0; int passPc = 0; int failPc = 0; int i = 0; for(i = 0; i < [[card history] count]; ++i) { if( [[[[card history] objectAtIndex: i] objectForKey: @"Result"] intValue] == PASSED ) pass++; else fail++; } passPc = (pass!=0 ? (((double)pass/((double)pass + (double)fail)) * 100) : 0); failPc = (fail!=0 ? (((double)fail/((double)pass + (double)fail)) * 100) : 0); if( pass > 0 || fail > 0) { [passField setStringValue: [NSString stringWithFormat: @"%i%%", passPc]]; [failField setStringValue: [NSString stringWithFormat: @"%i%%", failPc]]; } else { [passField setStringValue: @""]; [failField setStringValue: @""]; } [resultGraph setPassValue: pass]; [resultGraph setFailValue: fail]; [resultGraph setNeedsDisplay: YES]; } // **************************** // NSTableView delegate methods // **************************** // ********************************** // NSTableDataSource Protocol methods // ********************************** - (int) numberOfRowsInTableView: (NSTableView *)aTableView { return [[card history] count]; } - (id) tableView: (NSTableView *)aTableView objectValueForTableColumn: (NSTableColumn *)aTableColumn row: (int)rowIndex { if( aTableView == historyView ) { if( [[aTableColumn identifier] isEqualToString: @"Time"] ) return [[[[card history] objectAtIndex: rowIndex] objectForKey: @"Time"] dateWithCalendarFormat: @"%a, %b %d, %I:%M %p" timeZone: nil]; if( [[aTableColumn identifier] isEqualToString: @"Result"] ) { if( [[[[card history] objectAtIndex: rowIndex] objectForKey: @"Result"] intValue] == PASSED ) return passImage; else return failImage; } } return nil; } @end