source: trunk/macosx/TorrentCell.m @ 1577

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

update progress bar when seeding immediately when the stop ratio setting is changed

  • Property svn:keywords set to Date Rev Author Id
File size: 12.7 KB
Line 
1/******************************************************************************
2 * $Id: TorrentCell.m 1577 2007-03-22 19:16: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 and widths
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    //place bar
143    [barLeftEnd compositeToPoint: point operation: NSCompositeSourceOver];
144   
145    point.x += 1.0;
146    [self placeBar: barComplete width: completedWidth point: point];
147   
148    point.x += completedWidth;
149    [self placeBar: barRemaining width: remainingWidth point: point];
150   
151    point.x += remainingWidth;
152    [barRightEnd compositeToPoint: point operation: NSCompositeSourceOver];
153}
154
155- (void) buildAdvancedBar: (float) widthFloat point: (NSPoint) point
156{
157    NSDictionary * info = [self objectValue];
158   
159    //draw overlay over advanced bar
160    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
161   
162    widthFloat -= 2.0;
163    point.x += 1.0;
164   
165    //place actual advanced bar
166    NSImage * image = [info objectForKey: @"AdvancedBar"];
167    [image setSize: NSMakeSize(widthFloat, BAR_HEIGHT)];
168    [image compositeToPoint: point operation: NSCompositeSourceOver];
169   
170    [self placeBar: fProgressAdvanced width: widthFloat point: point];
171   
172    point.x += widthFloat;
173    [fProgressEndAdvanced compositeToPoint: point operation: NSCompositeSourceOver];
174}
175
176- (void) toggleMinimalStatus
177{
178    fStatusRegular = !fStatusRegular;
179    [fDefaults setBool: fStatusRegular forKey: @"SmallStatusRegular"];
180}
181
182- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) view
183{
184    BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: view]
185                                                        isEqual: [NSColor alternateSelectedControlColor]];
186   
187    NSMutableParagraphStyle * paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
188    [paragraphStyle setLineBreakMode: NSLineBreakByTruncatingTail];
189   
190    NSDictionary * nameAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
191                    highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
192                    [NSFont messageFontOfSize: 12.0], NSFontAttributeName,
193                    paragraphStyle, NSParagraphStyleAttributeName, nil];
194    NSDictionary * statusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
195                    highlighted ? [NSColor whiteColor] : [NSColor darkGrayColor], NSForegroundColorAttributeName,
196                    [NSFont messageFontOfSize: 9.0], NSFontAttributeName,
197                    paragraphStyle, NSParagraphStyleAttributeName, nil];
198    [paragraphStyle release];
199
200    NSPoint pen = cellFrame.origin;
201    const float PADDING = 3.0, LINE_PADDING = 2.0, EXTRA_NAME_SHIFT = 1.0;
202   
203    NSDictionary * info = [self objectValue];
204   
205    if (![fDefaults boolForKey: @"SmallView"]) //regular size
206    {
207        //icon
208        NSImage * icon = [info objectForKey: @"Icon"];
209        NSSize iconSize = [icon size];
210       
211        pen.x += PADDING;
212        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
213       
214        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
215                operation: NSCompositeSourceOver fraction: 1.0];
216       
217        //error badge
218        if ([[info objectForKey: @"Error"] boolValue])
219        {
220            NSSize errorIconSize = [fErrorImage size];
221            [fErrorImage drawAtPoint: NSMakePoint(pen.x + iconSize.width - errorIconSize.width,
222                                                    pen.y + iconSize.height  - errorIconSize.height)
223                fromRect: NSMakeRect(0, 0, errorIconSize.width, errorIconSize.height)
224                operation: NSCompositeSourceOver fraction: 1.0];
225        }
226
227        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
228
229        //name string
230        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
231        pen.y = cellFrame.origin.y + PADDING;
232       
233        NSString * nameString = [info objectForKey: @"Name"];
234        NSSize nameSize = [nameString sizeWithAttributes: nameAttributes];
235        [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, nameSize.height) withAttributes: nameAttributes];
236       
237        //progress string
238        pen.y += nameSize.height + LINE_PADDING - 1.0;
239       
240        NSString * progressString = [info objectForKey: @"ProgressString"];
241        NSSize progressSize = [progressString sizeWithAttributes: statusAttributes];
242        [progressString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, progressSize.height) withAttributes: statusAttributes];
243
244        //progress bar
245        pen.x -= EXTRA_NAME_SHIFT;
246        pen.y += progressSize.height + LINE_PADDING + BAR_HEIGHT;
247       
248        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
249       
250        if ([fDefaults boolForKey: @"UseAdvancedBar"])
251            [self buildAdvancedBar: barWidth point: pen];
252        else
253            [self buildSimpleBar: barWidth point: pen];
254
255        //status string
256        pen.x += EXTRA_NAME_SHIFT;
257        pen.y += LINE_PADDING;
258       
259        NSString * statusString = [info objectForKey: @"StatusString"];
260        NSSize statusSize = [statusString sizeWithAttributes: statusAttributes];
261        [statusString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth, statusSize.height) withAttributes: statusAttributes];
262    }
263    else //small size
264    {
265        //icon
266        NSImage * icon = ![[info objectForKey: @"Error"] boolValue] ? [info objectForKey: @"Icon"] : fErrorImage;
267        NSSize iconSize = [icon size];
268       
269        pen.x += PADDING;
270        pen.y += (cellFrame.size.height - iconSize.height) * 0.5;
271       
272        [icon drawAtPoint: pen fromRect: NSMakeRect(0, 0, iconSize.width, iconSize.height)
273                operation: NSCompositeSourceOver fraction: 1.0];
274
275        //name and status string
276        float mainWidth = cellFrame.size.width - iconSize.width - 3.0 * PADDING - EXTRA_NAME_SHIFT;
277                     
278        //place name string
279        pen.x += iconSize.width + PADDING + EXTRA_NAME_SHIFT;
280        pen.y = cellFrame.origin.y + LINE_PADDING;
281
282        NSString * nameString = [info objectForKey: @"Name"];
283        NSSize nameSize = [nameString sizeWithAttributes: nameAttributes];
284       
285        NSString * statusString = !fStatusRegular && [[info objectForKey: @"Active"] boolValue]
286                                        ? [info objectForKey: @"RemainingTimeString"]
287                                        : [info objectForKey: @"ShortStatusString"];
288        NSSize statusSize = [statusString sizeWithAttributes: statusAttributes];
289       
290        [nameString drawInRect: NSMakeRect(pen.x, pen.y, mainWidth - statusSize.width - 2.0 * LINE_PADDING, nameSize.height)
291                        withAttributes: nameAttributes];
292       
293        //place status string
294        pen.x = NSMaxX(cellFrame) - PADDING - statusSize.width;
295        pen.y += (nameSize.height - statusSize.height) * 0.5;
296       
297        [statusString drawInRect: NSMakeRect(pen.x, pen.y, statusSize.width, statusSize.height)
298                        withAttributes: statusAttributes];
299       
300        //progress bar
301        pen.x = cellFrame.origin.x + iconSize.width + 2.0 * PADDING;
302        pen.y = cellFrame.origin.y + nameSize.height + LINE_PADDING + PADDING + BAR_HEIGHT;
303       
304        float barWidth = mainWidth + EXTRA_NAME_SHIFT - BUTTONS_TOTAL_WIDTH + PADDING;
305       
306        if ([fDefaults boolForKey: @"UseAdvancedBar"])
307            [self buildAdvancedBar: barWidth point: pen];
308        else
309            [self buildSimpleBar: barWidth point: pen];
310    }
311   
312    [nameAttributes release];
313    [statusAttributes release];
314}
315
316@end
Note: See TracBrowser for help on using the repository browser.