blob: dea96358fe36acfd8e8a3fdeb9d692a67bd9c2d2 [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 "videoformatresolutionmodel.h"
20
21VideoFormatResolutionModel::VideoFormatResolutionModel(QObject *parent)
22 : QAbstractListModel(parent)
23{}
24
25VideoFormatResolutionModel::~VideoFormatResolutionModel() {}
26
27int
28VideoFormatResolutionModel::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
57int
58VideoFormatResolutionModel::columnCount(const QModelIndex &parent) const
59{
60 Q_UNUSED(parent);
61 /*
62 * Only need one column.
63 */
64 return 1;
65}
66
67QVariant
68VideoFormatResolutionModel::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
98QHash<int, QByteArray>
99VideoFormatResolutionModel::roleNames() const
100{
101 QHash<int, QByteArray> roles;
102 roles[Resolution] = "Resolution";
103 roles[Resolution_UTF8] = "Resolution_UTF8";
104 return roles;
105}
106
107QModelIndex
108VideoFormatResolutionModel::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
121QModelIndex
122VideoFormatResolutionModel::parent(const QModelIndex &child) const
123{
124 Q_UNUSED(child);
125 return QModelIndex();
126}
127
128Qt::ItemFlags
129VideoFormatResolutionModel::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
138void
139VideoFormatResolutionModel::reset()
140{
141 beginResetModel();
142 endResetModel();
143}
144
145int
146VideoFormatResolutionModel::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}