transmission/macosx/PiecesView.m

214 lines
6.4 KiB
Mathematica
Raw Normal View History

2007-09-16 01:02:06 +00:00
/******************************************************************************
* $Id$
*
2008-01-02 16:55:05 +00:00
* Copyright (c) 2006-2008 Transmission authors and contributors
2007-09-16 01:02:06 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "PiecesView.h"
#import "InfoWindowController.h"
2008-06-09 22:44:08 +00:00
#import "CTGradient.h"
2007-09-16 01:02:06 +00:00
#define MAX_ACROSS 18
#define BETWEEN 1.0
#define HIGH_PEERS 15
#define FINISHED 1
2007-09-16 01:02:06 +00:00
@implementation PiecesView
- (void) awakeFromNib
{
//back image
fBack = [[NSImage alloc] initWithSize: [self bounds].size];
[fBack lockFocus];
2008-06-09 22:44:08 +00:00
CTGradient * gradient = [CTGradient gradientWithBeginningColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4]
endingColor: [NSColor colorWithCalibratedWhite: 0.2 alpha: 0.4]];
2008-06-09 22:44:08 +00:00
[gradient fillRect: [self bounds] angle: 90.0];
2007-09-16 01:02:06 +00:00
[fBack unlockFocus];
//store box colors
fGreenAvailabilityColor = [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] retain];
fBluePieceColor = [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] retain];
2007-09-16 01:02:06 +00:00
//actually draw the box
[self setTorrent: nil];
}
- (void) dealloc
{
2008-06-09 21:20:01 +00:00
tr_free(fPieces);
2007-09-16 01:02:06 +00:00
[fBack release];
[fGreenAvailabilityColor release];
[fBluePieceColor release];
2007-09-16 01:02:06 +00:00
[super dealloc];
}
- (void) setTorrent: (Torrent *) torrent
{
[self clearView];
2007-09-16 01:02:06 +00:00
2008-06-09 21:20:01 +00:00
fTorrent = torrent;
if (fTorrent)
2007-09-16 01:02:06 +00:00
{
//determine relevant values
2008-06-09 21:20:01 +00:00
fNumPieces = MIN([fTorrent pieceCount], MAX_ACROSS * MAX_ACROSS);
fAcross = ceil(sqrt(fNumPieces));
2007-09-16 01:02:06 +00:00
float width = [self bounds].size.width;
fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
}
//reset the view to blank
NSImage * newBack = [fBack copy];
[self setImage: newBack];
[newBack release];
[self setNeedsDisplay];
2007-09-16 01:02:06 +00:00
}
- (void) clearView
2008-06-09 21:20:01 +00:00
{
tr_free(fPieces);
fPieces = NULL;
}
- (void) updateView
2007-09-16 01:02:06 +00:00
{
if (!fTorrent)
return;
2008-06-09 21:20:01 +00:00
//determine if first time
BOOL first = NO;
if (!fPieces)
2007-09-16 01:02:06 +00:00
{
2008-06-09 21:20:01 +00:00
fPieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
first = YES;
2007-09-16 01:02:06 +00:00
}
2008-06-09 21:20:01 +00:00
2007-09-16 01:02:06 +00:00
NSImage * image = [self image];
2008-06-09 21:20:01 +00:00
int8_t * pieces = NULL;
float * piecesPercent = NULL;
2007-09-16 01:02:06 +00:00
BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
if (showAvailablity)
{
2008-06-09 21:20:01 +00:00
pieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
2007-09-16 01:02:06 +00:00
[fTorrent getAvailability: pieces size: fNumPieces];
}
else
{
2008-06-09 21:20:01 +00:00
piecesPercent = (float *)tr_malloc(fNumPieces * sizeof(float));
2007-09-16 01:02:06 +00:00
[fTorrent getAmountFinished: piecesPercent size: fNumPieces];
}
2008-06-09 22:44:08 +00:00
int i, j, index = -1;
2007-09-16 01:02:06 +00:00
NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
2008-06-09 21:20:01 +00:00
2007-09-16 01:02:06 +00:00
BOOL change = NO;
2007-09-16 01:02:06 +00:00
for (i = 0; i < fAcross; i++)
for (j = 0; j < fAcross; j++)
{
index++;
if (index >= fNumPieces)
{
i = fAcross;
break;
}
NSColor * pieceColor;
2007-09-16 01:02:06 +00:00
if (showAvailablity)
{
if (pieces[index] == -1)
pieceColor = !first && fPieces[index] != FINISHED ? [NSColor orangeColor] : fBluePieceColor;
2007-09-16 01:02:06 +00:00
else
{
float percent;
if (pieces[index] < HIGH_PEERS)
percent = (float)pieces[index]/HIGH_PEERS;
else
percent = 1.0;
pieceColor = [[NSColor whiteColor] blendedColorWithFraction: percent ofColor: fGreenAvailabilityColor];
2007-09-16 01:02:06 +00:00
}
fPieces[index] = pieces[index] == -1 ? FINISHED : 0;
2007-09-16 01:02:06 +00:00
}
else
{
if (piecesPercent[index] == 1.0 && !first && fPieces[index] != FINISHED)
pieceColor = [NSColor orangeColor];
2007-09-16 01:02:06 +00:00
else
pieceColor = [[NSColor whiteColor] blendedColorWithFraction: piecesPercent[index] ofColor: fBluePieceColor];
fPieces[index] = piecesPercent[index] == 1.0 ? FINISHED : 0;
2007-09-16 01:02:06 +00:00
}
if (pieceColor)
{
//drawing actually will occur
if (!change)
{
[image lockFocus];
change = YES;
}
rect.origin = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
[image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
2008-06-09 21:20:01 +00:00
[pieceColor set];
2007-09-16 01:02:06 +00:00
NSRectFill(rect);
}
}
if (change)
{
[image unlockFocus];
[self setNeedsDisplay];
}
2008-06-09 21:20:01 +00:00
tr_free(pieces);
tr_free(piecesPercent);
2007-09-16 01:02:06 +00:00
}
- (BOOL) acceptsFirstMouse: (NSEvent *) event
{
return YES;
}
- (void) mouseDown: (NSEvent *) event
{
if (fTorrent)
[[[self window] windowController] setPiecesViewForAvailable:
![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];
[super mouseDown: event];
}
@end