source: trunk/macosx/FilePriorityCell.m @ 2338

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

for priorities experiment with a set instead of an array

File size: 3.5 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    if ([priorities count] == 0)
58        return;
59   
60    BOOL low = [priorities containsObject: [NSNumber numberWithInt: TR_PRI_LOW]],
61        normal = [priorities containsObject: [NSNumber numberWithInt: TR_PRI_NORMAL]],
62        high = [priorities containsObject: [NSNumber numberWithInt: TR_PRI_HIGH]];
63   
64    FileOutlineView * view = (FileOutlineView *)[self controlView];
65    int row = [view hoverRow];
66    if (row != -1 && [view itemAtRow: row] == fItem)
67    {
68        [super setSelected: low forSegment: 0];
69        [super setSelected: normal forSegment: 1];
70        [super setSelected: high forSegment: 2];
71       
72        [super drawWithFrame: cellFrame inView: controlView];
73    }
74    else
75    {
76        if (high || low)
77        {
78            BOOL highlighted = [self isHighlighted] && [[self highlightColorWithFrame: cellFrame inView: controlView]
79                                        isEqual: [NSColor alternateSelectedControlColor]];
80            NSDictionary * attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
81                            highlighted ? [NSColor whiteColor] : [NSColor controlTextColor], NSForegroundColorAttributeName,
82                            [NSFont messageFontOfSize: 18.0], NSFontAttributeName, nil];
83           
84            NSString * text;
85            if (low && !normal && !high)
86              text = @"-";
87            else if (!low && !normal && high)
88                text = @"+";
89            else
90                text = @"*";
91           
92            NSSize textSize = [text sizeWithAttributes: attributes];
93            NSRect textRect = NSMakeRect(cellFrame.origin.x + (cellFrame.size.width - textSize.width) * 0.5,
94                                    cellFrame.origin.y + (cellFrame.size.height - textSize.height) * 0.5,
95                                    textSize.width, textSize.height);
96           
97            [text drawInRect: textRect withAttributes: attributes];
98        }
99    }
100}
101
102@end
Note: See TracBrowser for help on using the repository browser.