Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 232 lines (181 sloc) 7.386 kb
fe8f173 @pieter Add custom cell
pieter authored
1 //
2 // PBGitRevisionCell.m
3 // GitX
4 //
5 // Created by Pieter de Bie on 17-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "PBGitRevisionCell.h"
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
10 #import "PBGitRef.h"
fe8f173 @pieter Add custom cell
pieter authored
11
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
12 @implementation NSBezierPath (RoundedRectangle)
13 + (NSBezierPath *)bezierPathWithRoundedRect: (NSRect) aRect cornerRadius: (double) cRadius
14 {
15 double left = aRect.origin.x, bottom = aRect.origin.y, width = aRect.size.width, height = aRect.size.height;
16
17 //now, crop the radius so we don't get weird effects
18 double lesserDim = width < height ? width : height;
19 if ( cRadius > lesserDim / 2 )
20 {
21 cRadius = lesserDim / 2;
22 }
23
24 //these points describe the rectangle as start and stop points of the
25 //arcs making up its corners --points c, e, & g are implicit endpoints of arcs
26 //and are unnecessary
27 NSPoint a = NSMakePoint( 0, cRadius ), b = NSMakePoint( 0, height - cRadius ),
28 d = NSMakePoint( width - cRadius, height ), f = NSMakePoint( width, cRadius ),
29 h = NSMakePoint( cRadius, 0 );
30
31 //these points describe the center points of the corner arcs
32 NSPoint cA = NSMakePoint( cRadius, height - cRadius ),
33 cB = NSMakePoint( width - cRadius, height - cRadius ),
34 cC = NSMakePoint( width - cRadius, cRadius ),
35 cD = NSMakePoint( cRadius, cRadius );
36
37 //start
38 NSBezierPath *bp = [NSBezierPath bezierPath];
39 [bp moveToPoint: a ];
40 [bp lineToPoint: b ];
41 [bp appendBezierPathWithArcWithCenter: cA radius: cRadius startAngle:180 endAngle:90 clockwise: YES];
42 [bp lineToPoint: d ];
43 [bp appendBezierPathWithArcWithCenter: cB radius: cRadius startAngle:90 endAngle:0 clockwise: YES];
44 [bp lineToPoint: f ];
45 [bp appendBezierPathWithArcWithCenter: cC radius: cRadius startAngle:0 endAngle:270 clockwise: YES];
46 [bp lineToPoint: h ];
47 [bp appendBezierPathWithArcWithCenter: cD radius: cRadius startAngle:270 endAngle:180 clockwise: YES];
48 [bp closePath];
49
50 //Transform path to rectangle's origin
51 NSAffineTransform *transform = [NSAffineTransform transform];
52 [transform translateXBy: left yBy: bottom];
53 [bp transformUsingAffineTransform: transform];
54
55 return bp; //it's already been autoreleased
56 }
57 @end
58
fe8f173 @pieter Add custom cell
pieter authored
59 @implementation PBGitRevisionCell
60
52d9402 @pieter First take on graphing
pieter authored
61 @synthesize cellInfo;
bbeedd1 @pieter Rewrite the graphing code
pieter authored
62 -(void) setCellInfo: (PBGraphCellInfo*) info
fe8f173 @pieter Add custom cell
pieter authored
63 {
52d9402 @pieter First take on graphing
pieter authored
64 isReady = YES;
65 cellInfo = info;
66 }
fe8f173 @pieter Add custom cell
pieter authored
67
52d9402 @pieter First take on graphing
pieter authored
68 - (id) initWithCoder: (id) coder
69 {
70 self = [super initWithCoder:coder];
71 if (self != nil) {
72 isReady = NO;
73 }
74 return self;
75 }
fe8f173 @pieter Add custom cell
pieter authored
76
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
77 - (NSArray*) colors
52d9402 @pieter First take on graphing
pieter authored
78 {
ed13cf5 @pieter Grapher: use Tango colors for now
pieter authored
79 return [NSArray arrayWithObjects:
80 [NSColor colorWithCalibratedRed: 0X4e/256.0 green:0X9A/256.0 blue: 0X06/256.0 alpha: 1.0],
81 [NSColor colorWithCalibratedRed: 0X20/256.0 green:0X4A/256.0 blue: 0X87/256.0 alpha: 1.0],
82 [NSColor colorWithCalibratedRed: 0XC4/256.0 green:0XA0/256.0 blue: 0 alpha: 1.0],
83 [NSColor colorWithCalibratedRed: 0X5C/256.0 green:0X35/256.0 blue: 0X66/256.0 alpha: 1.0],
84 [NSColor colorWithCalibratedRed: 0XA4/256.0 green:0X00/256.0 blue: 0X00/256.0 alpha: 1.0],
85 [NSColor colorWithCalibratedRed: 0XCE/256.0 green:0X5C/256.0 blue: 0 alpha: 1.0],
86 nil];
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
87 }
88
a294d91 @pieter Grapher: Add colors to lane
pieter authored
89 - (void) drawLineFromColumn: (int) from toColumn: (int) to inRect: (NSRect) r offset: (int) offset color: (int) c
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
90 {
52d9402 @pieter First take on graphing
pieter authored
91
92 int columnWidth = 10;
93 NSPoint origin = r.origin;
94
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
95 NSPoint source = NSMakePoint(origin.x + columnWidth* from, origin.y + offset);
96 NSPoint center = NSMakePoint( origin.x + columnWidth * to, origin.y + r.size.height * 0.5);
fe8f173 @pieter Add custom cell
pieter authored
97
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
98 // Just use red for now.
ed13cf5 @pieter Grapher: use Tango colors for now
pieter authored
99 NSArray* colors = [self colors];
100 [[colors objectAtIndex: c % [colors count]] set];
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
101
fe8f173 @pieter Add custom cell
pieter authored
102 NSBezierPath * path = [NSBezierPath bezierPath];
103 [path setLineWidth:2];
52d9402 @pieter First take on graphing
pieter authored
104
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
105 [path moveToPoint: source];
52d9402 @pieter First take on graphing
pieter authored
106 [path lineToPoint: center];
fe8f173 @pieter Add custom cell
pieter authored
107 [path stroke];
108
52d9402 @pieter First take on graphing
pieter authored
109 }
fe8f173 @pieter Add custom cell
pieter authored
110
52d9402 @pieter First take on graphing
pieter authored
111 - (void) drawCircleForColumn: (int) c inRect: (NSRect) r
112 {
b4d7816 @pieter Grapher: draw ref names
pieter authored
113 if (!cellInfo.refs)
9bfccb5 @pieter Grapher: add first part of displaying refs
pieter authored
114 [[NSColor blackColor] set];
115 else
116 [[NSColor redColor] set];
117
52d9402 @pieter First take on graphing
pieter authored
118 int columnWidth = 10;
119 NSPoint origin = r.origin;
120 NSPoint columnOrigin = { origin.x + columnWidth * c, origin.y};
fe8f173 @pieter Add custom cell
pieter authored
121
52d9402 @pieter First take on graphing
pieter authored
122 NSRect oval = { columnOrigin.x - 5, columnOrigin.y + r.size.height * 0.5 - 5, 10, 10};
123
124
125 NSBezierPath * path = [NSBezierPath bezierPath];
126 path = [NSBezierPath bezierPathWithOvalInRect:oval];
bbeedd1 @pieter Rewrite the graphing code
pieter authored
127 //[[col objectAtIndex:cellInfo.columns[c].color] set];
52d9402 @pieter First take on graphing
pieter authored
128 [path fill];
fe8f173 @pieter Add custom cell
pieter authored
129
52d9402 @pieter First take on graphing
pieter authored
130 NSRect smallOval = { columnOrigin.x - 3, columnOrigin.y + r.size.height * 0.5 - 3, 6, 6};
fe8f173 @pieter Add custom cell
pieter authored
131 [[NSColor whiteColor] set];
132 path = [NSBezierPath bezierPathWithOvalInRect:smallOval];
52d9402 @pieter First take on graphing
pieter authored
133 [path fill];
134 }
135
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
136 - (NSMutableDictionary*) attributesForRefLabelSelected: (BOOL) selected
137 {
138 NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
139 NSMutableParagraphStyle* style = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
140
141 [style setAlignment:NSCenterTextAlignment];
142 [attributes setObject:style forKey:NSParagraphStyleAttributeName];
143 [attributes setObject:[NSFont fontWithName:@"Helvetica" size:9] forKey:NSFontAttributeName];
144
145 //if (selected)
146 // [attributes setObject:[NSColor alternateSelectedControlTextColor] forKey:NSForegroundColorAttributeName];
147
148 return attributes;
149 }
150
151 - (NSColor*) colorForRef: (PBGitRef*) ref
152 {
153 NSString* type = [ref type];
154 if ([type isEqualToString:@"head"])
155 return [NSColor yellowColor];
156 else if ([type isEqualToString:@"remote"])
157 return [NSColor greenColor];
158 else if ([type isEqualToString:@"tag"])
159 return [NSColor cyanColor];
160
161 return [NSColor yellowColor];
162 }
163
b4d7816 @pieter Grapher: draw ref names
pieter authored
164 - (void) drawRefsInRect: (NSRect*) rect
165 {
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
166 static const float ref_padding = 10.0f;
167 static const float ref_spacing = 2.0f;
168
169 NSRect refRect = (NSRect){rect->origin, rect->size};
170
171 if([self isHighlighted])
172 [[NSColor whiteColor] setStroke];
173 else
174 [[NSColor blackColor] setStroke];
175
176 int index;
177 for (index = 0; index < [cellInfo.refs count]; ++index) {
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
178 PBGitRef* ref = [PBGitRef refFromString:[cellInfo.refs objectAtIndex:index]];
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
179
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
180 NSMutableDictionary* attributes = [self attributesForRefLabelSelected:[self isHighlighted]];
181 NSSize refSize = [[ref shortName] sizeWithAttributes:attributes];
182
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
183 refRect.size.width = refSize.width + ref_padding;
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
184 refRect.size.height = refSize.height;
185 refRect.origin.y += (rect->size.height - refRect.size.height) / 2;
186
187 // Round rects to 0.5 pixels in order to draw only a single pixel
188 refRect.origin.x = round(refRect.origin.x) - 0.5;
189 refRect.origin.y = round(refRect.origin.y) - 0.5;
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
190
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
191 NSBezierPath *border = [NSBezierPath bezierPathWithRoundedRect:refRect cornerRadius: 2.0];
192 [[self colorForRef: ref] set];
193 [border fill];
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
194
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
195 [[ref shortName] drawInRect:refRect withAttributes:attributes];
196 [border stroke];
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
197
939a107 @pieter Grapher: add coloring to ref labels
pieter authored
198 refRect.origin.x += (int)refRect.size.width + ref_spacing;
b4d7816 @pieter Grapher: draw ref names
pieter authored
199 }
f631f54 @ciaran Some minor cleanup of the commit-ref drawing.
ciaran authored
200
201 rect->size.width -= refRect.origin.x - rect->origin.x;
202 rect->origin.x = refRect.origin.x;
b4d7816 @pieter Grapher: draw ref names
pieter authored
203 }
204
52d9402 @pieter First take on graphing
pieter authored
205 - (void) drawWithFrame: (NSRect) rect inView:(NSView *)view
206 {
207 if (!isReady)
208 return [super drawWithFrame:rect inView:view];
209
210 float pathWidth = 10 + 10 * cellInfo.numColumns;
211
212 NSRect ownRect;
213 NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);
214
727e42f @pieter Refactor and rename graph code
pieter authored
215 for (PBGitGraphLine* line in cellInfo.lines) {
bbeedd1 @pieter Rewrite the graphing code
pieter authored
216 if (line.upper == 0)
a294d91 @pieter Grapher: Add colors to lane
pieter authored
217 [self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: ownRect.size.height color: line.colorIndex];
bbeedd1 @pieter Rewrite the graphing code
pieter authored
218 else
a294d91 @pieter Grapher: Add colors to lane
pieter authored
219 [self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: 0 color:line.colorIndex];
52d9402 @pieter First take on graphing
pieter authored
220 }
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
221
52d9402 @pieter First take on graphing
pieter authored
222 [self drawCircleForColumn: cellInfo.position inRect: ownRect];
df49010 @pieter PBGitGrapher: Improve the grapher
pieter authored
223
b4d7816 @pieter Grapher: draw ref names
pieter authored
224 if (cellInfo.refs)
225 [self drawRefsInRect:&rect];
fe8f173 @pieter Add custom cell
pieter authored
226
227 [super drawWithFrame:rect inView:view];
52d9402 @pieter First take on graphing
pieter authored
228 isReady = NO;
fe8f173 @pieter Add custom cell
pieter authored
229 }
230
231 @end
Something went wrong with that request. Please try again.