1// 2// PRPreviewController.m 3// PRICE 4// 5// Created by Riccardo Mottola on 2/19/10. 6// Copyright 2010-2014 Carduus. All rights reserved. 7// 8// This application is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 9// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 10 11 12#import "PRPreviewController.h" 13 14 15@implementation PRPreviewController 16 17- (id)init 18{ 19 if ((self = [super init])) 20 { 21 [NSBundle loadNibNamed:@"Preview" owner:self]; 22 23 /* add an observer for the window resize */ 24 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_windowDidResize:) name:NSWindowDidResizeNotification object:previewWindow]; 25 } 26 return self; 27} 28 29- (void)dealloc 30{ 31 /* remove observer for the window resize */ 32 [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:previewWindow]; 33 34 [super dealloc]; 35} 36 37 38- (void)setFilterController: (PRFilterController *)controller 39{ 40 filterController = controller; 41} 42 43- (void)showPreview 44{ 45 [previewWindow orderFront: self]; 46} 47 48- (void)hidePreview 49{ 50 [previewWindow orderOut: self]; 51} 52 53/** returns wether continuous update of the preview is desired or not */ 54- (BOOL)continuous 55{ 56 if ([buttContinuous state] == NSOnState) 57 return YES; 58 return NO; 59} 60 61- (void)setContinuous :(BOOL)flag 62{ 63 if (flag == YES) 64 [buttContinuous setState: NSOnState]; 65 else 66 [buttContinuous setState: NSOffState]; 67} 68 69 70- (IBAction)cleanPreview:(id)sender 71{ 72 [view setImage:nil]; 73} 74 75- (IBAction)updatePreview:(id)sender 76{ 77 PRImage *img; 78 79 img = [filterController filteredImage]; 80 [view setImage:img]; 81 82 [self scaleFromMenu:scalePopUp]; 83} 84 85- (void)scaleFromMenu:(id)sender 86{ 87 int tag; 88 NSSize currViewSize; 89 NSSize imgSize; 90 float hscale, vscale; 91 92 tag = [[sender selectedItem] tag]; 93 currViewSize = [[[view enclosingScrollView] contentView] visibleRect].size; 94 imgSize = [(PRImage *)[view image] size]; 95 switch (tag) 96 { 97 case -3: 98 vscale = currViewSize.height / imgSize.height; 99 hscale = currViewSize.width / imgSize.width; 100 if (hscale > vscale) 101 [view scaleFrameBy:vscale]; 102 else 103 [view scaleFrameBy:hscale]; 104 break; 105 case -2: 106 hscale = currViewSize.width / imgSize.width; 107 NSLog(@"%f %f", currViewSize.width, imgSize.width); 108 [view scaleFrameBy:hscale]; 109 break; 110 case -1: 111 vscale = currViewSize.height / imgSize.height; 112 NSLog(@"%f %f", currViewSize.height, imgSize.height); 113 [view scaleFrameBy:vscale]; 114 break; 115 case 12: 116 [view scaleFrameBy:0.125]; 117 break; 118 case 25: 119 [view scaleFrameBy:0.25]; 120 break; 121 case 50: 122 [view scaleFrameBy:0.5]; 123 break; 124 case 75: 125 [view scaleFrameBy:0.75]; 126 break; 127 case 100: 128 [view scaleFrameBy:1.0]; 129 break; 130 case 150: 131 [view scaleFrameBy:1.55]; 132 break; 133 case 200: 134 [view scaleFrameBy:2.0]; 135 break; 136 default: 137 NSLog(@"unexpected case in scale menu selection"); 138 break; 139 } 140} 141 142/** method called as a notification from the window resize 143or if scale preferences changed */ 144- (void)_windowDidResize :(NSNotification *)notif 145{ 146 int tag; 147 NSSize currViewSize; 148 NSSize imgSize; 149 float hscale, vscale; 150 151 tag = [[scalePopUp selectedItem] tag]; 152 currViewSize = [[[view enclosingScrollView] contentView] visibleRect].size; 153 imgSize = [(PRImage *)[view image] pixelSize]; 154 switch (tag) 155 { 156 case -3: 157 vscale = currViewSize.height / imgSize.height; 158 hscale = currViewSize.width / imgSize.width; 159 if (hscale > vscale) 160 [view scaleFrameBy:vscale]; 161 else 162 [view scaleFrameBy:hscale]; 163 break; 164 case -2: 165 hscale = currViewSize.width / imgSize.width; 166 NSLog(@"%f %f", currViewSize.width, imgSize.width); 167 [view scaleFrameBy:hscale]; 168 break; 169 case -1: 170 vscale = currViewSize.height / imgSize.height; 171 NSLog(@"%f %f", currViewSize.height, imgSize.height); 172 [view scaleFrameBy:vscale]; 173 break; 174 default: 175 break; 176 } 177} 178 179 180/* ---- FilterProgress protocol methods */ 181 182- (void)setTitle:(NSString *)title 183{ 184} 185 186- (void)setProgress:(double)progress 187{ 188 [progressBar setDoubleValue:progress]; 189 [previewWindow displayIfNeeded]; 190 [previewWindow flushWindowIfNeeded]; 191} 192 193- (void)setActivity:(NSString *)title 194{ 195 [activityDescription setStringValue:title]; 196 [previewWindow displayIfNeeded]; 197} 198 199 200@end 201