CallView.mm 9 KB
Newer Older
Alexandre Lision's avatar
Alexandre Lision committed
1
/*
Alexandre Lision's avatar
Alexandre Lision committed
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision's avatar
Alexandre Lision committed
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
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#import "CallView.h"

#import <QItemSelectionModel>
#import <QAbstractProxyModel>
#import <QUrl>

#import <video/configurationproxy.h>
#import <video/sourcemodel.h>
#import <video/previewmanager.h>
#import <video/renderer.h>
#import <video/device.h>
#import <video/devicemodel.h>

@interface CallView ()

@property NSMenu *contextualMenu;

@end

@implementation CallView
@synthesize contextualMenu;
@synthesize shouldAcceptInteractions;


- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
    }
Alexandre Lision's avatar
Alexandre Lision committed
51 52 53 54 55 56 57 58 59 60 61

    [self.window setAcceptsMouseMovedEvents:YES];

    NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);

    NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:frame
                                                        options:options
                                                          owner:self
                                                       userInfo:nil];

    [self addTrackingArea:area];
Alexandre Lision's avatar
Alexandre Lision committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    return self;
}


#pragma mark - Destination Operations

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    /*------------------------------------------------------
     method called whenever a drag enters our drop zone
     --------------------------------------------------------*/
    NSLog(@"Dragging entered");

    NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
76
    CFStringRef fileExtension = (__bridge CFStringRef) [fileURL.path pathExtension];
Alexandre Lision's avatar
Alexandre Lision committed
77 78 79 80
    CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

    // Check if the pasteboard contains image data and source/user wants it copied
    if ( [sender draggingSourceOperationMask] & NSDragOperationCopy &&
Alexandre Lision's avatar
Alexandre Lision committed
81 82 83
        (UTTypeConformsTo(fileUTI, kUTTypeVideo)) ||
        (UTTypeConformsTo(fileUTI, kUTTypeMovie)) ||
        (UTTypeConformsTo(fileUTI, kUTTypeImage))) {
Alexandre Lision's avatar
Alexandre Lision committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

        //highlight our drop zone
        highlight=YES;

        [self setNeedsDisplay: YES];

        /* When an image from one window is dragged over another, we want to resize the dragging item to
         * preview the size of the image as it would appear if the user dropped it in. */
        [sender enumerateDraggingItemsWithOptions:NSDraggingItemEnumerationConcurrent
                                          forView:self
                                          classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
                                    searchOptions:nil
                                       usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop) {
                                           *stop = YES;
                                       }];
99
        CFRelease(fileUTI);
Alexandre Lision's avatar
Alexandre Lision committed
100 101 102 103
        //accept data as a copy operation
        return NSDragOperationCopy;
    }

104
    CFRelease(fileUTI);
Alexandre Lision's avatar
Alexandre Lision committed
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
    return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
    /*------------------------------------------------------
     method called whenever a drag exits our drop zone
     --------------------------------------------------------*/
    //remove highlight of the drop zone
    highlight=NO;

    [self setNeedsDisplay: YES];
}

-(void)drawRect:(NSRect)rect
{
    /*------------------------------------------------------
     draw method is overridden to do drop highlighing
     --------------------------------------------------------*/
    //do the usual draw operation to display the image
    [super drawRect:rect];

    if ( highlight ) {
        //highlight by overlaying a gray border
        [[NSColor blueColor] set];
        [NSBezierPath setDefaultLineWidth: 5];
        [NSBezierPath strokeRect: rect];
    }
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
    /*------------------------------------------------------
     method to determine if we can accept the drop
     --------------------------------------------------------*/
    //finished with the drag so remove any highlighting
    highlight=NO;

    [self setNeedsDisplay: YES];

    NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
146
    CFStringRef fileExtension = (__bridge CFStringRef) [fileURL.path pathExtension];
Alexandre Lision's avatar
Alexandre Lision committed
147 148
    CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);

Alexandre Lision's avatar
Alexandre Lision committed
149 150 151
    BOOL conforms = (UTTypeConformsTo(fileUTI, kUTTypeVideo)) ||
                    (UTTypeConformsTo(fileUTI, kUTTypeMovie)) ||
                    UTTypeConformsTo(fileUTI, kUTTypeImage);
152
    CFRelease(fileUTI);
Alexandre Lision's avatar
Alexandre Lision committed
153
    //check to see if we can accept the data
154
    return conforms;
Alexandre Lision's avatar
Alexandre Lision committed
155 156 157 158 159 160 161 162 163
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    /*------------------------------------------------------
     method that should handle the drop data
     --------------------------------------------------------*/
    if ( [sender draggingSource] != self ) {
        NSURL* fileURL = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
Alexandre Lision's avatar
Alexandre Lision committed
164
        Video::SourceModel::instance().setFile(QUrl::fromLocalFile(QString::fromUtf8([fileURL.path UTF8String])));
Alexandre Lision's avatar
Alexandre Lision committed
165 166 167 168 169 170 171 172 173
    }

    return YES;
}

