forked from bettar/miele-lxiv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnonymizationTagsView.mm
313 lines (255 loc) · 12.3 KB
/
AnonymizationTagsView.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
/*=========================================================================
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 "AnonymizationTagsView.h"
#import "DCMAttributeTag.h"
#import "N2HighlightImageButtonCell.h"
#import "AnonymizationViewController.h"
#import "N2TextField.h"
#import "AnonymizationTagsPopUpButton.h"
#include <algorithm>
#include <cmath>
@implementation AnonymizationTagsView
-(id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
viewGroups = [[NSMutableArray alloc] init];
intercellSpacing = NSMakeSize(13,1);
dcmTagsPopUpButton = [[AnonymizationTagsPopUpButton alloc] initWithFrame:NSZeroRect];
[dcmTagsPopUpButton.cell setControlSize:NSMiniControlSize];
[dcmTagsPopUpButton setFont:[NSFont labelFontOfSize:[NSFont smallSystemFontSize]-2]];
[self addSubview:dcmTagsPopUpButton];
NSButtonCell* addButtonCell = [[N2HighlightImageButtonCell alloc] initWithImage:[NSImage imageNamed:@"PlusButton"]];
dcmTagAddButton = [[NSButton alloc] initWithFrame:NSZeroRect];
dcmTagAddButton.cell = addButtonCell;
[addButtonCell release];
dcmTagAddButton.target = self;
dcmTagAddButton.action = @selector(addButtonAction:);
[self addSubview:dcmTagAddButton];
return self;
}
-(NSArray*)groupForObject:(id)object {
for (NSArray* group in viewGroups)
for (id obj in group)
if (object == obj || [obj isEqual:object])
return group;
return NULL;
}
-(void)addButtonAction:(NSButton*)sender {
[anonymizationViewController addTag:dcmTagsPopUpButton.selectedTag];
[[anonymizationViewController.tagsView checkBoxForObject:dcmTagsPopUpButton.selectedTag] setState:NSOnState];
[self.window makeFirstResponder:[anonymizationViewController.tagsView textFieldForObject:dcmTagsPopUpButton.selectedTag]];
[dcmTagsPopUpButton setSelectedTag:NULL];
}
-(void)rmButtonAction:(NSButton*)sender {
[anonymizationViewController removeTag:[[self groupForObject:sender] objectAtIndex:3]];
}
-(void)awakeFromNib {
[self resizeSubviewsWithOldSize:self.frame.size];
}
-(BOOL)isFlipped {
return YES;
}
-(void)dealloc {
// NSLog(@"AnonymizationTagsView dealloc");
[dcmTagsPopUpButton release];
[dcmTagAddButton release];
[viewGroups release];
[super dealloc];
}
-(NSInteger)columnCount {
return 2;
}
-(NSInteger)rowCount {
return std::ceil(CGFloat(viewGroups.count+1)/self.columnCount);
}
-(NSRect)cellFrameForIndex:(NSInteger)index {
NSInteger column = index%self.columnCount, row = std::floor(CGFloat(index)/self.columnCount);
return NSMakeRect((cellSize.width+intercellSpacing.width)*column, (cellSize.height+intercellSpacing.height)*row, cellSize.width, cellSize.height);
}
#define kMaxTextFieldWidth 200.f
#define kButtonSpace 15.f
-(NSRect)checkBoxFrameForCellFrame:(NSRect)frame {
CGFloat textFieldWidth;
if((frame.size.width-kButtonSpace)/2 < kMaxTextFieldWidth) textFieldWidth = (frame.size.width-kButtonSpace)/2;
else textFieldWidth = kMaxTextFieldWidth;
frame.size.width -= frame.size.height+textFieldWidth;
return frame;
}
-(NSRect)textFieldFrameForCellFrame:(NSRect)frame {
CGFloat textFieldWidth;
if((frame.size.width-kButtonSpace)/2 < kMaxTextFieldWidth) textFieldWidth = (frame.size.width-kButtonSpace)/2;
else textFieldWidth = kMaxTextFieldWidth;
frame.origin.x += frame.size.width - textFieldWidth - frame.size.height;
frame.size.width = textFieldWidth;
return frame;
}
-(NSRect)buttonFrameForCellFrame:(NSRect)frame {
frame.origin.x += frame.size.width-10;
frame.origin.y += 4;
frame.size = NSMakeSize(10,10);
return frame;
}
-(NSRect)popUpButtonFrameForCellFrame:(NSRect)frame {
frame.size.width -= kButtonSpace;
return frame;
}
-(void)repositionGroupViews:(NSArray*)group {
NSRect cellFrame = [self cellFrameForIndex:[viewGroups indexOfObject:group]];
[[group objectAtIndex:0] setFrame:[self checkBoxFrameForCellFrame:cellFrame]];
[[group objectAtIndex:1] setFrame:[self textFieldFrameForCellFrame:cellFrame]];
[[group objectAtIndex:2] setFrame:[self buttonFrameForCellFrame:cellFrame]];
}
-(void)repositionAddTagInterface {
NSRect cellFrame = [self cellFrameForIndex:viewGroups.count];
[dcmTagsPopUpButton setFrame:[self popUpButtonFrameForCellFrame:cellFrame]];
[dcmTagAddButton setFrame:[self buttonFrameForCellFrame:cellFrame]];
}
-(void)addTag:(DCMAttributeTag*)tag {
static NSFont* font = [[NSFont labelFontOfSize:[NSFont smallSystemFontSize]-1] retain];
NSButton* checkBox = [[NSButton alloc] initWithFrame:NSZeroRect];
[[checkBox cell] setControlSize:NSMiniControlSize];
[checkBox setFont:font];
[[checkBox cell] setLineBreakMode:NSLineBreakByTruncatingMiddle];
[checkBox setButtonType:NSSwitchButton];
[checkBox setTitle:tag.name];
[self addSubview:checkBox];
N2TextField* textField = [[N2TextField alloc] initWithFrame:NSZeroRect];
[[textField cell] setControlSize:NSMiniControlSize];
[textField setFont:font];
[textField setBezeled:YES];
[textField setBezelStyle:NSTextFieldSquareBezel];
[textField setDrawsBackground:YES];
[[textField cell] setPlaceholderString:NSLocalizedString(@"Reset", @"Placeholder string for Anonymization Tag cells")];
[textField setStringValue:@""];
[self addSubview:textField];
// NSLog( @"VR: %@", tag.vr);
NSDateFormatter* df = NULL;
NSNumberFormatter* nf = NULL;
if ([tag.vr isEqualToString:@"DA"] || [tag.vr isEqualToString:@"TM"] || [tag.vr isEqualToString:@"DT"])
{
[textField.cell setFormatter: df = [[[NSDateFormatter alloc] init] autorelease]];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
if ([tag.vr isEqualToString:@"DA"]) { //Date String
[df setTimeStyle:NSDateFormatterNoStyle];
[df setDateStyle:NSDateFormatterShortStyle];
} else if ([tag.vr isEqualToString:@"TM"]) { //Time String
[df setTimeStyle:NSDateFormatterShortStyle];
[df setDateStyle:NSDateFormatterNoStyle];
} else if ([tag.vr isEqualToString:@"DT"]) { //Date Time
[df setTimeStyle:NSDateFormatterShortStyle];
[df setDateStyle:NSDateFormatterShortStyle];
}
if ([df.dateFormat rangeOfString:@"yyyy"].location == NSNotFound && [df.dateFormat rangeOfString:@"yy"].location != NSNotFound)
{
NSString *fourDigitYearFormat = [[df dateFormat] stringByReplacingOccurrencesOfString:@"yy" withString:@"yyyy"];
[df setDateFormat:fourDigitYearFormat];
}
[textField setToolTip: [NSString stringWithFormat: NSLocalizedString( @"Required format: %@", nil), df.dateFormat]];
} else if ([tag.vr isEqualToString:@"DS"] || [tag.vr isEqualToString:@"IS"] || [tag.vr isEqualToString:@"SL"] || [tag.vr isEqualToString:@"SS"] || [tag.vr isEqualToString:@"UL"] || [tag.vr isEqualToString:@"US"] || [tag.vr isEqualToString:@"FL"] || [tag.vr isEqualToString:@"FD"]) {
[textField.cell setFormatter: nf = [[[NSNumberFormatter alloc] init] autorelease]];
[nf setFormatterBehavior:NSNumberFormatterBehavior10_4];
[nf setNumberStyle:NSNumberFormatterDecimalStyle];
if ([tag.vr isEqualToString:@"DS"]) { //Decimal String representing floating point
[nf setMaximumSignificantDigits:16];
[textField setToolTip: NSLocalizedString( @"Required format: floating point number", nil)];
} else if ([tag.vr isEqualToString:@"IS"]) { //Integer String
[nf setMaximumSignificantDigits:12];
[nf setAllowsFloats:NO];
[textField setToolTip: NSLocalizedString( @"Required format: integer number", nil)];
} else if ([tag.vr isEqualToString:@"SL"]) { //signed long
[nf setAllowsFloats:NO];
[nf setMinimum:[NSNumber numberWithInteger:-0x80000000]];
[nf setMaximum:[NSNumber numberWithInteger:0x7FFFFFFF]];
[textField setToolTip: NSLocalizedString( @"Required format: integer number", nil)];
} else if ([tag.vr isEqualToString:@"SS"]) { //signed short
[nf setAllowsFloats:NO];
[nf setMinimum:[NSNumber numberWithInteger:-0x8000]];
[nf setMaximum:[NSNumber numberWithInteger:0x7FFF]];
[textField setToolTip: NSLocalizedString( @"Required format: integer number", nil)];
} else if ([tag.vr isEqualToString:@"UL"]) { //unsigned long
[textField.cell setFormatter: nf = [[[NSNumberFormatter alloc] init] autorelease]];
[nf setAllowsFloats:NO];
[nf setMinimum:[NSNumber numberWithInteger:0]];
[nf setMaximum:[NSNumber numberWithInteger:0xFFFFFFFF]];
[textField setToolTip: NSLocalizedString( @"Required format: integer number", nil)];
} else if ([tag.vr isEqualToString:@"US"]) { //unsigned short
[nf setAllowsFloats:NO];
[nf setMinimum:[NSNumber numberWithInteger:0]];
[nf setMaximum:[NSNumber numberWithInteger:0xFFFF]];
[textField setToolTip: NSLocalizedString( @"Required format: integer number", nil)];
} else if ([tag.vr isEqualToString:@"FL"]) { //float
[textField setToolTip: NSLocalizedString( @"Required format: floating point number", nil)];
} else if ([tag.vr isEqualToString:@"FD"]) { //double
[textField setToolTip: NSLocalizedString( @"Required format: floating point number", nil)];
}
}
NSButtonCell* rmButtonCell = [[N2HighlightImageButtonCell alloc] initWithImage:[NSImage imageNamed:@"MinusButton"]];
NSButton* rmButton = [[NSButton alloc] initWithFrame:NSZeroRect];
rmButton.cell = rmButtonCell;
[rmButtonCell release];
rmButton.target = self;
rmButton.action = @selector(rmButtonAction:);
[self addSubview:rmButton];
[textField bind:@"enabled" toObject:checkBox.cell withKeyPath:@"state" options:NULL];
[checkBox.cell addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionInitial context:textField];
[textField addObserver:self forKeyPath:@"formatIsOk" options:NSKeyValueObservingOptionInitial context:textField];
NSArray* group = [NSArray arrayWithObjects: checkBox, textField, rmButton, tag, NULL];
[viewGroups addObject:group];
[self resizeSubviewsWithOldSize:self.frame.size];
[checkBox release];
[textField release];
[rmButton release];
}
-(void)removeTag:(DCMAttributeTag*)tag {
NSArray* group = [self groupForObject:tag];
if (!group) return;
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSControlTextDidEndEditingNotification object:[group objectAtIndex:1]];
// [[NSNotificationCenter defaultCenter] removeObserver:self name:NSControlTextDidChangeNotification object:[group objectAtIndex:1]];
[[[group objectAtIndex:0] cell] removeObserver:self forKeyPath:@"state"];
[[group objectAtIndex:1] removeObserver:self forKeyPath:@"formatIsOk"];
// [[group objectAtIndex:1] removeObserver:self forKeyPath:@"value"];
[[group objectAtIndex:0] removeFromSuperview];
[[group objectAtIndex:1] removeFromSuperview];
[[group objectAtIndex:2] removeFromSuperview];
[viewGroups removeObject:group];
[self resizeSubviewsWithOldSize:self.frame.size];
}
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)obj change:(NSDictionary*)change context:(void*)context {
N2TextField* textField = (id)context;
if ([textField isKindOfClass:[NSTextField class]]) {
NSButton* checkBox = [self checkBoxForObject:textField];
[textField setBackgroundColor: (checkBox.state&&textField.stringValue.length&&!textField.formatIsOk)? [NSColor colorWithCalibratedHue:[[NSColor orangeColor] hueComponent] saturation:0.25 brightness:1 alpha:1] : [NSColor whiteColor] ];
}
}
-(void)observeTextDidChange:(NSNotification*)notification {
NSTextField* textField = notification.object;
[self observeValueForKeyPath:NULL ofObject:NULL change:NULL context:textField];
}
-(NSButton*)checkBoxForObject:(id)object {
return [[self groupForObject:object] objectAtIndex:0];
}
-(N2TextField*)textFieldForObject:(id)object {
return [[self groupForObject:object] objectAtIndex:1];
}
-(NSSize)idealSize {
NSInteger columnCount = self.columnCount, rowCount = self.rowCount;
float rC = rowCount-1;
if( rC < 0) rC = 0;
float cC = columnCount-1;
if( cC < 0) cC = 0;
return NSMakeSize(cellSize.width*columnCount+intercellSpacing.width*cC, cellSize.height*rowCount+intercellSpacing.height*rC);
}
-(void)resizeSubviewsWithOldSize:(NSSize)oldSize {
cellSize = NSMakeSize((self.frame.size.width-intercellSpacing.width*(self.columnCount-1))/2,17);
for (NSArray* group in viewGroups)
[self repositionGroupViews:group];
[self repositionAddTagInterface];
}
@end