Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 Savoir-Faire Linux Inc. |
| 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Additional permission under GNU GPL version 3 section 7: |
| 20 | * |
| 21 | * If you modify this program, or any covered work, by linking or |
| 22 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 23 | * modified version of that library), containing parts covered by the |
| 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 25 | * grants you additional permission to convey the resulting work. |
| 26 | * Corresponding Source for a non-source form of such a combination |
| 27 | * shall include the source code for the parts of OpenSSL used as well |
| 28 | * as that of the covered work. |
| 29 | */ |
| 30 | |
| 31 | #import "CallView.h" |
| 32 | |
| 33 | #import <QItemSelectionModel> |
| 34 | #import <QAbstractProxyModel> |
| 35 | #import <QUrl> |
| 36 | |
| 37 | #import <video/configurationproxy.h> |
| 38 | #import <video/sourcemodel.h> |
| 39 | #import <video/previewmanager.h> |
| 40 | #import <video/renderer.h> |
| 41 | #import <video/device.h> |
| 42 | #import <video/devicemodel.h> |
| 43 | |
| 44 | @interface CallView () |
| 45 | |
| 46 | @property NSMenu *contextualMenu; |
| 47 | |
| 48 | @end |
| 49 | |
| 50 | @implementation CallView |
| 51 | @synthesize contextualMenu; |
| 52 | @synthesize shouldAcceptInteractions; |
| 53 | |
| 54 | |
| 55 | - (id)initWithFrame:(NSRect)frame |
| 56 | { |
| 57 | self = [super initWithFrame:frame]; |
| 58 | if (self) |
| 59 | { |
| 60 | [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; |
| 61 | } |
| 62 | return self; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | #pragma mark - Destination Operations |
| 67 | |
| 68 | - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender |
| 69 | { |
| 70 | /*------------------------------------------------------ |
| 71 | method called whenever a drag enters our drop zone |
| 72 | --------------------------------------------------------*/ |
| 73 | NSLog(@"Dragging entered"); |
| 74 | |
| 75 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
| 76 | CFStringRef fileExtension = (CFStringRef) [fileURL.path pathExtension]; |
| 77 | CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); |
| 78 | |
| 79 | // Check if the pasteboard contains image data and source/user wants it copied |
| 80 | if ( [sender draggingSourceOperationMask] & NSDragOperationCopy && |
| 81 | (UTTypeConformsTo(fileUTI, kUTTypeVideo)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie))) { |
| 82 | |
| 83 | //highlight our drop zone |
| 84 | highlight=YES; |
| 85 | |
| 86 | [self setNeedsDisplay: YES]; |
| 87 | |
| 88 | /* When an image from one window is dragged over another, we want to resize the dragging item to |
| 89 | * preview the size of the image as it would appear if the user dropped it in. */ |
| 90 | [sender enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent |
| 91 | forView:self |
| 92 | classes:[NSArray arrayWithObject:[NSPasteboardItem class]] |
| 93 | searchOptions:nil |
| 94 | usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop) { |
| 95 | *stop = YES; |
| 96 | }]; |
| 97 | |
| 98 | //accept data as a copy operation |
| 99 | return NSDragOperationCopy; |
| 100 | } |
| 101 | |
| 102 | return NSDragOperationNone; |
| 103 | } |
| 104 | |
| 105 | - (void)draggingExited:(id <NSDraggingInfo>)sender |
| 106 | { |
| 107 | /*------------------------------------------------------ |
| 108 | method called whenever a drag exits our drop zone |
| 109 | --------------------------------------------------------*/ |
| 110 | //remove highlight of the drop zone |
| 111 | highlight=NO; |
| 112 | |
| 113 | [self setNeedsDisplay: YES]; |
| 114 | } |
| 115 | |
| 116 | -(void)drawRect:(NSRect)rect |
| 117 | { |
| 118 | /*------------------------------------------------------ |
| 119 | draw method is overridden to do drop highlighing |
| 120 | --------------------------------------------------------*/ |
| 121 | //do the usual draw operation to display the image |
| 122 | [super drawRect:rect]; |
| 123 | |
| 124 | if ( highlight ) { |
| 125 | //highlight by overlaying a gray border |
| 126 | [[NSColor blueColor] set]; |
| 127 | [NSBezierPath setDefaultLineWidth: 5]; |
| 128 | [NSBezierPath strokeRect: rect]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender |
| 133 | { |
| 134 | /*------------------------------------------------------ |
| 135 | method to determine if we can accept the drop |
| 136 | --------------------------------------------------------*/ |
| 137 | //finished with the drag so remove any highlighting |
| 138 | highlight=NO; |
| 139 | |
| 140 | [self setNeedsDisplay: YES]; |
| 141 | |
| 142 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
| 143 | CFStringRef fileExtension = (CFStringRef) [fileURL.path pathExtension]; |
| 144 | CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); |
| 145 | |
| 146 | //check to see if we can accept the data |
| 147 | return (UTTypeConformsTo(fileUTI, kUTTypeVideo)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie)); |
| 148 | } |
| 149 | |
| 150 | - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender |
| 151 | { |
| 152 | /*------------------------------------------------------ |
| 153 | method that should handle the drop data |
| 154 | --------------------------------------------------------*/ |
| 155 | if ( [sender draggingSource] != self ) { |
| 156 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
| 157 | Video::SourceModel::instance()->setFile(QUrl::fromLocalFile(QString::fromUtf8([fileURL.path UTF8String]))); |
| 158 | } |
| 159 | |
| 160 | return YES; |
| 161 | } |
| 162 | |
| 163 | - (void)showContextualMenu:(NSEvent *)theEvent { |
| 164 | |
| 165 | contextualMenu = [[NSMenu alloc] initWithTitle:@"Switch camera"]; |
| 166 | |
| 167 | for(int i = 0 ; i < Video::DeviceModel::instance()->devices().size() ; ++i) { |
| 168 | Video::Device* device = Video::DeviceModel::instance()->devices()[i]; |
| 169 | [contextualMenu insertItemWithTitle:device->name().toNSString() action:@selector(switchInput:) keyEquivalent:@"" atIndex:i]; |
| 170 | } |
| 171 | |
| 172 | [contextualMenu addItem:[NSMenuItem separatorItem]]; |
| 173 | [contextualMenu insertItemWithTitle:@"Choose file" action:@selector(chooseFile:) keyEquivalent:@"" atIndex:contextualMenu.itemArray.count]; |
| 174 | |
| 175 | [NSMenu popUpContextMenu:contextualMenu withEvent:theEvent forView:self]; |
| 176 | } |
| 177 | |
| 178 | - (void)mouseUp:(NSEvent *)theEvent |
| 179 | { |
| 180 | if([theEvent clickCount] == 1 && shouldAcceptInteractions) { |
| 181 | if(!contextualMenu) |
| 182 | [self performSelector:@selector(showContextualMenu:) withObject:theEvent afterDelay:[NSEvent doubleClickInterval]]; |
| 183 | else |
| 184 | contextualMenu = nil; |
| 185 | } |
| 186 | else if([theEvent clickCount] == 2) |
| 187 | { |
| 188 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; // cancel showContextualMenu |
| 189 | if(self.isInFullScreenMode) |
| 190 | [self exitFullScreenModeWithOptions:nil]; |
| 191 | else { |
| 192 | NSApplicationPresentationOptions options = NSApplicationPresentationDefault + NSApplicationPresentationAutoHideDock + |
| 193 | NSApplicationPresentationAutoHideMenuBar + NSApplicationPresentationAutoHideToolbar; |
| 194 | NSDictionary *opts = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:options], NSFullScreenModeApplicationPresentationOptions, nil]; |
| 195 | |
| 196 | [self enterFullScreenMode:[NSScreen mainScreen] withOptions:opts]; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | - (void) switchInput:(NSMenuItem*) sender |
| 202 | { |
| 203 | int index = [contextualMenu indexOfItem:sender]; |
| 204 | Video::SourceModel::instance()->switchTo(Video::DeviceModel::instance()->devices()[index]); |
| 205 | } |
| 206 | |
| 207 | - (void) chooseFile:(NSMenuItem*) sender |
| 208 | { |
| 209 | NSOpenPanel *browsePanel = [[NSOpenPanel alloc] init]; |
| 210 | [browsePanel setDirectoryURL:[NSURL URLWithString:NSHomeDirectory()]]; |
| 211 | [browsePanel setCanChooseFiles:YES]; |
| 212 | [browsePanel setCanChooseDirectories:NO]; |
| 213 | [browsePanel setCanCreateDirectories:NO]; |
| 214 | |
| 215 | //NSMutableArray* fileTypes = [[NSMutableArray alloc] initWithArray:[NSImage imageTypes]]; |
| 216 | NSMutableArray* fileTypes = [NSMutableArray array]; |
| 217 | [fileTypes addObject:(__bridge NSString *)kUTTypeVideo]; |
| 218 | [fileTypes addObject:(__bridge NSString *)kUTTypeMovie]; |
| 219 | [browsePanel setAllowedFileTypes:fileTypes]; |
| 220 | |
| 221 | [browsePanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) { |
| 222 | if (result == NSFileHandlingPanelOKButton) { |
| 223 | NSURL* theDoc = [[browsePanel URLs] objectAtIndex:0]; |
| 224 | Video::SourceModel::instance()->setFile(QUrl::fromLocalFile(QString::fromUtf8([theDoc.path UTF8String]))); |
| 225 | } |
| 226 | }]; |
| 227 | |
| 228 | } |
| 229 | |
| 230 | @end |