Edric Milaret | 864a205 | 2016-01-14 15:45:03 -0500 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2016 by Savoir-faire Linux * |
| 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>* |
| 4 | * Author: Stepan Salenikovich <stepan.salenikovich@savoirfairelinux.com> * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 3 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 18 | **************************************************************************/ |
| 19 | |
| 20 | #include "qualitydialog.h" |
| 21 | #include "ui_qualitydialog.h" |
| 22 | |
| 23 | #include "callmodel.h" |
| 24 | #include "account.h" |
| 25 | #include "audio/codecmodel.h" |
| 26 | |
| 27 | #include <QSortFilterProxyModel> |
| 28 | |
| 29 | QualityDialog::QualityDialog(QWidget *parent) : |
| 30 | QDialog(parent), |
| 31 | ui(new Ui::QualityDialog) |
| 32 | { |
| 33 | ui->setupUi(this); |
| 34 | |
| 35 | this->setWindowFlags(Qt::CustomizeWindowHint); |
| 36 | this->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup); |
| 37 | } |
| 38 | |
| 39 | QualityDialog::~QualityDialog() |
| 40 | { |
| 41 | delete ui; |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | QualityDialog::on_autoCheckBox_toggled(bool checked) |
| 46 | { |
| 47 | setQuality(); |
| 48 | ui->qualitySlider->setEnabled(not checked); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void |
| 53 | QualityDialog::on_qualitySlider_sliderReleased() |
| 54 | { |
| 55 | setQuality(); |
| 56 | } |
| 57 | |
| 58 | void QualityDialog::showEvent(QShowEvent* event) { |
| 59 | QWidget::showEvent(event); |
| 60 | ui->autoCheckBox->blockSignals(true); |
| 61 | const auto& call = CallModel::instance().selectedCall(); |
| 62 | if (const auto& codecModel = call->account()->codecModel()) { |
| 63 | const auto& videoCodecs = codecModel->videoCodecs(); |
| 64 | if (videoCodecs->rowCount() > 0) { |
| 65 | /* we only need to check the first codec since by default it is ON for all, and the |
| 66 | * client sets its ON or OFF for all codecs as well */ |
| 67 | const auto& idx = videoCodecs->index(0,0); |
| 68 | auto auto_quality_enabled = idx.data(static_cast<int>(CodecModel::Role::AUTO_QUALITY_ENABLED)).toString() == "true"; |
| 69 | ui->autoCheckBox->setChecked(auto_quality_enabled); |
| 70 | ui->qualitySlider->setEnabled(not auto_quality_enabled); |
| 71 | |
| 72 | // TODO: save the manual quality setting in the client and set the slider to that value here; |
| 73 | // the daemon resets the bitrate/quality between each call, and the default may be |
| 74 | // different for each codec, so there is no reason to check it here |
| 75 | } |
| 76 | } |
| 77 | ui->autoCheckBox->blockSignals(false); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | QualityDialog::setQuality() |
| 82 | { |
| 83 | /* set auto quality true or false, also set the bitrate and quality values; |
| 84 | * the slider is from 0 to 100, use the min and max vals to scale each value accordingly */ |
| 85 | const auto& call = CallModel::instance().selectedCall(); |
| 86 | if (const auto& codecModel = call->account()->codecModel()) { |
| 87 | const auto& videoCodecs = codecModel->videoCodecs(); |
| 88 | |
| 89 | for (int i=0; i < videoCodecs->rowCount();i++) { |
| 90 | const auto& idx = videoCodecs->index(i,0); |
| 91 | |
| 92 | if (ui->autoCheckBox->isChecked()) { |
| 93 | videoCodecs->setData(idx, "true", CodecModel::Role::AUTO_QUALITY_ENABLED); |
| 94 | } else { |
| 95 | auto min_bitrate = idx.data(static_cast<int>(CodecModel::Role::MIN_BITRATE)).toInt(); |
| 96 | auto max_bitrate = idx.data(static_cast<int>(CodecModel::Role::MAX_BITRATE)).toInt(); |
| 97 | auto min_quality = idx.data(static_cast<int>(CodecModel::Role::MIN_QUALITY)).toInt(); |
| 98 | auto max_quality = idx.data(static_cast<int>(CodecModel::Role::MAX_QUALITY)).toInt(); |
| 99 | |
| 100 | double bitrate; |
| 101 | bitrate = min_bitrate + (double)(max_bitrate - min_bitrate)*(ui->qualitySlider->value()/100.0); |
| 102 | if (bitrate < 0) bitrate = 0; |
| 103 | |
| 104 | double quality; |
| 105 | // note: a lower value means higher quality |
| 106 | quality = (double)min_quality - (min_quality - max_quality)*(ui->qualitySlider->value()/100.0); |
| 107 | if (quality < 0) quality = 0; |
| 108 | |
| 109 | videoCodecs->setData(idx, "false", CodecModel::Role::AUTO_QUALITY_ENABLED); |
| 110 | videoCodecs->setData(idx, QString::number((int)bitrate), CodecModel::Role::BITRATE); |
| 111 | videoCodecs->setData(idx, QString::number((int)quality), CodecModel::Role::QUALITY); |
| 112 | } |
| 113 | } |
| 114 | codecModel << CodecModel::EditAction::SAVE; |
| 115 | } |
| 116 | } |