summaryrefslogtreecommitdiffstats
path: root/macosx/HBPictureController.m
blob: ba6dc9a241d42b27e1956b159731ab6f3c997810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* $Id: PictureController.mm,v 1.11 2005/08/01 15:10:44 titer Exp $

 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "HBPictureController.h"
#import "HBFilters.h"
#import "HBPicture.h"

static void *HBPictureControllerContext = &HBPictureControllerContext;

@interface HBPictureController ()
{
    /* Picture Sizing */
    IBOutlet NSTabView       * fSizeFilterView;
    IBOutlet NSBox           * fPictureSizeBox;
    IBOutlet NSBox           * fPictureCropBox;

	IBOutlet NSPopUpButton   * fAnamorphicPopUp;
    IBOutlet NSStepper       * fWidthStepper;
    IBOutlet NSStepper       * fHeightStepper;

    /* Video Filters */
    IBOutlet NSBox           * fDetelecineBox;
    IBOutlet NSBox           * fDecombDeinterlaceBox;
    IBOutlet NSBox           * fDecombBox;
    IBOutlet NSBox           * fDeinterlaceBox;

    IBOutlet NSTextField     * fDeblockField;

    IBOutlet NSTextField    *fDenoisePreset;
    IBOutlet NSPopUpButton  *fDenoisePresetPopUp;
    IBOutlet NSTextField    *fDenoiseTuneLabel;
    IBOutlet NSPopUpButton  *fDenoiseTunePopUp;
    IBOutlet NSTextField    *fDenoiseCustomLabel;
    IBOutlet NSTextField    *fDenoiseCustomField;
}

@end

@implementation HBPictureController

- (instancetype)init
{
	if (self = [super initWithWindowNibName:@"PictureSettings"])
	{
        // NSWindowController likes to lazily load its window. However since
        // this controller tries to set all sorts of outlets before the window
        // is displayed, we need it to load immediately. The correct way to do
        // this, according to the documentation, is simply to invoke the window
        // getter once.
        //
        // If/when we switch a lot of this stuff to bindings, this can probably
        // go away.
        [self window];

        // Add the observers for the filters values
        NSArray *observerdKeyPaths = @[@"self.filters.useDecomb", @"self.filters.deblock",
                                       @"self.filters.denoise", @"self.filters.denoisePreset"];
        for (NSString *keyPath in observerdKeyPaths)
        {
            [self addObserver:self forKeyPath:keyPath options:NSKeyValueObservingOptionInitial context:HBPictureControllerContext];
        }

        [self addObserver:self forKeyPath:@"self.picture.anamorphicMode" options:NSKeyValueObservingOptionInitial context:HBPictureControllerContext];
        [self addObserver:self forKeyPath:@"self.picture.modulus" options:NSKeyValueObservingOptionInitial context:HBPictureControllerContext];
    }

	return self;
}

- (void) dealloc
{
    NSArray *observerdKeyPaths = @[@"self.filters.useDecomb", @"self.filters.deblock",
                                   @"self.filters.denoise", @"self.filters.denoisePreset"];
    @try {
        for (NSString *keyPath in observerdKeyPaths)
        {
            [self removeObserver:self forKeyPath:keyPath];
        }

    } @catch (NSException * __unused exception) {}
}

- (void)windowDidLoad
{
    [[self window] setExcludedFromWindowsMenu:YES];

    // Set the panel appearance explicity to aqua.
    // can be removed when Apple will fix UI appearance on Yosemite.
    if (NSClassFromString(@"NSVisualEffectView")) {
        [self.window setAppearance:[NSClassFromString(@"NSAppearance") appearanceNamed:@"NSAppearanceNameAqua"]];
    }

    /* Populate the Anamorphic NSPopUp button here */
    [fAnamorphicPopUp removeAllItems];
    [fAnamorphicPopUp addItemsWithTitles:@[@"None", @"Strict", @"Loose", @"Custom"]];

    [self resizeInspectorForTab:nil];
    [self adjustSizingDisplay:nil];
}

