AccAudioVC.mm 7.57 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *  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 "AccAudioVC.h"

21
///Qt
22
#import <QSortFilterProxyModel>
23 24 25
#import <qitemselectionmodel.h>

///LRC
26 27
#import <audio/codecmodel.h>
#import <accountmodel.h>
28 29
#import <ringtonemodel.h>
#import <ringtone.h>
30 31 32

@interface AccAudioVC ()

33 34 35 36 37
@property QNSTreeController* treeController;
@property (assign) IBOutlet NSOutlineView* codecsView;
@property (unsafe_unretained) IBOutlet NSPopUpButton* ringtonePopUpButton;
@property (unsafe_unretained) IBOutlet NSButton* enableRingtone;
@property (unsafe_unretained) IBOutlet NSButton* playRingtone;
38 39 40 41 42 43

@end

@implementation AccAudioVC
@synthesize treeController;
@synthesize codecsView;
44
@synthesize ringtonePopUpButton, enableRingtone, playRingtone;
45

46 47 48 49 50
NSInteger const TAG_CHECK       =   100;
NSInteger const TAG_NAME        =   200;
NSInteger const TAG_FREQUENCY   =   300;

- (void) loadView
51
{
52
    [super loadView];
53
    NSLog(@"INIT Audio VC");
54
    QObject::connect(AccountModel::instance().selectionModel(),
55 56 57 58 59 60
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;
                         [self loadAccount];
                     });
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

    QObject::connect(&RingtoneModel::instance(),
                     &QAbstractItemModel::dataChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;

                         NSString* label;
                         if (!RingtoneModel::instance().isPlaying()) {
                             label = NSLocalizedString(@"Play", @"Button label");
                         } else {
                             label = NSLocalizedString(@"Pause", @"Button label");
                         }
                         [playRingtone setTitle:label];
                     });
76 77
}

78 79
- (void)loadAccount
{
80
    auto account = AccountModel::instance().selectedAccount();
81
    treeController = [[QNSTreeController alloc] initWithQModel:account->codecModel()->audioCodecs()];
82 83 84 85 86 87 88

    [treeController setAvoidsEmptySelection:NO];
    [treeController setChildrenKeyPath:@"children"];

    [codecsView bind:@"content" toObject:treeController withKeyPath:@"arrangedObjects" options:nil];
    [codecsView bind:@"sortDescriptors" toObject:treeController withKeyPath:@"sortDescriptors" options:nil];
    [codecsView bind:@"selectionIndexPaths" toObject:treeController withKeyPath:@"selectionIndexPaths" options:nil];
89 90 91 92 93 94 95 96 97 98

    QModelIndex qIdx = RingtoneModel::instance().selectionModel(account)->currentIndex();
    [ringtonePopUpButton removeAllItems];
    [ringtonePopUpButton addItemWithTitle:RingtoneModel::instance().data(qIdx, Qt::DisplayRole).toString().toNSString()];

    [enableRingtone setState:account->isRingtoneEnabled()];
    [ringtonePopUpButton setEnabled:account->isRingtoneEnabled()];
}

- (IBAction)startStopRingtone:(id)sender {
99
    auto qIdx = RingtoneModel::instance().selectionModel(AccountModel::instance().selectedAccount())->currentIndex();
100 101 102 103
    RingtoneModel::instance().play(qIdx);
}

- (IBAction)toggleRingtoneEnabled:(id)sender {
104
    AccountModel::instance().selectedAccount()->setRingtoneEnabled([sender state]);
105 106 107 108 109 110
    [ringtonePopUpButton setEnabled:[sender state]];
}

- (IBAction)chooseRingtone:(id)sender {
    int index = [sender indexOfSelectedItem];
    QModelIndex qIdx = RingtoneModel::instance().index(index, 0);
111
    RingtoneModel::instance().selectionModel(AccountModel::instance().selectedAccount())->setCurrentIndex(qIdx, QItemSelectionModel::ClearAndSelect);
112 113 114
}

- (IBAction)moveUp:(id)sender {
115 116 117 118 119
    if([[treeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
        if(!qIdx.isValid())
            return;

120 121
        QMimeData* mime = AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->mimeData(QModelIndexList() << qIdx);
        AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->dropMimeData(mime, Qt::MoveAction, qIdx.row() - 1, 0, QModelIndex());
122
    }
123 124 125
}

- (IBAction)moveDown:(id)sender {
126 127 128 129 130
    if([[treeController selectedNodes] count] > 0) {
        QModelIndex qIdx = [treeController toQIdx:[treeController selectedNodes][0]];
        if(!qIdx.isValid())
            return;

131 132
        QMimeData* mime = AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->mimeData(QModelIndexList() << qIdx);
        AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->dropMimeData(mime, Qt::MoveAction, qIdx.row() + 1, 0, QModelIndex());
133
    }
134 135
}

136 137 138 139
- (IBAction)toggleCodec:(NSButton*)sender {
    NSInteger row = [codecsView rowForView:sender];
    QModelIndex qIdx = AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->index(row, 0, QModelIndex());
    AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->setData(qIdx, sender.state == NSOnState ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
140 141 142 143 144 145 146 147 148
}

#pragma mark - NSOutlineViewDelegate methods

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item;
{
    return YES;
}

149
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
150
{
151
    NSTableView* result = [outlineView makeViewWithIdentifier:@"CodecView" owner:self];
152 153 154

    QModelIndex qIdx = [treeController toQIdx:((NSTreeNode*)item)];
    if(!qIdx.isValid())
155 156 157 158 159 160 161 162 163
        return result;
    NSTextField* name = [result viewWithTag:TAG_NAME];
    NSTextField* frequency = [result viewWithTag:TAG_FREQUENCY];
    NSButton* check = [result viewWithTag:TAG_CHECK];

    [name setStringValue:AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->data(qIdx, CodecModel::Role::NAME).toString().toNSString()];
    [frequency setStringValue:[NSString stringWithFormat:@"%@ Hz", AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->data(qIdx, CodecModel::Role::SAMPLERATE).toString().toNSString()]];
    [check setState:AccountModel::instance().selectedAccount()->codecModel()->audioCodecs()->data(qIdx, Qt::CheckStateRole).value<BOOL>()?NSOnState:NSOffState];
    return result;
164 165
}

166 167 168
#pragma mark - NSMenuDelegate methods

- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
169
{
170 171 172 173
    QModelIndex qIdx;
    qIdx = RingtoneModel::instance().index(index, 0);
    [item setTitle:RingtoneModel::instance().data(qIdx, Qt::DisplayRole).toString().toNSString()];

174
    if (qIdx == RingtoneModel::instance().selectionModel(AccountModel::instance().selectedAccount())->currentIndex()) {
175 176 177 178
        [ringtonePopUpButton selectItem:item];
    }
    return YES;
}
179

180 181 182
- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
{
    return RingtoneModel::instance().rowCount();
183 184 185
}

@end