source: trunk/macosx/PiecesView.m @ 2149

Last change on this file since 2149 was 2149, checked in by livings124, 15 years ago

Merge file selection and torrent creation into the main branch.

The new code for these features is under a new license.

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