source: trunk/macosx/PiecesView.m @ 10448

Last change on this file since 10448 was 10448, checked in by livings124, 12 years ago

restore ability to toggle pieces view by clicking on it

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