forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnonymizationViewController.mm
348 lines (269 loc) · 12.6 KB
/
AnonymizationViewController.mm
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
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#import "AnonymizationViewController.h"
#import "AnonymizationTemplateNamePanelController.h"
#import "AnonymizationTagsView.h"
#import "DCMAttributeTag.h"
#import "N2Operators.h"
#import "Anonymization.h"
#import "N2AdaptiveBox.h"
#import "N2TextField.h"
#import "N2CustomTitledPopUpButtonCell.h"
#include <cmath>
#include <algorithm>
@interface AnonymizationViewController ()
@property(retain,readwrite) NSMutableArray* tags;
@property(readwrite) BOOL formatsAreOk;
@end
@implementation AnonymizationViewController
@synthesize annotationsBox;
@synthesize templatesPopup;
@synthesize tagsView;
@synthesize tags;
@synthesize formatsAreOk;
+(NSArray*)basicTags {
return [NSArray arrayWithObjects:
[DCMAttributeTag tagWithName:@"PatientsName"],
[DCMAttributeTag tagWithName:@"PatientsSex"],
[DCMAttributeTag tagWithName:@"PatientID"],
[DCMAttributeTag tagWithName:@"PatientsWeight"],
[DCMAttributeTag tagWithName:@"PatientsAge"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialSponsorName"],
// [DCMAttributeTag tagWithName:@"PatientsBirthDate"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialProtocolID"],
// [DCMAttributeTag tagWithName:@"InstitutionName"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialProtocolName"],
// [DCMAttributeTag tagWithName:@"StudyID"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialSiteID"],
// [DCMAttributeTag tagWithName:@"StudyDate"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialSiteName"],
// [DCMAttributeTag tagWithName:@"StudyTime"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialSubjectReadingID"],
// [DCMAttributeTag tagWithName:@"AcquisitionDatetime"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialSubjectID"],
// [DCMAttributeTag tagWithName:@"SeriesDate"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialTimePointID"],
// [DCMAttributeTag tagWithName:@"SeriesTime"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialTimePointDescription"],
// [DCMAttributeTag tagWithName:@"InstanceCreationDate"],
// [DCMAttributeTag tagWithName:@"ClinicalTrialCoordinatingCenterName"],
// [DCMAttributeTag tagWithName:@"InstanceCreationTime"],
// [DCMAttributeTag tagWithName:@"PerformingPhysiciansName"],
// [DCMAttributeTag tagWithName:@"ReferringPhysiciansName"],
// [DCMAttributeTag tagWithName:@"PhysiciansOfRecord"],
// [DCMAttributeTag tagWithName:@"AccessionNumber"],
NULL];
}
-(void)refreshTemplatesList {
[templatesPopup removeAllItems];
NSDictionary* templates = [[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:@"anonymizeTemplate"];
for (NSString* name in [[templates allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)])
[templatesPopup addItemWithTitle:name];
[templatesPopup setEnabled:templates.count>0];
}
-(id)initWithTags:(NSArray*)shownDcmTags values:(NSArray*)values
{
self = [super initWithNibName:@"AnonymizationView" bundle:NULL];
[self view]; // load
self.tags = [NSMutableArray array];
// templates
NSCell* tempCell = [templatesPopup.cell retain];
templatesPopup.cell = [[[N2CustomTitledPopUpButtonCell alloc] init] autorelease];
[templatesPopup.cell setControlSize:tempCell.controlSize];
[templatesPopup.cell setFont:tempCell.font];
[tempCell release];
templatesPopup.autoenablesItems = NO;
templatesPopup.target = self;
templatesPopup.action = @selector(templatesPopupAction:);
[self refreshTemplatesList];
// tags
NSMutableArray* dcmTagsToShow = [shownDcmTags mutableCopy];
if (!dcmTagsToShow.count)
[dcmTagsToShow addObjectsFromArray:[AnonymizationViewController basicTags]];
for (DCMAttributeTag* tag in dcmTagsToShow)
[self addTag:tag];
[dcmTagsToShow release];
[self setTagsValues:values];
[self observeValueForKeyPath:NULL ofObject:NULL change:NULL context:self.tagsView];
return self;
}
-(void)adaptBoxToAnnotations {
NSSize annotationsBoxPadding = ((NSView*)annotationsBox.contentView).frame.size - tagsView.frame.size;
NSSize idealAnnotationsBoxSize = [self.tagsView idealSize];
[annotationsBox adaptContainersToIdealSize:NSMakeSize(((NSView*)annotationsBox.contentView).frame.size.width, idealAnnotationsBoxSize.height+annotationsBoxPadding.height)];
[self.view.window setMaxSize:NSMakeSize(CGFLOAT_MAX, self.view.window.frame.size.height)];
[self.view.window setMinSize:NSMakeSize(600, self.view.window.frame.size.height)];
}
-(void)addTag:(DCMAttributeTag*)tag {
if( tag == nil) return;
if ([self.tags containsObject:tag])
return;
[self.tags addObject:tag];
[self.tagsView addTag:tag];
[[[self.tagsView checkBoxForObject:tag] cell] addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionInitial context:self.tagsView];
[[self.tagsView textFieldForObject:tag] addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionInitial context:self.tagsView];
[[self.tagsView textFieldForObject:tag] addObserver:self forKeyPath:@"formatIsOk" options:NSKeyValueObservingOptionInitial context:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observeTextDidChangeNotification:) name:NSControlTextDidChangeNotification object:[self.tagsView textFieldForObject:tag]];
[self adaptBoxToAnnotations];
}
-(void)removeTag:(DCMAttributeTag*)tag {
if (![self.tags containsObject:tag])
return;
[[[self.tagsView checkBoxForObject:tag] cell] removeObserver:self forKeyPath:@"state"];
[[self.tagsView textFieldForObject:tag] removeObserver:self forKeyPath:@"value"];
[[self.tagsView textFieldForObject:tag] removeObserver:self forKeyPath:@"formatIsOk"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSControlTextDidChangeNotification object:[self.tagsView textFieldForObject:tag]];
[self.tags removeObject:tag];
[self.tagsView removeTag:tag];
[self adaptBoxToAnnotations];
}
-(NSString*)nameOfCurrentMatchingTemplate {
NSArray* currentTagsValues = [self tagsValues];
NSString* matchName = NULL;
NSDictionary* templates = [[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:@"anonymizeTemplate"];
for (NSString* name in templates) {
NSArray* named = [Anonymization tagsValuesArrayFromDictionary:[templates objectForKey:name]];
if ([Anonymization tagsValues:currentTagsValues isEqualTo:named])
matchName = name;
}
return matchName;
}
-(void)updateFormatsAreOk {
BOOL ok = YES;
for (DCMAttributeTag* tag in tags) {
N2TextField* textField = [tagsView textFieldForObject:tag];
if (!textField.formatIsOk)
ok = NO;
}
[self setFormatsAreOk:ok];
}
-(void)setFormatsAreOk:(BOOL)flag {
if (flag == formatsAreOk)
return;
formatsAreOk = flag;
[self didChangeValueForKey:@"formatsAreOk"];
}
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
// NSLog(@"observeValueForKeyPath:%@ ofObject....", keyPath);
if (context == self.tagsView) {
NSString* matchName = [self nameOfCurrentMatchingTemplate];
if (matchName)
[templatesPopup selectItemWithTitle:matchName];
else [templatesPopup.selectedItem setState:0];
[templatesPopup.cell setDisplayedTitle: matchName? matchName : NSLocalizedString(@"Custom", NULL) ];
[templatesPopup setNeedsDisplay:YES];
[deleteTemplateButton setEnabled: matchName != NULL];
}
else if ([keyPath isEqualToString:@"formatIsOk"]) {
[self updateFormatsAreOk];
}
}
-(void)observeTextDidChangeNotification:(NSNotification*)notif {
[self observeValueForKeyPath:NULL ofObject:NULL change:NULL context:self.tagsView];
}
//-(void)observeViewFrameDidChange:(NSNotification*)notification {
// [self.tagsView adaptCellSizeToViewSize];
//}
-(void)dealloc {
// NSLog(@"AnonymizationViewController dealloc");
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewFrameDidChangeNotification object:self.view];
while (tags.count)
[self removeTag:[tags objectAtIndex:(long)tags.count-1]];
self.tags = NULL;
[super dealloc];
}
-(NSArray*)tagsValues {
NSMutableArray* out = [NSMutableArray array];
for (DCMAttributeTag* tag in tags)
if ([[self.tagsView checkBoxForObject:tag] state]) {
NSTextField* tf = [self.tagsView textFieldForObject:tag];
id value = tf.stringValue.length? tf.objectValue : NULL;
//if (value)
// if () // TODO: objs
[out addObject:[NSArray arrayWithObjects: tag, value, NULL]];
}
return [[out copy] autorelease];
}
NSInteger CompareArraysByNameOfDCMAttributeTagAtIndexZero(id arg1, id arg2, void* context) {
return [[[arg1 objectAtIndex:0] name] caseInsensitiveCompare:[[arg2 objectAtIndex:0] name]];
}
-(void)setTagsValues:(NSArray*)tagsValues
{
tagsValues = [tagsValues sortedArrayUsingFunction:CompareArraysByNameOfDCMAttributeTagAtIndexZero context:NULL];
NSMutableArray* zeroTags = [self.tags mutableCopy];
// this removes all previous tags
if (tagsValues.count)
while (self.tags.count)
[self removeTag:[self.tags objectAtIndex:0]];
for (NSArray* tagValue in tagsValues) {
DCMAttributeTag* tag = [tagValue objectAtIndex:0];
[self addTag:tag];
id value = tagValue.count>1? [tagValue objectAtIndex:1] : NULL;
NSButton* checkBox = [self.tagsView checkBoxForObject:tag];
[checkBox setState: value? NSOnState : NSOffState];
NSTextField* textField = [self.tagsView textFieldForObject:tag];
if (!value || [value isKindOfClass:[NSString class]])
[textField setStringValue: value? value : @""];
else @try {
[textField setObjectValue: value];
} @catch (NSException* e) {
NSLog(@"Warning: invalid value type %@ for DICOM tag %@", [value class], tag.name);
}
[zeroTags removeObject:tag];
}
for (DCMAttributeTag* tag in zeroTags) {
[[self.tagsView checkBoxForObject:tag] setState:NSOffState];
[[self.tagsView textFieldForObject:tag] setStringValue:@""];
}
[self observeValueForKeyPath:NULL ofObject:NULL change:NULL context:self.tagsView];
[zeroTags release];
}
-(void)saveTemplate:(NSArray*)templ withName:(NSString*)name {
NSMutableDictionary* dic = [[[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:@"anonymizeTemplate"] mutableCopy];
if (!dic) dic = [[NSMutableDictionary alloc] init];
[dic setObject:[Anonymization tagsValuesDictionaryFromArray:templ] forKey:name];
[[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"anonymizeTemplate"];
[dic release];
[self refreshTemplatesList];
[self observeValueForKeyPath:NULL ofObject:NULL change:NULL context:self.tagsView];
}
-(void)templatesPopupAction:(NSPopUpButton*)sender {
NSString* name = sender.selectedItem.title;
NSDictionary* dic = [[[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:@"anonymizeTemplate"] objectForKey:name];
NSArray* arr = [Anonymization tagsValuesArrayFromDictionary:dic];
[self setTagsValues:arr];
// backwards compatibility: prefs might contain NSStrings, which might not match with the corresponding objects, so if no match is found we autosave
if (![name isEqualToString:[self nameOfCurrentMatchingTemplate]])
[self saveTemplate:[self tagsValues] withName:name];
}
-(IBAction)saveTemplateAction:(id)sender {
AnonymizationTemplateNamePanelController* panelController = [[AnonymizationTemplateNamePanelController alloc] initWithReplaceValues:[[[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:@"anonymizeTemplate"] allKeys]];
[NSApp beginSheet:panelController.window modalForWindow:self.view.window modalDelegate:self didEndSelector:@selector(saveTemplateNamePanelDidEnd:returnCode:contextInfo:) contextInfo:panelController];
[panelController.window orderFront:self];
}
-(void)saveTemplateNamePanelDidEnd:(NSPanel*)panel returnCode:(NSInteger)returnCode contextInfo:(void*)contextInfo {
AnonymizationTemplateNamePanelController* panelController = (id)contextInfo;
if (returnCode == NSRunStoppedResponse) {
[self saveTemplate:[self tagsValues] withName:panelController.value];
}
[panel close];
[panelController release];
}
-(IBAction)deleteTemplateAction:(id)sender {
NSString* name = [templatesPopup.cell displayedTitle];
NSMutableDictionary* dic = [[[NSUserDefaultsController sharedUserDefaultsController] dictionaryForKey:@"anonymizeTemplate"] mutableCopy];
[dic removeObjectForKey:name];
[[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"anonymizeTemplate"];
[dic release];
[self refreshTemplatesList];
[self observeValueForKeyPath:NULL ofObject:NULL change:NULL context:self.tagsView];
}
@end