source: trunk/macosx/PiecesView.m @ 1021

Last change on this file since 1021 was 1021, checked in by livings124, 15 years ago

flash individual pieces red when they complete

File size: 11.7 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2005-2006 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) init
33{
34    if ((self = [super init]))
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 = [fImageView bounds].size;
47        NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView 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        //red box image
66        fRedPiece = [[NSImage alloc] initWithSize: size];
67       
68        [fRedPiece lockFocus];
69        [[NSColor redColor] set];
70        [bp fill];
71        [fRedPiece 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        [fImageView setToolTip: [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]
138                            ? @"Piece Availability" : @"Piece Progress"];
139       
140        //actually draw the box
141        [self setTorrent: nil];
142}
143
144- (void) dealloc
145{
146    free(fPieces);
147   
148    [fBack release];
149    [fWhitePiece release];
150    [fRedPiece release];
151    [fBluePiece release];
152    [fGreen1Piece release];
153    [fGreen2Piece release];
154    [fGreen3Piece release];
155   
156    if (fTorrent)
157        [fTorrent release];
158    [super dealloc];
159}
160
161- (void) setTorrent: (Torrent *) torrent
162{
163    if (fTorrent)
164    {
165        [fTorrent release];
166       
167        if (!torrent)
168            fTorrent = nil;
169    }
170   
171    if (torrent)
172    {
173        fTorrent = [torrent retain];
174       
175        //determine relevant values
176        fNumPieces = MAX_ACROSS * MAX_ACROSS;
177        int pieceCount = [fTorrent pieceCount];
178        if (pieceCount < fNumPieces)
179        {
180            fNumPieces = pieceCount;
181           
182            fAcross = sqrt(fNumPieces);
183            if (fAcross * fAcross < fNumPieces)
184                fAcross++;
185        }
186        else
187            fAcross = MAX_ACROSS;
188       
189        float width = [fImageView bounds].size.width;
190        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
191        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
192       
193        [self updateView: YES];
194    }
195   
196    [fImageView setHidden: torrent == nil];
197}
198
199- (void) updateView: (BOOL) first
200{
201    if (!fTorrent)
202        return;
203   
204    if (first)
205        [fImageView setImage: [[fBack copy] autorelease]];
206    NSImage * image = [fImageView image];
207
208    int8_t * pieces;
209    float * piecesPercent;
210   
211    BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
212    if (showAvailablity)
213    {   
214        pieces = malloc(fNumPieces);
215        [fTorrent getAvailability: pieces size: fNumPieces];
216    }
217    else
218    {   
219        piecesPercent = malloc(fNumPieces * sizeof(float));
220        [fTorrent getAmountFinished: piecesPercent size: fNumPieces];
221    }
222   
223    int i, j, piece, index = -1;
224    float piecePercent;
225    NSPoint point;
226    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
227    NSImage * pieceImage;
228    BOOL change = NO;
229       
230    for (i = 0; i < fAcross; i++)
231        for (j = 0; j < fAcross; j++)
232        {
233            index++;
234            if (index >= fNumPieces)
235            {
236                i = fAcross;
237                break;
238            }
239           
240            pieceImage = nil;
241           
242            if (showAvailablity)
243            {
244                piece = pieces[index];
245                if (piece < 0)
246                {
247                    if (first || fPieces[index] == -2)
248                    {
249                        fPieces[index] = -1;
250                        pieceImage = fBluePiece;
251                    }
252                    else if (fPieces[index] != -1)
253                    {
254                        fPieces[index] = -2;
255                        pieceImage = fRedPiece;
256                    }
257                    else;
258                }
259                else if (piece == 0)
260                {
261                    if (first || fPieces[index] != 0)
262                    {
263                        fPieces[index] = 0;
264                        pieceImage = fWhitePiece;
265                    }
266                }
267                else if (piece == 1)
268                {
269                    if (first || fPieces[index] != 1)
270                    {
271                        fPieces[index] = 1;
272                        pieceImage = fGreen1Piece;
273                    }
274                }
275                else if (piece == 2)
276                {
277                    if (first || fPieces[index] != 2)
278                    {
279                        fPieces[index] = 2;
280                        pieceImage = fGreen2Piece;
281                    }
282                }
283                else
284                {
285                    if (first || fPieces[index] != 3)
286                    {
287                        fPieces[index] = 3;
288                        pieceImage = fGreen3Piece;
289                    }
290                }
291            }
292            else
293            {
294                piecePercent = piecesPercent[index];
295                if (piecePercent >= 1.0)
296                {
297                    if (first || fPieces[index] == -2)
298                    {
299                        fPieces[index] = -1;
300                        pieceImage = fBluePiece;
301                    }
302                    else if (fPieces[index] != -1)
303                    {
304                        fPieces[index] = -2;
305                        pieceImage = fRedPiece;
306                    }
307                    else;
308                }
309                else if (piecePercent <= 0.0)
310                {
311                    if (first || fPieces[index] != 0)
312                    {
313                        fPieces[index] = 0;
314                        pieceImage = fWhitePiece;
315                    }
316                }
317                else if (piecePercent < 0.25)
318                {
319                    if (first || fPieces[index] != 1)
320                    {
321                        fPieces[index] = 1;
322                        pieceImage = fBlue1Piece;
323                    }
324                }
325                else if (piecePercent < 0.50)
326                {
327                    if (first || fPieces[index] != 2)
328                    {
329                        fPieces[index] = 2;
330                        pieceImage = fBlue2Piece;
331                    }
332                }
333                else if (piecePercent < 0.75)
334                {
335                    if (first || fPieces[index] != 3)
336                    {
337                        fPieces[index] = 3;
338                        pieceImage = fBlue3Piece;
339                    }
340                }
341                else
342                {
343                    if (first || fPieces[index] != 4)
344                    {
345                        fPieces[index] = 4;
346                        pieceImage = fBlue4Piece;
347                    }
348                }
349            }
350           
351            if (pieceImage)
352            {
353                //drawing actually will occur, so figure out values
354                if (!change)
355                {
356                    [image lockFocus];
357                    change = YES;
358                }
359               
360                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
361                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
362                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
363            }
364        }
365   
366    if (change)
367    {
368        [image unlockFocus];
369        [fImageView setNeedsDisplay];
370    }
371   
372    if (showAvailablity)
373        free(pieces);
374    else
375        free(piecesPercent);
376}
377
378- (void) toggleView
379{
380    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
381    BOOL showAvailability = ![defaults boolForKey: @"PiecesViewShowAvailability"];
382   
383    [defaults setBool: showAvailability forKey: @"PiecesViewShowAvailability"];
384   
385    [fImageView setToolTip: showAvailability ? @"Piece Availability" : @"Piece Progress"];
386   
387    [self updateView: YES];
388}
389
390@end
Note: See TracBrowser for help on using the repository browser.