GVDocument.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at http://www.graphviz.org/
  9. *************************************************************************/
  10. #import "GVDocument.h"
  11. #import "GVExportViewController.h"
  12. #import "GVFileNotificationCenter.h"
  13. #import "GVZGraph.h"
  14. #import "GVWindowController.h"
  15. @implementation GVDocument
  16. @synthesize graph = _graph;
  17. - (id)init
  18. {
  19. if (self = [super init]) {
  20. _exporter = nil;
  21. _graph = nil;
  22. }
  23. return self;
  24. }
  25. - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
  26. {
  27. [_graph release];
  28. _graph = [[GVZGraph alloc] initWithURL:absoluteURL error:outError];
  29. [_graph.arguments setValue:@"dot" forKey:@"layout"];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(graphDidChange:) name:@"GVGraphDidChange" object:_graph];
  31. [[GVFileNotificationCenter defaultCenter] addObserver:self selector:@selector(fileDidChange:) path:[absoluteURL path]];
  32. return _graph != nil;
  33. }
  34. - (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
  35. {
  36. return [_graph writeToURL:absoluteURL error:outError];
  37. }
  38. - (void)makeWindowControllers
  39. {
  40. [self addWindowController: [[[GVWindowController alloc] init] autorelease]];
  41. }
  42. - (void)setPrintInfo:(NSPrintInfo *)printInfo
  43. {
  44. /* after Page Setup is run, change the page size and margins of the graph to fit the page setup parameters */
  45. [super setPrintInfo:printInfo];
  46. NSSize paperSize = [printInfo paperSize];
  47. NSRect imageablePageBounds = [printInfo imageablePageBounds];
  48. double scalingFactor = 72.0 * [[[printInfo dictionary] objectForKey:NSPrintScalingFactor] doubleValue];
  49. [_graph.graphAttributes setObject:[NSString stringWithFormat:@"%f,%f",
  50. paperSize.width / scalingFactor,
  51. paperSize.height / scalingFactor]
  52. forKey:@"page"];
  53. [_graph.graphAttributes setObject:[NSString stringWithFormat:@"%f,%f",
  54. fmax(imageablePageBounds.origin.x, paperSize.width - imageablePageBounds.size.width - imageablePageBounds.origin.x) / scalingFactor,
  55. fmax(imageablePageBounds.origin.y, paperSize.height - imageablePageBounds.size.height - imageablePageBounds.origin.y) / scalingFactor]
  56. forKey:@"margin"];
  57. }
  58. - (IBAction)exportDocument:(id)sender
  59. {
  60. if (!_exporter) {
  61. _exporter = [[GVExportViewController alloc] init];
  62. [_exporter setURL:[[self fileURL] URLByDeletingPathExtension]];
  63. }
  64. [_exporter beginSheetModalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(exporterDidEnd:)];
  65. }
  66. - (void)exporterDidEnd:(GVExportViewController *)exporter
  67. {
  68. [_graph renderWithFormat:[exporter device] toURL:[exporter URL]];
  69. }
  70. - (void)fileDidChange:(NSString *)path
  71. {
  72. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  73. [defaultCenter removeObserver:self name:@"GVGraphDidChange" object:_graph];
  74. /* reparse the graph fresh from the file */
  75. [_graph release];
  76. _graph = [[GVZGraph alloc] initWithURL:[self fileURL] error:nil];
  77. [_graph.arguments setValue:@"dot" forKey:@"layout"];
  78. [defaultCenter addObserver:self selector:@selector(graphDidChange:) name:@"GVGraphDidChange" object:_graph];
  79. [defaultCenter postNotificationName:@"GVGraphDocumentDidChange" object:self];
  80. }
  81. - (void)graphDidChange:(NSNotification *)notification
  82. {
  83. [[NSNotificationCenter defaultCenter] postNotificationName:@"GVGraphDocumentDidChange" object:self];
  84. }
  85. - (void)dealloc
  86. {
  87. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GVGraphDidChange" object:_graph];
  88. [[GVFileNotificationCenter defaultCenter] removeObserver:self path:[[self fileURL] path]];
  89. [_exporter release];
  90. [_graph release];
  91. [super dealloc];
  92. }
  93. @end