2
Copyright (C) 2012 Dan Vratil <dvratil@redhat.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Lesser General Public
6
License as published by the Free Software Foundation; either
7
version 2.1 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public
15
License along with this library; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
#include "modesproxymodel.h"
20
#include "qmloutput.h"
24
#include <KLocalizedString>
26
ModesProxyModel::ModesProxyModel(QObject* parent):
27
QSortFilterProxyModel(parent),
28
m_currentSourceRow(-1)
30
setSortRole(QMLOutput::RefreshRateRole);
31
setDynamicSortFilter(true);
34
ModesProxyModel::~ModesProxyModel()
39
int ModesProxyModel::columnCount(const QModelIndex& parent) const
44
int ModesProxyModel::rowCount(const QModelIndex& parent) const
50
QModelIndex parentIndex = sourceModel()->index(m_currentSourceRow, 0);
51
return sourceModel()->rowCount(parentIndex) + 1;
54
QVariant ModesProxyModel::data(const QModelIndex& index, int role) const
56
if (!sourceModel() || !index.isValid()) {
60
if (index.row() == 0) {
61
if (role == Qt::DisplayRole) {
63
} else if (role == QMLOutput::RefreshRateRole) {
65
} else if (role == QMLOutput::ModeIdRole) {
72
QModelIndex parentIndex = sourceModel()->index(m_currentSourceRow, 0);
73
QModelIndex realIndex = parentIndex.child(index.row() - 1, 0);
74
return sourceModel()->data(realIndex, role);
77
QModelIndex ModesProxyModel::index(int row, int column, const QModelIndex& parent) const
79
return createIndex(row, column, 0);
82
QModelIndex ModesProxyModel::parent(const QModelIndex& child) const
84
/* Flatten the model */
88
QModelIndex ModesProxyModel::mapToSource(const QModelIndex& proxyIndex) const
90
if (!sourceModel() || !proxyIndex.isValid()) {
94
QModelIndex realParentIndex = sourceModel()->index(m_currentSourceRow, 0);
96
if (proxyIndex.row() == 0) {
97
return realParentIndex.child(proxyIndex.row(), 0);
100
return realParentIndex.child(proxyIndex.row() - 1, 0);
103
bool ModesProxyModel::lessThan(const QModelIndex& left, const QModelIndex& right) const
105
float rateA = left.data(QMLOutput::RefreshRateRole).toFloat();
106
float rateB = right.data(QMLOutput::RefreshRateRole).toFloat();
112
return (rateA < rateB);
115
void ModesProxyModel::setSourceModelCurrentRow(int index)
117
m_currentSourceRow = index;