-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vol52WindowController.m
291 lines (249 loc) · 11.5 KB
/
Vol52WindowController.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
/*
* Vol52WindowController.m
* Ellipsoid Volume Plugin
*
* Created by Rafael Cruz on 15/12/13.
* Copyright (c) 2013, 2014 Rafael Cruz. All rights reserved.
*
*
* This file is part of Ellipsoid Volume, a OsiriX Plugin.
*
* Ellipsoid Volume 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 3 of the License, or
* (at your option) any later version.
*
* Ellipsoid Volume 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.
*
* You should have received a copy of the GNU General Public License
* along with Ellipsoid Volume. If not, see <http://www.gnu.org/licenses/>.
*/
#import "Vol52WindowController.h"
#import "Vol52RoiManager.h"
float _corrCoeff; // Will store the value of the selected correction coefficient.
NSString *_apRoiName; // Name to the ROI created with the "AP" button. Will be used as key for this ROI in vol52ManagedRois NSMutableDictionary.
NSString *_trvRoiName; // Name to the ROI created with the "TRV" button. Will be used as key for this ROI in vol52ManagedRois NSMutableDictionary.
NSString *_lonRoiName; // Name to the ROI created with the "LON" button. Will be used as key for this ROI in vol52ManagedRois NSMutableDictionary.
Vol52RoiManager *vol52RoiManager; // Pointer to Vol52RoiManager sharedInstance.
@implementation Vol52WindowController
@synthesize popover = _popover;
@synthesize resultText = _resultText;
@synthesize customResultTextField = _customResultTextField;
@synthesize buttonCopyResult = _buttonCopyResult;
@synthesize iconAp = _iconAp;
@synthesize iconTrv = _iconTrv;
@synthesize iconLon = _iconLon;
@synthesize customResultString = _customResultString;
@synthesize resultString = _resultString;
@synthesize apDiameterString = _apDiameterString;
@synthesize trvDiameterString = _trvDiameterString;
@synthesize lonDiameterString = _lonDiameterString;
#pragma mark - Class Methods
// ---------------------------------------------------------------------------
// Class Methods
// ---------------------------------------------------------------------------
+ (id) sharedInstance {
static Vol52WindowController *vol52Window = nil;
@synchronized(self) {
if (vol52Window == nil)
vol52Window = [[self alloc] initWithWindowNibName:@"Vol52WindowController"];
}
//[vol52Window autorelease];
return vol52Window;
}
#pragma mark - Instance Methods
// ---------------------------------------------------------------------------
// Instance Methods
// ---------------------------------------------------------------------------
- (id)initWithWindow:(NSWindow *)window {
NSLog(@"Yes, was called!");
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc {
NSLog(@"vol52WindowControler dealloc");
[vol52RoiManager release];
[super dealloc];
}
- (void)windowDidLoad {
[super windowDidLoad];
// Implement this method to handle any initialization after vol52Window has been loaded from its nib file.
_corrCoeff = M_PI/6;
_apRoiName = @"Anteroposterior";
_trvRoiName = @"Transverse";
_lonRoiName = @"Longitudinal";
vol52RoiManager = [[Vol52RoiManager sharedInstance] retain];
[self updateResultText];
}
// Custom setter to read from the customResultString.txt file.
- (NSString *) customResultString {
if (_customResultString == nil) {
NSURL *pluginResourcesURL = [[NSBundle bundleForClass:[self class]] resourceURL];
NSURL *fileUrl = [pluginResourcesURL URLByAppendingPathComponent:@"customResultString.txt"];
self.customResultString = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil];
}
return _customResultString;
}
// Custom setter to save customResultString to the customResultString.txt file.
- (void)setCustomResultString:(NSString *)newString {
if (newString != _customResultString) {
[_customResultString release];
_customResultString = [newString copy];
NSURL *pluginResourcesURL = [[NSBundle bundleForClass:[self class]] resourceURL];
NSURL *fileUrl = [pluginResourcesURL URLByAppendingPathComponent:@"customResultString.txt"];
[_customResultString writeToURL:fileUrl atomically:YES encoding:NSUTF8StringEncoding error:nil];
[self updateResultText];
}
}
- (void) updateResultText {
// Updating status icons of managed ROIs.
NSString *roiBeingCreated = [vol52RoiManager vol52RoiName];
ROI *apRoi = [[vol52RoiManager vol52ManagedRois] objectForKey:_apRoiName];
ROI *trvRoi = [[vol52RoiManager vol52ManagedRois] objectForKey:_trvRoiName];
ROI *lonRoi = [[vol52RoiManager vol52ManagedRois] objectForKey:_lonRoiName];
if (apRoi) {
[self.iconAp setImage:[NSImage imageNamed:@"NSStatusAvailable"]];
} else if (roiBeingCreated == _apRoiName) {
[self.iconAp setImage:[NSImage imageNamed:@"NSStatusPartiallyAvailable"]];
} else {
[self.iconAp setImage:[NSImage imageNamed:@"NSStatusNone"]];
}
if (trvRoi) {
[self.iconTrv setImage:[NSImage imageNamed:@"NSStatusAvailable"]];
} else if (roiBeingCreated == _trvRoiName) {
[self.iconTrv setImage:[NSImage imageNamed:@"NSStatusPartiallyAvailable"]];
} else {
[self.iconTrv setImage:[NSImage imageNamed:@"NSStatusNone"]];
}
if (lonRoi) {
[self.iconLon setImage:[NSImage imageNamed:@"NSStatusAvailable"]];
} else if (roiBeingCreated == _lonRoiName) {
[self.iconLon setImage:[NSImage imageNamed:@"NSStatusPartiallyAvailable"]];
} else {
[self.iconLon setImage:[NSImage imageNamed:@"NSStatusNone"]];
}
// Updating NSTextFields and NSTextView
float apRoiLength = [self lengthForRoiName:_apRoiName];
float trvRoiLength = [self lengthForRoiName:_trvRoiName];
float lonRoiLength = [self lengthForRoiName:_lonRoiName];
float estimatedVolume = apRoiLength * trvRoiLength * lonRoiLength * _corrCoeff;
if (apRoiLength > 0) {
self.apDiameterString = [NSString stringWithFormat:@"= %@ cm", [self stringFromFloat:apRoiLength]];
} else {
self.apDiameterString = @"Set AP.";
}
if (trvRoiLength > 0) {
self.trvDiameterString = [NSString stringWithFormat:@"= %@ cm", [self stringFromFloat:trvRoiLength]];
} else {
self.trvDiameterString = @"Set TRV.";
}
if (lonRoiLength > 0) {
self.lonDiameterString = [NSString stringWithFormat:@"= %@ cm", [self stringFromFloat:lonRoiLength]];
} else {
self.lonDiameterString = @"Set LON.";
}
if (estimatedVolume > 0) {
NSString *stringToReplace; // I'm using this local string because resultString is bound to the user interface and I want to avoid updates on the user interface during the for loop below.
stringToReplace = self.customResultString;
NSDictionary *replacements = [NSDictionary dictionaryWithObjectsAndKeys:
[self stringFromFloat:apRoiLength], @"@AP",
[self stringFromFloat:trvRoiLength], @"@TRV",
[self stringFromFloat:lonRoiLength], @"@LON",
[self stringFromFloat:estimatedVolume], @"@VOL",
nil];
for (NSString *toReplace in replacements) {
stringToReplace = [stringToReplace stringByReplacingOccurrencesOfString:toReplace
withString:[replacements objectForKey:toReplace]];
}
self.resultString = stringToReplace;
[self.buttonCopyResult setEnabled:YES];
} else {
self.resultString = @"Set anteroposterior, transverse and longitudinal diameters to estimate volume.";
[self.buttonCopyResult setEnabled:NO];
}
}
- (NSString *) stringFromFloat:(float)myFloat {
NSNumber *floatNSNumber = [NSNumber numberWithFloat:myFloat];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:1];
NSString *floatString = [numberFormatter stringFromNumber:floatNSNumber];
return floatString;
}
#pragma mark - IBActions
// ---------------------------------------------------------------------------
// IBActions
// ---------------------------------------------------------------------------
- (IBAction)selectCoefficient:(NSPopUpButton *)sender {
switch ([sender indexOfSelectedItem]) {
case 0:
_corrCoeff = M_PI/6;
break;
case 1:
_corrCoeff = 0.625;
break;
case 2:
_corrCoeff = 0.71;
break;
default:
_corrCoeff = M_PI/6;
[sender selectItemAtIndex:0];
break;
}
[self updateResultText];
}
- (IBAction)buttonCreateApRoi:(NSButton *)sender {
[vol52RoiManager createRoiType:tMesure withName:_apRoiName withColor:[NSColor blueColor]];
}
- (IBAction)buttonCreateTrvRoi:(NSButton *)sender {
[vol52RoiManager createRoiType:tMesure withName:_trvRoiName withColor:[NSColor redColor]];
}
- (IBAction)buttonCreateLonRoi:(NSButton *)sender {
[vol52RoiManager createRoiType:tMesure withName:_lonRoiName withColor:[NSColor yellowColor]];
}
- (IBAction)copyResult:(NSButton *)sender {
NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
[pasteBoard declareTypes:types owner:self];
[pasteBoard setString:self.resultString forType:NSStringPboardType];
}
- (IBAction)editResultText:(NSButton *)sender {
[self.popover showRelativeToRect:[self.resultText bounds] ofView:self.resultText preferredEdge:NSMinXEdge];
}
- (IBAction)insertPlaceholder:(NSButton *)sender {
NSMutableString *stringToInsert = [NSMutableString stringWithString:@"@"];
NSString *stringToAppend = [sender title];
[stringToInsert appendString:stringToAppend];
[self.customResultTextField.currentEditor insertText:stringToInsert];
}
- (IBAction)resetCustomResultString:(NSButton *)sender {
self.customResultString = [[NSMutableString alloc] initWithString:@"Measures @AP x @TRV x @LON cm, with an estimated volume of about @VOL ml."];
[self.customResultTextField.currentEditor setString:self.customResultString];
}
#pragma mark - Tweaks...
// ---------------------------------------------------------------------------
// This method was created to get [roi MesureLength] avoiding exceptions.
//
// * Check the number of points. Theres an exception when the user select and
// deletes a single point.
// * In this case we should not call [ROI MesureLength] before the ROI is
// completely deleted, because the ROI will still exists and this method will
// not check the number of points - raising an exception.
// ---------------------------------------------------------------------------
- (float) lengthForRoiName: (NSString*) roiName {
NSDictionary *vol52ManagedRois = [vol52RoiManager vol52ManagedRois];
ROI *roi = [vol52ManagedRois objectForKey:roiName];
int numberOfPoints = [[roi points] count];
float roiLength = 0;
if (numberOfPoints > 1) { // See comments.
roiLength = [roi MesureLength:NULL];
}
return roiLength;
}
@end