From 1ca62a0b52244e988f5505dd3cb9cf509cc3552a Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Wed, 6 Mar 2019 10:14:02 +0100 Subject: [PATCH] MacGui: show the start/end seconds range as formatted time strings. --- macosx/Base.lproj/MainWindow.xib | 16 ++++------- macosx/HBRange+UIAdditions.h | 3 ++ macosx/HBRange+UIAdditions.m | 49 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/macosx/Base.lproj/MainWindow.xib b/macosx/Base.lproj/MainWindow.xib index d2d7383e8..59b5a211d 100644 --- a/macosx/Base.lproj/MainWindow.xib +++ b/macosx/Base.lproj/MainWindow.xib @@ -1,8 +1,8 @@ - + - + @@ -228,9 +228,6 @@ - - - @@ -242,10 +239,11 @@ NSNegateBoolean - + + HBTimeInSecondsTransformer @@ -281,9 +279,6 @@ - + + HBTimeInSecondsTransformer diff --git a/macosx/HBRange+UIAdditions.h b/macosx/HBRange+UIAdditions.h index 47b7cde4f..21d323ebb 100644 --- a/macosx/HBRange+UIAdditions.h +++ b/macosx/HBRange+UIAdditions.h @@ -22,3 +22,6 @@ @interface HBTimeTransformer : NSValueTransformer @end + +@interface HBTimeInSecondsTransformer: NSValueTransformer +@end diff --git a/macosx/HBRange+UIAdditions.m b/macosx/HBRange+UIAdditions.m index f574225a3..559d5e26e 100644 --- a/macosx/HBRange+UIAdditions.m +++ b/macosx/HBRange+UIAdditions.m @@ -86,3 +86,52 @@ } @end + +@implementation HBTimeInSecondsTransformer + ++ (Class)transformedValueClass +{ + return [NSString class]; +} + +- (id)transformedValue:(id)value +{ + uint64_t duration = [value integerValue]; + uint64_t hours = duration / 3600; + uint64_t minutes = (duration % 3600) / 60; + uint64_t seconds = duration % 60; + + NSString *result = [NSString stringWithFormat:@"%02llu:%02llu:%02llu", hours, minutes, seconds]; + return result; +} + ++ (BOOL)allowsReverseTransformation +{ + return YES; +} + +- (id)reverseTransformedValue:(id)value +{ + const char *time = [value UTF8String]; + if (time) + { + unsigned hour, minute, second, timeval; + + if (sscanf(time, "%2u:%u:%u", &hour, &minute, &second) < 3) { + if (sscanf(time, "%u:%u:%u", &hour, &minute, &second) < 3) { + return 0; + } + } + + if (second > 60) { + second = 0; + } + + timeval = hour * 60 * 60 + minute * 60 + second; + + return @(timeval); + } + return @"00:00:00"; +} + +@end -- 2.40.0