Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019-2020 by Savoir-faire Linux |
| 3 | * Author: Yang Wang <yang.wang@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, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "videoformatresolutionmodel.h" |
| 20 | |
| 21 | VideoFormatResolutionModel::VideoFormatResolutionModel(QObject *parent) |
| 22 | : QAbstractListModel(parent) |
| 23 | {} |
| 24 | |
| 25 | VideoFormatResolutionModel::~VideoFormatResolutionModel() {} |
| 26 | |
| 27 | int |
| 28 | VideoFormatResolutionModel::rowCount(const QModelIndex &parent) const |
| 29 | { |
| 30 | if (!parent.isValid()) { |
| 31 | /* |
| 32 | * Count. |
| 33 | */ |
| 34 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 35 | auto deviceCapabilities = LRCInstance::avModel().getDeviceCapabilities(currentDeviceId); |
| 36 | if (deviceCapabilities.size() == 0) { |
| 37 | return 0; |
| 38 | } |
| 39 | try { |
| 40 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 41 | auto currentChannel = currentSettings.channel; |
| 42 | currentChannel = currentChannel.isEmpty() ? "default" : currentChannel; |
| 43 | auto channelCaps = deviceCapabilities[currentChannel]; |
| 44 | |
| 45 | return channelCaps.size(); |
| 46 | } catch (const std::exception &e) { |
| 47 | qWarning() << e.what(); |
| 48 | return 0; |
| 49 | } |
| 50 | } |
| 51 | /* |
| 52 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 53 | */ |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | int |
| 58 | VideoFormatResolutionModel::columnCount(const QModelIndex &parent) const |
| 59 | { |
| 60 | Q_UNUSED(parent); |
| 61 | /* |
| 62 | * Only need one column. |
| 63 | */ |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | QVariant |
| 68 | VideoFormatResolutionModel::data(const QModelIndex &index, int role) const |
| 69 | { |
| 70 | try { |
| 71 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 72 | auto deviceCapabilities = LRCInstance::avModel().getDeviceCapabilities(currentDeviceId); |
| 73 | |
| 74 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 75 | auto currentChannel = currentSettings.channel; |
| 76 | currentChannel = currentChannel.isEmpty() ? "default" : currentChannel; |
| 77 | auto channelCaps = deviceCapabilities[currentChannel]; |
| 78 | |
| 79 | if (!index.isValid() || channelCaps.size() <= index.row() |
| 80 | || deviceCapabilities.size() == 0) { |
| 81 | return QVariant(); |
| 82 | } |
| 83 | |
| 84 | switch (role) { |
| 85 | case Role::Resolution: |
| 86 | return QVariant(channelCaps.at(index.row()).first); |
| 87 | case Role::Resolution_UTF8: |
| 88 | return QVariant(channelCaps.at(index.row()).first.toUtf8()); |
| 89 | } |
| 90 | |
| 91 | } catch (const std::exception &e) { |
| 92 | qWarning() << e.what(); |
| 93 | } |
| 94 | |
| 95 | return QVariant(); |
| 96 | } |
| 97 | |
| 98 | QHash<int, QByteArray> |
| 99 | VideoFormatResolutionModel::roleNames() const |
| 100 | { |
| 101 | QHash<int, QByteArray> roles; |
| 102 | roles[Resolution] = "Resolution"; |
| 103 | roles[Resolution_UTF8] = "Resolution_UTF8"; |
| 104 | return roles; |
| 105 | } |
| 106 | |
| 107 | QModelIndex |
| 108 | VideoFormatResolutionModel::index(int row, int column, const QModelIndex &parent) const |
| 109 | { |
| 110 | Q_UNUSED(parent); |
| 111 | if (column != 0) { |
| 112 | return QModelIndex(); |
| 113 | } |
| 114 | |
| 115 | if (row >= 0 && row < rowCount()) { |
| 116 | return createIndex(row, column); |
| 117 | } |
| 118 | return QModelIndex(); |
| 119 | } |
| 120 | |
| 121 | QModelIndex |
| 122 | VideoFormatResolutionModel::parent(const QModelIndex &child) const |
| 123 | { |
| 124 | Q_UNUSED(child); |
| 125 | return QModelIndex(); |
| 126 | } |
| 127 | |
| 128 | Qt::ItemFlags |
| 129 | VideoFormatResolutionModel::flags(const QModelIndex &index) const |
| 130 | { |
| 131 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 132 | if (!index.isValid()) { |
| 133 | return QAbstractItemModel::flags(index); |
| 134 | } |
| 135 | return flags; |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | VideoFormatResolutionModel::reset() |
| 140 | { |
| 141 | beginResetModel(); |
| 142 | endResetModel(); |
| 143 | } |
| 144 | |
| 145 | int |
| 146 | VideoFormatResolutionModel::getCurrentSettingIndex() |
| 147 | { |
| 148 | int resultRowIndex = 0; |
| 149 | try { |
| 150 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 151 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 152 | QString currentResolution = currentSettings.size; |
| 153 | auto resultList = match(index(0, 0), Resolution, QVariant(currentResolution)); |
| 154 | |
| 155 | if (resultList.size() > 0) { |
| 156 | resultRowIndex = resultList[0].row(); |
| 157 | } |
| 158 | |
| 159 | } catch (const std::exception &e) { |
| 160 | qWarning() << e.what(); |
| 161 | } |
| 162 | |
| 163 | return resultRowIndex; |
| 164 | } |