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]]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame^] | 76 | CFStringRef fileExtension = (__bridge CFStringRef) [fileURL.path pathExtension]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 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 | }]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame^] | 97 | CFRelease(fileUTI); |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 98 | //accept data as a copy operation |
| 99 | return NSDragOperationCopy; |
| 100 | } |
| 101 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame^] | 102 | CFRelease(fileUTI); |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 103 | return NSDragOperationNone; |
| 104 | } |
| 105 | |
| 106 | - (void)draggingExited:(id <NSDraggingInfo>)sender |
| 107 | { |
| 108 | /*------------------------------------------------------ |
| 109 | method called whenever a drag exits our drop zone |
| 110 | --------------------------------------------------------*/ |
| 111 | //remove highlight of the drop zone |
| 112 | highlight=NO; |
| 113 | |
| 114 | [self setNeedsDisplay: YES]; |
| 115 | } |
| 116 | |
| 117 | -(void)drawRect:(NSRect)rect |
| 118 | { |
| 119 | /*------------------------------------------------------ |
| 120 | draw method is overridden to do drop highlighing |
| 121 | --------------------------------------------------------*/ |
| 122 | //do the usual draw operation to display the image |
| 123 | [super drawRect:rect]; |
| 124 | |
| 125 | if ( highlight ) { |
| 126 | //highlight by overlaying a gray border |
| 127 | [[NSColor blueColor] set]; |
| 128 | [NSBezierPath setDefaultLineWidth: 5]; |
| 129 | [NSBezierPath strokeRect: rect]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender |
| 134 | { |
| 135 | /*------------------------------------------------------ |
| 136 | method to determine if we can accept the drop |
| 137 | --------------------------------------------------------*/ |
| 138 | //finished with the drag so remove any highlighting |
| 139 | highlight=NO; |
| 140 | |
| 141 | [self setNeedsDisplay: YES]; |
| 142 | |
| 143 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame^] | 144 | CFStringRef fileExtension = (__bridge CFStringRef) [fileURL.path pathExtension]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 145 | CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL); |
| 146 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame^] | 147 | BOOL conforms = (UTTypeConformsTo(fileUTI, kUTTypeVideo)) || (UTTypeConformsTo(fileUTI, kUTTypeMovie)); |
| 148 | CFRelease(fileUTI); |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 149 | //check to see if we can accept the data |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame^] | 150 | return conforms; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender |
| 154 | { |
| 155 | /*------------------------------------------------------ |
| 156 | method that should handle the drop data |
| 157 | --------------------------------------------------------*/ |
| 158 | if ( [sender draggingSource] != self ) { |
| 159 | NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]]; |
| 160 | Video::SourceModel::instance()->setFile(QUrl::fromLocalFile(QString::fromUtf8([fileURL.path UTF8String]))); |
| 161 | } |
| 162 | |
| 163 | return YES; |
| 164 | } |
| 165 | |
| 166 | - (void)showContextualMenu:(NSEvent *)theEvent { |
| 167 | |
| 168 | contextualMenu = [[NSMenu alloc] initWithTitle:@"Switch camera"]; |
| 169 | |
| 170 | for(int i = 0 ; i < Video::DeviceModel::instance()->devices().size() ; ++i) { |
| 171 | Video::Device* device = Video::DeviceModel::instance()->devices()[i]; |
| 172 | [contextualMenu insertItemWithTitle:device->name().toNSString() action:@selector(switchInput:) keyEquivalent:@"" atIndex:i]; |
| 173 | } |
| 174 | |
| 175 | [contextualMenu addItem:[NSMenuItem separatorItem]]; |
| 176 | [contextualMenu insertItemWithTitle:@"Choose file" action:@selector(chooseFile:) keyEquivalent:@"" atIndex:contextualMenu.itemArray.count]; |
| 177 | |
| 178 | [NSMenu popUpContextMenu:contextualMenu withEvent:theEvent forView:self]; |
| 179 | } |
| 180 | |
| 181 | - (void)mouseUp:(NSEvent *)theEvent |
| 182 | { |
| 183 | if([theEvent clickCount] == 1 && shouldAcceptInteractions) { |
| 184 | if(!contextualMenu) |
| 185 | [self performSelector:@selector(showContextualMenu:) withObject:theEvent afterDelay:[NSEvent doubleClickInterval]]; |
| 186 | else |
| 187 | contextualMenu = nil; |
| 188 | } |
| 189 | else if([theEvent clickCount] == 2) |
| 190 | { |
| 191 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; // cancel showContextualMenu |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 192 | if(self.fullScreenDelegate) |
| 193 | [self.fullScreenDelegate callShouldToggleFullScreen]; |
Alexandre Lision | 74dd47f | 2015-04-14 13:47:42 -0400 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | - (void) switchInput:(NSMenuItem*) sender |
| 198 | { |
| 199 | int index = [contextualMenu indexOfItem:sender]; |
| 200 | Video::SourceModel::instance()->switchTo(Video::DeviceModel::instance()->devices()[index]); |
| 201 | } |
| 202 | |
| 203 | - (void) chooseFile:(NSMenuItem*) sender |
| 204 | { |
| 205 | NSOpenPanel *browsePanel = [[NSOpenPanel alloc] init]; |
| 206 | [browsePanel setDirectoryURL:[NSURL URLWithString:NSHomeDirectory()]]; |
| 207 | [browsePanel setCanChooseFiles:YES]; |
| 208 | [browsePanel setCanChooseDirectories:NO]; |
| 209 | [browsePanel setCanCreateDirectories:NO]; |
| 210 | |
| 211 | //NSMutableArray* fileTypes = [[NSMutableArray alloc] initWithArray:[NSImage imageTypes]]; |
| 212 | NSMutableArray* fileTypes = [NSMutableArray array]; |
| 213 | [fileTypes addObject:(__bridge NSString *)kUTTypeVideo]; |
| 214 | [fileTypes addObject:(__bridge NSString *)kUTTypeMovie]; |
| 215 | [browsePanel setAllowedFileTypes:fileTypes]; |
| 216 | |
| 217 | [browsePanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) { |
| 218 | if (result == NSFileHandlingPanelOKButton) { |
| 219 | NSURL* theDoc = [[browsePanel URLs] objectAtIndex:0]; |
| 220 | Video::SourceModel::instance()->setFile(QUrl::fromLocalFile(QString::fromUtf8([theDoc.path UTF8String]))); |
| 221 | } |
| 222 | }]; |
| 223 | |
| 224 | } |
| 225 | |
| 226 | @end |