source: trunk/macosx/TorrentCell.m @ 1575

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

seeding bar shows progress towards ratio

  • Property svn:keywords set to Date Rev Author Id
File size: 12.7 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 1575 2007-03-22 04:30:08Z livings124 $
3 *
4 * Copyright (c) 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 "TorrentCell.h"
26#import "TorrentTableView.h"
27#import "StringAdditions.h"
28
29//also defined in Torrent.m
30#define BAR_HEIGHT 12.0
31
32@interface TorrentCell (Private)
33
34- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point;
35- (void) buildSimpleBar: (float) width point: (NSPoint) point;
36- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point;
37
38@end
39
40@implementation TorrentCell
41
42- (id) init
43{
44    if ((self = [super init]))
45    {
46        fDefaults = [NSUserDefaults standardUserDefaults];
47       
48        fStatusRegular = [fDefaults boolForKey: @"SmallStatusRegular"];
49   
50        NSSize startSize = NSMakeSize(100.0, BAR_HEIGHT);
51       
52        fProgressWhite = [NSImage imageNamed: @"ProgressBarWhite.png"];
53        [fProgressWhite setScalesWhenResized: YES];
54       
55        fProgressBlue = [NSImage imageNamed: @"ProgressBarBlue.png"];
56        [fProgressBlue setScalesWhenResized: YES];
57        [fProgressBlue setSize: startSize];
58       
59        fProgressGray = [NSImage imageNamed: @"ProgressBarGray.png"];
60        [fProgressGray setScalesWhenResized: YES];
61        [fProgressGray setSize: startSize];
62       
63        fProgressGreen = [NSImage imageNamed: @"ProgressBarGreen.png"];
64        [fProgressGreen setScalesWhenResized: YES];
65       
66        fProgressLightGreen = [NSImage imageNamed: @"ProgressBarLightGreen.png"];
67        [fProgressLightGreen setScalesWhenResized: YES];
68       
69        fProgressAdvanced = [NSImage imageNamed: @"ProgressBarAdvanced.png"];
70        [fProgressAdvanced setScalesWhenResized: YES];
71       
72        fProgressEndWhite = [NSImage imageNamed: @"ProgressBarEndWhite.png"];
73        fProgressEndBlue = [NSImage imageNamed: @"ProgressBarEndBlue.png"];
74        fProgressEndGray = [NSImage imageNamed: @"ProgressBarEndGray.png"];
75        fProgressEndGreen = [NSImage imageNamed: @"ProgressBarEndGreen.png"];
76        fProgressEndAdvanced = [NSImage imageNamed: @"ProgressBarEndAdvanced.png"];
77       
78        fErrorImage = [[NSImage imageNamed: @"Error.tiff"] copy]; //no need to release for some reason
79        [fErrorImage setFlipped: YES];
80    }
81    return self;
82}
83
84- (void) placeBar: (NSImage *) barImage width: (float) width point: (NSPoint) point
85{
86    if ([barImage size].width < width)
87        [barImage setSize: NSMakeSize(width * 2.0, BAR_HEIGHT)];
88
89    [barImage compositeToPoint: point fromRect: NSMakeRect(0, 0, width, BAR_HEIGHT) operation: NSCompositeSourceOver];
90}
91
92- (void) buildSimpleBar: (float) width point: (NSPoint) point
93{
94    NSDictionary * info = [self objectValue];
95
96    width -= 2.0;
97   
98    BOOL seeding = [[info objectForKey: @"Seeding"] boolValue],
99        isActive = [[info objectForKey: @"Active"] boolValue];
100    float completedWidth, remainingWidth;
101   
102    //bar images
103    NSImage * barLeftEnd, * barRightEnd, * barComplete, * barRemaining;
104    if (seeding)
105    {
106        float stopRatio, ratio;
107        if ((stopRatio = [[info objectForKey: @"StopRatio"] floatValue]) != INVALID
108                && (ratio = [[info objectForKey: @"Ratio"] floatValue]) < stopRatio)
109            completedWidth = width * ratio / stopRatio;
110        else
111            completedWidth = width;
112        remainingWidth = width - completedWidth;
113       
114        barLeftEnd = fProgressEndGreen;
115        barRightEnd = fProgressEndGreen;
116        barComplete = fProgressGreen;
117        barRemaining = fProgressLightGreen;
118    }
119    else
120    {
121        completedWidth = [[info objectForKey: @"Progress"] floatValue] * width;
122        remainingWidth = width - completedWidth;
123       
124        if (remainingWidth == width)
125            barLeftEnd = fProgressEndWhite;
126        else if (isActive)
127            barLeftEnd = fProgressEndBlue;
128        else
129            barLeftEnd = fProgressEndGray;
130       
131        if (completedWidth < width)
132            barRightEnd = fProgressEndWhite;
133        else if (isActive)
134            barRightEnd = fProgressEndBlue;
135        else
136            barRightEnd = fProgressEndGray;
137       
138        barComplete = isActive ? fProgressBlue : fProgressGray;
139        barRemaining = fProgressWhite;
140    }
141   
142    [barLeftEnd compositeToPoint: point operation: NSCompositeSourceOver];
143   
144    //active bar
145    point.x += 1.0;
146    [self placeBar: barComplete width: completedWidth point: point];
147   
148    //remaining bar
149    point.x += completedWidth;
150    [self placeBar: barRemaining width: remainingWidth point: point];
151   
152    point.x += remainingWidth;
153    [barRightEnd compositeToPoint: point operation: NSCompositeSourceOver];
154}
155
156- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point
157{
158    NSDictionary * info = [self objectValue];
159   
160    //draw overlay over advanced bar
161    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
162   
163    widthFloat -= 2.0;
164    point.x += 1.0;
165   
166    //place actual advanced bar
167    NSImage * image = [info objectForKey: @"AdvancedBar"];
168    [image setSize: NSMakeSize(widthFloat, BAR_HEIGHT)];
169    [image compositeToPoint: point operation: NSCompositeSourceOver];
170   
171    [self placeBar: fProgressAdvanced width: widthFloat point: point];
172   
173    point.x += widthFloat;
174    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
175}
176
177- (void) toggleMinimalStatus
178{
179    fStatusRegular = !fStatusRegular;
180    [fDefaults setBool: fStatusRegular forKey: @"SmallStatusRegular"];
181}
182
183- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
184{
185    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view]
186                                                        isEqual: [NSColor alternateSelectedControlColor]];
187   
188    NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
189    [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
190   
191    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
192                    highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
193                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
194                    paragraphStyle, NSParagraphStyleAttributeName, nil];
195    NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
196                    highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
197                    [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
198                    paragraphStyle, NSParagraphStyleAttributeName, nil];
199    [paragraphStyle release];
200
201    NSPoint pen = cellFrame.origin;
202    const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0;
203   
204    NSDictionary * info = [self objectValue];
205   
206    if (![fDefaults boolForKey: @"SmallView"]) //regular size
207    {
208        //icon
209        NSImage * icon = [info objectForKey: @"Icon"];
210        NSSize iconSize = [icon size];
211       
212        pen.x += PADDING;
213        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
214       
215        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
216                operation: NSCompositeSourceOver fraction: 1.0];
217       
218        //error badge
219        if ([[info objectForKey: @"Error"] boolValue])
220        {
221            NSSize errorIconSize = [fErrorImage size];
222            [fErrorImage drawAtPoint: NSMakePoint(pen.x + iconSize.width - errorIconSize.width,
223                                                    pen.y + iconSize.height  - errorIconSize.height)
224                fromRect: NSMakeRect(0, 0, errorIconSize.width, errorIconSize.height)
225                operation: NSCompositeSourceOver fraction: 1.0];
226        }
227
228        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
229
230        //name string
231        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
232        pen.y = cellFrame.origin.y + PADDING;
233       
234        NSString * nameString = [info objectForKey: @"Name"];
235        NSSize nameSize = [nameString sizeWithAttributes: nameAttributes];
236        [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, nameSize.height) withAttributes: nameAttributes];
237       
238        //progress string
239        pen.y += nameSize.height + LINE_PADDING - 1.0;
240       
241        NSString * progressString = [info objectForKey: @"ProgressString"];
242        NSSize progressSize = [progressString sizeWithAttributes: statusAttributes];
243        [progressString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, progressSize.height) withAttributes: statusAttributes];
244
245        //progress bar
246        pen.x -= EXTRA_NAME_SHIFT;
247        pen.y += progressSize.height + LINE_PADDING + BAR_HEIGHT;
248       
249        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
250       
251        if ([fDefaults boolForKey: @"UseAdvancedBar"])
252            [self buildAdvancedBar: barWidth point: pen];
253        else
254            [self buildSimpleBar: barWidth point: pen];
255
256        //status string
257        pen.x += EXTRA_NAME_SHIFT;
258        pen.y += LINE_PADDING;
259       
260        NSString * statusString = [info objectForKey: @"StatusString"];
261        NSSize statusSize = [statusString sizeWithAttributes: statusAttributes];
262        [statusString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, statusSize.height) withAttributes: statusAttributes];
263    }
264    else //small size
265    {
266        //icon
267        NSImage * icon = ![[info objectForKey: @"Error"] boolValue] ? [info objectForKey: @"Icon"] : fErrorImage;
268        NSSize iconSize = [icon size];
269       
270        pen.x += PADDING;
271        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
272       
273        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
274                operation: NSCompositeSourceOver fraction: 1.0];
275
276        //name and status string
277        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
278                     
279        //place name string
280        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
281        pen.y = cellFrame.origin.y + LINE_PADDING;
282
283        NSString * nameString = [info objectForKey: @"Name"];
284        NSSize nameSize = [nameString sizeWithAttributes: nameAttributes];
285       
286        NSString * statusString = !fStatusRegular && [[info objectForKey: @"Active"] boolValue]
287                                        ? [info objectForKey: @"RemainingTimeString"]
288                                        : [info objectForKey: @"ShortStatusString"];
289        NSSize statusSize = [statusString sizeWithAttributes: statusAttributes];
290       
291        [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth - statusSize.width - 2.0 * LINE_PADDING, nameSize.height)
292                        withAttributes: nameAttributes];
293       
294        //place status string
295        pen.x = NSMaxX(cellFrame) - PADDING - statusSize.width;
296        pen.y += (nameSize.height - statusSize.height) * 0.5;
297       
298        [statusString drawInRect: NSMakeRect(pen.x, pen.y, statusSize.width, statusSize.height)
299                        withAttributes: statusAttributes];
300       
301        //progress bar
302        pen.x = cellFrame.origin.x + iconSize.width + 2.0 * PADDING;
303        pen.y = cellFrame.origin.y + nameSize.height + LINE_PADDING + PADDING + BAR_HEIGHT;
304       
305        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
306       
307        if ([fDefaults boolForKey: @"UseAdvancedBar"])
308            [self buildAdvancedBar: barWidth point: pen];
309        else
310            [self buildSimpleBar: barWidth point: pen];
311    }
312   
313    [nameAttributes release];
314    [statusAttributes release];
315}
316
317@end
Note: See TracBrowser for help on using the repository browser.