source: trunk/macosx/PiecesView.m @ 995

Last change on this file since 995 was 995, checked in by livings124, 16 years ago

For now the pieces view will show the completeness of the torrent...colors, etc. might change

File size: 11.0 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2005-2006 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
27#define MAX_ACROSS 18
28#define BETWEEN 1.0
29
30@implementation PiecesView
31
32- (id) init
33{
34    if ((self = [super init]))
35    {
36        fTorrent = nil;
37        int numPieces = MAX_ACROSS * MAX_ACROSS;
38        fPieces = malloc(numPieces);
39    }
40   
41    return self;
42}
43
44- (void) awakeFromNib
45{
46        NSSize size = [fImageView bounds].size;
47        NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView bounds]];
48       
49        //back image
50        fBack = [[NSImage alloc] initWithSize: size];
51       
52        [fBack lockFocus];
53        [[NSColor blackColor] set];
54        [bp fill];
55        [fBack unlockFocus];
56       
57        //white box image
58        fWhitePiece = [[NSImage alloc] initWithSize: size];
59       
60        [fWhitePiece lockFocus];
61        [[NSColor whiteColor] set];
62        [bp fill];
63        [fWhitePiece unlockFocus];
64       
65        //red box image
66        fRedPiece = [[NSImage alloc] initWithSize: size];
67       
68        [fRedPiece lockFocus];
69        [[NSColor redColor] set];
70        [bp fill];
71        [fRedPiece unlockFocus];
72       
73        //green 1 box image
74        fGreen1Piece = [[NSImage alloc] initWithSize: size];
75       
76        [fGreen1Piece lockFocus];
77        [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 0.8 alpha: 1.0] set];
78        [bp fill];
79        [fGreen1Piece unlockFocus];
80       
81        //green 2 box image
82        fGreen2Piece = [[NSImage alloc] initWithSize: size];
83       
84        [fGreen2Piece lockFocus];
85        [[NSColor colorWithCalibratedRed: 0.4 green: 1.0 blue: 0.6 alpha: 1.0] set];
86        [bp fill];
87        [fGreen2Piece unlockFocus];
88       
89        //green 3 box image
90        fGreen3Piece = [[NSImage alloc] initWithSize: size];
91       
92        [fGreen3Piece lockFocus];
93        [[NSColor colorWithCalibratedRed: 0.0 green: 1.0 blue: 0.4 alpha: 1.0] set];
94        [bp fill];
95        [fGreen3Piece unlockFocus];
96       
97        //blue 1 box image
98        fBlue1Piece = [[NSImage alloc] initWithSize: size];
99       
100        [fBlue1Piece lockFocus];
101        [[NSColor colorWithCalibratedRed: 0.8 green: 1.0 blue: 1.0 alpha: 1.0] set];
102        [bp fill];
103        [fBlue1Piece unlockFocus];
104       
105        //blue 2 box image
106        fBlue2Piece = [[NSImage alloc] initWithSize: size];
107       
108        [fBlue2Piece lockFocus];
109        [[NSColor colorWithCalibratedRed: 0.6 green: 1.0 blue: 1.0 alpha: 1.0] set];
110        [bp fill];
111        [fBlue2Piece unlockFocus];
112       
113        //blue 3 box image
114        fBlue3Piece = [[NSImage alloc] initWithSize: size];
115       
116        [fBlue3Piece lockFocus];
117        [[NSColor colorWithCalibratedRed: 0.6 green: 0.8 blue: 1.0 alpha: 1.0] set];
118        [bp fill];
119        [fBlue3Piece unlockFocus];
120       
121        //blue 4 box image
122        fBlue4Piece = [[NSImage alloc] initWithSize: size];
123       
124        [fBlue4Piece lockFocus];
125        [[NSColor colorWithCalibratedRed: 0.4 green: 0.6 blue: 1.0 alpha: 1.0] set];
126        [bp fill];
127        [fBlue4Piece unlockFocus];
128       
129        //blue box image
130        fBluePiece = [[NSImage alloc] initWithSize: size];
131       
132        [fBluePiece lockFocus];
133        [[NSColor colorWithCalibratedRed: 0.0 green: 0.4 blue: 0.8 alpha: 1.0] set];
134        [bp fill];
135        [fBluePiece unlockFocus];
136       
137        //actually draw the box
138        [self setTorrent: nil];
139}
140
141- (void) dealloc
142{
143    free(fPieces);
144   
145    [fBack release];
146    [fWhitePiece release];
147    [fRedPiece release];
148    [fBluePiece release];
149    [fGreen1Piece release];
150    [fGreen2Piece release];
151    [fGreen3Piece release];
152   
153    if (fTorrent)
154        [fTorrent release];
155    [super dealloc];
156}
157
158- (void) setTorrent: (Torrent *) torrent
159{
160    if (fTorrent)
161    {
162        [fTorrent release];
163       
164        if (!torrent)
165            fTorrent = nil;
166    }
167   
168    if (torrent)
169    {
170        fTorrent = [torrent retain];
171       
172        //determine relevant values
173        fNumPieces = MAX_ACROSS * MAX_ACROSS;
174        int pieceCount = [fTorrent pieceCount];
175        if (pieceCount < fNumPieces)
176        {
177            fNumPieces = pieceCount;
178           
179            fAcross = sqrt(fNumPieces);
180            if (fAcross * fAcross < fNumPieces)
181                fAcross++;
182        }
183        else
184            fAcross = MAX_ACROSS;
185       
186        float width = [fImageView bounds].size.width;
187        fWidth = (width - (fAcross + 1) * BETWEEN) / fAcross;
188        fExtraBorder = (width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
189       
190        [self updateView: YES];
191    }
192   
193    [fImageView setHidden: torrent == nil];
194}
195
196- (void) updateView: (BOOL) first
197{
198    if (!fTorrent)
199        return;
200   
201    if (first)
202        [fImageView setImage: [[fBack copy] autorelease]];
203    NSImage * image = [fImageView image];
204
205    int8_t * pieces = malloc(fNumPieces);
206   
207    BOOL showAvailablity = [[NSUserDefaults standardUserDefaults] boolForKey: @"PiecesViewShowAvailability"];
208   
209    if (showAvailablity)
210        [fTorrent getAvailability: pieces size: fNumPieces];
211    else
212        [fTorrent getAmountFinished: pieces size: fNumPieces];
213   
214    int i, j, piece, index = -1;
215    NSPoint point;
216    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
217    NSImage * pieceImage;
218    BOOL change = NO;
219       
220    for (i = 0; i < fAcross; i++)
221        for (j = 0; j < fAcross; j++)
222        {
223            index++;
224            if (index >= fNumPieces)
225            {
226                i = fAcross;
227                break;
228            }
229           
230            pieceImage = nil;
231            piece = pieces[index];
232           
233            if (showAvailablity)
234            {
235                if (piece < 0)
236                {
237                    if (first || fPieces[index] == -2)
238                    {
239                        fPieces[index] = -1;
240                        pieceImage = fBluePiece;
241                    }
242                    else if (fPieces[index] != -1)
243                    {
244                        fPieces[index] = -2;
245                        pieceImage = fRedPiece;
246                    }
247                    else;
248                }
249                else if (piece == 0)
250                {
251                    if (first || fPieces[index] != 0)
252                    {
253                        fPieces[index] = 0;
254                        pieceImage = fWhitePiece;
255                    }
256                }
257                else if (piece == 1)
258                {
259                    if (first || fPieces[index] != 1)
260                    {
261                        fPieces[index] = 1;
262                        pieceImage = fGreen1Piece;
263                    }
264                }
265                else if (piece == 2)
266                {
267                    if (first || fPieces[index] != 2)
268                    {
269                        fPieces[index] = 2;
270                        pieceImage = fGreen2Piece;
271                    }
272                }
273                else
274                {
275                    if (first || fPieces[index] != 3)
276                    {
277                        fPieces[index] = 3;
278                        pieceImage = fGreen3Piece;
279                    }
280                }
281            }
282            else
283            {
284                /*if (i==0)
285                    pieceImage = fBlue1Piece;
286                else if (i==1)
287                    pieceImage = fBlue2Piece;
288                else if (i==2)
289                    pieceImage = fBlue3Piece;
290                else if (i==3)
291                    pieceImage = fBlue4Piece;
292                else if (i==4)
293                    pieceImage = fBluePiece;
294               
295                else */if (piece <= 0)
296                {
297                    if (first || fPieces[index] != 0)
298                    {
299                        fPieces[index] = 0;
300                        pieceImage = fWhitePiece;
301                    }
302                }
303                else if (piece < 25)
304                {
305                    if (first || fPieces[index] != 1)
306                    {
307                        fPieces[index] = 1;
308                        pieceImage = fBlue1Piece;
309                    }
310                }
311                else if (piece < 50)
312                {
313                    if (first || fPieces[index] != 2)
314                    {
315                        fPieces[index] = 2;
316                        pieceImage = fBlue2Piece;
317                    }
318                }
319                else if (piece < 75)
320                {
321                    if (first || fPieces[index] != 3)
322                    {
323                        fPieces[index] = 3;
324                        pieceImage = fBlue3Piece;
325                    }
326                }
327                else if (piece < 100)
328                {
329                    if (first || fPieces[index] != 4)
330                    {
331                        fPieces[index] = 4;
332                        pieceImage = fBlue4Piece;
333                    }
334                }
335                else
336                {
337                    if (first || fPieces[index] != 5)
338                    {
339                        fPieces[index] = 5;
340                        pieceImage = fBluePiece;
341                    }
342                }
343            }
344           
345            if (pieceImage)
346            {
347                //drawing actually will occur, so figure out values
348                if (!change)
349                {
350                    [image lockFocus];
351                    change = YES;
352                }
353               
354                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
355                                    [image size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
356                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
357            }
358        }
359   
360    if (change)
361    {
362        [image unlockFocus];
363        [fImageView setNeedsDisplay];
364    }
365   
366    free(pieces);
367}
368
369@end
Note: See TracBrowser for help on using the repository browser.