source: trunk/macosx/PiecesView.m @ 9139

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

remove unneeded group column resize code

  • 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 9139 2009-09-18 02:18:22Z 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    //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    const BOOL first = fPieces == NULL;
112    if (first)
113        fPieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
114
115    int8_t * pieces = NULL;
116    float * piecesPercent = NULL;
117   
118    const BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
119    if (showAvailablity)
120    {   
121        pieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
122        [fTorrent getAvailability: pieces size: fNumPieces];
123    }
124    else
125    {   
126        piecesPercent = (float *)tr_malloc(fNumPieces * sizeof(float));
127        [fTorrent getAmountFinished: piecesPercent size: fNumPieces];
128    }
129   
130    NSImage * image = [self image];
131   
132    NSRect fillRects[fNumPieces];
133    NSColor * fillColors[fNumPieces];
134   
135    NSInteger index = -1, usedCount = 0;
136   
137    for (NSInteger i = 0; i < fAcross; i++)
138        for (NSInteger j = 0; j < fAcross; j++)
139        {
140            index++;
141            if (index >= fNumPieces)
142            {
143                i = fAcross;
144                break;
145            }
146           
147            NSColor * pieceColor = nil;
148           
149            if (showAvailablity ? pieces[index] == -1 : piecesPercent[index] == 1.0)
150            {
151                if (first || fPieces[index] != PIECE_FINISHED)
152                {
153                    if (!first && fPieces[index] != PIECE_FLASHING)
154                    {
155                        pieceColor = [NSColor orangeColor];
156                        fPieces[index] = PIECE_FLASHING;
157                    }
158                    else
159                    {
160                        pieceColor = fBluePieceColor;
161                        fPieces[index] = PIECE_FINISHED;
162                    }
163                }
164            }
165            else if (showAvailablity ? pieces[index] == 0 : piecesPercent[index] == 0.0)
166            {
167                if (first || fPieces[index] != PIECE_NONE)
168                {
169                    pieceColor = [NSColor whiteColor];
170                    fPieces[index] = PIECE_NONE;
171                }
172            }
173            else if (showAvailablity && pieces[index] >= HIGH_PEERS)
174            {
175                if (first || fPieces[index] != PIECE_HIGH_PEERS)
176                {
177                    pieceColor = fGreenAvailabilityColor;
178                    fPieces[index] = PIECE_HIGH_PEERS;
179                }
180            }
181            else
182            {
183                //always redraw "mixed"
184                CGFloat percent = showAvailablity ? (CGFloat)pieces[index]/HIGH_PEERS : piecesPercent[index];
185                NSColor * fullColor = showAvailablity ? fGreenAvailabilityColor : fBluePieceColor;
186                pieceColor = [[NSColor whiteColor] blendedColorWithFraction: percent ofColor: fullColor];
187                fPieces[index] = PIECE_SOME;
188            }
189           
190            if (pieceColor)
191            {
192                fillRects[usedCount] = NSMakeRect(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
193                                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder,
194                                                    fWidth, fWidth);
195                fillColors[usedCount] = pieceColor;
196               
197                usedCount++;
198            }
199        }
200   
201    if (usedCount > 0)
202    {
203        [image lockFocus];
204        NSRectFillListWithColors(fillRects, fillColors, usedCount);
205        [image unlockFocus];
206        [self setNeedsDisplay];
207    }
208   
209    tr_free(pieces);
210    tr_free(piecesPercent);
211}
212
213- (BOOL) acceptsFirstMouse: (NSEvent *) event
214{
215    return YES;
216}
217
218- (void) mouseDown: (NSEvent *) event
219{
220    if (fTorrent)
221        [[[self window] windowController] setPiecesViewForAvailable:
222            ![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];
223    [super mouseDown: event];
224}
225
226@end
Note: See TracBrowser for help on using the repository browser.