Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 127 lines (105 sloc) 4.079 kB
f934a90 @sorbits • scrap code which converts NSIndexSet and NSIndexPath to integer arr…
sorbits authored
1 #import "ValueTransformers.h"
2 #import "Dialog.h"
3
4 // ===================================================
5 // = Int Array To Index Path Array Value Transformer =
6 // ===================================================
7
8 @implementation OakIntArrayToIndexPathTransformer
9 + (Class)transformedValueClass { return [NSArray class]; }
10 + (BOOL)allowsReverseTransformation { return YES; }
11
12 + (void)load
13 {
14 id transformer = [self new];
15 [NSValueTransformer setValueTransformer:transformer forName:@"OakIntArrayToIndexPathTransformer"];
16 [transformer release];
17 }
18
19 - (NSIndexPath*)arrayToIndexPath:(NSArray*)anArray
20 {
21 NSIndexPath* indexPath = [[NSIndexPath new] autorelease];
22 enumerate(anArray, id index)
23 indexPath = [indexPath indexPathByAddingIndex:[index intValue]];
24 return indexPath;
25 }
26
27 - (id)transformedValue:(id)value
28 {
29 NSMutableArray* res = [NSMutableArray array];
30 enumerate(value, NSArray* intArray)
31 [res addObject:[self arrayToIndexPath:intArray]];
32 return res;
33 }
34
35 - (NSArray*)indexPathToArray:(NSIndexPath*)anIndexPath
36 {
37 NSMutableArray* array = [NSMutableArray array];
38 for(size_t i = 0; i < [anIndexPath length]; ++i)
39 [array addObject:[NSNumber numberWithUnsignedInt:[anIndexPath indexAtPosition:i]]];
40 return array;
41 }
42
43 - (id)reverseTransformedValue:(id)value
44 {
45 NSMutableArray* array = [NSMutableArray array];
46 enumerate(value, NSIndexPath* indexPath)
47 [array addObject:[self indexPathToArray:indexPath]];
48 return array;
49 }
50 @end
51
52 // ============================================
53 // = Int Array To Index Set Value Transformer =
54 // ============================================
55
56 @implementation OakIntArrayToIndexSetTransformer
57 + (Class)transformedValueClass { return [NSIndexSet class]; }
58 + (BOOL)allowsReverseTransformation { return YES; }
59
60 + (void)load
61 {
62 id transformer = [self new];
63 [NSValueTransformer setValueTransformer:transformer forName:@"OakIntArrayToIndexSetTransformer"];
64 [transformer release];
65 }
66
67 - (id)transformedValue:(id)value
68 {
69 NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet];
70 enumerate(value, NSNumber* integer)
71 [indexSet addIndex:[integer intValue]];
72 return indexSet;
73 }
74
75 - (id)reverseTransformedValue:(id)value
76 {
77 NSMutableArray* array = [NSMutableArray array];
d2b99d4 @sorbits Fix unsigned int → NSUInteger to satisfy compiler.
sorbits authored
78 NSUInteger buf[[value count]];
f934a90 @sorbits • scrap code which converts NSIndexSet and NSIndexPath to integer arr…
sorbits authored
79 [(NSIndexSet*)value getIndexes:buf maxCount:[value count] inIndexRange:nil];
80 for(unsigned int i = 0; i != [value count]; i++)
81 [array addObject:[NSNumber numberWithUnsignedInt:buf[i]]];
82 return array;
83 }
84 @end
0c88a5d @sorbits • add an OakStringToColorTransformer — this can be used as the value …
sorbits authored
85
86 // =============================================
87 // = #RRGGBB String To Color Value Transformer =
88 // =============================================
89
90 static NSColor* NSColorFromString (NSString* aColor)
91 {
92 if(!aColor || [aColor isEqualToString:@""])
93 return nil;
94
95 unsigned int red = 0, green = 0, blue = 0, alpha = 0xFF;
96 if(sscanf([aColor UTF8String], "#%02x%02x%02x%02x", &red, &green, &blue, &alpha) < 3)
97 return nil;
98
99 return [NSColor colorWithCalibratedRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f];
100 }
101
102 static NSString* NSStringFromColor (NSColor* aColor)
103 {
104 if(aColor == nil)
105 return nil;
106
107 aColor = [aColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
108 if([aColor alphaComponent] != 1.0f)
b394c3a @sorbits Fix type and deprecation warnings.
sorbits authored
109 return [NSString stringWithFormat:@"#%02lX%02lX%02lX%02lX", lroundf(255.0f*[aColor redComponent]), lroundf(255.0f*[aColor greenComponent]), lroundf(255.0f*[aColor blueComponent]), lroundf(255.0f*[aColor alphaComponent])];
110 else return [NSString stringWithFormat:@"#%02lX%02lX%02lX", lroundf(255.0f*[aColor redComponent]), lroundf(255.0f*[aColor greenComponent]), lroundf(255.0f*[aColor blueComponent])];
0c88a5d @sorbits • add an OakStringToColorTransformer — this can be used as the value …
sorbits authored
111 }
112
113 @implementation OakStringToColorTransformer
114 + (Class)transformedValueClass { return [NSColor class]; }
115 + (BOOL)allowsReverseTransformation { return YES; }
116
117 - (id)transformedValue:(id)value { return NSColorFromString(value); }
118 - (id)reverseTransformedValue:(id)value { return NSStringFromColor(value); }
119
120 + (void)load
121 {
122 id transformer = [self new];
123 [NSValueTransformer setValueTransformer:transformer forName:@"OakStringToColorTransformer"];
124 [transformer release];
125 }
126 @end
Something went wrong with that request. Please try again.