forked from apaffenholz/PolyViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropertyView.m
152 lines (120 loc) · 5.59 KB
/
PropertyView.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
/***********************************************************************
* 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.
*
* PropertyView.m
* PolyViewer
**************************************************************************/
#import "PropertyView.h"
#import "PropertyNode.h"
@implementation PropertyView
- (id)init {
NSLog(@"[PropertyView] init]");
self = [super init];
return self;
}
// highlight color for selected item
-(id)_highlightColorForCell:(NSCell *)cell {
return [NSColor colorWithCalibratedRed:0.914 green:0.686 blue:0.227 alpha:1];
}
// get the secondary menu
-(NSMenu*)menuForEvent:(NSEvent*)event {
NSPoint pt = [self convertPoint:[event locationInWindow] fromView:nil];
int row=[self rowAtPoint:pt];
return [self defaultMenuForRow:row];
}
// build up context menu
-(NSMenu*)defaultMenuForRow:(int)row {
// can this happen?
if (row < 0)
return nil;
// get the clicked property node
// FIXME eventually we want to offer different options depending on the type:
// FIXME for objects (non-leafs) offer to add a subproperty of the object or
// FIXME a property at the same level (as for leaf nodes)
// we can't stick with just adding subporperties as we have no visual "root node"
//
PropertyNode * propNode = (PropertyNode *)[self itemAtRow:row];
BOOL isObj = [propNode isObject];
NSMenuItem * addPropItem = [[NSMenuItem alloc] initWithTitle:@"Compute property"
action:@selector(addProperty:)
keyEquivalent:@""];
[addPropItem setRepresentedObject:[NSNumber numberWithInt:row]];
NSMenu *theMenu = [[[NSMenu alloc]
initWithTitle:@"Context Menu"]
autorelease];
[theMenu addItem:addPropItem];
// FIXME the following can be done faster by adding at some index different from 0
if ( isObj )
[theMenu insertItemWithTitle:@"Compute subproperty of object"
action:@selector(addSubProperty:)
keyEquivalent:@""
atIndex:0];
[theMenu insertItemWithTitle:[NSString stringWithFormat:@"Remove property"]
action:@selector(removeProperty:)
keyEquivalent:@""
atIndex:0];
if ( isObj )
[theMenu insertItemWithTitle:@"Remove subproperty of object"
action:@selector(removeSubProperty:)
keyEquivalent:@""
atIndex:0];
return theMenu;
}
// actually ocmpute the property and force a reload of the outline view
// reloading is necessary as we cannot just add another row to the view:
// polymake might add further properties necessary to compute the one asked for
// those should be displayed as well but we have no chance to get notified about what
// was actually added
//
// FIXME this should go into the controller
- (void)addProperty:(id)sender {
NSLog(@"[PropertyView addProperty] called");
int row = [[sender representedObject] intValue];
NSLog(@"The menu item's object is %d",row);
NSString * _property = nil;
NSAlert *alert = [NSAlert alertWithMessageText: @"Compute property"
defaultButton:@"OK"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@""];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:@"<property>"];
[input autorelease];
[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertDefaultReturn) {
[input validateEditing];
_property = [input stringValue];
NSLog(@"got %@", [input stringValue]);
} else if (button == NSAlertAlternateReturn) {
} else {
}
PropertyNode * propNode = (PropertyNode *)[self itemAtRow:row];
if ( ![propNode isObject] ) {
NSLog(@"[PropertyView addProperty] switching to parent");
[[propNode polyObj] getProperty:_property]; //FIXME change this: we are computing the prop and throw away the result
// here we again have to find out wether our property is a child of the root or some lower node
// we treat the root differently as it is not displayed in the view
// and we associate additional information with the node
PropertyNode * parent = (PropertyNode *)[self parentForItem:[self itemAtRow:row]];
if ( parent == nil ) {
NSLog(@"[PropertyView addProperty] property added to root");
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChildrenOfRootHaveChanged" object:nil];
} else {
[parent resetChildren];
[self reloadItem:nil reloadChildren:YES];
}
} else {
NSLog(@"[PropertyView addProperty] not at a leaf node");
}
}
@end