source: trunk/macosx/PiecesView.m @ 9134

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

remove unnecessary alloca

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