source: trunk/macosx/FilePriorityCell.m @ 12473

Last change on this file since 12473 was 12473, checked in by livings124, 11 years ago

instead of displaying a star for multiple priorities, display all relevant icons

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 6.8 KB
Line 
1/******************************************************************************
2 * $Id: FilePriorityCell.m 12473 2011-05-30 00:09:22Z livings124 $
3 *
4 * Copyright (c) 2007-2011 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 "FilePriorityCell.h"
26#import "FileOutlineView.h"
27#import "FileListNode.h"
28#import "NSApplicationAdditions.h"
29#import "Torrent.h"
30
31#define IMAGE_OVERLAP 3.0
32
33@implementation FilePriorityCell
34
35- (id) init
36{
37    if ((self = [super init]))
38    {
39        [self setTrackingMode: NSSegmentSwitchTrackingSelectAny];
40        [self setControlSize: NSMiniControlSize];
41        [self setSegmentCount: 3];
42       
43        for (NSInteger i = 0; i < [self segmentCount]; i++)
44        {
45            [self setLabel: @"" forSegment: i];
46            [self setWidth: 9.0f forSegment: i]; //9 is minimum size to get proper look
47        }
48       
49        [self setImage: [NSImage imageNamed: @"PriorityControlLow.png"] forSegment: 0];
50        [self setImage: [NSImage imageNamed: @"PriorityControlNormal.png"] forSegment: 1];
51        [self setImage: [NSImage imageNamed: @"PriorityControlHigh.png"] forSegment: 2];
52       
53        fHoverRow = NO;
54    }
55    return self;
56}
57
58- (id) copyWithZone: (NSZone *) zone
59{
60    id value = [super copyWithZone: zone];
61    [value setRepresentedObject: [self representedObject]];
62    return value;
63}
64
65- (void) setSelected: (BOOL) flag forSegment: (NSInteger) segment
66{
67    [super setSelected: flag forSegment: segment];
68   
69    //only for when clicking manually
70    NSInteger priority;
71    switch (segment)
72    {
73        case 0:
74            priority = TR_PRI_LOW;
75            break;
76        case 1:
77            priority = TR_PRI_NORMAL;
78            break;
79        case 2:
80            priority = TR_PRI_HIGH;
81            break;
82    }
83   
84    FileOutlineView * controlView = (FileOutlineView *)[self controlView];
85    Torrent * torrent = [controlView torrent];
86    [torrent setFilePriority: priority forIndexes: [(FileListNode *)[self representedObject] indexes]];
87    [controlView reloadData];
88}
89
90- (void) addTrackingAreasForView: (NSView *) controlView inRect: (NSRect) cellFrame withUserInfo: (NSDictionary *) userInfo
91            mouseLocation: (NSPoint) mouseLocation
92{
93    NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
94   
95    if (NSMouseInRect(mouseLocation, cellFrame, [controlView isFlipped]))
96    {
97        options |= NSTrackingAssumeInside;
98        [controlView setNeedsDisplayInRect: cellFrame];
99    }
100   
101    NSTrackingArea * area = [[NSTrackingArea alloc] initWithRect: cellFrame options: options owner: controlView userInfo: userInfo];
102    [controlView addTrackingArea: area];
103    [area release];
104}
105
106- (void) setHovered: (BOOL) hovered
107{
108    fHoverRow = hovered;
109}
110
111- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
112{
113    Torrent * torrent = [(FileOutlineView *)controlView torrent];
114    FileListNode * node = [self representedObject];
115    NSSet * priorities = [torrent filePrioritiesForIndexes: [node indexes]];
116   
117    const NSUInteger count = [priorities count];
118    if (fHoverRow && count > 0)
119    {
120        [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_LOW]] forSegment: 0];
121        [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_NORMAL]] forSegment: 1];
122        [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_HIGH]] forSegment: 2];
123       
124        [super drawWithFrame: cellFrame inView: controlView];
125    }
126    else
127    {
128        NSMutableArray * images = [NSMutableArray arrayWithCapacity: MAX(count, 1)];
129        CGFloat totalWidth;
130       
131        if (count == 0)
132        {
133            NSImage * image = [NSImage imageNamed: @"PriorityNone.png"];
134            [images addObject: image];
135            totalWidth = [image size].width;
136        }
137        else
138        {
139            totalWidth = 0.0;
140            if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_LOW]])
141            {
142                NSImage * image = [NSImage imageNamed: @"PriorityLow.png"];
143                [images addObject: image];
144                totalWidth += [image size].width;
145            }
146            if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_NORMAL]])
147            {
148                NSImage * image = [NSImage imageNamed: @"PriorityNormal.png"];
149                [images addObject: image];
150                totalWidth += [image size].width;
151            }
152            if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_HIGH]])
153            {
154                NSImage * image = [NSImage imageNamed: @"PriorityHigh.png"];
155                [images addObject: image];
156                totalWidth += [image size].width;
157            }
158        }
159       
160        if (count > 1)
161            totalWidth -= IMAGE_OVERLAP * (count-1);
162       
163        CGFloat currentWidth = floor(NSMidX(cellFrame) - totalWidth * 0.5);
164       
165        for (NSImage * image in images)
166        {
167            const NSSize imageSize = [image size];
168            NSRect imageRect = NSMakeRect(currentWidth, floor(NSMidY(cellFrame) - imageSize.height * 0.5), imageSize.width, imageSize.height);
169           
170            if ([NSApp isOnSnowLeopardOrBetter])
171                [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0 respectFlipped: YES hints: nil];
172            else
173            {
174                image = [image copy];
175                [image setFlipped: YES];
176                [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
177                [image release];
178            }
179           
180            currentWidth += imageSize.width - IMAGE_OVERLAP;
181        }
182    }
183}
184
185@end
Note: See TracBrowser for help on using the repository browser.