- (void)showContextualMenu:(NSEvent *)theEvent {

    contextualMenu = [[NSMenu alloc] initWithTitle:@"Switch camera"];

Alexandre Lision's avatar
Alexandre Lision committed
174 175
    for(int i = 0 ; i < Video::DeviceModel::instance().devices().size() ; ++i) {
        Video::Device* device = Video::DeviceModel::instance().devices()[i];
Alexandre Lision's avatar
Alexandre Lision committed
176 177 178 179
        [contextualMenu insertItemWithTitle:device->name().toNSString() action:@selector(switchInput:) keyEquivalent:@"" atIndex:i];
    }

    [contextualMenu addItem:[NSMenuItem separatorItem]];
180 181 182 183
    [contextualMenu insertItemWithTitle:NSLocalizedString(@"Choose file", @"Contextual menu entry")
                                 action:@selector(chooseFile:)
                          keyEquivalent:@""
                                atIndex:contextualMenu.itemArray.count];
Alexandre Lision's avatar
Alexandre Lision committed
184 185 186 187

    [NSMenu popUpContextMenu:contextualMenu withEvent:theEvent forView:self];
}

Alexandre Lision's avatar
Alexandre Lision committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
- (void)mouseMoved:(NSEvent *)theEvent
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self]; // cancel showContextualMenu
    [self performSelector:@selector(mouseIdle:) withObject:theEvent afterDelay:3];
    if (self.callDelegate)
        [self.callDelegate mouseIsMoving:YES];
}

- (void) mouseIdle:(NSEvent *)theEvent
{
    if (self.callDelegate && shouldAcceptInteractions)
        [self.callDelegate mouseIsMoving:NO];
}


Alexandre Lision's avatar
Alexandre Lision committed
203 204 205 206 207 208 209 210 211 212 213
- (void)mouseUp:(NSEvent *)theEvent
{
    if([theEvent clickCount] == 1 && shouldAcceptInteractions) {
        if(!contextualMenu)
            [self performSelector:@selector(showContextualMenu:) withObject:theEvent afterDelay:[NSEvent doubleClickInterval]];
        else
            contextualMenu = nil;
    }
    else if([theEvent clickCount] == 2)
    {
        [NSObject cancelPreviousPerformRequestsWithTarget:self]; // cancel showContextualMenu
Alexandre Lision's avatar
Alexandre Lision committed
214 215
        if(self.callDelegate)
            [self.callDelegate callShouldToggleFullScreen];
Alexandre Lision's avatar
Alexandre Lision committed
216 217 218 219 220 221
    }
}

- (void) switchInput:(NSMenuItem*) sender
{
    int index = [contextualMenu indexOfItem:sender];
Alexandre Lision's avatar
Alexandre Lision committed
222
    Video::SourceModel::instance().switchTo(Video::DeviceModel::instance().devices()[index]);
Alexandre Lision's avatar
Alexandre Lision committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236
}

- (void) chooseFile:(NSMenuItem*) sender
{
    NSOpenPanel *browsePanel = [[NSOpenPanel alloc] init];
    [browsePanel setDirectoryURL:[NSURL URLWithString:NSHomeDirectory()]];
    [browsePanel setCanChooseFiles:YES];
    [browsePanel setCanChooseDirectories:NO];
    [browsePanel setCanCreateDirectories:NO];

    //NSMutableArray* fileTypes = [[NSMutableArray alloc] initWithArray:[NSImage imageTypes]];
    NSMutableArray* fileTypes = [NSMutableArray array];
    [fileTypes addObject:(__bridge NSString *)kUTTypeVideo];
    [fileTypes addObject:(__bridge NSString *)kUTTypeMovie];
Alexandre Lision's avatar
Alexandre Lision committed
237
    [fileTypes addObject:(__bridge NSString *)kUTTypeImage];
Alexandre Lision's avatar
Alexandre Lision committed
238 239 240 241 242
    [browsePanel setAllowedFileTypes:fileTypes];

    [browsePanel beginSheetModalForWindow:[self window] completionHandler:^(NSInteger result) {
        if (result == NSFileHandlingPanelOKButton) {
            NSURL*  theDoc = [[browsePanel URLs] objectAtIndex:0];
Alexandre Lision's avatar
Alexandre Lision committed
243
            Video::SourceModel::instance().setFile(QUrl::fromLocalFile(QString::fromUtf8([theDoc.path UTF8String])));
Alexandre Lision's avatar
Alexandre Lision committed
244 245 246 247 248 249
        }
    }];

}

@end