source: trunk/macosx/FilePriorityCell.m @ 2345

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

simplify check for priority text

File size: 3.4 KB
Line 
1#import "FilePriorityCell.h"
2#import "InfoWindowController.h"
3#import "Torrent.h"
4
5@implementation FilePriorityCell
6
7- (id) init
8{
9    if ((self = [super init]))
10    {
11        [self setTrackingMode: NSSegmentSwitchTrackingSelectAny];
12        [self setControlSize: NSMiniControlSize];
13        [self setSegmentCount: 3];
14       
15        int i;
16        for (i = 0; i < [self segmentCount]; i++)
17        {
18            [self setLabel: @"" forSegment: i];
19            [self setWidth: 6.0 forSegment: i];
20        }
21    }
22    return self;
23}
24
25- (void) setItem: (NSMutableDictionary *) item
26{
27    fItem = item;
28}
29
30- (void) setSelected: (BOOL) flag forSegment: (int) segment
31{
32    [super setSelected: flag forSegment: segment];
33   
34    //only for when clicking manually
35    Torrent * torrent = [[[[self controlView] window] windowController] selectedTorrent];
36    NSIndexSet * indexes = [fItem objectForKey: @"Indexes"];
37    if (![torrent canChangeDownloadCheckForFiles: indexes])
38        return;
39   
40    int priority;
41    if (segment == 0)
42        priority = TR_PRI_LOW;
43    else if (segment == 2)
44        priority = TR_PRI_HIGH;
45    else
46        priority = TR_PRI_NORMAL;
47   
48    [torrent setFilePriority: priority forIndexes: indexes];
49    [(FileOutlineView *)[self controlView] reloadData];
50}
51
52- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
53{
54    Torrent * torrent = [(InfoWindowController *)[[[self controlView] window] windowController] selectedTorrent];
55    NSSet * priorities = [torrent filePrioritiesForIndexes: [fItem objectForKey: @"Indexes"]];
56   
57    int count = [priorities count];
58    if (count == 0)
59        return;
60   
61    BOOL low = [priorities containsObject: [NSNumber numberWithInt: TR_PRI_LOW]],
62        normal = [priorities containsObject: [NSNumber numberWithInt: TR_PRI_NORMAL]],
63        high = [priorities containsObject: [NSNumber numberWithInt: TR_PRI_HIGH]];
64   
65    FileOutlineView * view = (FileOutlineView *)[self controlView];
66    int row = [view hoverRow];
67    if (row != -1 && [view itemAtRow: row] == fItem)
68    {
69        [super setSelected: low forSegment: 0];
70        [super setSelected: normal forSegment: 1];
71        [super setSelected: high forSegment: 2];
72       
73        [super drawWithFrame: cellFrame inView: controlView];
74    }
75    else
76    {
77        if (high || low)
78        {
79            BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
80                                        isEqual: [NSColor alternateSelectedControlColor]];
81            NSDictionary * attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
82                            highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
83                            [NSFont messageFontOfSize: 18.0], NSFontAttributeName, nil];
84           
85            NSString * text;
86            if (count > 1)
87                text = @"*";
88            else if (low)
89                text = @"-";
90            else
91                text = @"+";
92           
93            NSSize textSize = [text sizeWithAttributes: attributes];
94            NSRect textRect = NSMakeRect(cellFrame.origin.x + (cellFrame.size.width - textSize.width) * 0.5,
95                                    cellFrame.origin.y + (cellFrame.size.height - textSize.height) * 0.5,
96                                    textSize.width, textSize.height);
97           
98            [text drawInRect: textRect withAttributes: attributes];
99        }
100    }
101}
102
103@end
Note: See TracBrowser for help on using the repository browser.