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