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 "videoinputdevicemodel.h" |
| 20 | |
| 21 | VideoInputDeviceModel::VideoInputDeviceModel(QObject *parent) |
| 22 | : QAbstractListModel(parent) |
| 23 | {} |
| 24 | |
| 25 | VideoInputDeviceModel::~VideoInputDeviceModel() {} |
| 26 | |
| 27 | int |
| 28 | VideoInputDeviceModel::rowCount(const QModelIndex &parent) const |
| 29 | { |
| 30 | if (!parent.isValid()) { |
| 31 | /* |
| 32 | * Count. |
| 33 | */ |
| 34 | int deviceListSize = LRCInstance::avModel().getDevices().size(); |
| 35 | if (deviceListSize > 0) { |
| 36 | return deviceListSize; |
| 37 | } else { |
| 38 | return 1; |
| 39 | } |
| 40 | } |
| 41 | /* |
| 42 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 43 | */ |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | int |
| 48 | VideoInputDeviceModel::columnCount(const QModelIndex &parent) const |
| 49 | { |
| 50 | Q_UNUSED(parent); |
| 51 | /* |
| 52 | * Only need one column. |
| 53 | */ |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | QVariant |
| 58 | VideoInputDeviceModel::data(const QModelIndex &index, int role) const |
| 59 | { |
| 60 | auto deviceList = LRCInstance::avModel().getDevices(); |
| 61 | if (!index.isValid()) { |
| 62 | return QVariant(); |
| 63 | } |
| 64 | |
| 65 | if (deviceList.size() == 0 && index.row() == 0) { |
| 66 | switch (role) { |
| 67 | case Role::DeviceName: |
| 68 | return QVariant(QObject::tr("No Device")); |
| 69 | case Role::DeviceName_UTF8: |
| 70 | return QVariant(QObject::tr("No Device").toUtf8()); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (deviceList.size() <= index.row()) { |
| 75 | return QVariant(); |
| 76 | } |
| 77 | |
| 78 | auto currentDeviceSetting = LRCInstance::avModel().getDeviceSettings(deviceList[index.row()]); |
| 79 | |
| 80 | switch (role) { |
| 81 | case Role::DeviceChannel: |
| 82 | return QVariant((QString) currentDeviceSetting.channel); |
| 83 | case Role::DeviceName: |
| 84 | return QVariant(currentDeviceSetting.name); |
| 85 | case Role::DeviceId: |
| 86 | return QVariant(currentDeviceSetting.id); |
| 87 | case Role::CurrentFrameRate: |
| 88 | return QVariant((float) currentDeviceSetting.rate); |
| 89 | case Role::CurrentResolution: |
| 90 | return QVariant((QString) currentDeviceSetting.size); |
| 91 | case Role::DeviceName_UTF8: |
| 92 | return QVariant(currentDeviceSetting.name.toUtf8()); |
| 93 | } |
| 94 | return QVariant(); |
| 95 | } |
| 96 | |
| 97 | QHash<int, QByteArray> |
| 98 | VideoInputDeviceModel::roleNames() const |
| 99 | { |
| 100 | QHash<int, QByteArray> roles; |
| 101 | roles[DeviceChannel] = "DeviceChannel"; |
| 102 | roles[DeviceName] = "DeviceName"; |
| 103 | roles[DeviceId] = "DeviceId"; |
| 104 | roles[CurrentFrameRate] = "CurrentFrameRate"; |
| 105 | roles[CurrentResolution] = "CurrentResolution"; |
| 106 | roles[DeviceName_UTF8] = "DeviceName_UTF8"; |
| 107 | return roles; |
| 108 | } |
| 109 | |
| 110 | QModelIndex |
| 111 | VideoInputDeviceModel::index(int row, int column, const QModelIndex &parent) const |
| 112 | { |
| 113 | Q_UNUSED(parent); |
| 114 | if (column != 0) { |
| 115 | return QModelIndex(); |
| 116 | } |
| 117 | |
| 118 | if (row >= 0 && row < rowCount()) { |
| 119 | return createIndex(row, column); |
| 120 | } |
| 121 | return QModelIndex(); |
| 122 | } |
| 123 | |
| 124 | QModelIndex |
| 125 | VideoInputDeviceModel::parent(const QModelIndex &child) const |
| 126 | { |
| 127 | Q_UNUSED(child); |
| 128 | return QModelIndex(); |
| 129 | } |
| 130 | |
| 131 | Qt::ItemFlags |
| 132 | VideoInputDeviceModel::flags(const QModelIndex &index) const |
| 133 | { |
| 134 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable; |
| 135 | if (!index.isValid()) { |
| 136 | return QAbstractItemModel::flags(index); |
| 137 | } |
| 138 | return flags; |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | VideoInputDeviceModel::reset() |
| 143 | { |
| 144 | beginResetModel(); |
| 145 | endResetModel(); |
| 146 | } |
| 147 | |
| 148 | int |
| 149 | VideoInputDeviceModel::deviceCount() |
| 150 | { |
| 151 | return LRCInstance::avModel().getDevices().size(); |
| 152 | } |
| 153 | |
| 154 | int |
| 155 | VideoInputDeviceModel::getCurrentSettingIndex() |
| 156 | { |
| 157 | QString currentId = LRCInstance::avModel().getCurrentVideoCaptureDevice(); |
| 158 | auto resultList = match(index(0, 0), DeviceId, QVariant(currentId)); |
| 159 | |
| 160 | int resultRowIndex = 0; |
| 161 | if (resultList.size() > 0) { |
| 162 | resultRowIndex = resultList[0].row(); |
| 163 | } |
| 164 | |
| 165 | return resultRowIndex; |
| 166 | } |