PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/GraphDraw_Mixed/GDGraphView.m

http://github.com/jsyedidia/nugraphdraw
Objective C | 68 lines | 55 code | 8 blank | 5 comment | 4 complexity | 1eb42a910534ca13cf5df8faf9b48d02 MD5 | raw file
 1//  Created by Jonathan Yedidia on 5/31/08.
 2//  Copyright 2008. All rights reserved.
 3//  Objective-C code based on Aaron Hillegass'
 4//  "GraphLaughs" PyObjC application, available online at
 5//  http://weblog.bignerdranch.com/pythonex/GraphLaughs.tgz
 6
 7#import "GDGraphView.h"
 8#import "GDGraph.h"
 9#import "GDNode.h"
10#import "GDEdge.h"
11
12@implementation GDGraphView
13
14- (id)initWithFrame:(NSRect)frameRect
15{
16	if ((self = [super initWithFrame:frameRect]) != nil) {
17        NSRect b = [self bounds];
18        [self translateOriginToPoint:NSMakePoint(b.origin.x + b.size.width/2.0,
19                                                 b.origin.y + b.size.height/2.0)];
20	}
21	return self;
22}
23
24- (void)setGraph:(GDGraph *)g
25{
26    [g retain];
27    [graph release];
28    graph = g;
29    [self setNeedsDisplay:YES];
30}
31
32- (void)drawRect:(NSRect)rect
33{
34    NSRect b = [self bounds];
35    [[NSColor darkGrayColor] set];
36    [NSBezierPath fillRect:b];
37    [self translateOriginToPoint:NSMakePoint(b.origin.x + b.size.width/2.0,
38                                             b.origin.y + b.size.height/2.0)];
39        
40    [[NSColor redColor] set];
41    NSArray *nodes = [graph nodes];
42    int i, count;
43    count = [nodes count];
44    NSRect r = NSMakeRect(0,0, 6, 6);
45    for (i = 0; i < count; i++) {
46        GDNode *n = [nodes objectAtIndex:i];
47        NSPoint np = [n point];
48        r.origin.x = np.x - 3;
49        r.origin.y = np.y - 3;
50        [NSBezierPath fillRect:r];
51    }
52    
53    [[NSColor whiteColor] set];
54    [NSBezierPath setDefaultLineWidth:0.9];
55    NSArray *edges = [graph edges];
56    count = [edges count];
57    for (i = 0; i < count; i++) {
58        GDEdge *e = [edges objectAtIndex:i];
59        GDNode *n1 = [e sourceNode];
60        NSPoint np1 = [n1 point];
61        GDNode *n2 = [e destinationNode];
62        NSPoint np2 = [n2 point];
63        [NSBezierPath strokeLineFromPoint:np1
64                                  toPoint:np2];
65    }    
66}
67
68@end