#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == HBPictureControllerContext)
    {
        // We use KVO to update the panel
        // and notify the main controller of the changes
        // in the filters and picture settings.

        if ([keyPath isEqualToString:@"self.picture.anamorphicMode"])
        {
            [self adjustSizingDisplay:nil];
        }
        else if ([keyPath isEqualToString:@"self.picture.modulus"])
        {
            [fWidthStepper setIncrement:self.picture.modulus];
            [fHeightStepper setIncrement:self.picture.modulus];
        }
        else if ([keyPath isEqualToString:@"self.filters.useDecomb"])
        {
            if (self.filters.useDecomb)
            {
                [fDecombBox setHidden:NO];
                [fDeinterlaceBox setHidden:YES];
            }
            else
            {
                [fDecombBox setHidden:YES];
                [fDeinterlaceBox setHidden:NO];
            }
        }
        else if ([keyPath isEqualToString:@"self.filters.deblock"])
        {
            // The minimum deblock value is 5,
            // set it to 0 if the value is
            // less than 4.
            if (self.filters.deblock == 4)
            {
                [fDeblockField setStringValue: @"Off"];
                self.filters.deblock = 0;
            }
            else if (self.filters.deblock > 4)
            {
                [fDeblockField setStringValue:[NSString stringWithFormat: @"%.0ld", (long)self.filters.deblock]];
            }
        }
        else if ([keyPath isEqualToString:@"self.filters.denoise"] || [keyPath isEqualToString:@"self.filters.denoisePreset"])
        {
            [self validateDenoiseUI];
        }
    }
    else
    {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

/**
 *  Validates the denoise UI items,
 *  disables/enables the right ones.
 */
- (void)validateDenoiseUI
{
    if ([self.filters.denoise isEqualToString:@"off"])
    {
        NSArray *uiElements = @[fDenoisePreset, fDenoisePresetPopUp,
                                fDenoiseTuneLabel, fDenoiseTunePopUp,
                                fDenoiseCustomLabel, fDenoiseCustomField];
        for (NSView *view in uiElements)
            [view setHidden:YES];
    }
    else
    {
        NSArray *uiElements = @[fDenoisePreset, fDenoisePresetPopUp];
        for (NSView *view in uiElements)
            [view setHidden:NO];

        if ([self.filters.denoisePreset isEqualToString:@"none"])
        {
            [fDenoiseTuneLabel setHidden:YES];
            [fDenoiseTunePopUp setHidden:YES];
            [fDenoiseCustomLabel setHidden:NO];
            [fDenoiseCustomField setHidden:NO];
        }
        else if ([self.filters.denoise isEqualToString:@"hqdn3d"])
        {
            [fDenoiseTuneLabel setHidden:YES];
            [fDenoiseTunePopUp setHidden:YES];
            [fDenoiseCustomLabel setHidden:YES];
            [fDenoiseCustomField setHidden:YES];
        }
        else
        {
            [fDenoiseTuneLabel setHidden:NO];
            [fDenoiseTunePopUp setHidden:NO];
            [fDenoiseCustomLabel setHidden:YES];
            [fDenoiseCustomField setHidden:YES];
        }
    }
}

#pragma mark -
#pragma mark Interface Resize

/**
 * This method is used to detect clicking on a tab in fSizeFilterView
 */
- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
    [self resizeInspectorForTab:nil];
}

/**
 * resizeInspectorForTab is called at launch, and each time either the
 * Size or Filters tab is clicked. Size gives a horizontally oriented
 * inspector and Filters is a vertically aligned inspector.
 */
