/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. #import "GDGraphView.h"
  7. #import "GDGraph.h"
  8. #import "GDNode.h"
  9. #import "GDEdge.h"
  10. @implementation GDGraphView
  11. - (id)initWithFrame:(NSRect)frameRect
  12. {
  13. if ((self = [super initWithFrame:frameRect]) != nil) {
  14. NSRect b = [self bounds];
  15. [self translateOriginToPoint:NSMakePoint(b.origin.x + b.size.width/2.0,
  16. b.origin.y + b.size.height/2.0)];
  17. }
  18. return self;
  19. }
  20. - (void)setGraph:(GDGraph *)g
  21. {
  22. [g retain];
  23. [graph release];
  24. graph = g;
  25. [self setNeedsDisplay:YES];
  26. }
  27. - (void)drawRect:(NSRect)rect
  28. {
  29. NSRect b = [self bounds];
  30. [[NSColor darkGrayColor] set];
  31. [NSBezierPath fillRect:b];
  32. [self translateOriginToPoint:NSMakePoint(b.origin.x + b.size.width/2.0,
  33. b.origin.y + b.size.height/2.0)];
  34. [[NSColor redColor] set];
  35. NSArray *nodes = [graph nodes];
  36. int i, count;
  37. count = [nodes count];
  38. NSRect r = NSMakeRect(0,0, 6, 6);
  39. for (i = 0; i < count; i++) {
  40. GDNode *n = [nodes objectAtIndex:i];
  41. NSPoint np = [n point];
  42. r.origin.x = np.x - 3;
  43. r.origin.y = np.y - 3;
  44. [NSBezierPath fillRect:r];
  45. }
  46. [[NSColor whiteColor] set];
  47. [NSBezierPath setDefaultLineWidth:0.9];
  48. NSArray *edges = [graph edges];
  49. count = [edges count];
  50. for (i = 0; i < count; i++) {
  51. GDEdge *e = [edges objectAtIndex:i];
  52. GDNode *n1 = [e sourceNode];
  53. NSPoint np1 = [n1 point];
  54. GDNode *n2 = [e destinationNode];
  55. NSPoint np2 = [n2 point];
  56. [NSBezierPath strokeLineFromPoint:np1
  57. toPoint:np2];
  58. }
  59. }
  60. @end