2007-09-16 01:02:06 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2010-01-01 21:12:04 +00:00
|
|
|
* Copyright (c) 2006-2010 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"
|
2008-11-01 15:22:32 +00:00
|
|
|
#import "Torrent.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "InfoWindowController.h"
|
2008-10-25 20:54:09 +00:00
|
|
|
#import "utils.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
#define MAX_ACROSS 18
|
2009-09-18 02:18:22 +00:00
|
|
|
#define BETWEEN 1.0
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2008-06-10 22:54:30 +00:00
|
|
|
#define HIGH_PEERS 30
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2009-10-18 02:12:43 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PIECE_NONE,
|
|
|
|
PIECE_SOME,
|
|
|
|
PIECE_HIGH_PEERS,
|
|
|
|
PIECE_FINISHED,
|
|
|
|
PIECE_FLASHING
|
|
|
|
};
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation PiecesView
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
2008-06-17 17:17:15 +00:00
|
|
|
//store box colors
|
2009-10-18 01:54:13 +00:00
|
|
|
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];
|
|
|
|
|
2008-06-17 17:17:15 +00:00
|
|
|
//actually draw the box
|
|
|
|
[self setTorrent: nil];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2008-06-09 21:20:01 +00:00
|
|
|
tr_free(fPieces);
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2008-06-10 19:56:53 +00:00
|
|
|
[fGreenAvailabilityColor release];
|
|
|
|
[fBluePieceColor release];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setTorrent: (Torrent *) torrent
|
|
|
|
{
|
2008-06-09 23:23:55 +00:00
|
|
|
[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
|
|
|
|
2009-10-18 02:12:43 +00:00
|
|
|
const CGFloat width = [self bounds].size.width;
|
2007-09-16 01:02:06 +00:00
|
|
|
fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
|
|
|
|
fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
|
|
|
|
}
|
2008-06-09 23:23:55 +00:00
|
|
|
|
2009-10-18 01:54:13 +00:00
|
|
|
NSImage * back = [[NSImage alloc] initWithSize: [self bounds].size];
|
|
|
|
[back lockFocus];
|
|
|
|
|
|
|
|
NSGradient * gradient = [[NSGradient alloc] initWithStartingColor: [NSColor colorWithCalibratedWhite: 0.0 alpha: 0.4]
|
|
|
|
endingColor: [NSColor colorWithCalibratedWhite: 0.2 alpha: 0.4]];
|
|
|
|
[gradient drawInRect: [self bounds] angle: 90.0];
|
|
|
|
[gradient release];
|
|
|
|
[back unlockFocus];
|
|
|
|
|
|
|
|
[self setImage: back];
|
|
|
|
[back release];
|
2008-06-09 23:23:55 +00:00
|
|
|
|
|
|
|
[self setNeedsDisplay];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2008-06-09 23:23:55 +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
|
2009-09-18 02:18:22 +00:00
|
|
|
const BOOL first = fPieces == NULL;
|
|
|
|
if (first)
|
2008-06-09 21:20:01 +00:00
|
|
|
fPieces = (int8_t *)tr_malloc(fNumPieces * sizeof(int8_t));
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2008-06-09 21:20:01 +00:00
|
|
|
int8_t * pieces = NULL;
|
2008-11-02 14:04:57 +00:00
|
|
|
float * piecesPercent = NULL;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-09-18 02:18:22 +00:00
|
|
|
const BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
|
2007-09-16 01:02:06 +00:00
|
|
|
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-11-02 14:04:57 +00:00
|
|
|
piecesPercent = (float *)tr_malloc(fNumPieces * sizeof(float));
|
2007-09-16 01:02:06 +00:00
|
|
|
[fTorrent getAmountFinished: piecesPercent size: fNumPieces];
|
|
|
|
}
|
|
|
|
|
2008-06-10 21:46:34 +00:00
|
|
|
NSImage * image = [self image];
|
|
|
|
|
2009-09-17 23:33:50 +00:00
|
|
|
NSRect fillRects[fNumPieces];
|
|
|
|
NSColor * fillColors[fNumPieces];
|
|
|
|
|
2009-10-18 02:12:43 +00:00
|
|
|
NSInteger usedCount = 0;
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2009-10-18 02:23:10 +00:00
|
|
|
for (NSInteger index = 0; index < fNumPieces; index++)
|
|
|
|
{
|
|
|
|
NSColor * pieceColor = nil;
|
|
|
|
|
|
|
|
if (showAvailablity ? pieces[index] == -1 : piecesPercent[index] == 1.0)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2009-10-18 02:23:10 +00:00
|
|
|
if (first || fPieces[index] != PIECE_FINISHED)
|
2008-06-09 22:45:31 +00:00
|
|
|
{
|
2009-10-18 02:23:10 +00:00
|
|
|
if (!first && fPieces[index] != PIECE_FLASHING)
|
2008-06-10 20:15:00 +00:00
|
|
|
{
|
2009-10-18 02:23:10 +00:00
|
|
|
pieceColor = [NSColor orangeColor];
|
|
|
|
fPieces[index] = PIECE_FLASHING;
|
2008-06-10 21:13:33 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
else
|
2008-06-10 21:13:33 +00:00
|
|
|
{
|
2009-10-18 02:23:10 +00:00
|
|
|
pieceColor = fBluePieceColor;
|
|
|
|
fPieces[index] = PIECE_FINISHED;
|
2008-06-10 20:15:00 +00:00
|
|
|
}
|
2008-06-10 21:33:13 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
|
|
|
else if (showAvailablity ? pieces[index] == 0 : piecesPercent[index] == 0.0)
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] != PIECE_NONE)
|
2008-06-10 21:33:13 +00:00
|
|
|
{
|
2009-10-18 02:23:10 +00:00
|
|
|
pieceColor = [NSColor whiteColor];
|
|
|
|
fPieces[index] = PIECE_NONE;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
|
|
|
else if (showAvailablity && pieces[index] >= HIGH_PEERS)
|
|
|
|
{
|
|
|
|
if (first || fPieces[index] != PIECE_HIGH_PEERS)
|
2008-06-09 22:45:31 +00:00
|
|
|
{
|
2009-10-18 02:23:10 +00:00
|
|
|
pieceColor = fGreenAvailabilityColor;
|
|
|
|
fPieces[index] = PIECE_HIGH_PEERS;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//always redraw "mixed"
|
|
|
|
CGFloat percent = showAvailablity ? (CGFloat)pieces[index]/HIGH_PEERS : piecesPercent[index];
|
|
|
|
NSColor * fullColor = showAvailablity ? fGreenAvailabilityColor : fBluePieceColor;
|
|
|
|
pieceColor = [[NSColor whiteColor] blendedColorWithFraction: percent ofColor: fullColor];
|
|
|
|
fPieces[index] = PIECE_SOME;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pieceColor)
|
|
|
|
{
|
|
|
|
const NSInteger across = index % fAcross,
|
|
|
|
down = index / fAcross;
|
|
|
|
fillRects[usedCount] = NSMakeRect(across * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
|
|
|
|
[image size].width - (down + 1) * (fWidth + BETWEEN) - fExtraBorder,
|
|
|
|
fWidth, fWidth);
|
|
|
|
fillColors[usedCount] = pieceColor;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-10-18 02:23:10 +00:00
|
|
|
usedCount++;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2009-09-18 02:18:22 +00:00
|
|
|
if (usedCount > 0)
|
2008-06-10 21:46:34 +00:00
|
|
|
{
|
2009-09-17 23:33:50 +00:00
|
|
|
[image lockFocus];
|
2009-09-18 02:18:22 +00:00
|
|
|
NSRectFillListWithColors(fillRects, fillColors, usedCount);
|
2008-06-10 21:46:34 +00:00
|
|
|
[image unlockFocus];
|
|
|
|
[self setNeedsDisplay];
|
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
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
|
|
|
|
{
|
2010-03-06 23:12:30 +00:00
|
|
|
#warning fix
|
|
|
|
/*if (fTorrent)
|
2007-09-16 01:02:06 +00:00
|
|
|
[[[self window] windowController] setPiecesViewForAvailable:
|
2010-03-06 23:12:30 +00:00
|
|
|
![[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"]];*/
|
2007-09-16 01:02:06 +00:00
|
|
|
[super mouseDown: event];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|