diff options
author | ritsuka <[email protected]> | 2015-04-30 16:04:37 +0000 |
---|---|---|
committer | ritsuka <[email protected]> | 2015-04-30 16:04:37 +0000 |
commit | 96bf5cafd9e7891d2ad5741543e6e1e76ae52f77 (patch) | |
tree | 36e14374381e55963fd603bc32c2079fea9d6170 | |
parent | 1c59ab0f6e1a92c0f79629658302936796c6f7ed (diff) |
MacGui: add a new formatter to convert a hb_state_t to a textual representation.
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@7134 b64f7644-9d1e-0410-96f1-a4d463321fa5
-rw-r--r-- | macosx/HBController.m | 21 | ||||
-rw-r--r-- | macosx/HBPreviewGenerator.m | 57 | ||||
-rw-r--r-- | macosx/HBQueueController.m | 130 | ||||
-rw-r--r-- | macosx/HBStateFormatter.h | 41 | ||||
-rw-r--r-- | macosx/HBStateFormatter.m | 155 | ||||
-rw-r--r-- | macosx/HandBrake.xcodeproj/project.pbxproj | 6 |
6 files changed, 260 insertions, 150 deletions
diff --git a/macosx/HBController.m b/macosx/HBController.m index f25b5607d..07086cd2d 100644 --- a/macosx/HBController.m +++ b/macosx/HBController.m @@ -28,6 +28,7 @@ #import "HBCore.h" #import "HBJob.h" +#import "HBStateFormatter.h" @interface HBController () <HBPresetsViewControllerDelegate, HBTitleSelectionDelegate> @@ -678,29 +679,17 @@ int hb_num_previews = [[[NSUserDefaults standardUserDefaults] objectForKey:@"PreviewsNumber"] intValue]; int min_title_duration_seconds = [[[NSUserDefaults standardUserDefaults] objectForKey:@"MinTitleScanSeconds"] intValue]; + HBStateFormatter *formatter = [[HBStateFormatter alloc] init]; + [self.core scanURL:scanURL titleIndex:scanTitleNum previews:hb_num_previews minDuration:min_title_duration_seconds progressHandler:^(HBState state, hb_state_t hb_state) { - #define p hb_state.param.scanning - if (p.preview_cur) - { - fSrcDVD2Field.stringValue = [NSString stringWithFormat: - NSLocalizedString( @"Scanning title %d of %d, preview %d…", @"" ), - p.title_cur, p.title_count, - p.preview_cur]; - } - else - { - fSrcDVD2Field.stringValue = [NSString stringWithFormat: - NSLocalizedString( @"Scanning title %d of %d…", @"" ), - p.title_cur, p.title_count]; - } + fSrcDVD2Field.stringValue = [formatter stateToString:hb_state title:nil]; fScanIndicator.hidden = NO; fScanHorizontalLine.hidden = YES; - fScanIndicator.doubleValue = 100.0 * p.progress; - #undef p + fScanIndicator.doubleValue = [formatter stateToPercentComplete:hb_state]; } completionHandler:^(BOOL success) { diff --git a/macosx/HBPreviewGenerator.m b/macosx/HBPreviewGenerator.m index 75d2b0ab8..6719b54d2 100644 --- a/macosx/HBPreviewGenerator.m +++ b/macosx/HBPreviewGenerator.m @@ -10,6 +10,7 @@ #import "HBCore.h" #import "HBJob.h" +#import "HBStateFormatter.h" #import "HBPicture+UIAdditions.h" @interface HBPreviewGenerator () @@ -180,46 +181,28 @@ int level = [[[NSUserDefaults standardUserDefaults] objectForKey:@"LoggingLevel"] intValue]; self.core = [[HBCore alloc] initWithLogLevel:level name:@"PreviewCore"]; + HBStateFormatter *formatter = [[HBStateFormatter alloc] init]; + formatter.twoLines = NO; + formatter.showPassNumber = NO; + // start the actual encode [self.core encodeJob:job progressHandler:^(HBState state, hb_state_t hb_state) { - switch (state) { - case HBStateWorking: - { - NSMutableString *info = [NSMutableString stringWithFormat: @"Encoding preview: %.2f %%", 100.0 * hb_state.param.working.progress]; - - if (hb_state.param.working.seconds > -1) - { - [info appendFormat:@" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)", - hb_state.param.working.rate_cur, hb_state.param.working.rate_avg, hb_state.param.working.hours, - hb_state.param.working.minutes, hb_state.param.working.seconds]; - } - - double progress = 100.0 * hb_state.param.working.progress; - - [self.delegate updateProgress:progress info:info]; - break; - } - case HBStateMuxing: - [self.delegate updateProgress:100.0 info:@"Muxing Preview…"]; - break; - - default: - break; - } - } - completionHandler:^(BOOL success) { - // Encode done, call the delegate and close libhb handle - if (success) - { - [self.delegate didCreateMovieAtURL:destURL]; - } - else - { - [self.delegate didCancelMovieCreation]; - } - self.core = nil; - }]; + [self.delegate updateProgress:[formatter stateToPercentComplete:hb_state] * 100 + info:[formatter stateToString:hb_state title:@"preview"]]; + } + completionHandler:^(BOOL success) { + // Encode done, call the delegate and close libhb handle + if (success) + { + [self.delegate didCreateMovieAtURL:destURL]; + } + else + { + [self.delegate didCancelMovieCreation]; + } + self.core = nil; + }]; return YES; } diff --git a/macosx/HBQueueController.m b/macosx/HBQueueController.m index 54d154a79..a34e1c71f 100644 --- a/macosx/HBQueueController.m +++ b/macosx/HBQueueController.m @@ -17,6 +17,8 @@ #import "HBJob.h" #import "HBJob+UIAdditions.h" +#import "HBStateFormatter.h" + #import "HBDistributedArray.h" #import "HBDockTile.h" @@ -496,6 +498,8 @@ */ - (void)performScan:(NSURL *)scanURL titleIdx:(NSInteger)index { + HBStateFormatter *formatter = [[HBStateFormatter alloc] init]; + // Only scan 10 previews before an encode - additional previews are // only useful for autocrop and static previews, which are already taken care of at this point [self.core scanURL:scanURL @@ -503,13 +507,7 @@ previews:10 minDuration:0 progressHandler:^(HBState state, hb_state_t hb_state) { - NSMutableString *status = [NSMutableString stringWithFormat: - NSLocalizedString( @"Queue Scanning title %d of %d…", @"" ), - hb_state.param.scanning.title_cur, hb_state.param.scanning.title_count]; - if (hb_state.param.scanning.preview_cur) - { - [status appendFormat:@", preview %d…", hb_state.param.scanning.preview_cur]; - } + NSString *status = [formatter stateToString:hb_state title:nil]; self.progressTextField.stringValue = status; [self.controller setQueueInfo:status progress:0 hidden:NO]; @@ -542,105 +540,43 @@ // Reset the title in the job. self.currentJob.title = self.core.titles[0]; + HBStateFormatter *converter = [[HBStateFormatter alloc] init]; + NSString *destinationName = self.currentJob.destURL.lastPathComponent; + // We should be all setup so let 'er rip [self.core encodeJob:self.currentJob progressHandler:^(HBState state, hb_state_t hb_state) { - NSMutableString *string = nil; - CGFloat progress = 0; - #define p hb_state.param.working - switch (state) - { - case HBStateSearching: - { - string = [NSMutableString stringWithFormat: - NSLocalizedString(@"Searching for start point… : %.2f %%", @""), - 100.0 * p.progress]; - - if (p.seconds > -1) - { - [string appendFormat:NSLocalizedString(@" (ETA %02dh%02dm%02ds)", @"" ), p.hours, p.minutes, p.seconds]; - } + NSString *string = [converter stateToString:hb_state title:destinationName]; + CGFloat progress = [converter stateToPercentComplete:hb_state]; - break; - } - case HBStateWorking: + if (state == HBStateWorking) + { + // Update dock icon + if (self.dockIconProgress < 100.0 * progress) { - if (p.pass_id == HB_PASS_SUBTITLE) - { - string = [NSMutableString stringWithFormat: - NSLocalizedString(@"Encoding: %@ \nPass %d %@ of %d, %.2f %%", nil), - self.currentJob.destURL.lastPathComponent, - p.pass, - NSLocalizedString(@"(subtitle scan)", nil), - p.pass_count, 100.0 * p.progress]; - } + // ETA format is [XX]X:XX:XX when ETA is greater than one hour + // [X]X:XX when ETA is greater than 0 (minutes or seconds) + // When these conditions doesn't applied (eg. when ETA is undefined) + // we show just a tilde (~) + + #define p hb_state.param.working + NSString *etaStr; + if (p.hours > 0) + etaStr = [NSString stringWithFormat:@"%d:%02d:%02d", p.hours, p.minutes, p.seconds]; + else if (p.minutes > 0 || p.seconds > 0) + etaStr = [NSString stringWithFormat:@"%d:%02d", p.minutes, p.seconds]; else - { - string = [NSMutableString stringWithFormat: - NSLocalizedString(@"Encoding: %@ \nPass %d of %d, %.2f %%", nil), - self.currentJob.destURL.lastPathComponent, - p.pass, p.pass_count, 100.0 * p.progress]; - } - - if (p.seconds > -1) - { - if (p.rate_cur > 0.0) - { - [string appendFormat: - NSLocalizedString(@" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)", @""), - p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds]; - } - else - { - [string appendFormat: - NSLocalizedString(@" (ETA %02dh%02dm%02ds)", @""), - p.hours, p.minutes, p.seconds]; - } - } - - progress = (p.progress + p.pass - 1) / p.pass_count; - - // Update dock icon - if (self.dockIconProgress < 100.0 * progress) - { - // ETA format is [XX]X:XX:XX when ETA is greater than one hour - // [X]X:XX when ETA is greater than 0 (minutes or seconds) - // When these conditions doesn't applied (eg. when ETA is undefined) - // we show just a tilde (~) - - NSString *etaStr = @""; - if (p.hours > 0) - etaStr = [NSString stringWithFormat:@"%d:%02d:%02d", p.hours, p.minutes, p.seconds]; - else if (p.minutes > 0 || p.seconds > 0) - etaStr = [NSString stringWithFormat:@"%d:%02d", p.minutes, p.seconds]; - else - etaStr = @"~"; - - [self.dockTile updateDockIcon:progress withETA:etaStr]; - - self.dockIconProgress += dockTileUpdateFrequency; - } - - break; - } - case HBStateMuxing: - { - string = [NSMutableString stringWithString:NSLocalizedString(@"Muxing…", @"")]; + etaStr = @"~"; + #undef p - // Update dock icon - [self.dockTile updateDockIcon:1.0 withETA:@""]; - - break; + [self.dockTile updateDockIcon:progress withETA:etaStr]; + self.dockIconProgress += dockTileUpdateFrequency; } - case HBStatePaused: - { - string = [NSMutableString stringWithString:NSLocalizedString(@"Paused", @"")]; - break; - } - default: - break; } - #undef p + else if (state == HBStateMuxing) + { + [self.dockTile updateDockIcon:1.0 withETA:@""]; + } // Update text field self.progressTextField.stringValue = string; diff --git a/macosx/HBStateFormatter.h b/macosx/HBStateFormatter.h new file mode 100644 index 000000000..f5b97f2fb --- /dev/null +++ b/macosx/HBStateFormatter.h @@ -0,0 +1,41 @@ +/* HBStateFormatter.h + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.fr/>. + It may be used under the terms of the GNU General Public License. */ + +#import <Foundation/Foundation.h> +#include "hb.h" + +/** + * Instances of HBStateFormatter format and conver a hb_state_t struct to a textual representation. + */ +@interface HBStateFormatter : NSObject + +/** + * Returns a string containing the formatted value of the provided hb_state_t struct. + * + * @param s hb_state_t + * @param title the title of the current job + */ +- (NSString *)stateToString:(hb_state_t)s title:(NSString *)title; + +/** + * Returns a CGFloat containing the completion percent. + * the CGFloat range is [0,1] + * + * @param s hb_state_t + */ +- (CGFloat)stateToPercentComplete:(hb_state_t)s; + +/** + * Break the output string in two lines. + */ +@property (nonatomic, readwrite) BOOL twoLines; + +/** + * Shows the pass number in the output string + */ +@property (nonatomic, readwrite) BOOL showPassNumber; + +@end diff --git a/macosx/HBStateFormatter.m b/macosx/HBStateFormatter.m new file mode 100644 index 000000000..13c987a50 --- /dev/null +++ b/macosx/HBStateFormatter.m @@ -0,0 +1,155 @@ +/* HBStateFormatter.m + + This file is part of the HandBrake source code. + Homepage: <http://handbrake.fr/>. + It may be used under the terms of the GNU General Public License. */ + +#import "HBStateFormatter.h" + +@implementation HBStateFormatter + +- (instancetype)init +{ + self = [super init]; + if (self) + { + _twoLines = YES; + _showPassNumber = YES; + } + return self; +} + +- (NSString *)stateToString:(hb_state_t)s title:(NSString *)title +{ + NSMutableString *string = [NSMutableString string]; + + switch (s.state) + { + #define p s.param.working + + case HB_STATE_SEARCHING: + { + [string appendFormat: + NSLocalizedString(@"Searching for start point… : %.2f %%", nil), + 100.0 * p.progress]; + + if (p.seconds > -1) + { + [string appendFormat:NSLocalizedString(@" (ETA %02dh%02dm%02ds)", nil), p.hours, p.minutes, p.seconds]; + } + + break; + } + + case HB_STATE_WORKING: + { + [string appendFormat:NSLocalizedString(@"Encoding %@ ", nil), title]; + + if (_twoLines) + { + [string appendString:@"\n"]; + } + + if (_showPassNumber) + { + if (p.pass_id == HB_PASS_SUBTITLE) + { + [string appendFormat: + NSLocalizedString(@"Pass %d %@ of %d, %.2f %%", nil), + p.pass, + NSLocalizedString(@"(subtitle scan)", nil), + p.pass_count, 100.0 * p.progress]; + } + else + { + [string appendFormat: + NSLocalizedString(@"Pass %d of %d, %.2f %%", nil), + p.pass, p.pass_count, 100.0 * p.progress]; + } + } + + if (p.seconds > -1) + { + if (p.rate_cur > 0.0) + { + [string appendFormat: + NSLocalizedString(@" (%.2f fps, avg %.2f fps, ETA %02dh%02dm%02ds)", nil), + p.rate_cur, p.rate_avg, p.hours, p.minutes, p.seconds]; + } + else + { + [string appendFormat: + NSLocalizedString(@" (ETA %02dh%02dm%02ds)", nil), + p.hours, p.minutes, p.seconds]; + } + } + + break; + } + + case HB_STATE_MUXING: + { + [string appendString:NSLocalizedString(@"Muxing…", nil)]; + break; + } + + case HB_STATE_PAUSED: + { + [string appendString:NSLocalizedString(@"Paused", nil)]; + break; + } + + #undef p + case HB_STATE_SCANNING: + { + #define p s.param.scanning + if (p.preview_cur) + { + [string appendFormat: + NSLocalizedString(@"Scanning title %d of %d, preview %d…", nil), + p.title_cur, p.title_count, + p.preview_cur]; + } + else + { + [string appendFormat: + NSLocalizedString(@"Scanning title %d of %d…", nil), + p.title_cur, p.title_count]; + } + #undef p + break; + } + + default: + break; + } + + return string; +} + +- (CGFloat)stateToPercentComplete:(hb_state_t)s +{ + CGFloat progress = 0; + + switch (s.state) + { + case HB_STATE_WORKING: + #define p s.param.working + progress = (p.progress + p.pass - 1) / p.pass_count; + #undef p + + break; + + case HB_STATE_SCANNING: + #define p s.param.scanning + progress = p.progress; + #undef p + + default: + break; + } + + return progress; +} + +@end diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj index 93782a5c3..e595f4c30 100644 --- a/macosx/HandBrake.xcodeproj/project.pbxproj +++ b/macosx/HandBrake.xcodeproj/project.pbxproj @@ -156,6 +156,7 @@ A9706CB71AC1437800BAEAA8 /* HBExceptionAlertController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9706CB61AC1437800BAEAA8 /* HBExceptionAlertController.m */; }; A9706CBA1AC1452800BAEAA8 /* ExceptionAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9706CB81AC1452800BAEAA8 /* ExceptionAlert.xib */; }; A971281F1A2C75180088C076 /* HBTitle.m in Sources */ = {isa = PBXBuildFile; fileRef = A971281E1A2C75180088C076 /* HBTitle.m */; }; + A975C08E1AE8C5270061870D /* HBStateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = A975C08D1AE8C5270061870D /* HBStateFormatter.m */; }; A983494F1A9A64B80059CB94 /* presets.plist in Resources */ = {isa = PBXBuildFile; fileRef = A983494E1A9A64B80059CB94 /* presets.plist */; }; A98C29C41977B10600AF5DED /* HBLanguagesSelection.m in Sources */ = {isa = PBXBuildFile; fileRef = A98C29C31977B10600AF5DED /* HBLanguagesSelection.m */; }; A9906B2C1A710920001D82D5 /* HBQueueController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9906B2B1A710920001D82D5 /* HBQueueController.m */; }; @@ -431,6 +432,8 @@ A9706CB91AC1452800BAEAA8 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = ExceptionAlert.xib; sourceTree = "<group>"; }; A971281D1A2C75180088C076 /* HBTitle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBTitle.h; sourceTree = "<group>"; }; A971281E1A2C75180088C076 /* HBTitle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBTitle.m; sourceTree = "<group>"; }; + A975C08C1AE8C5270061870D /* HBStateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBStateFormatter.h; sourceTree = "<group>"; }; + A975C08D1AE8C5270061870D /* HBStateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBStateFormatter.m; sourceTree = "<group>"; }; A983494E1A9A64B80059CB94 /* presets.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = presets.plist; sourceTree = "<group>"; }; A98C29C21977B10600AF5DED /* HBLanguagesSelection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBLanguagesSelection.h; sourceTree = "<group>"; }; A98C29C31977B10600AF5DED /* HBLanguagesSelection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBLanguagesSelection.m; sourceTree = "<group>"; }; @@ -972,6 +975,8 @@ A9160A331AE7A165009A7818 /* HBCodingUtilities.h */, A9160A341AE7A165009A7818 /* HBCodingUtilities.m */, A997D8EB1A4ABB0900E19B6F /* HBPresetCoding.h */, + A975C08C1AE8C5270061870D /* HBStateFormatter.h */, + A975C08D1AE8C5270061870D /* HBStateFormatter.m */, ); name = Core; sourceTree = "<group>"; @@ -1334,6 +1339,7 @@ 46AB433515F98A2B009C0961 /* DockTextField.m in Sources */, A9DEC87F1A23DF6F00C79B48 /* HBJob.m in Sources */, A9E2FD271A21BC4A000E8D3F /* HBAddPresetController.m in Sources */, + A975C08E1AE8C5270061870D /* HBStateFormatter.m in Sources */, A9D488A51996270300E9B1BA /* HBTreeNode.m in Sources */, A9597A2A1A49749D00007771 /* HBRange+UIAdditions.m in Sources */, ); |