Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 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. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 19 | #import "VideoPrefsVC.h" |
| 20 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 21 | #import <QuartzCore/QuartzCore.h> |
| 22 | |
| 23 | #import <QItemSelectionModel> |
| 24 | #import <QAbstractProxyModel> |
| 25 | |
| 26 | #import <video/configurationproxy.h> |
| 27 | #import <video/sourcemodel.h> |
| 28 | #import <video/previewmanager.h> |
| 29 | #import <video/renderer.h> |
| 30 | #import <video/device.h> |
| 31 | #import <video/devicemodel.h> |
| 32 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 33 | @interface VideoPrefsVC () |
| 34 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 35 | @property (assign) IBOutlet NSView *previewView; |
| 36 | @property (assign) IBOutlet NSPopUpButton *videoDevicesList; |
| 37 | @property (assign) IBOutlet NSPopUpButton *sizesList; |
| 38 | @property (assign) IBOutlet NSPopUpButton *ratesList; |
| 39 | |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 40 | @property BOOL shouldHandlePreview; |
| 41 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 42 | @end |
| 43 | |
| 44 | @implementation VideoPrefsVC |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 45 | @synthesize previewView; |
| 46 | @synthesize videoDevicesList; |
| 47 | @synthesize sizesList; |
| 48 | @synthesize ratesList; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 49 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 50 | QMetaObject::Connection frameUpdated; |
| 51 | QMetaObject::Connection previewStarted; |
| 52 | QMetaObject::Connection previewStopped; |
| 53 | |
| 54 | - (void)loadView |
| 55 | { |
| 56 | [super loadView]; |
| 57 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 58 | Video::ConfigurationProxy::deviceModel().rowCount(); |
| 59 | Video::ConfigurationProxy::resolutionModel().rowCount(); |
| 60 | Video::ConfigurationProxy::rateModel().rowCount(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 61 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 62 | QModelIndex qDeviceIdx = Video::ConfigurationProxy::deviceSelectionModel().currentIndex(); |
| 63 | qDeviceIdx = Video::ConfigurationProxy::deviceSelectionModel().currentIndex(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 64 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 65 | [videoDevicesList addItemWithTitle:Video::ConfigurationProxy::deviceModel().data(qDeviceIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 66 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 67 | QModelIndex qSizeIdx = Video::ConfigurationProxy::resolutionSelectionModel().currentIndex(); |
| 68 | [sizesList addItemWithTitle:Video::ConfigurationProxy::resolutionModel().data(qSizeIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 69 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 70 | if(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::resolutionModel())) { |
| 71 | QObject::connect(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::resolutionModel()), |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 72 | &QAbstractProxyModel::modelReset, |
| 73 | [=]() { |
| 74 | NSLog(@"resolution Source model changed!!!"); |
| 75 | }); |
| 76 | |
| 77 | } |
| 78 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 79 | QModelIndex qRate = Video::ConfigurationProxy::rateSelectionModel().currentIndex(); |
| 80 | [ratesList addItemWithTitle:Video::ConfigurationProxy::rateModel().data(qDeviceIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 81 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 82 | if(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::rateModel())) { |
| 83 | QObject::connect(qobject_cast<QAbstractProxyModel*>(&Video::ConfigurationProxy::rateModel()), |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 84 | &QAbstractProxyModel::modelReset, |
| 85 | [=]() { |
| 86 | NSLog(@"rates Source model changed!!!"); |
| 87 | }); |
| 88 | |
| 89 | } |
| 90 | |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 91 | [previewView setWantsLayer:YES]; |
| 92 | [previewView setLayer:[CALayer layer]]; |
| 93 | [previewView.layer setBackgroundColor:[NSColor blackColor].CGColor]; |
| 94 | [previewView.layer setContentsGravity:kCAGravityResizeAspect]; |
| 95 | [previewView.layer setFrame:previewView.frame]; |
| 96 | [previewView.layer setBounds:previewView.frame]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | - (IBAction)chooseDevice:(id)sender { |
| 100 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 101 | QModelIndex qIdx = Video::ConfigurationProxy::deviceModel().index(index, 0); |
| 102 | Video::ConfigurationProxy::deviceSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | - (IBAction)chooseSize:(id)sender { |
| 106 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 107 | QModelIndex qIdx = Video::ConfigurationProxy::resolutionModel().index(index, 0); |
| 108 | Video::ConfigurationProxy::resolutionSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | - (IBAction)chooseRate:(id)sender { |
| 112 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 113 | QModelIndex qIdx = Video::ConfigurationProxy::rateModel().index(index, 0); |
| 114 | Video::ConfigurationProxy::rateSelectionModel().setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | - (void) connectPreviewSignals |
| 118 | { |
| 119 | QObject::disconnect(frameUpdated); |
| 120 | QObject::disconnect(previewStopped); |
| 121 | QObject::disconnect(previewStarted); |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 122 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 123 | previewStarted = QObject::connect(&Video::PreviewManager::instance(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 124 | &Video::PreviewManager::previewStarted, |
| 125 | [=](Video::Renderer* renderer) { |
| 126 | NSLog(@"Preview started"); |
| 127 | QObject::disconnect(frameUpdated); |
| 128 | frameUpdated = QObject::connect(renderer, |
| 129 | &Video::Renderer::frameUpdated, |
| 130 | [=]() { |
| 131 | [self renderer:Video::PreviewManager::instance().previewRenderer() renderFrameForView:previewView]; |
| 132 | }); |
| 133 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 134 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 135 | previewStopped = QObject::connect(&Video::PreviewManager::instance(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 136 | &Video::PreviewManager::previewStopped, |
| 137 | [=](Video::Renderer* renderer) { |
| 138 | NSLog(@"Preview stopped"); |
| 139 | QObject::disconnect(frameUpdated); |
| 140 | [previewView.layer setContents:nil]; |
| 141 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 142 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 143 | frameUpdated = QObject::connect(Video::PreviewManager::instance().previewRenderer(), |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 144 | &Video::Renderer::frameUpdated, |
| 145 | [=]() { |
| 146 | [self renderer:Video::PreviewManager::instance().previewRenderer() |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 147 | renderFrameForView:previewView]; |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 148 | }); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | -(void) renderer: (Video::Renderer*)renderer renderFrameForView:(NSView*) view |
| 152 | { |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 153 | QSize res = renderer->size(); |
| 154 | |
Alexandre Lision | 6731e13 | 2015-10-14 14:29:06 -0400 | [diff] [blame] | 155 | auto frame_ptr = renderer->currentFrame(); |
| 156 | auto frame_data = frame_ptr.ptr; |
| 157 | if (!frame_data) |
| 158 | return; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 159 | |
| 160 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
Alexandre Lision | 6731e13 | 2015-10-14 14:29:06 -0400 | [diff] [blame] | 161 | CGContextRef newContext = CGBitmapContextCreate(frame_data, |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 162 | res.width(), |
| 163 | res.height(), |
| 164 | 8, |
| 165 | 4*res.width(), |
| 166 | colorSpace, |
| 167 | kCGImageAlphaPremultipliedLast); |
| 168 | |
| 169 | |
| 170 | CGImageRef newImage = CGBitmapContextCreateImage(newContext); |
| 171 | |
| 172 | /*We release some components*/ |
| 173 | CGContextRelease(newContext); |
| 174 | CGColorSpaceRelease(colorSpace); |
| 175 | |
| 176 | [CATransaction begin]; |
| 177 | view.layer.contents = (__bridge id)newImage; |
| 178 | [CATransaction commit]; |
| 179 | |
| 180 | CFRelease(newImage); |
| 181 | } |
| 182 | |
Alexandre Lision | e78a09b | 2015-07-03 15:52:23 -0400 | [diff] [blame] | 183 | - (void) viewWillAppear |
| 184 | { |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 185 | // check if preview has to be started/stopped by this controller |
| 186 | self.shouldHandlePreview = !Video::PreviewManager::instance().previewRenderer()->isRendering(); |
| 187 | |
| 188 | [self connectPreviewSignals]; |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 189 | if (self.shouldHandlePreview) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 190 | Video::PreviewManager::instance().startPreview(); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 191 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | - (void)viewWillDisappear |
| 195 | { |
Alexandre Lision | d7b8546 | 2015-11-10 17:14:29 -0500 | [diff] [blame] | 196 | QObject::disconnect(frameUpdated); |
| 197 | QObject::disconnect(previewStopped); |
| 198 | QObject::disconnect(previewStarted); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 199 | if (self.shouldHandlePreview) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 200 | Video::PreviewManager::instance().stopPreview(); |
Alexandre Lision | e0c7e33 | 2015-07-30 11:11:33 -0400 | [diff] [blame] | 201 | } |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #pragma mark - NSMenuDelegate methods |
| 205 | |
| 206 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 207 | { |
| 208 | QModelIndex qIdx; |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 209 | if(self.videoDevicesList.menu == menu) { |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 210 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 211 | qIdx = Video::ConfigurationProxy::deviceModel().index(index, 0); |
| 212 | [item setTitle:Video::ConfigurationProxy::deviceModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 213 | |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 214 | } else if(self.sizesList.menu == menu) { |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 215 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 216 | qIdx = Video::ConfigurationProxy::resolutionModel().index(index, 0); |
| 217 | [item setTitle:Video::ConfigurationProxy::resolutionModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 218 | |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 219 | } else if(self.ratesList.menu == menu) { |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 220 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 221 | qIdx = Video::ConfigurationProxy::rateModel().index(index, 0); |
| 222 | [item setTitle:Video::ConfigurationProxy::rateModel().data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 223 | |
| 224 | } |
| 225 | return YES; |
| 226 | } |
| 227 | |
| 228 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 229 | { |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 230 | if(self.videoDevicesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 231 | return Video::ConfigurationProxy::deviceModel().rowCount(); |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 232 | } else if(self.sizesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 233 | return Video::ConfigurationProxy::resolutionModel().rowCount(); |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 234 | } else if(self.ratesList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 235 | return Video::ConfigurationProxy::rateModel().rowCount(); |
Alexandre Lision | b8a8116 | 2015-03-20 18:08:01 -0400 | [diff] [blame] | 236 | } |
Alexandre Lision | e1cf446 | 2015-10-08 15:44:38 -0400 | [diff] [blame] | 237 | return 0; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @end |