forked from pixmeo/osirix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadCell.mm
270 lines (214 loc) · 9.67 KB
/
ThreadCell.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
/*=========================================================================
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 "ThreadCell.h"
#import "ThreadsManager.h"
#import "BrowserController.h"
#import "NSString+N2.h"
#import "NSThread+N2.h"
#import "N2Debug.h"
#import "N2Operators.h"
#import "AppController.h"
@implementation ThreadCell
@synthesize progressIndicator = _progressIndicator;
@synthesize cancelButton = _cancelButton;
@synthesize thread = _thread;
@synthesize manager = _manager;
@synthesize view = _view;
-(id)initWithThread:(NSThread*)thread manager:(ThreadsManager*)manager view:(NSTableView*)view {
self = [super init];
_view = view;
_manager = manager;
_progressIndicator = [[NSProgressIndicator alloc] initWithFrame:NSZeroRect];
[_progressIndicator setUsesThreadedAnimation:YES];
[_progressIndicator setMinValue:0];
[_progressIndicator setMaxValue:1];
_cancelButton = [[NSButton alloc] initWithFrame:NSZeroRect];
[_cancelButton setImage:[NSImage imageNamed:@"Activity_Stop"]];
[_cancelButton setAlternateImage:[NSImage imageNamed:@"Activity_StopPressed"]];
[_cancelButton setBordered:NO];
[_cancelButton setButtonType:NSMomentaryChangeButton];
_cancelButton.target = self;
_cancelButton.action = @selector(cancelThreadAction:);
_lastDisplayedProgress = -1;
self.thread = thread;
return self;
}
-(void)cleanup
{
if( _progressIndicator == nil && _cancelButton == nil && KVOObserving == NO)
return;
if( [NSThread isMainThread] == NO)
N2LogStackTrace( @"We shoud be on MAIN thread");
@synchronized( _thread)
{
[_progressIndicator removeFromSuperview];
[_progressIndicator autorelease]; _progressIndicator = nil;
_cancelButton.target = nil;
_cancelButton.action = nil;
[_cancelButton removeFromSuperview];
[_cancelButton autorelease]; _cancelButton = nil;
[self.view reloadData];
[self.view setNeedsDisplay: YES];
if( KVOObserving)
{
[_thread removeObserver:self forKeyPath:NSThreadSupportsCancelKey];
[_thread removeObserver:self forKeyPath:NSThreadProgressKey];
[_thread removeObserver:self forKeyPath:NSThreadStatusKey];
[_thread removeObserver:self forKeyPath:NSThreadIsCancelledKey];
KVOObserving = NO;
}
}
}
-(void)dealloc
{
[self cleanup];
[_thread autorelease];
[_retainedThreadDictionary autorelease];
[super dealloc];
}
-(void)setThread:(NSThread*)thread {
@synchronized( _thread)
{
@try
{
if( KVOObserving)
{
[_thread removeObserver:self forKeyPath:NSThreadSupportsCancelKey];
[_thread removeObserver:self forKeyPath:NSThreadProgressKey];
[_thread removeObserver:self forKeyPath:NSThreadStatusKey];
[_thread removeObserver:self forKeyPath:NSThreadIsCancelledKey];
KVOObserving = NO;
}
}
@catch ( NSException *e)
{
N2LogException( e);
}
[_thread autorelease];
[_retainedThreadDictionary autorelease];
_thread = [thread retain];
@synchronized( _thread)
{
_retainedThreadDictionary = [_thread.threadDictionary retain];
if( _retainedThreadDictionary)
{
[_thread addObserver:self forKeyPath:NSThreadIsCancelledKey options:NSKeyValueObservingOptionInitial context:NULL];
[_thread addObserver:self forKeyPath:NSThreadStatusKey options:NSKeyValueObservingOptionInitial context:NULL];
[_thread addObserver:self forKeyPath:NSThreadProgressKey options:NSKeyValueObservingOptionInitial context:NULL];
[_thread addObserver:self forKeyPath:NSThreadSupportsCancelKey options:NSKeyValueObservingOptionInitial context:NULL];
KVOObserving = YES;
}
}
}
}
-(void)_observeValueForKeyPathOfObjectChangeContext:(NSArray*)args {
[self observeValueForKeyPath:[args objectAtIndex:0] ofObject:[args objectAtIndex:1] change:[args objectAtIndex:2] context:[[args objectAtIndex:3] pointerValue]];
}
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(NSThread*)obj change:(NSDictionary*)change context:(void*)context
{
if (obj == _thread)
{
@synchronized( _thread)
{
if( _thread.isFinished)
return;
if( _retainedThreadDictionary != _thread.threadDictionary)
return;
}
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(_observeValueForKeyPathOfObjectChangeContext:) withObject:[NSArray arrayWithObjects: keyPath, obj, change, [NSValue valueWithPointer:context], NULL] waitUntilDone:NO];
return;
}
if ([keyPath isEqualToString:NSThreadStatusKey]) {
[self.view setNeedsDisplayInRect: [self.view rectOfRow:[self.manager.threads indexOfObject:self.thread]]];
return;
} else if ([keyPath isEqualToString:NSThreadProgressKey]) {
[self.progressIndicator setDoubleValue:self.thread.subthreadsAwareProgress];
[self.progressIndicator setIndeterminate: self.thread.progress < 0];
if (self.thread.progress < 0) [self.progressIndicator startAnimation:self];
if (fabs(_lastDisplayedProgress-obj.progress) > 1.0/self.progressIndicator.frame.size.width) {
_lastDisplayedProgress = obj.progress;
[self.progressIndicator setNeedsDisplay: YES];
}
return;
} else if ([keyPath isEqualToString:NSThreadSupportsCancelKey] || [keyPath isEqualToString:NSThreadIsCancelledKey]) {
[self.cancelButton setHidden:(!self.thread.supportsCancel)||self.thread.isCancelled];
[self.cancelButton setEnabled:self.thread.supportsCancel];
return;
}
}
[super observeValueForKeyPath:keyPath ofObject:obj change:change context:context];
}
-(void)cancelThreadAction:(id)source
{
@synchronized( _thread)
{
if( [self.thread isFinished] == NO)
{
_thread.status = NSLocalizedString( @"Cancelling...", nil);
[_thread setIsCancelled:YES];
}
}
}
-(void)drawInteriorWithFrame:(NSRect)frame inView:(NSView*)view {
@synchronized( _thread)
{
if ([_thread isFinished])
return;
}
NSMutableParagraphStyle* paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary* textAttributes = [NSMutableDictionary dictionaryWithObjectsAndKeys: [self textColor], NSForegroundColorAttributeName, [NSFont labelFontOfSize:[[BrowserController currentBrowser] fontSize: @"threadNameSize"]], NSFontAttributeName, paragraphStyle, NSParagraphStyleAttributeName, NULL];
[NSGraphicsContext saveGraphicsState];
NSString* tempName;
NSString* tempStatus;
@synchronized (_thread) {
tempName = [[_thread.name retain] autorelease];
tempStatus = [[_thread.status retain] autorelease];
}
NSRect nameFrame = NSMakeRect(frame.origin.x+3, frame.origin.y-1, frame.size.width-23, frame.size.height);
if (!tempName) tempName = NSLocalizedString(@"Unspecified Task", nil);
[tempName drawWithRect:nameFrame options:NSStringDrawingUsesLineFragmentOrigin+NSStringDrawingTruncatesLastVisibleLine attributes:textAttributes];
NSRect statusFrame = [self statusFrame];
[textAttributes setObject:[NSFont labelFontOfSize: [[BrowserController currentBrowser] fontSize: @"threadNameStatus"]] forKey:NSFontAttributeName];
if (!tempStatus) tempStatus = @"";
[tempStatus drawWithRect:statusFrame options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes];
if (![self.progressIndicator superview]) {
[view addSubview:self.progressIndicator];
[self.progressIndicator setIndeterminate:YES];
[self.progressIndicator startAnimation:self];
}
NSRect progressFrame = NSMakeRect(frame.origin.x+3, frame.origin.y + (frame.size.height - 12), frame.size.width-6, 10);
if (!NSEqualRects(self.progressIndicator.frame, progressFrame))
[self.progressIndicator setFrame:progressFrame];
[NSGraphicsContext restoreGraphicsState];
}
-(void)drawWithFrame:(NSRect)frame inView:(NSView*)view {
// { // for debug
// [NSGraphicsContext saveGraphicsState];
//
// [[NSColor colorWithCalibratedRed:1 green:0 blue:0 alpha:0.25] setFill];
// [NSBezierPath fillRect:frame];
//
// [NSGraphicsContext restoreGraphicsState];
// }
[self drawInteriorWithFrame:frame inView:view];
[NSGraphicsContext saveGraphicsState];
[[[NSColor grayColor] colorWithAlphaComponent:0.5] set];
[NSBezierPath strokeLineFromPoint:frame.origin+NSMakeSize(-2, frame.size.height) toPoint:frame.origin+frame.size+NSMakeSize(2,0)];
[NSGraphicsContext restoreGraphicsState];
}
-(NSRect)statusFrame
{
NSRect frame = [self.view rectOfRow:[self.manager.threads indexOfObject:self.thread]];
return NSMakeRect(frame.origin.x+3, frame.origin.y + [[BrowserController currentBrowser] fontSize: @"threadCellLineSpace"], frame.size.width-22, frame.size.height- (frame.origin.y + [[BrowserController currentBrowser] fontSize: @"threadCellLineSpace"]));
}
@end