Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -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 | f5fc479 | 2015-03-17 09:15:43 -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 | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 18 | */ |
| 19 | #define COLUMNID_STATE @"VideoStateColumn" |
| 20 | #define COLUMNID_CODECS @"VideoCodecsColumn" |
| 21 | #define COLUMNID_FREQ @"VideoFrequencyColumn" |
| 22 | #define COLUMNID_BITRATE @"VideoBitrateColumn" |
| 23 | |
| 24 | #import "AccVideoVC.h" |
| 25 | |
| 26 | #include <QtCore/QSortFilterProxyModel> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 27 | #import <audio/codecmodel.h> |
| 28 | #import <accountmodel.h> |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 29 | #import <qitemselectionmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 30 | |
| 31 | #import "QNSTreeController.h" |
| 32 | |
| 33 | @interface AccVideoVC () |
| 34 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 35 | @property QNSTreeController *treeController; |
| 36 | @property (assign) IBOutlet NSOutlineView *codecsView; |
| 37 | @property (assign) IBOutlet NSView *videoPanelContainer; |
| 38 | @property (assign) IBOutlet NSButton *toggleVideoButton; |
| 39 | |
| 40 | @end |
| 41 | |
| 42 | @implementation AccVideoVC |
| 43 | @synthesize treeController; |
| 44 | @synthesize codecsView; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 45 | @synthesize videoPanelContainer; |
| 46 | @synthesize toggleVideoButton; |
| 47 | |
| 48 | - (void)awakeFromNib |
| 49 | { |
| 50 | NSLog(@"INIT Video VC"); |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 51 | QObject::connect(AccountModel::instance().selectionModel(), |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 52 | &QItemSelectionModel::currentChanged, |
| 53 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 54 | if(!current.isValid()) |
| 55 | return; |
| 56 | [self loadAccount]; |
| 57 | }); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 60 | - (Account*) currentAccount |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 61 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 62 | auto accIdx = AccountModel::instance().selectionModel()->currentIndex(); |
| 63 | return AccountModel::instance().getAccountByModelIndex(accIdx); |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 64 | } |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 65 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 66 | - (void)loadAccount |
| 67 | { |
| 68 | auto account = [self currentAccount]; |
| 69 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 70 | treeController = [[QNSTreeController alloc] initWithQModel:account->codecModel()->videoCodecs()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 71 | [treeController setAvoidsEmptySelection:NO]; |
| 72 | [treeController setChildrenKeyPath:@"children"]; |
| 73 | |
| 74 | [codecsView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil]; |
| 75 | [codecsView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil]; |
| 76 | [codecsView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 77 | [videoPanelContainer setHidden:!account->isVideoEnabled()]; |
| 78 | [toggleVideoButton setState:account->isVideoEnabled()?NSOnState:NSOffState]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | - (IBAction)toggleVideoEnabled:(id)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 82 | [self currentAccount]->setVideoEnabled([sender state] == NSOnState); |
| 83 | [videoPanelContainer setHidden:![self currentAccount]->isVideoEnabled()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | - (IBAction)toggleCodec:(NSOutlineView*)sender { |
| 87 | NSInteger row = [sender clickedRow]; |
| 88 | NSTableColumn *col = [sender tableColumnWithIdentifier:COLUMNID_STATE]; |
| 89 | NSButtonCell *cell = [col dataCellForRow:row]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 90 | [self currentAccount]->codecModel()->videoCodecs()->setData([self currentAccount]->codecModel()->videoCodecs()->index(row, 0, QModelIndex()), |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 91 | cell.state == NSOnState ? Qt::Unchecked : Qt::Checked, Qt::CheckStateRole); |
| 92 | } |
| 93 | |
Alexandre Lision | e1e9e6a | 2015-03-20 17:51:21 -0400 | [diff] [blame] | 94 | - (IBAction)moveUp:(id)sender { |
| 95 | |
| 96 | if([[treeController selectedNodes] count] > 0) { |
| 97 | QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
| 98 | if(!qIdx.isValid()) |
| 99 | return; |
| 100 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 101 | QMimeData* mime = [self currentAccount]->codecModel()->mimeData(QModelIndexList() << qIdx); |
| 102 | [self currentAccount]->codecModel()->dropMimeData(mime, Qt::MoveAction, qIdx.row() - 1, 0, QModelIndex()); |
Alexandre Lision | e1e9e6a | 2015-03-20 17:51:21 -0400 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | - (IBAction)moveDown:(id)sender { |
| 107 | if([[treeController selectedNodes] count] > 0) { |
| 108 | QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]]; |
| 109 | if(!qIdx.isValid()) |
| 110 | return; |
| 111 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 112 | QMimeData* mime = [self currentAccount]->codecModel()->mimeData(QModelIndexList() << qIdx); |
| 113 | [self currentAccount]->codecModel()->dropMimeData(mime, Qt::MoveAction, qIdx.row() + 1, 0, QModelIndex()); |
Alexandre Lision | e1e9e6a | 2015-03-20 17:51:21 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 117 | #pragma mark - NSOutlineViewDelegate methods |
| 118 | |
| 119 | // ------------------------------------------------------------------------------- |
| 120 | // shouldSelectItem:item |
| 121 | // ------------------------------------------------------------------------------- |
| 122 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item; |
| 123 | { |
| 124 | return YES; |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------------- |
| 128 | // dataCellForTableColumn:tableColumn:item |
| 129 | // ------------------------------------------------------------------------------- |
| 130 | - (NSCell *)outlineView:(NSOutlineView *)outlineView dataCellForTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 131 | { |
| 132 | NSCell *returnCell = [tableColumn dataCell]; |
| 133 | |
| 134 | if(item == nil) |
| 135 | return returnCell; |
| 136 | return returnCell; |
| 137 | } |
| 138 | |
| 139 | // ------------------------------------------------------------------------------- |
| 140 | // textShouldEndEditing:fieldEditor |
| 141 | // ------------------------------------------------------------------------------- |
| 142 | - (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor |
| 143 | { |
| 144 | if ([[fieldEditor string] length] == 0) |
| 145 | { |
| 146 | // don't allow empty node names |
| 147 | return NO; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | return YES; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // ------------------------------------------------------------------------------- |
| 156 | // shouldEditTableColumn:tableColumn:item |
| 157 | // |
| 158 | // Decide to allow the edit of the given outline view "item". |
| 159 | // ------------------------------------------------------------------------------- |
| 160 | - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 161 | { |
| 162 | return NO; |
| 163 | } |
| 164 | |
| 165 | // ------------------------------------------------------------------------------- |
| 166 | // outlineView:willDisplayCell:forTableColumn:item |
| 167 | // ------------------------------------------------------------------------------- |
| 168 | - (void)outlineView:(NSOutlineView *)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item |
| 169 | { |
| 170 | QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)]; |
| 171 | if(!qIdx.isValid()) |
| 172 | return; |
| 173 | |
| 174 | if([[tableColumn identifier] isEqualToString:COLUMNID_STATE]) { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 175 | [cell setState:[self currentAccount]->codecModel()->videoCodecs()->data(qIdx, Qt::CheckStateRole).value<BOOL>()?NSOnState:NSOffState]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 176 | } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CODECS]) |
| 177 | { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 178 | cell.title = [self currentAccount]->codecModel()->videoCodecs()->data(qIdx, CodecModel::Role::NAME).toString().toNSString(); |
| 179 | [cell setState:[self currentAccount]->codecModel()->videoCodecs()->data(qIdx, Qt::CheckStateRole).value<BOOL>()?NSOnState:NSOffState]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 180 | } else if ([[tableColumn identifier] isEqualToString:COLUMNID_FREQ]) |
| 181 | { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 182 | cell.title = [self currentAccount]->codecModel()->videoCodecs()->data(qIdx, CodecModel::Role::SAMPLERATE).toString().toNSString(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 183 | } else if ([[tableColumn identifier] isEqualToString:COLUMNID_BITRATE]) |
| 184 | { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 185 | cell.title = [self currentAccount]->codecModel()->videoCodecs()->data(qIdx, CodecModel::Role::BITRATE).toString().toNSString(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | // ------------------------------------------------------------------------------- |
| 190 | // outlineViewSelectionDidChange:notification |
| 191 | // ------------------------------------------------------------------------------- |
| 192 | - (void)outlineViewSelectionDidChange:(NSNotification *)notification |
| 193 | { |
| 194 | // ask the tree controller for the current selection |
| 195 | } |
| 196 | |
| 197 | @end |