blob: 3ff97a9e9525345b6a12021bf245b76210292fd7 [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04003 * 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 Lisionf5fc4792015-03-17 09:15:43 -040018 */
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 Lision91d11e52015-03-20 17:42:05 -040027#import <audio/codecmodel.h>
28#import <accountmodel.h>
Alexandre Lision7f3164c2015-06-12 11:45:37 -040029#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040030
31#import "QNSTreeController.h"
32
33@interface AccVideoVC ()
34
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040035@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 Lisionf5fc4792015-03-17 09:15:43 -040045@synthesize videoPanelContainer;
46@synthesize toggleVideoButton;
47
48- (void)awakeFromNib
49{
50 NSLog(@"INIT Video VC");
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040051 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040052 &QItemSelectionModel::currentChanged,
53 [=](const QModelIndex &current, const QModelIndex &previous) {
54 if(!current.isValid())
55 return;
56 [self loadAccount];
57 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040058}
59
Alexandre Lision7f3164c2015-06-12 11:45:37 -040060- (Account*) currentAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040061{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040062 auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
63 return AccountModel::instance().getAccountByModelIndex(accIdx);
Alexandre Lision7f3164c2015-06-12 11:45:37 -040064}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040065
Alexandre Lision7f3164c2015-06-12 11:45:37 -040066- (void)loadAccount
67{
68 auto account = [self currentAccount];
69
Alexandre Lision81c97212015-06-17 15:51:53 -040070 treeController = [[QNSTreeController alloc] initWithQModel:account->codecModel()->videoCodecs()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040071 [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 Lision81c97212015-06-17 15:51:53 -040077 [videoPanelContainer setHidden:!account->isVideoEnabled()];
78 [toggleVideoButton setState:account->isVideoEnabled()?NSOnState:NSOffState];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040079}
80
81- (IBAction)toggleVideoEnabled:(id)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -040082 [self currentAccount]->setVideoEnabled([sender state] == NSOnState);
83 [videoPanelContainer setHidden:![self currentAccount]->isVideoEnabled()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040084}
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 Lision7f3164c2015-06-12 11:45:37 -040090 [self currentAccount]->codecModel()->videoCodecs()->setData([self currentAccount]->codecModel()->videoCodecs()->index(row, 0, QModelIndex()),
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040091 cell.state == NSOnState ? Qt::Unchecked : Qt::Checked, Qt::CheckStateRole);
92}
93
Alexandre Lisione1e9e6a2015-03-20 17:51:21 -040094- (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 Lision7f3164c2015-06-12 11:45:37 -0400101 QMimeData* mime = [self currentAccount]->codecModel()->mimeData(QModelIndexList() << qIdx);
102 [self currentAccount]->codecModel()->dropMimeData(mime, Qt::MoveAction, qIdx.row() - 1, 0, QModelIndex());
Alexandre Lisione1e9e6a2015-03-20 17:51:21 -0400103 }
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 Lision7f3164c2015-06-12 11:45:37 -0400112 QMimeData* mime = [self currentAccount]->codecModel()->mimeData(QModelIndexList() << qIdx);
113 [self currentAccount]->codecModel()->dropMimeData(mime, Qt::MoveAction, qIdx.row() + 1, 0, QModelIndex());
Alexandre Lisione1e9e6a2015-03-20 17:51:21 -0400114 }
115}
116
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400117#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 Lision7f3164c2015-06-12 11:45:37 -0400175 [cell setState:[self currentAccount]->codecModel()->videoCodecs()->data(qIdx, Qt::CheckStateRole).value<BOOL>()?NSOnState:NSOffState];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400176 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_CODECS])
177 {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400178 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 Lisionf5fc4792015-03-17 09:15:43 -0400180 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_FREQ])
181 {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400182 cell.title = [self currentAccount]->codecModel()->videoCodecs()->data(qIdx, CodecModel::Role::SAMPLERATE).toString().toNSString();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400183 } else if ([[tableColumn identifier] isEqualToString:COLUMNID_BITRATE])
184 {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400185 cell.title = [self currentAccount]->codecModel()->videoCodecs()->data(qIdx, CodecModel::Role::BITRATE).toString().toNSString();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400186 }
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