blob: 4c5fe15ee26e55e6143b02f2818a8cc0532109c4 [file] [log] [blame]
Sébastien Blin1f915762020-08-03 13:27:42 -04001/*
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
21VideoFormatFpsModel::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
33VideoFormatFpsModel::~VideoFormatFpsModel() {}
34
35int
36VideoFormatFpsModel::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
79int
80VideoFormatFpsModel::columnCount(const QModelIndex &parent) const
81{
82 Q_UNUSED(parent);
83 /*
84 * Only need one column.
85 */
86 return 1;
87}
88
89QVariant
90VideoFormatFpsModel::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
134QHash<int, QByteArray>
135VideoFormatFpsModel::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
143QModelIndex
144VideoFormatFpsModel::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
157QModelIndex
158VideoFormatFpsModel::parent(const QModelIndex &child) const
159{
160 Q_UNUSED(child);
161 return QModelIndex();
162}
163
164Qt::ItemFlags
165VideoFormatFpsModel::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
174void
175VideoFormatFpsModel::reset()
176{
177 beginResetModel();
178 endResetModel();
179}
180
181int
182VideoFormatFpsModel::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
202QString
203VideoFormatFpsModel::getCurrentResolution()
204{
205 return currentResolution_;
206}
207
208void
209VideoFormatFpsModel::setCurrentResolution(QString resNew)
210{
211 if (currentResolution_ != resNew) {
212 currentResolution_ = resNew;
213 reset();
214 emit currentResolutionChanged(resNew);
215 }
216}