source: trunk/macosx/PiecesView.m @ 1004

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

Pieces View background is light gray instead of black

File size: 11.9 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 (i==0)
296                    pieceImage = fBlue1Piece;
297                else if (i==1)
298                    pieceImage = fBlue2Piece;
299                else if (i==2)
300                    pieceImage = fBlue3Piece;
301                else if (i==3)
302                    pieceImage = fBlue4Piece;
303                else if (i==4)
304                    pieceImage = fBluePiece;
305               
306                else */if (piecePercent <= 0.0)
307                {
308                    if (first || fPieces[index] != 0)
309                    {
310                        fPieces[index] = 0;
311                        pieceImage = fWhitePiece;
312                    }
313                }
314                else if (piecePercent < 0.25)
315                {
316                    if (first || fPieces[index] != 1)
317                    {
318                        fPieces[index] = 1;
319                        pieceImage = fBlue1Piece;
320                    }
321                }
322                else if (piecePercent < 0.50)
323                {
324                    if (first || fPieces[index] != 2)
325                    {
326                        fPieces[index] = 2;
327                        pieceImage = fBlue2Piece;
328                    }
329                }
330                else if (piecePercent < 0.75)
331                {
332                    if (first || fPieces[index] != 3)
333                    {
334                        fPieces[index] = 3;
335                        pieceImage = fBlue3Piece;
336                    }
337                }
338                else if (piecePercent < 1.0)
339                {
340                    if (first || fPieces[index] != 4)
341                    {
342                        fPieces[index] = 4;
343                        pieceImage = fBlue4Piece;
344                    }
345                }
346                else
347                {
348                    if (first || fPieces[index] != 5)
349                    {
350                        fPieces[index] = 5;
351                        pieceImage = fBluePiece;
352                    }
353                }
354            }
355           
356            if (pieceImage)
357            {
358                //drawing actually will occur, so figure out values
359                if (!change)
360                {
361                    [image lockFocus];
362                    change = YES;
363                }
364               
365                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
366                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
367                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
368            }
369        }
370   
371    if (change)
372    {
373        [image unlockFocus];
374        [fImageView setNeedsDisplay];
375    }
376   
377    if (showAvailablity)
378        free(pieces);
379    else
380        free(piecesPercent);
381}
382
383- (void) toggleView
384{
385    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
386    BOOL showAvailability = ![defaults boolForKey: @"PiecesViewShowAvailability"];
387   
388    [defaults setBool: showAvailability forKey: @"PiecesViewShowAvailability"];
389   
390    [fImageView setToolTip: showAvailability ? @"Piece Availability" : @"Piece Progress"];
391   
392    [self updateView: YES];
393}
394
395@end
Note: See TracBrowser for help on using the repository browser.