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 "videoformatfpsmodel.h" |
| 20 | |
| 21 | VideoFormatFpsModel::VideoFormatFpsModel(QObject *parent) |
| 22 | : QAbstractListModel(parent) |
| 23 | { |
| 24 | try { |
| 25 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 26 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 27 | currentResolution_ = currentSettings.size; |
| 28 | } catch (const std::exception &e) { |
| 29 | qWarning() << "Constructor of VideoFormatFpsModel, exception: " << e.what(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | VideoFormatFpsModel::~VideoFormatFpsModel() {} |
| 34 | |
| 35 | int |
| 36 | VideoFormatFpsModel::rowCount(const QModelIndex &parent) const |
| 37 | { |
| 38 | if (!parent.isValid()) { |
| 39 | /* |
| 40 | * Count. |
| 41 | */ |
| 42 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 43 | auto deviceCapabilities = LRCInstance::avModel().getDeviceCapabilities(currentDeviceId); |
| 44 | if (deviceCapabilities.size() == 0) { |
| 45 | return 0; |
| 46 | } |
| 47 | try { |
| 48 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 49 | auto currentChannel = currentSettings.channel; |
| 50 | currentChannel = currentChannel.isEmpty() ? "default" : currentChannel; |
| 51 | auto channelCaps = deviceCapabilities[currentChannel]; |
| 52 | |
| 53 | bool resolutionFound = false; |
| 54 | int indexOfCurrentResolutionInResRateList = 0; |
| 55 | |
| 56 | for (int i = 0; i < channelCaps.size(); i++) { |
| 57 | if (channelCaps[i].first == currentResolution_) { |
| 58 | indexOfCurrentResolutionInResRateList = i; |
| 59 | resolutionFound = true; |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (resolutionFound) { |
| 65 | auto fpsList = channelCaps[indexOfCurrentResolutionInResRateList].second; |
| 66 | return fpsList.size(); |
| 67 | } |
| 68 | } catch (const std::exception &e) { |
| 69 | qWarning() << e.what(); |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | /* |
| 74 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 75 | */ |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | int |
| 80 | VideoFormatFpsModel::columnCount(const QModelIndex &parent) const |
| 81 | { |
| 82 | Q_UNUSED(parent); |
| 83 | /* |
| 84 | * Only need one column. |
| 85 | */ |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | QVariant |
| 90 | VideoFormatFpsModel::data(const QModelIndex &index, int role) const |
| 91 | { |
| 92 | try { |
| 93 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 94 | auto deviceCapabilities = LRCInstance::avModel().getDeviceCapabilities(currentDeviceId); |
| 95 | |
| 96 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 97 | auto currentChannel = currentSettings.channel; |
| 98 | currentChannel = currentChannel.isEmpty() ? "default" : currentChannel; |
| 99 | auto channelCaps = deviceCapabilities[currentChannel]; |
| 100 | |
| 101 | bool resolutionFound = false; |
| 102 | int indexOfCurrentResolutionInResRateList = 0; |
| 103 | |
| 104 | for (int i = 0; i < channelCaps.size(); i++) { |
| 105 | if (channelCaps[i].first == currentResolution_) { |
| 106 | indexOfCurrentResolutionInResRateList = i; |
| 107 | resolutionFound = true; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!index.isValid() || channelCaps.size() <= index.row() || deviceCapabilities.size() == 0 |
| 113 | || !resolutionFound) { |
| 114 | return QVariant(); |
| 115 | } |
| 116 | |
| 117 | auto fpsList = channelCaps[indexOfCurrentResolutionInResRateList].second; |
| 118 | |
| 119 | switch (role) { |
| 120 | case Role::FPS: |
| 121 | return QVariant(fpsList[index.row()]); |
| 122 | case Role::FPS_ToDisplay_UTF8: |
| 123 | QString rateDisplayUtf8 = QString("%1 fps").arg((int) fpsList[index.row()]); |
| 124 | return QVariant(rateDisplayUtf8.toUtf8()); |
| 125 | } |
| 126 | |
| 127 | } catch (const std::exception &e) { |
| 128 | qWarning() << e.what(); |
| 129 | } |
| 130 | |
| 131 | return QVariant(); |
| 132 | } |
| 133 | |
| 134 | QHash<int, QByteArray> |
| 135 | VideoFormatFpsModel::roleNames() const |
| 136 | { |
| 137 | QHash<int, QByteArray> roles; |
| 138 | roles[FPS] = "FPS"; |
| 139 | roles[FPS_ToDisplay_UTF8] = "FPS_ToDisplay_UTF8"; |
| 140 | return roles; |
| 141 | } |
| 142 | |
| 143 | QModelIndex |
| 144 | VideoFormatFpsModel::index(int row, int column, const QModelIndex &parent) const |
| 145 | { |
| 146 | Q_UNUSED(parent); |
| 147 | if (column != 0) { |
| 148 | return QModelIndex(); |
| 149 | } |
| 150 | |
| 151 | if (row >= 0 && row < rowCount()) { |
| 152 | return createIndex(row, column); |
| 153 | } |
| 154 | return QModelIndex(); |
| 155 | } |
| 156 | |
| 157 | QModelIndex |
| 158 | VideoFormatFpsModel::parent(const QModelIndex &child) const |
| 159 | { |
| 160 | Q_UNUSED(child); |
| 161 | return QModelIndex(); |
| 162 | } |
| 163 | |
| 164 | Qt::ItemFlags |
| 165 | VideoFormatFpsModel::flags(const QModelIndex &index) const |
| 166 | { |
| 167 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 168 | if (!index.isValid()) { |
| 169 | return QAbstractItemModel::flags(index); |
| 170 | } |
| 171 | return flags; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | VideoFormatFpsModel::reset() |
| 176 | { |
| 177 | beginResetModel(); |
| 178 | endResetModel(); |
| 179 | } |
| 180 | |
| 181 | int |
| 182 | VideoFormatFpsModel::getCurrentSettingIndex() |
| 183 | { |
| 184 | int resultRowIndex = 0; |
| 185 | try { |
| 186 | QString currentDeviceId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 187 | auto currentSettings = LRCInstance::avModel().getDeviceSettings(currentDeviceId); |
| 188 | float currentFps = currentSettings.rate; |
| 189 | auto resultList = match(index(0, 0), FPS, QVariant(currentFps)); |
| 190 | |
| 191 | if (resultList.size() > 0) { |
| 192 | resultRowIndex = resultList[0].row(); |
| 193 | } |
| 194 | |
| 195 | } catch (const std::exception &e) { |
| 196 | qWarning() << e.what(); |
| 197 | } |
| 198 | |
| 199 | return resultRowIndex; |
| 200 | } |
| 201 | |
| 202 | QString |
| 203 | VideoFormatFpsModel::getCurrentResolution() |
| 204 | { |
| 205 | return currentResolution_; |
| 206 | } |
| 207 | |
| 208 | void |
| 209 | VideoFormatFpsModel::setCurrentResolution(QString resNew) |
| 210 | { |
| 211 | if (currentResolution_ != resNew) { |
| 212 | currentResolution_ = resNew; |
| 213 | reset(); |
| 214 | emit currentResolutionChanged(resNew); |
| 215 | } |
| 216 | } |