source: trunk/macosx/FileOutlineView.m @ 2337

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

hopefully function for getting priorities is a bit faster

File size: 5.6 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2007 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 "FileOutlineView.h"
26#import "FileBrowserCell.h"
27#import "InfoWindowController.h"
28#import "Torrent.h"
29
30@implementation FileOutlineView
31
32- (void) awakeFromNib
33{
34    NSBrowserCell * browserCell = [[[FileBrowserCell alloc] init] autorelease];
35    [[self tableColumnWithIdentifier: @"Name"] setDataCell: browserCell];
36   
37    [self setAutoresizesOutlineColumn: NO];
38    [self setIndentationPerLevel: 14.0];
39   
40    fNormalColor = [self backgroundColor];
41    fHighPriorityColor = [[NSColor colorWithCalibratedRed: 0.678 green: 0.937 blue: 0.451 alpha: 1.0] retain];
42    fLowPriorityColor = [[NSColor colorWithCalibratedRed: 0.984 green: 0.878 blue: 0.431 alpha: 1.0] retain];
43   
44    fHoverRow = -1;
45}
46
47- (void) dealloc
48{
49    [fHighPriorityColor release];
50    [fLowPriorityColor release];
51   
52    [super dealloc];
53}
54
55- (void) mouseDown: (NSEvent *) event
56{
57    [[self window] makeKeyWindow];
58    [super mouseDown: event];
59}
60
61- (NSMenu *) menuForEvent: (NSEvent *) event
62{
63    int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
64   
65    if (row >= 0)
66    {
67        if (![self isRowSelected: row])
68            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
69    }
70    else
71        [self deselectAll: self];
72   
73    return [self menu];
74}
75
76- (void) setHoverRowForEvent: (NSEvent *) event
77{
78    int row = -1;
79    if (event)
80    {
81        NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
82        if ([self columnAtPoint: point] == [self columnWithIdentifier: @"Priority"])
83            row = [self rowAtPoint: point];
84    }
85   
86    if (row != fHoverRow)
87    {
88        if (fHoverRow != -1)
89            [self reloadItem: [self itemAtRow: fHoverRow]];
90        fHoverRow = row;
91        if (fHoverRow != -1)
92            [self reloadItem: [self itemAtRow: fHoverRow]];
93    }
94}
95
96- (int) hoverRow
97{
98    return fHoverRow;
99}
100
101- (void) drawRow: (int) row clipRect: (NSRect) clipRect
102{
103    if (![self isRowSelected: row])
104    {
105        NSDictionary * item = [self itemAtRow: row];
106        Torrent * torrent = [(InfoWindowController *)[[self window] windowController] selectedTorrent];
107       
108        if ([[item objectForKey: @"IsFolder"] boolValue])
109            [fNormalColor set];
110        else
111        {
112            NSArray * priorities = [torrent filePrioritiesForIndexes: [item objectForKey: @"Indexes"]];
113            if ([priorities count] == 0)
114                [fNormalColor set];
115            else
116            {
117                int priority = [[priorities objectAtIndex: 0] intValue];
118                if (priority == TR_PRI_LOW)
119                    [fLowPriorityColor set];
120                else if (priority == TR_PRI_HIGH)
121                    [fHighPriorityColor set];
122                else
123                    [fNormalColor set];
124            }
125        }
126       
127        NSRect rect = [self rectOfRow: row];
128        rect.size.height -= 1.0;
129       
130        NSRectFill(rect);
131    }
132   
133    [super drawRow: row clipRect: clipRect];
134}
135
136- (void) drawRect: (NSRect) r
137{
138    [super drawRect: r];
139
140    NSDictionary * item;
141    NSIndexSet * indexSet;
142    int i, priority;
143    Torrent * torrent = [(InfoWindowController *)[[self window] windowController] selectedTorrent];
144    for (i = 0; i < [self numberOfRows]; i++)
145    {
146        if ([self isRowSelected: i])
147        {
148            item = [self itemAtRow: i];
149            if (![[item objectForKey: @"IsFolder"] boolValue])
150            {
151                NSArray * priorities = [torrent filePrioritiesForIndexes: [item objectForKey: @"Indexes"]];
152                if ([priorities count] == 1)
153                {
154                    int priority = [[priorities objectAtIndex: 0] intValue];
155                    if (priority == TR_PRI_LOW)
156                        [fLowPriorityColor set];
157                    else if (priority == TR_PRI_HIGH)
158                        [fHighPriorityColor set];
159                    else
160                        continue;
161               
162                    NSRect rect = [self rectOfRow: i];
163                    float width = 14.0;
164                    rect.origin.y += (rect.size.height - width) * 0.5;
165                    rect.origin.x += 3.0;
166                    rect.size.width = width;
167                    rect.size.height = width;
168                   
169                    [[NSBezierPath bezierPathWithOvalInRect: rect] fill];
170                }
171            }
172        }
173    }
174}
175
176@end
Note: See TracBrowser for help on using the repository browser.