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 "videocodeclistmodel.h" |
| 20 | |
| 21 | #include <QList> |
| 22 | |
| 23 | VideoCodecListModel::VideoCodecListModel(QObject *parent) |
| 24 | : QAbstractListModel(parent) |
| 25 | {} |
| 26 | |
| 27 | VideoCodecListModel::~VideoCodecListModel() {} |
| 28 | |
| 29 | int |
| 30 | VideoCodecListModel::rowCount(const QModelIndex &parent) const |
| 31 | { |
| 32 | if (!parent.isValid()) { |
| 33 | /* |
| 34 | * Count. |
| 35 | */ |
| 36 | QList<lrc::api::Codec> realCodecList; |
| 37 | auto videoCodecListOld = LRCInstance::getCurrentAccountInfo().codecModel->getVideoCodecs(); |
| 38 | |
| 39 | for (auto codec : videoCodecListOld) { |
| 40 | if (codec.name.length()) { |
| 41 | realCodecList.append(codec); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return realCodecList.size(); |
| 46 | } |
| 47 | /* |
| 48 | * A valid QModelIndex returns 0 as no entry has sub-elements. |
| 49 | */ |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | int |
| 54 | VideoCodecListModel::columnCount(const QModelIndex &parent) const |
| 55 | { |
| 56 | Q_UNUSED(parent); |
| 57 | /* |
| 58 | * Only need one column. |
| 59 | */ |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | QVariant |
| 64 | VideoCodecListModel::data(const QModelIndex &index, int role) const |
| 65 | { |
| 66 | auto videoCodecList = LRCInstance::getCurrentAccountInfo().codecModel->getVideoCodecs(); |
| 67 | if (!index.isValid() || videoCodecList.size() <= index.row()) { |
| 68 | return QVariant(); |
| 69 | } |
| 70 | |
| 71 | QList<lrc::api::Codec> realCodecList; |
| 72 | |
| 73 | for (auto codec : videoCodecList) { |
| 74 | if (codec.name.length()) { |
| 75 | realCodecList.append(codec); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | switch (role) { |
| 80 | case Role::VideoCodecName: |
| 81 | return QVariant(realCodecList.at(index.row()).name); |
| 82 | case Role::IsEnabled: |
| 83 | return QVariant(realCodecList.at(index.row()).enabled); |
| 84 | case Role::VideoCodecID: |
| 85 | return QVariant(realCodecList.at(index.row()).id); |
| 86 | } |
| 87 | return QVariant(); |
| 88 | } |
| 89 | |
| 90 | QHash<int, QByteArray> |
| 91 | VideoCodecListModel::roleNames() const |
| 92 | { |
| 93 | QHash<int, QByteArray> roles; |
| 94 | roles[VideoCodecName] = "VideoCodecName"; |
| 95 | roles[IsEnabled] = "IsEnabled"; |
| 96 | roles[VideoCodecID] = "VideoCodecID"; |
| 97 | return roles; |
| 98 | } |
| 99 | |
| 100 | QModelIndex |
| 101 | VideoCodecListModel::index(int row, int column, const QModelIndex &parent) const |
| 102 | { |
| 103 | Q_UNUSED(parent); |
| 104 | if (column != 0) { |
| 105 | return QModelIndex(); |
| 106 | } |
| 107 | |
| 108 | if (row >= 0 && row < rowCount()) { |
| 109 | return createIndex(row, column); |
| 110 | } |
| 111 | return QModelIndex(); |
| 112 | } |
| 113 | |
| 114 | QModelIndex |
| 115 | VideoCodecListModel::parent(const QModelIndex &child) const |
| 116 | { |
| 117 | Q_UNUSED(child); |
| 118 | return QModelIndex(); |
| 119 | } |
| 120 | |
| 121 | Qt::ItemFlags |
| 122 | VideoCodecListModel::flags(const QModelIndex &index) const |
| 123 | { |
| 124 | auto flags = QAbstractItemModel::flags(index) | Qt::ItemNeverHasChildren | Qt::ItemIsSelectable |
| 125 | | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled; |
| 126 | if (!index.isValid()) { |
| 127 | return QAbstractItemModel::flags(index); |
| 128 | } |
| 129 | return flags; |
| 130 | } |