1 | /****************************************************************************** |
---|
2 | * $Id$ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2007 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "PiecesView.h" |
---|
26 | |
---|
27 | #define MAX_ACROSS 18 |
---|
28 | #define BETWEEN 1.0 |
---|
29 | |
---|
30 | @implementation PiecesView |
---|
31 | |
---|
32 | - (id) initWithCoder: (NSCoder *) decoder |
---|
33 | { |
---|
34 | if ((self = [super initWithCoder: decoder])) |
---|
35 | { |
---|
36 | fTorrent = nil; |
---|
37 | int numPieces = MAX_ACROSS * MAX_ACROSS; |
---|
38 | fPieces = malloc(numPieces); |
---|
39 | } |
---|
40 | |
---|
41 | return self; |
---|
42 | } |
---|
43 | |
---|
44 | - (void) awakeFromNib |
---|
45 | { |
---|
46 | NSSize size = [self bounds].size; |
---|
47 | NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [self bounds]]; |
---|
48 | |
---|
49 | //back image |
---|
50 | fBack = [[NSImage alloc] initWithSize: size]; |
---|
51 | |
---|
52 | [fBack lockFocus]; |
---|
53 | [[NSColor lightGrayColor] set]; |
---|
54 | [bp fill]; |
---|
55 | [fBack unlockFocus]; |
---|
56 | |
---|
57 | //white box image |
---|
58 | fWhitePiece = [[NSImage alloc] initWithSize: size]; |
---|
59 | |
---|
60 | [fWhitePiece lockFocus]; |
---|
61 | [[NSColor whiteColor] set]; |
---|
62 | [bp fill]; |
---|
63 | [fWhitePiece unlockFocus]; |
---|
64 | |
---|
65 | //orange box image |
---|
66 | fOrangePiece = [[NSImage alloc] initWithSize: size]; |
---|
67 | |
---|
68 | [fOrangePiece lockFocus]; |
---|
69 | [[NSColor orangeColor] set]; |
---|
70 | [bp fill]; |
---|
71 | [fOrangePiece unlockFocus]; |
---|
72 | |
---|
73 | //green 1 box image |
---|
74 | fGreen1Piece = [[NSImage alloc] initWithSize: size]; |
---|
75 | |
---|
76 | [fGreen1Piece lockFocus]; |
---|
77 | [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] set]; |
---|
78 | [bp fill]; |
---|
79 | [fGreen1Piece unlockFocus]; |
---|
80 | |
---|
81 | //green 2 box image |
---|
82 | fGreen2Piece = [[NSImage alloc] initWithSize: size]; |
---|
83 | |
---|
84 | [fGreen2Piece lockFocus]; |
---|
85 | [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] set]; |
---|
86 | [bp fill]; |
---|
87 | [fGreen2Piece unlockFocus]; |
---|
88 | |
---|
89 | //green 3 box image |
---|
90 | fGreen3Piece = [[NSImage alloc] initWithSize: size]; |
---|
91 | |
---|
92 | [fGreen3Piece lockFocus]; |
---|
93 | [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] set]; |
---|
94 | [bp fill]; |
---|
95 | [fGreen3Piece unlockFocus]; |
---|
96 | |
---|
97 | //blue 1 box image |
---|
98 | fBlue1Piece = [[NSImage alloc] initWithSize: size]; |
---|
99 | |
---|
100 | [fBlue1Piece lockFocus]; |
---|
101 | [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] set]; |
---|
102 | [bp fill]; |
---|
103 | [fBlue1Piece unlockFocus]; |
---|
104 | |
---|
105 | //blue 2 box image |
---|
106 | fBlue2Piece = [[NSImage alloc] initWithSize: size]; |
---|
107 | |
---|
108 | [fBlue2Piece lockFocus]; |
---|
109 | [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] set]; |
---|
110 | [bp fill]; |
---|
111 | [fBlue2Piece unlockFocus]; |
---|
112 | |
---|
113 | //blue 3 box image |
---|
114 | fBlue3Piece = [[NSImage alloc] initWithSize: size]; |
---|
115 | |
---|
116 | [fBlue3Piece lockFocus]; |
---|
117 | [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] set]; |
---|
118 | [bp fill]; |
---|
119 | [fBlue3Piece unlockFocus]; |
---|
120 | |
---|
121 | //blue 4 box image |
---|
122 | fBlue4Piece = [[NSImage alloc] initWithSize: size]; |
---|
123 | |
---|
124 | [fBlue4Piece lockFocus]; |
---|
125 | [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] set]; |
---|
126 | [bp fill]; |
---|
127 | [fBlue4Piece unlockFocus]; |
---|
128 | |
---|
129 | //blue box image |
---|
130 | fBluePiece = [[NSImage alloc] initWithSize: size]; |
---|
131 | |
---|
132 | [fBluePiece lockFocus]; |
---|
133 | [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] set]; |
---|
134 | [bp fill]; |
---|
135 | [fBluePiece unlockFocus]; |
---|
136 | |
---|
137 | [self setToolTip: [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"] |
---|
138 | ? NSLocalizedString(@"Piece Availability", "Inspector -> Activity -> detailed pieces view tooltip") |
---|
139 | : NSLocalizedString(@"Piece Progress", "Inspector -> Activity -> detailed pieces view tooltip")]; |
---|
140 | |
---|
141 | //actually draw the box |
---|
142 | [self setTorrent: nil]; |
---|
143 | } |
---|
144 | |
---|
145 | - (void) dealloc |
---|
146 | { |
---|
147 | free(fPieces); |
---|
148 | |
---|
149 | [fBack release]; |
---|
150 | [fWhitePiece release]; |
---|
151 | [fOrangePiece release]; |
---|
152 | [fBluePiece release]; |
---|
153 | [fGreen1Piece release]; |
---|
154 | [fGreen2Piece release]; |
---|
155 | [fGreen3Piece release]; |
---|
156 | |
---|
157 | if (fTorrent) |
---|
158 | [fTorrent release]; |
---|
159 | [super dealloc]; |
---|
160 | } |
---|
161 | |
---|
162 | - (void) setTorrent: (Torrent *) torrent |
---|
163 | { |
---|
164 | if (fTorrent) |
---|
165 | { |
---|
166 | [fTorrent release]; |
---|
167 | |
---|
168 | if (!torrent) |
---|
169 | fTorrent = nil; |
---|
170 | } |
---|
171 | |
---|
172 | if (torrent) |
---|
173 | { |
---|
174 | fTorrent = [torrent retain]; |
---|
175 | |
---|
176 | //determine relevant values |
---|
177 | fNumPieces = MAX_ACROSS * MAX_ACROSS; |
---|
178 | int pieceCount = [fTorrent pieceCount]; |
---|
179 | if (pieceCount < fNumPieces) |
---|
180 | { |
---|
181 | fNumPieces = pieceCount; |
---|
182 | |
---|
183 | fAcross = sqrt(fNumPieces); |
---|
184 | if (fAcross * fAcross < fNumPieces) |
---|
185 | fAcross++; |
---|
186 | } |
---|
187 | else |
---|
188 | fAcross = MAX_ACROSS; |
---|
189 | |
---|
190 | float width = [self bounds].size.width; |
---|
191 | fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross; |
---|
192 | fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2; |
---|
193 | |
---|
194 | [self updateView: YES]; |
---|
195 | } |
---|
196 | |
---|
197 | [self setHidden: torrent == nil]; |
---|
198 | } |
---|
199 | |
---|
200 | - (void) updateView: (BOOL) first |
---|
201 | { |
---|
202 | if (!fTorrent) |
---|
203 | return; |
---|
204 | |
---|
205 | if (first) |
---|
206 | [self setImage: [[fBack copy] autorelease]]; |
---|
207 | NSImage * image = [self image]; |
---|
208 | |
---|
209 | int8_t * pieces; |
---|
210 | float * piecesPercent; |
---|
211 | |
---|
212 | BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]; |
---|
213 | if (showAvailablity) |
---|
214 | { |
---|
215 | pieces = malloc(fNumPieces); |
---|
216 | [fTorrent getAvailability: pieces size: fNumPieces]; |
---|
217 | } |
---|
218 | else |
---|
219 | { |
---|
220 | piecesPercent = malloc(fNumPieces * sizeof(float)); |
---|
221 | [fTorrent getAmountFinished: piecesPercent size: fNumPieces]; |
---|
222 | } |
---|
223 | |
---|
224 | int i, j, piece, index = -1; |
---|
225 | float piecePercent; |
---|
226 | NSPoint point; |
---|
227 | NSRect rect = NSMakeRect(0, 0, fWidth, fWidth); |
---|
228 | NSImage * pieceImage; |
---|
229 | BOOL change = NO; |
---|
230 | |
---|
231 | for (i = 0; i < fAcross; i++) |
---|
232 | for (j = 0; j < fAcross; j++) |
---|
233 | { |
---|
234 | index++; |
---|
235 | if (index >= fNumPieces) |
---|
236 | { |
---|
237 | i = fAcross; |
---|
238 | break; |
---|
239 | } |
---|
240 | |
---|
241 | pieceImage = nil; |
---|
242 | |
---|
243 | if (showAvailablity) |
---|
244 | { |
---|
245 | piece = pieces[index]; |
---|
246 | if (piece < 0) |
---|
247 | { |
---|
248 | if (first || fPieces[index] == -2) |
---|
249 | { |
---|
250 | fPieces[index] = -1; |
---|
251 | pieceImage = fBluePiece; |
---|
252 | } |
---|
253 | else if (fPieces[index] != -1) |
---|
254 | { |
---|
255 | fPieces[index] = -2; |
---|
256 | pieceImage = fOrangePiece; |
---|
257 | } |
---|
258 | else; |
---|
259 | } |
---|
260 | else if (piece == 0) |
---|
261 | { |
---|
262 | if (first || fPieces[index] != 0) |
---|
263 | { |
---|
264 | fPieces[index] = 0; |
---|
265 | pieceImage = fWhitePiece; |
---|
266 | } |
---|
267 | } |
---|
268 | else if (piece <= 4) |
---|
269 | { |
---|
270 | if (first || fPieces[index] != 1) |
---|
271 | { |
---|
272 | fPieces[index] = 1; |
---|
273 | pieceImage = fGreen1Piece; |
---|
274 | } |
---|
275 | } |
---|
276 | else if (piece <= 8) |
---|
277 | { |
---|
278 | if (first || fPieces[index] != 2) |
---|
279 | { |
---|
280 | fPieces[index] = 2; |
---|
281 | pieceImage = fGreen2Piece; |
---|
282 | } |
---|
283 | } |
---|
284 | else |
---|
285 | { |
---|
286 | if (first || fPieces[index] != 3) |
---|
287 | { |
---|
288 | fPieces[index] = 3; |
---|
289 | pieceImage = fGreen3Piece; |
---|
290 | } |
---|
291 | } |
---|
292 | } |
---|
293 | else |
---|
294 | { |
---|
295 | piecePercent = piecesPercent[index]; |
---|
296 | if (piecePercent >= 1.0) |
---|
297 | { |
---|
298 | if (first || fPieces[index] == -2) |
---|
299 | { |
---|
300 | fPieces[index] = -1; |
---|
301 | pieceImage = fBluePiece; |
---|
302 | } |
---|
303 | else if (fPieces[index] != -1) |
---|
304 | { |
---|
305 | fPieces[index] = -2; |
---|
306 | pieceImage = fOrangePiece; |
---|
307 | } |
---|
308 | else; |
---|
309 | } |
---|
310 | else if (piecePercent <= 0.0) |
---|
311 | { |
---|
312 | if (first || fPieces[index] != 0) |
---|
313 | { |
---|
314 | fPieces[index] = 0; |
---|
315 | pieceImage = fWhitePiece; |
---|
316 | } |
---|
317 | } |
---|
318 | else if (piecePercent < 0.25) |
---|
319 | { |
---|
320 | if (first || fPieces[index] != 1) |
---|
321 | { |
---|
322 | fPieces[index] = 1; |
---|
323 | pieceImage = fBlue1Piece; |
---|
324 | } |
---|
325 | } |
---|
326 | else if (piecePercent < 0.5) |
---|
327 | { |
---|
328 | if (first || fPieces[index] != 2) |
---|
329 | { |
---|
330 | fPieces[index] = 2; |
---|
331 | pieceImage = fBlue2Piece; |
---|
332 | } |
---|
333 | } |
---|
334 | else if (piecePercent < 0.75) |
---|
335 | { |
---|
336 | if (first || fPieces[index] != 3) |
---|
337 | { |
---|
338 | fPieces[index] = 3; |
---|
339 | pieceImage = fBlue3Piece; |
---|
340 | } |
---|
341 | } |
---|
342 | else |
---|
343 | { |
---|
344 | if (first || fPieces[index] != 4) |
---|
345 | { |
---|
346 | fPieces[index] = 4; |
---|
347 | pieceImage = fBlue4Piece; |
---|
348 | } |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | if (pieceImage) |
---|
353 | { |
---|
354 | //drawing actually will occur, so figure out values |
---|
355 | if (!change) |
---|
356 | { |
---|
357 | [image lockFocus]; |
---|
358 | change = YES; |
---|
359 | } |
---|
360 | |
---|
361 | point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder, |
---|
362 | [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder); |
---|
363 | [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver]; |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | if (change) |
---|
368 | { |
---|
369 | [image unlockFocus]; |
---|
370 | [self setNeedsDisplay]; |
---|
371 | } |
---|
372 | |
---|
373 | if (showAvailablity) |
---|
374 | free(pieces); |
---|
375 | else |
---|
376 | free(piecesPercent); |
---|
377 | } |
---|
378 | |
---|
379 | - (BOOL) acceptsFirstMouse: (NSEvent *) event |
---|
380 | { |
---|
381 | return YES; |
---|
382 | } |
---|
383 | |
---|
384 | - (void) mouseDown: (NSEvent *) event |
---|
385 | { |
---|
386 | NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; |
---|
387 | BOOL showAvailability = ![defaults boolForKey: @"PiecesViewShowAvailability"]; |
---|
388 | |
---|
389 | [defaults setBool: showAvailability forKey: @"PiecesViewShowAvailability"]; |
---|
390 | |
---|
391 | [self setToolTip: showAvailability |
---|
392 | ? NSLocalizedString(@"Piece Availability", "Inspector -> Activity -> detailed pieces view tooltip") |
---|
393 | : NSLocalizedString(@"Piece Progress", "Inspector -> Activity -> detailed pieces view tooltip")]; |
---|
394 | |
---|
395 | [self updateView: YES]; |
---|
396 | |
---|
397 | [super mouseDown: event]; |
---|
398 | } |
---|
399 | |
---|
400 | @end |
---|