1
/****************************************************************************
2
* Copyright (C) 2012-2014 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Lesser General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2.1 of the License, or (at your option) any later version. *
10
* This library 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 GNU *
13
* Lesser General Public License for more details. *
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
#include "videocodecmodel.h"
21
#include <video/videocodec.h>
22
#include "../dbus/videomanager.h"
24
#include <QtCore/QCoreApplication>
26
///Get data from the model
27
QVariant VideoCodecModel::data( const QModelIndex& idx, int role) const
29
if(idx.column() == 0 && role == Qt::DisplayRole)
30
return QVariant(m_lCodecs[idx.row()]->name());
31
else if(idx.column() == 0 && role == Qt::CheckStateRole) {
32
return QVariant(m_lCodecs[idx.row()]->isEnabled()?Qt::Checked:Qt::Unchecked);
34
else if (idx.column() == 0 && role == VideoCodecModel::BITRATE_ROLE)
35
return QVariant(m_lCodecs[idx.row()]->bitrate());
39
///The number of codec
40
int VideoCodecModel::rowCount( const QModelIndex& par ) const
43
return m_lCodecs.size();
47
Qt::ItemFlags VideoCodecModel::flags( const QModelIndex& idx ) const
49
if (idx.column() == 0)
50
return QAbstractItemModel::flags(idx) | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
51
return QAbstractItemModel::flags(idx);
54
///Set the codec data (codecs can't be added or removed that way)
55
bool VideoCodecModel::setData(const QModelIndex& idx, const QVariant &value, int role)
58
if (idx.column() == 0 && role == Qt::CheckStateRole) {
59
bool changed = m_lCodecs[idx.row()]->isEnabled() != (value == Qt::Checked);
60
m_lCodecs[idx.row()]->setEnabled(value == Qt::Checked);
62
emit dataChanged(idx, idx);
65
else if (idx.column() == 0 && role == VideoCodecModel::BITRATE_ROLE) {
66
bool changed = m_lCodecs[idx.row()]->bitrate() != value.toUInt();
67
m_lCodecs[idx.row()]->setBitrate(value.toInt());
69
emit dataChanged(idx, idx);
76
VideoCodecModel::VideoCodecModel(Account* account) : QAbstractListModel(QCoreApplication::instance()),m_pAccount(account)
82
VideoCodecModel::~VideoCodecModel()
84
while (m_lCodecs.size()) {
85
VideoCodec* c = m_lCodecs[0];
86
m_lCodecs.removeAt(0);
91
///Force a model reload from dbus
92
void VideoCodecModel::reload()
94
while (m_lCodecs.size()) {
95
VideoCodec* c = m_lCodecs[0];
96
m_lCodecs.removeAt(0);
99
VideoManagerInterface& interface = DBus::VideoManager::instance();
100
const VectorMapStringString codecs = interface.getCodecs(m_pAccount->id());
101
foreach(const MapStringString& h,codecs) {
102
VideoCodec* c = new VideoCodec(h[VideoCodec::CodecFields::NAME],
103
h[VideoCodec::CodecFields::BITRATE].toInt(),
104
h[VideoCodec::CodecFields::ENABLED]=="true");
105
c->setParamaters(h[VideoCodec::CodecFields::PARAMETERS]);
108
emit dataChanged(index(0,0), index(m_lCodecs.size()-1,0));
111
///Save the current model over dbus
112
void VideoCodecModel::save()
114
VideoManagerInterface& interface = DBus::VideoManager::instance();
115
VectorMapStringString toSave;
116
foreach(VideoCodec* vc,m_lCodecs) {
117
toSave << vc->toMap();
119
interface.setCodecs(m_pAccount->id(),toSave);
122
///Increase codec priority
123
bool VideoCodecModel::moveUp(QModelIndex idx)
125
if(idx.row() > 0 && idx.row() <= rowCount()) {
126
VideoCodec* data2 = m_lCodecs[idx.row()];
127
m_lCodecs.removeAt(idx.row());
128
m_lCodecs.insert(idx.row() - 1, data2);
129
emit dataChanged(index(idx.row() - 1, 0, QModelIndex()), index(idx.row(), 0, QModelIndex()));
135
///Decrease codec priority
136
bool VideoCodecModel::moveDown(QModelIndex idx)
138
if(idx.row() >= 0 && idx.row() < rowCount()) {
139
VideoCodec* data2 = m_lCodecs[idx.row()];
140
m_lCodecs.removeAt(idx.row());
141
m_lCodecs.insert(idx.row() + 1, data2);
142
emit dataChanged(index(idx.row(), 0, QModelIndex()), index(idx.row() + 1, 0, QModelIndex()));