source: trunk/macosx/PiecesView.m @ 9317

Last change on this file since 9317 was 9317, checked in by livings124, 13 years ago

don't store a copy of the pieces view's background - regenerate it when needed instead

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 6.9 KB
Line 
1/******************************************************************************
2 * $Id: PiecesView.m 9317 2009-10-18 01:54:13Z livings124 $
3 *
4 * Copyright (c) 2006-2009 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#import "Torrent.h"
27#import "InfoWindowController.h"
28#import "utils.h"
29
30#define MAX_ACROSS 18
31#define BETWEEN 1.0
32
33#define HIGH_PEERS 30
34
35#define PIECE_NONE 0
36#define PIECE_SOME 1
37#define PIECE_HIGH_PEERS 2
38#define PIECE_FINISHED 3
39#define PIECE_FLASHING 4
40
41@implementation PiecesView
42
43- (void) awakeFromNib
44{
45    //store box colors
46    fGreenAvailabilityColor = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain];
47    fBluePieceColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
48   
49    //actually draw the box
50    [self setTorrent: nil];
51}
52
53- (void) dealloc
54{
55    tr_free(fPieces);
56   
57    [fGreenAvailabilityColor release];
58    [fBluePieceColor release];
59   
60    [super dealloc];
61}
62
63- (void) setTorrent: (Torrent *) torrent
64{
65    [self clearView];
66   
67    fTorrent = torrent;
68    if (fTorrent)
69    {
70        //determine relevant values
71        fNumPieces = MIN([fTorrent pieceCount], MAX_ACROSS * MAX_ACROSS);
72        fAcross = ceil(sqrt(fNumPieces));
73       
74        CGFloat width = [self bounds].size.width;
75        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
76        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
77    }
78   
79    NSImage * back = [[NSImage alloc] initWithSize: [self bounds].size];
80    [back lockFocus];
81   
82    NSGradient * gradient = [[NSGradient alloc] initWithStartingColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4]
83                                endingColor: [NSColor colorWithCalibratedWhite: 0.2 alpha: 0.4]];
84    [gradient drawInRect: [self bounds] angle: 90.0];
85    [gradient release];
86    [back unlockFocus];
87   
88    [self setImage: back];
89    [back release];
90   
91    [self setNeedsDisplay];
92}
93
94- (void) clearView
95{
96    tr_free(fPieces);
97    fPieces = NULL;
98}
99
100- (void) updateView
101{
102    if (!fTorrent)
103        return;
104   
105    //determine if first time
106    const BOOL first = fPieces == NULL;
107    if (first)
108        fPieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
109
110    int8_t * pieces = NULL;
111    float * piecesPercent = NULL;
112   
113    const BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
114    if (showAvailablity)
115    {   
116        pieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
117        [fTorrent getAvailability: pieces size: fNumPieces];
118    }
119    else
120    {   
121        piecesPercent = (float *)tr_malloc(fNumPieces * sizeof(float));
122        [fTorrent getAmountFinished: piecesPercent size: fNumPieces];
123    }
124   
125    NSImage * image = [self image];
126   
127    NSRect fillRects[fNumPieces];
128    NSColor * fillColors[fNumPieces];
129   
130    NSInteger index = -1, usedCount = 0;
131   
132    for (NSInteger i = 0; i < fAcross; i++)
133        for (NSInteger j = 0; j < fAcross; j++)
134        {
135            index++;
136            if (index >= fNumPieces)
137            {
138                i = fAcross;
139                break;
140            }
141           
142            NSColor * pieceColor = nil;
143           
144            if (showAvailablity ? pieces[index] == -1 : piecesPercent[index] == 1.0)
145            {
146                if (first || fPieces[index] != PIECE_FINISHED)
147                {
148                    if (!first && fPieces[index] != PIECE_FLASHING)
149                    {
150                        pieceColor = [NSColor orangeColor];
151                        fPieces[index] = PIECE_FLASHING;
152                    }
153                    else
154                    {
155                        pieceColor = fBluePieceColor;
156                        fPieces[index] = PIECE_FINISHED;
157                    }
158                }
159            }
160            else if (showAvailablity ? pieces[index] == 0 : piecesPercent[index] == 0.0)
161            {
162                if (first || fPieces[index] != PIECE_NONE)
163                {
164                    pieceColor = [NSColor whiteColor];
165                    fPieces[index] = PIECE_NONE;
166                }
167            }
168            else if (showAvailablity && pieces[index] >= HIGH_PEERS)
169            {
170                if (first || fPieces[index] != PIECE_HIGH_PEERS)
171                {
172                    pieceColor = fGreenAvailabilityColor;
173                    fPieces[index] = PIECE_HIGH_PEERS;
174                }
175            }
176            else
177            {
178                //always redraw "mixed"
179                CGFloat percent = showAvailablity ? (CGFloat)pieces[index]/HIGH_PEERS : piecesPercent[index];
180                NSColor * fullColor = showAvailablity ? fGreenAvailabilityColor : fBluePieceColor;
181                pieceColor = [[NSColor whiteColor] blendedColorWithFraction: percent ofColor: fullColor];
182                fPieces[index] = PIECE_SOME;
183            }
184           
185            if (pieceColor)
186            {
187                fillRects[usedCount] = NSMakeRect(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
188                                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder,
189                                                    fWidth, fWidth);
190                fillColors[usedCount] = pieceColor;
191               
192                usedCount++;
193            }
194        }
195   
196    if (usedCount > 0)
197    {
198        [image lockFocus];
199        NSRectFillListWithColors(fillRects, fillColors, usedCount);
200        [image unlockFocus];
201        [self setNeedsDisplay];
202    }
203   
204    tr_free(pieces);
205    tr_free(piecesPercent);
206}
207
208- (BOOL) acceptsFirstMouse: (NSEvent *) event
209{
210    return YES;
211}
212
213- (void) mouseDown: (NSEvent *) event
214{
215    if (fTorrent)
216        [[[self window] windowController] setPiecesViewForAvailable:
217            ![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];
218    [super mouseDown: event];
219}
220
221@end
Note: See TracBrowser for help on using the repository browser.