- (void) resizeInspectorForTab: (id) sender
{
    NSRect frame = [[self window] frame];
    NSSize screenSize = [[[self window] screen] frame].size;
    NSPoint screenOrigin = [[[self window] screen] frame].origin;

    /* We base our inspector size/layout on which tab is active for fSizeFilterView */
    /* we are 1 which is Filters*/
    if ([fSizeFilterView indexOfTabViewItem: [fSizeFilterView selectedTabViewItem]] == 1)
    {
        frame.size.width = 484;
        /* we glean the height from the size of the boxes plus the extra window space
         * needed for non boxed display
         */
        frame.size.height = 100.0 + [fDetelecineBox frame].size.height + [fDecombDeinterlaceBox frame].size.height;
        /* Hide the size readout at the bottom as the vertical inspector is not wide enough */
    }
    else // we are Tab index 0 which is size
    {
        frame.size.width = 30.0 + [fPictureSizeBox frame].size.width + [fPictureCropBox frame].size.width;
        frame.size.height = [fPictureSizeBox frame].size.height + 90;
        /* hide the size summary field at the bottom */
    }
    /* get delta's for the change in window size */
    CGFloat deltaX = frame.size.width - [[self window] frame].size.width;
    CGFloat deltaY = frame.size.height - [[self window] frame].size.height;

    /* change the inspector origin via the deltaY */
    frame.origin.y -= deltaY;
    /* keep the inspector centered so the tabs stay in place */
    frame.origin.x -= deltaX / 2.0;

    /* we make sure we are not horizontally off of our screen.
     * this would be the case if we are on the vertical filter tab
     * and we hit the size tab and the inspector grows horizontally
     * off the screen to the right
     */
    if ((frame.origin.x + frame.size.width) > (screenOrigin.x + screenSize.width))
    {
        /* the right side of the preview is off the screen, so shift to the left */
        frame.origin.x = (screenOrigin.x + screenSize.width) - frame.size.width;
    }

    [[self window] setFrame:frame display:YES animate:YES];
}

- (void) adjustSizingDisplay: (id) sender
{
    NSSize pictureSizingBoxSize = [fPictureSizeBox frame].size;

    NSPoint fPictureSizeBoxOrigin = [fPictureSizeBox frame].origin;
    NSSize pictureCropBoxSize = [fPictureCropBox frame].size;
    NSPoint fPictureCropBoxOrigin = [fPictureCropBox frame].origin;

    if ([fAnamorphicPopUp indexOfSelectedItem] == 3)
    {   // custom / power user jamboree
        pictureSizingBoxSize.width = 350;
    }
    else
    {
        pictureSizingBoxSize.width = 200;
    }

    /* Check to see if we have changed the size from current */
    if (pictureSizingBoxSize.height != [fPictureSizeBox frame].size.height ||
        pictureSizingBoxSize.width != [fPictureSizeBox frame].size.width)
    {
        /* Get our delta for the change in picture size box height */
        CGFloat deltaYSizeBoxShift = pictureSizingBoxSize.height -
                                     [fPictureSizeBox frame].size.height;
        fPictureSizeBoxOrigin.y -= deltaYSizeBoxShift;
        /* Get our delta for the change in picture size box width */
        CGFloat deltaXSizeBoxShift = pictureSizingBoxSize.width -
                                     [fPictureSizeBox frame].size.width;
        //fPictureSizeBoxOrigin.x += deltaXSizeBoxShift;
        /* set our new Picture size box size */
        [fPictureSizeBox setFrameSize:pictureSizingBoxSize];
        [fPictureSizeBox setFrameOrigin:fPictureSizeBoxOrigin];

        pictureCropBoxSize.height += deltaYSizeBoxShift;
        fPictureCropBoxOrigin.y -= deltaYSizeBoxShift;
        fPictureCropBoxOrigin.x += deltaXSizeBoxShift;

        [fPictureCropBox setFrameSize:pictureCropBoxSize];
        [[fPictureCropBox animator] setFrameOrigin:fPictureCropBoxOrigin];
    }

    /* now we call to resize the entire inspector window */
    [self resizeInspectorForTab:nil];
}

#pragma mark -

/**
 * Displays and brings the picture window to the front
 */
- (void)showWindow:(id)sender
{
    if (self.window.isVisible)
    {
        [self.window close];
    }
    else
    {
        [super showWindow:self];
    }

    [self resizeInspectorForTab:nil];
    [self adjustSizingDisplay:nil];
}

- (IBAction)showPreviewWindow:(id)sender
{
    [self.previewWindow showWindow:sender];
}

@end