-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolymakeObjectController.m
465 lines (350 loc) · 17.3 KB
/
PolymakeObjectController.m
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/***********************************************************************
* Created by Andreas Paffenholz on 04/18/12.
* Copyright 2012-2014 by Andreas Paffenholz.
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version: http://www.gnu.org/licenses/gpl.txt.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* PolymakeObjectController.m
* PolyViewer
**************************************************************************/
#import "PolymakeObjectController.h"
#import "AppWindowController.h"
#import "PolymakeInstanceWrapper.h"
#import "PolymakeObjectWrapper.h"
#import "MainPolymakeObject.h"
NSString * const PVValueFormattingDidChangeNotification = @"PVValueFormattingDidChange";
NSString * const ChildrenOfRootHaveChangedNotification = @"ChildrenOfRootHaveChanged";
NSString * const ComputePropertyOfRootNotification = @"ComputePropertyOfRoot";
@implementation PolymakeObjectController
@synthesize lastOpenDialogStartDirectory = _lastOpenDialogStartDirectory;
@synthesize polymakeObject = _polyObj;
@synthesize alignedColumns = _alignedColumns;
# pragma mark init
- (id)init {
self = [super init];
if (self) {
[self setPolymakeObject:nil];
_currentPropertyValue = nil;
_alignedColumns = NO;
// the directory shown in the file open dialog
// initially this should point to the users $HOME
[self setLastOpenDialogStartDirectory:NSHomeDirectory()];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(redrawValueTextViewWrapper:)
name:PVValueFormattingDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(redrawValueTextViewWrapper:)
name:NSOutlineViewSelectionDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(outlineViewSelectionWillChange:)
name:NSOutlineViewSelectionIsChangingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(tableViewSelectionDidChange:)
name:NSTableViewSelectionDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(childrenOfRootHaveChanged:)
name:ChildrenOfRootHaveChangedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(computePropertyOfRoot:)
name:ComputePropertyOfRootNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_currentPropertyValue release];
[_polyObj release];
[_valueLineNumberView release];
[super dealloc];
}
- (void)makeWindowControllers {
AppWindowController* controller = nil;
controller = [[AppWindowController alloc] initWithWindowNibName: @"PolyViewer" owner: self];
[self addWindowController: controller];
[controller release];
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController {
[super windowControllerDidLoadNib:aController];
NSFont * font = [NSFont fontWithName:@"Menlo" size:14];
[_type setStringValue:[_polyObj objectType]]; // set object type
// [_type setStringValue:[[self polymakeObject] objectType]];
[_type setFont:font];
[_name setStringValue:[_polyObj name]]; // set object name
[_name setFont:font];
[_currentPropertyName setStringValue:@"<root>"]; // set object name
[_currentPropertyName setFont:font];
[_descriptionView setString:[_polyObj description]]; // object description
[_descriptionView setFont:font];
// propertyView is an PropertyView::NSOutlineView
// initialize
[_propertyView setTarget:self];
[_propertyView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleRegular];
[_propertyView reloadData];
[_propertyView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
[self redrawValueTextView];
// _creditTable is an NSTableView
// we want no borders
// and custom colors for the fields
[_creditTable setDelegate:self];
NSSize size;
size.height = 0.0;
size.width = 0.0;
[_creditTable setIntercellSpacing:size];
[_creditTable setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
[_creditTable reloadData];
[_creditTable selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:@"NSTableViewSelectionDidChangeNotification" object:_creditTable];
// initialize the row numbers displayed left of the property values
_valueLineNumberView = [[ValueLineNumberView alloc] initWithScrollView:_valueScrollView];
[_valueScrollView setVerticalRulerView:_valueLineNumberView];
[_valueScrollView setHasHorizontalRuler:NO];
[_valueScrollView setHasVerticalRuler:YES];
[_valueScrollView setRulersVisible:YES];
// set the font for the property values
NSString * floatString = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"propertyWindowFontSize"];
CGFloat floatVal = [floatString floatValue];
NSFont * valueFont = [NSFont fontWithName:[[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"textFontName"]
size:floatVal];
if ( valueFont != nil )
[_valueTextView setFont:valueFont];
else // if there is nothing selected, use a default
[_valueTextView setFont:font];
if ( ![_polyObj databaseObject] )
[_metaInfoTabView removeTabViewItem:[_metaInfoTabView tabViewItemAtIndex:3]];
else {
[_metaInfoTabView selectTabViewItemAtIndex:3];
}
} // end of windowControllerDidLoadNib
- (BOOL)readFromDatabase:(NSString *)database
andCollection:(NSString *)collection
withID:(NSString *)ID {
NSLog(@"[PolymakeObjectController readFromDatabase andCollection withID] called");
_polyObj = [[MainPolymakeObject alloc] retrieveFromDatabase:database
andCollection:collection
withID:ID];
if ( _polyObj == nil )
return NO;
return YES;
}
// method overriden from NSDocument
// the only input routine in this application
- (BOOL)readFromURL:(NSURL *)input
ofType:(NSString *)typeName
error:(NSError **)outError {
NSLog(@"[PolymakeObjectController readFromURL] called");
_polyObj = [[MainPolymakeObject alloc] initObjectWithURL:input];
NSLog(@"[PolymakeObjectController readFromURL] returning");
if ( _polyObj == nil )
return NO;
else
return YES;
}
// the close button on the main window should just close the window
- (IBAction)closePoly:(id)sender {
[self close];
}
// aligned columns for matrices
- (IBAction)fixAlignedColumns:(id)sender {
if ( [_alignedColumnsBox state] == 0 )
_alignedColumns = NO;
else
_alignedColumns = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:PVValueFormattingDidChangeNotification object:nil];
}
# pragma mark PropertyView
/*********************************************************************
* methods for _propertyView
* this is a PropertyView::NSOutlineView
* methods overwritten from base class
*********************************************************************/
/****************/
- (NSInteger) outlineView:(NSOutlineView *)ov numberOfChildrenOfItem:(id)item {
NSLog(@"[PolymakeObjectController outlineView:NumberOfChildrenOfItem:] called");
if ( item == nil ) {
if ( _polyObj == nil )
return 0;
return [[[_polyObj rootPerl] children] count];
}
PolymakeObjectPropertyTreeNode * propNode = (PolymakeObjectPropertyTreeNode *)item;
NSLog(@"[PolymakeObjectController outlineView:NumberOfChildrenOfItem:] returning number of children of node %@", [propNode propertyName]);
NSLog(@"[PolymakeObjectController outlineView:NumberOfChildrenOfItem:] returning %lu children",(unsigned long)[[propNode children] count]);
return [[propNode children] count];
}
/****************/
- (BOOL) outlineView:(NSOutlineView *)ov isItemExpandable:(id)item {
//NSLog(@"[PolymakeObjectController outlineView is Expandable] entering");
if ( item == nil ) {
//NSLog(@"[PolymakeObjectController outlineView is Expandable] leaving (item is nil)");
return YES;
}
PolymakeObjectPropertyTreeNode * propNode = (PolymakeObjectPropertyTreeNode *)item;
//NSLog(@"[PolymakeObjectController outlineView is Expandable] leaving, item is %@", [propNode propertyName]);
return ![propNode isLeaf];
}
/****************/
- (id)outlineView:(NSOutlineView *)ov child:(NSInteger)index ofItem:(id)item {
//NSLog(@"[PolymakeObjectController outlineView child ofItem] entering]");
if ( item == nil ) {
//NSLog(@"[PolymakeObjectController outlineView child ofItem] leaving]");
return [[[_polyObj rootPerl] children] objectAtIndex:index];
}
PolymakeObjectPropertyTreeNode *node = (PolymakeObjectPropertyTreeNode *)item;
//NSLog(@"[PolymakeObjectController outlineView child ofItem] leaving]");
return [[node children] objectAtIndex:index];
}
/****************/
- (id)outlineView:(NSOutlineView *)ov objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
//NSLog(@"[PolymakeObjectController outlineView child objectValueForTableColumn] entering");
PolymakeObjectPropertyTreeNode *node = (PolymakeObjectPropertyTreeNode *)item;
NSString *name = [NSString stringWithString:[node propertyName]];
if ( [node isMultiple] ) {
NSString *propname = [NSString stringWithString:[node name]];
if ( [propname length] > 0 )
name = [name stringByAppendingString:[NSString stringWithFormat:@" (%@)",propname]];
else
name = [name stringByAppendingString:[NSString stringWithFormat:@" (%d)",[node index]]];
}
//NSLog(@"[PolymakeObjectController outlineView child objectValueForTableColumn] leaving for item %@", item);
return name;
}
/****************/
- (void)outlineViewSelectionWillChange:(NSNotification *)aNotification {
}
- (void)childrenOfRootHaveChanged:(NSNotification *)aNotification {
[[_polyObj rootPerl] resetChildren];
[_propertyView reloadItem:nil reloadChildren:YES];
}
- (void)computePropertyOfRoot:(NSNotification *)aNotification {
NSLog(@"[PolymakeObjectController computePropertyOfRoot] called");
NSString *_property = [aNotification object];
[[[_polyObj rootPerl] polyObj] getProperty:_property];
}
#pragma mark _ValueTextView
/*********************************************************************
* methods for _valueTextView
* methods overwritten from base class
**********************************************************************/
// fill the view with the value of the current property
- (void)redrawValueTextView {
NSLog(@"[PolymakeObjectController redrawValueTextField] called");
// determine which property we want to display
id selectedItem = [_propertyView itemAtRow:[_propertyView selectedRow]];
// get the property
if ( selectedItem != nil ) {
PolymakeObjectPropertyTreeNode * propNode = (PolymakeObjectPropertyTreeNode *)selectedItem;
[_propertyTypeField setStringValue:[propNode propertyType]];
[_currentPropertyName setStringValue:[propNode propertyName]];
if ( [[propNode value] isEmpty] ) {
_currentPropertyValue = [[NSString alloc] initWithString:@"<empty property>"];
} else {
if ( [propNode isLeaf] ) {
NSString * proptemp = [[NSString alloc] initWithString:[[propNode value] dataWithAlignedColumns:_alignedColumns]];
NSLog(@"[PolymakeObjectController redrawValueTextView] setting prop:%@", proptemp);
_currentPropertyValue = [[NSString alloc] initWithString:proptemp];
[proptemp release];
} else {
_currentPropertyValue = [[NSString alloc] initWithString:@"<select a sub-property>"];
}
}
[_valueTextView setString:_currentPropertyValue];
} else { // okay, nothing is selected, so clear the view
NSString * attrString = [[NSString alloc] initWithString:@"<empty>"];
_currentPropertyValue = [[NSString alloc] initWithString:attrString];
[_valueTextView setString:attrString];
[_propertyTypeField setStringValue:@""];
[attrString release];
}
}
// just a wrapper that accepts a notification and ignores it
- (void)redrawValueTextViewWrapper:(NSNotification *)aNotification {
NSLog(@"[PolymakeObjectController redrawValueTextViewWrapper] called");
[self redrawValueTextView];
}
// the property list should not resize horizontally if the window is resized
// the split view has two subviews: 0 are the properties, 1 are the values
- (BOOL)splitView:(NSSplitView *)sv shouldAdjustSizeOfSubview:(id)aSubView {
if ( aSubView == [[sv subviews] objectAtIndex:1] )
return YES;
return NO;
}
#pragma mark CreditView
/*********************************************************************
* TableView methods for credits info window
* methods overwritten from base class
*********************************************************************/
/****************/
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tv {
if ( tv == _creditTable ) {
NSLog(@"[PolymakeObjectController numberOfRowsInTableView] called");
if ( _polyObj != nil )
return [[_polyObj credits] count];
}
if ( tv == _databaseTableView ) {
NSLog(@"[PolymakeObjectController numberOfRowsInTableView] called dor database");
return [[_polyObj databaseInfo] count];
}
// default return 0
return 0;
}
/****************/
- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger) row {
NSLog(@"[PolymakeObjectController tableView ObjectValueTableColumn column row] called");
if ( tv == _creditTable ) {
NSLog(@"[PolymakeObjectController tableView ObjectValueTableColumn column row] called for credits");
NSString * value = [NSString stringWithString:(NSString *)[[[_polyObj credits] allKeys] objectAtIndex:row]];
return value;
}
if ( tv == _databaseTableView ) {
NSLog(@"[PolymakeObjectController tableView ObjectValueTableColumn column row] called for database");
if ( [[column identifier] isEqual: @"label"]) {
NSString *key = [[[_polyObj databaseInfo] allKeys] objectAtIndex:row];
return key;
} else {
NSString *key = [[[_polyObj databaseInfo] allKeys] objectAtIndex:row];
NSString * value = [NSString stringWithString:(NSString *)[[_polyObj databaseInfo] objectForKey:key]];
return value;
}
}
return nil;
}
/****************/
- (void)tableView:(NSTableView *)tv setObjectValue:(id)item forTableColumn:(NSTableColumn *)column row:(NSInteger)row {
NSLog(@"[PolymakeObjectController tableView setObjectvalue forTableColumn row] called");
if ( tv == _creditTable ) {
item = [NSString stringWithString:(NSString *)[[[_polyObj credits] allKeys] objectAtIndex:row]];
}
}
/****************/
- (void) tableViewSelectionDidChange: (NSNotification *) notification {
NSLog(@"[PolymakeObjectController tableViewSelectionDidChange] called");
if ( [notification object] == _creditTable ) {
int row = [_creditTable selectedRow];
if ( row != -1 ) {
NSArray * allValues = [[_polyObj credits] allValues];
NSString * value = [[NSString alloc] initWithString:(NSString *)[allValues objectAtIndex:row]];
[_creditView setString:value];
[value release];
}
}
}
// this just sets the background color
-(void)tableView:(NSTableView *)tv willDisplayCell:(id)item forTableColumn:(NSTableColumn *)column row:(NSInteger)row {
NSLog(@"[PolymakeObjectController tableView willDisplayCell for TableColumn row] called");
if ( [tv selectedRow] == row )
[item setBackgroundColor:[NSColor colorWithCalibratedRed:0.914 green:0.686 blue:0.227 alpha:1]];
else
[item setBackgroundColor:[NSColor colorWithCalibratedRed:0.8 green:0.41 blue:0.14 alpha:1]];
}
@end